difftreelog
feat field destructuring
in: master
22 files changed
bindings/jsonnet/src/native.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/native.rs
+++ b/bindings/jsonnet/src/native.rs
@@ -9,7 +9,7 @@
use jrsonnet_evaluator::{
error::{Error, LocError},
function::builtin::{BuiltinParam, NativeCallback, NativeCallbackHandler},
- gc::TraceBox,
+ tb,
typed::Typed,
IStr, State, Val,
};
@@ -78,9 +78,9 @@
vm.add_native(
name,
#[allow(deprecated)]
- Cc::new(TraceBox(Box::new(NativeCallback::new(
+ Cc::new(tb!(NativeCallback::new(
params,
- TraceBox(Box::new(JsonnetNativeCallbackHandler { ctx, cb })),
- )))),
+ tb!(JsonnetNativeCallbackHandler { ctx, cb }),
+ ))),
)
}
bindings/jsonnet/src/val_modify.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/val_modify.rs
+++ b/bindings/jsonnet/src/val_modify.rs
@@ -5,7 +5,7 @@
use std::{ffi::CStr, os::raw::c_char};
use gcmodule::Cc;
-use jrsonnet_evaluator::{val::ArrValue, LazyVal, State, Val};
+use jrsonnet_evaluator::{val::ArrValue, State, Thunk, Val};
/// # Safety
///
@@ -18,7 +18,8 @@
for item in old.iter_lazy() {
new.push(item);
}
- new.push(LazyVal::new_resolved(val.clone()));
+
+ new.push(Thunk::evaluated(val.clone()));
*arr = Val::Arr(ArrValue::Lazy(Cc::new(new)));
}
_ => panic!("should receive array"),
cmds/jrsonnet/Cargo.tomldiffbeforeafterboth--- a/cmds/jrsonnet/Cargo.toml
+++ b/cmds/jrsonnet/Cargo.toml
@@ -16,6 +16,8 @@
"jrsonnet-evaluator/exp-serde-preserve-order",
"jrsonnet-cli/exp-preserve-order",
]
+# Destructuring of locals
+exp-destruct = ["jrsonnet-evaluator/exp-destruct"]
[dependencies]
jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.4.2" }
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -18,6 +18,8 @@
# Allows to preserve field order in objects
exp-preserve-order = []
exp-serde-preserve-order = ["serde_json/preserve_order"]
+# Implements field destructuring
+exp-destruct = []
[dependencies]
jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.2" }
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -4,12 +4,12 @@
use jrsonnet_interner::IStr;
use crate::{
- cc_ptr_eq, error::Error::*, gc::GcHashMap, map::LayeredHashMap, FutureWrapper, LazyBinding,
- LazyVal, ObjValue, Result, State, Val,
+ cc_ptr_eq, error::Error::*, gc::GcHashMap, map::LayeredHashMap, LazyBinding, ObjValue, Pending,
+ Result, State, Thunk, Val,
};
#[derive(Clone, Trace)]
-pub struct ContextCreator(pub Context, pub FutureWrapper<GcHashMap<IStr, LazyBinding>>);
+pub struct ContextCreator(pub Context, pub Pending<GcHashMap<IStr, LazyBinding>>);
impl ContextCreator {
pub fn create(
&self,
@@ -43,8 +43,8 @@
#[derive(Debug, Clone, Trace)]
pub struct Context(Cc<ContextInternals>);
impl Context {
- pub fn new_future() -> FutureWrapper<Self> {
- FutureWrapper::new()
+ pub fn new_future() -> Pending<Self> {
+ Pending::new()
}
pub fn dollar(&self) -> &Option<ObjValue> {
@@ -68,7 +68,7 @@
}))
}
- pub fn binding(&self, name: IStr) -> Result<LazyVal> {
+ pub fn binding(&self, name: IStr) -> Result<Thunk<Val>> {
Ok(self
.0
.bindings
@@ -80,7 +80,7 @@
self.0.bindings.contains_key(&name)
}
#[must_use]
- pub fn into_future(self, ctx: FutureWrapper<Self>) -> Self {
+ pub fn into_future(self, ctx: Pending<Self>) -> Self {
{
ctx.0.borrow_mut().replace(self);
}
@@ -90,7 +90,7 @@
#[must_use]
pub fn with_var(self, name: IStr, value: Val) -> Self {
let mut new_bindings = GcHashMap::with_capacity(1);
- new_bindings.insert(name, LazyVal::new_resolved(value));
+ new_bindings.insert(name, Thunk::evaluated(value));
self.extend(new_bindings, None, None, None)
}
@@ -102,7 +102,7 @@
#[must_use]
pub fn extend(
self,
- new_bindings: GcHashMap<IStr, LazyVal>,
+ new_bindings: GcHashMap<IStr, Thunk<Val>>,
new_dollar: Option<ObjValue>,
new_this: Option<ObjValue>,
new_super_obj: Option<ObjValue>,
@@ -124,7 +124,7 @@
}))
}
#[must_use]
- pub fn extend_bound(self, new_bindings: GcHashMap<IStr, LazyVal>) -> Self {
+ pub fn extend_bound(self, new_bindings: GcHashMap<IStr, Thunk<Val>>) -> Self {
let new_this = self.0.this.clone();
let new_super_obj = self.0.super_obj.clone();
self.extend(new_bindings, None, new_this, new_super_obj)
crates/jrsonnet-evaluator/src/dynamic.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/dynamic.rs
+++ b/crates/jrsonnet-evaluator/src/dynamic.rs
@@ -3,8 +3,8 @@
use gcmodule::{Cc, Trace};
#[derive(Clone, Trace)]
-pub struct FutureWrapper<V: Trace + 'static>(pub Cc<RefCell<Option<V>>>);
-impl<T: Trace + 'static> FutureWrapper<T> {
+pub struct Pending<V: Trace + 'static>(pub Cc<RefCell<Option<V>>>);
+impl<T: Trace + 'static> Pending<T> {
pub fn new() -> Self {
Self(Cc::new(RefCell::new(None)))
}
@@ -15,7 +15,7 @@
self.0.borrow_mut().replace(value);
}
}
-impl<T: Clone + Trace + 'static> FutureWrapper<T> {
+impl<T: Clone + Trace + 'static> Pending<T> {
/// # Panics
/// If wrapper is not yet filled
pub fn unwrap(&self) -> T {
@@ -23,7 +23,7 @@
}
}
-impl<T: Trace + 'static> Default for FutureWrapper<T> {
+impl<T: Trace + 'static> Default for Pending<T> {
fn default() -> Self {
Self::new()
}
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -45,6 +45,9 @@
#[error("variable is not defined: {0}")]
VariableIsNotDefined(IStr),
+ #[error("duplicate local var: {0}")]
+ DuplicateLocalVar(IStr),
+
#[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{}", e)).collect::<Vec<_>>().join(", "))]
TypeMismatch(&'static str, Vec<ValType>, ValType),
#[error("no such field: {0}")]
crates/jrsonnet-evaluator/src/evaluate/destructure.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
@@ -0,0 +1,294 @@
+use gcmodule::Trace;
+use jrsonnet_interner::IStr;
+use jrsonnet_parser::{BindSpec, Destruct, LocExpr, ParamsDesc};
+
+use crate::{
+ error::{Error::*, Result},
+ evaluate, evaluate_method,
+ gc::GcHashMap,
+ tb, throw,
+ val::ThunkValue,
+ Context, Pending, State, Thunk, Val,
+};
+
+fn destruct(
+ d: &Destruct,
+ parent: Thunk<Val>,
+ new_bindings: &mut GcHashMap<IStr, Thunk<Val>>,
+) -> Result<()> {
+ Ok(match d {
+ Destruct::Full(v) => {
+ let old = new_bindings.insert(v.clone(), parent);
+ if old.is_some() {
+ throw!(DuplicateLocalVar(v.clone()))
+ }
+ }
+ #[cfg(feature = "exp-destruct")]
+ Destruct::Skip => {}
+ #[cfg(feature = "exp-destruct")]
+ Destruct::Array { start, rest, end } => {
+ use jrsonnet_parser::DestructRest;
+
+ use crate::{throw_runtime, val::ArrValue};
+
+ #[derive(Trace)]
+ struct DataThunk {
+ parent: Thunk<Val>,
+ min_len: usize,
+ has_rest: bool,
+ }
+ impl ThunkValue for DataThunk {
+ type Output = ArrValue;
+
+ fn get(self: Box<Self>, s: State) -> Result<Self::Output> {
+ let v = self.parent.evaluate(s)?;
+ let arr = match v {
+ Val::Arr(a) => a,
+ _ => throw_runtime!("expected array"),
+ };
+ if !self.has_rest {
+ if arr.len() != self.min_len {
+ throw_runtime!("expected {} elements, got {}", self.min_len, arr.len())
+ }
+ } else if arr.len() < self.min_len {
+ throw_runtime!(
+ "expected at least {} elements, but array was only {}",
+ self.min_len,
+ arr.len()
+ )
+ }
+ Ok(arr)
+ }
+ }
+
+ let full = Thunk::new(tb!(DataThunk {
+ min_len: start.len() + end.len(),
+ has_rest: rest.is_some(),
+ parent,
+ }));
+
+ {
+ #[derive(Trace)]
+ struct BaseThunk {
+ full: Thunk<ArrValue>,
+ index: usize,
+ }
+ impl ThunkValue for BaseThunk {
+ type Output = Val;
+
+ fn get(self: Box<Self>, s: State) -> Result<Self::Output> {
+ let full = self.full.evaluate(s.clone())?;
+ Ok(full.get(s, self.index)?.expect("length is checked"))
+ }
+ }
+ for (i, d) in start.iter().enumerate() {
+ destruct(
+ d,
+ Thunk::new(tb!(BaseThunk {
+ full: full.clone(),
+ index: i,
+ })),
+ new_bindings,
+ )?;
+ }
+ }
+
+ match rest {
+ Some(DestructRest::Keep(v)) => {
+ #[derive(Trace)]
+ struct RestThunk {
+ full: Thunk<ArrValue>,
+ start: usize,
+ end: usize,
+ }
+ impl ThunkValue for RestThunk {
+ type Output = Val;
+
+ fn get(self: Box<Self>, s: State) -> Result<Self::Output> {
+ let full = self.full.evaluate(s)?;
+ let to = full.len() - self.end;
+ Ok(Val::Arr(full.slice(Some(self.start), Some(to), None)))
+ }
+ }
+
+ destruct(
+ &Destruct::Full(v.clone()),
+ Thunk::new(tb!(RestThunk {
+ full: full.clone(),
+ start: start.len(),
+ end: end.len(),
+ })),
+ new_bindings,
+ )?;
+ }
+ Some(DestructRest::Drop) => {}
+ None => {}
+ }
+
+ {
+ #[derive(Trace)]
+ struct EndThunk {
+ full: Thunk<ArrValue>,
+ index: usize,
+ end: usize,
+ }
+ impl ThunkValue for EndThunk {
+ type Output = Val;
+
+ fn get(self: Box<Self>, s: State) -> Result<Self::Output> {
+ let full = self.full.evaluate(s.clone())?;
+ Ok(full
+ .get(s, full.len() - self.end + self.index)?
+ .expect("length is checked"))
+ }
+ }
+ for (i, d) in end.iter().enumerate() {
+ destruct(
+ d,
+ Thunk::new(tb!(EndThunk {
+ full: full.clone(),
+ index: i,
+ end: end.len(),
+ })),
+ new_bindings,
+ )?;
+ }
+ }
+ }
+ #[cfg(feature = "exp-destruct")]
+ Destruct::Object { fields, rest } => {
+ use jrsonnet_parser::DestructRest;
+
+ use crate::{obj::ObjValue, throw_runtime};
+
+ #[derive(Trace)]
+ struct DataThunk {
+ parent: Thunk<Val>,
+ field_names: Vec<IStr>,
+ has_rest: bool,
+ }
+ impl ThunkValue for DataThunk {
+ type Output = ObjValue;
+
+ fn get(self: Box<Self>, s: State) -> Result<Self::Output> {
+ let v = self.parent.evaluate(s)?;
+ let obj = match v {
+ Val::Obj(o) => o,
+ _ => throw_runtime!("expected object"),
+ };
+ for field in &self.field_names {
+ if !obj.has_field_ex(field.clone(), true) {
+ throw_runtime!("missing field: {}", field);
+ }
+ }
+ if !self.has_rest {
+ let len = obj.len();
+ if len != self.field_names.len() {
+ throw_runtime!("too many fields, and rest not found");
+ }
+ }
+ Ok(obj)
+ }
+ }
+ let field_names: Vec<_> = fields.iter().map(|f| f.0.clone()).collect();
+ let full = Thunk::new(tb!(DataThunk {
+ parent,
+ field_names: field_names.clone(),
+ has_rest: rest.is_some()
+ }));
+
+ for (field, d) in fields {
+ #[derive(Trace)]
+ struct FieldThunk {
+ full: Thunk<ObjValue>,
+ field: IStr,
+ }
+ impl ThunkValue for FieldThunk {
+ type Output = Val;
+
+ fn get(self: Box<Self>, s: State) -> Result<Self::Output> {
+ let full = self.full.evaluate(s.clone())?;
+ let field = full.get(s, self.field)?.expect("shape is checked");
+ Ok(field)
+ }
+ }
+ let value = Thunk::new(tb!(FieldThunk {
+ full: full.clone(),
+ field: field.clone()
+ }));
+ if let Some(d) = d {
+ destruct(d, value, new_bindings)?;
+ } else {
+ destruct(&Destruct::Full(field.clone()), value, new_bindings)?;
+ }
+ }
+ }
+ })
+}
+
+pub fn evaluate_dest(
+ d: &BindSpec,
+ fctx: Pending<Context>,
+ new_bindings: &mut GcHashMap<IStr, Thunk<Val>>,
+) -> Result<()> {
+ match d {
+ BindSpec::Field { into, value } => {
+ #[derive(Trace)]
+ struct EvaluateThunkValue {
+ fctx: Pending<Context>,
+ expr: LocExpr,
+ }
+ impl ThunkValue for EvaluateThunkValue {
+ type Output = Val;
+ fn get(self: Box<Self>, s: State) -> Result<Self::Output> {
+ evaluate(s, self.fctx.unwrap(), &self.expr)
+ }
+ }
+ // TODO: Generate some name, as destructure spec may be used with plain functions
+ let data = Thunk::new(tb!(EvaluateThunkValue {
+ fctx,
+ expr: value.clone(),
+ }));
+ destruct(into, data, new_bindings)?;
+ }
+ BindSpec::Function {
+ name,
+ params,
+ value,
+ } => {
+ #[derive(Trace)]
+ struct MethodThunk {
+ fctx: Pending<Context>,
+ name: IStr,
+ params: ParamsDesc,
+ value: LocExpr,
+ }
+ impl ThunkValue for MethodThunk {
+ type Output = Val;
+
+ fn get(self: Box<Self>, _s: State) -> Result<Self::Output> {
+ Ok(evaluate_method(
+ self.fctx.unwrap(),
+ self.name,
+ self.params,
+ self.value,
+ ))
+ }
+ }
+
+ let old = new_bindings.insert(
+ name.clone(),
+ Thunk::new(tb!(MethodThunk {
+ fctx,
+ name: name.clone(),
+ params: params.clone(),
+ value: value.clone()
+ })),
+ );
+ if old.is_some() {
+ throw!(DuplicateLocalVar(name.clone()))
+ }
+ }
+ }
+ Ok(())
+}
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -1,189 +1,157 @@
use gcmodule::{Cc, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{
- ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, ForSpecData, IfSpecData,
+ ArgsDesc, AssertStmt, BindSpec, CompSpec, Destruct, Expr, FieldMember, ForSpecData, IfSpecData,
LiteralType, LocExpr, Member, ObjBody, ParamsDesc,
};
use jrsonnet_types::ValType;
use crate::{
+ destructure::evaluate_dest,
error::Error::*,
evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
function::{CallLocation, FuncDesc, FuncVal},
- gc::TraceBox,
stdlib::{std_slice, BUILTINS},
- throw,
+ tb, throw,
typed::Typed,
- val::{ArrValue, LazyValValue},
- Bindable, Context, ContextCreator, FutureWrapper, GcHashMap, LazyBinding, LazyVal, ObjValue,
- ObjValueBuilder, ObjectAssertion, Result, State, Val,
+ val::{ArrValue, Thunk, ThunkValue},
+ Bindable, Context, ContextCreator, GcHashMap, LazyBinding, ObjValue, ObjValueBuilder,
+ ObjectAssertion, Pending, Result, State, Val,
};
+pub mod destructure;
pub mod operator;
-pub fn evaluate_binding_in_future(b: &BindSpec, fctx: FutureWrapper<Context>) -> LazyVal {
- let b = b.clone();
- if let Some(params) = &b.params {
- #[derive(Trace)]
- struct LazyMethodBinding {
- fctx: FutureWrapper<Context>,
- name: IStr,
- params: ParamsDesc,
- value: LocExpr,
- }
- impl LazyValValue for LazyMethodBinding {
- fn get(self: Box<Self>, _: State) -> Result<Val> {
- Ok(evaluate_method(
- self.fctx.unwrap(),
- self.name,
- self.params,
- self.value,
- ))
+#[allow(clippy::too_many_lines)]
+pub fn evaluate_binding(b: BindSpec, cctx: ContextCreator) -> Result<(IStr, LazyBinding)> {
+ match b {
+ BindSpec::Field {
+ into: Destruct::Full(name),
+ value,
+ } => {
+ #[derive(Trace)]
+ struct BindableNamedThunk {
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+
+ cctx: ContextCreator,
+ name: IStr,
+ value: LocExpr,
}
- }
+ impl ThunkValue for BindableNamedThunk {
+ type Output = Val;
+ fn get(self: Box<Self>, s: State) -> Result<Val> {
+ evaluate_named(
+ s.clone(),
+ self.cctx.create(s, self.this, self.super_obj)?,
+ &self.value,
+ self.name,
+ )
+ }
+ }
- let params = params.clone();
+ #[derive(Trace)]
+ struct BindableNamed {
+ cctx: ContextCreator,
+ name: IStr,
+ value: LocExpr,
+ }
+ impl Bindable for BindableNamed {
+ fn bind(
+ &self,
+ _: State,
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+ ) -> Result<Thunk<Val>> {
+ Ok(Thunk::new(tb!(BindableNamedThunk {
+ this,
+ super_obj,
- LazyVal::new(TraceBox(Box::new(LazyMethodBinding {
- fctx,
- name: b.name.clone(),
- params,
- value: b.value.clone(),
- })))
- } else {
- #[derive(Trace)]
- struct LazyNamedBinding {
- fctx: FutureWrapper<Context>,
- name: IStr,
- value: LocExpr,
- }
- impl LazyValValue for LazyNamedBinding {
- fn get(self: Box<Self>, s: State) -> Result<Val> {
- evaluate_named(s, self.fctx.unwrap(), &self.value, self.name)
+ cctx: self.cctx.clone(),
+ name: self.name.clone(),
+ value: self.value.clone(),
+ })))
+ }
}
- }
- LazyVal::new(TraceBox(Box::new(LazyNamedBinding {
- fctx,
- name: b.name.clone(),
- value: b.value,
- })))
- }
-}
-#[allow(clippy::too_many_lines)]
-pub fn evaluate_binding(b: &BindSpec, cctx: ContextCreator) -> (IStr, LazyBinding) {
- let b = b.clone();
- if let Some(params) = &b.params {
- #[derive(Trace)]
- struct BindableMethodLazyVal {
- this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
-
- cctx: ContextCreator,
- name: IStr,
- params: ParamsDesc,
- value: LocExpr,
- }
- impl LazyValValue for BindableMethodLazyVal {
- fn get(self: Box<Self>, s: State) -> Result<Val> {
- Ok(evaluate_method(
- self.cctx.create(s, self.this, self.super_obj)?,
- self.name,
- self.params,
- self.value,
- ))
- }
+ Ok((
+ name.clone(),
+ LazyBinding::Bindable(Cc::new(tb!(BindableNamed {
+ cctx,
+ name: name.clone(),
+ value: value.clone(),
+ }))),
+ ))
}
-
- #[derive(Trace)]
- struct BindableMethod {
- cctx: ContextCreator,
- name: IStr,
- params: ParamsDesc,
- value: LocExpr,
+ #[cfg(feature = "exp-destruct")]
+ BindSpec::Field { into: _, .. } => {
+ use crate::throw_runtime;
+ throw_runtime!("destructuring is not yet supported here")
}
- impl Bindable for BindableMethod {
- fn bind(
- &self,
- _: State,
+ BindSpec::Function {
+ name,
+ params,
+ value,
+ } => {
+ #[derive(Trace)]
+ struct BindableMethodThunk {
this: Option<ObjValue>,
super_obj: Option<ObjValue>,
- ) -> Result<LazyVal> {
- Ok(LazyVal::new(TraceBox(Box::new(BindableMethodLazyVal {
- this,
- super_obj,
- cctx: self.cctx.clone(),
- name: self.name.clone(),
- params: self.params.clone(),
- value: self.value.clone(),
- }))))
+ cctx: ContextCreator,
+ name: IStr,
+ params: ParamsDesc,
+ value: LocExpr,
}
- }
+ impl ThunkValue for BindableMethodThunk {
+ type Output = Val;
+ fn get(self: Box<Self>, s: State) -> Result<Val> {
+ Ok(evaluate_method(
+ self.cctx.create(s, self.this, self.super_obj)?,
+ self.name,
+ self.params,
+ self.value,
+ ))
+ }
+ }
- let params = params.clone();
-
- (
- b.name.clone(),
- LazyBinding::Bindable(Cc::new(TraceBox(Box::new(BindableMethod {
- cctx,
- name: b.name.clone(),
- params,
- value: b.value.clone(),
- })))),
- )
- } else {
- #[derive(Trace)]
- struct BindableNamedLazyVal {
- this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
+ #[derive(Trace)]
+ struct BindableMethod {
+ cctx: ContextCreator,
+ name: IStr,
+ params: ParamsDesc,
+ value: LocExpr,
+ }
+ impl Bindable for BindableMethod {
+ fn bind(
+ &self,
+ _: State,
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+ ) -> Result<Thunk<Val>> {
+ Ok(Thunk::<Val>::new(tb!(BindableMethodThunk {
+ this,
+ super_obj,
- cctx: ContextCreator,
- name: IStr,
- value: LocExpr,
- }
- impl LazyValValue for BindableNamedLazyVal {
- fn get(self: Box<Self>, s: State) -> Result<Val> {
- evaluate_named(
- s.clone(),
- self.cctx.create(s, self.this, self.super_obj)?,
- &self.value,
- self.name,
- )
+ cctx: self.cctx.clone(),
+ name: self.name.clone(),
+ params: self.params.clone(),
+ value: self.value.clone(),
+ })))
+ }
}
- }
- #[derive(Trace)]
- struct BindableNamed {
- cctx: ContextCreator,
- name: IStr,
- value: LocExpr,
- }
- impl Bindable for BindableNamed {
- fn bind(
- &self,
- _: State,
- this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
- ) -> Result<LazyVal> {
- Ok(LazyVal::new(TraceBox(Box::new(BindableNamedLazyVal {
- this,
- super_obj,
+ let params = params.clone();
- cctx: self.cctx.clone(),
- name: self.name.clone(),
- value: self.value.clone(),
- }))))
- }
+ Ok((
+ name.clone(),
+ LazyBinding::Bindable(Cc::new(tb!(BindableMethod {
+ cctx,
+ name: name.clone(),
+ params,
+ value,
+ }))),
+ ))
}
-
- (
- b.name.clone(),
- LazyBinding::Bindable(Cc::new(TraceBox(Box::new(BindableNamed {
- cctx,
- name: b.name.clone(),
- value: b.value.clone(),
- })))),
- )
}
}
@@ -252,19 +220,20 @@
#[allow(clippy::too_many_lines)]
pub fn evaluate_member_list_object(s: State, ctx: Context, members: &[Member]) -> Result<ObjValue> {
- let new_bindings = FutureWrapper::new();
- let future_this = FutureWrapper::new();
+ let new_bindings = Pending::new();
+ let future_this = Pending::new();
let cctx = ContextCreator(ctx.clone(), new_bindings.clone());
{
let mut bindings: GcHashMap<IStr, LazyBinding> = GcHashMap::with_capacity(members.len());
- for (n, b) in members
+ for r in members
.iter()
.filter_map(|m| match m {
Member::BindStmt(b) => Some(b.clone()),
_ => None,
})
- .map(|b| evaluate_binding(&b, cctx.clone()))
+ .map(|b| evaluate_binding(b.clone(), cctx.clone()))
{
+ let (n, b) = r?;
bindings.insert(n, b);
}
new_bindings.fill(bindings);
@@ -292,8 +261,8 @@
s: State,
this: Option<ObjValue>,
super_obj: Option<ObjValue>,
- ) -> Result<LazyVal> {
- Ok(LazyVal::new_resolved(evaluate_named(
+ ) -> Result<Thunk<Val>> {
+ Ok(Thunk::evaluated(evaluate_named(
s.clone(),
self.cctx.create(s, this, super_obj)?,
&self.value,
@@ -316,11 +285,11 @@
.with_location(value.1.clone())
.bindable(
s.clone(),
- TraceBox(Box::new(ObjMemberBinding {
+ tb!(ObjMemberBinding {
cctx: cctx.clone(),
value: value.clone(),
name,
- })),
+ }),
)?;
}
Member::Field(FieldMember {
@@ -342,8 +311,8 @@
s: State,
this: Option<ObjValue>,
super_obj: Option<ObjValue>,
- ) -> Result<LazyVal> {
- Ok(LazyVal::new_resolved(evaluate_method(
+ ) -> Result<Thunk<Val>> {
+ Ok(Thunk::evaluated(evaluate_method(
self.cctx.create(s, this, super_obj)?,
self.name.clone(),
self.params.clone(),
@@ -364,12 +333,12 @@
.with_location(value.1.clone())
.bindable(
s.clone(),
- TraceBox(Box::new(ObjMemberBinding {
+ tb!(ObjMemberBinding {
cctx: cctx.clone(),
value: value.clone(),
params: params.clone(),
name,
- })),
+ }),
)?;
}
Member::BindStmt(_) => {}
@@ -390,10 +359,10 @@
evaluate_assert(s, ctx, &self.assert)
}
}
- builder.assert(TraceBox(Box::new(ObjectAssert {
+ builder.assert(tb!(ObjectAssert {
cctx: cctx.clone(),
assert: stmt.clone(),
- })));
+ }));
}
}
}
@@ -406,19 +375,20 @@
Ok(match object {
ObjBody::MemberList(members) => evaluate_member_list_object(s, ctx, members)?,
ObjBody::ObjComp(obj) => {
- let future_this = FutureWrapper::new();
+ let future_this = Pending::new();
let mut builder = ObjValueBuilder::new();
evaluate_comp(s.clone(), ctx, &obj.compspecs, &mut |ctx| {
- let new_bindings = FutureWrapper::new();
+ let new_bindings = Pending::new();
let cctx = ContextCreator(ctx.clone(), new_bindings.clone());
let mut bindings: GcHashMap<IStr, LazyBinding> =
GcHashMap::with_capacity(obj.pre_locals.len() + obj.post_locals.len());
- for (n, b) in obj
+ for r in obj
.pre_locals
.iter()
.chain(obj.post_locals.iter())
- .map(|b| evaluate_binding(b, cctx.clone()))
+ .map(|b| evaluate_binding(b.clone(), cctx.clone()))
{
+ let (n, b) = r?;
bindings.insert(n, b);
}
new_bindings.fill(bindings.clone());
@@ -439,8 +409,8 @@
s: State,
this: Option<ObjValue>,
_super_obj: Option<ObjValue>,
- ) -> Result<LazyVal> {
- Ok(LazyVal::new_resolved(evaluate(
+ ) -> Result<Thunk<Val>> {
+ Ok(Thunk::evaluated(evaluate(
s,
self.ctx.clone().extend(GcHashMap::new(), None, this, None),
&self.value,
@@ -453,10 +423,10 @@
.with_add(obj.plus)
.bindable(
s.clone(),
- TraceBox(Box::new(ObjCompBinding {
+ tb!(ObjCompBinding {
ctx,
value: obj.value.clone(),
- })),
+ }),
)?;
}
v => throw!(FieldMustBeStringGot(v.value_type())),
@@ -620,11 +590,11 @@
}
}
LocalExpr(bindings, returned) => {
- let mut new_bindings: GcHashMap<IStr, LazyVal> =
+ let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =
GcHashMap::with_capacity(bindings.len());
let fctx = Context::new_future();
for b in bindings {
- new_bindings.insert(b.name.clone(), evaluate_binding_in_future(b, fctx.clone()));
+ evaluate_dest(b, fctx.clone(), &mut new_bindings)?;
}
let ctx = ctx.extend_bound(new_bindings).into_future(fctx);
evaluate(s, ctx, &returned.clone())?
@@ -638,15 +608,16 @@
ctx: Context,
item: LocExpr,
}
- impl LazyValValue for ArrayElement {
+ impl ThunkValue for ArrayElement {
+ type Output = Val;
fn get(self: Box<Self>, s: State) -> Result<Val> {
evaluate(s, self.ctx, &self.item)
}
}
- out.push(LazyVal::new(TraceBox(Box::new(ArrayElement {
+ out.push(Thunk::new(tb!(ArrayElement {
ctx: ctx.clone(),
item: item.clone(),
- }))));
+ })));
}
Val::Arr(out.into())
}
crates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/arglike.rs
+++ b/crates/jrsonnet-evaluator/src/function/arglike.rs
@@ -5,34 +5,34 @@
use jrsonnet_parser::{ArgsDesc, LocExpr};
use crate::{
- error::Result, evaluate, gc::TraceBox, typed::Typed, val::LazyValValue, Context, LazyVal,
- State, Val,
+ error::Result, evaluate, tb, typed::Typed, val::ThunkValue, Context, State, Thunk, Val,
};
#[derive(Trace)]
-struct EvaluateLazyVal {
+struct EvaluateThunk {
ctx: Context,
expr: LocExpr,
}
-impl LazyValValue for EvaluateLazyVal {
+impl ThunkValue for EvaluateThunk {
+ type Output = Val;
fn get(self: Box<Self>, s: State) -> Result<Val> {
evaluate(s, self.ctx, &self.expr)
}
}
pub trait ArgLike {
- fn evaluate_arg(&self, s: State, ctx: Context, tailstrict: bool) -> Result<LazyVal>;
+ fn evaluate_arg(&self, s: State, ctx: Context, tailstrict: bool) -> Result<Thunk<Val>>;
}
impl ArgLike for &LocExpr {
- fn evaluate_arg(&self, s: State, ctx: Context, tailstrict: bool) -> Result<LazyVal> {
+ fn evaluate_arg(&self, s: State, ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {
Ok(if tailstrict {
- LazyVal::new_resolved(evaluate(s, ctx, self)?)
+ Thunk::evaluated(evaluate(s, ctx, self)?)
} else {
- LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
+ Thunk::new(tb!(EvaluateThunk {
ctx,
expr: (*self).clone(),
- })))
+ }))
})
}
}
@@ -41,9 +41,9 @@
where
T: Typed + Clone,
{
- fn evaluate_arg(&self, s: State, _ctx: Context, _tailstrict: bool) -> Result<LazyVal> {
+ fn evaluate_arg(&self, s: State, _ctx: Context, _tailstrict: bool) -> Result<Thunk<Val>> {
let val = T::into_untyped(self.clone(), s)?;
- Ok(LazyVal::new_resolved(val))
+ Ok(Thunk::evaluated(val))
}
}
@@ -53,18 +53,18 @@
Val(Val),
}
impl ArgLike for TlaArg {
- fn evaluate_arg(&self, s: State, ctx: Context, tailstrict: bool) -> Result<LazyVal> {
+ fn evaluate_arg(&self, s: State, ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {
match self {
- TlaArg::String(s) => Ok(LazyVal::new_resolved(Val::Str(s.clone()))),
+ TlaArg::String(s) => Ok(Thunk::evaluated(Val::Str(s.clone()))),
TlaArg::Code(code) => Ok(if tailstrict {
- LazyVal::new_resolved(evaluate(s, ctx, code)?)
+ Thunk::evaluated(evaluate(s, ctx, code)?)
} else {
- LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
+ Thunk::new(tb!(EvaluateThunk {
ctx,
expr: code.clone(),
- })))
+ }))
}),
- TlaArg::Val(val) => Ok(LazyVal::new_resolved(val.clone())),
+ TlaArg::Val(val) => Ok(Thunk::evaluated(val.clone())),
}
}
}
@@ -83,14 +83,14 @@
s: State,
ctx: Context,
tailstrict: bool,
- handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+ handler: &mut dyn FnMut(usize, Thunk<Val>) -> Result<()>,
) -> Result<()>;
fn named_iter(
&self,
s: State,
ctx: Context,
tailstrict: bool,
- handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+ handler: &mut dyn FnMut(&IStr, Thunk<Val>) -> Result<()>,
) -> Result<()>;
fn named_names(&self, handler: &mut dyn FnMut(&IStr));
}
@@ -105,18 +105,18 @@
s: State,
ctx: Context,
tailstrict: bool,
- handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+ handler: &mut dyn FnMut(usize, Thunk<Val>) -> Result<()>,
) -> Result<()> {
for (id, arg) in self.unnamed.iter().enumerate() {
handler(
id,
if tailstrict {
- LazyVal::new_resolved(evaluate(s.clone(), ctx.clone(), arg)?)
+ Thunk::evaluated(evaluate(s.clone(), ctx.clone(), arg)?)
} else {
- LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
+ Thunk::new(tb!(EvaluateThunk {
ctx: ctx.clone(),
expr: arg.clone(),
- })))
+ }))
},
)?;
}
@@ -128,18 +128,18 @@
s: State,
ctx: Context,
tailstrict: bool,
- handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+ handler: &mut dyn FnMut(&IStr, Thunk<Val>) -> Result<()>,
) -> Result<()> {
for (name, arg) in &self.named {
handler(
name,
if tailstrict {
- LazyVal::new_resolved(evaluate(s.clone(), ctx.clone(), arg)?)
+ Thunk::evaluated(evaluate(s.clone(), ctx.clone(), arg)?)
} else {
- LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {
+ Thunk::new(tb!(EvaluateThunk {
ctx: ctx.clone(),
expr: arg.clone(),
- })))
+ }))
},
)?;
}
@@ -164,7 +164,7 @@
_s: State,
_ctx: Context,
_tailstrict: bool,
- _handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+ _handler: &mut dyn FnMut(usize, Thunk<Val>) -> Result<()>,
) -> Result<()> {
Ok(())
}
@@ -174,7 +174,7 @@
s: State,
ctx: Context,
tailstrict: bool,
- handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+ handler: &mut dyn FnMut(&IStr, Thunk<Val>) -> Result<()>,
) -> Result<()> {
for (name, value) in self.iter() {
handler(
@@ -205,7 +205,7 @@
s: State,
ctx: Context,
tailstrict: bool,
- handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+ handler: &mut dyn FnMut(usize, Thunk<Val>) -> Result<()>,
) -> Result<()> {
let mut i = 0usize;
let ($($gen,)*) = self;
@@ -220,7 +220,7 @@
_s: State,
_ctx: Context,
_tailstrict: bool,
- _handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+ _handler: &mut dyn FnMut(&IStr, Thunk<Val>) -> Result<()>,
) -> Result<()> {
Ok(())
}
@@ -236,7 +236,7 @@
_s: State,
_ctx: Context,
_tailstrict: bool,
- _handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+ _handler: &mut dyn FnMut(usize, Thunk<Val>) -> Result<()>,
) -> Result<()> {
Ok(())
}
@@ -246,7 +246,7 @@
s: State,
ctx: Context,
tailstrict: bool,
- handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+ handler: &mut dyn FnMut(&IStr, Thunk<Val>) -> Result<()>,
) -> Result<()> {
let ($($gen,)*) = self;
$(
@@ -285,7 +285,7 @@
_s: State,
_ctx: Context,
_tailstrict: bool,
- _handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,
+ _handler: &mut dyn FnMut(usize, Thunk<Val>) -> Result<()>,
) -> Result<()> {
Ok(())
}
@@ -295,7 +295,7 @@
_s: State,
_ctx: Context,
_tailstrict: bool,
- _handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
+ _handler: &mut dyn FnMut(&IStr, Thunk<Val>) -> Result<()>,
) -> Result<()> {
Ok(())
}
crates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/parse.rs
+++ b/crates/jrsonnet-evaluator/src/function/parse.rs
@@ -9,20 +9,21 @@
use crate::{
error::{Error::*, Result},
evaluate_named,
- gc::{GcHashMap, TraceBox},
- throw,
- val::LazyValValue,
- Context, FutureWrapper, LazyVal, State, Val,
+ gc::GcHashMap,
+ tb, throw,
+ val::ThunkValue,
+ Context, Pending, State, Thunk, Val,
};
#[derive(Trace)]
-struct EvaluateNamedLazyVal {
- ctx: FutureWrapper<Context>,
+struct EvaluateNamedThunk {
+ ctx: Pending<Context>,
name: IStr,
value: LocExpr,
}
-impl LazyValValue for EvaluateNamedLazyVal {
+impl ThunkValue for EvaluateNamedThunk {
+ type Output = Val;
fn get(self: Box<Self>, s: State) -> Result<Val> {
evaluate_named(s, self.ctx.unwrap(), &self.value, self.name)
}
@@ -83,11 +84,11 @@
defaults.insert(
param.0.clone(),
- LazyVal::new(TraceBox(Box::new(EvaluateNamedLazyVal {
+ Thunk::new(tb!(EvaluateNamedThunk {
ctx: fctx.clone(),
name: param.0.clone(),
value: param.1.clone().expect("default exists"),
- }))),
+ })),
);
filled_args += 1;
}
@@ -131,7 +132,7 @@
params: &[BuiltinParam],
args: &dyn ArgsLike,
tailstrict: bool,
-) -> Result<GcHashMap<BuiltinParamName, LazyVal>> {
+) -> Result<GcHashMap<BuiltinParamName, Thunk<Val>>> {
let mut passed_args = GcHashMap::with_capacity(params.len());
if args.unnamed_len() > params.len() {
throw!(TooManyArgsFunctionHas(params.len()))
@@ -191,7 +192,8 @@
pub fn parse_default_function_call(body_ctx: Context, params: &ParamsDesc) -> Context {
#[derive(Trace)]
struct DependsOnUnbound(IStr);
- impl LazyValValue for DependsOnUnbound {
+ impl ThunkValue for DependsOnUnbound {
+ type Output = Val;
fn get(self: Box<Self>, _: State) -> Result<Val> {
Err(FunctionParameterNotBoundInCall(self.0.clone()).into())
}
@@ -205,16 +207,16 @@
if let Some(v) = ¶m.1 {
bindings.insert(
param.0.clone(),
- LazyVal::new(TraceBox(Box::new(EvaluateNamedLazyVal {
+ Thunk::new(tb!(EvaluateNamedThunk {
ctx: fctx.clone(),
name: param.0.clone(),
value: v.clone(),
- }))),
+ })),
);
} else {
bindings.insert(
param.0.clone(),
- LazyVal::new(TraceBox(Box::new(DependsOnUnbound(param.0.clone())))),
+ Thunk::new(tb!(DependsOnUnbound(param.0.clone()))),
);
}
}
crates/jrsonnet-evaluator/src/gc.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/gc.rs
+++ b/crates/jrsonnet-evaluator/src/gc.rs
@@ -10,8 +10,15 @@
use rustc_hash::{FxHashMap, FxHashSet};
/// Replacement for box, which assumes that the underlying type is [`Trace`]
+/// Used in places, where Cc<dyn Trait> should be used instead, but it can't, because CoerceUnsiced is not stable
#[derive(Debug, Clone)]
pub struct TraceBox<T: ?Sized>(pub Box<T>);
+#[macro_export]
+macro_rules! tb {
+ ($v:expr) => {
+ $crate::gc::TraceBox(Box::new($v))
+ };
+}
impl<T: ?Sized + Trace> Trace for TraceBox<T> {
fn trace(&self, tracer: &mut Tracer) {
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -58,7 +58,7 @@
use jrsonnet_parser::*;
pub use obj::*;
use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};
-pub use val::{LazyVal, ManifestFormat, Val};
+pub use val::{ManifestFormat, Thunk, Val};
pub trait Bindable: Trace + 'static {
fn bind(
@@ -66,13 +66,13 @@
s: State,
this: Option<ObjValue>,
super_obj: Option<ObjValue>,
- ) -> Result<LazyVal>;
+ ) -> Result<Thunk<Val>>;
}
#[derive(Clone, Trace)]
pub enum LazyBinding {
Bindable(Cc<TraceBox<dyn Bindable>>),
- Bound(LazyVal),
+ Bound(Thunk<Val>),
}
impl Debug for LazyBinding {
@@ -86,7 +86,7 @@
s: State,
this: Option<ObjValue>,
super_obj: Option<ObjValue>,
- ) -> Result<LazyVal> {
+ ) -> Result<Thunk<Val>> {
match self {
Self::Bindable(v) => v.bind(s, this, super_obj),
Self::Bound(v) => Ok(v.clone()),
@@ -343,7 +343,7 @@
let globals = &self.settings().globals;
let mut new_bindings = GcHashMap::with_capacity(globals.len());
for (name, value) in globals.iter() {
- new_bindings.insert(name.clone(), LazyVal::new_resolved(value.clone()));
+ new_bindings.insert(name.clone(), Thunk::evaluated(value.clone()));
}
Context::new().extend_bound(new_bindings)
}
crates/jrsonnet-evaluator/src/map.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/map.rs
+++ b/crates/jrsonnet-evaluator/src/map.rs
@@ -1,27 +1,27 @@
use gcmodule::{Cc, Trace};
use jrsonnet_interner::IStr;
-use crate::{GcHashMap, LazyVal};
+use crate::{GcHashMap, Thunk, Val};
#[derive(Trace)]
#[force_tracking]
pub struct LayeredHashMapInternals {
parent: Option<LayeredHashMap>,
- current: GcHashMap<IStr, LazyVal>,
+ current: GcHashMap<IStr, Thunk<Val>>,
}
#[derive(Trace)]
pub struct LayeredHashMap(Cc<LayeredHashMapInternals>);
impl LayeredHashMap {
- pub fn extend(self, new_layer: GcHashMap<IStr, LazyVal>) -> Self {
+ pub fn extend(self, new_layer: GcHashMap<IStr, Thunk<Val>>) -> Self {
Self(Cc::new(LayeredHashMapInternals {
parent: Some(self),
current: new_layer,
}))
}
- pub fn get(&self, key: &IStr) -> Option<&LazyVal> {
+ pub fn get(&self, key: &IStr) -> Option<&Thunk<Val>> {
(self.0)
.current
.get(key)
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -16,7 +16,7 @@
function::CallLocation,
gc::{GcHashMap, GcHashSet, TraceBox},
operator::evaluate_add_op,
- throw, weak_ptr_eq, weak_raw, Bindable, LazyBinding, LazyVal, Result, State, Val,
+ throw, weak_ptr_eq, weak_raw, Bindable, LazyBinding, Result, State, Thunk, Val,
};
#[cfg(not(feature = "exp-preserve-order"))]
@@ -581,7 +581,7 @@
pub struct ValueBuilder<'v>(&'v mut ObjValueBuilder);
impl<'v> ObjMemberBuilder<ValueBuilder<'v>> {
pub fn value(self, s: State, value: Val) -> Result<()> {
- self.binding(s, LazyBinding::Bound(LazyVal::new_resolved(value)))
+ self.binding(s, LazyBinding::Bound(Thunk::evaluated(value)))
}
pub fn bindable(self, s: State, bindable: TraceBox<dyn Bindable>) -> Result<()> {
self.binding(s, LazyBinding::Bindable(Cc::new(bindable)))
@@ -604,7 +604,7 @@
pub struct ExtendBuilder<'v>(&'v mut ObjValue);
impl<'v> ObjMemberBuilder<ExtendBuilder<'v>> {
pub fn value(self, value: Val) {
- self.binding(LazyBinding::Bound(LazyVal::new_resolved(value)));
+ self.binding(LazyBinding::Bound(Thunk::evaluated(value)));
}
pub fn bindable(self, bindable: TraceBox<dyn Bindable>) {
self.binding(LazyBinding::Bindable(Cc::new(bindable)));
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -15,41 +15,45 @@
throw, ObjValue, Result, State,
};
-pub trait LazyValValue: Trace {
- fn get(self: Box<Self>, s: State) -> Result<Val>;
+pub trait ThunkValue: Trace {
+ type Output;
+ fn get(self: Box<Self>, s: State) -> Result<Self::Output>;
}
#[derive(Trace)]
-enum LazyValInternals {
- Computed(Val),
+enum ThunkInner<T> {
+ Computed(T),
Errored(LocError),
- Waiting(TraceBox<dyn LazyValValue>),
+ Waiting(TraceBox<dyn ThunkValue<Output = T>>),
Pending,
}
#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Trace)]
-pub struct LazyVal(Cc<RefCell<LazyValInternals>>);
-impl LazyVal {
- pub fn new(f: TraceBox<dyn LazyValValue>) -> Self {
- Self(Cc::new(RefCell::new(LazyValInternals::Waiting(f))))
+pub struct Thunk<T>(Cc<RefCell<ThunkInner<T>>>);
+impl<T> Thunk<T>
+where
+ T: Clone + Trace,
+{
+ pub fn new(f: TraceBox<dyn ThunkValue<Output = T>>) -> Self {
+ Self(Cc::new(RefCell::new(ThunkInner::Waiting(f))))
}
- pub fn new_resolved(val: Val) -> Self {
- Self(Cc::new(RefCell::new(LazyValInternals::Computed(val))))
+ pub fn evaluated(val: T) -> Self {
+ Self(Cc::new(RefCell::new(ThunkInner::Computed(val))))
}
pub fn force(&self, s: State) -> Result<()> {
self.evaluate(s)?;
Ok(())
}
- pub fn evaluate(&self, s: State) -> Result<Val> {
+ pub fn evaluate(&self, s: State) -> Result<T> {
match &*self.0.borrow() {
- LazyValInternals::Computed(v) => return Ok(v.clone()),
- LazyValInternals::Errored(e) => return Err(e.clone()),
- LazyValInternals::Pending => return Err(InfiniteRecursionDetected.into()),
- LazyValInternals::Waiting(..) => (),
+ ThunkInner::Computed(v) => return Ok(v.clone()),
+ ThunkInner::Errored(e) => return Err(e.clone()),
+ ThunkInner::Pending => return Err(InfiniteRecursionDetected.into()),
+ ThunkInner::Waiting(..) => (),
};
- let value = if let LazyValInternals::Waiting(value) =
- std::mem::replace(&mut *self.0.borrow_mut(), LazyValInternals::Pending)
+ let value = if let ThunkInner::Waiting(value) =
+ std::mem::replace(&mut *self.0.borrow_mut(), ThunkInner::Pending)
{
value
} else {
@@ -58,21 +62,21 @@
let new_value = match value.0.get(s) {
Ok(v) => v,
Err(e) => {
- *self.0.borrow_mut() = LazyValInternals::Errored(e.clone());
+ *self.0.borrow_mut() = ThunkInner::Errored(e.clone());
return Err(e);
}
};
- *self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());
+ *self.0.borrow_mut() = ThunkInner::Computed(new_value.clone());
Ok(new_value)
}
}
-impl Debug for LazyVal {
+impl<T: Debug> Debug for Thunk<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Lazy")
}
}
-impl PartialEq for LazyVal {
+impl<T> PartialEq for Thunk<T> {
fn eq(&self, other: &Self) -> bool {
cc_ptr_eq(&self.0, &other.0)
}
@@ -142,7 +146,7 @@
#[force_tracking]
pub enum ArrValue {
Bytes(#[skip_trace] Rc<[u8]>),
- Lazy(Cc<Vec<LazyVal>>),
+ Lazy(Cc<Vec<Thunk<Val>>>),
Eager(Cc<Vec<Val>>),
Extended(Box<(Self, Self)>),
Range(i32, i32),
@@ -240,13 +244,13 @@
}
}
- pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {
+ pub fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
match self {
Self::Bytes(i) => i
.get(index)
- .map(|b| LazyVal::new_resolved(Val::Num(f64::from(*b)))),
+ .map(|b| Thunk::evaluated(Val::Num(f64::from(*b)))),
Self::Lazy(vec) => vec.get(index).cloned(),
- Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),
+ Self::Eager(vec) => vec.get(index).cloned().map(Thunk::evaluated),
Self::Extended(v) => {
let a_len = v.0.len();
if a_len > index {
@@ -259,7 +263,7 @@
if index >= self.len() {
return None;
}
- Some(LazyVal::new_resolved(Val::Num(
+ Some(Thunk::evaluated(Val::Num(
((*a as isize) + index as isize) as f64,
)))
}
@@ -343,11 +347,11 @@
})
}
- pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {
+ pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = Thunk<Val>> + '_ {
(0..self.len()).map(move |idx| match self {
- Self::Bytes(b) => LazyVal::new_resolved(Val::Num(f64::from(b[idx]))),
+ Self::Bytes(b) => Thunk::evaluated(Val::Num(f64::from(b[idx]))),
Self::Lazy(l) => l[idx].clone(),
- Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),
+ Self::Eager(e) => Thunk::evaluated(e[idx].clone()),
Self::Slice(..) | Self::Extended(..) | Self::Range(..) | Self::Reversed(..) => {
self.get_lazy(idx).expect("idx < len")
}
@@ -391,8 +395,8 @@
}
}
-impl From<Vec<LazyVal>> for ArrValue {
- fn from(v: Vec<LazyVal>) -> Self {
+impl From<Vec<Thunk<Val>>> for ArrValue {
+ fn from(v: Vec<Thunk<Val>>) -> Self {
Self::Lazy(Cc::new(v))
}
}
crates/jrsonnet-evaluator/tests/builtin.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/tests/builtin.rs
+++ b/crates/jrsonnet-evaluator/tests/builtin.rs
@@ -7,6 +7,7 @@
error::Result,
function::{builtin, builtin::Builtin, CallLocation, FuncVal},
gc::TraceBox,
+ tb,
typed::Typed,
State, Val,
};
@@ -70,9 +71,7 @@
#[builtin]
fn curry_add(a: u32) -> Result<FuncVal> {
- Ok(FuncVal::Builtin(Cc::new(TraceBox(Box::new(curried_add {
- a,
- })))))
+ Ok(FuncVal::Builtin(Cc::new(tb!(curried_add { a }))))
}
#[test]
crates/jrsonnet-evaluator/tests/common.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/tests/common.rs
+++ b/crates/jrsonnet-evaluator/tests/common.rs
@@ -1,7 +1,7 @@
use jrsonnet_evaluator::{
error::Result,
function::{builtin, FuncVal},
- throw_runtime, LazyVal, ObjValueBuilder, State, Val,
+ throw_runtime, ObjValueBuilder, State, Thunk, Val,
};
#[macro_export]
@@ -38,7 +38,7 @@
}
#[builtin]
-fn assert_throw(s: State, lazy: LazyVal, message: String) -> Result<bool> {
+fn assert_throw(s: State, lazy: Thunk<Val>, message: String) -> Result<bool> {
match lazy.evaluate(s) {
Ok(_) => {
throw_runtime!("expected argument to throw on evaluation, but it returned instead")
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -150,7 +150,7 @@
return Ok(Self::State);
} else if type_is_path(ty, "CallLocation").is_some() {
return Ok(Self::Location);
- } else if type_is_path(ty, "LazyVal").is_some() {
+ } else if type_is_path(ty, "Thunk").is_some() {
return Ok(Self::Lazy {
is_option: false,
name: ident.to_string(),
@@ -163,7 +163,7 @@
}
let (is_option, ty) = if let Some(ty) = extract_type_from_option(ty)? {
- if type_is_path(ty, "LazyVal").is_some() {
+ if type_is_path(ty, "Thunk").is_some() {
return Ok(Self::Lazy {
is_option: true,
name: ident.to_string(),
crates/jrsonnet-parser/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-parser/Cargo.toml
+++ b/crates/jrsonnet-parser/Cargo.toml
@@ -6,6 +6,9 @@
license = "MIT"
edition = "2021"
+[features]
+exp-destruct = []
+
[dependencies]
jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.2" }
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -179,10 +179,44 @@
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Trace)]
-pub struct BindSpec {
- pub name: IStr,
- pub params: Option<ParamsDesc>,
- pub value: LocExpr,
+pub enum DestructRest {
+ /// ...rest
+ Keep(IStr),
+ /// ...
+ Drop,
+}
+
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[derive(Debug, Clone, PartialEq, Trace)]
+pub enum Destruct {
+ Full(IStr),
+ #[cfg(feature = "exp-destruct")]
+ Skip,
+ #[cfg(feature = "exp-destruct")]
+ Array {
+ start: Vec<Destruct>,
+ rest: Option<DestructRest>,
+ end: Vec<Destruct>,
+ },
+ #[cfg(feature = "exp-destruct")]
+ Object {
+ fields: Vec<(IStr, Option<Destruct>)>,
+ rest: Option<DestructRest>,
+ },
+}
+
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[derive(Debug, Clone, PartialEq, Trace)]
+pub enum BindSpec {
+ Field {
+ into: Destruct,
+ value: LocExpr,
+ },
+ Function {
+ name: IStr,
+ params: ParamsDesc,
+ value: LocExpr,
+ },
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth1#![allow(clippy::redundant_closure_call)]23use std::{4 path::{Path, PathBuf},5 rc::Rc,6};78use peg::parser;9mod expr;10pub use expr::*;11pub use jrsonnet_interner::IStr;12pub use peg;13mod unescape;1415pub struct ParserSettings {16 pub file_name: Rc<Path>,17}1819macro_rules! expr_bin {20 ($a:ident $op:ident $b:ident) => {21 Expr::BinaryOp($a, $op, $b)22 };23}24macro_rules! expr_un {25 ($op:ident $a:ident) => {26 Expr::UnaryOp($op, $a)27 };28}2930parser! {31 grammar jsonnet_parser() for str {32 use peg::ParseLiteral;3334 rule eof() = quiet!{![_]} / expected!("<eof>")35 rule eol() = "\n" / eof()3637 /// Standard C-like comments38 rule comment()39 = "//" (!eol()[_])* eol()40 / "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"41 / "#" (!eol()[_])* eol()4243 rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")44 rule _() = quiet!{([' ' | '\r' | '\n' | '\t']+) / comment()}* / expected!("<whitespace>")4546 /// For comma-delimited elements47 rule comma() = quiet!{_ "," _} / expected!("<comma>")48 rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}49 rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}50 rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']51 /// Sequence of digits52 rule uint_str() -> &'input str = a:$(digit()+) { a }53 /// Number in scientific notation format54 rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")5556 /// Reserved word followed by any non-alphanumberic57 rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()58 rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")5960 rule keyword(id: &'static str) -> ()61 = ##parse_string_literal(id) end_of_ident()6263 pub rule param(s: &ParserSettings) -> expr::Param = name:$(id()) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name.into(), expr) }64 pub rule params(s: &ParserSettings) -> expr::ParamsDesc65 = params:param(s) ** comma() comma()? { expr::ParamsDesc(Rc::new(params)) }66 / { expr::ParamsDesc(Rc::new(Vec::new())) }6768 pub rule arg(s: &ParserSettings) -> (Option<IStr>, LocExpr)69 = quiet! { name:(s:$(id()) _ "=" _ {s})? expr:expr(s) {(name.map(Into::into), expr)} }70 / expected!("<argument>")7172 pub rule args(s: &ParserSettings) -> expr::ArgsDesc73 = args:arg(s)**comma() comma()? {?74 let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();75 let mut unnamed = Vec::with_capacity(unnamed_count);76 let mut named = Vec::with_capacity(args.len() - unnamed_count);77 let mut named_started = false;78 for (name, value) in args {79 if let Some(name) = name {80 named_started = true;81 named.push((name, value));82 } else {83 if named_started {84 return Err("<named argument>")85 }86 unnamed.push(value);87 }88 }89 Ok(expr::ArgsDesc::new(unnamed, named))90 }9192 pub rule bind(s: &ParserSettings) -> expr::BindSpec93 = name:$(id()) _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: None, value: expr}}94 / name:$(id()) _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: Some(params), value: expr}}95 pub rule assertion(s: &ParserSettings) -> expr::AssertStmt96 = keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }9798 pub rule whole_line() -> &'input str99 = str:$((!['\n'][_])* "\n") {str}100 pub rule string_block() -> String101 = "|||" (!['\n']single_whitespace())* "\n"102 empty_lines:$(['\n']*)103 prefix:[' ' | '\t']+ first_line:whole_line()104 lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*105 [' ' | '\t']*<, {prefix.len() - 1}> "|||"106 {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}107108 rule hex_char()109 = quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")110111 rule string_char(c: rule<()>)112 = (!['\\']!c()[_])+113 / "\\\\"114 / "\\u" hex_char() hex_char() hex_char() hex_char()115 / "\\x" hex_char() hex_char()116 / ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't' | '"' | '\''] } / expected!("<escape character>"))117 pub rule string() -> String118 = ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}119 / ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}120 / quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}121 / "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}122 / string_block() } / expected!("<string>")123124 pub rule field_name(s: &ParserSettings) -> expr::FieldName125 = name:$(id()) {expr::FieldName::Fixed(name.into())}126 / name:string() {expr::FieldName::Fixed(name.into())}127 / "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}128 pub rule visibility() -> expr::Visibility129 = ":::" {expr::Visibility::Unhide}130 / "::" {expr::Visibility::Hidden}131 / ":" {expr::Visibility::Normal}132 pub rule field(s: &ParserSettings) -> expr::FieldMember133 = name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{134 name,135 plus: plus.is_some(),136 params: None,137 visibility,138 value,139 }}140 / name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{141 name,142 plus: false,143 params: Some(params),144 visibility,145 value,146 }}147 pub rule obj_local(s: &ParserSettings) -> BindSpec148 = keyword("local") _ bind:bind(s) {bind}149 pub rule member(s: &ParserSettings) -> expr::Member150 = bind:obj_local(s) {expr::Member::BindStmt(bind)}151 / assertion:assertion(s) {expr::Member::AssertStmt(assertion)}152 / field:field(s) {expr::Member::Field(field)}153 pub rule objinside(s: &ParserSettings) -> expr::ObjBody154 = pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ plus:"+"? _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {155 let mut compspecs = vec![CompSpec::ForSpec(forspec)];156 compspecs.extend(others.unwrap_or_default());157 expr::ObjBody::ObjComp(expr::ObjComp{158 pre_locals,159 key,160 plus: plus.is_some(),161 value,162 post_locals,163 compspecs,164 })165 }166 / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}167 pub rule ifspec(s: &ParserSettings) -> IfSpecData168 = keyword("if") _ expr:expr(s) {IfSpecData(expr)}169 pub rule forspec(s: &ParserSettings) -> ForSpecData170 = keyword("for") _ id:$(id()) _ keyword("in") _ cond:expr(s) {ForSpecData(id.into(), cond)}171 pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>172 = s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}173 pub rule local_expr(s: &ParserSettings) -> Expr174 = keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }175 pub rule string_expr(s: &ParserSettings) -> Expr176 = s:string() {Expr::Str(s.into())}177 pub rule obj_expr(s: &ParserSettings) -> Expr178 = "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}179 pub rule array_expr(s: &ParserSettings) -> Expr180 = "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}181 pub rule array_comp_expr(s: &ParserSettings) -> Expr182 = "[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {183 let mut specs = vec![CompSpec::ForSpec(forspec)];184 specs.extend(others.unwrap_or_default());185 Expr::ArrComp(expr, specs)186 }187 pub rule number_expr(s: &ParserSettings) -> Expr188 = n:number() { expr::Expr::Num(n) }189 pub rule var_expr(s: &ParserSettings) -> Expr190 = n:$(id()) { expr::Expr::Var(n.into()) }191 pub rule id_loc(s: &ParserSettings) -> LocExpr192 = a:position!() n:$(id()) b:position!() { LocExpr(Rc::new(expr::Expr::Str(n.into())), ExprLocation(s.file_name.clone(), a,b)) }193 pub rule if_then_else_expr(s: &ParserSettings) -> Expr194 = cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{195 cond,196 cond_then,197 cond_else,198 }}199200 pub rule literal(s: &ParserSettings) -> Expr201 = v:(202 keyword("null") {LiteralType::Null}203 / keyword("true") {LiteralType::True}204 / keyword("false") {LiteralType::False}205 / keyword("self") {LiteralType::This}206 / keyword("$") {LiteralType::Dollar}207 / keyword("super") {LiteralType::Super}208 ) {Expr::Literal(v)}209210 pub rule expr_basic(s: &ParserSettings) -> Expr211 = literal(s)212213 / quiet!{"$intrinsicThisFile" {Expr::IntrinsicThisFile}}214 / quiet!{"$intrinsicId" {Expr::IntrinsicId}}215 / quiet!{"$intrinsic(" name:$(id()) ")" {Expr::Intrinsic(name.into())}}216217 / string_expr(s) / number_expr(s)218 / array_expr(s)219 / obj_expr(s)220 / array_expr(s)221 / array_comp_expr(s)222223 / keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}224 / keyword("importbin") _ path:string() {Expr::ImportBin(PathBuf::from(path))}225 / keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}226227 / var_expr(s)228 / local_expr(s)229 / if_then_else_expr(s)230231 / keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}232 / assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }233234 / keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }235236 rule slice_part(s: &ParserSettings) -> Option<LocExpr>237 = _ e:(e:expr(s) _{e})? {e}238 pub rule slice_desc(s: &ParserSettings) -> SliceDesc239 = start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {240 let (end, step) = if let Some((end, step)) = pair {241 (end, step)242 }else{243 (None, None)244 };245246 SliceDesc { start, end, step }247 }248249 rule binop(x: rule<()>) -> ()250 = quiet!{ x() } / expected!("<binary op>")251 rule unaryop(x: rule<()>) -> ()252 = quiet!{ x() } / expected!("<unary op>")253254255 use BinaryOpType::*;256 use UnaryOpType::*;257 rule expr(s: &ParserSettings) -> LocExpr258 = precedence! {259 start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.file_name.clone(), start, end)) }260 --261 a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}262 --263 a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}264 --265 a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}266 --267 a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}268 --269 a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}270 --271 a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}272 a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}273 --274 a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}275 a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}276 a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}277 a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}278 a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}279 --280 a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}281 a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}282 --283 a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}284 a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}285 --286 a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}287 a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}288 a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}289 --290 unaryop(<"-">) _ b:@ {expr_un!(Minus b)}291 unaryop(<"!">) _ b:@ {expr_un!(Not b)}292 unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}293 --294 a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}295 a:(@) _ "." _ a:position!() e:id_loc(s) b:position!() {Expr::Index(a, e)}296 a:(@) _ "[" _ e:expr(s) _ "]" {Expr::Index(a, e)}297 a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}298 a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}299 --300 e:expr_basic(s) {e}301 "(" _ e:expr(s) _ ")" {Expr::Parened(e)}302 }303304 pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}305 }306}307308pub type ParseError = peg::error::ParseError<peg::str::LineCol>;309pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {310 jsonnet_parser::jsonnet(str, settings)311}312/// Used for importstr values313pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {314 let len = str.len();315 LocExpr(316 Rc::new(Expr::Str(str)),317 ExprLocation(settings.file_name.clone(), 0, len),318 )319}320321#[cfg(test)]322pub mod tests {323 use std::path::PathBuf;324325 use BinaryOpType::*;326327 use super::{expr::*, parse};328 use crate::ParserSettings;329330 macro_rules! parse {331 ($s:expr) => {332 parse(333 $s,334 &ParserSettings {335 file_name: PathBuf::from("test.jsonnet").into(),336 },337 )338 .unwrap()339 };340 }341342 macro_rules! el {343 ($expr:expr, $from:expr, $to:expr$(,)?) => {344 LocExpr(345 std::rc::Rc::new($expr),346 ExprLocation(PathBuf::from("test.jsonnet").into(), $from, $to),347 )348 };349 }350351 #[test]352 fn multiline_string() {353 assert_eq!(354 parse!("|||\n Hello world!\n a\n|||"),355 el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),356 );357 assert_eq!(358 parse!("|||\n Hello world!\n a\n|||"),359 el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),360 );361 assert_eq!(362 parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),363 el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),364 );365 assert_eq!(366 parse!("|||\n Hello world!\n a\n |||"),367 el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),368 );369 }370371 #[test]372 fn slice() {373 parse!("a[1:]");374 parse!("a[1::]");375 parse!("a[:1:]");376 parse!("a[::1]");377 parse!("str[:len - 1]");378 }379380 #[test]381 fn string_escaping() {382 assert_eq!(383 parse!(r#""Hello, \"world\"!""#),384 el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),385 );386 assert_eq!(387 parse!(r#"'Hello \'world\'!'"#),388 el!(Expr::Str("Hello 'world'!".into()), 0, 18),389 );390 assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));391 }392393 #[test]394 fn string_unescaping() {395 assert_eq!(396 parse!(r#""Hello\nWorld""#),397 el!(Expr::Str("Hello\nWorld".into()), 0, 14),398 );399 }400401 #[test]402 fn string_verbantim() {403 assert_eq!(404 parse!(r#"@"Hello\n""World""""#),405 el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),406 );407 }408409 #[test]410 fn imports() {411 assert_eq!(412 parse!("import \"hello\""),413 el!(Expr::Import(PathBuf::from("hello")), 0, 14),414 );415 assert_eq!(416 parse!("importstr \"garnish.txt\""),417 el!(Expr::ImportStr(PathBuf::from("garnish.txt")), 0, 23)418 );419 }420421 #[test]422 fn empty_object() {423 assert_eq!(424 parse!("{}"),425 el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)426 );427 }428429 #[test]430 fn basic_math() {431 assert_eq!(432 parse!("2+2*2"),433 el!(434 Expr::BinaryOp(435 el!(Expr::Num(2.0), 0, 1),436 Add,437 el!(438 Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),439 2,440 5441 )442 ),443 0,444 5445 )446 );447 }448449 #[test]450 fn basic_math_with_indents() {451 assert_eq!(452 parse!("2 + 2 * 2 "),453 el!(454 Expr::BinaryOp(455 el!(Expr::Num(2.0), 0, 1),456 Add,457 el!(458 Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),459 7,460 14461 ),462 ),463 0,464 14465 )466 );467 }468469 #[test]470 fn basic_math_parened() {471 assert_eq!(472 parse!("2+(2+2*2)"),473 el!(474 Expr::BinaryOp(475 el!(Expr::Num(2.0), 0, 1),476 Add,477 el!(478 Expr::Parened(el!(479 Expr::BinaryOp(480 el!(Expr::Num(2.0), 3, 4),481 Add,482 el!(483 Expr::BinaryOp(484 el!(Expr::Num(2.0), 5, 6),485 Mul,486 el!(Expr::Num(2.0), 7, 8),487 ),488 5,489 8490 ),491 ),492 3,493 8494 )),495 2,496 9497 ),498 ),499 0,500 9501 )502 );503 }504505 /// Comments should not affect parsing506 #[test]507 fn comments() {508 assert_eq!(509 parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),510 el!(511 Expr::BinaryOp(512 el!(Expr::Num(2.0), 0, 1),513 Add,514 el!(515 Expr::BinaryOp(516 el!(Expr::Num(3.0), 22, 23),517 Mul,518 el!(Expr::Num(4.0), 40, 41)519 ),520 22,521 41522 )523 ),524 0,525 41526 )527 );528 }529530 /// Comments should be able to be escaped531 #[test]532 fn comment_escaping() {533 assert_eq!(534 parse!("2/*\\*/+*/ - 22"),535 el!(536 Expr::BinaryOp(el!(Expr::Num(2.0), 0, 1), Sub, el!(Expr::Num(22.0), 12, 14)),537 0,538 14539 )540 );541 }542543 #[test]544 fn suffix() {545 // assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));546 // assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));547 // assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));548 // assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))549 }550551 #[test]552 fn array_comp() {553 use Expr::*;554 /*555 `ArrComp(Apply(Index(Var("std") from "test.jsonnet":1-4, Var("deepJoin") from "test.jsonnet":5-13) from "test.jsonnet":1-13, ArgsDesc { unnamed: [Var("x") from "test.jsonnet":14-15], named: [] }, false) from "test.jsonnet":1-16, [ForSpec(ForSpecData("x", Var("arr") from "test.jsonnet":26-29))]) from "test.jsonnet":0-30`,556 `ArrComp(Apply(Index(Var("std") from "test.jsonnet":1-4, Str("deepJoin") from "test.jsonnet":5-13) from "test.jsonnet":1-13, ArgsDesc { unnamed: [Var("x") from "test.jsonnet":14-15], named: [] }, false) from "test.jsonnet":1-16, [ForSpec(ForSpecData("x", Var("arr") from "test.jsonnet":26-29))]) from "test.jsonnet":0-30`557 */558 assert_eq!(559 parse!("[std.deepJoin(x) for x in arr]"),560 el!(561 ArrComp(562 el!(563 Apply(564 el!(565 Index(566 el!(Var("std".into()), 1, 4),567 el!(Str("deepJoin".into()), 5, 13)568 ),569 1,570 13571 ),572 ArgsDesc::new(vec![el!(Var("x".into()), 14, 15)], vec![]),573 false,574 ),575 1,576 16577 ),578 vec![CompSpec::ForSpec(ForSpecData(579 "x".into(),580 el!(Var("arr".into()), 26, 29)581 ))]582 ),583 0,584 30585 ),586 )587 }588589 #[test]590 fn reserved() {591 use Expr::*;592 assert_eq!(parse!("null"), el!(Literal(LiteralType::Null), 0, 4));593 assert_eq!(parse!("nulla"), el!(Var("nulla".into()), 0, 5));594 }595596 #[test]597 fn multiple_args_buf() {598 parse!("a(b, null_fields)");599 }600601 #[test]602 fn infix_precedence() {603 use Expr::*;604 assert_eq!(605 parse!("!a && !b"),606 el!(607 BinaryOp(608 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),609 And,610 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 7, 8)), 6, 8)611 ),612 0,613 8614 )615 );616 }617618 #[test]619 fn infix_precedence_division() {620 use Expr::*;621 assert_eq!(622 parse!("!a / !b"),623 el!(624 BinaryOp(625 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),626 Div,627 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 6, 7)), 5, 7)628 ),629 0,630 7631 )632 );633 }634635 #[test]636 fn double_negation() {637 use Expr::*;638 assert_eq!(639 parse!("!!a"),640 el!(641 UnaryOp(642 UnaryOpType::Not,643 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 2, 3)), 1, 3)644 ),645 0,646 3647 )648 )649 }650651 #[test]652 fn array_test_error() {653 parse!("[a for a in b if c for e in f]");654 // ^^^^ failed code655 }656657 #[test]658 fn missing_newline_between_comment_and_eof() {659 parse!(660 "{a:1}661662 //+213"663 );664 }665666 #[test]667 fn default_param_before_nondefault() {668 parse!("local x(foo = 'foo', bar) = null; null");669 }670671 #[test]672 fn can_parse_stdlib() {673 parse!(jrsonnet_stdlib::STDLIB_STR);674 }675676 #[test]677 fn add_location_info_to_all_sub_expressions() {678 use Expr::*;679680 let file_name: std::rc::Rc<std::path::Path> = PathBuf::from("test.jsonnet").into();681 let expr = parse(682 "{} { local x = 1, x: x } + {}",683 &ParserSettings {684 file_name: file_name.clone(),685 },686 )687 .unwrap();688 assert_eq!(689 expr,690 el!(691 BinaryOp(692 el!(693 ObjExtend(694 el!(Obj(ObjBody::MemberList(vec![])), 0, 2),695 ObjBody::MemberList(vec![696 Member::BindStmt(BindSpec {697 name: "x".into(),698 params: None,699 value: el!(Num(1.0), 15, 16)700 }),701 Member::Field(FieldMember {702 name: FieldName::Fixed("x".into()),703 plus: false,704 params: None,705 visibility: Visibility::Normal,706 value: el!(Var("x".into()), 21, 22),707 })708 ])709 ),710 0,711 24712 ),713 BinaryOpType::Add,714 el!(Obj(ObjBody::MemberList(vec![])), 27, 29),715 ),716 0,717 29718 ),719 );720 }721 // From source code722 /*723 #[bench]724 fn bench_parse_peg(b: &mut Bencher) {725 b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))726 }727 */728}1#![allow(clippy::redundant_closure_call)]23use std::{4 path::{Path, PathBuf},5 rc::Rc,6};78use peg::parser;9mod expr;10pub use expr::*;11pub use jrsonnet_interner::IStr;12pub use peg;13mod unescape;1415pub struct ParserSettings {16 pub file_name: Rc<Path>,17}1819macro_rules! expr_bin {20 ($a:ident $op:ident $b:ident) => {21 Expr::BinaryOp($a, $op, $b)22 };23}24macro_rules! expr_un {25 ($op:ident $a:ident) => {26 Expr::UnaryOp($op, $a)27 };28}2930parser! {31 grammar jsonnet_parser() for str {32 use peg::ParseLiteral;3334 rule eof() = quiet!{![_]} / expected!("<eof>")35 rule eol() = "\n" / eof()3637 /// Standard C-like comments38 rule comment()39 = "//" (!eol()[_])* eol()40 / "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"41 / "#" (!eol()[_])* eol()4243 rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")44 rule _() = quiet!{([' ' | '\r' | '\n' | '\t']+) / comment()}* / expected!("<whitespace>")4546 /// For comma-delimited elements47 rule comma() = quiet!{_ "," _} / expected!("<comma>")48 rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}49 rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}50 rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']51 /// Sequence of digits52 rule uint_str() -> &'input str = a:$(digit()+) { a }53 /// Number in scientific notation format54 rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")5556 /// Reserved word followed by any non-alphanumberic57 rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()58 rule id() -> IStr = v:$(quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")) { v.into() }5960 rule keyword(id: &'static str) -> ()61 = ##parse_string_literal(id) end_of_ident()6263 pub rule param(s: &ParserSettings) -> expr::Param = name:id() expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr) }64 pub rule params(s: &ParserSettings) -> expr::ParamsDesc65 = params:param(s) ** comma() comma()? { expr::ParamsDesc(Rc::new(params)) }66 / { expr::ParamsDesc(Rc::new(Vec::new())) }6768 pub rule arg(s: &ParserSettings) -> (Option<IStr>, LocExpr)69 = quiet! { name:(s:id() _ "=" _ {s})? expr:expr(s) {(name, expr)} }70 / expected!("<argument>")7172 pub rule args(s: &ParserSettings) -> expr::ArgsDesc73 = args:arg(s)**comma() comma()? {?74 let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();75 let mut unnamed = Vec::with_capacity(unnamed_count);76 let mut named = Vec::with_capacity(args.len() - unnamed_count);77 let mut named_started = false;78 for (name, value) in args {79 if let Some(name) = name {80 named_started = true;81 named.push((name, value));82 } else {83 if named_started {84 return Err("<named argument>")85 }86 unnamed.push(value);87 }88 }89 Ok(expr::ArgsDesc::new(unnamed, named))90 }9192 pub rule destruct_rest() -> expr::DestructRest93 = "..." into:(_ into:id() {into})? {if let Some(into) = into {94 expr::DestructRest::Keep(into)95 } else {expr::DestructRest::Drop}}96 pub rule destruct_array(s: &ParserSettings) -> expr::Destruct97 = "[" _ start:destruct(s)**comma() rest:(98 comma() _ rest:destruct_rest()? end:(99 comma() end:destruct(s)**comma() (_ comma())? {end}100 / comma()? {Vec::new()}101 ) {(rest, end)}102 / comma()? {(None, Vec::new())}103 ) _ "]" {?104 #[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Array {105 start,106 rest: rest.0,107 end: rest.1,108 });109 #[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")110 }111 pub rule destruct_object(s: &ParserSettings) -> expr::Destruct112 = "{" _113 fields:(name:id() _ into:(":" _ into:destruct(s) {into})? {(name, into)})**comma()114 rest:(115 comma() rest:destruct_rest()? {rest}116 / comma()? {None}117 )118 _ "}" {?119 #[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Object {120 fields,121 rest,122 });123 #[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")124 }125 pub rule destruct(s: &ParserSettings) -> expr::Destruct126 = v:id() {expr::Destruct::Full(v)}127 / "?" {?128 #[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Skip);129 #[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")130 }131 / arr:destruct_array(s) {arr}132 / obj:destruct_object(s) {obj}133134 pub rule bind(s: &ParserSettings) -> expr::BindSpec135 = into:destruct(s) _ "=" _ expr:expr(s) {expr::BindSpec::Field{into, value: expr}}136 / name:id() _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec::Function{name, params, value: expr}}137138 pub rule assertion(s: &ParserSettings) -> expr::AssertStmt139 = keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }140141 pub rule whole_line() -> &'input str142 = str:$((!['\n'][_])* "\n") {str}143 pub rule string_block() -> String144 = "|||" (!['\n']single_whitespace())* "\n"145 empty_lines:$(['\n']*)146 prefix:[' ' | '\t']+ first_line:whole_line()147 lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*148 [' ' | '\t']*<, {prefix.len() - 1}> "|||"149 {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}150151 rule hex_char()152 = quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")153154 rule string_char(c: rule<()>)155 = (!['\\']!c()[_])+156 / "\\\\"157 / "\\u" hex_char() hex_char() hex_char() hex_char()158 / "\\x" hex_char() hex_char()159 / ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't' | '"' | '\''] } / expected!("<escape character>"))160 pub rule string() -> String161 = ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}162 / ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}163 / quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}164 / "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}165 / string_block() } / expected!("<string>")166167 pub rule field_name(s: &ParserSettings) -> expr::FieldName168 = name:id() {expr::FieldName::Fixed(name.into())}169 / name:string() {expr::FieldName::Fixed(name.into())}170 / "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}171 pub rule visibility() -> expr::Visibility172 = ":::" {expr::Visibility::Unhide}173 / "::" {expr::Visibility::Hidden}174 / ":" {expr::Visibility::Normal}175 pub rule field(s: &ParserSettings) -> expr::FieldMember176 = name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{177 name,178 plus: plus.is_some(),179 params: None,180 visibility,181 value,182 }}183 / name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{184 name,185 plus: false,186 params: Some(params),187 visibility,188 value,189 }}190 pub rule obj_local(s: &ParserSettings) -> BindSpec191 = keyword("local") _ bind:bind(s) {bind}192 pub rule member(s: &ParserSettings) -> expr::Member193 = bind:obj_local(s) {expr::Member::BindStmt(bind)}194 / assertion:assertion(s) {expr::Member::AssertStmt(assertion)}195 / field:field(s) {expr::Member::Field(field)}196 pub rule objinside(s: &ParserSettings) -> expr::ObjBody197 = pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ plus:"+"? _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {198 let mut compspecs = vec![CompSpec::ForSpec(forspec)];199 compspecs.extend(others.unwrap_or_default());200 expr::ObjBody::ObjComp(expr::ObjComp{201 pre_locals,202 key,203 plus: plus.is_some(),204 value,205 post_locals,206 compspecs,207 })208 }209 / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}210 pub rule ifspec(s: &ParserSettings) -> IfSpecData211 = keyword("if") _ expr:expr(s) {IfSpecData(expr)}212 pub rule forspec(s: &ParserSettings) -> ForSpecData213 = keyword("for") _ id:id() _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}214 pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>215 = s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}216 pub rule local_expr(s: &ParserSettings) -> Expr217 = keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }218 pub rule string_expr(s: &ParserSettings) -> Expr219 = s:string() {Expr::Str(s.into())}220 pub rule obj_expr(s: &ParserSettings) -> Expr221 = "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}222 pub rule array_expr(s: &ParserSettings) -> Expr223 = "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}224 pub rule array_comp_expr(s: &ParserSettings) -> Expr225 = "[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {226 let mut specs = vec![CompSpec::ForSpec(forspec)];227 specs.extend(others.unwrap_or_default());228 Expr::ArrComp(expr, specs)229 }230 pub rule number_expr(s: &ParserSettings) -> Expr231 = n:number() { expr::Expr::Num(n) }232 pub rule var_expr(s: &ParserSettings) -> Expr233 = n:id() { expr::Expr::Var(n) }234 pub rule id_loc(s: &ParserSettings) -> LocExpr235 = a:position!() n:id() b:position!() { LocExpr(Rc::new(expr::Expr::Str(n)), ExprLocation(s.file_name.clone(), a,b)) }236 pub rule if_then_else_expr(s: &ParserSettings) -> Expr237 = cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{238 cond,239 cond_then,240 cond_else,241 }}242243 pub rule literal(s: &ParserSettings) -> Expr244 = v:(245 keyword("null") {LiteralType::Null}246 / keyword("true") {LiteralType::True}247 / keyword("false") {LiteralType::False}248 / keyword("self") {LiteralType::This}249 / keyword("$") {LiteralType::Dollar}250 / keyword("super") {LiteralType::Super}251 ) {Expr::Literal(v)}252253 pub rule expr_basic(s: &ParserSettings) -> Expr254 = literal(s)255256 / quiet!{"$intrinsicThisFile" {Expr::IntrinsicThisFile}}257 / quiet!{"$intrinsicId" {Expr::IntrinsicId}}258 / quiet!{"$intrinsic(" name:id() ")" {Expr::Intrinsic(name)}}259260 / string_expr(s) / number_expr(s)261 / array_expr(s)262 / obj_expr(s)263 / array_expr(s)264 / array_comp_expr(s)265266 / keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}267 / keyword("importbin") _ path:string() {Expr::ImportBin(PathBuf::from(path))}268 / keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}269270 / var_expr(s)271 / local_expr(s)272 / if_then_else_expr(s)273274 / keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}275 / assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }276277 / keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }278279 rule slice_part(s: &ParserSettings) -> Option<LocExpr>280 = _ e:(e:expr(s) _{e})? {e}281 pub rule slice_desc(s: &ParserSettings) -> SliceDesc282 = start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {283 let (end, step) = if let Some((end, step)) = pair {284 (end, step)285 }else{286 (None, None)287 };288289 SliceDesc { start, end, step }290 }291292 rule binop(x: rule<()>) -> ()293 = quiet!{ x() } / expected!("<binary op>")294 rule unaryop(x: rule<()>) -> ()295 = quiet!{ x() } / expected!("<unary op>")296297298 use BinaryOpType::*;299 use UnaryOpType::*;300 rule expr(s: &ParserSettings) -> LocExpr301 = precedence! {302 start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.file_name.clone(), start, end)) }303 --304 a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}305 --306 a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}307 --308 a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}309 --310 a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}311 --312 a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}313 --314 a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}315 a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}316 --317 a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}318 a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}319 a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}320 a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}321 a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}322 --323 a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}324 a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}325 --326 a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}327 a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}328 --329 a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}330 a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}331 a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}332 --333 unaryop(<"-">) _ b:@ {expr_un!(Minus b)}334 unaryop(<"!">) _ b:@ {expr_un!(Not b)}335 unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}336 --337 a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}338 a:(@) _ "." _ a:position!() e:id_loc(s) b:position!() {Expr::Index(a, e)}339 a:(@) _ "[" _ e:expr(s) _ "]" {Expr::Index(a, e)}340 a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}341 a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}342 --343 e:expr_basic(s) {e}344 "(" _ e:expr(s) _ ")" {Expr::Parened(e)}345 }346347 pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}348 }349}350351pub type ParseError = peg::error::ParseError<peg::str::LineCol>;352pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {353 jsonnet_parser::jsonnet(str, settings)354}355/// Used for importstr values356pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {357 let len = str.len();358 LocExpr(359 Rc::new(Expr::Str(str)),360 ExprLocation(settings.file_name.clone(), 0, len),361 )362}363364#[cfg(test)]365pub mod tests {366 use std::path::PathBuf;367368 use BinaryOpType::*;369370 use super::{expr::*, parse};371 use crate::ParserSettings;372373 macro_rules! parse {374 ($s:expr) => {375 parse(376 $s,377 &ParserSettings {378 file_name: PathBuf::from("test.jsonnet").into(),379 },380 )381 .unwrap()382 };383 }384385 macro_rules! el {386 ($expr:expr, $from:expr, $to:expr$(,)?) => {387 LocExpr(388 std::rc::Rc::new($expr),389 ExprLocation(PathBuf::from("test.jsonnet").into(), $from, $to),390 )391 };392 }393394 #[test]395 fn multiline_string() {396 assert_eq!(397 parse!("|||\n Hello world!\n a\n|||"),398 el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),399 );400 assert_eq!(401 parse!("|||\n Hello world!\n a\n|||"),402 el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),403 );404 assert_eq!(405 parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),406 el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),407 );408 assert_eq!(409 parse!("|||\n Hello world!\n a\n |||"),410 el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),411 );412 }413414 #[test]415 fn slice() {416 parse!("a[1:]");417 parse!("a[1::]");418 parse!("a[:1:]");419 parse!("a[::1]");420 parse!("str[:len - 1]");421 }422423 #[test]424 fn string_escaping() {425 assert_eq!(426 parse!(r#""Hello, \"world\"!""#),427 el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),428 );429 assert_eq!(430 parse!(r#"'Hello \'world\'!'"#),431 el!(Expr::Str("Hello 'world'!".into()), 0, 18),432 );433 assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));434 }435436 #[test]437 fn string_unescaping() {438 assert_eq!(439 parse!(r#""Hello\nWorld""#),440 el!(Expr::Str("Hello\nWorld".into()), 0, 14),441 );442 }443444 #[test]445 fn string_verbantim() {446 assert_eq!(447 parse!(r#"@"Hello\n""World""""#),448 el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),449 );450 }451452 #[test]453 fn imports() {454 assert_eq!(455 parse!("import \"hello\""),456 el!(Expr::Import(PathBuf::from("hello")), 0, 14),457 );458 assert_eq!(459 parse!("importstr \"garnish.txt\""),460 el!(Expr::ImportStr(PathBuf::from("garnish.txt")), 0, 23)461 );462 }463464 #[test]465 fn empty_object() {466 assert_eq!(467 parse!("{}"),468 el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)469 );470 }471472 #[test]473 fn basic_math() {474 assert_eq!(475 parse!("2+2*2"),476 el!(477 Expr::BinaryOp(478 el!(Expr::Num(2.0), 0, 1),479 Add,480 el!(481 Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),482 2,483 5484 )485 ),486 0,487 5488 )489 );490 }491492 #[test]493 fn basic_math_with_indents() {494 assert_eq!(495 parse!("2 + 2 * 2 "),496 el!(497 Expr::BinaryOp(498 el!(Expr::Num(2.0), 0, 1),499 Add,500 el!(501 Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),502 7,503 14504 ),505 ),506 0,507 14508 )509 );510 }511512 #[test]513 fn basic_math_parened() {514 assert_eq!(515 parse!("2+(2+2*2)"),516 el!(517 Expr::BinaryOp(518 el!(Expr::Num(2.0), 0, 1),519 Add,520 el!(521 Expr::Parened(el!(522 Expr::BinaryOp(523 el!(Expr::Num(2.0), 3, 4),524 Add,525 el!(526 Expr::BinaryOp(527 el!(Expr::Num(2.0), 5, 6),528 Mul,529 el!(Expr::Num(2.0), 7, 8),530 ),531 5,532 8533 ),534 ),535 3,536 8537 )),538 2,539 9540 ),541 ),542 0,543 9544 )545 );546 }547548 /// Comments should not affect parsing549 #[test]550 fn comments() {551 assert_eq!(552 parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),553 el!(554 Expr::BinaryOp(555 el!(Expr::Num(2.0), 0, 1),556 Add,557 el!(558 Expr::BinaryOp(559 el!(Expr::Num(3.0), 22, 23),560 Mul,561 el!(Expr::Num(4.0), 40, 41)562 ),563 22,564 41565 )566 ),567 0,568 41569 )570 );571 }572573 /// Comments should be able to be escaped574 #[test]575 fn comment_escaping() {576 assert_eq!(577 parse!("2/*\\*/+*/ - 22"),578 el!(579 Expr::BinaryOp(el!(Expr::Num(2.0), 0, 1), Sub, el!(Expr::Num(22.0), 12, 14)),580 0,581 14582 )583 );584 }585586 #[test]587 fn suffix() {588 // assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));589 // assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));590 // assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));591 // assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))592 }593594 #[test]595 fn array_comp() {596 use Expr::*;597 /*598 `ArrComp(Apply(Index(Var("std") from "test.jsonnet":1-4, Var("deepJoin") from "test.jsonnet":5-13) from "test.jsonnet":1-13, ArgsDesc { unnamed: [Var("x") from "test.jsonnet":14-15], named: [] }, false) from "test.jsonnet":1-16, [ForSpec(ForSpecData("x", Var("arr") from "test.jsonnet":26-29))]) from "test.jsonnet":0-30`,599 `ArrComp(Apply(Index(Var("std") from "test.jsonnet":1-4, Str("deepJoin") from "test.jsonnet":5-13) from "test.jsonnet":1-13, ArgsDesc { unnamed: [Var("x") from "test.jsonnet":14-15], named: [] }, false) from "test.jsonnet":1-16, [ForSpec(ForSpecData("x", Var("arr") from "test.jsonnet":26-29))]) from "test.jsonnet":0-30`600 */601 assert_eq!(602 parse!("[std.deepJoin(x) for x in arr]"),603 el!(604 ArrComp(605 el!(606 Apply(607 el!(608 Index(609 el!(Var("std".into()), 1, 4),610 el!(Str("deepJoin".into()), 5, 13)611 ),612 1,613 13614 ),615 ArgsDesc::new(vec![el!(Var("x".into()), 14, 15)], vec![]),616 false,617 ),618 1,619 16620 ),621 vec![CompSpec::ForSpec(ForSpecData(622 "x".into(),623 el!(Var("arr".into()), 26, 29)624 ))]625 ),626 0,627 30628 ),629 )630 }631632 #[test]633 fn reserved() {634 use Expr::*;635 assert_eq!(parse!("null"), el!(Literal(LiteralType::Null), 0, 4));636 assert_eq!(parse!("nulla"), el!(Var("nulla".into()), 0, 5));637 }638639 #[test]640 fn multiple_args_buf() {641 parse!("a(b, null_fields)");642 }643644 #[test]645 fn infix_precedence() {646 use Expr::*;647 assert_eq!(648 parse!("!a && !b"),649 el!(650 BinaryOp(651 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),652 And,653 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 7, 8)), 6, 8)654 ),655 0,656 8657 )658 );659 }660661 #[test]662 fn infix_precedence_division() {663 use Expr::*;664 assert_eq!(665 parse!("!a / !b"),666 el!(667 BinaryOp(668 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),669 Div,670 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 6, 7)), 5, 7)671 ),672 0,673 7674 )675 );676 }677678 #[test]679 fn double_negation() {680 use Expr::*;681 assert_eq!(682 parse!("!!a"),683 el!(684 UnaryOp(685 UnaryOpType::Not,686 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 2, 3)), 1, 3)687 ),688 0,689 3690 )691 )692 }693694 #[test]695 fn array_test_error() {696 parse!("[a for a in b if c for e in f]");697 // ^^^^ failed code698 }699700 #[test]701 fn missing_newline_between_comment_and_eof() {702 parse!(703 "{a:1}704705 //+213"706 );707 }708709 #[test]710 fn default_param_before_nondefault() {711 parse!("local x(foo = 'foo', bar) = null; null");712 }713714 #[test]715 fn can_parse_stdlib() {716 parse!(jrsonnet_stdlib::STDLIB_STR);717 }718719 #[test]720 fn add_location_info_to_all_sub_expressions() {721 use Expr::*;722723 let file_name: std::rc::Rc<std::path::Path> = PathBuf::from("test.jsonnet").into();724 let expr = parse(725 "{} { local x = 1, x: x } + {}",726 &ParserSettings {727 file_name: file_name.clone(),728 },729 )730 .unwrap();731 assert_eq!(732 expr,733 el!(734 BinaryOp(735 el!(736 ObjExtend(737 el!(Obj(ObjBody::MemberList(vec![])), 0, 2),738 ObjBody::MemberList(vec![739 Member::BindStmt(BindSpec::Field {740 into: Destruct::Full("x".into()),741 value: el!(Num(1.0), 15, 16)742 }),743 Member::Field(FieldMember {744 name: FieldName::Fixed("x".into()),745 plus: false,746 params: None,747 visibility: Visibility::Normal,748 value: el!(Var("x".into()), 21, 22),749 })750 ])751 ),752 0,753 24754 ),755 BinaryOpType::Add,756 el!(Obj(ObjBody::MemberList(vec![])), 27, 29),757 ),758 0,759 29760 ),761 );762 }763 // From source code764 /*765 #[bench]766 fn bench_parse_peg(b: &mut Bencher) {767 b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))768 }769 */770}