difftreelog
feat experimental null-coaelse operator
in: master
6 files changed
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -24,6 +24,8 @@
exp-object-iteration = []
# Bigint type
exp-bigint = ["num-bigint", "jrsonnet-types/exp-bigint"]
+# obj?.field, obj?.['field']
+exp-null-coaelse = ["jrsonnet-parser/exp-null-coaelse"]
# Improves performance, and implements some useful things using nightly-only features
nightly = ["hashbrown/nightly"]
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth1use std::rc::Rc;23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6 ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, FieldName, ForSpecData,7 IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use self::destructure::destruct;12use crate::{13 arr::ArrValue,14 destructure::evaluate_dest,15 error::{suggest_object_fields, ErrorKind::*},16 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},17 function::{CallLocation, FuncDesc, FuncVal},18 throw,19 typed::Typed,20 val::{CachedUnbound, IndexableVal, StrValue, Thunk, ThunkValue},21 Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,22 Unbound, Val,23};24pub mod destructure;25pub mod operator;2627pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {28 fn is_trivial(expr: &LocExpr) -> bool {29 match &*expr.0 {30 Expr::Str(_)31 | Expr::Num(_)32 | Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,33 Expr::Arr(a) => a.iter().all(is_trivial),34 Expr::Parened(e) => is_trivial(e),35 _ => false,36 }37 }38 Some(match &*expr.0 {39 Expr::Str(s) => Val::Str(StrValue::Flat(s.clone())),40 Expr::Num(n) => Val::Num(*n),41 Expr::Literal(LiteralType::False) => Val::Bool(false),42 Expr::Literal(LiteralType::True) => Val::Bool(true),43 Expr::Literal(LiteralType::Null) => Val::Null,44 Expr::Arr(n) => {45 if n.iter().any(|e| !is_trivial(e)) {46 return None;47 }48 Val::Arr(ArrValue::eager(49 n.iter()50 .map(evaluate_trivial)51 .map(|e| e.expect("checked trivial"))52 .collect(),53 ))54 }55 Expr::Parened(e) => evaluate_trivial(e)?,56 _ => return None,57 })58}5960pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {61 Val::Func(FuncVal::Normal(Cc::new(FuncDesc {62 name,63 ctx,64 params,65 body,66 })))67}6869pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {70 Ok(match field_name {71 FieldName::Fixed(n) => Some(n.clone()),72 FieldName::Dyn(expr) => State::push(73 CallLocation::new(&expr.1),74 || "evaluating field name".to_string(),75 || {76 let value = evaluate(ctx, expr)?;77 if matches!(value, Val::Null) {78 Ok(None)79 } else {80 Ok(Some(IStr::from_untyped(value)?))81 }82 },83 )?,84 })85}8687pub fn evaluate_comp(88 ctx: Context,89 specs: &[CompSpec],90 callback: &mut impl FnMut(Context) -> Result<()>,91) -> Result<()> {92 match specs.get(0) {93 None => callback(ctx)?,94 Some(CompSpec::IfSpec(IfSpecData(cond))) => {95 if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {96 evaluate_comp(ctx, &specs[1..], callback)?;97 }98 }99 Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(ctx.clone(), expr)? {100 Val::Arr(list) => {101 for item in list.iter_lazy() {102 let fctx = Pending::new();103 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());104 destruct(var, item, fctx.clone(), &mut new_bindings)?;105 let ctx = ctx106 .clone()107 .extend(new_bindings, None, None, None)108 .into_future(fctx);109110 evaluate_comp(ctx, &specs[1..], callback)?;111 }112 }113 #[cfg(feature = "exp-object-iteration")]114 Val::Obj(obj) => {115 for field in obj.fields(116 // TODO: Should there be ability to preserve iteration order?117 #[cfg(feature = "exp-preserve-order")]118 false,119 ) {120 #[derive(Trace)]121 struct ObjectFieldThunk {122 obj: ObjValue,123 field: IStr,124 }125 impl ThunkValue for ObjectFieldThunk {126 type Output = Val;127128 fn get(self: Box<Self>) -> Result<Self::Output> {129 self.obj.get(self.field).transpose().expect(130 "field exists, as field name was obtained from object.fields()",131 )132 }133 }134135 let fctx = Pending::new();136 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());137 let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(Cc::new(vec![138 Thunk::evaluated(Val::Str(StrValue::Flat(field.clone()))),139 Thunk::new(ObjectFieldThunk {140 field: field.clone(),141 obj: obj.clone(),142 }),143 ]))));144 destruct(var, value, fctx.clone(), &mut new_bindings)?;145 let ctx = ctx146 .clone()147 .extend(new_bindings, None, None, None)148 .into_future(fctx);149150 evaluate_comp(ctx, &specs[1..], callback)?;151 }152 }153 _ => throw!(InComprehensionCanOnlyIterateOverArray),154 },155 }156 Ok(())157}158159trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}160impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}161162fn evaluate_object_locals(163 fctx: Pending<Context>,164 locals: Rc<Vec<BindSpec>>,165) -> impl CloneableUnbound<Context> {166 #[derive(Trace, Clone)]167 struct UnboundLocals {168 fctx: Pending<Context>,169 locals: Rc<Vec<BindSpec>>,170 }171 impl Unbound for UnboundLocals {172 type Bound = Context;173174 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Context> {175 let fctx = Context::new_future();176 let mut new_bindings =177 GcHashMap::with_capacity(self.locals.iter().map(BindSpec::capacity_hint).sum());178 for b in self.locals.iter() {179 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;180 }181182 let ctx = self.fctx.unwrap();183 let new_dollar = ctx.dollar().cloned().or_else(|| this.clone());184185 let ctx = ctx186 .extend(new_bindings, new_dollar, sup, this)187 .into_future(fctx);188189 Ok(ctx)190 }191 }192193 UnboundLocals { fctx, locals }194}195196pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(197 builder: &mut ObjValueBuilder,198 ctx: Context,199 uctx: B,200 field: &FieldMember,201) -> Result<()> {202 let name = evaluate_field_name(ctx, &field.name)?;203 let Some(name) = name else {204 return Ok(());205 };206207 match field {208 FieldMember {209 plus,210 params: None,211 visibility,212 value,213 ..214 } => {215 #[derive(Trace)]216 struct UnboundValue<B: Trace> {217 uctx: B,218 value: LocExpr,219 name: IStr,220 }221 impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {222 type Bound = Val;223 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {224 evaluate_named(self.uctx.bind(sup, this)?, &self.value, self.name.clone())225 }226 }227228 builder229 .member(name.clone())230 .with_add(*plus)231 .with_visibility(*visibility)232 .with_location(value.1.clone())233 .bindable(UnboundValue {234 uctx,235 value: value.clone(),236 name,237 })?;238 }239 FieldMember {240 params: Some(params),241 visibility,242 value,243 ..244 } => {245 #[derive(Trace)]246 struct UnboundMethod<B: Trace> {247 uctx: B,248 value: LocExpr,249 params: ParamsDesc,250 name: IStr,251 }252 impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {253 type Bound = Val;254 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {255 Ok(evaluate_method(256 self.uctx.bind(sup, this)?,257 self.name.clone(),258 self.params.clone(),259 self.value.clone(),260 ))261 }262 }263264 builder265 .member(name.clone())266 .with_visibility(*visibility)267 .with_location(value.1.clone())268 .bindable(UnboundMethod {269 uctx,270 value: value.clone(),271 params: params.clone(),272 name,273 })?;274 }275 }276 Ok(())277}278279#[allow(clippy::too_many_lines)]280pub fn evaluate_member_list_object(ctx: Context, members: &[Member]) -> Result<ObjValue> {281 let mut builder = ObjValueBuilder::new();282 let locals = Rc::new(283 members284 .iter()285 .filter_map(|m| match m {286 Member::BindStmt(bind) => Some(bind.clone()),287 _ => None,288 })289 .collect::<Vec<_>>(),290 );291292 let fctx = Context::new_future();293294 // We have single context for all fields, so we can cache binds295 let uctx = CachedUnbound::new(evaluate_object_locals(fctx.clone(), locals));296297 for member in members.iter() {298 match member {299 Member::Field(field) => {300 evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;301 }302 Member::AssertStmt(stmt) => {303 #[derive(Trace)]304 struct ObjectAssert<B: Trace> {305 uctx: B,306 assert: AssertStmt,307 }308 impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {309 fn run(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<()> {310 let ctx = self.uctx.bind(sup, this)?;311 evaluate_assert(ctx, &self.assert)312 }313 }314 builder.assert(ObjectAssert {315 uctx: uctx.clone(),316 assert: stmt.clone(),317 });318 }319 Member::BindStmt(_) => {320 // Already handled321 }322 }323 }324 let this = builder.build();325 fctx.fill(ctx.extend(GcHashMap::new(), None, None, Some(this.clone())));326 Ok(this)327}328329pub fn evaluate_object(ctx: Context, object: &ObjBody) -> Result<ObjValue> {330 Ok(match object {331 ObjBody::MemberList(members) => evaluate_member_list_object(ctx, members)?,332 ObjBody::ObjComp(obj) => {333 let mut builder = ObjValueBuilder::new();334 let locals = Rc::new(335 obj.pre_locals336 .iter()337 .chain(obj.post_locals.iter())338 .cloned()339 .collect::<Vec<_>>(),340 );341 let mut ctxs = vec![];342 evaluate_comp(ctx, &obj.compspecs, &mut |ctx| {343 let fctx = Context::new_future();344 ctxs.push((ctx.clone(), fctx.clone()));345 let uctx = evaluate_object_locals(fctx, locals.clone());346347 evaluate_field_member(&mut builder, ctx, uctx, &obj.field)348 })?;349350 let this = builder.build();351 for (ctx, fctx) in ctxs {352 let _ctx = ctx353 .extend(GcHashMap::new(), None, None, Some(this.clone()))354 .into_future(fctx);355 }356 this357 }358 })359}360361pub fn evaluate_apply(362 ctx: Context,363 value: &LocExpr,364 args: &ArgsDesc,365 loc: CallLocation<'_>,366 tailstrict: bool,367) -> Result<Val> {368 let value = evaluate(ctx.clone(), value)?;369 Ok(match value {370 Val::Func(f) => {371 let body = || f.evaluate(ctx, loc, args, tailstrict);372 if tailstrict {373 body()?374 } else {375 State::push(loc, || format!("function <{}> call", f.name()), body)?376 }377 }378 v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),379 })380}381382pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {383 let value = &assertion.0;384 let msg = &assertion.1;385 let assertion_result = State::push(386 CallLocation::new(&value.1),387 || "assertion condition".to_owned(),388 || bool::from_untyped(evaluate(ctx.clone(), value)?),389 )?;390 if !assertion_result {391 State::push(392 CallLocation::new(&value.1),393 || "assertion failure".to_owned(),394 || {395 if let Some(msg) = msg {396 throw!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));397 }398 throw!(AssertionFailed(Val::Null.to_string()?));399 },400 )?;401 }402 Ok(())403}404405pub fn evaluate_named(ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {406 use Expr::*;407 let LocExpr(raw_expr, _loc) = expr;408 Ok(match &**raw_expr {409 Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),410 _ => evaluate(ctx, expr)?,411 })412}413414#[allow(clippy::too_many_lines)]415pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {416 use Expr::*;417418 if let Some(trivial) = evaluate_trivial(expr) {419 return Ok(trivial);420 }421 let LocExpr(expr, loc) = expr;422 Ok(match &**expr {423 Literal(LiteralType::This) => {424 Val::Obj(ctx.this().ok_or(CantUseSelfOutsideOfObject)?.clone())425 }426 Literal(LiteralType::Super) => Val::Obj(427 ctx.super_obj().ok_or(NoSuperFound)?.with_this(428 ctx.this()429 .expect("if super exists - then this should too")430 .clone(),431 ),432 ),433 Literal(LiteralType::Dollar) => {434 Val::Obj(ctx.dollar().ok_or(NoTopLevelObjectFound)?.clone())435 }436 Literal(LiteralType::True) => Val::Bool(true),437 Literal(LiteralType::False) => Val::Bool(false),438 Literal(LiteralType::Null) => Val::Null,439 Parened(e) => evaluate(ctx, e)?,440 Str(v) => Val::Str(StrValue::Flat(v.clone())),441 Num(v) => Val::new_checked_num(*v)?,442 BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,443 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,444 Var(name) => State::push(445 CallLocation::new(loc),446 || format!("variable <{name}> access"),447 || ctx.binding(name.clone())?.evaluate(),448 )?,449 Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {450 let name = evaluate(ctx.clone(), index)?;451 let Val::Str(name) = name else {452 throw!(ValueIndexMustBeTypeGot(453 ValType::Obj,454 ValType::Str,455 name.value_type(),456 ))457 };458 ctx.super_obj()459 .expect("no super found")460 .get_for(name.into_flat(), ctx.this().expect("no this found").clone())?461 .expect("value not found")462 }463 Index(value, index) => match (evaluate(ctx.clone(), value)?, evaluate(ctx, index)?) {464 (Val::Obj(v), Val::Str(key)) => State::push(465 CallLocation::new(loc),466 || format!("field <{key}> access"),467 || match v.get(key.clone().into_flat()) {468 Ok(Some(v)) => Ok(v),469 Ok(None) => {470 let suggestions = suggest_object_fields(&v, key.clone().into_flat());471472 throw!(NoSuchField(key.clone().into_flat(), suggestions))473 }474 Err(e) => Err(e),475 },476 )?,477 (Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(478 ValType::Obj,479 ValType::Str,480 n.value_type(),481 )),482483 (Val::Arr(v), Val::Num(n)) => {484 if n.fract() > f64::EPSILON {485 throw!(FractionalIndex)486 }487 v.get(n as usize)?488 .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?489 }490 (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n.into_flat())),491 (Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(492 ValType::Arr,493 ValType::Num,494 n.value_type(),495 )),496497 (Val::Str(s), Val::Num(n)) => Val::Str({498 let v: IStr = s499 .clone()500 .into_flat()501 .chars()502 .skip(n as usize)503 .take(1)504 .collect::<String>()505 .into();506 if v.is_empty() {507 let size = s.into_flat().chars().count();508 throw!(StringBoundsError(n as usize, size))509 }510 StrValue::Flat(v)511 }),512 (Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(513 ValType::Str,514 ValType::Num,515 n.value_type(),516 )),517518 (v, _) => throw!(CantIndexInto(v.value_type())),519 },520 LocalExpr(bindings, returned) => {521 let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =522 GcHashMap::with_capacity(bindings.iter().map(BindSpec::capacity_hint).sum());523 let fctx = Context::new_future();524 for b in bindings {525 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;526 }527 let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);528 evaluate(ctx, &returned.clone())?529 }530 Arr(items) => {531 if items.is_empty() {532 Val::Arr(ArrValue::empty())533 } else if items.len() == 1 {534 #[derive(Trace)]535 struct ArrayElement {536 ctx: Context,537 item: LocExpr,538 }539 impl ThunkValue for ArrayElement {540 type Output = Val;541 fn get(self: Box<Self>) -> Result<Val> {542 evaluate(self.ctx, &self.item)543 }544 }545 Val::Arr(ArrValue::lazy(Cc::new(vec![Thunk::new(ArrayElement {546 ctx,547 item: items[0].clone(),548 })])))549 } else {550 Val::Arr(ArrValue::expr(ctx, items.iter().cloned()))551 }552 }553 ArrComp(expr, comp_specs) => {554 let mut out = Vec::new();555 evaluate_comp(ctx, comp_specs, &mut |ctx| {556 out.push(evaluate(ctx, expr)?);557 Ok(())558 })?;559 Val::Arr(ArrValue::eager(out))560 }561 Obj(body) => Val::Obj(evaluate_object(ctx, body)?),562 ObjExtend(a, b) => evaluate_add_op(563 &evaluate(ctx.clone(), a)?,564 &Val::Obj(evaluate_object(ctx, b)?),565 )?,566 Apply(value, args, tailstrict) => {567 evaluate_apply(ctx, value, args, CallLocation::new(loc), *tailstrict)?568 }569 Function(params, body) => {570 evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())571 }572 AssertExpr(assert, returned) => {573 evaluate_assert(ctx.clone(), assert)?;574 evaluate(ctx, returned)?575 }576 ErrorStmt(e) => State::push(577 CallLocation::new(loc),578 || "error statement".to_owned(),579 || throw!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),580 )?,581 IfElse {582 cond,583 cond_then,584 cond_else,585 } => {586 if State::push(587 CallLocation::new(loc),588 || "if condition".to_owned(),589 || bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),590 )? {591 evaluate(ctx, cond_then)?592 } else {593 match cond_else {594 Some(v) => evaluate(ctx, v)?,595 None => Val::Null,596 }597 }598 }599 Slice(value, desc) => {600 fn parse_idx<T: Typed>(601 loc: CallLocation<'_>,602 ctx: &Context,603 expr: Option<&LocExpr>,604 desc: &'static str,605 ) -> Result<Option<T>> {606 if let Some(value) = expr {607 Ok(Some(State::push(608 loc,609 || format!("slice {desc}"),610 || T::from_untyped(evaluate(ctx.clone(), value)?),611 )?))612 } else {613 Ok(None)614 }615 }616617 let indexable = evaluate(ctx.clone(), value)?;618 let loc = CallLocation::new(loc);619620 let start = parse_idx(loc, &ctx, desc.start.as_ref(), "start")?;621 let end = parse_idx(loc, &ctx, desc.end.as_ref(), "end")?;622 let step = parse_idx(loc, &ctx, desc.step.as_ref(), "step")?;623624 IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?625 }626 i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {627 let Expr::Str(path) = &*path.0 else {628 throw!("computed imports are not supported")629 };630 let tmp = loc.clone().0;631 let s = ctx.state();632 let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;633 match i {634 Import(_) => State::push(635 CallLocation::new(loc),636 || format!("import {:?}", path.clone()),637 || s.import_resolved(resolved_path),638 )?,639 ImportStr(_) => Val::Str(StrValue::Flat(s.import_resolved_str(resolved_path)?)),640 ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),641 _ => unreachable!(),642 }643 }644 })645}crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -88,6 +88,10 @@
Ok(match (evaluate(ctx.clone(), a)?, op, b) {
(Bool(true), Or, _o) => Val::Bool(true),
(Bool(false), And, _o) => Val::Bool(false),
+ #[cfg(feature = "exp-null-coaelse")]
+ (Null, NullCoaelse, eb) => evaluate(ctx, eb)?,
+ #[cfg(feature = "exp-null-coaelse")]
+ (a, NullCoaelse, _o) => a,
(a, op, eb) => evaluate_binary_op_normal(&a, op, &evaluate(ctx, eb)?)?,
})
}
crates/jrsonnet-parser/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-parser/Cargo.toml
+++ b/crates/jrsonnet-parser/Cargo.toml
@@ -10,6 +10,7 @@
[features]
default = []
exp-destruct = []
+exp-null-coaelse = []
# Implement serialization of AST using structdump
#
# Structdump generates code, which exactly replicated passed AST
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -122,6 +122,8 @@
And,
Or,
+ #[cfg(feature = "exp-null-coaelse")]
+ NullCoaelse,
// Equialent to std.objectHasEx(a, b, true)
In,
@@ -153,6 +155,8 @@
And => "&&",
Or => "||",
In => "in",
+ #[cfg(feature = "exp-null-coaelse")]
+ NullCoaelse => "??",
}
)
}
@@ -399,8 +403,13 @@
ErrorStmt(LocExpr),
/// a(b, c)
Apply(LocExpr, ArgsDesc, bool),
- /// a[b]
- Index(LocExpr, LocExpr),
+ /// a[b], a.b, a?.b
+ Index {
+ indexable: LocExpr,
+ index: LocExpr,
+ #[cfg(feature = "exp-null-coaelse")]
+ null_coaelse: bool,
+ },
/// function(x) x
Function(ParamsDesc, LocExpr),
/// if true == false then 1 else 2
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -288,7 +288,11 @@
rule unaryop(x: rule<()>) -> ()
= quiet!{ x() } / expected!("<unary op>")
-
+ rule ensure_null_coaelse()
+ = "" {?
+ #[cfg(not(feature = "exp-null-coaelse"))] return Err("!!!experimental null coaelscing was not enabled");
+ #[cfg(feature = "exp-null-coaelse")] Ok(())
+ }
use BinaryOpType::*;
use UnaryOpType::*;
rule expr(s: &ParserSettings) -> LocExpr
@@ -296,6 +300,10 @@
start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.source.clone(), start as u32, end as u32)) }
--
a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}
+ a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {
+ #[cfg(feature = "exp-null-coaelse")] return expr_bin!(a NullCoaelse b);
+ unreachable!("ensure_null_coaelse will fail if feature is not enabled")
+ }
--
a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}
--
@@ -329,8 +337,16 @@
unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}
--
a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}
- a:(@) _ "." _ a:position!() e:id_loc(s) b:position!() {Expr::Index(a, e)}
- a:(@) _ "[" _ e:expr(s) _ "]" {Expr::Index(a, e)}
+ indexable:(@) _ null_coaelse:("?" _ ensure_null_coaelse())? "." _ index:id_loc(s) {Expr::Index{
+ indexable, index,
+ #[cfg(feature = "exp-null-coaelse")]
+ null_coaelse: null_coaelse.is_some(),
+ }}
+ indexable:(@) _ null_coaelse:("?" _ "." _ ensure_null_coaelse())? "[" _ index:expr(s) _ "]" {Expr::Index{
+ indexable, index,
+ #[cfg(feature = "exp-null-coaelse")]
+ null_coaelse: null_coaelse.is_some(),
+ }}
a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}
a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}
--