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

difftreelog

feat plus in object comprehensions

Yaroslav Bolyukin2021-08-20parent: #a6c5806.patch.diff
in: master
Fixes #60

3 files changed

modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -431,6 +431,7 @@
 						builder
 							.member(n)
 							.with_location(obj.value.1.clone())
+							.with_add(obj.plus)
 							.bindable(Box::new(ObjCompBinding {
 								context: ctx,
 								value: obj.value.clone(),
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -247,6 +247,7 @@
 pub struct ObjComp {
 	pub pre_locals: Vec<BindSpec>,
 	pub key: LocExpr,
+	pub plus: bool,
 	pub value: LocExpr,
 	pub post_locals: Vec<BindSpec>,
 	pub compspecs: Vec<CompSpec>,
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-parser/src/lib.rs
1#![allow(clippy::redundant_closure_call)]23use peg::parser;4use std::{5	path::{Path, PathBuf},6	rc::Rc,7};8mod expr;9pub use expr::*;10pub use peg;1112pub struct ParserSettings {13	pub loc_data: bool,14	pub file_name: Rc<Path>,15}1617macro_rules! expr_bin {18	($a:ident $op:ident $b:ident) => {19		loc_expr_todo!(Expr::BinaryOp($a, $op, $b))20	};21}22macro_rules! expr_un {23	($op:ident $a:ident) => {24		loc_expr_todo!(Expr::UnaryOp($op, $a))25	};26}2728parser! {29	grammar jsonnet_parser() for str {30		use peg::ParseLiteral;3132		/// Standard C-like comments33		rule comment()34			= "//" (!['\n'][_])* "\n"35			/ "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"36			/ "#" (!['\n'][_])* "\n"3738		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")39		rule _() = single_whitespace()*4041		/// For comma-delimited elements42		rule comma() = quiet!{_ "," _} / expected!("<comma>")43		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}44		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}45		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']46		/// Sequence of digits47		rule uint_str() -> &'input str = a:$(digit()+) { a }48		/// Number in scientific notation format49		rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")5051		/// Reserved word followed by any non-alphanumberic52		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()53		rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")5455		rule keyword(id: &'static str) -> ()56			= ##parse_string_literal(id) end_of_ident()57		// Adds location data information to existing expression58		rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr59			= start:position!() v:x() end:position!() {loc_expr!(v, s.loc_data, (s.file_name.clone(), start, end))}6061		pub rule param(s: &ParserSettings) -> expr::Param = name:$(id()) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name.into(), expr) }62		pub rule params(s: &ParserSettings) -> expr::ParamsDesc63			= params:param(s) ** comma() comma()? {64				let mut defaults_started = false;65				for param in &params {66					defaults_started = defaults_started || param.1.is_some();67					assert_eq!(defaults_started, param.1.is_some(), "defauld parameters should be used after all positionals");68				}69				expr::ParamsDesc(Rc::new(params))70			}71			/ { expr::ParamsDesc(Rc::new(Vec::new())) }7273		pub rule arg(s: &ParserSettings) -> expr::Arg74			= name:$(id()) _ "=" _ expr:expr(s) {expr::Arg(Some(name.into()), expr)}75			/ expr:expr(s) {expr::Arg(None, expr)}76		pub rule args(s: &ParserSettings) -> expr::ArgsDesc77			= args:arg(s) ** comma() comma()? {78				let mut named_started = false;79				for arg in &args {80					named_started = named_started || arg.0.is_some();81					assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");82				}83				expr::ArgsDesc(args)84			}85			/ { expr::ArgsDesc(Vec::new()) }8687		pub rule bind(s: &ParserSettings) -> expr::BindSpec88			= name:$(id()) _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: None, value: expr}}89			/ name:$(id()) _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: Some(params), value: expr}}90		pub rule assertion(s: &ParserSettings) -> expr::AssertStmt91			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }9293		pub rule whole_line() -> &'input str94			= str:$((!['\n'][_])* "\n") {str}95		pub rule string_block() -> String96			= "|||" (!['\n']single_whitespace())* "\n"97			  empty_lines:$(['\n']*)98			  prefix:[' ' | '\t']+ first_line:whole_line()99			  lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*100			  [' ' | '\t']*<, {prefix.len() - 1}> "|||"101			  {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}102		pub rule string() -> String103			= quiet!{ "\"" str:$(("\\\"" / "\\\\" / (!['"'][_]))*) "\"" {unescape::unescape(str).unwrap()}104			/ "'" str:$(("\\'" / "\\\\" / (!['\''][_]))*) "'" {unescape::unescape(str).unwrap()}105			/ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}106			/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}107			/ string_block() } / expected!("<string>")108109		pub rule field_name(s: &ParserSettings) -> expr::FieldName110			= name:$(id()) {expr::FieldName::Fixed(name.into())}111			/ name:string() {expr::FieldName::Fixed(name.into())}112			/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}113		pub rule visibility() -> expr::Visibility114			= ":::" {expr::Visibility::Unhide}115			/ "::" {expr::Visibility::Hidden}116			/ ":" {expr::Visibility::Normal}117		pub rule field(s: &ParserSettings) -> expr::FieldMember118			= name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{119				name,120				plus: plus.is_some(),121				params: None,122				visibility,123				value,124			}}125			/ name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{126				name,127				plus: false,128				params: Some(params),129				visibility,130				value,131			}}132		pub rule obj_local(s: &ParserSettings) -> BindSpec133			= keyword("local") _ bind:bind(s) {bind}134		pub rule member(s: &ParserSettings) -> expr::Member135			= bind:obj_local(s) {expr::Member::BindStmt(bind)}136			/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}137			/ field:field(s) {expr::Member::Field(field)}138		pub rule objinside(s: &ParserSettings) -> expr::ObjBody139			= pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {140				let mut compspecs = vec![CompSpec::ForSpec(forspec)];141				compspecs.extend(others.unwrap_or_default());142				expr::ObjBody::ObjComp(expr::ObjComp{143					pre_locals,144					key,145					value,146					post_locals,147					compspecs,148				})149			}150			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}151		pub rule ifspec(s: &ParserSettings) -> IfSpecData152			= keyword("if") _ expr:expr(s) {IfSpecData(expr)}153		pub rule forspec(s: &ParserSettings) -> ForSpecData154			= keyword("for") _ id:$(id()) _ keyword("in") _ cond:expr(s) {ForSpecData(id.into(), cond)}155		pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>156			= s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}157		pub rule local_expr(s: &ParserSettings) -> LocExpr158			= l(s,<keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }>)159		pub rule string_expr(s: &ParserSettings) -> LocExpr160			= l(s, <s:string() {Expr::Str(s.into())}>)161		pub rule obj_expr(s: &ParserSettings) -> LocExpr162			= l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)163		pub rule array_expr(s: &ParserSettings) -> LocExpr164			= l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)165		pub rule array_comp_expr(s: &ParserSettings) -> LocExpr166			= l(s,<"[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {167				let mut specs = vec![CompSpec::ForSpec(forspec)];168				specs.extend(others.unwrap_or_default());169				Expr::ArrComp(expr, specs)170			}>)171		pub rule number_expr(s: &ParserSettings) -> LocExpr172			= l(s,<n:number() { expr::Expr::Num(n) }>)173		pub rule var_expr(s: &ParserSettings) -> LocExpr174			= l(s,<n:$(id()) { expr::Expr::Var(n.into()) }>)175		pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr176			= l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{177				cond,178				cond_then,179				cond_else,180			}}>)181182		pub rule literal(s: &ParserSettings) -> LocExpr183			= l(s,<v:(184				keyword("null") {LiteralType::Null}185				/ keyword("true") {LiteralType::True}186				/ keyword("false") {LiteralType::False}187				/ keyword("self") {LiteralType::This}188				/ keyword("$") {LiteralType::Dollar}189				/ keyword("super") {LiteralType::Super}190			) {Expr::Literal(v)}>)191192		pub rule expr_basic(s: &ParserSettings) -> LocExpr193			= literal(s)194195			/ quiet!{l(s,<"$intrinsic(" name:$(id()) ")" {Expr::Intrinsic(name.into())}>)}196197			/ string_expr(s) / number_expr(s)198			/ array_expr(s)199			/ obj_expr(s)200			/ array_expr(s)201			/ array_comp_expr(s)202203			/ l(s,<keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}>)204			/ l(s,<keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}>)205206			/ var_expr(s)207			/ local_expr(s)208			/ if_then_else_expr(s)209210			/ l(s,<keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}>)211			/ l(s,<assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }>)212213			/ l(s,<keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }>)214215		rule slice_part(s: &ParserSettings) -> Option<LocExpr>216			= e:(_ e:expr(s) _{e})? {e}217		pub rule slice_desc(s: &ParserSettings) -> SliceDesc218			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {219				let (end, step) = if let Some((end, step)) = pair {220					(end, step)221				}else{222					(None, None)223				};224225				SliceDesc { start, end, step }226			}227228		rule binop(x: rule<()>) -> ()229			= quiet!{ x() } / expected!("<binary op>")230		rule unaryop(x: rule<()>) -> ()231			= quiet!{ x() } / expected!("<unary op>")232233234		use BinaryOpType::*;235		use UnaryOpType::*;236		rule expr(s: &ParserSettings) -> LocExpr237			= start:position!() a:precedence! {238				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}239				--240				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}241				--242				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}243				--244				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}245				--246				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}247				--248				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}249				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}250				--251				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}252				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}253				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}254				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}255				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}256				--257				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}258				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}259				--260				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}261				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}262				--263				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}264				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}265				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}266				--267						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}268						unaryop(<"!">) _ b:@ {expr_un!(Not b)}269						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}270				--271				a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Slice(a, s))}272				a:(@) _ "." _ s:$(id()) {loc_expr_todo!(Expr::Index(a, el!(Expr::Str(s.into()))))}273				a:(@) _ "[" _ s:expr(s) _ "]" {loc_expr_todo!(Expr::Index(a, s))}274				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {loc_expr_todo!(Expr::Apply(a, args, ts.is_some()))}275				a:(@) _ "{" _ body:objinside(s) _ "}" {loc_expr_todo!(Expr::ObjExtend(a, body))}276				--277				e:expr_basic(s) {e}278				"(" _ e:expr(s) _ ")" {loc_expr_todo!(Expr::Parened(e))}279			} end:position!() {280				let LocExpr(e, _) = a;281				LocExpr(e, if s.loc_data {282					Some(ExprLocation(s.file_name.clone(), start, end))283				} else {284					None285				})286			}287			/ e:expr_basic(s) {e}288289		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}290	}291}292293pub type ParseError = peg::error::ParseError<peg::str::LineCol>;294pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {295	jsonnet_parser::jsonnet(str, settings)296}297298#[macro_export]299macro_rules! el {300	($expr:expr) => {301		LocExpr(std::rc::Rc::new($expr), None)302	};303}304305#[cfg(test)]306pub mod tests {307	use super::{expr::*, parse};308	use crate::ParserSettings;309	use std::path::PathBuf;310	use BinaryOpType::*;311312	macro_rules! parse {313		($s:expr) => {314			parse(315				$s,316				&ParserSettings {317					loc_data: false,318					file_name: PathBuf::from("/test.jsonnet").into(),319				},320			)321			.unwrap()322		};323	}324325	mod expressions {326		use super::*;327328		pub fn basic_math() -> LocExpr {329			el!(Expr::BinaryOp(330				el!(Expr::Num(2.0)),331				Add,332				el!(Expr::BinaryOp(333					el!(Expr::Num(2.0)),334					Mul,335					el!(Expr::Num(2.0)),336				)),337			))338		}339	}340341	#[test]342	fn multiline_string() {343		assert_eq!(344			parse!("|||\n    Hello world!\n     a\n|||"),345			el!(Expr::Str("Hello world!\n a\n".into())),346		);347		assert_eq!(348			parse!("|||\n  Hello world!\n   a\n|||"),349			el!(Expr::Str("Hello world!\n a\n".into())),350		);351		assert_eq!(352			parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),353			el!(Expr::Str("Hello world!\n\ta\n".into())),354		);355		assert_eq!(356			parse!("|||\n   Hello world!\n    a\n |||"),357			el!(Expr::Str("Hello world!\n a\n".into())),358		);359	}360361	#[test]362	fn slice() {363		parse!("a[1:]");364		parse!("a[1::]");365		parse!("a[:1:]");366		parse!("a[::1]");367		parse!("str[:len - 1]");368	}369370	#[test]371	fn string_escaping() {372		assert_eq!(373			parse!(r#""Hello, \"world\"!""#),374			el!(Expr::Str(r#"Hello, "world"!"#.into())),375		);376		assert_eq!(377			parse!(r#"'Hello \'world\'!'"#),378			el!(Expr::Str("Hello 'world'!".into())),379		);380		assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into())),);381	}382383	#[test]384	fn string_unescaping() {385		assert_eq!(386			parse!(r#""Hello\nWorld""#),387			el!(Expr::Str("Hello\nWorld".into())),388		);389	}390391	#[test]392	fn string_verbantim() {393		assert_eq!(394			parse!(r#"@"Hello\n""World""""#),395			el!(Expr::Str("Hello\\n\"World\"".into())),396		);397	}398399	#[test]400	fn imports() {401		assert_eq!(402			parse!("import \"hello\""),403			el!(Expr::Import(PathBuf::from("hello"))),404		);405		assert_eq!(406			parse!("importstr \"garnish.txt\""),407			el!(Expr::ImportStr(PathBuf::from("garnish.txt")))408		);409	}410411	#[test]412	fn empty_object() {413		assert_eq!(parse!("{}"), el!(Expr::Obj(ObjBody::MemberList(vec![]))));414	}415416	#[test]417	fn basic_math() {418		assert_eq!(419			parse!("2+2*2"),420			el!(Expr::BinaryOp(421				el!(Expr::Num(2.0)),422				Add,423				el!(Expr::BinaryOp(424					el!(Expr::Num(2.0)),425					Mul,426					el!(Expr::Num(2.0))427				))428			))429		);430	}431432	#[test]433	fn basic_math_with_indents() {434		assert_eq!(parse!("2	+ 	  2	  *	2   	"), expressions::basic_math());435	}436437	#[test]438	fn basic_math_parened() {439		assert_eq!(440			parse!("2+(2+2*2)"),441			el!(Expr::BinaryOp(442				el!(Expr::Num(2.0)),443				Add,444				el!(Expr::Parened(expressions::basic_math())),445			))446		);447	}448449	/// Comments should not affect parsing450	#[test]451	fn comments() {452		assert_eq!(453			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),454			el!(Expr::BinaryOp(455				el!(Expr::Num(2.0)),456				Add,457				el!(Expr::BinaryOp(458					el!(Expr::Num(3.0)),459					Mul,460					el!(Expr::Num(4.0))461				))462			))463		);464	}465466	/// Comments should be able to be escaped467	#[test]468	fn comment_escaping() {469		assert_eq!(470			parse!("2/*\\*/+*/ - 22"),471			el!(Expr::BinaryOp(472				el!(Expr::Num(2.0)),473				Sub,474				el!(Expr::Num(22.0))475			))476		);477	}478479	#[test]480	fn suffix() {481		// assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));482		// assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));483		// assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));484		// assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))485	}486487	#[test]488	fn array_comp() {489		use Expr::*;490		assert_eq!(491			parse!("[std.deepJoin(x) for x in arr]"),492			el!(ArrComp(493				el!(Apply(494					el!(Index(el!(Var("std".into())), el!(Str("deepJoin".into())))),495					ArgsDesc(vec![Arg(None, el!(Var("x".into())))]),496					false,497				)),498				vec![CompSpec::ForSpec(ForSpecData(499					"x".into(),500					el!(Var("arr".into()))501				))]502			)),503		)504	}505506	#[test]507	fn reserved() {508		use Expr::*;509		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null)));510		assert_eq!(parse!("nulla"), el!(Var("nulla".into())));511	}512513	#[test]514	fn multiple_args_buf() {515		parse!("a(b, null_fields)");516	}517518	#[test]519	fn infix_precedence() {520		use Expr::*;521		assert_eq!(522			parse!("!a && !b"),523			el!(BinaryOp(524				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),525				And,526				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))527			))528		);529	}530531	#[test]532	fn infix_precedence_division() {533		use Expr::*;534		assert_eq!(535			parse!("!a / !b"),536			el!(BinaryOp(537				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),538				Div,539				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))540			))541		);542	}543544	#[test]545	fn double_negation() {546		use Expr::*;547		assert_eq!(548			parse!("!!a"),549			el!(UnaryOp(550				UnaryOpType::Not,551				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()))))552			))553		)554	}555556	#[test]557	fn array_test_error() {558		parse!("[a for a in b if c for e in f]");559		//                    ^^^^ failed code560	}561562	#[test]563	fn can_parse_stdlib() {564		parse!(jrsonnet_stdlib::STDLIB_STR);565	}566567	// From source code568	/*569	#[bench]570	fn bench_parse_peg(b: &mut Bencher) {571		b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))572	}573	*/574}
after · crates/jrsonnet-parser/src/lib.rs
1#![allow(clippy::redundant_closure_call)]23use peg::parser;4use std::{5	path::{Path, PathBuf},6	rc::Rc,7};8mod expr;9pub use expr::*;10pub use peg;1112pub struct ParserSettings {13	pub loc_data: bool,14	pub file_name: Rc<Path>,15}1617macro_rules! expr_bin {18	($a:ident $op:ident $b:ident) => {19		loc_expr_todo!(Expr::BinaryOp($a, $op, $b))20	};21}22macro_rules! expr_un {23	($op:ident $a:ident) => {24		loc_expr_todo!(Expr::UnaryOp($op, $a))25	};26}2728parser! {29	grammar jsonnet_parser() for str {30		use peg::ParseLiteral;3132		/// Standard C-like comments33		rule comment()34			= "//" (!['\n'][_])* "\n"35			/ "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"36			/ "#" (!['\n'][_])* "\n"3738		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")39		rule _() = single_whitespace()*4041		/// For comma-delimited elements42		rule comma() = quiet!{_ "," _} / expected!("<comma>")43		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}44		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}45		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']46		/// Sequence of digits47		rule uint_str() -> &'input str = a:$(digit()+) { a }48		/// Number in scientific notation format49		rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")5051		/// Reserved word followed by any non-alphanumberic52		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()53		rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")5455		rule keyword(id: &'static str) -> ()56			= ##parse_string_literal(id) end_of_ident()57		// Adds location data information to existing expression58		rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr59			= start:position!() v:x() end:position!() {loc_expr!(v, s.loc_data, (s.file_name.clone(), start, end))}6061		pub rule param(s: &ParserSettings) -> expr::Param = name:$(id()) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name.into(), expr) }62		pub rule params(s: &ParserSettings) -> expr::ParamsDesc63			= params:param(s) ** comma() comma()? {64				let mut defaults_started = false;65				for param in &params {66					defaults_started = defaults_started || param.1.is_some();67					assert_eq!(defaults_started, param.1.is_some(), "defauld parameters should be used after all positionals");68				}69				expr::ParamsDesc(Rc::new(params))70			}71			/ { expr::ParamsDesc(Rc::new(Vec::new())) }7273		pub rule arg(s: &ParserSettings) -> expr::Arg74			= name:$(id()) _ "=" _ expr:expr(s) {expr::Arg(Some(name.into()), expr)}75			/ expr:expr(s) {expr::Arg(None, expr)}76		pub rule args(s: &ParserSettings) -> expr::ArgsDesc77			= args:arg(s) ** comma() comma()? {78				let mut named_started = false;79				for arg in &args {80					named_started = named_started || arg.0.is_some();81					assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");82				}83				expr::ArgsDesc(args)84			}85			/ { expr::ArgsDesc(Vec::new()) }8687		pub rule bind(s: &ParserSettings) -> expr::BindSpec88			= name:$(id()) _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: None, value: expr}}89			/ name:$(id()) _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: Some(params), value: expr}}90		pub rule assertion(s: &ParserSettings) -> expr::AssertStmt91			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }9293		pub rule whole_line() -> &'input str94			= str:$((!['\n'][_])* "\n") {str}95		pub rule string_block() -> String96			= "|||" (!['\n']single_whitespace())* "\n"97			  empty_lines:$(['\n']*)98			  prefix:[' ' | '\t']+ first_line:whole_line()99			  lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*100			  [' ' | '\t']*<, {prefix.len() - 1}> "|||"101			  {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}102		pub rule string() -> String103			= quiet!{ "\"" str:$(("\\\"" / "\\\\" / (!['"'][_]))*) "\"" {unescape::unescape(str).unwrap()}104			/ "'" str:$(("\\'" / "\\\\" / (!['\''][_]))*) "'" {unescape::unescape(str).unwrap()}105			/ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}106			/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}107			/ string_block() } / expected!("<string>")108109		pub rule field_name(s: &ParserSettings) -> expr::FieldName110			= name:$(id()) {expr::FieldName::Fixed(name.into())}111			/ name:string() {expr::FieldName::Fixed(name.into())}112			/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}113		pub rule visibility() -> expr::Visibility114			= ":::" {expr::Visibility::Unhide}115			/ "::" {expr::Visibility::Hidden}116			/ ":" {expr::Visibility::Normal}117		pub rule field(s: &ParserSettings) -> expr::FieldMember118			= name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{119				name,120				plus: plus.is_some(),121				params: None,122				visibility,123				value,124			}}125			/ name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{126				name,127				plus: false,128				params: Some(params),129				visibility,130				value,131			}}132		pub rule obj_local(s: &ParserSettings) -> BindSpec133			= keyword("local") _ bind:bind(s) {bind}134		pub rule member(s: &ParserSettings) -> expr::Member135			= bind:obj_local(s) {expr::Member::BindStmt(bind)}136			/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}137			/ field:field(s) {expr::Member::Field(field)}138		pub rule objinside(s: &ParserSettings) -> expr::ObjBody139			= pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ plus:"+"? _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {140				let mut compspecs = vec![CompSpec::ForSpec(forspec)];141				compspecs.extend(others.unwrap_or_default());142				expr::ObjBody::ObjComp(expr::ObjComp{143					pre_locals,144					key,145					plus: plus.is_some(),146					value,147					post_locals,148					compspecs,149				})150			}151			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}152		pub rule ifspec(s: &ParserSettings) -> IfSpecData153			= keyword("if") _ expr:expr(s) {IfSpecData(expr)}154		pub rule forspec(s: &ParserSettings) -> ForSpecData155			= keyword("for") _ id:$(id()) _ keyword("in") _ cond:expr(s) {ForSpecData(id.into(), cond)}156		pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>157			= s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}158		pub rule local_expr(s: &ParserSettings) -> LocExpr159			= l(s,<keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }>)160		pub rule string_expr(s: &ParserSettings) -> LocExpr161			= l(s, <s:string() {Expr::Str(s.into())}>)162		pub rule obj_expr(s: &ParserSettings) -> LocExpr163			= l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)164		pub rule array_expr(s: &ParserSettings) -> LocExpr165			= l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)166		pub rule array_comp_expr(s: &ParserSettings) -> LocExpr167			= l(s,<"[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {168				let mut specs = vec![CompSpec::ForSpec(forspec)];169				specs.extend(others.unwrap_or_default());170				Expr::ArrComp(expr, specs)171			}>)172		pub rule number_expr(s: &ParserSettings) -> LocExpr173			= l(s,<n:number() { expr::Expr::Num(n) }>)174		pub rule var_expr(s: &ParserSettings) -> LocExpr175			= l(s,<n:$(id()) { expr::Expr::Var(n.into()) }>)176		pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr177			= l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{178				cond,179				cond_then,180				cond_else,181			}}>)182183		pub rule literal(s: &ParserSettings) -> LocExpr184			= l(s,<v:(185				keyword("null") {LiteralType::Null}186				/ keyword("true") {LiteralType::True}187				/ keyword("false") {LiteralType::False}188				/ keyword("self") {LiteralType::This}189				/ keyword("$") {LiteralType::Dollar}190				/ keyword("super") {LiteralType::Super}191			) {Expr::Literal(v)}>)192193		pub rule expr_basic(s: &ParserSettings) -> LocExpr194			= literal(s)195196			/ quiet!{l(s,<"$intrinsic(" name:$(id()) ")" {Expr::Intrinsic(name.into())}>)}197198			/ string_expr(s) / number_expr(s)199			/ array_expr(s)200			/ obj_expr(s)201			/ array_expr(s)202			/ array_comp_expr(s)203204			/ l(s,<keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}>)205			/ l(s,<keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}>)206207			/ var_expr(s)208			/ local_expr(s)209			/ if_then_else_expr(s)210211			/ l(s,<keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}>)212			/ l(s,<assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }>)213214			/ l(s,<keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }>)215216		rule slice_part(s: &ParserSettings) -> Option<LocExpr>217			= e:(_ e:expr(s) _{e})? {e}218		pub rule slice_desc(s: &ParserSettings) -> SliceDesc219			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {220				let (end, step) = if let Some((end, step)) = pair {221					(end, step)222				}else{223					(None, None)224				};225226				SliceDesc { start, end, step }227			}228229		rule binop(x: rule<()>) -> ()230			= quiet!{ x() } / expected!("<binary op>")231		rule unaryop(x: rule<()>) -> ()232			= quiet!{ x() } / expected!("<unary op>")233234235		use BinaryOpType::*;236		use UnaryOpType::*;237		rule expr(s: &ParserSettings) -> LocExpr238			= start:position!() a:precedence! {239				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}240				--241				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}242				--243				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}244				--245				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}246				--247				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}248				--249				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}250				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}251				--252				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}253				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}254				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}255				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}256				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}257				--258				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}259				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}260				--261				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}262				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}263				--264				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}265				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}266				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}267				--268						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}269						unaryop(<"!">) _ b:@ {expr_un!(Not b)}270						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}271				--272				a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Slice(a, s))}273				a:(@) _ "." _ s:$(id()) {loc_expr_todo!(Expr::Index(a, el!(Expr::Str(s.into()))))}274				a:(@) _ "[" _ s:expr(s) _ "]" {loc_expr_todo!(Expr::Index(a, s))}275				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {loc_expr_todo!(Expr::Apply(a, args, ts.is_some()))}276				a:(@) _ "{" _ body:objinside(s) _ "}" {loc_expr_todo!(Expr::ObjExtend(a, body))}277				--278				e:expr_basic(s) {e}279				"(" _ e:expr(s) _ ")" {loc_expr_todo!(Expr::Parened(e))}280			} end:position!() {281				let LocExpr(e, _) = a;282				LocExpr(e, if s.loc_data {283					Some(ExprLocation(s.file_name.clone(), start, end))284				} else {285					None286				})287			}288			/ e:expr_basic(s) {e}289290		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}291	}292}293294pub type ParseError = peg::error::ParseError<peg::str::LineCol>;295pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {296	jsonnet_parser::jsonnet(str, settings)297}298299#[macro_export]300macro_rules! el {301	($expr:expr) => {302		LocExpr(std::rc::Rc::new($expr), None)303	};304}305306#[cfg(test)]307pub mod tests {308	use super::{expr::*, parse};309	use crate::ParserSettings;310	use std::path::PathBuf;311	use BinaryOpType::*;312313	macro_rules! parse {314		($s:expr) => {315			parse(316				$s,317				&ParserSettings {318					loc_data: false,319					file_name: PathBuf::from("/test.jsonnet").into(),320				},321			)322			.unwrap()323		};324	}325326	mod expressions {327		use super::*;328329		pub fn basic_math() -> LocExpr {330			el!(Expr::BinaryOp(331				el!(Expr::Num(2.0)),332				Add,333				el!(Expr::BinaryOp(334					el!(Expr::Num(2.0)),335					Mul,336					el!(Expr::Num(2.0)),337				)),338			))339		}340	}341342	#[test]343	fn multiline_string() {344		assert_eq!(345			parse!("|||\n    Hello world!\n     a\n|||"),346			el!(Expr::Str("Hello world!\n a\n".into())),347		);348		assert_eq!(349			parse!("|||\n  Hello world!\n   a\n|||"),350			el!(Expr::Str("Hello world!\n a\n".into())),351		);352		assert_eq!(353			parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),354			el!(Expr::Str("Hello world!\n\ta\n".into())),355		);356		assert_eq!(357			parse!("|||\n   Hello world!\n    a\n |||"),358			el!(Expr::Str("Hello world!\n a\n".into())),359		);360	}361362	#[test]363	fn slice() {364		parse!("a[1:]");365		parse!("a[1::]");366		parse!("a[:1:]");367		parse!("a[::1]");368		parse!("str[:len - 1]");369	}370371	#[test]372	fn string_escaping() {373		assert_eq!(374			parse!(r#""Hello, \"world\"!""#),375			el!(Expr::Str(r#"Hello, "world"!"#.into())),376		);377		assert_eq!(378			parse!(r#"'Hello \'world\'!'"#),379			el!(Expr::Str("Hello 'world'!".into())),380		);381		assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into())),);382	}383384	#[test]385	fn string_unescaping() {386		assert_eq!(387			parse!(r#""Hello\nWorld""#),388			el!(Expr::Str("Hello\nWorld".into())),389		);390	}391392	#[test]393	fn string_verbantim() {394		assert_eq!(395			parse!(r#"@"Hello\n""World""""#),396			el!(Expr::Str("Hello\\n\"World\"".into())),397		);398	}399400	#[test]401	fn imports() {402		assert_eq!(403			parse!("import \"hello\""),404			el!(Expr::Import(PathBuf::from("hello"))),405		);406		assert_eq!(407			parse!("importstr \"garnish.txt\""),408			el!(Expr::ImportStr(PathBuf::from("garnish.txt")))409		);410	}411412	#[test]413	fn empty_object() {414		assert_eq!(parse!("{}"), el!(Expr::Obj(ObjBody::MemberList(vec![]))));415	}416417	#[test]418	fn basic_math() {419		assert_eq!(420			parse!("2+2*2"),421			el!(Expr::BinaryOp(422				el!(Expr::Num(2.0)),423				Add,424				el!(Expr::BinaryOp(425					el!(Expr::Num(2.0)),426					Mul,427					el!(Expr::Num(2.0))428				))429			))430		);431	}432433	#[test]434	fn basic_math_with_indents() {435		assert_eq!(parse!("2	+ 	  2	  *	2   	"), expressions::basic_math());436	}437438	#[test]439	fn basic_math_parened() {440		assert_eq!(441			parse!("2+(2+2*2)"),442			el!(Expr::BinaryOp(443				el!(Expr::Num(2.0)),444				Add,445				el!(Expr::Parened(expressions::basic_math())),446			))447		);448	}449450	/// Comments should not affect parsing451	#[test]452	fn comments() {453		assert_eq!(454			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),455			el!(Expr::BinaryOp(456				el!(Expr::Num(2.0)),457				Add,458				el!(Expr::BinaryOp(459					el!(Expr::Num(3.0)),460					Mul,461					el!(Expr::Num(4.0))462				))463			))464		);465	}466467	/// Comments should be able to be escaped468	#[test]469	fn comment_escaping() {470		assert_eq!(471			parse!("2/*\\*/+*/ - 22"),472			el!(Expr::BinaryOp(473				el!(Expr::Num(2.0)),474				Sub,475				el!(Expr::Num(22.0))476			))477		);478	}479480	#[test]481	fn suffix() {482		// assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));483		// assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));484		// assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));485		// assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))486	}487488	#[test]489	fn array_comp() {490		use Expr::*;491		assert_eq!(492			parse!("[std.deepJoin(x) for x in arr]"),493			el!(ArrComp(494				el!(Apply(495					el!(Index(el!(Var("std".into())), el!(Str("deepJoin".into())))),496					ArgsDesc(vec![Arg(None, el!(Var("x".into())))]),497					false,498				)),499				vec![CompSpec::ForSpec(ForSpecData(500					"x".into(),501					el!(Var("arr".into()))502				))]503			)),504		)505	}506507	#[test]508	fn reserved() {509		use Expr::*;510		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null)));511		assert_eq!(parse!("nulla"), el!(Var("nulla".into())));512	}513514	#[test]515	fn multiple_args_buf() {516		parse!("a(b, null_fields)");517	}518519	#[test]520	fn infix_precedence() {521		use Expr::*;522		assert_eq!(523			parse!("!a && !b"),524			el!(BinaryOp(525				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),526				And,527				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))528			))529		);530	}531532	#[test]533	fn infix_precedence_division() {534		use Expr::*;535		assert_eq!(536			parse!("!a / !b"),537			el!(BinaryOp(538				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),539				Div,540				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))541			))542		);543	}544545	#[test]546	fn double_negation() {547		use Expr::*;548		assert_eq!(549			parse!("!!a"),550			el!(UnaryOp(551				UnaryOpType::Not,552				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()))))553			))554		)555	}556557	#[test]558	fn array_test_error() {559		parse!("[a for a in b if c for e in f]");560		//                    ^^^^ failed code561	}562563	#[test]564	fn can_parse_stdlib() {565		parse!(jrsonnet_stdlib::STDLIB_STR);566	}567568	// From source code569	/*570	#[bench]571	fn bench_parse_peg(b: &mut Bencher) {572		b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))573	}574	*/575}