git.delta.rocks / jrsonnet / refs/commits / c6d0d24a32d3

difftreelog

refactor only keep used spans in IR

uxonrmwzYaroslav Bolyukin2026-03-22parent: #44f6e2c.patch.diff
in: master

12 files changed

modifiedCargo.lockdiffbeforeafterboth
730version = "0.5.0-pre97"730version = "0.5.0-pre97"
731dependencies = [731dependencies = [
732 "insta",732 "insta",
733 "jrsonnet-gcmodule",
733 "jrsonnet-ir",734 "jrsonnet-ir",
734 "peg",735 "peg",
735]736]
modifiedcrates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth
38 Self::new(RangeArray::empty())38 Self::new(RangeArray::empty())
39 }39 }
4040
41 pub fn expr(ctx: Context, exprs: Rc<Vec<Spanned<Expr>>>) -> Self {41 pub fn expr(ctx: Context, exprs: Rc<Vec<Expr>>) -> Self {
42 Self::new(ExprArray::new(ctx, exprs))42 Self::new(ExprArray::new(ctx, exprs))
43 }43 }
4444
modifiedcrates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth
118#[derive(Debug, Trace, Clone)]118#[derive(Debug, Trace, Clone)]
119pub struct ExprArray {119pub struct ExprArray {
120 ctx: Context,120 ctx: Context,
121 src: Rc<Vec<Spanned<Expr>>>,121 src: Rc<Vec<Expr>>,
122 cached: Cc<RefCell<Vec<ArrayThunk>>>,122 cached: Cc<RefCell<Vec<ArrayThunk>>>,
123}123}
124impl ExprArray {124impl ExprArray {
125 pub fn new(ctx: Context, src: Rc<Vec<Spanned<Expr>>>) -> Self {125 pub fn new(ctx: Context, src: Rc<Vec<Expr>>) -> Self {
126 Self {126 Self {
127 ctx,127 ctx,
128 cached: Cc::new(RefCell::new(vec![ArrayThunk::Waiting; src.len()])),128 cached: Cc::new(RefCell::new(vec![ArrayThunk::Waiting; src.len()])),
modifiedcrates/jrsonnet-evaluator/src/async_import.rsdiffbeforeafterboth
139 if let Expr::Str(s) = &***v {139 if let Expr::Str(s) = &***v {
140 out.0.push(Import {140 out.0.push(Import {
141 path: ResolvePathOwned::Str(s.to_string()),141 path: ResolvePathOwned::Str(s.to_string()),
142 expression: matches!(&**expr, Expr::Import(ImportKind::Normal, _)),142 expression: todo!(),
143 });143 });
144 }144 }
145 // Non-string import will fail in runtime145 // Non-string import will fail in runtime
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
53 Expr::Str(_)53 Expr::Str(_)
54 | Expr::Num(_)54 | Expr::Num(_)
55 | Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,55 | Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,
56 Expr::Arr(a) => a.iter().all(|e| is_trivial(&**e)),56 Expr::Arr(a) => a.iter().all(|e| is_trivial(&*e)),
57 _ => false,57 _ => false,
58 }58 }
59 }59 }
71 }71 }
72 Val::Arr(ArrValue::eager(72 Val::Arr(ArrValue::eager(
73 n.iter()73 n.iter()
74 .map(|e| evaluate_trivial(&**e))74 .map(|e| evaluate_trivial(&*e))
75 .map(|e| e.expect("checked trivial"))75 .map(|e| e.expect("checked trivial"))
76 .collect(),76 .collect(),
77 ))77 ))
84 ctx: Context,
85 name: IStr,
86 params: ExprParams,
87 body: Rc<Spanned<Expr>>,
88) -> Val {
89 Val::Func(FuncVal::Normal(Cc::new(FuncDesc {84 Val::Func(FuncVal::Normal(Cc::new(FuncDesc {
90 name,85 name,
97pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {92pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {
98 Ok(match field_name {93 Ok(match field_name {
99 FieldName::Fixed(n) => Some(n.clone()),94 FieldName::Fixed(n) => Some(n.clone()),
100 FieldName::Dyn(expr) => in_frame(95 FieldName::Dyn(expr) => {
101 CallLocation::new(&expr.span()),96 // FIXME: Span
102 || "evaluating field name".to_string(),
103 || {
104 let value = evaluate(ctx, expr)?;97 let value = evaluate(ctx, expr)?;
105 if matches!(value, Val::Null) {98 if matches!(value, Val::Null) {
106 Ok(None)99 None
107 } else {100 } else {
108 Ok(Some(IStr::from_untyped(value)?))101 Some(IStr::from_untyped(value)?)
109 }102 }
110 },103 } //
111 )?,104 // in_frame(
105 // CallLocation::new(&expr.span()),
106 // || "evaluating field name".to_string(),
107 // || {
108 // },
109 // )?,
112 })110 })
113}111}
114112
119) -> Result<()> {117) -> Result<()> {
120 match specs.first() {118 match specs.first() {
121 None => callback(ctx)?,119 None => callback(ctx)?,
122 Some(CompSpec::IfSpec(IfSpecData(cond))) => {120 Some(CompSpec::IfSpec(Spanned(IfSpecData(cond), _))) => {
123 if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {121 if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {
124 evaluate_comp(ctx, &specs[1..], callback)?;122 evaluate_comp(ctx, &specs[1..], callback)?;
125 }123 }
126 }124 }
127 Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(ctx.clone(), expr)? {125 Some(CompSpec::ForSpec(Spanned(ForSpecData(var, expr), _))) => {
126 match evaluate(ctx.clone(), expr)? {
128 Val::Arr(list) => {127 Val::Arr(list) => {
129 for item in list.iter_lazy() {128 for item in list.iter_lazy() {
158 }157 }
159 }158 }
160 _ => bail!(InComprehensionCanOnlyIterateOverArray),159 _ => bail!(InComprehensionCanOnlyIterateOverArray),
161 },160 }
161 }
162 }162 }
163 Ok(())163 Ok(())
164}164}
221 #[derive(Trace)]221 #[derive(Trace)]
222 struct UnboundValue<B: Trace> {222 struct UnboundValue<B: Trace> {
223 uctx: B,223 uctx: B,
224 value: Rc<Spanned<Expr>>,224 value: Rc<Expr>,
225 name: IStr,225 name: IStr,
226 }226 }
227 impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {227 impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {
235 .field(name.clone())235 .field(name.clone())
236 .with_add(*plus)236 .with_add(*plus)
237 .with_visibility(*visibility)237 .with_visibility(*visibility)
238 .with_location(value.span())238 // FIXME
239 // .with_location(value.span())
239 .bindable(UnboundValue {240 .bindable(UnboundValue {
240 uctx,241 uctx,
241 value: value.clone(),242 value: value.clone(),
251 #[derive(Trace)]252 #[derive(Trace)]
252 struct UnboundMethod<B: Trace> {253 struct UnboundMethod<B: Trace> {
253 uctx: B,254 uctx: B,
254 value: Rc<Spanned<Expr>>,255 value: Rc<Expr>,
255 params: ExprParams,256 params: ExprParams,
256 name: IStr,257 name: IStr,
257 }258 }
270 builder271 builder
271 .field(name.clone())272 .field(name.clone())
272 .with_visibility(*visibility)273 .with_visibility(*visibility)
273 .with_location(value.span())274 // .with_location(value.span())
274 .bindable(UnboundMethod {275 .bindable(UnboundMethod {
275 uctx,276 uctx,
276 value: value.clone(),277 value: value.clone(),
337338
338pub fn evaluate_apply(339pub fn evaluate_apply(
339 ctx: Context,340 ctx: Context,
340 value: &Spanned<Expr>,341 value: &Expr,
341 args: &ArgsDesc,342 args: &ArgsDesc,
342 loc: CallLocation<'_>,343 loc: CallLocation<'_>,
343 tailstrict: bool,344 tailstrict: bool,
379 Ok(())380 Ok(())
380}381}
381382
382pub fn evaluate_named_param(ctx: Context, expr: &Spanned<Expr>, name: ParamName) -> Result<Val> {383pub fn evaluate_named_param(ctx: Context, expr: &Expr, name: ParamName) -> Result<Val> {
383 match name {384 match name {
384 ParamName::Named(name) => evaluate_named(ctx, expr, name),385 ParamName::Named(name) => evaluate_named(ctx, expr, name),
385 ParamName::Unnamed => evaluate(ctx, expr),386 ParamName::Unnamed => evaluate(ctx, expr),
386 }387 }
387}388}
388389
389pub fn evaluate_named(ctx: Context, expr: &Spanned<Expr>, name: IStr) -> Result<Val> {390pub fn evaluate_named(ctx: Context, expr: &Expr, name: IStr) -> Result<Val> {
390 use Expr::*;391 use Expr::*;
391 Ok(match &**expr {392 Ok(match &*expr {
392 Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),393 Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),
393 _ => evaluate(ctx, expr)?,394 _ => evaluate(ctx, expr)?,
394 })395 })
417 // because the standalone super literal is not supported, that is because in other418 // because the standalone super literal is not supported, that is because in other
418 // implementations `in super` treated differently from `in smth_else`.419 // implementations `in super` treated differently from `in smth_else`.
419 BinaryOp(bin)420 BinaryOp(bin)
420 if matches!(&*bin.rhs, Expr::Literal(LiteralType::Super))421 if matches!(&bin.rhs, Expr::Literal(LiteralType::Super))
421 && bin.op == BinaryOpType::In =>422 && bin.op == BinaryOpType::In =>
422 {423 {
423 let sup_this = ctx.try_sup_this()?;424 let sup_this = ctx.try_sup_this()?;
433 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,434 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,
434 Var(name) => in_frame(435 Var(name) => in_frame(
435 CallLocation::new(&name.span()),436 CallLocation::new(&name.span()),
436 || format!("local <{name}> access"),437 || format!("local <{}> access", &**name),
437 || ctx.binding((**name).clone())?.evaluate(),438 || ctx.binding((**name).clone())?.evaluate(),
438 )?,439 )?,
439 Index { indexable, parts } => ensure_sufficient_stack(|| {440 Index { indexable, parts } => ensure_sufficient_stack(|| {
440 let mut parts = parts.iter();441 let mut parts = parts.iter();
441 let mut indexable = if matches!(&***indexable, Expr::Literal(LiteralType::Super)) {442 let mut indexable = if matches!(&**indexable, Expr::Literal(LiteralType::Super)) {
442 let part = parts.next().expect("at least part should exist");443 let part = parts.next().expect("at least part should exist");
443 // sup_this existence check might also be skipped here for null-coalesce...444 // sup_this existence check might also be skipped here for null-coalesce...
444 // But I believe this might cause errors.445 // But I believe this might cause errors.
463 let name = name.into_flat();464 let name = name.into_flat();
464 match sup_this465 match sup_this
465 .get_super(name.clone())466 .get_super(name.clone())
466 .with_description_src(&part.value, || format!("field <{name}> access"))?467 .with_description_src(&part.span, || format!("field <{name}> access"))?
467 {468 {
468 Some(v) => v,469 Some(v) => v,
469 #[cfg(feature = "exp-null-coaelse")]470 #[cfg(feature = "exp-null-coaelse")]
485 indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {486 indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {
486 (Val::Obj(v), Val::Str(key)) => match v487 (Val::Obj(v), Val::Str(key)) => match v
487 .get(key.clone().into_flat())488 .get(key.clone().into_flat())
488 .with_description_src(&part.value, || format!("field <{key}> access"))?489 .with_description_src(&part.span, || format!("field <{key}> access"))?
489 {490 {
490 Some(v) => v,491 Some(v) => v,
491 #[cfg(feature = "exp-null-coaelse")]492 #[cfg(feature = "exp-null-coaelse")]
497 key.clone().into_flat(),498 key.clone().into_flat(),
498 suggestions,499 suggestions,
499 )))500 )))
500 .with_description_src(&part.value, || format!("field <{key}> access"));501 .with_description_src(&part.span, || format!("field <{key}> access"));
501 }502 }
502 },503 },
503 (Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(504 (Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(
605 evaluate_assert(ctx.clone(), &assert.assert)?;606 evaluate_assert(ctx.clone(), &assert.assert)?;
606 evaluate(ctx, &assert.rest)?607 evaluate(ctx, &assert.rest)?
607 }608 }
608 ErrorStmt(e) => in_frame(609 ErrorStmt(s, e) => in_frame(
609 CallLocation::new(&e.span()),610 CallLocation::new(&s),
610 || "error statement".to_owned(),611 || "error statement".to_owned(),
611 || bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),612 || bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),
612 )?,613 )?,
613 IfElse(if_else) => {614 IfElse(if_else) => {
614 if in_frame(615 if
615 CallLocation::new(&if_else.cond.0.span()),616 // FIXME
616 || "if condition".to_owned(),617 //in_frame(
618 // CallLocation::new(&if_else.cond.0.span()),
619 // || "if condition".to_owned(),
620 // ||
617 || bool::from_untyped(evaluate(ctx.clone(), &if_else.cond.0)?),621 bool::from_untyped(evaluate(ctx.clone(), &if_else.cond.0)?)?
622 // )?
618 )? {623 {
619 evaluate(ctx, &if_else.cond_then)?624 evaluate(ctx, &if_else.cond_then)?
620 } else {625 } else {
621 match &if_else.cond_else {626 match &if_else.cond_else {
626 }631 }
627 Slice(slice) => {632 Slice(slice) => {
628 fn parse_idx<T: Typed + FromUntyped>(633 fn parse_idx<T: Typed + FromUntyped>(
629 loc: CallLocation<'_>,
630 ctx: Context,634 ctx: Context,
631 expr: Option<&Spanned<Expr>>,635 expr: Option<&Spanned<Expr>>,
632 desc: &'static str,636 desc: &'static str,
633 ) -> Result<Option<T>> {637 ) -> Result<Option<T>> {
634 if let Some(value) = expr {638 if let Some(value) = expr {
635 Ok(in_frame(639 Ok(in_frame(
636 loc,640 CallLocation::new(&value.span()),
637 || format!("slice {desc}"),641 || format!("slice {desc}"),
638 || <Option<T>>::from_untyped(evaluate(ctx, value)?),642 || <Option<T>>::from_untyped(evaluate(ctx, value)?),
639 )?)643 )?)
643 }647 }
644648
645 let indexable = evaluate(ctx.clone(), &slice.value)?;649 let indexable = evaluate(ctx.clone(), &slice.value)?;
646 let loc = CallLocation::new(&loc);
647650
648 let start = parse_idx(loc, ctx.clone(), slice.slice.start.as_ref(), "start")?;651 let start = parse_idx(ctx.clone(), slice.slice.start.as_ref(), "start")?;
649 let end = parse_idx(loc, ctx.clone(), slice.slice.end.as_ref(), "end")?;652 let end = parse_idx(ctx.clone(), slice.slice.end.as_ref(), "end")?;
650 let step = parse_idx(loc, ctx, slice.slice.step.as_ref(), "step")?;653 let step = parse_idx(ctx, slice.slice.step.as_ref(), "step")?;
651654
652 IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?655 IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?
653 }656 }
654 Import(kind, path) => {657 Import(kind, path) => {
655 let Expr::Str(path) = &***path else {658 let Expr::Str(path) = &**path else {
656 bail!("computed imports are not supported")659 bail!("computed imports are not supported")
657 };660 };
658 let tmp = loc.clone().0;
659 with_state(|s| {661 with_state(|s| {
662 let span = kind.span();
660 let resolved_path = s.resolve_from(tmp.source_path(), path)?;663 let resolved_path = s.resolve_from(span.0.source_path(), path)?;
661 Ok(match kind {664 Ok(match &**kind {
662 ImportKind::Normal => in_frame(665 ImportKind::Normal => in_frame(
663 CallLocation::new(&loc),666 CallLocation::new(&span),
664 || format!("import {:?}", path.clone()),667 || format!("import {:?}", path.clone()),
665 || s.import_resolved(resolved_path),668 || s.import_resolved(resolved_path),
666 )?,669 )?,
modifiedcrates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth
147147
148pub fn evaluate_binary_op_special(148pub fn evaluate_binary_op_special(
149 ctx: Context,149 ctx: Context,
150 a: &Spanned<Expr>,150 a: &Expr,
151 op: BinaryOpType,151 op: BinaryOpType,
152 b: &Spanned<Expr>,152 b: &Expr,
153) -> Result<Val> {153) -> Result<Val> {
154 use BinaryOpType::*;154 use BinaryOpType::*;
155 use Val::*;155 use Val::*;
modifiedcrates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth
3use educe::Educe;3use educe::Educe;
4use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_gcmodule::{Cc, Trace};
5use jrsonnet_interner::IStr;5use jrsonnet_interner::IStr;
6pub use jrsonnet_macros::builtin;
7use jrsonnet_ir::{ArgsDesc, Destruct, Expr, ExprParams, Span, Spanned};6use jrsonnet_ir::{ArgsDesc, Destruct, Expr, ExprParams, Span, Spanned};
7pub use jrsonnet_macros::builtin;
88
9use self::{9use self::{
10 builtin::{Builtin, StaticBuiltin},10 builtin::{Builtin, StaticBuiltin},
71 /// Function parameter definition71 /// Function parameter definition
72 pub params: ExprParams,72 pub params: ExprParams,
73 /// Function body73 /// Function body
74 pub body: Rc<Spanned<Expr>>,74 pub body: Rc<Expr>,
75}75}
76impl FuncDesc {76impl FuncDesc {
77 /// Create body context, but fill arguments without defaults with lazy error77 /// Create body context, but fill arguments without defaults with lazy error
256 #[cfg(feature = "exp-destruct")]256 #[cfg(feature = "exp-destruct")]
257 _ => return false,257 _ => return false,
258 };258 };
259 **desc.body == Expr::Var(id.clone())259 matches!(&*desc.body, Expr::Var(v) if &**v == id)
260 }260 }
261 _ => false,261 _ => false,
262 }262 }
modifiedcrates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth
22
3use jrsonnet_ir::{3use jrsonnet_ir::{
4 function::{FunctionSignature, ParamName},4 function::{FunctionSignature, ParamName},
5 ArgsDesc, Expr, ExprParams, Spanned,5 ArgsDesc, Expr, ExprParams,
6};6};
7use rustc_hash::FxHashMap;7use rustc_hash::FxHashMap;
88
15 Context, Pending, Thunk, Val,15 Context, Pending, Thunk, Val,
16};16};
1717
18fn eval_arg(ctx: Context, arg: &Rc<Spanned<Expr>>, tailstrict: bool) -> Result<Thunk<Val>> {18fn eval_arg(ctx: Context, arg: &Rc<Expr>, tailstrict: bool) -> Result<Thunk<Val>> {
19 if tailstrict {19 if tailstrict {
20 Ok(Thunk::evaluated(evaluate(ctx, arg)?))20 Ok(Thunk::evaluated(evaluate(ctx, arg)?))
21 } else {21 } else {
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
5extern crate self as jrsonnet_evaluator;5extern crate self as jrsonnet_evaluator;
66
7mod arr;7mod arr;
8pub mod async_import;8// pub mod async_import;
9mod ctx;9mod ctx;
10mod dynamic;10mod dynamic;
11pub mod error;11pub mod error;
187struct FileData {187struct FileData {
188 string: Option<IStr>,188 string: Option<IStr>,
189 bytes: Option<IBytes>,189 bytes: Option<IBytes>,
190 parsed: Option<Rc<Spanned<Expr>>>,190 parsed: Option<Rc<Expr>>,
191 evaluated: Option<Val>,191 evaluated: Option<Val>,
192192
193 evaluating: bool,193 evaluating: bool,
modifiedcrates/jrsonnet-ir/src/expr.rsdiffbeforeafterboth
17 /// {fixed: 2}17 /// {fixed: 2}
18 Fixed(IStr),18 Fixed(IStr),
19 /// {["dyn"+"amic"]: 3}19 /// {["dyn"+"amic"]: 3}
20 Dyn(Spanned<Expr>),20 Dyn(Expr),
21}21}
2222
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Acyclic)]23#[derive(Debug, Clone, Copy, PartialEq, Eq, Acyclic)]
46 pub plus: bool,46 pub plus: bool,
47 pub params: Option<ExprParams>,47 pub params: Option<ExprParams>,
48 pub visibility: Visibility,48 pub visibility: Visibility,
49 pub value: Rc<Spanned<Expr>>,49 pub value: Rc<Expr>,
50}50}
5151
52#[derive(Debug, PartialEq, Acyclic)]52#[derive(Debug, PartialEq, Acyclic)]
152#[derive(Debug, PartialEq, Acyclic)]152#[derive(Debug, PartialEq, Acyclic)]
153pub struct ExprParam {153pub struct ExprParam {
154 pub destruct: Destruct,154 pub destruct: Destruct,
155 pub default: Option<Rc<Spanned<Expr>>>,155 pub default: Option<Rc<Expr>>,
156}156}
157157
158/// Defined function parameters158/// Defined function parameters
194194
195#[derive(Debug, PartialEq, Acyclic)]195#[derive(Debug, PartialEq, Acyclic)]
196pub struct ArgsDesc {196pub struct ArgsDesc {
197 pub unnamed: Vec<Rc<Spanned<Expr>>>,197 pub unnamed: Vec<Rc<Expr>>,
198 pub named: Vec<(IStr, Rc<Spanned<Expr>>)>,198 pub named: Vec<(IStr, Rc<Expr>)>,
199}199}
200impl ArgsDesc {200impl ArgsDesc {
201 pub fn new(unnamed: Vec<Rc<Spanned<Expr>>>, named: Vec<(IStr, Rc<Spanned<Expr>>)>) -> Self {201 pub fn new(unnamed: Vec<Rc<Expr>>, named: Vec<(IStr, Rc<Expr>)>) -> Self {
202 Self { unnamed, named }202 Self { unnamed, named }
203 }203 }
204}204}
277pub enum BindSpec {277pub enum BindSpec {
278 Field {278 Field {
279 into: Destruct,279 into: Destruct,
280 value: Rc<Spanned<Expr>>,280 value: Rc<Expr>,
281 },281 },
282 Function {282 Function {
283 name: IStr,283 name: IStr,
284 params: ExprParams,284 params: ExprParams,
285 value: Rc<Spanned<Expr>>,285 value: Rc<Expr>,
286 },286 },
287}287}
288impl BindSpec {288impl BindSpec {
295}295}
296296
297#[derive(Debug, PartialEq, Acyclic)]297#[derive(Debug, PartialEq, Acyclic)]
298pub struct IfSpecData(pub Spanned<Expr>);298pub struct IfSpecData(pub Expr);
299299
300#[derive(Debug, PartialEq, Acyclic)]300#[derive(Debug, PartialEq, Acyclic)]
301pub struct ForSpecData(pub Destruct, pub Spanned<Expr>);301pub struct ForSpecData(pub Destruct, pub Expr);
302302
303#[derive(Debug, PartialEq, Acyclic)]303#[derive(Debug, PartialEq, Acyclic)]
304pub enum CompSpec {304pub enum CompSpec {
305 IfSpec(IfSpecData),305 IfSpec(Spanned<IfSpecData>),
306 ForSpec(ForSpecData),306 ForSpec(Spanned<ForSpecData>),
307}307}
308308
309#[derive(Debug, PartialEq, Acyclic)]309#[derive(Debug, PartialEq, Acyclic)]
346#[derive(Debug, PartialEq, Acyclic)]346#[derive(Debug, PartialEq, Acyclic)]
347pub struct AssertExpr {347pub struct AssertExpr {
348 pub assert: AssertStmt,348 pub assert: AssertStmt,
349 pub rest: Spanned<Expr>,349 pub rest: Expr,
350}350}
351351
352#[derive(Debug, PartialEq, Acyclic)]352#[derive(Debug, PartialEq, Acyclic)]
353pub struct BinaryOp {353pub struct BinaryOp {
354 pub lhs: Spanned<Expr>,354 pub lhs: Expr,
355 pub op: BinaryOpType,355 pub op: BinaryOpType,
356 pub rhs: Spanned<Expr>,356 pub rhs: Expr,
357}357}
358358
359#[derive(Debug, PartialEq, Acyclic)]359#[derive(Debug, PartialEq, Acyclic)]
366#[derive(Debug, PartialEq, Acyclic)]366#[derive(Debug, PartialEq, Acyclic)]
367pub struct IfElse {367pub struct IfElse {
368 pub cond: IfSpecData,368 pub cond: IfSpecData,
369 pub cond_then: Spanned<Expr>,369 pub cond_then: Expr,
370 pub cond_else: Option<Spanned<Expr>>,370 pub cond_else: Option<Expr>,
371}371}
372372
373#[derive(Debug, PartialEq, Acyclic)]373#[derive(Debug, PartialEq, Acyclic)]
374pub struct Slice {374pub struct Slice {
375 pub value: Spanned<Expr>,375 pub value: Expr,
376 pub slice: SliceDesc,376 pub slice: SliceDesc,
377}377}
378378
389 Var(Spanned<IStr>),389 Var(Spanned<IStr>),
390390
391 /// Array of expressions: [1, 2, "Hello"]391 /// Array of expressions: [1, 2, "Hello"]
392 Arr(Rc<Vec<Spanned<Expr>>>),392 Arr(Rc<Vec<Expr>>),
393 /// Array comprehension:393 /// Array comprehension:
394 /// ```jsonnet394 /// ```jsonnet
395 /// ingredients: [395 /// ingredients: [
401 /// ]401 /// ]
402 /// ],402 /// ],
403 /// ```403 /// ```
404 ArrComp(Rc<Spanned<Expr>>, Vec<CompSpec>),404 ArrComp(Rc<Expr>, Vec<CompSpec>),
405405
406 /// Object: {a: 2}406 /// Object: {a: 2}
407 Obj(ObjBody),407 Obj(ObjBody),
408 /// Object extension: var1 {b: 2}408 /// Object extension: var1 {b: 2}
409 ObjExtend(Rc<Spanned<Expr>>, ObjBody),409 ObjExtend(Rc<Expr>, ObjBody),
410410
411 /// -2411 /// -2
412 UnaryOp(UnaryOpType, Box<Spanned<Expr>>),412 UnaryOp(UnaryOpType, Box<Expr>),
413 /// 2 - 2413 /// 2 - 2
414 BinaryOp(Box<BinaryOp>),414 BinaryOp(Box<BinaryOp>),
415 /// assert 2 == 2 : "Math is broken"415 /// assert 2 == 2 : "Math is broken"
416 AssertExpr(Rc<AssertExpr>),416 AssertExpr(Rc<AssertExpr>),
417 /// local a = 2; { b: a }417 /// local a = 2; { b: a }
418 LocalExpr(Vec<BindSpec>, Box<Spanned<Expr>>),418 LocalExpr(Vec<BindSpec>, Box<Expr>),
419419
420 /// import* "hello"420 /// import* "hello"
421 Import(ImportKind, Box<Spanned<Expr>>),421 Import(Spanned<ImportKind>, Box<Expr>),
422 /// error "I'm broken"422 /// error "I'm broken"
423 ErrorStmt(Box<Spanned<Expr>>),423 ErrorStmt(Span, Box<Expr>),
424 /// a(b, c)424 /// a(b, c)
425 Apply(Box<Spanned<Expr>>, Spanned<ArgsDesc>, bool),425 Apply(Box<Expr>, Spanned<ArgsDesc>, bool),
426 /// a[b], a.b, a?.b426 /// a[b], a.b, a?.b
427 Index {427 Index {
428 indexable: Box<Spanned<Expr>>,428 indexable: Box<Expr>,
429 parts: Vec<IndexPart>,429 parts: Vec<IndexPart>,
430 },430 },
431 /// function(x) x431 /// function(x) x
432 Function(ExprParams, Rc<Spanned<Expr>>),432 Function(ExprParams, Rc<Expr>),
433 /// if true == false then 1 else 2433 /// if true == false then 1 else 2
434 IfElse(Box<IfElse>),434 IfElse(Box<IfElse>),
435 Slice(Box<Slice>),435 Slice(Box<Slice>),
436}436}
437437
438#[derive(Debug, PartialEq, Acyclic)]438#[derive(Debug, PartialEq, Acyclic)]
439pub struct IndexPart {439pub struct IndexPart {
440 pub span: Span,
440 pub value: Spanned<Expr>,441 pub value: Expr,
441 #[cfg(feature = "exp-null-coaelse")]442 #[cfg(feature = "exp-null-coaelse")]
442 pub null_coaelse: bool,443 pub null_coaelse: bool,
443}444}
461}462}
462463
463#[derive(Clone, PartialEq, Acyclic)]464#[derive(Clone, PartialEq, Acyclic)]
464pub struct Spanned<T: Acyclic>(T, Span);465pub struct Spanned<T: Acyclic>(pub T, pub Span);
465impl<T: Acyclic> Deref for Spanned<T> {466impl<T: Acyclic> Deref for Spanned<T> {
466 type Target = T;467 type Target = T;
467 fn deref(&self) -> &Self::Target {468 fn deref(&self) -> &Self::Target {
modifiedcrates/jrsonnet-peg-parser/Cargo.tomldiffbeforeafterboth
7version.workspace = true7version.workspace = true
88
9[dependencies]9[dependencies]
10jrsonnet-gcmodule.workspace = true
10jrsonnet-ir.workspace = true11jrsonnet-ir.workspace = true
11peg.workspace = true12peg.workspace = true
1213
modifiedcrates/jrsonnet-peg-parser/src/lib.rsdiffbeforeafterboth
1use jrsonnet_gcmodule::Acyclic;
1use jrsonnet_ir::{2use jrsonnet_ir::{
2 BinaryOp, Expr, ExprParams, IStr, IndexPart, Member, Slice, SliceDesc, Source, Span, Spanned,3 unescape, ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BindSpec, CompSpec, Destruct,
3 ExprParam, ArgsDesc, AssertExpr, ImportKind, LiteralType, IfElse, CompSpec, ForSpecData, IfSpecData, ObjMembers, ObjBody,4 DestructRest, Expr, ExprParam, ExprParams, FieldMember, FieldName, ForSpecData, IStr, IfElse,
4 ObjComp, FieldMember, Visibility, FieldName, unescape, AssertStmt, BindSpec, Destruct, DestructRest,5 IfSpecData, ImportKind, IndexPart, LiteralType, Member, ObjBody, ObjComp, ObjMembers, Slice,
6 SliceDesc, Source, Span, Spanned, Visibility,
5};7};
6use peg::parser;8use peg::parser;
7use std::rc::Rc;9use std::rc::Rc;
63 = params:param(s) ** comma() comma()? { ExprParams::new(params) }65 = params:param(s) ** comma() comma()? { ExprParams::new(params) }
64 / { ExprParams::new(Vec::new()) }66 / { ExprParams::new(Vec::new()) }
6567
66 pub rule arg(s: &ParserSettings) -> (Option<IStr>, Rc<Spanned<Expr>>)68 pub rule arg(s: &ParserSettings) -> (Option<IStr>, Rc<Expr>)
67 = name:(quiet! { (s:id() _ "=" !['='] _ {s})? } / expected!("<argument name>")) expr:expr(s) {(name, Rc::new(expr))}69 = name:(quiet! { (s:id() _ "=" !['='] _ {s})? } / expected!("<argument name>")) expr:expr(s) {(name, Rc::new(expr))}
6870
69 pub rule args(s: &ParserSettings) -> ArgsDesc71 pub rule args(s: &ParserSettings) -> ArgsDesc
133 / name:id() _ "(" _ params:params(s) _ ")" _ "=" _ value:expr(s) {BindSpec::Function{name, params, value: Rc::new(value)}}135 / name:id() _ "(" _ params:params(s) _ ")" _ "=" _ value:expr(s) {BindSpec::Function{name, params, value: Rc::new(value)}}
134136
135 pub rule assertion(s: &ParserSettings) -> AssertStmt137 pub rule assertion(s: &ParserSettings) -> AssertStmt
136 = keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { AssertStmt(cond, msg) }138 = keyword("assert") _ cond:spanned(<expr(s)>, s) msg:(_ ":" _ e:spanned(<expr(s)>, s) {e})? { AssertStmt(cond, msg) }
137139
138 pub rule whole_line() -> &'input str140 pub rule whole_line() -> &'input str
139 = str:$((!['\n'][_])* "\n") {str}141 = str:$((!['\n'][_])* "\n") {str}
241 pub rule forspec(s: &ParserSettings) -> ForSpecData243 pub rule forspec(s: &ParserSettings) -> ForSpecData
242 = keyword("for") _ id:destruct(s) _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}244 = keyword("for") _ id:destruct(s) _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}
243 rule compspec(s: &ParserSettings) -> CompSpec245 rule compspec(s: &ParserSettings) -> CompSpec
244 = i:ifspec(s) { CompSpec::IfSpec(i) } / f:forspec(s) {CompSpec::ForSpec(f)}246 = i:spanned(<ifspec(s)>, s) { CompSpec::IfSpec(i) } / f:spanned(<forspec(s)>, s) {CompSpec::ForSpec(f)}
245 pub rule compspecs(s: &ParserSettings) -> Vec<CompSpec>247 pub rule compspecs(s: &ParserSettings) -> Vec<CompSpec>
246 = specs:compspec(s) ++ _ {?248 = specs:compspec(s) ++ _ {?
247 if !matches!(specs[0], CompSpec::ForSpec(_)) {249 if !matches!(specs[0], CompSpec::ForSpec(_)) {
268 Err("!!!numbers are finite")270 Err("!!!numbers are finite")
269 }}271 }}
272
273 rule spanned<T: Acyclic>(x: rule<T>, s: &ParserSettings) -> Spanned<T>
274 = a:position!() n:x() b:position!() { Spanned::new(n, Span(s.source.clone(), a as u32, b as u32)) }
275
270 pub rule var_expr(s: &ParserSettings) -> Expr276 pub rule var_expr(s: &ParserSettings) -> Expr
271 = n:id() { Expr::Var(n) }277 = n:spanned(<id()>, s) { Expr::Var(n) }
272 pub rule id_loc(s: &ParserSettings) -> Spanned<Expr>278 pub rule id_loc(s: &ParserSettings) -> Spanned<Expr>
273 = a:position!() n:id() b:position!() { Spanned::new(Expr::Str(n), Span(s.source.clone(), a as u32,b as u32)) }279 = a:position!() n:id() b:position!() { Spanned::new(Expr::Str(n), Span(s.source.clone(), a as u32,b as u32)) }
274 pub rule if_then_else_expr(s: &ParserSettings) -> Expr280 pub rule if_then_else_expr(s: &ParserSettings) -> Expr
302 / array_expr(s)308 / array_expr(s)
303 / array_comp_expr(s)309 / array_comp_expr(s)
304310
305 / kind:import_kind() _ path:expr(s) {Expr::Import(kind, Box::new(path))}311 / kind:spanned(<import_kind()>, s) _ path:expr(s) {Expr::Import(kind, Box::new(path))}
306312
307 / var_expr(s)313 / var_expr(s)
308 / local_expr(s)314 / local_expr(s)
313 assert, rest319 assert, rest
314 })) }320 })) }
315321
316 / keyword("error") _ expr:expr(s) { Expr::ErrorStmt(Box::new(expr)) }322 / err_kw:spanned(<keyword("error")>, s) _ expr:expr(s) { Expr::ErrorStmt(err_kw.1, Box::new(expr)) }
317323
318 rule slice_part(s: &ParserSettings) -> Option<Spanned<Expr>>324 rule slice_part(s: &ParserSettings) -> Option<Spanned<Expr>>
319 = _ e:(e:expr(s) _{e})? {e}325 = _ e:(e:spanned(<expr(s)>, s) _{e})? {e}
320 pub rule slice_desc(s: &ParserSettings) -> SliceDesc326 pub rule slice_desc(s: &ParserSettings) -> SliceDesc
321 = start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {327 = start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {
322 let (end, step) = if let Some((end, step)) = pair {328 let (end, step) = if let Some((end, step)) = pair {
340 }346 }
341 use jrsonnet_ir::BinaryOpType::*;347 use jrsonnet_ir::BinaryOpType::*;
342 use jrsonnet_ir::UnaryOpType::*;348 use jrsonnet_ir::UnaryOpType::*;
343 rule expr(s: &ParserSettings) -> Spanned<Expr>349 rule expr(s: &ParserSettings) -> Expr
344 = precedence! {350 = precedence! {
345 "(" _ e:expr(s) _ ")" {e}
346 start:position!() v:@ end:position!() { Spanned::new(v, Span(s.source.clone(), start as u32, end as u32)) }
347 --
348 a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}351 a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}
349 a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {352 a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {
350 #[cfg(feature = "exp-null-coaelse")] return expr_bin!(a NullCoaelse b);353 #[cfg(feature = "exp-null-coaelse")] return expr_bin!(a NullCoaelse b);
385 --388 --
386 value:(@) _ "[" _ slice:slice_desc(s) _ "]" {Expr::Slice(Box::new(Slice{value, slice}))}389 value:(@) _ "[" _ slice:slice_desc(s) _ "]" {Expr::Slice(Box::new(Slice{value, slice}))}
387 indexable:(@) _ parts:index_part(s)+ {Expr::Index{indexable: Box::new(indexable), parts}}390 indexable:(@) _ parts:index_part(s)+ {Expr::Index{indexable: Box::new(indexable), parts}}
388 a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(Box::new(a), args, ts.is_some())}391 a:(@) _ args:spanned(<"(" _ a:args(s) _ ")" {a}>, s) ts:(_ keyword("tailstrict"))? {Expr::Apply(Box::new(a), args, ts.is_some())}
389 a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(Rc::new(a), body)}392 a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(Rc::new(a), body)}
390 --393 --
391 e:expr_basic(s) {e}394 e:expr_basic(s) {e}
395 "(" _ e:expr(s) _ ")" {e}
392 }396 }
393 pub rule index_part(s: &ParserSettings) -> IndexPart397 pub rule index_part(s: &ParserSettings) -> IndexPart
394 = n:("?" _ ensure_null_coaelse())? "." _ value:id_loc(s) {IndexPart {398 = n:("?" _ ensure_null_coaelse())? "." _ value:id_loc(s) {IndexPart {
395 value,399 span: value.1,
400 value: value.0,
396 #[cfg(feature = "exp-null-coaelse")]401 #[cfg(feature = "exp-null-coaelse")]
397 null_coaelse: n.is_some(),402 null_coaelse: n.is_some(),
398 }}403 }}
399 / n:("?" _ "." _ ensure_null_coaelse())? "[" _ value:expr(s) _ "]" {IndexPart {404 / n:("?" _ "." _ ensure_null_coaelse())? value:spanned(<"[" _ v:expr(s) _ "]" {v}>, s) {IndexPart {
400 value,405 span: value.1,
406 value: value.0,
401 #[cfg(feature = "exp-null-coaelse")]407 #[cfg(feature = "exp-null-coaelse")]
402 null_coaelse: n.is_some(),408 null_coaelse: n.is_some(),
403 }}409 }}
404410
405 pub rule jsonnet(s: &ParserSettings) -> Spanned<Expr> = _ e:expr(s) _ {e}411 pub rule jsonnet(s: &ParserSettings) -> Expr = _ e:expr(s) _ {e}
406 }412 }
407}413}
408414
409pub type ParseError = peg::error::ParseError<peg::str::LineCol>;415pub type ParseError = peg::error::ParseError<peg::str::LineCol>;
410pub fn parse(str: &str, settings: &ParserSettings) -> Result<Spanned<Expr>, ParseError> {416pub fn parse(str: &str, settings: &ParserSettings) -> Result<Expr, ParseError> {
411 jsonnet_parser::jsonnet(str, settings)417 jsonnet_parser::jsonnet(str, settings)
412}418}
413/// Used for importstr values419/// Used for importstr values