git.delta.rocks / jrsonnet / refs/commits / 55e13f5b9438

difftreelog

source

crates/jsonnet-parser/src/lib.rs15.7 KiBsourcehistory
1#![feature(box_syntax)]2#![feature(test)]34extern crate test;56use peg::parser;7use std::rc::Rc;8mod expr;9pub use expr::*;1011enum Suffix {12	String(String),13	Slice(SliceDesc),14	Expression(LocExpr),15	Apply(expr::ArgsDesc),16	Extend(expr::ObjBody),17}18struct LocSuffix(Suffix, ExprLocation);1920pub struct ParserSettings {21	pub loc_data: bool,22	pub file_name: String,23}2425parser! {26	grammar jsonnet_parser() for str {27		use peg::ParseLiteral;2829		/// Standard C-like comments30		rule comment()31			= "//" (!['\n'][_])* "\n"32			/ "/*" ((!("*/")[_][_])/("\\" "*/"))* "*/"33			/ "#" (!['\n'][_])* "\n"3435		rule _() = ([' ' | '\n' | '\t'] / comment())*3637		/// For comma-delimited elements38		rule comma() = quiet!{_ "," _} / expected!("<comma>")39		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}40		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}41		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']42		/// Sequence of digits43		rule uint() -> u32 = a:$(digit()+) { a.parse().unwrap() }44		/// Number in scientific notation format45		rule number() -> f64 = quiet!{a:$(uint() ("." uint())? (['e'|'E'] (s:['+'|'-'])? uint())?) { a.parse().unwrap() }} / expected!("<number>")4647		/// Reserved word followed by any non-alphanumberic48		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()49		rule id() -> String = quiet!{ !reserved() s:$(alpha() (alpha() / digit())*) {s.to_owned()}} / expected!("<identifier>")5051		rule keyword(id: &'static str)52			= ##parse_string_literal(id) end_of_ident()53		// Adds location data information to existing expression54		rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr55			= start:position!() v:x() end:position!() {loc_expr!(v, s.loc_data, (s.file_name.clone(), start, end))}5657		pub rule param(s: &ParserSettings) -> expr::Param = name:id() expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr) }58		pub rule params(s: &ParserSettings) -> expr::ParamsDesc59			= params:(param(s) ** comma()) {60				let mut defaults_started = false;61				for param in &params {62					defaults_started = defaults_started || param.1.is_some();63					assert_eq!(defaults_started, param.1.is_some(), "defauld parameters should be used after all positionals");64				}65				expr::ParamsDesc(params)66			}67			/ { expr::ParamsDesc(Vec::new()) }6869		pub rule arg(s: &ParserSettings) -> expr::Arg70			= name:id() _ "=" _ expr:expr(s) {expr::Arg(Some(name), expr)}71			/ expr:expr(s) {expr::Arg(None, expr)}72		pub rule args(s: &ParserSettings) -> expr::ArgsDesc73			= args:arg(s) ** comma() comma()? {74				let mut named_started = false;75				for arg in &args {76					named_started = named_started || arg.0.is_some();77					assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");78				}79				expr::ArgsDesc(args)80			}81			/ { expr::ArgsDesc(Vec::new()) }8283		pub rule bind(s: &ParserSettings) -> expr::BindSpec84			= name:id() _ "=" _ expr:expr(s) {expr::BindSpec{name, params: None, value: expr}}85			/ name:id() _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name, params: Some(params), value: expr}}86		pub rule assertion(s: &ParserSettings) -> expr::AssertStmt87			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }88		pub rule string() -> String89			= v:("\"" str:$(("\\\"" / !['"'][_])*) "\"" {str.to_owned()}90			/ "'" str:$((!['\''][_])*) "'" {str.to_owned()}) {v.replace("\\n", "\n")}91		pub rule field_name(s: &ParserSettings) -> expr::FieldName92			= name:id() {expr::FieldName::Fixed(name)}93			/ name:string() {expr::FieldName::Fixed(name)}94			/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}95		pub rule visibility() -> expr::Visibility96			= ":::" {expr::Visibility::Unhide}97			/ "::" {expr::Visibility::Hidden}98			/ ":" {expr::Visibility::Normal}99		pub rule field(s: &ParserSettings) -> expr::FieldMember100			= name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{101				name,102				plus: plus.is_some(),103				params: None,104				visibility,105				value,106			}}107			/ name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{108				name,109				plus: false,110				params: Some(params),111				visibility,112				value,113			}}114		pub rule obj_local(s: &ParserSettings) -> BindSpec115			= keyword("local") _ bind:bind(s) {bind}116		pub rule member(s: &ParserSettings) -> expr::Member117			= bind:obj_local(s) {expr::Member::BindStmt(bind)}118			/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}119			/ field:field(s) {expr::Member::Field(field)}120		pub rule objinside(s: &ParserSettings) -> expr::ObjBody121			= pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ first:forspec(s) rest:(_ rest:compspec(s) {rest})? {122				expr::ObjBody::ObjComp {123					pre_locals,124					key,125					value,126					post_locals,127					first,128					rest: rest.unwrap_or_default(),129				}130			}131			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}132		pub rule ifspec(s: &ParserSettings) -> IfSpecData133			= keyword("if") _ expr:expr(s) {IfSpecData(expr)}134		pub rule forspec(s: &ParserSettings) -> ForSpecData135			= keyword("for") _ id:id() _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}136		pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>137			= s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} )+ {s}138		pub rule local_expr(s: &ParserSettings) -> LocExpr139			= l(s,<keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }>)140		pub rule string_expr(s: &ParserSettings) -> LocExpr141			= l(s, <s:string() {Expr::Str(s)}>)142		pub rule obj_expr(s: &ParserSettings) -> LocExpr143			= l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)144		pub rule array_expr(s: &ParserSettings) -> LocExpr145			= l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)146		pub rule array_comp_expr(s: &ParserSettings) -> LocExpr147			= l(s,<"[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {Expr::ArrComp(expr, [vec![CompSpec::ForSpec(forspec)], others.unwrap_or_default()].concat())}>)148		pub rule number_expr(s: &ParserSettings) -> LocExpr149			= l(s,<n:number() { expr::Expr::Num(n) }>)150		pub rule var_expr(s: &ParserSettings) -> LocExpr151			= l(s,<n:id() { expr::Expr::Var(n) }>)152		pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr153			= l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{154				cond,155				cond_then,156				cond_else,157			}}>)158159		pub rule literal(s: &ParserSettings) -> LocExpr160			= l(s,<v:(161				keyword("null") {LiteralType::Null}162				/ keyword("true") {LiteralType::True}163				/ keyword("false") {LiteralType::False}164				/ keyword("self") {LiteralType::This}165				/ keyword("$") {LiteralType::Dollar}166				/ keyword("super") {LiteralType::Super}167			) {Expr::Literal(v)}>)168169		pub rule expr_basic(s: &ParserSettings) -> LocExpr170			= literal(s)171172			/ string_expr(s) / number_expr(s)173			/ array_expr(s)174			/ obj_expr(s)175			/ array_expr(s)176			/ array_comp_expr(s)177178			/ var_expr(s)179			/ local_expr(s)180			/ if_then_else_expr(s)181182			/ l(s,<keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}>)183			/ l(s,<assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }>)184185			/ l(s,<keyword("error") _ expr:expr(s) { Expr::Error(expr) }>)186187		rule expr_basic_with_suffix(s: &ParserSettings) -> LocExpr188			= a:expr_basic(s) suffixes:(_ suffix:l_expr_suffix(s) {suffix})* {189				let mut cur = a;190				for suffix in suffixes {191					let LocSuffix(suffix, location) = suffix;192					cur = LocExpr(Rc::new(match suffix {193						Suffix::String(index) => Expr::Index(cur, loc_expr!(Expr::Str(index), s.loc_data, (s.file_name.clone(), location.1, location.2))),194						Suffix::Slice(desc) => Expr::Slice(cur, desc),195						Suffix::Expression(index) => Expr::Index(cur, index),196						Suffix::Apply(args) => Expr::Apply(cur, args),197						Suffix::Extend(body) => Expr::ObjExtend(cur, body),198					}), if s.loc_data { Some(Rc::new(location)) } else { None })199				}200				cur201			}202203		pub rule slice_desc(s: &ParserSettings) -> SliceDesc204			= start:expr(s)? _ ":" _ pair:(end:expr(s)? _ step:(":" _ e:expr(s) {e})? {(end, step)})? {205				if let Some((end, step)) = pair {206					SliceDesc { start, end, step }207				}else{208					SliceDesc { start, end: None, step: None }209				}210			}211212		rule expr_suffix(s: &ParserSettings) -> Suffix213			= "." _ s:id() { Suffix::String(s) }214			/ "[" _ s:slice_desc(s) _ "]" { Suffix::Slice(s) }215			/ "[" _ s:expr(s) _ "]" { Suffix::Expression(s) }216			/ "(" _ args:args(s) _ ")" (_ keyword("tailstrict"))? { Suffix::Apply(args) }217			/ "{" _ body:objinside(s) _ "}" { Suffix::Extend(body) }218		rule l_expr_suffix(s: &ParserSettings) -> LocSuffix219			= start:position!() suffix:expr_suffix(s) end:position!() {LocSuffix(suffix, ExprLocation(s.file_name.clone(), start, end))}220221		rule expr(s: &ParserSettings) -> LocExpr222			= start:position!() a:precedence! {223				a:(@) _ "||" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}224				--225				a:(@) _ "&&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}226				--227				a:(@) _ "|" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}228				--229				a:@ _ "^" _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}230				--231				a:(@) _ "&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}232				--233				a:(@) _ "==" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Eq, b))}234				a:(@) _ "!=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Ne, b))}235				--236				a:(@) _ "<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}237				a:(@) _ ">" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}238				a:(@) _ "<=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}239				a:(@) _ ">=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}240				--241				a:(@) _ "<<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}242				a:(@) _ ">>" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}243				--244				a:(@) _ "+" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}245				a:(@) _ "-" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}246				--247				a:(@) _ "*" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}248				a:(@) _ "/" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}249				a:(@) _ "%" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mod, b))}250				--251				e:expr_basic_with_suffix(s) {e}252				"-" _ expr:expr(s) { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, expr)) }253				"!" _ expr:expr(s) { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, expr)) }254				"~" _ expr:expr(s) { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, expr)) }255				"(" _ e:expr(s) _ ")" {loc_expr_todo!(Expr::Parened(e))}256			} end:position!() {257				let LocExpr(e, _) = a;258				LocExpr(e, if s.loc_data {259					Some(Rc::new(ExprLocation(s.file_name.to_owned(), start, end)))260				} else {261					None262				})263			}264			/ e:expr_basic_with_suffix(s) {e}265266		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}267	}268}269270pub fn parse(271	str: &str,272	settings: &ParserSettings,273) -> Result<LocExpr, peg::error::ParseError<peg::str::LineCol>> {274	jsonnet_parser::jsonnet(str, settings)275}276277#[macro_export]278macro_rules! el {279	($expr:expr) => {280		LocExpr(std::rc::Rc::new($expr), None)281	};282}283284#[cfg(test)]285pub mod tests {286	use super::{expr::*, parse};287	use crate::ParserSettings;288289	macro_rules! parse {290		($s:expr) => {291			parse(292				$s,293				&ParserSettings {294					loc_data: false,295					file_name: "test.jsonnet".to_owned(),296					},297				)298			.unwrap()299		};300	}301302	mod expressions {303		use super::*;304305		pub fn basic_math() -> LocExpr {306			el!(Expr::BinaryOp(307				el!(Expr::Num(2.0)),308				BinaryOpType::Add,309				el!(Expr::BinaryOp(310					el!(Expr::Num(2.0)),311					BinaryOpType::Mul,312					el!(Expr::Num(2.0)),313				)),314			))315		}316	}317318	#[test]319	fn empty_object() {320		assert_eq!(parse!("{}"), el!(Expr::Obj(ObjBody::MemberList(vec![]))));321	}322323	#[test]324	fn basic_math() {325		assert_eq!(326			parse!("2+2*2"),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 basic_math_with_indents() {341		assert_eq!(parse!("2	+ 	  2	  *	2   	"), expressions::basic_math());342	}343344	#[test]345	fn basic_math_parened() {346		assert_eq!(347			parse!("2+(2+2*2)"),348			el!(Expr::BinaryOp(349				el!(Expr::Num(2.0)),350				BinaryOpType::Add,351				el!(Expr::Parened(expressions::basic_math())),352			))353		);354	}355356	/// Comments should not affect parsing357	#[test]358	fn comments() {359		assert_eq!(360			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),361			el!(Expr::BinaryOp(362				el!(Expr::Num(2.0)),363				BinaryOpType::Add,364				el!(Expr::BinaryOp(365					el!(Expr::Num(3.0)),366					BinaryOpType::Mul,367					el!(Expr::Num(4.0))368				))369			))370		);371	}372373	/// Comments should be able to be escaped374	#[test]375	fn comment_escaping() {376		assert_eq!(377			parse!("2/*\\*/+*/ - 22"),378			el!(Expr::BinaryOp(379				el!(Expr::Num(2.0)),380				BinaryOpType::Sub,381				el!(Expr::Num(22.0))382			))383		);384	}385386	#[test]387	fn suffix_comparsion() {388		use Expr::*;389		assert_eq!(390			parse!("std.type(a) == \"string\""),391			el!(BinaryOp(392				el!(Apply(393					el!(Index(394						el!(Var("std".to_owned())),395						el!(Str("type".to_owned()))396					)),397					ArgsDesc(vec![Arg(None, el!(Var("a".to_owned())))])398				)),399				BinaryOpType::Eq,400				el!(Str("string".to_owned()))401			))402		);403	}404405	#[test]406	fn array_comp() {407		use Expr::*;408		assert_eq!(409			parse!("[std.deepJoin(x) for x in arr]"),410			el!(ArrComp(411				el!(Apply(412					el!(Index(413						el!(Var("std".to_owned())),414						el!(Str("deepJoin".to_owned()))415					)),416					ArgsDesc(vec![Arg(None, el!(Var("x".to_owned())))])417				)),418				vec![CompSpec::ForSpec(ForSpecData(419					"x".to_owned(),420					el!(Var("arr".to_owned()))421				))]422			)),423		)424	}425426	#[test]427	fn array_comp_with_ifs() {428		use Expr::*;429		assert_eq!(430			parse!("[k for k in std.objectFields(patch) if patch[k] == null]"),431			el!(ArrComp(432				el!(Var("k".to_owned())),433				vec![434					CompSpec::ForSpec(ForSpecData(435						"k".to_owned(),436						el!(Apply(437							el!(Index(438								el!(Var("std".to_owned())),439								el!(Str("objectFields".to_owned()))440							)),441							ArgsDesc(vec![Arg(None, el!(Var("patch".to_owned())))])442						))443					)),444					CompSpec::IfSpec(IfSpecData(el!(BinaryOp(445						el!(Index(446							el!(Var("patch".to_owned())),447							el!(Var("k".to_owned()))448						)),449						BinaryOpType::Eq,450						el!(Literal(LiteralType::Null))451					))))452				]453			))454		);455	}456457	#[test]458	fn reserved() {459		use Expr::*;460		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null)));461		assert_eq!(parse!("nulla"), el!(Var("nulla".to_owned())));462	}463464	#[test]465	fn multiple_args_buf() {466		parse!("a(b, null_fields)");467	}468469	#[test]470	fn infix_precedence() {471		use Expr::*;472		assert_eq!(473			parse!("!a && !b"),474			el!(BinaryOp(475				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned())))),476				BinaryOpType::And,477				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".to_owned()))))478			))479		);480	}481482	#[test]483	fn double_negation() {484		use Expr::*;485		assert_eq!(486			parse!("!!a"),487			el!(UnaryOp(488				UnaryOpType::Not,489				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned()))))490			))491		)492	}493494	#[test]495	fn can_parse_stdlib() {496		parse!(jsonnet_stdlib::STDLIB_STR);497	}498499	use test::Bencher;500501	// From source code502	#[bench]503	fn bench_parse_peg(b: &mut Bencher) {504		b.iter(|| parse!(jsonnet_stdlib::STDLIB_STR))505	}506507	// From serialized blob508	#[bench]509	fn bench_parse_serde_bincode(b: &mut Bencher) {510		let serialized = bincode::serialize(&parse!(jsonnet_stdlib::STDLIB_STR)).unwrap();511		b.iter(|| bincode::deserialize::<LocExpr>(&serialized))512	}513}