git.delta.rocks / jrsonnet / refs/commits / 46b4dd0e38ba

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() -> 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			/ 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}