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

difftreelog

style fix clippy warnings

ylxknpqrYaroslav Bolyukin2026-04-25parent: #7af406e.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-peg-parser/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-peg-parser/src/lib.rs
1use std::rc::Rc;23use jrsonnet_gcmodule::Acyclic;4use jrsonnet_ir::{5	ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BindSpec, CompSpec, Destruct, DestructRest, Expr,6	ExprParam, ExprParams, FieldMember, FieldName, ForSpecData, IStr, IfElse, IfSpecData,7	ImportKind, IndexPart, LiteralType, Member, NumValue, ObjBody, ObjComp, ObjMembers, Slice,8	SliceDesc, Source, Span, Spanned, Visibility, unescape,9};10use peg::parser;1112pub struct ParserSettings {13	pub source: Source,14}1516macro_rules! expr_bin {17	($a:ident $op:ident $b:ident) => {18		Expr::BinaryOp(Box::new(BinaryOp {19			lhs: $a,20			op: $op,21			rhs: $b,22		}))23	};24}25macro_rules! expr_un {26	($op:ident $a:ident) => {27		Expr::UnaryOp($op, Box::new($a))28	};29}3031parser! {32	grammar jsonnet_parser() for str {33		use peg::ParseLiteral;3435		rule eof() = quiet!{![_]} / expected!("<eof>")36		rule eol() = "\n" / eof()3738		/// Standard C-like comments39		rule comment()40			= "//" (!eol()[_])* eol()41			/ "/*" (!("*/")[_])* "*/"42			/ "#" (!eol()[_])* eol()4344		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")45		rule _() = quiet!{([' ' | '\r' | '\n' | '\t']+) / comment()}* / expected!("<whitespace>")4647		/// For comma-delimited elements48		rule comma() = quiet!{_ "," _} / expected!("<comma>")49		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}50		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}51		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']52		/// Sequence of digits53		rule uint_str() -> &'input str = a:$(digit()+ ("_" digit()+)*) { a }54		/// Number in scientific notation format55		rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.replace('_',"").parse().map_err(|_| "<number>") }} / expected!("<number>")5657		/// Reserved word followed by any non-alphanumberic58		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()59		rule id() -> IStr = v:$(quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")) { v.into() }6061		rule keyword(id: &'static str) -> ()62			= #parse_string_literal(id) end_of_ident()6364		pub rule param(s: &ParserSettings) -> ExprParam = destruct:destruct(s) expr:(_ "=" _ expr:expr(s){expr})? { ExprParam { destruct, default: expr.map(Rc::new) } }65		pub rule params(s: &ParserSettings) -> ExprParams66			= params:param(s) ** comma() comma()? { ExprParams::new(params) }67			/ { ExprParams::new(Vec::new()) }6869		pub rule arg(s: &ParserSettings) -> (Option<IStr>, Rc<Expr>)70			= name:(quiet! { (s:id() _ "=" !['='] _ {s})? } / expected!("<argument name>")) expr:expr(s) {(name, Rc::new(expr))}7172		pub rule args(s: &ParserSettings) -> ArgsDesc73			= args:arg(s)**comma() comma()? {?74				let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();75				let mut unnamed = Vec::with_capacity(unnamed_count);76				let mut names = Vec::with_capacity(args.len() - unnamed_count);77				let mut values = Vec::with_capacity(args.len() - unnamed_count);78				let mut named_started = false;79				for (name, value) in args {80					if let Some(name) = name {81						named_started = true;82						names.push(name);83						values.push(value);84					} else {85						if named_started {86							return Err("<named argument>")87						}88						unnamed.push(value);89					}90				}91				Ok(ArgsDesc{unnamed, names, values})92			}9394		pub rule destruct_rest() -> DestructRest95			= "..." into:(_ into:id() {into})? {if let Some(into) = into {96				DestructRest::Keep(into)97			} else {DestructRest::Drop}}98		pub rule destruct_array(s: &ParserSettings) -> Destruct99			= "[" _ start:destruct(s)**comma() rest:(100				comma() _ rest:destruct_rest()? end:(101					comma() end:destruct(s)**comma() (_ comma())? {end}102					/ comma()? {Vec::new()}103				) {(rest, end)}104				/ comma()? {(None, Vec::new())}105			) _ "]" {?106				#[cfg(feature = "exp-destruct")] return Ok(Destruct::Array {107					start,108					rest: rest.0,109					end: rest.1,110				});111				#[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")112			}113		pub rule destruct_object(s: &ParserSettings) -> Destruct114			= "{" _115				fields:(name:id() into:(_ ":" _ into:destruct(s) {into})? default:(_ "=" _ v:spanned(<expr(s)>, s) {v})? {(name, into, default.map(Rc::new))})**comma()116				rest:(117					comma() rest:destruct_rest()? {rest}118					/ comma()? {None}119				)120			_ "}" {?121				#[cfg(feature = "exp-destruct")] return Ok(Destruct::Object {122					fields,123					rest,124				});125				#[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")126			}127		pub rule destruct(s: &ParserSettings) -> Destruct128			= v:id() {Destruct::Full(v)}129			/ "?" {?130				#[cfg(feature = "exp-destruct")] return Ok(Destruct::Skip);131				#[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")132			}133			/ arr:destruct_array(s) {arr}134			/ obj:destruct_object(s) {obj}135136		pub rule bind(s: &ParserSettings) -> BindSpec137			= into:destruct(s) _ "=" _ value:expr(s) {BindSpec::Field{into, value: Rc::new(value)}}138			/ name:id() _ "(" _ params:params(s) _ ")" _ "=" _ value:expr(s) {BindSpec::Function{name, params, value: Rc::new(value)}}139140		pub rule assertion(s: &ParserSettings) -> AssertStmt141			= keyword("assert") _ assertion:spanned(<expr(s)>, s) message:(_ ":" _ e:expr(s) {e})? { AssertStmt{assertion, message} }142143		pub rule whole_line() -> &'input str144			= str:$((!['\n'][_])* "\n") {str}145		pub rule string_block() -> String146			= "|||" chomped:"-"? (!['\n']single_whitespace())* "\n"147			empty_lines:$(['\n']*)148			prefix:[' ' | '\t']+ first_line:whole_line()149			lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*150			[' ' | '\t']*<, {prefix.len() - 1}> "|||"151			{152				let mut l = empty_lines.to_owned();153				l.push_str(first_line);154				l.extend(lines);155				if chomped.is_some() {156					debug_assert!(l.ends_with('\n'));157					l.truncate(l.len() - 1);158				}159				l160			}161162		rule hex_char()163			= quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")164165		rule string_char(c: rule<()>)166			= (!['\\']!c()[_])+167			/ "\\\\"168			/ "\\u" hex_char() hex_char() hex_char() hex_char()169			/ "\\x" hex_char() hex_char()170			/ ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't' | '"' | '\''] } / expected!("<escape character>"))171		pub rule string() -> String172			= ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}173			/ ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}174			/ quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}175			/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}176			/ string_block() } / expected!("<string>")177178		pub rule field_name(s: &ParserSettings) -> FieldName179			= name:id() {FieldName::Fixed(name)}180			/ name:string() {FieldName::Fixed(name.into())}181			/ "[" _ expr:expr(s) _ "]" {FieldName::Dyn(expr)}182		pub rule visibility() -> Visibility183			= ":::" {Visibility::Unhide}184			/ "::" {Visibility::Hidden}185			/ ":" {Visibility::Normal}186		pub rule field(s: &ParserSettings) -> FieldMember187			= name:spanned(<field_name(s)>, s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {FieldMember{188				name,189				plus: plus.is_some(),190				params: None,191				visibility,192				value: Rc::new(value),193			}}194			/ name:spanned(<field_name(s)>, s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {FieldMember{195				name,196				plus: false,197				params: Some(params),198				visibility,199				value: Rc::new(value),200			}}201		pub rule obj_local(s: &ParserSettings) -> BindSpec202			= keyword("local") _ bind:bind(s) {bind}203		pub rule member(s: &ParserSettings) -> Member204			= bind:obj_local(s) {Member::BindStmt(bind)}205			/ assertion:assertion(s) {Member::AssertStmt(assertion)}206			/ field:field(s) {Member::Field(field)}207		pub rule objinside(s: &ParserSettings) -> ObjBody208			=  members:(member(s) ** comma()) comma()? _ compspecs:compspecs(s)? {?209				Ok(if let Some(compspecs) = compspecs {210					let mut locals = Vec::new();211					let mut field = None;212					for member in members {213						match member {214							Member::Field(field_member) => if field.replace(field_member).is_some() {215								return Err("<object comprehension can only contain one field>")216							},217							Member::BindStmt(bind_spec) => locals.push(bind_spec),218							Member::AssertStmt(assert_stmt) => return Err("<asserts are unsupported in object comprehension>"),219						}220					}221					ObjBody::ObjComp(ObjComp {222						locals: Rc::new(locals),223						field: field.map(Rc::new).ok_or("<missing object comprehension field>")?,224						compspecs225					})226				} else {227					let mut locals = Vec::new();228					let mut asserts = Vec::new();229					let mut fields = Vec::new();230					for member in members {231						match member {232							Member::Field(field_member) => fields.push(field_member),233							Member::BindStmt(bind_spec) => locals.push(bind_spec),234							Member::AssertStmt(assert_stmt) => asserts.push(assert_stmt),235						}236					}237					ObjBody::MemberList(ObjMembers {238						locals: Rc::new(locals),239						asserts: Rc::new(asserts),240						fields241					})242				})243			}244		pub rule ifspec(s: &ParserSettings) -> IfSpecData245			= i:spanned(<keyword("if")>, s) _ cond:expr(s) {IfSpecData { span: i.span, cond }}246		pub rule forspec(s: &ParserSettings) -> ForSpecData247			= keyword("for") _ destruct:destruct(s) _ keyword("in") _ over:expr(s) { ForSpecData { destruct, over } }248		rule compspec(s: &ParserSettings) -> CompSpec249			= i:ifspec(s) { CompSpec::IfSpec(i) } / f:forspec(s) {CompSpec::ForSpec(f)}250		pub rule compspecs(s: &ParserSettings) -> Vec<CompSpec>251			= specs:compspec(s) ++ _ {?252				if !matches!(specs[0], CompSpec::ForSpec(_)) {253					return Err("<first compspec should be for>")254				}255				Ok(specs)256			}257		pub rule local_expr(s: &ParserSettings) -> Expr258			= keyword("local") _ binds:bind(s) ** comma() (_ ",")? _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, Box::new(expr)) }259		pub rule string_expr(s: &ParserSettings) -> Expr260			= s:string() {Expr::Str(s.into())}261		pub rule obj_expr(s: &ParserSettings) -> Expr262			= "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}263		pub rule array_expr(s: &ParserSettings) -> Expr264			= "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(Rc::new(elems))}265		pub rule array_comp_expr(s: &ParserSettings) -> Expr266			= "[" _ expr:expr(s) _ comma()? _ specs:(r: compspecs(s) _ {r}) "]" {267				Expr::ArrComp(Rc::new(expr), specs)268			}269		pub rule number_expr(s: &ParserSettings) -> Expr270			= n:number() {? if let Some(n) = NumValue::new(n) {271				Ok(Expr::Num(n))272			} else {273				Err("!!!numbers are finite")274			}}275276		rule spanned<T: Acyclic>(x: rule<T>, s: &ParserSettings) -> Spanned<T>277			= a:position!() n:x() b:position!() { Spanned::new(n, Span(s.source.clone(), a as u32, b as u32)) }278279		pub rule var_expr(s: &ParserSettings) -> Expr280			= n:spanned(<id()>, s) { Expr::Var(n) }281		pub rule id_loc(s: &ParserSettings) -> Spanned<Expr>282			= a:position!() n:id() b:position!() { Spanned::new(Expr::Str(n), Span(s.source.clone(), a as u32,b as u32)) }283		pub rule if_then_else_expr(s: &ParserSettings) -> Expr284			= cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse(Box::new(IfElse{285				cond,286				cond_then,287				cond_else,288			}))}289290		pub rule literal(s: &ParserSettings) -> Expr291			= v:(292				keyword("null") {LiteralType::Null}293				/ keyword("true") {LiteralType::True}294				/ keyword("false") {LiteralType::False}295				/ keyword("self") {LiteralType::This}296				/ keyword("$") {LiteralType::Dollar}297				/ keyword("super") {LiteralType::Super}298			) {Expr::Literal(v)}299300		rule import_kind() -> ImportKind301			= keyword("importstr") { ImportKind::Str }302			/ keyword("importbin") { ImportKind::Bin }303			/ keyword("import") { ImportKind::Normal }304305		pub rule expr_basic(s: &ParserSettings) -> Expr306			= literal(s)307308			/ string_expr(s) / number_expr(s)309			/ array_expr(s)310			/ obj_expr(s)311			/ array_expr(s)312			/ array_comp_expr(s)313314			/ kind:spanned(<import_kind()>, s) _ path:expr(s) {Expr::Import(kind, Box::new(path))}315316			/ var_expr(s)317			/ local_expr(s)318			/ if_then_else_expr(s)319320			/ keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, Rc::new(expr))}321			/ assert:assertion(s) _ ";" _ rest:expr(s) { Expr::AssertExpr(Rc::new(AssertExpr{322				assert, rest323			})) }324325			/ err_kw:spanned(<keyword("error")>, s) _ expr:expr(s) { Expr::ErrorStmt(err_kw.span, Box::new(expr)) }326327		rule slice_part(s: &ParserSettings) -> Option<Spanned<Expr>>328			= _ e:(e:spanned(<expr(s)>, s) _{e})? {e}329		pub rule slice_desc(s: &ParserSettings) -> SliceDesc330			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {331				let (end, step) = if let Some((end, step)) = pair {332					(end, step)333				}else{334					(None, None)335				};336337				SliceDesc { start, end, step }338			}339340		rule binop(x: rule<()>) -> ()341			= quiet!{ x() } / expected!("<binary op>")342		rule unaryop(x: rule<()>) -> ()343			= quiet!{ x() } / expected!("<unary op>")344345		rule ensure_null_coaelse()346			= "" {?347				#[cfg(not(feature = "exp-null-coaelse"))] return Err("!!!experimental null coaelscing was not enabled");348				#[cfg(feature = "exp-null-coaelse")] Ok(())349			}350		use jrsonnet_ir::BinaryOpType::*;351		use jrsonnet_ir::UnaryOpType::*;352		rule expr(s: &ParserSettings) -> Expr353			= precedence! {354				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}355				a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {356					#[cfg(feature = "exp-null-coaelse")] return expr_bin!(a NullCoaelse b);357					unreachable!("ensure_null_coaelse will fail if feature is not enabled")358				}359				--360				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}361				--362				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}363				--364				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}365				--366				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}367				--368				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}369				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}370				--371				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}372				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}373				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}374				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}375				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}376				--377				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}378				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}379				--380				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}381				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}382				--383				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}384				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}385				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}386				--387						unaryop(<"+">) _ b:@ {expr_un!(Plus b)}388						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}389						unaryop(<"!">) _ b:@ {expr_un!(Not b)}390						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}391				--392				value:(@) _ "[" _ slice:slice_desc(s) _ "]" {Expr::Slice(Box::new(Slice{value, slice}))}393				indexable:(@) _ parts:index_part(s)+ {Expr::Index{indexable: Box::new(indexable), parts}}394				a:(@) _ args:spanned(<"(" _ a:args(s) _ ")" {a}>, s) ts:(_ keyword("tailstrict"))? {Expr::Apply(Box::new(a), args, ts.is_some())}395				a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(Rc::new(a), body)}396				--397				e:expr_basic(s) {e}398				"(" _ e:expr(s) _ ")" {e}399			}400		pub rule index_part(s: &ParserSettings) -> IndexPart401		= n:("?" _ ensure_null_coaelse())? "." _ value:id_loc(s) {IndexPart {402			span: value.span,403			value: value.value,404			#[cfg(feature = "exp-null-coaelse")]405			null_coaelse: n.is_some(),406		}}407		/ n:("?" _ "." _ ensure_null_coaelse())? value:spanned(<"[" _ v:expr(s) _ "]" {v}>, s) {IndexPart {408			span: value.span,409			value: value.value,410			#[cfg(feature = "exp-null-coaelse")]411			null_coaelse: n.is_some(),412		}}413414		pub rule jsonnet(s: &ParserSettings) -> Expr = _ e:expr(s) _ {e}415	}416}417418pub type ParseError = peg::error::ParseError<peg::str::LineCol>;419pub fn parse(str: &str, settings: &ParserSettings) -> Result<Expr, ParseError> {420	jsonnet_parser::jsonnet(str, settings)421}422/// Used for importstr values423pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> Spanned<Expr> {424	let len = str.len();425	Spanned::new(Expr::Str(str), Span(settings.source.clone(), 0, len as u32))426}427428#[cfg(test)]429mod tests {430	use std::fs;431432	use insta::{assert_snapshot, glob};433	use jrsonnet_ir::{IStr, Source};434435	use crate::{ParserSettings, parse};436437	#[test]438	fn snapshots() {439		glob!("tests/*.jsonnet", |path| {440			let input = fs::read_to_string(path).expect("read test file");441			let v = parse(442				&input,443				&ParserSettings {444					source: Source::new_virtual("<test>".into(), IStr::empty()),445				},446			)447			.unwrap();448			let v = format!("{v:#?}");449			assert_snapshot!(v);450		});451	}452}
after · crates/jrsonnet-peg-parser/src/lib.rs
1use std::rc::Rc;23use jrsonnet_gcmodule::Acyclic;4use jrsonnet_ir::{5	ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BindSpec, CompSpec, Destruct, DestructRest, Expr,6	ExprParam, ExprParams, FieldMember, FieldName, ForSpecData, IStr, IfElse, IfSpecData,7	ImportKind, IndexPart, LiteralType, Member, NumValue, ObjBody, ObjComp, ObjMembers, Slice,8	SliceDesc, Source, Span, Spanned, Visibility, unescape,9};10use peg::parser;1112pub struct ParserSettings {13	pub source: Source,14}1516macro_rules! expr_bin {17	($a:ident $op:ident $b:ident) => {18		Expr::BinaryOp(Box::new(BinaryOp {19			lhs: $a,20			op: $op,21			rhs: $b,22		}))23	};24}25macro_rules! expr_un {26	($op:ident $a:ident) => {27		Expr::UnaryOp($op, Box::new($a))28	};29}3031parser! {32	pub grammar jsonnet_parser() for str {33		use peg::ParseLiteral;3435		rule eof() = quiet!{![_]} / expected!("<eof>")36		rule eol() = "\n" / eof()3738		/// Standard C-like comments39		rule comment()40			= "//" (!eol()[_])* eol()41			/ "/*" (!("*/")[_])* "*/"42			/ "#" (!eol()[_])* eol()4344		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")45		rule _() = quiet!{([' ' | '\r' | '\n' | '\t']+) / comment()}* / expected!("<whitespace>")4647		/// For comma-delimited elements48		rule comma() = quiet!{_ "," _} / expected!("<comma>")49		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}50		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}51		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']52		/// Sequence of digits53		rule uint_str() -> &'input str = a:$(digit()+ ("_" digit()+)*) { a }54		/// Number in scientific notation format55		rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.replace('_',"").parse().map_err(|_| "<number>") }} / expected!("<number>")5657		/// Reserved word followed by any non-alphanumberic58		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()59		rule id() -> IStr = v:$(quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")) { v.into() }6061		rule keyword(id: &'static str) -> ()62			= #{|input, pos| input.parse_string_literal(pos, id)} end_of_ident()6364		pub rule param(s: &ParserSettings) -> ExprParam = destruct:destruct(s) expr:(_ "=" _ expr:expr(s){expr})? { ExprParam { destruct, default: expr.map(Rc::new) } }65		pub rule params(s: &ParserSettings) -> ExprParams66			= params:param(s) ** comma() comma()? { ExprParams::new(params) }67			/ { ExprParams::new(Vec::new()) }6869		pub rule arg(s: &ParserSettings) -> (Option<IStr>, Rc<Expr>)70			= name:(quiet! { (s:id() _ "=" !['='] _ {s})? } / expected!("<argument name>")) expr:expr(s) {(name, Rc::new(expr))}7172		pub rule args(s: &ParserSettings) -> ArgsDesc73			= args:arg(s)**comma() comma()? {?74				let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();75				let mut unnamed = Vec::with_capacity(unnamed_count);76				let mut names = Vec::with_capacity(args.len() - unnamed_count);77				let mut values = Vec::with_capacity(args.len() - unnamed_count);78				let mut named_started = false;79				for (name, value) in args {80					if let Some(name) = name {81						named_started = true;82						names.push(name);83						values.push(value);84					} else {85						if named_started {86							return Err("<named argument>")87						}88						unnamed.push(value);89					}90				}91				Ok(ArgsDesc{unnamed, names, values})92			}9394		pub rule destruct_rest() -> DestructRest95			= "..." into:(_ into:id() {into})? {into.map_or_else(|| DestructRest::Drop, DestructRest::Keep)}96		pub rule destruct_array(s: &ParserSettings) -> Destruct97			= "[" _ start:destruct(s)**comma() rest:(98				comma() _ rest:destruct_rest()? end:(99					comma() end:destruct(s)**comma() (_ comma())? {end}100					/ comma()? {Vec::new()}101				) {(rest, end)}102				/ comma()? {(None, Vec::new())}103			) _ "]" {?104				#[cfg(feature = "exp-destruct")] return Ok(Destruct::Array {105					start,106					rest: rest.0,107					end: rest.1,108				});109				#[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")110			}111		pub rule destruct_object(s: &ParserSettings) -> Destruct112			= "{" _113				fields:(name:id() into:(_ ":" _ into:destruct(s) {into})? default:(_ "=" _ v:spanned(<expr(s)>, s) {v})? {(name, into, default.map(Rc::new))})**comma()114				rest:(115					comma() rest:destruct_rest()? {rest}116					/ comma()? {None}117				)118			_ "}" {?119				#[cfg(feature = "exp-destruct")] return Ok(Destruct::Object {120					fields,121					rest,122				});123				#[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")124			}125		pub rule destruct(s: &ParserSettings) -> Destruct126			= v:id() {Destruct::Full(v)}127			/ "?" {?128				#[cfg(feature = "exp-destruct")] return Ok(Destruct::Skip);129				#[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")130			}131			/ arr:destruct_array(s) {arr}132			/ obj:destruct_object(s) {obj}133134		pub rule bind(s: &ParserSettings) -> BindSpec135			= into:destruct(s) _ "=" _ value:expr(s) {BindSpec::Field{into, value: Rc::new(value)}}136			/ name:id() _ "(" _ params:params(s) _ ")" _ "=" _ value:expr(s) {BindSpec::Function{name, params, value: Rc::new(value)}}137138		pub rule assertion(s: &ParserSettings) -> AssertStmt139			= keyword("assert") _ assertion:spanned(<expr(s)>, s) message:(_ ":" _ e:expr(s) {e})? { AssertStmt{assertion, message} }140141		pub rule whole_line() -> &'input str142			= str:$((!['\n'][_])* "\n") {str}143		pub rule string_block() -> String144			= "|||" chomped:"-"? (!['\n']single_whitespace())* "\n"145			empty_lines:$(['\n']*)146			prefix:[' ' | '\t']+ first_line:whole_line()147			lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*148			[' ' | '\t']*<, {prefix.len() - 1}> "|||"149			{150				let mut l = empty_lines.to_owned();151				l.push_str(first_line);152				l.extend(lines);153				if chomped.is_some() {154					debug_assert!(l.ends_with('\n'));155					l.truncate(l.len() - 1);156				}157				l158			}159160		rule hex_char()161			= quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")162163		rule string_char(c: rule<()>)164			= (!['\\']!c()[_])+165			/ "\\\\"166			/ "\\u" hex_char() hex_char() hex_char() hex_char()167			/ "\\x" hex_char() hex_char()168			/ ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't' | '"' | '\''] } / expected!("<escape character>"))169		pub rule string() -> String170			= ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}171			/ ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}172			/ quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}173			/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}174			/ string_block() } / expected!("<string>")175176		pub rule field_name(s: &ParserSettings) -> FieldName177			= name:id() {FieldName::Fixed(name)}178			/ name:string() {FieldName::Fixed(name.into())}179			/ "[" _ expr:expr(s) _ "]" {FieldName::Dyn(expr)}180		pub rule visibility() -> Visibility181			= ":::" {Visibility::Unhide}182			/ "::" {Visibility::Hidden}183			/ ":" {Visibility::Normal}184		pub rule field(s: &ParserSettings) -> FieldMember185			= name:spanned(<field_name(s)>, s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {FieldMember{186				name,187				plus: plus.is_some(),188				params: None,189				visibility,190				value: Rc::new(value),191			}}192			/ name:spanned(<field_name(s)>, s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {FieldMember{193				name,194				plus: false,195				params: Some(params),196				visibility,197				value: Rc::new(value),198			}}199		pub rule obj_local(s: &ParserSettings) -> BindSpec200			= keyword("local") _ bind:bind(s) {bind}201		pub rule member(s: &ParserSettings) -> Member202			= bind:obj_local(s) {Member::BindStmt(bind)}203			/ assertion:assertion(s) {Member::AssertStmt(assertion)}204			/ field:field(s) {Member::Field(field)}205		pub rule objinside(s: &ParserSettings) -> ObjBody206			=  members:(member(s) ** comma()) comma()? _ compspecs:compspecs(s)? {?207				Ok(if let Some(compspecs) = compspecs {208					let mut locals = Vec::new();209					let mut field = None;210					for member in members {211						match member {212							Member::Field(field_member) => if field.replace(field_member).is_some() {213								return Err("<object comprehension can only contain one field>")214							},215							Member::BindStmt(bind_spec) => locals.push(bind_spec),216							Member::AssertStmt(assert_stmt) => return Err("<asserts are unsupported in object comprehension>"),217						}218					}219					ObjBody::ObjComp(ObjComp {220						locals: Rc::new(locals),221						field: field.map(Rc::new).ok_or("<missing object comprehension field>")?,222						compspecs223					})224				} else {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					ObjBody::MemberList(ObjMembers {236						locals: Rc::new(locals),237						asserts: Rc::new(asserts),238						fields239					})240				})241			}242		pub rule ifspec(s: &ParserSettings) -> IfSpecData243			= i:spanned(<keyword("if")>, s) _ cond:expr(s) {IfSpecData { span: i.span, cond }}244		pub rule forspec(s: &ParserSettings) -> ForSpecData245			= keyword("for") _ destruct:destruct(s) _ keyword("in") _ over:expr(s) { ForSpecData { destruct, over } }246		rule compspec(s: &ParserSettings) -> CompSpec247			= i:ifspec(s) { CompSpec::IfSpec(i) } / f:forspec(s) {CompSpec::ForSpec(f)}248		pub rule compspecs(s: &ParserSettings) -> Vec<CompSpec>249			= specs:compspec(s) ++ _ {?250				if !matches!(specs[0], CompSpec::ForSpec(_)) {251					return Err("<first compspec should be for>")252				}253				Ok(specs)254			}255		pub rule local_expr(s: &ParserSettings) -> Expr256			= keyword("local") _ binds:bind(s) ** comma() (_ ",")? _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, Box::new(expr)) }257		pub rule string_expr(s: &ParserSettings) -> Expr258			= s:string() {Expr::Str(s.into())}259		pub rule obj_expr(s: &ParserSettings) -> Expr260			= "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}261		pub rule array_expr(s: &ParserSettings) -> Expr262			= "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(Rc::new(elems))}263		pub rule array_comp_expr(s: &ParserSettings) -> Expr264			= "[" _ expr:expr(s) _ comma()? _ specs:(r: compspecs(s) _ {r}) "]" {265				Expr::ArrComp(Rc::new(expr), specs)266			}267		pub rule number_expr(s: &ParserSettings) -> Expr268			= n:number() {? if let Some(n) = NumValue::new(n) {269				Ok(Expr::Num(n))270			} else {271				Err("!!!numbers are finite")272			}}273274		rule spanned<T: Acyclic>(x: rule<T>, s: &ParserSettings) -> Spanned<T>275			= a:position!() n:x() b:position!() { Spanned::new(n, Span(s.source.clone(), a as u32, b as u32)) }276277		pub rule var_expr(s: &ParserSettings) -> Expr278			= n:spanned(<id()>, s) { Expr::Var(n) }279		pub rule id_loc(s: &ParserSettings) -> Spanned<Expr>280			= a:position!() n:id() b:position!() { Spanned::new(Expr::Str(n), Span(s.source.clone(), a as u32,b as u32)) }281		pub rule if_then_else_expr(s: &ParserSettings) -> Expr282			= cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse(Box::new(IfElse{283				cond,284				cond_then,285				cond_else,286			}))}287288		pub rule literal(s: &ParserSettings) -> Expr289			= v:(290				keyword("null") {LiteralType::Null}291				/ keyword("true") {LiteralType::True}292				/ keyword("false") {LiteralType::False}293				/ keyword("self") {LiteralType::This}294				/ keyword("$") {LiteralType::Dollar}295				/ keyword("super") {LiteralType::Super}296			) {Expr::Literal(v)}297298		rule import_kind() -> ImportKind299			= keyword("importstr") { ImportKind::Str }300			/ keyword("importbin") { ImportKind::Bin }301			/ keyword("import") { ImportKind::Normal }302303		pub rule expr_basic(s: &ParserSettings) -> Expr304			= literal(s)305306			/ string_expr(s) / number_expr(s)307			/ array_expr(s)308			/ obj_expr(s)309			/ array_expr(s)310			/ array_comp_expr(s)311312			/ kind:spanned(<import_kind()>, s) _ path:expr(s) {Expr::Import(kind, Box::new(path))}313314			/ var_expr(s)315			/ local_expr(s)316			/ if_then_else_expr(s)317318			/ keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, Rc::new(expr))}319			/ assert:assertion(s) _ ";" _ rest:expr(s) { Expr::AssertExpr(Rc::new(AssertExpr{320				assert, rest321			})) }322323			/ err_kw:spanned(<keyword("error")>, s) _ expr:expr(s) { Expr::ErrorStmt(err_kw.span, Box::new(expr)) }324325		rule slice_part(s: &ParserSettings) -> Option<Spanned<Expr>>326			= _ e:(e:spanned(<expr(s)>, s) _{e})? {e}327		pub rule slice_desc(s: &ParserSettings) -> SliceDesc328			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {329				let (end, step) = if let Some((end, step)) = pair {330					(end, step)331				}else{332					(None, None)333				};334335				SliceDesc { start, end, step }336			}337338		rule binop(x: rule<()>) -> ()339			= quiet!{ x() } / expected!("<binary op>")340		rule unaryop(x: rule<()>) -> ()341			= quiet!{ x() } / expected!("<unary op>")342343		rule ensure_null_coaelse()344			= "" {?345				#[cfg(not(feature = "exp-null-coaelse"))] return Err("!!!experimental null coaelscing was not enabled");346				#[cfg(feature = "exp-null-coaelse")] Ok(())347			}348		use jrsonnet_ir::BinaryOpType::*;349		use jrsonnet_ir::UnaryOpType::*;350		rule expr(s: &ParserSettings) -> Expr351			= precedence! {352				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}353				a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {354					#[cfg(feature = "exp-null-coaelse")] return expr_bin!(a NullCoaelse b);355					unreachable!("ensure_null_coaelse will fail if feature is not enabled")356				}357				--358				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}359				--360				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}361				--362				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}363				--364				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}365				--366				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}367				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}368				--369				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}370				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}371				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}372				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}373				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}374				--375				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}376				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}377				--378				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}379				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}380				--381				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}382				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}383				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}384				--385						unaryop(<"+">) _ b:@ {expr_un!(Plus b)}386						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}387						unaryop(<"!">) _ b:@ {expr_un!(Not b)}388						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}389				--390				value:(@) _ "[" _ slice:slice_desc(s) _ "]" {Expr::Slice(Box::new(Slice{value, slice}))}391				indexable:(@) _ parts:index_part(s)+ {Expr::Index{indexable: Box::new(indexable), parts}}392				a:(@) _ args:spanned(<"(" _ a:args(s) _ ")" {a}>, s) ts:(_ keyword("tailstrict"))? {Expr::Apply(Box::new(a), args, ts.is_some())}393				a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(Rc::new(a), body)}394				--395				e:expr_basic(s) {e}396				"(" _ e:expr(s) _ ")" {e}397			}398		pub rule index_part(s: &ParserSettings) -> IndexPart399		= n:("?" _ ensure_null_coaelse())? "." _ value:id_loc(s) {IndexPart {400			span: value.span,401			value: value.value,402			#[cfg(feature = "exp-null-coaelse")]403			null_coaelse: n.is_some(),404		}}405		/ n:("?" _ "." _ ensure_null_coaelse())? value:spanned(<"[" _ v:expr(s) _ "]" {v}>, s) {IndexPart {406			span: value.span,407			value: value.value,408			#[cfg(feature = "exp-null-coaelse")]409			null_coaelse: n.is_some(),410		}}411412		pub rule jsonnet(s: &ParserSettings) -> Expr = _ e:expr(s) _ {e}413	}414}415416pub type ParseError = peg::error::ParseError<peg::str::LineCol>;417pub fn parse(str: &str, settings: &ParserSettings) -> Result<Expr, ParseError> {418	jsonnet_parser::jsonnet(str, settings)419}420/// Used for importstr values421pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> Spanned<Expr> {422	let len = str.len();423	Spanned::new(Expr::Str(str), Span(settings.source.clone(), 0, len as u32))424}425426#[cfg(test)]427mod tests {428	use std::fs;429430	use insta::{assert_snapshot, glob};431	use jrsonnet_ir::{IStr, Source};432433	use crate::{ParserSettings, parse};434435	#[test]436	fn snapshots() {437		glob!("tests/*.jsonnet", |path| {438			let input = fs::read_to_string(path).expect("read test file");439			let v = parse(440				&input,441				&ParserSettings {442					source: Source::new_virtual("<test>".into(), IStr::empty()),443				},444			)445			.unwrap();446			let v = format!("{v:#?}");447			assert_snapshot!(v);448		});449	}450}