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

difftreelog

source

crates/jrsonnet-parser/src/lib.rs17.6 KiBsourcehistory
1use peg::parser;2use std::{path::PathBuf, rc::Rc};3mod expr;4pub use expr::*;5pub use peg;67#[derive(Default)]8pub struct ParserSettings {9	pub loc_data: bool,10	pub file_name: Rc<PathBuf>,11}1213parser! {14	grammar jsonnet_parser() for str {15		use peg::ParseLiteral;1617		/// Standard C-like comments18		rule comment()19			= "//" (!['\n'][_])* "\n"20			/ "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"21			/ "#" (!['\n'][_])* "\n"2223		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")24		rule _() = single_whitespace()*2526		/// For comma-delimited elements27		rule comma() = quiet!{_ "," _} / expected!("<comma>")28		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}29		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}30		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']31		/// Sequence of digits32		rule uint() -> u64 = a:$(digit()+) { a.parse().unwrap() }33		/// Number in scientific notation format34		rule number() -> f64 = quiet!{a:$(uint() ("." uint())? (['e'|'E'] (s:['+'|'-'])? uint())?) { a.parse().unwrap() }} / expected!("<number>")3536		/// Reserved word followed by any non-alphanumberic37		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()38		rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")3940		rule keyword(id: &'static str)41			= ##parse_string_literal(id) end_of_ident()42		// Adds location data information to existing expression43		rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr44			= start:position!() v:x() end:position!() {loc_expr!(v, s.loc_data, (s.file_name.clone(), start, end))}4546		pub rule param(s: &ParserSettings) -> expr::Param = name:$(id()) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name.into(), expr) }47		pub rule params(s: &ParserSettings) -> expr::ParamsDesc48			= params:param(s) ** comma() comma()? {49				let mut defaults_started = false;50				for param in &params {51					defaults_started = defaults_started || param.1.is_some();52					assert_eq!(defaults_started, param.1.is_some(), "defauld parameters should be used after all positionals");53				}54				expr::ParamsDesc(Rc::new(params))55			}56			/ { expr::ParamsDesc(Rc::new(Vec::new())) }5758		pub rule arg(s: &ParserSettings) -> expr::Arg59			= name:$(id()) _ "=" _ expr:expr(s) {expr::Arg(Some(name.into()), expr)}60			/ expr:expr(s) {expr::Arg(None, expr)}61		pub rule args(s: &ParserSettings) -> expr::ArgsDesc62			= args:arg(s) ** comma() comma()? {63				let mut named_started = false;64				for arg in &args {65					named_started = named_started || arg.0.is_some();66					assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");67				}68				expr::ArgsDesc(args)69			}70			/ { expr::ArgsDesc(Vec::new()) }7172		pub rule bind(s: &ParserSettings) -> expr::BindSpec73			= name:$(id()) _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: None, value: expr}}74			/ name:$(id()) _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: Some(params), value: expr}}75		pub rule assertion(s: &ParserSettings) -> expr::AssertStmt76			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }7778		pub rule whole_line() -> &'input str79			= str:$((!['\n'][_])* "\n") {str}80		pub rule string_block() -> String81			= "|||" (!['\n']single_whitespace())* "\n"82			  empty_lines:$(['\n']*)83			  prefix:[' ' | '\t']+ first_line:whole_line()84			  lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*85			  [' ' | '\t']*<, {prefix.len() - 1}> "|||"86			  {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}87		pub rule string() -> String88			= "\"" str:$(("\\\"" / "\\\\" / (!['"'][_]))*) "\"" {unescape::unescape(str).unwrap()}89			/ "'" str:$(("\\'" / "\\\\" / (!['\''][_]))*) "'" {unescape::unescape(str).unwrap()}90			/ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}91			/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}92			/ string_block()9394		pub rule field_name(s: &ParserSettings) -> expr::FieldName95			= name:$(id()) {expr::FieldName::Fixed(name.into())}96			/ name:string() {expr::FieldName::Fixed(name.into())}97			/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}98		pub rule visibility() -> expr::Visibility99			= ":::" {expr::Visibility::Unhide}100			/ "::" {expr::Visibility::Hidden}101			/ ":" {expr::Visibility::Normal}102		pub rule field(s: &ParserSettings) -> expr::FieldMember103			= name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{104				name,105				plus: plus.is_some(),106				params: None,107				visibility,108				value,109			}}110			/ name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{111				name,112				plus: false,113				params: Some(params),114				visibility,115				value,116			}}117		pub rule obj_local(s: &ParserSettings) -> BindSpec118			= keyword("local") _ bind:bind(s) {bind}119		pub rule member(s: &ParserSettings) -> expr::Member120			= bind:obj_local(s) {expr::Member::BindStmt(bind)}121			/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}122			/ field:field(s) {expr::Member::Field(field)}123		pub rule objinside(s: &ParserSettings) -> expr::ObjBody124			= 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})? {125				let mut compspecs = vec![CompSpec::ForSpec(forspec)];126				compspecs.extend(others.unwrap_or_default());127				expr::ObjBody::ObjComp(expr::ObjComp{128					pre_locals,129					key,130					value,131					post_locals,132					compspecs,133				})134			}135			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}136		pub rule ifspec(s: &ParserSettings) -> IfSpecData137			= keyword("if") _ expr:expr(s) {IfSpecData(expr)}138		pub rule forspec(s: &ParserSettings) -> ForSpecData139			= keyword("for") _ id:$(id()) _ keyword("in") _ cond:expr(s) {ForSpecData(id.into(), cond)}140		pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>141			= s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}142		pub rule local_expr(s: &ParserSettings) -> LocExpr143			= l(s,<keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }>)144		pub rule string_expr(s: &ParserSettings) -> LocExpr145			= l(s, <s:string() {Expr::Str(s.into())}>)146		pub rule obj_expr(s: &ParserSettings) -> LocExpr147			= l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)148		pub rule array_expr(s: &ParserSettings) -> LocExpr149			= l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)150		pub rule array_comp_expr(s: &ParserSettings) -> LocExpr151			= l(s,<"[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {152				let mut specs = vec![CompSpec::ForSpec(forspec)];153				specs.extend(others.unwrap_or_default());154				Expr::ArrComp(expr, specs)155			}>)156		pub rule number_expr(s: &ParserSettings) -> LocExpr157			= l(s,<n:number() { expr::Expr::Num(n) }>)158		pub rule var_expr(s: &ParserSettings) -> LocExpr159			= l(s,<n:$(id()) { expr::Expr::Var(n.into()) }>)160		pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr161			= l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{162				cond,163				cond_then,164				cond_else,165			}}>)166167		pub rule literal(s: &ParserSettings) -> LocExpr168			= l(s,<v:(169				keyword("null") {LiteralType::Null}170				/ keyword("true") {LiteralType::True}171				/ keyword("false") {LiteralType::False}172				/ keyword("self") {LiteralType::This}173				/ keyword("$") {LiteralType::Dollar}174				/ keyword("super") {LiteralType::Super}175			) {Expr::Literal(v)}>)176177		pub rule expr_basic(s: &ParserSettings) -> LocExpr178			= literal(s)179180			/ string_expr(s) / number_expr(s)181			/ array_expr(s)182			/ obj_expr(s)183			/ array_expr(s)184			/ array_comp_expr(s)185186			/ l(s,<keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}>)187			/ l(s,<keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}>)188189			/ var_expr(s)190			/ local_expr(s)191			/ if_then_else_expr(s)192193			/ l(s,<keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}>)194			/ l(s,<assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }>)195196			/ l(s,<keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }>)197198		rule slice_part(s: &ParserSettings) -> Option<LocExpr>199			= e:(_ e:expr(s) _{e})? {e}200		pub rule slice_desc(s: &ParserSettings) -> SliceDesc201			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {202				let (end, step) = if let Some((end, step)) = pair {203					(end, step)204				}else{205					(None, None)206				};207208				SliceDesc { start, end, step }209			}210211		rule expr(s: &ParserSettings) -> LocExpr212			= start:position!() a:precedence! {213				a:(@) _ "||" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}214				--215				a:(@) _ "&&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}216				--217				a:(@) _ "|" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}218				--219				a:@ _ "^" _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}220				--221				a:(@) _ "&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}222				--223				a:(@) _ "==" _ b:@ {loc_expr_todo!(Expr::Apply(224					el!(Expr::Intrinsic("equals".into())),225					ArgsDesc(vec![Arg(None, a), Arg(None, b)]),226					true227				))}228				a:(@) _ "!=" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, el!(Expr::Apply(229					el!(Expr::Intrinsic("equals".into())),230					ArgsDesc(vec![Arg(None, a), Arg(None, b)]),231					true232				))))}233				--234				a:(@) _ "<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}235				a:(@) _ ">" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}236				a:(@) _ "<=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}237				a:(@) _ ">=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}238				a:(@) _ keyword("in") _ b:@ {loc_expr_todo!(Expr::Apply(239					el!(Expr::Intrinsic("objectHasEx".into())), ArgsDesc(vec![Arg(None, b), Arg(None, a), Arg(None, el!(Expr::Literal(LiteralType::True)))]),240					true241				))}242				--243				a:(@) _ "<<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}244				a:(@) _ ">>" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}245				--246				a:(@) _ "+" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}247				a:(@) _ "-" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}248				--249				a:(@) _ "*" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}250				a:(@) _ "/" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}251				a:(@) _ "%" _ b:@ {loc_expr_todo!(Expr::Apply(252					el!(Expr::Intrinsic("mod".into())), ArgsDesc(vec![Arg(None, a), Arg(None, b)]),253					false254				))}255				--256						"-" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}257						"!" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}258						"~" _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }259				--260				a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Apply(261					el!(Expr::Intrinsic("slice".into())),262					ArgsDesc(vec![263						Arg(None, a),264						Arg(None, s.start.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),265						Arg(None, s.end.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),266						Arg(None, s.step.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),267					]),268					true,269				))}270				a:(@) _ "." _ s:$(id()) {loc_expr_todo!(Expr::Index(a, el!(Expr::Str(s.into()))))}271				a:(@) _ "[" _ s:expr(s) _ "]" {loc_expr_todo!(Expr::Index(a, s))}272				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {loc_expr_todo!(Expr::Apply(a, args, ts.is_some()))}273				a:(@) _ "{" _ body:objinside(s) _ "}" {loc_expr_todo!(Expr::ObjExtend(a, body))}274				--275				e:expr_basic(s) {e}276				"(" _ e:expr(s) _ ")" {loc_expr_todo!(Expr::Parened(e))}277			} end:position!() {278				let LocExpr(e, _) = a;279				LocExpr(e, if s.loc_data {280					Some(ExprLocation(s.file_name.clone(), start, end))281				} else {282					None283				})284			}285			/ e:expr_basic(s) {e}286287		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}288	}289}290291pub type ParseError = peg::error::ParseError<peg::str::LineCol>;292pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {293	jsonnet_parser::jsonnet(str, settings)294}295296#[macro_export]297macro_rules! el {298	($expr:expr) => {299		LocExpr(std::rc::Rc::new($expr), None)300	};301}302303#[cfg(test)]304pub mod tests {305	use super::{expr::*, parse};306	use crate::ParserSettings;307	use std::path::PathBuf;308	use std::rc::Rc;309310	macro_rules! parse {311		($s:expr) => {312			parse(313				$s,314				&ParserSettings {315					loc_data: false,316					file_name: Rc::new(PathBuf::from("/test.jsonnet")),317					},318				)319			.unwrap()320		};321	}322323	mod expressions {324		use super::*;325326		pub fn basic_math() -> LocExpr {327			el!(Expr::BinaryOp(328				el!(Expr::Num(2.0)),329				BinaryOpType::Add,330				el!(Expr::BinaryOp(331					el!(Expr::Num(2.0)),332					BinaryOpType::Mul,333					el!(Expr::Num(2.0)),334				)),335			))336		}337	}338339	#[test]340	fn multiline_string() {341		assert_eq!(342			parse!("|||\n    Hello world!\n     a\n|||"),343			el!(Expr::Str("Hello world!\n a\n".into())),344		);345		assert_eq!(346			parse!("|||\n  Hello world!\n   a\n|||"),347			el!(Expr::Str("Hello world!\n a\n".into())),348		);349		assert_eq!(350			parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),351			el!(Expr::Str("Hello world!\n\ta\n".into())),352		);353		assert_eq!(354			parse!("|||\n   Hello world!\n    a\n |||"),355			el!(Expr::Str("Hello world!\n a\n".into())),356		);357	}358359	#[test]360	fn slice() {361		parse!("a[1:]");362		parse!("a[1::]");363		parse!("a[:1:]");364		parse!("a[::1]");365		parse!("str[:len - 1]");366	}367368	#[test]369	fn string_escaping() {370		assert_eq!(371			parse!(r#""Hello, \"world\"!""#),372			el!(Expr::Str(r#"Hello, "world"!"#.into())),373		);374		assert_eq!(375			parse!(r#"'Hello \'world\'!'"#),376			el!(Expr::Str("Hello 'world'!".into())),377		);378		assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into())),);379	}380381	#[test]382	fn string_unescaping() {383		assert_eq!(384			parse!(r#""Hello\nWorld""#),385			el!(Expr::Str("Hello\nWorld".into())),386		);387	}388389	#[test]390	fn string_verbantim() {391		assert_eq!(392			parse!(r#"@"Hello\n""World""""#),393			el!(Expr::Str("Hello\\n\"World\"".into())),394		);395	}396397	#[test]398	fn imports() {399		assert_eq!(400			parse!("import \"hello\""),401			el!(Expr::Import(PathBuf::from("hello"))),402		);403		assert_eq!(404			parse!("importstr \"garnish.txt\""),405			el!(Expr::ImportStr(PathBuf::from("garnish.txt")))406		);407	}408409	#[test]410	fn empty_object() {411		assert_eq!(parse!("{}"), el!(Expr::Obj(ObjBody::MemberList(vec![]))));412	}413414	#[test]415	fn basic_math() {416		assert_eq!(417			parse!("2+2*2"),418			el!(Expr::BinaryOp(419				el!(Expr::Num(2.0)),420				BinaryOpType::Add,421				el!(Expr::BinaryOp(422					el!(Expr::Num(2.0)),423					BinaryOpType::Mul,424					el!(Expr::Num(2.0))425				))426			))427		);428	}429430	#[test]431	fn basic_math_with_indents() {432		assert_eq!(parse!("2	+ 	  2	  *	2   	"), expressions::basic_math());433	}434435	#[test]436	fn basic_math_parened() {437		assert_eq!(438			parse!("2+(2+2*2)"),439			el!(Expr::BinaryOp(440				el!(Expr::Num(2.0)),441				BinaryOpType::Add,442				el!(Expr::Parened(expressions::basic_math())),443			))444		);445	}446447	/// Comments should not affect parsing448	#[test]449	fn comments() {450		assert_eq!(451			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),452			el!(Expr::BinaryOp(453				el!(Expr::Num(2.0)),454				BinaryOpType::Add,455				el!(Expr::BinaryOp(456					el!(Expr::Num(3.0)),457					BinaryOpType::Mul,458					el!(Expr::Num(4.0))459				))460			))461		);462	}463464	/// Comments should be able to be escaped465	#[test]466	fn comment_escaping() {467		assert_eq!(468			parse!("2/*\\*/+*/ - 22"),469			el!(Expr::BinaryOp(470				el!(Expr::Num(2.0)),471				BinaryOpType::Sub,472				el!(Expr::Num(22.0))473			))474		);475	}476477	#[test]478	fn suffix() {479		// assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));480		// assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));481		// assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));482		// assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))483	}484485	#[test]486	fn array_comp() {487		use Expr::*;488		assert_eq!(489			parse!("[std.deepJoin(x) for x in arr]"),490			el!(ArrComp(491				el!(Apply(492					el!(Index(el!(Var("std".into())), el!(Str("deepJoin".into())))),493					ArgsDesc(vec![Arg(None, el!(Var("x".into())))]),494					false,495				)),496				vec![CompSpec::ForSpec(ForSpecData(497					"x".into(),498					el!(Var("arr".into()))499				))]500			)),501		)502	}503504	#[test]505	fn reserved() {506		use Expr::*;507		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null)));508		assert_eq!(parse!("nulla"), el!(Var("nulla".into())));509	}510511	#[test]512	fn multiple_args_buf() {513		parse!("a(b, null_fields)");514	}515516	#[test]517	fn infix_precedence() {518		use Expr::*;519		assert_eq!(520			parse!("!a && !b"),521			el!(BinaryOp(522				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),523				BinaryOpType::And,524				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))525			))526		);527	}528529	#[test]530	fn infix_precedence_division() {531		use Expr::*;532		assert_eq!(533			parse!("!a / !b"),534			el!(BinaryOp(535				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),536				BinaryOpType::Div,537				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))538			))539		);540	}541542	#[test]543	fn double_negation() {544		use Expr::*;545		assert_eq!(546			parse!("!!a"),547			el!(UnaryOp(548				UnaryOpType::Not,549				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()))))550			))551		)552	}553554	#[test]555	fn array_test_error() {556		parse!("[a for a in b if c for e in f]");557		//                    ^^^^ failed code558	}559560	#[test]561	fn can_parse_stdlib() {562		parse!(jrsonnet_stdlib::STDLIB_STR);563	}564565	// From source code566	/*567	#[bench]568	fn bench_parse_peg(b: &mut Bencher) {569		b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))570	}571	*/572}