difftreelog
feat lazy evaluation of function default params
in: master
Fixes #59
7 files changed
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -73,6 +73,9 @@
.cloned()
.ok_or(VariableIsNotDefined(name))?)
}
+ pub fn contains_binding(&self, name: IStr) -> bool {
+ self.0.bindings.contains_key(&name)
+ }
pub fn into_future(self, ctx: FutureWrapper<Self>) -> Self {
{
ctx.0.borrow_mut().replace(self);
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -56,7 +56,7 @@
BindingParameterASecondTime(IStr),
#[error("too many args, function has {0}")]
TooManyArgsFunctionHas(usize),
- #[error("founction argument is not passed: {0}")]
+ #[error("function argument is not passed: {0}")]
FunctionParameterNotBoundInCall(IStr),
#[error("external variable is not defined: {0}")]
crates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -1,4 +1,7 @@
-use crate::{error::Error::*, evaluate, throw, Context, LazyVal, LazyValValue, Result, Val};
+use crate::{
+ error::Error::*, evaluate, evaluate_named, throw, Context, FutureWrapper, LazyVal,
+ LazyValValue, Result, Val,
+};
use jrsonnet_gc::Trace;
use jrsonnet_interner::IStr;
use jrsonnet_parser::{ArgsDesc, LocExpr, ParamsDesc};
@@ -8,6 +11,18 @@
const NO_DEFAULT_CONTEXT: &str =
"no default context set for call with defined default parameter value";
+#[derive(Trace)]
+#[trivially_drop]
+struct EvaluateLazyVal {
+ context: Context,
+ expr: LocExpr,
+}
+impl LazyValValue for EvaluateLazyVal {
+ fn get(self: Box<Self>) -> Result<Val> {
+ evaluate(self.context, &self.expr)
+ }
+}
+
/// Creates correct [context](Context) for function body evaluation returning error on invalid call.
///
/// ## Parameters
@@ -18,64 +33,119 @@
/// * `tailstrict`: if set to `true` function arguments are eagerly executed, otherwise - lazily
pub fn parse_function_call(
ctx: Context,
- body_ctx: Option<Context>,
+ body_ctx: Context,
params: &ParamsDesc,
args: &ArgsDesc,
tailstrict: bool,
) -> Result<Context> {
- let mut out = HashMap::with_capacity_and_hasher(params.len(), BuildHasherDefault::default());
- let mut positioned_args = vec![None; params.0.len()];
- for (id, arg) in args.iter().enumerate() {
- let idx = if let Some(name) = &arg.0 {
- params
- .iter()
- .position(|p| *p.0 == *name)
- .ok_or_else(|| UnknownFunctionParameter(name.clone()))?
- } else {
- id
- };
+ let mut passed_args =
+ HashMap::with_capacity_and_hasher(params.len(), BuildHasherDefault::default());
+ if args.unnamed.len() > params.len() {
+ throw!(TooManyArgsFunctionHas(params.len()))
+ }
+
+ let mut filled_args = 0;
- if idx >= params.len() {
- throw!(TooManyArgsFunctionHas(params.len()));
+ for (id, arg) in args.unnamed.iter().enumerate() {
+ let name = params[id].0.clone();
+ passed_args.insert(
+ name,
+ if tailstrict {
+ LazyVal::new_resolved(evaluate(ctx.clone(), arg)?)
+ } else {
+ LazyVal::new(Box::new(EvaluateLazyVal {
+ context: ctx.clone(),
+ expr: arg.clone(),
+ }))
+ },
+ );
+ filled_args += 1;
+ }
+
+ for (name, value) in args.named.iter() {
+ // FIXME: O(n) for arg existence check
+ if !params.iter().any(|p| &p.0 == name) {
+ throw!(UnknownFunctionParameter((name as &str).to_owned()));
}
- if positioned_args[idx].is_some() {
- throw!(BindingParameterASecondTime(params[idx].0.clone()));
+ if passed_args
+ .insert(
+ name.clone(),
+ if tailstrict {
+ LazyVal::new_resolved(evaluate(ctx.clone(), value)?)
+ } else {
+ LazyVal::new(Box::new(EvaluateLazyVal {
+ context: ctx.clone(),
+ expr: value.clone(),
+ }))
+ },
+ )
+ .is_some()
+ {
+ throw!(BindingParameterASecondTime(name.clone()));
}
- positioned_args[idx] = Some(arg.1.clone());
+ filled_args += 1;
}
- // Fill defaults
- for (id, p) in params.iter().enumerate() {
- let (ctx, expr) = if let Some(arg) = &positioned_args[id] {
- (ctx.clone(), arg)
- } else if let Some(default) = &p.1 {
- (body_ctx.clone().expect(NO_DEFAULT_CONTEXT), default)
- } else {
- throw!(FunctionParameterNotBoundInCall(p.0.clone()));
- };
- let val = if tailstrict {
- LazyVal::new_resolved(evaluate(ctx, expr)?)
- } else {
+
+ if filled_args < params.len() {
+ // Some args are unset, but maybe we have defaults for them
+ // Default values should be created in newly created context
+ let future_context = FutureWrapper::<Context>::new();
+ let mut defaults = HashMap::with_capacity_and_hasher(
+ params.len() - filled_args,
+ BuildHasherDefault::default(),
+ );
+
+ for param in params.iter().filter(|p| p.1.is_some()) {
+ if passed_args.contains_key(¶m.0.clone()) {
+ continue;
+ }
#[derive(Trace)]
#[trivially_drop]
- struct EvaluateLazyVal {
- context: Context,
- expr: LocExpr,
+ struct LazyNamedBinding {
+ future_context: FutureWrapper<Context>,
+ name: IStr,
+ value: LocExpr,
}
- impl LazyValValue for EvaluateLazyVal {
+ impl LazyValValue for LazyNamedBinding {
fn get(self: Box<Self>) -> Result<Val> {
- evaluate(self.context, &self.expr)
+ evaluate_named(self.future_context.unwrap(), &self.value, self.name)
}
}
+ LazyVal::new(Box::new(LazyNamedBinding {
+ future_context: future_context.clone(),
+ name: param.0.clone(),
+ value: param.1.clone().unwrap(),
+ }));
- LazyVal::new(Box::new(EvaluateLazyVal {
- context: ctx.clone(),
- expr: expr.clone(),
- }))
- };
- out.insert(p.0.clone(), val);
- }
+ defaults.insert(
+ param.0.clone(),
+ LazyVal::new(Box::new(LazyNamedBinding {
+ future_context: future_context.clone(),
+ name: param.0.clone(),
+ value: param.1.clone().unwrap(),
+ })),
+ );
+ filled_args += 1;
+ }
+
+ // Some args still wasn't filled
+ if filled_args != params.len() {
+ for param in params.iter().skip(args.unnamed.len()) {
+ if !args.named.iter().any(|a| a.0 == param.0) {
+ throw!(FunctionParameterNotBoundInCall(param.0.clone()));
+ }
+ }
+ unreachable!();
+ }
- Ok(body_ctx.unwrap_or(ctx).extend(out, None, None, None))
+ Ok(body_ctx
+ .extend(passed_args, None, None, None)
+ .extend_bound(defaults)
+ .into_future(future_context))
+ } else {
+ let body_ctx = body_ctx.extend(passed_args, None, None, None);
+ Ok(body_ctx)
+ }
}
pub fn parse_function_call_map(
@@ -176,21 +246,25 @@
use $crate::{error::Error::*, throw, evaluate, push_stack_frame, typed::CheckType};
let args = $args;
- if args.len() > $total_args {
+ if args.unnamed.len() + args.named.len() > $total_args {
throw!(TooManyArgsFunctionHas($total_args));
}
$(
- if args.len() <= $id {
+ if args.unnamed.len() + args.named.len() <= $id {
throw!(FunctionParameterNotBoundInCall(stringify!($name).into()));
}
- let $name = &args[$id];
- if $name.0.is_some() {
- if $name.0.as_ref().unwrap() != stringify!($name) {
+ // Is named
+ let $name = if $id >= $args.unnamed.len() {
+ let named = &args.named[$id - $args.unnamed.len()];
+ if &named.0 != stringify!($name) {
throw!(IntrinsicArgumentReorderingIsNotSupportedYet);
}
- }
+ &named.1
+ } else {
+ &$args.unnamed[$id]
+ };
let $name = push_stack_frame(None, || format!("evaluating argument"), || {
- let value = evaluate($ctx.clone(), &$name.1)?;
+ let value = evaluate($ctx.clone(), &$name)?;
$ty.check(&value)?;
Ok(value)
})?;
crates/jrsonnet-evaluator/src/map.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/map.rs
+++ b/crates/jrsonnet-evaluator/src/map.rs
@@ -29,6 +29,16 @@
.get(key)
.or_else(|| self.0.parent.as_ref().and_then(|p| p.get(key)))
}
+
+ pub fn contains_key(&self, key: &IStr) -> bool {
+ (self.0).current.contains_key(key)
+ || self
+ .0
+ .parent
+ .as_ref()
+ .map(|p| p.contains_key(key))
+ .unwrap_or(false)
+ }
}
impl Clone for LayeredHashMap {
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -11,7 +11,7 @@
};
use jrsonnet_gc::{Gc, GcCell, Trace};
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};
+use jrsonnet_parser::{el, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};
use jrsonnet_types::ValType;
use std::{collections::HashMap, fmt::Debug, rc::Rc};
@@ -127,7 +127,7 @@
Self::Normal(func) => {
let ctx = parse_function_call(
call_ctx,
- Some(func.ctx.clone()),
+ func.ctx.clone(),
&func.params,
args,
tailstrict,
@@ -136,7 +136,8 @@
}
Self::Intrinsic(name) => call_builtin(call_ctx, loc, name, args),
Self::NativeExt(_name, handler) => {
- let args = parse_function_call(call_ctx, None, &handler.params, args, true)?;
+ let args =
+ parse_function_call(call_ctx, Context::new(), &handler.params, args, true)?;
let mut out_args = Vec::with_capacity(handler.params.len());
for p in handler.params.0.iter() {
out_args.push(args.binding(p.0.clone())?.evaluate()?);
@@ -554,17 +555,17 @@
el!(Expr::Var("std".into())),
el!(Expr::Str("manifestYamlDoc".into()))
)),
- ArgsDesc(vec![
- Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),
- Arg(
- None,
+ ArgsDesc::new(
+ vec![
+ el!(Expr::Var("__tmp__to_json__".into())),
el!(Expr::Literal(if padding != 0 {
LiteralType::True
} else {
LiteralType::False
- }))
- )
- ]),
+ })),
+ ],
+ vec![]
+ ),
false
)),
)?
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth1use jrsonnet_gc::{unsafe_empty_trace, Finalize, Trace};2use jrsonnet_interner::IStr;3#[cfg(feature = "deserialize")]4use serde::Deserialize;5#[cfg(feature = "serialize")]6use serde::Serialize;7use std::{8 fmt::{Debug, Display},9 ops::Deref,10 path::{Path, PathBuf},11 rc::Rc,12};1314#[cfg_attr(feature = "serialize", derive(Serialize))]15#[cfg_attr(feature = "deserialize", derive(Deserialize))]16#[derive(Debug, PartialEq, Trace)]17#[trivially_drop]18pub enum FieldName {19 /// {fixed: 2}20 Fixed(IStr),21 /// {["dyn"+"amic"]: 3}22 Dyn(LocExpr),23}2425#[cfg_attr(feature = "serialize", derive(Serialize))]26#[cfg_attr(feature = "deserialize", derive(Deserialize))]27#[derive(Debug, Clone, Copy, PartialEq, Trace)]28#[trivially_drop]29pub enum Visibility {30 /// :31 Normal,32 /// ::33 Hidden,34 /// :::35 Unhide,36}3738impl Visibility {39 pub fn is_visible(&self) -> bool {40 matches!(self, Self::Normal | Self::Unhide)41 }42}4344#[cfg_attr(feature = "serialize", derive(Serialize))]45#[cfg_attr(feature = "deserialize", derive(Deserialize))]46#[derive(Clone, Debug, PartialEq, Trace)]47#[trivially_drop]48pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);4950#[cfg_attr(feature = "serialize", derive(Serialize))]51#[cfg_attr(feature = "deserialize", derive(Deserialize))]52#[derive(Debug, PartialEq, Trace)]53#[trivially_drop]54pub struct FieldMember {55 pub name: FieldName,56 pub plus: bool,57 pub params: Option<ParamsDesc>,58 pub visibility: Visibility,59 pub value: LocExpr,60}6162#[cfg_attr(feature = "serialize", derive(Serialize))]63#[cfg_attr(feature = "deserialize", derive(Deserialize))]64#[derive(Debug, PartialEq, Trace)]65#[trivially_drop]66pub enum Member {67 Field(FieldMember),68 BindStmt(BindSpec),69 AssertStmt(AssertStmt),70}7172#[cfg_attr(feature = "serialize", derive(Serialize))]73#[cfg_attr(feature = "deserialize", derive(Deserialize))]74#[derive(Debug, Clone, Copy, PartialEq, Trace)]75#[trivially_drop]76pub enum UnaryOpType {77 Plus,78 Minus,79 BitNot,80 Not,81}8283impl Display for UnaryOpType {84 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {85 use UnaryOpType::*;86 write!(87 f,88 "{}",89 match self {90 Plus => "+",91 Minus => "-",92 BitNot => "~",93 Not => "!",94 }95 )96 }97}9899#[cfg_attr(feature = "serialize", derive(Serialize))]100#[cfg_attr(feature = "deserialize", derive(Deserialize))]101#[derive(Debug, Clone, Copy, PartialEq, Trace)]102#[trivially_drop]103pub enum BinaryOpType {104 Mul,105 Div,106107 /// Implemented as intrinsic, put here for completeness108 Mod,109110 Add,111 Sub,112113 Lhs,114 Rhs,115116 Lt,117 Gt,118 Lte,119 Gte,120121 BitAnd,122 BitOr,123 BitXor,124125 Eq,126 Neq,127128 And,129 Or,130131 // Equialent to std.objectHasEx(a, b, true)132 In,133}134135impl Display for BinaryOpType {136 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {137 use BinaryOpType::*;138 write!(139 f,140 "{}",141 match self {142 Mul => "*",143 Div => "/",144 Mod => "%",145 Add => "+",146 Sub => "-",147 Lhs => "<<",148 Rhs => ">>",149 Lt => "<",150 Gt => ">",151 Lte => "<=",152 Gte => ">=",153 BitAnd => "&",154 BitOr => "|",155 BitXor => "^",156 Eq => "==",157 Neq => "!=",158 And => "&&",159 Or => "||",160 In => "in",161 }162 )163 }164}165166/// name, default value167#[cfg_attr(feature = "serialize", derive(Serialize))]168#[cfg_attr(feature = "deserialize", derive(Deserialize))]169#[derive(Debug, PartialEq, Trace)]170#[trivially_drop]171pub struct Param(pub IStr, pub Option<LocExpr>);172173/// Defined function parameters174#[cfg_attr(feature = "serialize", derive(Serialize))]175#[cfg_attr(feature = "deserialize", derive(Deserialize))]176#[derive(Debug, Clone, PartialEq)]177pub struct ParamsDesc(pub Rc<Vec<Param>>);178179/// Safety:180/// AST is acyclic, and there should be no gc pointers181unsafe impl Trace for ParamsDesc {182 unsafe_empty_trace!();183}184impl Finalize for ParamsDesc {}185186impl Deref for ParamsDesc {187 type Target = Vec<Param>;188 fn deref(&self) -> &Self::Target {189 &self.0190 }191}192193#[cfg_attr(feature = "serialize", derive(Serialize))]194#[cfg_attr(feature = "deserialize", derive(Deserialize))]195#[derive(Debug, PartialEq, Trace)]196#[trivially_drop]197pub struct Arg(pub Option<String>, pub LocExpr);198199#[cfg_attr(feature = "serialize", derive(Serialize))]200#[cfg_attr(feature = "deserialize", derive(Deserialize))]201#[derive(Debug, PartialEq, Trace)]202#[trivially_drop]203pub struct ArgsDesc(pub Vec<Arg>);204205impl Deref for ArgsDesc {206 type Target = Vec<Arg>;207 fn deref(&self) -> &Self::Target {208 &self.0209 }210}211212#[cfg_attr(feature = "serialize", derive(Serialize))]213#[cfg_attr(feature = "deserialize", derive(Deserialize))]214#[derive(Debug, Clone, PartialEq, Trace)]215#[trivially_drop]216pub struct BindSpec {217 pub name: IStr,218 pub params: Option<ParamsDesc>,219 pub value: LocExpr,220}221222#[cfg_attr(feature = "serialize", derive(Serialize))]223#[cfg_attr(feature = "deserialize", derive(Deserialize))]224#[derive(Debug, PartialEq, Trace)]225#[trivially_drop]226pub struct IfSpecData(pub LocExpr);227228#[cfg_attr(feature = "serialize", derive(Serialize))]229#[cfg_attr(feature = "deserialize", derive(Deserialize))]230#[derive(Debug, PartialEq, Trace)]231#[trivially_drop]232pub struct ForSpecData(pub IStr, pub LocExpr);233234#[cfg_attr(feature = "serialize", derive(Serialize))]235#[cfg_attr(feature = "deserialize", derive(Deserialize))]236#[derive(Debug, PartialEq, Trace)]237#[trivially_drop]238pub enum CompSpec {239 IfSpec(IfSpecData),240 ForSpec(ForSpecData),241}242243#[cfg_attr(feature = "serialize", derive(Serialize))]244#[cfg_attr(feature = "deserialize", derive(Deserialize))]245#[derive(Debug, PartialEq, Trace)]246#[trivially_drop]247pub struct ObjComp {248 pub pre_locals: Vec<BindSpec>,249 pub key: LocExpr,250 pub plus: bool,251 pub value: LocExpr,252 pub post_locals: Vec<BindSpec>,253 pub compspecs: Vec<CompSpec>,254}255256#[cfg_attr(feature = "serialize", derive(Serialize))]257#[cfg_attr(feature = "deserialize", derive(Deserialize))]258#[derive(Debug, PartialEq, Trace)]259#[trivially_drop]260pub enum ObjBody {261 MemberList(Vec<Member>),262 ObjComp(ObjComp),263}264265#[cfg_attr(feature = "serialize", derive(Serialize))]266#[cfg_attr(feature = "deserialize", derive(Deserialize))]267#[derive(Debug, PartialEq, Clone, Copy, Trace)]268#[trivially_drop]269pub enum LiteralType {270 This,271 Super,272 Dollar,273 Null,274 True,275 False,276}277278#[cfg_attr(feature = "serialize", derive(Serialize))]279#[cfg_attr(feature = "deserialize", derive(Deserialize))]280#[derive(Debug, PartialEq, Trace)]281#[trivially_drop]282pub struct SliceDesc {283 pub start: Option<LocExpr>,284 pub end: Option<LocExpr>,285 pub step: Option<LocExpr>,286}287288/// Syntax base289#[cfg_attr(feature = "serialize", derive(Serialize))]290#[cfg_attr(feature = "deserialize", derive(Deserialize))]291#[derive(Debug, PartialEq, Trace)]292#[trivially_drop]293pub enum Expr {294 Literal(LiteralType),295296 /// String value: "hello"297 Str(IStr),298 /// Number: 1, 2.0, 2e+20299 Num(f64),300 /// Variable name: test301 Var(IStr),302303 /// Array of expressions: [1, 2, "Hello"]304 Arr(Vec<LocExpr>),305 /// Array comprehension:306 /// ```jsonnet307 /// ingredients: [308 /// { kind: kind, qty: 4 / 3 }309 /// for kind in [310 /// 'Honey Syrup',311 /// 'Lemon Juice',312 /// 'Farmers Gin',313 /// ]314 /// ],315 /// ```316 ArrComp(LocExpr, Vec<CompSpec>),317318 /// Object: {a: 2}319 Obj(ObjBody),320 /// Object extension: var1 {b: 2}321 ObjExtend(LocExpr, ObjBody),322323 /// (obj)324 Parened(LocExpr),325326 /// -2327 UnaryOp(UnaryOpType, LocExpr),328 /// 2 - 2329 BinaryOp(LocExpr, BinaryOpType, LocExpr),330 /// assert 2 == 2 : "Math is broken"331 AssertExpr(AssertStmt, LocExpr),332 /// local a = 2; { b: a }333 LocalExpr(Vec<BindSpec>, LocExpr),334335 /// import "hello"336 Import(PathBuf),337 /// importStr "file.txt"338 ImportStr(PathBuf),339 /// error "I'm broken"340 ErrorStmt(LocExpr),341 /// a(b, c)342 Apply(LocExpr, ArgsDesc, bool),343 /// a[b]344 Index(LocExpr, LocExpr),345 /// function(x) x346 Function(ParamsDesc, LocExpr),347 /// std.primitiveEquals348 Intrinsic(IStr),349 /// if true == false then 1 else 2350 IfElse {351 cond: IfSpecData,352 cond_then: LocExpr,353 cond_else: Option<LocExpr>,354 },355 Slice(LocExpr, SliceDesc),356}357358/// file, begin offset, end offset359#[cfg_attr(feature = "serialize", derive(Serialize))]360#[cfg_attr(feature = "deserialize", derive(Deserialize))]361#[derive(Clone, PartialEq, Trace)]362#[trivially_drop]363pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);364365impl Debug for ExprLocation {366 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {367 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)368 }369}370371/// Holds AST expression and its location in source file372#[cfg_attr(feature = "serialize", derive(Serialize))]373#[cfg_attr(feature = "deserialize", derive(Deserialize))]374#[derive(Clone, PartialEq)]375pub struct LocExpr(pub Rc<Expr>, pub Option<ExprLocation>);376/// Safety:377/// AST is acyclic, and there should be no gc pointers378unsafe impl Trace for LocExpr {379 unsafe_empty_trace!();380}381impl Finalize for LocExpr {}382383impl Debug for LocExpr {384 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {385 if f.alternate() {386 write!(f, "{:#?}", self.0)?;387 } else {388 write!(f, "{:?}", self.0)?;389 }390 if let Some(loc) = &self.1 {391 write!(f, " from {:?}", loc)?;392 }393 Ok(())394 }395}396397/// Creates LocExpr from Expr and ExprLocation components398#[macro_export]399macro_rules! loc_expr {400 ($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {401 LocExpr(402 std::rc::Rc::new($expr),403 if $need_loc {404 Some(ExprLocation($name, $start, $end))405 } else {406 None407 },408 )409 };410}411412/// Creates LocExpr without location info413#[macro_export]414macro_rules! loc_expr_todo {415 ($expr:expr) => {416 LocExpr(Rc::new($expr), None)417 };418}1use jrsonnet_gc::{unsafe_empty_trace, Finalize, Trace};2use jrsonnet_interner::IStr;3#[cfg(feature = "deserialize")]4use serde::Deserialize;5#[cfg(feature = "serialize")]6use serde::Serialize;7use std::{8 fmt::{Debug, Display},9 ops::Deref,10 path::{Path, PathBuf},11 rc::Rc,12};1314#[cfg_attr(feature = "serialize", derive(Serialize))]15#[cfg_attr(feature = "deserialize", derive(Deserialize))]16#[derive(Debug, PartialEq, Trace)]17#[trivially_drop]18pub enum FieldName {19 /// {fixed: 2}20 Fixed(IStr),21 /// {["dyn"+"amic"]: 3}22 Dyn(LocExpr),23}2425#[cfg_attr(feature = "serialize", derive(Serialize))]26#[cfg_attr(feature = "deserialize", derive(Deserialize))]27#[derive(Debug, Clone, Copy, PartialEq, Trace)]28#[trivially_drop]29pub enum Visibility {30 /// :31 Normal,32 /// ::33 Hidden,34 /// :::35 Unhide,36}3738impl Visibility {39 pub fn is_visible(&self) -> bool {40 matches!(self, Self::Normal | Self::Unhide)41 }42}4344#[cfg_attr(feature = "serialize", derive(Serialize))]45#[cfg_attr(feature = "deserialize", derive(Deserialize))]46#[derive(Clone, Debug, PartialEq, Trace)]47#[trivially_drop]48pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);4950#[cfg_attr(feature = "serialize", derive(Serialize))]51#[cfg_attr(feature = "deserialize", derive(Deserialize))]52#[derive(Debug, PartialEq, Trace)]53#[trivially_drop]54pub struct FieldMember {55 pub name: FieldName,56 pub plus: bool,57 pub params: Option<ParamsDesc>,58 pub visibility: Visibility,59 pub value: LocExpr,60}6162#[cfg_attr(feature = "serialize", derive(Serialize))]63#[cfg_attr(feature = "deserialize", derive(Deserialize))]64#[derive(Debug, PartialEq, Trace)]65#[trivially_drop]66pub enum Member {67 Field(FieldMember),68 BindStmt(BindSpec),69 AssertStmt(AssertStmt),70}7172#[cfg_attr(feature = "serialize", derive(Serialize))]73#[cfg_attr(feature = "deserialize", derive(Deserialize))]74#[derive(Debug, Clone, Copy, PartialEq, Trace)]75#[trivially_drop]76pub enum UnaryOpType {77 Plus,78 Minus,79 BitNot,80 Not,81}8283impl Display for UnaryOpType {84 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {85 use UnaryOpType::*;86 write!(87 f,88 "{}",89 match self {90 Plus => "+",91 Minus => "-",92 BitNot => "~",93 Not => "!",94 }95 )96 }97}9899#[cfg_attr(feature = "serialize", derive(Serialize))]100#[cfg_attr(feature = "deserialize", derive(Deserialize))]101#[derive(Debug, Clone, Copy, PartialEq, Trace)]102#[trivially_drop]103pub enum BinaryOpType {104 Mul,105 Div,106107 /// Implemented as intrinsic, put here for completeness108 Mod,109110 Add,111 Sub,112113 Lhs,114 Rhs,115116 Lt,117 Gt,118 Lte,119 Gte,120121 BitAnd,122 BitOr,123 BitXor,124125 Eq,126 Neq,127128 And,129 Or,130131 // Equialent to std.objectHasEx(a, b, true)132 In,133}134135impl Display for BinaryOpType {136 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {137 use BinaryOpType::*;138 write!(139 f,140 "{}",141 match self {142 Mul => "*",143 Div => "/",144 Mod => "%",145 Add => "+",146 Sub => "-",147 Lhs => "<<",148 Rhs => ">>",149 Lt => "<",150 Gt => ">",151 Lte => "<=",152 Gte => ">=",153 BitAnd => "&",154 BitOr => "|",155 BitXor => "^",156 Eq => "==",157 Neq => "!=",158 And => "&&",159 Or => "||",160 In => "in",161 }162 )163 }164}165166/// name, default value167#[cfg_attr(feature = "serialize", derive(Serialize))]168#[cfg_attr(feature = "deserialize", derive(Deserialize))]169#[derive(Debug, PartialEq, Trace)]170#[trivially_drop]171pub struct Param(pub IStr, pub Option<LocExpr>);172173/// Defined function parameters174#[cfg_attr(feature = "serialize", derive(Serialize))]175#[cfg_attr(feature = "deserialize", derive(Deserialize))]176#[derive(Debug, Clone, PartialEq)]177pub struct ParamsDesc(pub Rc<Vec<Param>>);178179/// Safety:180/// AST is acyclic, and there should be no gc pointers181unsafe impl Trace for ParamsDesc {182 unsafe_empty_trace!();183}184impl Finalize for ParamsDesc {}185186impl Deref for ParamsDesc {187 type Target = Vec<Param>;188 fn deref(&self) -> &Self::Target {189 &self.0190 }191}192193#[cfg_attr(feature = "serialize", derive(Serialize))]194#[cfg_attr(feature = "deserialize", derive(Deserialize))]195#[derive(Debug, PartialEq, Trace)]196#[trivially_drop]197pub struct ArgsDesc {198 pub unnamed: Vec<LocExpr>,199 pub named: Vec<(IStr, LocExpr)>,200}201impl ArgsDesc {202 pub fn new(unnamed: Vec<LocExpr>, named: Vec<(IStr, LocExpr)>) -> Self {203 Self { unnamed, named }204 }205}206207#[cfg_attr(feature = "serialize", derive(Serialize))]208#[cfg_attr(feature = "deserialize", derive(Deserialize))]209#[derive(Debug, Clone, PartialEq, Trace)]210#[trivially_drop]211pub struct BindSpec {212 pub name: IStr,213 pub params: Option<ParamsDesc>,214 pub value: LocExpr,215}216217#[cfg_attr(feature = "serialize", derive(Serialize))]218#[cfg_attr(feature = "deserialize", derive(Deserialize))]219#[derive(Debug, PartialEq, Trace)]220#[trivially_drop]221pub struct IfSpecData(pub LocExpr);222223#[cfg_attr(feature = "serialize", derive(Serialize))]224#[cfg_attr(feature = "deserialize", derive(Deserialize))]225#[derive(Debug, PartialEq, Trace)]226#[trivially_drop]227pub struct ForSpecData(pub IStr, pub LocExpr);228229#[cfg_attr(feature = "serialize", derive(Serialize))]230#[cfg_attr(feature = "deserialize", derive(Deserialize))]231#[derive(Debug, PartialEq, Trace)]232#[trivially_drop]233pub enum CompSpec {234 IfSpec(IfSpecData),235 ForSpec(ForSpecData),236}237238#[cfg_attr(feature = "serialize", derive(Serialize))]239#[cfg_attr(feature = "deserialize", derive(Deserialize))]240#[derive(Debug, PartialEq, Trace)]241#[trivially_drop]242pub struct ObjComp {243 pub pre_locals: Vec<BindSpec>,244 pub key: LocExpr,245 pub plus: bool,246 pub value: LocExpr,247 pub post_locals: Vec<BindSpec>,248 pub compspecs: Vec<CompSpec>,249}250251#[cfg_attr(feature = "serialize", derive(Serialize))]252#[cfg_attr(feature = "deserialize", derive(Deserialize))]253#[derive(Debug, PartialEq, Trace)]254#[trivially_drop]255pub enum ObjBody {256 MemberList(Vec<Member>),257 ObjComp(ObjComp),258}259260#[cfg_attr(feature = "serialize", derive(Serialize))]261#[cfg_attr(feature = "deserialize", derive(Deserialize))]262#[derive(Debug, PartialEq, Clone, Copy, Trace)]263#[trivially_drop]264pub enum LiteralType {265 This,266 Super,267 Dollar,268 Null,269 True,270 False,271}272273#[cfg_attr(feature = "serialize", derive(Serialize))]274#[cfg_attr(feature = "deserialize", derive(Deserialize))]275#[derive(Debug, PartialEq, Trace)]276#[trivially_drop]277pub struct SliceDesc {278 pub start: Option<LocExpr>,279 pub end: Option<LocExpr>,280 pub step: Option<LocExpr>,281}282283/// Syntax base284#[cfg_attr(feature = "serialize", derive(Serialize))]285#[cfg_attr(feature = "deserialize", derive(Deserialize))]286#[derive(Debug, PartialEq, Trace)]287#[trivially_drop]288pub enum Expr {289 Literal(LiteralType),290291 /// String value: "hello"292 Str(IStr),293 /// Number: 1, 2.0, 2e+20294 Num(f64),295 /// Variable name: test296 Var(IStr),297298 /// Array of expressions: [1, 2, "Hello"]299 Arr(Vec<LocExpr>),300 /// Array comprehension:301 /// ```jsonnet302 /// ingredients: [303 /// { kind: kind, qty: 4 / 3 }304 /// for kind in [305 /// 'Honey Syrup',306 /// 'Lemon Juice',307 /// 'Farmers Gin',308 /// ]309 /// ],310 /// ```311 ArrComp(LocExpr, Vec<CompSpec>),312313 /// Object: {a: 2}314 Obj(ObjBody),315 /// Object extension: var1 {b: 2}316 ObjExtend(LocExpr, ObjBody),317318 /// (obj)319 Parened(LocExpr),320321 /// -2322 UnaryOp(UnaryOpType, LocExpr),323 /// 2 - 2324 BinaryOp(LocExpr, BinaryOpType, LocExpr),325 /// assert 2 == 2 : "Math is broken"326 AssertExpr(AssertStmt, LocExpr),327 /// local a = 2; { b: a }328 LocalExpr(Vec<BindSpec>, LocExpr),329330 /// import "hello"331 Import(PathBuf),332 /// importStr "file.txt"333 ImportStr(PathBuf),334 /// error "I'm broken"335 ErrorStmt(LocExpr),336 /// a(b, c)337 Apply(LocExpr, ArgsDesc, bool),338 /// a[b]339 Index(LocExpr, LocExpr),340 /// function(x) x341 Function(ParamsDesc, LocExpr),342 /// std.primitiveEquals343 Intrinsic(IStr),344 /// if true == false then 1 else 2345 IfElse {346 cond: IfSpecData,347 cond_then: LocExpr,348 cond_else: Option<LocExpr>,349 },350 Slice(LocExpr, SliceDesc),351}352353/// file, begin offset, end offset354#[cfg_attr(feature = "serialize", derive(Serialize))]355#[cfg_attr(feature = "deserialize", derive(Deserialize))]356#[derive(Clone, PartialEq, Trace)]357#[trivially_drop]358pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);359360impl Debug for ExprLocation {361 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {362 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)363 }364}365366/// Holds AST expression and its location in source file367#[cfg_attr(feature = "serialize", derive(Serialize))]368#[cfg_attr(feature = "deserialize", derive(Deserialize))]369#[derive(Clone, PartialEq)]370pub struct LocExpr(pub Rc<Expr>, pub Option<ExprLocation>);371/// Safety:372/// AST is acyclic, and there should be no gc pointers373unsafe impl Trace for LocExpr {374 unsafe_empty_trace!();375}376impl Finalize for LocExpr {}377378impl Debug for LocExpr {379 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {380 if f.alternate() {381 write!(f, "{:#?}", self.0)?;382 } else {383 write!(f, "{:?}", self.0)?;384 }385 if let Some(loc) = &self.1 {386 write!(f, " from {:?}", loc)?;387 }388 Ok(())389 }390}391392/// Creates LocExpr from Expr and ExprLocation components393#[macro_export]394macro_rules! loc_expr {395 ($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {396 LocExpr(397 std::rc::Rc::new($expr),398 if $need_loc {399 Some(ExprLocation($name, $start, $end))400 } else {401 None402 },403 )404 };405}406407/// Creates LocExpr without location info408#[macro_export]409macro_rules! loc_expr_todo {410 ($expr:expr) => {411 LocExpr(Rc::new($expr), None)412 };413}crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -7,6 +7,7 @@
};
mod expr;
pub use expr::*;
+pub use jrsonnet_interner::IStr;
pub use peg;
pub struct ParserSettings {
@@ -70,19 +71,29 @@
}
/ { expr::ParamsDesc(Rc::new(Vec::new())) }
- pub rule arg(s: &ParserSettings) -> expr::Arg
- = name:$(id()) _ "=" _ expr:expr(s) {expr::Arg(Some(name.into()), expr)}
- / expr:expr(s) {expr::Arg(None, expr)}
+ pub rule arg(s: &ParserSettings) -> (Option<IStr>, LocExpr)
+ = quiet! { name:(s:$(id()) _ "=" _ {s})? expr:expr(s) {(name.map(Into::into), expr)} }
+ / expected!("<argument>")
+
pub rule args(s: &ParserSettings) -> expr::ArgsDesc
- = args:arg(s) ** comma() comma()? {
+ = args:arg(s)**comma() comma()? {?
+ let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();
+ let mut unnamed = Vec::with_capacity(unnamed_count);
+ let mut named = Vec::with_capacity(args.len() - unnamed_count);
let mut named_started = false;
- for arg in &args {
- named_started = named_started || arg.0.is_some();
- assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");
+ for (name, value) in args {
+ if let Some(name) = name {
+ named_started = true;
+ named.push((name, value));
+ } else {
+ if named_started {
+ return Err("<named argument>")
+ }
+ unnamed.push(value);
+ }
}
- expr::ArgsDesc(args)
+ Ok(expr::ArgsDesc::new(unnamed, named))
}
- / { expr::ArgsDesc(Vec::new()) }
pub rule bind(s: &ParserSettings) -> expr::BindSpec
= name:$(id()) _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: None, value: expr}}
@@ -493,7 +504,7 @@
el!(ArrComp(
el!(Apply(
el!(Index(el!(Var("std".into())), el!(Str("deepJoin".into())))),
- ArgsDesc(vec![Arg(None, el!(Var("x".into())))]),
+ ArgsDesc::new(vec![el!(Var("x".into()))], vec![]),
false,
)),
vec![CompSpec::ForSpec(ForSpecData(