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

difftreelog

source

crates/jrsonnet-parser/src/lib.rs19.5 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 jrsonnet_interner::IStr;11pub use peg;12mod unescape;1314pub struct ParserSettings {15	pub file_name: Rc<Path>,16}1718macro_rules! expr_bin {19	($a:ident $op:ident $b:ident) => {20		Expr::BinaryOp($a, $op, $b)21	};22}23macro_rules! expr_un {24	($op:ident $a:ident) => {25		Expr::UnaryOp($op, $a)26	};27}2829parser! {30	grammar jsonnet_parser() for str {31		use peg::ParseLiteral;3233		rule eof() = quiet!{![_]} / expected!("<eof>")34		rule eol() = "\n" / eof()3536		/// Standard C-like comments37		rule comment()38			= "//" (!eol()[_])* eol()39			/ "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"40			/ "#" (!eol()[_])* eol()4142		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")43		rule _() = single_whitespace()*4445		/// For comma-delimited elements46		rule comma() = quiet!{_ "," _} / expected!("<comma>")47		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}48		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}49		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']50		/// Sequence of digits51		rule uint_str() -> &'input str = a:$(digit()+) { a }52		/// Number in scientific notation format53		rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")5455		/// Reserved word followed by any non-alphanumberic56		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()57		rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")5859		rule keyword(id: &'static str) -> ()60			= ##parse_string_literal(id) end_of_ident()6162		pub rule param(s: &ParserSettings) -> expr::Param = name:$(id()) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name.into(), expr) }63		pub rule params(s: &ParserSettings) -> expr::ParamsDesc64			= params:param(s) ** comma() comma()? { expr::ParamsDesc(Rc::new(params)) }65			/ { expr::ParamsDesc(Rc::new(Vec::new())) }6667		pub rule arg(s: &ParserSettings) -> (Option<IStr>, LocExpr)68			= quiet! { name:(s:$(id()) _ "=" _ {s})? expr:expr(s) {(name.map(Into::into), expr)} }69			/ expected!("<argument>")7071		pub rule args(s: &ParserSettings) -> expr::ArgsDesc72			= args:arg(s)**comma() comma()? {?73				let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();74				let mut unnamed = Vec::with_capacity(unnamed_count);75				let mut named = Vec::with_capacity(args.len() - unnamed_count);76				let mut named_started = false;77				for (name, value) in args {78					if let Some(name) = name {79						named_started = true;80						named.push((name, value));81					} else {82						if named_started {83							return Err("<named argument>")84						}85						unnamed.push(value);86					}87				}88				Ok(expr::ArgsDesc::new(unnamed, named))89			}9091		pub rule bind(s: &ParserSettings) -> expr::BindSpec92			= name:$(id()) _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: None, value: expr}}93			/ name:$(id()) _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: Some(params), value: expr}}94		pub rule assertion(s: &ParserSettings) -> expr::AssertStmt95			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }9697		pub rule whole_line() -> &'input str98			= str:$((!['\n'][_])* "\n") {str}99		pub rule string_block() -> String100			= "|||" (!['\n']single_whitespace())* "\n"101			  empty_lines:$(['\n']*)102			  prefix:[' ' | '\t']+ first_line:whole_line()103			  lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*104			  [' ' | '\t']*<, {prefix.len() - 1}> "|||"105			  {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}106107		rule hex_char()108			= quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")109110		rule string_char(c: rule<()>)111			= (!['\\']!c()[_])+112			/ "\\\\"113			/ "\\u" hex_char() hex_char() hex_char() hex_char()114			/ "\\x" hex_char() hex_char()115			/ ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't'] / c() } / expected!("<escape character>"))116		pub rule string() -> String117			= ['"'] str:$(string_char(<['"']>)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}118			/ ['\''] str:$(string_char(<['\'']>)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}119			/ quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}120			/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}121			/ string_block() } / expected!("<string>")122123		pub rule field_name(s: &ParserSettings) -> expr::FieldName124			= name:$(id()) {expr::FieldName::Fixed(name.into())}125			/ name:string() {expr::FieldName::Fixed(name.into())}126			/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}127		pub rule visibility() -> expr::Visibility128			= ":::" {expr::Visibility::Unhide}129			/ "::" {expr::Visibility::Hidden}130			/ ":" {expr::Visibility::Normal}131		pub rule field(s: &ParserSettings) -> expr::FieldMember132			= name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{133				name,134				plus: plus.is_some(),135				params: None,136				visibility,137				value,138			}}139			/ name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{140				name,141				plus: false,142				params: Some(params),143				visibility,144				value,145			}}146		pub rule obj_local(s: &ParserSettings) -> BindSpec147			= keyword("local") _ bind:bind(s) {bind}148		pub rule member(s: &ParserSettings) -> expr::Member149			= bind:obj_local(s) {expr::Member::BindStmt(bind)}150			/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}151			/ field:field(s) {expr::Member::Field(field)}152		pub rule objinside(s: &ParserSettings) -> expr::ObjBody153			= 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})? {154				let mut compspecs = vec![CompSpec::ForSpec(forspec)];155				compspecs.extend(others.unwrap_or_default());156				expr::ObjBody::ObjComp(expr::ObjComp{157					pre_locals,158					key,159					plus: plus.is_some(),160					value,161					post_locals,162					compspecs,163				})164			}165			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}166		pub rule ifspec(s: &ParserSettings) -> IfSpecData167			= keyword("if") _ expr:expr(s) {IfSpecData(expr)}168		pub rule forspec(s: &ParserSettings) -> ForSpecData169			= keyword("for") _ id:$(id()) _ keyword("in") _ cond:expr(s) {ForSpecData(id.into(), cond)}170		pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>171			= s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}172		pub rule local_expr(s: &ParserSettings) -> Expr173			= keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }174		pub rule string_expr(s: &ParserSettings) -> Expr175			= s:string() {Expr::Str(s.into())}176		pub rule obj_expr(s: &ParserSettings) -> Expr177			= "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}178		pub rule array_expr(s: &ParserSettings) -> Expr179			= "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}180		pub rule array_comp_expr(s: &ParserSettings) -> Expr181			= "[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {182				let mut specs = vec![CompSpec::ForSpec(forspec)];183				specs.extend(others.unwrap_or_default());184				Expr::ArrComp(expr, specs)185			}186		pub rule number_expr(s: &ParserSettings) -> Expr187			= n:number() { expr::Expr::Num(n) }188		pub rule var_expr(s: &ParserSettings) -> Expr189			= n:$(id()) { expr::Expr::Var(n.into()) }190		pub rule id_loc(s: &ParserSettings) -> LocExpr191			= a:position!() n:$(id()) b:position!() { LocExpr(Rc::new(expr::Expr::Str(n.into())), ExprLocation(s.file_name.clone(), a,b)) }192		pub rule if_then_else_expr(s: &ParserSettings) -> Expr193			= cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{194				cond,195				cond_then,196				cond_else,197			}}198199		pub rule literal(s: &ParserSettings) -> Expr200			= v:(201				keyword("null") {LiteralType::Null}202				/ keyword("true") {LiteralType::True}203				/ keyword("false") {LiteralType::False}204				/ keyword("self") {LiteralType::This}205				/ keyword("$") {LiteralType::Dollar}206				/ keyword("super") {LiteralType::Super}207			) {Expr::Literal(v)}208209		pub rule expr_basic(s: &ParserSettings) -> Expr210			= literal(s)211212			/ quiet!{"$intrinsic(" name:$(id()) ")" {Expr::Intrinsic(name.into())}}213214			/ string_expr(s) / number_expr(s)215			/ array_expr(s)216			/ obj_expr(s)217			/ array_expr(s)218			/ array_comp_expr(s)219220			/ keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}221			/ keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}222223			/ var_expr(s)224			/ local_expr(s)225			/ if_then_else_expr(s)226227			/ keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}228			/ assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }229230			/ keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }231232		rule slice_part(s: &ParserSettings) -> Option<LocExpr>233			= e:(_ e:expr(s) _{e})? {e}234		pub rule slice_desc(s: &ParserSettings) -> SliceDesc235			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {236				let (end, step) = if let Some((end, step)) = pair {237					(end, step)238				}else{239					(None, None)240				};241242				SliceDesc { start, end, step }243			}244245		rule binop(x: rule<()>) -> ()246			= quiet!{ x() } / expected!("<binary op>")247		rule unaryop(x: rule<()>) -> ()248			= quiet!{ x() } / expected!("<unary op>")249250251		use BinaryOpType::*;252		use UnaryOpType::*;253		rule expr(s: &ParserSettings) -> LocExpr254			= precedence! {255				start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.file_name.clone(), start, end)) }256				--257				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}258				--259				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}260				--261				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}262				--263				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}264				--265				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}266				--267				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}268				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}269				--270				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}271				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}272				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}273				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}274				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}275				--276				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}277				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}278				--279				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}280				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}281				--282				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}283				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}284				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}285				--286						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}287						unaryop(<"!">) _ b:@ {expr_un!(Not b)}288						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}289				--290				a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}291				a:(@) _ "." _ a:position!() e:id_loc(s) b:position!() {Expr::Index(a, e)}292				a:(@) _ "[" _ e:expr(s) _ "]" {Expr::Index(a, e)}293				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}294				a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}295				--296				e:expr_basic(s) {e}297				"(" _ e:expr(s) _ ")" {Expr::Parened(e)}298			}299300		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}301	}302}303304pub type ParseError = peg::error::ParseError<peg::str::LineCol>;305pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {306	jsonnet_parser::jsonnet(str, settings)307}308309#[cfg(test)]310pub mod tests {311	use super::{expr::*, parse};312	use crate::ParserSettings;313	use std::path::PathBuf;314	use BinaryOpType::*;315316	macro_rules! parse {317		($s:expr) => {318			parse(319				$s,320				&ParserSettings {321					file_name: PathBuf::from("test.jsonnet").into(),322				},323			)324			.unwrap()325		};326	}327328	macro_rules! el {329		($expr:expr, $from:expr, $to:expr$(,)?) => {330			LocExpr(331				std::rc::Rc::new($expr),332				ExprLocation(PathBuf::from("test.jsonnet").into(), $from, $to),333			)334		};335	}336337	#[test]338	fn multiline_string() {339		assert_eq!(340			parse!("|||\n    Hello world!\n     a\n|||"),341			el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),342		);343		assert_eq!(344			parse!("|||\n  Hello world!\n   a\n|||"),345			el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),346		);347		assert_eq!(348			parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),349			el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),350		);351		assert_eq!(352			parse!("|||\n   Hello world!\n    a\n |||"),353			el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),354		);355	}356357	#[test]358	fn slice() {359		parse!("a[1:]");360		parse!("a[1::]");361		parse!("a[:1:]");362		parse!("a[::1]");363		parse!("str[:len - 1]");364	}365366	#[test]367	fn string_escaping() {368		assert_eq!(369			parse!(r#""Hello, \"world\"!""#),370			el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),371		);372		assert_eq!(373			parse!(r#"'Hello \'world\'!'"#),374			el!(Expr::Str("Hello 'world'!".into()), 0, 18),375		);376		assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));377	}378379	#[test]380	fn string_unescaping() {381		assert_eq!(382			parse!(r#""Hello\nWorld""#),383			el!(Expr::Str("Hello\nWorld".into()), 0, 14),384		);385	}386387	#[test]388	fn string_verbantim() {389		assert_eq!(390			parse!(r#"@"Hello\n""World""""#),391			el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),392		);393	}394395	#[test]396	fn imports() {397		assert_eq!(398			parse!("import \"hello\""),399			el!(Expr::Import(PathBuf::from("hello")), 0, 14),400		);401		assert_eq!(402			parse!("importstr \"garnish.txt\""),403			el!(Expr::ImportStr(PathBuf::from("garnish.txt")), 0, 23)404		);405	}406407	#[test]408	fn empty_object() {409		assert_eq!(410			parse!("{}"),411			el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)412		);413	}414415	#[test]416	fn basic_math() {417		assert_eq!(418			parse!("2+2*2"),419			el!(420				Expr::BinaryOp(421					el!(Expr::Num(2.0), 0, 1),422					Add,423					el!(424						Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),425						2,426						5427					)428				),429				0,430				5431			)432		);433	}434435	#[test]436	fn basic_math_with_indents() {437		assert_eq!(438			parse!("2	+ 	  2	  *	2   	"),439			el!(440				Expr::BinaryOp(441					el!(Expr::Num(2.0), 0, 1),442					Add,443					el!(444						Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),445						7,446						14447					),448				),449				0,450				14451			)452		);453	}454455	#[test]456	fn basic_math_parened() {457		assert_eq!(458			parse!("2+(2+2*2)"),459			el!(460				Expr::BinaryOp(461					el!(Expr::Num(2.0), 0, 1),462					Add,463					el!(464						Expr::Parened(el!(465							Expr::BinaryOp(466								el!(Expr::Num(2.0), 3, 4),467								Add,468								el!(469									Expr::BinaryOp(470										el!(Expr::Num(2.0), 5, 6),471										Mul,472										el!(Expr::Num(2.0), 7, 8),473									),474									5,475									8476								),477							),478							3,479							8480						)),481						2,482						9483					),484				),485				0,486				9487			)488		);489	}490491	/// Comments should not affect parsing492	#[test]493	fn comments() {494		assert_eq!(495			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),496			el!(497				Expr::BinaryOp(498					el!(Expr::Num(2.0), 0, 1),499					Add,500					el!(501						Expr::BinaryOp(502							el!(Expr::Num(3.0), 22, 23),503							Mul,504							el!(Expr::Num(4.0), 40, 41)505						),506						22,507						41508					)509				),510				0,511				41512			)513		);514	}515516	/// Comments should be able to be escaped517	#[test]518	fn comment_escaping() {519		assert_eq!(520			parse!("2/*\\*/+*/ - 22"),521			el!(522				Expr::BinaryOp(el!(Expr::Num(2.0), 0, 1), Sub, el!(Expr::Num(22.0), 12, 14)),523				0,524				14525			)526		);527	}528529	#[test]530	fn suffix() {531		// assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));532		// assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));533		// assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));534		// assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))535	}536537	#[test]538	fn array_comp() {539		use Expr::*;540		/*541		`ArrComp(Apply(Index(Var("std") from "test.jsonnet":1-4, Var("deepJoin") from "test.jsonnet":5-13) from "test.jsonnet":1-13, ArgsDesc { unnamed: [Var("x") from "test.jsonnet":14-15], named: [] }, false) from "test.jsonnet":1-16, [ForSpec(ForSpecData("x", Var("arr") from "test.jsonnet":26-29))]) from "test.jsonnet":0-30`,542		`ArrComp(Apply(Index(Var("std") from "test.jsonnet":1-4, Str("deepJoin") from "test.jsonnet":5-13) from "test.jsonnet":1-13, ArgsDesc { unnamed: [Var("x") from "test.jsonnet":14-15], named: [] }, false) from "test.jsonnet":1-16, [ForSpec(ForSpecData("x", Var("arr") from "test.jsonnet":26-29))]) from "test.jsonnet":0-30`543				*/544		assert_eq!(545			parse!("[std.deepJoin(x) for x in arr]"),546			el!(547				ArrComp(548					el!(549						Apply(550							el!(551								Index(552									el!(Var("std".into()), 1, 4),553									el!(Str("deepJoin".into()), 5, 13)554								),555								1,556								13557							),558							ArgsDesc::new(vec![el!(Var("x".into()), 14, 15)], vec![]),559							false,560						),561						1,562						16563					),564					vec![CompSpec::ForSpec(ForSpecData(565						"x".into(),566						el!(Var("arr".into()), 26, 29)567					))]568				),569				0,570				30571			),572		)573	}574575	#[test]576	fn reserved() {577		use Expr::*;578		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null), 0, 4));579		assert_eq!(parse!("nulla"), el!(Var("nulla".into()), 0, 5));580	}581582	#[test]583	fn multiple_args_buf() {584		parse!("a(b, null_fields)");585	}586587	#[test]588	fn infix_precedence() {589		use Expr::*;590		assert_eq!(591			parse!("!a && !b"),592			el!(593				BinaryOp(594					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),595					And,596					el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 7, 8)), 6, 8)597				),598				0,599				8600			)601		);602	}603604	#[test]605	fn infix_precedence_division() {606		use Expr::*;607		assert_eq!(608			parse!("!a / !b"),609			el!(610				BinaryOp(611					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),612					Div,613					el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 6, 7)), 5, 7)614				),615				0,616				7617			)618		);619	}620621	#[test]622	fn double_negation() {623		use Expr::*;624		assert_eq!(625			parse!("!!a"),626			el!(627				UnaryOp(628					UnaryOpType::Not,629					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 2, 3)), 1, 3)630				),631				0,632				3633			)634		)635	}636637	#[test]638	fn array_test_error() {639		parse!("[a for a in b if c for e in f]");640		//                    ^^^^ failed code641	}642643	#[test]644	fn missing_newline_between_comment_and_eof() {645		parse!(646			"{a:1}647648			//+213"649		);650	}651652	#[test]653	fn default_param_before_nondefault() {654		parse!("local x(foo = 'foo', bar) = null; null");655	}656657	#[test]658	fn can_parse_stdlib() {659		parse!(jrsonnet_stdlib::STDLIB_STR);660	}661662	#[test]663	fn add_location_info_to_all_sub_expressions() {664		use Expr::*;665666		let file_name: std::rc::Rc<std::path::Path> = PathBuf::from("test.jsonnet").into();667		let expr = parse(668			"{} { local x = 1, x: x } + {}",669			&ParserSettings {670				file_name: file_name.clone(),671			},672		)673		.unwrap();674		assert_eq!(675			expr,676			el!(677				BinaryOp(678					el!(679						ObjExtend(680							el!(Obj(ObjBody::MemberList(vec![])), 0, 2),681							ObjBody::MemberList(vec![682								Member::BindStmt(BindSpec {683									name: "x".into(),684									params: None,685									value: el!(Num(1.0), 15, 16)686								}),687								Member::Field(FieldMember {688									name: FieldName::Fixed("x".into()),689									plus: false,690									params: None,691									visibility: Visibility::Normal,692									value: el!(Var("x".into()), 21, 22),693								})694							])695						),696						0,697						24698					),699					BinaryOpType::Add,700					el!(Obj(ObjBody::MemberList(vec![])), 27, 29),701				),702				0,703				29704			),705		);706	}707	// From source code708	/*709	#[bench]710	fn bench_parse_peg(b: &mut Bencher) {711		b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))712	}713	*/714}