git.delta.rocks / jrsonnet / refs/commits / 0b0d703c6d05

difftreelog

source

crates/jrsonnet-parser/src/lib.rs16.6 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() -> u64 = a:$(digit()+) { a.parse().unwrap() }48		/// Number in scientific notation format49		rule number() -> f64 = quiet!{a:$(uint() ("." uint())? (['e'|'E'] (s:['+'|'-'])? uint())?) { a.parse().unwrap() }} / 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			/ string_expr(s) / number_expr(s)196			/ array_expr(s)197			/ obj_expr(s)198			/ array_expr(s)199			/ array_comp_expr(s)200201			/ l(s,<keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}>)202			/ l(s,<keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}>)203204			/ var_expr(s)205			/ local_expr(s)206			/ if_then_else_expr(s)207208			/ l(s,<keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}>)209			/ l(s,<assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }>)210211			/ l(s,<keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }>)212213		rule slice_part(s: &ParserSettings) -> Option<LocExpr>214			= e:(_ e:expr(s) _{e})? {e}215		pub rule slice_desc(s: &ParserSettings) -> SliceDesc216			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {217				let (end, step) = if let Some((end, step)) = pair {218					(end, step)219				}else{220					(None, None)221				};222223				SliceDesc { start, end, step }224			}225226		rule binop(x: rule<()>) -> ()227			= quiet!{ x() } / expected!("<binary op>")228		rule unaryop(x: rule<()>) -> ()229			= quiet!{ x() } / expected!("<unary op>")230231232		use BinaryOpType::*;233		use UnaryOpType::*;234		rule expr(s: &ParserSettings) -> LocExpr235			= start:position!() a:precedence! {236				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}237				--238				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}239				--240				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}241				--242				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}243				--244				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}245				--246				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}247				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}248				--249				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}250				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}251				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}252				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}253				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}254				--255				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}256				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}257				--258				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}259				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}260				--261				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}262				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}263				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}264				--265						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}266						unaryop(<"!">) _ b:@ {expr_un!(Not b)}267						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}268				--269				a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Slice(a, s))}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 BinaryOpType::*;309310	macro_rules! parse {311		($s:expr) => {312			parse(313				$s,314				&ParserSettings {315					loc_data: false,316					file_name: PathBuf::from("/test.jsonnet").into(),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				Add,330				el!(Expr::BinaryOp(331					el!(Expr::Num(2.0)),332					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				Add,421				el!(Expr::BinaryOp(422					el!(Expr::Num(2.0)),423					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				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				Add,455				el!(Expr::BinaryOp(456					el!(Expr::Num(3.0)),457					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				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				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				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}