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

difftreelog

refactor(ir) flatten obj member

nztotvwkYaroslav Bolyukin2026-03-19parent: #9411dba.patch.diff
in: master

7 files changed

modifiedcrates/jrsonnet-evaluator/src/async_import.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/async_import.rs
+++ b/crates/jrsonnet-evaluator/src/async_import.rs
@@ -4,7 +4,7 @@
 use jrsonnet_gcmodule::Acyclic;
 use jrsonnet_parser::{
 	ArgsDesc, AssertExpr, AssertStmt, BindSpec, CompSpec, Destruct, Expr, FieldMember, FieldName,
-	ForSpecData, IfElse, IfSpecData, ImportKind, Member, ObjBody, Param, ParamsDesc,
+	ForSpecData, IfElse, IfSpecData, ImportKind, ObjBody, Param, ParamsDesc,
 	ParserSettings, Slice, SliceDesc, Source, SourcePath, Spanned,
 };
 use rustc_hash::FxHashMap;
@@ -102,31 +102,30 @@
 	}
 	fn in_obj(obj: &ObjBody, out: &mut FoundImports) {
 		match obj {
-			ObjBody::MemberList(v) => {
-				for member in v {
-					match member {
-						Member::Field(FieldMember {
-							name,
-							params,
-							value,
-							..
-						}) => {
-							match name {
-								FieldName::Fixed(_) => {}
-								FieldName::Dyn(expr) => find_imports(expr, out),
-							}
-							if let Some(params) = params {
-								in_params(params, out);
-							}
-							find_imports(value, out);
-						}
-						Member::BindStmt(_) => todo!(),
-						Member::AssertStmt(assert) => {
-							find_imports(&assert.0, out);
-							if let Some(expr) = &assert.1 {
-								find_imports(expr, out);
-							}
-						}
+			ObjBody::MemberList(obj) => {
+				for FieldMember {
+					name,
+					params,
+					value,
+					..
+				} in &obj.fields
+				{
+					match name {
+						FieldName::Fixed(_) => {}
+						FieldName::Dyn(expr) => find_imports(expr, out),
+					}
+					if let Some(params) = params {
+						in_params(params, out);
+					}
+					find_imports(value, out);
+				}
+				for _ in &*obj.locals {
+					todo!()
+				}
+				for assert in &*obj.asserts {
+					find_imports(&assert.0, out);
+					if let Some(expr) = &assert.1 {
+						find_imports(expr, out);
 					}
 				}
 			}
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -4,7 +4,7 @@
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{
 	ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, FieldMember, FieldName,
-	ForSpecData, IfSpecData, ImportKind, LiteralType, Member, ObjBody, ParamsDesc, Spanned,
+	ForSpecData, IfSpecData, ImportKind, LiteralType, ObjBody, ObjMembers, ParamsDesc, Spanned,
 };
 use jrsonnet_types::ValType;
 use rustc_hash::FxHashMap;
@@ -282,48 +282,38 @@
 }
 
 #[allow(clippy::too_many_lines)]
-pub fn evaluate_member_list_object(ctx: Context, members: &[Member]) -> Result<ObjValue> {
+pub fn evaluate_member_list_object(ctx: Context, members: &ObjMembers) -> Result<ObjValue> {
 	let mut builder = ObjValueBuilder::new();
-	let locals = Rc::new(
-		members
-			.iter()
-			.filter_map(|m| match m {
-				Member::BindStmt(bind) => Some(bind.clone()),
-				_ => None,
-			})
-			.collect::<Vec<_>>(),
-	);
+	let locals = members.locals.clone();
 
 	// We have single context for all fields, so we can cache binds
 	let uctx = CachedUnbound::new(evaluate_object_locals(ctx.clone(), locals));
 
-	for member in members {
-		match member {
-			Member::Field(field) => {
-				evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;
-			}
-			Member::AssertStmt(stmt) => {
-				#[derive(Trace)]
-				struct ObjectAssert<B: Trace> {
-					uctx: B,
-					assert: Rc<AssertStmt>,
+	for field in &members.fields {
+		evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), &field)?;
+	}
+
+	if !members.asserts.is_empty() {
+		#[derive(Trace)]
+		struct ObjectAssert<B: Trace> {
+			uctx: B,
+			asserts: Rc<Vec<AssertStmt>>,
+		}
+		impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {
+			fn run(&self, sup_this: SupThis) -> Result<()> {
+				let ctx = self.uctx.bind(sup_this)?;
+				for assert in &*self.asserts {
+					evaluate_assert(ctx.clone(), &assert)?;
 				}
-				impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {
-					fn run(&self, sup_this: SupThis) -> Result<()> {
-						let ctx = self.uctx.bind(sup_this)?;
-						evaluate_assert(ctx, &self.assert)
-					}
-				}
-				builder.assert(ObjectAssert {
-					uctx: uctx.clone(),
-					assert: stmt.clone(),
-				});
-			}
-			Member::BindStmt(_) => {
-				// Already handled
+				Ok(())
 			}
 		}
+		builder.assert(ObjectAssert {
+			uctx: uctx.clone(),
+			asserts: members.asserts.clone(),
+		});
 	}
+
 	Ok(builder.build())
 }
 
@@ -332,13 +322,7 @@
 		ObjBody::MemberList(members) => evaluate_member_list_object(ctx, members)?,
 		ObjBody::ObjComp(obj) => {
 			let mut builder = ObjValueBuilder::new();
-			let locals = Rc::new(
-				obj.pre_locals
-					.iter()
-					.chain(obj.post_locals.iter())
-					.cloned()
-					.collect::<Vec<_>>(),
-			);
+			let locals = obj.locals.clone();
 			evaluate_comp(ctx, &obj.compspecs, &mut |ctx| {
 				let uctx = evaluate_object_locals(ctx.clone(), locals.clone());
 
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -47,10 +47,10 @@
 }
 
 #[derive(Debug, PartialEq, Acyclic)]
-pub enum Member {
+pub(crate) enum Member {
 	Field(FieldMember),
 	BindStmt(BindSpec),
-	AssertStmt(Rc<AssertStmt>),
+	AssertStmt(AssertStmt),
 }
 
 #[derive(Debug, Clone, Copy, PartialEq, Eq, Acyclic)]
@@ -240,7 +240,7 @@
 	}
 }
 
-#[derive(Debug, Clone, PartialEq, Acyclic)]
+#[derive(Debug, PartialEq, Acyclic)]
 pub enum BindSpec {
 	Field {
 		into: Destruct,
@@ -275,15 +275,21 @@
 
 #[derive(Debug, PartialEq, Acyclic)]
 pub struct ObjComp {
-	pub pre_locals: Vec<BindSpec>,
+	pub locals: Rc<Vec<BindSpec>>,
 	pub field: Rc<FieldMember>,
-	pub post_locals: Vec<BindSpec>,
 	pub compspecs: Vec<CompSpec>,
 }
 
 #[derive(Debug, PartialEq, Acyclic)]
+pub struct ObjMembers {
+	pub locals: Rc<Vec<BindSpec>>,
+	pub asserts: Rc<Vec<AssertStmt>>,
+	pub fields: Vec<FieldMember>,
+}
+
+#[derive(Debug, PartialEq, Acyclic)]
 pub enum ObjBody {
-	MemberList(Vec<Member>),
+	MemberList(ObjMembers),
 	ObjComp(ObjComp),
 }
 
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-parser/src/lib.rs
1#![allow(clippy::redundant_closure_call, clippy::derive_partial_eq_without_eq)]23use std::rc::Rc;45use peg::parser;6mod expr;7pub use expr::*;8pub use jrsonnet_interner::IStr;9pub use peg;10mod location;11mod source;12mod unescape;13pub use location::CodeLocation;14pub use source::{15	Source, SourceDefaultIgnoreJpath, SourceDirectory, SourceFifo, SourceFile, SourcePath,16	SourcePathT, SourceVirtual,17};1819pub struct ParserSettings {20	pub source: Source,21}2223macro_rules! expr_bin {24	($a:ident $op:ident $b:ident) => {25		Expr::BinaryOp(Box::new(BinaryOp {26			lhs: $a,27			op: $op,28			rhs: $b,29		}))30	};31}32macro_rules! expr_un {33	($op:ident $a:ident) => {34		Expr::UnaryOp($op, Box::new($a))35	};36}3738parser! {39	grammar jsonnet_parser() for str {40		use peg::ParseLiteral;4142		rule eof() = quiet!{![_]} / expected!("<eof>")43		rule eol() = "\n" / eof()4445		/// Standard C-like comments46		rule comment()47			= "//" (!eol()[_])* eol()48			/ "/*" (!("*/")[_])* "*/"49			/ "#" (!eol()[_])* eol()5051		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")52		rule _() = quiet!{([' ' | '\r' | '\n' | '\t']+) / comment()}* / expected!("<whitespace>")5354		/// For comma-delimited elements55		rule comma() = quiet!{_ "," _} / expected!("<comma>")56		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}57		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}58		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']59		/// Sequence of digits60		rule uint_str() -> &'input str = a:$(digit()+ ("_" digit()+)*) { a }61		/// Number in scientific notation format62		rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.replace("_","").parse().map_err(|_| "<number>") }} / expected!("<number>")6364		/// Reserved word followed by any non-alphanumberic65		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()66		rule id() -> IStr = v:$(quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")) { v.into() }6768		rule keyword(id: &'static str) -> ()69			= ##parse_string_literal(id) end_of_ident()7071		pub rule param(s: &ParserSettings) -> expr::Param = name:destruct(s) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr.map(Rc::new)) }72		pub rule params(s: &ParserSettings) -> expr::ParamsDesc73			= params:param(s) ** comma() comma()? { expr::ParamsDesc(Rc::new(params)) }74			/ { expr::ParamsDesc(Rc::new(Vec::new())) }7576		pub rule arg(s: &ParserSettings) -> (Option<IStr>, Rc<Spanned<Expr>>)77			= name:(quiet! { (s:id() _ "=" !['='] _ {s})? } / expected!("<argument name>")) expr:expr(s) {(name, Rc::new(expr))}7879		pub rule args(s: &ParserSettings) -> expr::ArgsDesc80			= args:arg(s)**comma() comma()? {?81				let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();82				let mut unnamed = Vec::with_capacity(unnamed_count);83				let mut named = Vec::with_capacity(args.len() - unnamed_count);84				let mut named_started = false;85				for (name, value) in args {86					if let Some(name) = name {87						named_started = true;88						named.push((name, value));89					} else {90						if named_started {91							return Err("<named argument>")92						}93						unnamed.push(value);94					}95				}96				Ok(expr::ArgsDesc::new(unnamed, named))97			}9899		pub rule destruct_rest() -> expr::DestructRest100			= "..." into:(_ into:id() {into})? {if let Some(into) = into {101				expr::DestructRest::Keep(into)102			} else {expr::DestructRest::Drop}}103		pub rule destruct_array(s: &ParserSettings) -> expr::Destruct104			= "[" _ start:destruct(s)**comma() rest:(105				comma() _ rest:destruct_rest()? end:(106					comma() end:destruct(s)**comma() (_ comma())? {end}107					/ comma()? {Vec::new()}108				) {(rest, end)}109				/ comma()? {(None, Vec::new())}110			) _ "]" {?111				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Array {112					start,113					rest: rest.0,114					end: rest.1,115				});116				#[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")117			}118		pub rule destruct_object(s: &ParserSettings) -> expr::Destruct119			= "{" _120				fields:(name:id() into:(_ ":" _ into:destruct(s) {into})? default:(_ "=" _ v:expr(s) {v})? {(name, into, default)})**comma()121				rest:(122					comma() rest:destruct_rest()? {rest}123					/ comma()? {None}124				)125			_ "}" {?126				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Object {127					fields,128					rest,129				});130				#[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")131			}132		pub rule destruct(s: &ParserSettings) -> expr::Destruct133			= v:id() {expr::Destruct::Full(v)}134			/ "?" {?135				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Skip);136				#[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")137			}138			/ arr:destruct_array(s) {arr}139			/ obj:destruct_object(s) {obj}140141		pub rule bind(s: &ParserSettings) -> expr::BindSpec142			= into:destruct(s) _ "=" _ expr:expr(s) {expr::BindSpec::Field{into, value: Rc::new(expr)}}143			/ name:id() _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec::Function{name, params, value: Rc::new(expr)}}144145		pub rule assertion(s: &ParserSettings) -> expr::AssertStmt146			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }147148		pub rule whole_line() -> &'input str149			= str:$((!['\n'][_])* "\n") {str}150		pub rule string_block() -> String151			= "|||" chomped:"-"? (!['\n']single_whitespace())* "\n"152			empty_lines:$(['\n']*)153			prefix:[' ' | '\t']+ first_line:whole_line()154			lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*155			[' ' | '\t']*<, {prefix.len() - 1}> "|||"156			{157				let mut l = empty_lines.to_owned();158				l.push_str(first_line);159				l.extend(lines);160				if chomped.is_some() {161					debug_assert!(l.ends_with('\n'));162					l.truncate(l.len() - 1);163				}164				l165			}166167		rule hex_char()168			= quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")169170		rule string_char(c: rule<()>)171			= (!['\\']!c()[_])+172			/ "\\\\"173			/ "\\u" hex_char() hex_char() hex_char() hex_char()174			/ "\\x" hex_char() hex_char()175			/ ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't' | '"' | '\''] } / expected!("<escape character>"))176		pub rule string() -> String177			= ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}178			/ ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}179			/ quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}180			/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}181			/ string_block() } / expected!("<string>")182183		pub rule field_name(s: &ParserSettings) -> expr::FieldName184			= name:id() {expr::FieldName::Fixed(name)}185			/ name:string() {expr::FieldName::Fixed(name.into())}186			/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}187		pub rule visibility() -> expr::Visibility188			= ":::" {expr::Visibility::Unhide}189			/ "::" {expr::Visibility::Hidden}190			/ ":" {expr::Visibility::Normal}191		pub rule field(s: &ParserSettings) -> expr::FieldMember192			= name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{193				name,194				plus: plus.is_some(),195				params: None,196				visibility,197				value: Rc::new(value),198			}}199			/ name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{200				name,201				plus: false,202				params: Some(params),203				visibility,204				value: Rc::new(value),205			}}206		pub rule obj_local(s: &ParserSettings) -> BindSpec207			= keyword("local") _ bind:bind(s) {bind}208		pub rule member(s: &ParserSettings) -> expr::Member209			= bind:obj_local(s) {expr::Member::BindStmt(bind)}210			/ assertion:assertion(s) {expr::Member::AssertStmt(Rc::new(assertion))}211			/ field:field(s) {expr::Member::Field(field)}212		pub rule objinside(s: &ParserSettings) -> expr::ObjBody213			= pre_locals:(b: obj_local(s) comma() {b})* &"[" field:field(s) post_locals:(comma() b:obj_local(s) {b})* _ ("," _)? forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {214				let mut compspecs = vec![CompSpec::ForSpec(forspec)];215				compspecs.extend(others.unwrap_or_default());216				expr::ObjBody::ObjComp(expr::ObjComp{217					pre_locals,218					field: Rc::new(field),219					post_locals,220					compspecs,221				})222			}223			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}224		pub rule ifspec(s: &ParserSettings) -> IfSpecData225			= keyword("if") _ expr:expr(s) {IfSpecData(expr)}226		pub rule forspec(s: &ParserSettings) -> ForSpecData227			= keyword("for") _ id:destruct(s) _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}228		pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>229			= s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}230		pub rule local_expr(s: &ParserSettings) -> Expr231			= keyword("local") _ binds:bind(s) ** comma() (_ ",")? _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, Box::new(expr)) }232		pub rule string_expr(s: &ParserSettings) -> Expr233			= s:string() {Expr::Str(s.into())}234		pub rule obj_expr(s: &ParserSettings) -> Expr235			= "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}236		pub rule array_expr(s: &ParserSettings) -> Expr237			= "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(Rc::new(elems))}238		pub rule array_comp_expr(s: &ParserSettings) -> Expr239			= "[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {240				let mut specs = vec![CompSpec::ForSpec(forspec)];241				specs.extend(others.unwrap_or_default());242				Expr::ArrComp(Rc::new(expr), specs)243			}244		pub rule number_expr(s: &ParserSettings) -> Expr245			= n:number() {? if n.is_finite() {246				Ok(expr::Expr::Num(n))247			} else {248				Err("!!!numbers are finite")249			}}250		pub rule var_expr(s: &ParserSettings) -> Expr251			= n:id() { expr::Expr::Var(n) }252		pub rule id_loc(s: &ParserSettings) -> Spanned<Expr>253			= a:position!() n:id() b:position!() { Spanned::new(expr::Expr::Str(n), Span(s.source.clone(), a as u32,b as u32)) }254		pub rule if_then_else_expr(s: &ParserSettings) -> Expr255			= cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse(Box::new(IfElse{256				cond,257				cond_then,258				cond_else,259			}))}260261		pub rule literal(s: &ParserSettings) -> Expr262			= v:(263				keyword("null") {LiteralType::Null}264				/ keyword("true") {LiteralType::True}265				/ keyword("false") {LiteralType::False}266				/ keyword("self") {LiteralType::This}267				/ keyword("$") {LiteralType::Dollar}268				/ keyword("super") {LiteralType::Super}269			) {Expr::Literal(v)}270271		rule import_kind() -> ImportKind272			= keyword("importstr") { ImportKind::Str }273			/ keyword("importbin") { ImportKind::Bin }274			/ keyword("import") { ImportKind::Normal }275276		pub rule expr_basic(s: &ParserSettings) -> Expr277			= literal(s)278279			/ string_expr(s) / number_expr(s)280			/ array_expr(s)281			/ obj_expr(s)282			/ array_expr(s)283			/ array_comp_expr(s)284285			/ kind:import_kind() _ path:expr(s) {Expr::Import(kind, Box::new(path))}286287			/ var_expr(s)288			/ local_expr(s)289			/ if_then_else_expr(s)290291			/ keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, Rc::new(expr))}292			/ assert:assertion(s) _ ";" _ rest:expr(s) { Expr::AssertExpr(Rc::new(AssertExpr{293				assert, rest294			})) }295296			/ keyword("error") _ expr:expr(s) { Expr::ErrorStmt(Box::new(expr)) }297298		rule slice_part(s: &ParserSettings) -> Option<Spanned<Expr>>299			= _ e:(e:expr(s) _{e})? {e}300		pub rule slice_desc(s: &ParserSettings) -> SliceDesc301			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {302				let (end, step) = if let Some((end, step)) = pair {303					(end, step)304				}else{305					(None, None)306				};307308				SliceDesc { start, end, step }309			}310311		rule binop(x: rule<()>) -> ()312			= quiet!{ x() } / expected!("<binary op>")313		rule unaryop(x: rule<()>) -> ()314			= quiet!{ x() } / expected!("<unary op>")315316		rule ensure_null_coaelse()317			= "" {?318				#[cfg(not(feature = "exp-null-coaelse"))] return Err("!!!experimental null coaelscing was not enabled");319				#[cfg(feature = "exp-null-coaelse")] Ok(())320			}321		use BinaryOpType::*;322		use UnaryOpType::*;323		rule expr(s: &ParserSettings) -> Spanned<Expr>324			= precedence! {325				"(" _ e:expr(s) _ ")" {e}326				start:position!() v:@ end:position!() { Spanned::new(v, Span(s.source.clone(), start as u32, end as u32)) }327				--328				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}329				a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {330					#[cfg(feature = "exp-null-coaelse")] return expr_bin!(a NullCoaelse b);331					unreachable!("ensure_null_coaelse will fail if feature is not enabled")332				}333				--334				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}335				--336				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}337				--338				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}339				--340				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}341				--342				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}343				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}344				--345				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}346				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}347				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}348				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}349				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}350				--351				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}352				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}353				--354				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}355				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}356				--357				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}358				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}359				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}360				--361						unaryop(<"+">) _ b:@ {expr_un!(Plus b)}362						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}363						unaryop(<"!">) _ b:@ {expr_un!(Not b)}364						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}365				--366				value:(@) _ "[" _ slice:slice_desc(s) _ "]" {Expr::Slice(Box::new(Slice{value, slice}))}367				indexable:(@) _ parts:index_part(s)+ {Expr::Index{indexable: Box::new(indexable), parts}}368				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(Box::new(a), args, ts.is_some())}369				a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(Rc::new(a), body)}370				--371				e:expr_basic(s) {e}372			}373		pub rule index_part(s: &ParserSettings) -> IndexPart374		= n:("?" _ ensure_null_coaelse())? "." _ value:id_loc(s) {IndexPart {375			value,376			#[cfg(feature = "exp-null-coaelse")]377			null_coaelse: n.is_some(),378		}}379		/ n:("?" _ "." _ ensure_null_coaelse())? "[" _ value:expr(s) _ "]" {IndexPart {380			value,381			#[cfg(feature = "exp-null-coaelse")]382			null_coaelse: n.is_some(),383		}}384385		pub rule jsonnet(s: &ParserSettings) -> Spanned<Expr> = _ e:expr(s) _ {e}386	}387}388389pub type ParseError = peg::error::ParseError<peg::str::LineCol>;390pub fn parse(str: &str, settings: &ParserSettings) -> Result<Spanned<Expr>, ParseError> {391	jsonnet_parser::jsonnet(str, settings)392}393/// Used for importstr values394pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> Spanned<Expr> {395	let len = str.len();396	Spanned::new(Expr::Str(str), Span(settings.source.clone(), 0, len as u32))397}398399#[cfg(test)]400pub mod tests {401	use insta::assert_snapshot;402	use jrsonnet_interner::IStr;403404	use super::parse;405	use crate::{source::Source, ParserSettings};406407	fn parsep(s: &str) -> String {408		let v = parse(409			s,410			&ParserSettings {411				source: Source::new_virtual("<test>".into(), IStr::empty()),412			},413		)414		.unwrap();415		format!("{v:#?}")416	}417418	macro_rules! parse {419		($s:expr) => {420			assert_snapshot!(parsep($s));421		};422	}423424	#[test]425	fn multiline_string() {426		parse!("|||\n    Hello world!\n     a\n|||");427		parse!("|||\n  Hello world!\n   a\n|||");428		parse!("|||\n\t\tHello world!\n\t\t\ta\n|||");429		parse!("|||\n   Hello world!\n    a\n |||");430	}431432	#[test]433	fn slice() {434		parse!("a[1:]");435		parse!("a[1::]");436		parse!("a[:1:]");437		parse!("a[::1]");438		parse!("str[:len - 1]");439	}440441	#[test]442	fn string_escaping() {443		parse!(r#""Hello, \"world\"!""#);444		parse!(r#"'Hello \'world\'!'"#);445		parse!(r#"'\\\\'"#);446	}447448	#[test]449	fn string_unescaping() {450		parse!(r#""Hello\nWorld""#);451	}452453	#[test]454	fn string_verbantim() {455		parse!(r#"@"Hello\n""World""""#);456	}457458	#[test]459	fn imports() {460		parse!("import \"hello\"");461		parse!("importstr \"garnish.txt\"");462		parse!("importbin \"garnish.bin\"");463	}464465	#[test]466	fn empty_object() {467		parse!("{}");468	}469470	#[test]471	fn basic_math() {472		parse!("2+2*2");473		parse!("2	+ 	  2	  *	2   	");474		parse!("2+(2+2*2)");475		parse!("2//comment\n+//comment\n3/*test*/*/*test*/4");476	}477478	#[test]479	fn suffix() {480		parse!("std.test");481		parse!("std(2)");482		parse!("std.test(2)");483		parse!("a[b]");484	}485486	#[test]487	fn array_comp() {488		parse!("[std.deepJoin(x) for x in arr]");489	}490491	#[test]492	fn reserved() {493		parse!("null");494		parse!("nulla");495	}496497	#[test]498	fn multiple_args_buf() {499		parse!("a(b, null_fields)");500	}501502	#[test]503	fn infix_precedence() {504		parse!("!a && !b");505		parse!("!a / !b");506	}507508	#[test]509	fn double_negation() {510		parse!("!!a");511	}512513	#[test]514	fn array_test_error() {515		parse!("[a for a in b if c for e in f]");516	}517518	#[test]519	fn missing_newline_between_comment_and_eof() {520		parse!(521			"{a:1}522523			//+213"524		);525	}526527	#[test]528	fn default_param_before_nondefault() {529		parse!("local x(foo = 'foo', bar) = null; null");530	}531532	#[test]533	fn add_location_info_to_all_sub_expressions() {534		parse!("{} { local x = 1, x: x } + {}");535	}536}
after · crates/jrsonnet-parser/src/lib.rs
1#![allow(clippy::redundant_closure_call, clippy::derive_partial_eq_without_eq)]23use std::rc::Rc;45use peg::parser;6mod expr;7pub use expr::*;8pub use jrsonnet_interner::IStr;9pub use peg;10mod location;11mod source;12mod unescape;13pub use location::CodeLocation;14pub use source::{15	Source, SourceDefaultIgnoreJpath, SourceDirectory, SourceFifo, SourceFile, SourcePath,16	SourcePathT, SourceVirtual,17};1819pub struct ParserSettings {20	pub source: Source,21}2223macro_rules! expr_bin {24	($a:ident $op:ident $b:ident) => {25		Expr::BinaryOp(Box::new(BinaryOp {26			lhs: $a,27			op: $op,28			rhs: $b,29		}))30	};31}32macro_rules! expr_un {33	($op:ident $a:ident) => {34		Expr::UnaryOp($op, Box::new($a))35	};36}3738parser! {39	grammar jsonnet_parser() for str {40		use peg::ParseLiteral;4142		rule eof() = quiet!{![_]} / expected!("<eof>")43		rule eol() = "\n" / eof()4445		/// Standard C-like comments46		rule comment()47			= "//" (!eol()[_])* eol()48			/ "/*" (!("*/")[_])* "*/"49			/ "#" (!eol()[_])* eol()5051		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")52		rule _() = quiet!{([' ' | '\r' | '\n' | '\t']+) / comment()}* / expected!("<whitespace>")5354		/// For comma-delimited elements55		rule comma() = quiet!{_ "," _} / expected!("<comma>")56		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}57		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}58		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']59		/// Sequence of digits60		rule uint_str() -> &'input str = a:$(digit()+ ("_" digit()+)*) { a }61		/// Number in scientific notation format62		rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.replace("_","").parse().map_err(|_| "<number>") }} / expected!("<number>")6364		/// Reserved word followed by any non-alphanumberic65		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()66		rule id() -> IStr = v:$(quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")) { v.into() }6768		rule keyword(id: &'static str) -> ()69			= ##parse_string_literal(id) end_of_ident()7071		pub rule param(s: &ParserSettings) -> expr::Param = name:destruct(s) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr.map(Rc::new)) }72		pub rule params(s: &ParserSettings) -> expr::ParamsDesc73			= params:param(s) ** comma() comma()? { expr::ParamsDesc(Rc::new(params)) }74			/ { expr::ParamsDesc(Rc::new(Vec::new())) }7576		pub rule arg(s: &ParserSettings) -> (Option<IStr>, Rc<Spanned<Expr>>)77			= name:(quiet! { (s:id() _ "=" !['='] _ {s})? } / expected!("<argument name>")) expr:expr(s) {(name, Rc::new(expr))}7879		pub rule args(s: &ParserSettings) -> expr::ArgsDesc80			= args:arg(s)**comma() comma()? {?81				let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();82				let mut unnamed = Vec::with_capacity(unnamed_count);83				let mut named = Vec::with_capacity(args.len() - unnamed_count);84				let mut named_started = false;85				for (name, value) in args {86					if let Some(name) = name {87						named_started = true;88						named.push((name, value));89					} else {90						if named_started {91							return Err("<named argument>")92						}93						unnamed.push(value);94					}95				}96				Ok(expr::ArgsDesc::new(unnamed, named))97			}9899		pub rule destruct_rest() -> expr::DestructRest100			= "..." into:(_ into:id() {into})? {if let Some(into) = into {101				expr::DestructRest::Keep(into)102			} else {expr::DestructRest::Drop}}103		pub rule destruct_array(s: &ParserSettings) -> expr::Destruct104			= "[" _ start:destruct(s)**comma() rest:(105				comma() _ rest:destruct_rest()? end:(106					comma() end:destruct(s)**comma() (_ comma())? {end}107					/ comma()? {Vec::new()}108				) {(rest, end)}109				/ comma()? {(None, Vec::new())}110			) _ "]" {?111				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Array {112					start,113					rest: rest.0,114					end: rest.1,115				});116				#[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")117			}118		pub rule destruct_object(s: &ParserSettings) -> expr::Destruct119			= "{" _120				fields:(name:id() into:(_ ":" _ into:destruct(s) {into})? default:(_ "=" _ v:expr(s) {v})? {(name, into, default)})**comma()121				rest:(122					comma() rest:destruct_rest()? {rest}123					/ comma()? {None}124				)125			_ "}" {?126				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Object {127					fields,128					rest,129				});130				#[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")131			}132		pub rule destruct(s: &ParserSettings) -> expr::Destruct133			= v:id() {expr::Destruct::Full(v)}134			/ "?" {?135				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Skip);136				#[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")137			}138			/ arr:destruct_array(s) {arr}139			/ obj:destruct_object(s) {obj}140141		pub rule bind(s: &ParserSettings) -> expr::BindSpec142			= into:destruct(s) _ "=" _ value:expr(s) {expr::BindSpec::Field{into, value: Rc::new(value)}}143			/ name:id() _ "(" _ params:params(s) _ ")" _ "=" _ value:expr(s) {expr::BindSpec::Function{name, params, value: Rc::new(value)}}144145		pub rule assertion(s: &ParserSettings) -> expr::AssertStmt146			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }147148		pub rule whole_line() -> &'input str149			= str:$((!['\n'][_])* "\n") {str}150		pub rule string_block() -> String151			= "|||" chomped:"-"? (!['\n']single_whitespace())* "\n"152			empty_lines:$(['\n']*)153			prefix:[' ' | '\t']+ first_line:whole_line()154			lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*155			[' ' | '\t']*<, {prefix.len() - 1}> "|||"156			{157				let mut l = empty_lines.to_owned();158				l.push_str(first_line);159				l.extend(lines);160				if chomped.is_some() {161					debug_assert!(l.ends_with('\n'));162					l.truncate(l.len() - 1);163				}164				l165			}166167		rule hex_char()168			= quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")169170		rule string_char(c: rule<()>)171			= (!['\\']!c()[_])+172			/ "\\\\"173			/ "\\u" hex_char() hex_char() hex_char() hex_char()174			/ "\\x" hex_char() hex_char()175			/ ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't' | '"' | '\''] } / expected!("<escape character>"))176		pub rule string() -> String177			= ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}178			/ ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}179			/ quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}180			/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}181			/ string_block() } / expected!("<string>")182183		pub rule field_name(s: &ParserSettings) -> expr::FieldName184			= name:id() {expr::FieldName::Fixed(name)}185			/ name:string() {expr::FieldName::Fixed(name.into())}186			/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}187		pub rule visibility() -> expr::Visibility188			= ":::" {expr::Visibility::Unhide}189			/ "::" {expr::Visibility::Hidden}190			/ ":" {expr::Visibility::Normal}191		pub rule field(s: &ParserSettings) -> expr::FieldMember192			= name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{193				name,194				plus: plus.is_some(),195				params: None,196				visibility,197				value: Rc::new(value),198			}}199			/ name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{200				name,201				plus: false,202				params: Some(params),203				visibility,204				value: Rc::new(value),205			}}206		pub rule obj_local(s: &ParserSettings) -> BindSpec207			= keyword("local") _ bind:bind(s) {bind}208		pub rule member(s: &ParserSettings) -> expr::Member209			= bind:obj_local(s) {expr::Member::BindStmt(bind)}210			/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}211			/ field:field(s) {expr::Member::Field(field)}212		pub rule objinside(s: &ParserSettings) -> expr::ObjBody213			= pre_locals:(b: obj_local(s) comma() {b})* &"[" field:field(s) post_locals:(comma() b:obj_local(s) {b})* _ ("," _)? forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {214				let mut compspecs = vec![CompSpec::ForSpec(forspec)];215				compspecs.extend(others.unwrap_or_default());216				let mut locals = pre_locals;217				locals.extend(post_locals);218				expr::ObjBody::ObjComp(expr::ObjComp{219					locals: Rc::new(locals),220					field: Rc::new(field),221					compspecs,222				})223			}224			/ members:(member(s) ** comma()) comma()? {225				let mut locals = Vec::new();226				let mut asserts = Vec::new();227				let mut fields = Vec::new();228				for member in members {229					match member {230						Member::Field(field_member) => fields.push(field_member),231						Member::BindStmt(bind_spec) => locals.push(bind_spec),232						Member::AssertStmt(assert_stmt) => asserts.push(assert_stmt),233					}234				}235				expr::ObjBody::MemberList(ObjMembers {236					locals: Rc::new(locals), asserts: Rc::new(asserts), fields237				})238			}239		pub rule ifspec(s: &ParserSettings) -> IfSpecData240			= keyword("if") _ expr:expr(s) {IfSpecData(expr)}241		pub rule forspec(s: &ParserSettings) -> ForSpecData242			= keyword("for") _ id:destruct(s) _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}243		pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>244			= s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}245		pub rule local_expr(s: &ParserSettings) -> Expr246			= keyword("local") _ binds:bind(s) ** comma() (_ ",")? _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, Box::new(expr)) }247		pub rule string_expr(s: &ParserSettings) -> Expr248			= s:string() {Expr::Str(s.into())}249		pub rule obj_expr(s: &ParserSettings) -> Expr250			= "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}251		pub rule array_expr(s: &ParserSettings) -> Expr252			= "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(Rc::new(elems))}253		pub rule array_comp_expr(s: &ParserSettings) -> Expr254			= "[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {255				let mut specs = vec![CompSpec::ForSpec(forspec)];256				specs.extend(others.unwrap_or_default());257				Expr::ArrComp(Rc::new(expr), specs)258			}259		pub rule number_expr(s: &ParserSettings) -> Expr260			= n:number() {? if n.is_finite() {261				Ok(expr::Expr::Num(n))262			} else {263				Err("!!!numbers are finite")264			}}265		pub rule var_expr(s: &ParserSettings) -> Expr266			= n:id() { expr::Expr::Var(n) }267		pub rule id_loc(s: &ParserSettings) -> Spanned<Expr>268			= a:position!() n:id() b:position!() { Spanned::new(expr::Expr::Str(n), Span(s.source.clone(), a as u32,b as u32)) }269		pub rule if_then_else_expr(s: &ParserSettings) -> Expr270			= cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse(Box::new(IfElse{271				cond,272				cond_then,273				cond_else,274			}))}275276		pub rule literal(s: &ParserSettings) -> Expr277			= v:(278				keyword("null") {LiteralType::Null}279				/ keyword("true") {LiteralType::True}280				/ keyword("false") {LiteralType::False}281				/ keyword("self") {LiteralType::This}282				/ keyword("$") {LiteralType::Dollar}283				/ keyword("super") {LiteralType::Super}284			) {Expr::Literal(v)}285286		rule import_kind() -> ImportKind287			= keyword("importstr") { ImportKind::Str }288			/ keyword("importbin") { ImportKind::Bin }289			/ keyword("import") { ImportKind::Normal }290291		pub rule expr_basic(s: &ParserSettings) -> Expr292			= literal(s)293294			/ string_expr(s) / number_expr(s)295			/ array_expr(s)296			/ obj_expr(s)297			/ array_expr(s)298			/ array_comp_expr(s)299300			/ kind:import_kind() _ path:expr(s) {Expr::Import(kind, Box::new(path))}301302			/ var_expr(s)303			/ local_expr(s)304			/ if_then_else_expr(s)305306			/ keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, Rc::new(expr))}307			/ assert:assertion(s) _ ";" _ rest:expr(s) { Expr::AssertExpr(Rc::new(AssertExpr{308				assert, rest309			})) }310311			/ keyword("error") _ expr:expr(s) { Expr::ErrorStmt(Box::new(expr)) }312313		rule slice_part(s: &ParserSettings) -> Option<Spanned<Expr>>314			= _ e:(e:expr(s) _{e})? {e}315		pub rule slice_desc(s: &ParserSettings) -> SliceDesc316			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {317				let (end, step) = if let Some((end, step)) = pair {318					(end, step)319				}else{320					(None, None)321				};322323				SliceDesc { start, end, step }324			}325326		rule binop(x: rule<()>) -> ()327			= quiet!{ x() } / expected!("<binary op>")328		rule unaryop(x: rule<()>) -> ()329			= quiet!{ x() } / expected!("<unary op>")330331		rule ensure_null_coaelse()332			= "" {?333				#[cfg(not(feature = "exp-null-coaelse"))] return Err("!!!experimental null coaelscing was not enabled");334				#[cfg(feature = "exp-null-coaelse")] Ok(())335			}336		use BinaryOpType::*;337		use UnaryOpType::*;338		rule expr(s: &ParserSettings) -> Spanned<Expr>339			= precedence! {340				"(" _ e:expr(s) _ ")" {e}341				start:position!() v:@ end:position!() { Spanned::new(v, Span(s.source.clone(), start as u32, end as u32)) }342				--343				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}344				a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {345					#[cfg(feature = "exp-null-coaelse")] return expr_bin!(a NullCoaelse b);346					unreachable!("ensure_null_coaelse will fail if feature is not enabled")347				}348				--349				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}350				--351				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}352				--353				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}354				--355				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}356				--357				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}358				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}359				--360				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}361				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}362				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}363				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}364				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}365				--366				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}367				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}368				--369				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}370				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}371				--372				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}373				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}374				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}375				--376						unaryop(<"+">) _ b:@ {expr_un!(Plus b)}377						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}378						unaryop(<"!">) _ b:@ {expr_un!(Not b)}379						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}380				--381				value:(@) _ "[" _ slice:slice_desc(s) _ "]" {Expr::Slice(Box::new(Slice{value, slice}))}382				indexable:(@) _ parts:index_part(s)+ {Expr::Index{indexable: Box::new(indexable), parts}}383				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(Box::new(a), args, ts.is_some())}384				a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(Rc::new(a), body)}385				--386				e:expr_basic(s) {e}387			}388		pub rule index_part(s: &ParserSettings) -> IndexPart389		= n:("?" _ ensure_null_coaelse())? "." _ value:id_loc(s) {IndexPart {390			value,391			#[cfg(feature = "exp-null-coaelse")]392			null_coaelse: n.is_some(),393		}}394		/ n:("?" _ "." _ ensure_null_coaelse())? "[" _ value:expr(s) _ "]" {IndexPart {395			value,396			#[cfg(feature = "exp-null-coaelse")]397			null_coaelse: n.is_some(),398		}}399400		pub rule jsonnet(s: &ParserSettings) -> Spanned<Expr> = _ e:expr(s) _ {e}401	}402}403404pub type ParseError = peg::error::ParseError<peg::str::LineCol>;405pub fn parse(str: &str, settings: &ParserSettings) -> Result<Spanned<Expr>, ParseError> {406	jsonnet_parser::jsonnet(str, settings)407}408/// Used for importstr values409pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> Spanned<Expr> {410	let len = str.len();411	Spanned::new(Expr::Str(str), Span(settings.source.clone(), 0, len as u32))412}413414#[cfg(test)]415pub mod tests {416	use insta::assert_snapshot;417	use jrsonnet_interner::IStr;418419	use super::parse;420	use crate::{source::Source, ParserSettings};421422	fn parsep(s: &str) -> String {423		let v = parse(424			s,425			&ParserSettings {426				source: Source::new_virtual("<test>".into(), IStr::empty()),427			},428		)429		.unwrap();430		format!("{v:#?}")431	}432433	macro_rules! parse {434		($s:expr) => {435			assert_snapshot!(parsep($s));436		};437	}438439	#[test]440	fn multiline_string() {441		parse!("|||\n    Hello world!\n     a\n|||");442		parse!("|||\n  Hello world!\n   a\n|||");443		parse!("|||\n\t\tHello world!\n\t\t\ta\n|||");444		parse!("|||\n   Hello world!\n    a\n |||");445	}446447	#[test]448	fn slice() {449		parse!("a[1:]");450		parse!("a[1::]");451		parse!("a[:1:]");452		parse!("a[::1]");453		parse!("str[:len - 1]");454	}455456	#[test]457	fn string_escaping() {458		parse!(r#""Hello, \"world\"!""#);459		parse!(r#"'Hello \'world\'!'"#);460		parse!(r#"'\\\\'"#);461	}462463	#[test]464	fn string_unescaping() {465		parse!(r#""Hello\nWorld""#);466	}467468	#[test]469	fn string_verbantim() {470		parse!(r#"@"Hello\n""World""""#);471	}472473	#[test]474	fn imports() {475		parse!("import \"hello\"");476		parse!("importstr \"garnish.txt\"");477		parse!("importbin \"garnish.bin\"");478	}479480	#[test]481	fn empty_object() {482		parse!("{}");483	}484485	#[test]486	fn basic_math() {487		parse!("2+2*2");488		parse!("2	+ 	  2	  *	2   	");489		parse!("2+(2+2*2)");490		parse!("2//comment\n+//comment\n3/*test*/*/*test*/4");491	}492493	#[test]494	fn suffix() {495		parse!("std.test");496		parse!("std(2)");497		parse!("std.test(2)");498		parse!("a[b]");499	}500501	#[test]502	fn array_comp() {503		parse!("[std.deepJoin(x) for x in arr]");504	}505506	#[test]507	fn reserved() {508		parse!("null");509		parse!("nulla");510	}511512	#[test]513	fn multiple_args_buf() {514		parse!("a(b, null_fields)");515	}516517	#[test]518	fn infix_precedence() {519		parse!("!a && !b");520		parse!("!a / !b");521	}522523	#[test]524	fn double_negation() {525		parse!("!!a");526	}527528	#[test]529	fn array_test_error() {530		parse!("[a for a in b if c for e in f]");531	}532533	#[test]534	fn missing_newline_between_comment_and_eof() {535		parse!(536			"{a:1}537538			//+213"539		);540	}541542	#[test]543	fn default_param_before_nondefault() {544		parse!("local x(foo = 'foo', bar) = null; null");545	}546547	#[test]548	fn add_location_info_to_all_sub_expressions() {549		parse!("{} { local x = 1, x: x } + {}");550	}551}
modifiedcrates/jrsonnet-parser/src/snapshots/jrsonnet_parser__tests__add_location_info_to_all_sub_expressions.snapdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/snapshots/jrsonnet_parser__tests__add_location_info_to_all_sub_expressions.snap
+++ b/crates/jrsonnet-parser/src/snapshots/jrsonnet_parser__tests__add_location_info_to_all_sub_expressions.snap
@@ -7,12 +7,16 @@
         lhs: ObjExtend(
             Obj(
                 MemberList(
-                    [],
+                    ObjMembers {
+                        locals: [],
+                        asserts: [],
+                        fields: [],
+                    },
                 ),
             ) from virtual:<test>:0-2,
             MemberList(
-                [
-                    BindStmt(
+                ObjMembers {
+                    locals: [
                         Field {
                             into: Full(
                                 "x",
@@ -21,8 +25,9 @@
                                 1.0,
                             ) from virtual:<test>:15-16,
                         },
-                    ),
-                    Field(
+                    ],
+                    asserts: [],
+                    fields: [
                         FieldMember {
                             name: Fixed(
                                 "x",
@@ -34,14 +39,18 @@
                                 "x",
                             ) from virtual:<test>:21-22,
                         },
-                    ),
-                ],
+                    ],
+                },
             ),
         ) from virtual:<test>:0-24,
         op: Add,
         rhs: Obj(
             MemberList(
-                [],
+                ObjMembers {
+                    locals: [],
+                    asserts: [],
+                    fields: [],
+                },
             ),
         ) from virtual:<test>:27-29,
     },
modifiedcrates/jrsonnet-parser/src/snapshots/jrsonnet_parser__tests__empty_object.snapdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/snapshots/jrsonnet_parser__tests__empty_object.snap
+++ b/crates/jrsonnet-parser/src/snapshots/jrsonnet_parser__tests__empty_object.snap
@@ -4,6 +4,10 @@
 ---
 Obj(
     MemberList(
-        [],
+        ObjMembers {
+            locals: [],
+            asserts: [],
+            fields: [],
+        },
     ),
 ) from virtual:<test>:0-2
modifiedcrates/jrsonnet-parser/src/snapshots/jrsonnet_parser__tests__missing_newline_between_comment_and_eof.snapdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/snapshots/jrsonnet_parser__tests__missing_newline_between_comment_and_eof.snap
+++ b/crates/jrsonnet-parser/src/snapshots/jrsonnet_parser__tests__missing_newline_between_comment_and_eof.snap
@@ -4,8 +4,10 @@
 ---
 Obj(
     MemberList(
-        [
-            Field(
+        ObjMembers {
+            locals: [],
+            asserts: [],
+            fields: [
                 FieldMember {
                     name: Fixed(
                         "a",
@@ -17,7 +19,7 @@
                         1.0,
                     ) from virtual:<test>:3-4,
                 },
-            ),
-        ],
+            ],
+        },
     ),
 ) from virtual:<test>:0-5