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

difftreelog

source

crates/jrsonnet-parser/src/lib.rs16.7 KiBsourcehistory
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}