git.delta.rocks / jrsonnet / refs/commits / 1d4b842070e4

difftreelog

build stable rustc support

Lach2020-08-03parent: #bb6d643.patch.diff
in: master

7 files changed

modifiedbindings/jsonnet/src/lib.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -1,5 +1,3 @@
-#![feature(custom_inner_attributes)]
-
 pub mod import;
 pub mod interop;
 pub mod val_extract;
modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -20,6 +20,9 @@
 # Rustc-like trace visualization
 explaining-traces = ["annotate-snippets"]
 
+# Unlocks extra features, but works only on unstable
+unstable = []
+
 [dependencies]
 jrsonnet-parser = { path = "../jrsonnet-parser", version = "1.0.0" }
 jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "1.0.0" }
modifiedcrates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -2,12 +2,7 @@
 	error::Error::*, future_wrapper, map::LayeredHashMap, rc_fn_helper, resolved_lazy_val,
 	LazyBinding, LazyVal, ObjValue, Result, Val,
 };
-use std::{
-	cell::RefCell,
-	collections::HashMap,
-	fmt::Debug,
-	rc::{Rc, Weak},
-};
+use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
 
 rc_fn_helper!(
 	ContextCreator,
@@ -138,6 +133,7 @@
 		}
 		Ok(self.extend(new, new_dollar, this, super_obj))
 	}
+	#[cfg(feature = "unstable")]
 	pub fn into_weak(self) -> WeakContext {
 		WeakContext(Rc::downgrade(&self.0))
 	}
@@ -155,13 +151,16 @@
 	}
 }
 
+#[cfg(feature = "unstable")]
 #[derive(Debug, Clone)]
-pub struct WeakContext(Weak<ContextInternals>);
+pub struct WeakContext(std::rc::Weak<ContextInternals>);
+#[cfg(feature = "unstable")]
 impl WeakContext {
 	pub fn upgrade(&self) -> Context {
 		Context(self.0.upgrade().expect("context is removed"))
 	}
 }
+#[cfg(feature = "unstable")]
 impl PartialEq for WeakContext {
 	fn eq(&self, other: &Self) -> bool {
 		self.0.ptr_eq(&other.0)
modifiedcrates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -390,10 +390,16 @@
 
 /// Extracts code block and disables inlining for them
 /// Fixes WASM to java bytecode compilation failing because of very large method
+#[cfg(feature = "unstable")]
+macro_rules! noinline {
+	($e:expr) => {
+		(#![inline(never)] move || $e)()
+	};
+}
+#[cfg(not(feature = "unstable"))]
 macro_rules! noinline {
 	($e:expr) => {
-		(#[inline(never)]
-		move || $e)()
+		(move || $e)()
 	};
 }
 
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -1,12 +1,6 @@
-#![feature(box_syntax, box_patterns)]
-#![feature(type_alias_impl_trait)]
-#![feature(debug_non_exhaustive)]
-#![feature(test)]
-#![feature(stmt_expr_attributes)]
+#![cfg_attr(feature = "unstable", feature(stmt_expr_attributes))]
 #![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]
 
-extern crate test;
-
 mod builtin;
 mod ctx;
 mod dynamic;
@@ -820,8 +814,6 @@
 		"#
 		);
 	}
-
-	use test::Bencher;
 
 	// This test is commented out by default, because of huge compilation slowdown
 	// #[bench]
@@ -836,6 +828,7 @@
 	// 	})
 	// }
 
+	/*
 	#[bench]
 	fn bench_serialize(b: &mut Bencher) {
 		b.iter(|| {
@@ -859,6 +852,7 @@
 			)
 		})
 	}
+	*/
 
 	#[test]
 	fn equality() {
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -35,7 +35,14 @@
 		for (name, member) in self.0.this_entries.iter() {
 			debug.field(name, member);
 		}
-		debug.finish_non_exhaustive()
+		#[cfg(feature = "unstable")]
+		{
+			debug.finish_non_exhaustive()
+		}
+		#[cfg(not(feature = "unstable"))]
+		{
+			debug.finish()
+		}
 	}
 }
 
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-parser/src/lib.rs
1#![feature(box_syntax)]2#![feature(test)]34extern crate test;56use peg::parser;7use std::{path::PathBuf, rc::Rc};8mod expr;9pub use expr::*;10pub use peg;1112#[derive(Default)]13pub struct ParserSettings {14	pub loc_data: bool,15	pub file_name: Rc<PathBuf>,16}1718parser! {19	grammar jsonnet_parser() for str {20		use peg::ParseLiteral;2122		/// Standard C-like comments23		rule comment()24			= "//" (!['\n'][_])* "\n"25			/ "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"26			/ "#" (!['\n'][_])* "\n"2728		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")29		rule _() = single_whitespace()*3031		/// For comma-delimited elements32		rule comma() = quiet!{_ "," _} / expected!("<comma>")33		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}34		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}35		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']36		/// Sequence of digits37		rule uint() -> u64 = a:$(digit()+) { a.parse().unwrap() }38		/// Number in scientific notation format39		rule number() -> f64 = quiet!{a:$(uint() ("." uint())? (['e'|'E'] (s:['+'|'-'])? uint())?) { a.parse().unwrap() }} / expected!("<number>")4041		/// Reserved word followed by any non-alphanumberic42		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()43		rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")4445		rule keyword(id: &'static str)46			= ##parse_string_literal(id) end_of_ident()47		// Adds location data information to existing expression48		rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr49			= start:position!() v:x() end:position!() {loc_expr!(v, s.loc_data, (s.file_name.clone(), start, end))}5051		pub rule param(s: &ParserSettings) -> expr::Param = name:$(id()) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name.into(), expr) }52		pub rule params(s: &ParserSettings) -> expr::ParamsDesc53			= params:param(s) ** comma() comma()? {54				let mut defaults_started = false;55				for param in &params {56					defaults_started = defaults_started || param.1.is_some();57					assert_eq!(defaults_started, param.1.is_some(), "defauld parameters should be used after all positionals");58				}59				expr::ParamsDesc(Rc::new(params))60			}61			/ { expr::ParamsDesc(Rc::new(Vec::new())) }6263		pub rule arg(s: &ParserSettings) -> expr::Arg64			= name:$(id()) _ "=" _ expr:expr(s) {expr::Arg(Some(name.into()), expr)}65			/ expr:expr(s) {expr::Arg(None, expr)}66		pub rule args(s: &ParserSettings) -> expr::ArgsDesc67			= args:arg(s) ** comma() comma()? {68				let mut named_started = false;69				for arg in &args {70					named_started = named_started || arg.0.is_some();71					assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");72				}73				expr::ArgsDesc(args)74			}75			/ { expr::ArgsDesc(Vec::new()) }7677		pub rule bind(s: &ParserSettings) -> expr::BindSpec78			= name:$(id()) _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: None, value: expr}}79			/ name:$(id()) _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: Some(params), value: expr}}80		pub rule assertion(s: &ParserSettings) -> expr::AssertStmt81			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }8283		pub rule whole_line() -> &'input str84			= str:$((!['\n'][_])* "\n") {str}85		pub rule string_block() -> String86			= "|||" (!['\n']single_whitespace())* "\n"87			  prefix:[' ' | '\t']+ first_line:whole_line()88			  lines:([' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*89			  [' ' | '\t']*<, {prefix.len() - 1}> "|||"90			  {let mut l = first_line.to_owned(); l.extend(lines); l}91		pub rule string() -> String92			= "\"" str:$(("\\\"" / "\\\\" / (!['"'][_]))*) "\"" {unescape::unescape(str).unwrap()}93			/ "'" str:$(("\\'" / "\\\\" / (!['\''][_]))*) "'" {unescape::unescape(str).unwrap()}94			/ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}95			/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}96			/ string_block()9798		pub rule field_name(s: &ParserSettings) -> expr::FieldName99			= name:$(id()) {expr::FieldName::Fixed(name.into())}100			/ name:string() {expr::FieldName::Fixed(name.into())}101			/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}102		pub rule visibility() -> expr::Visibility103			= ":::" {expr::Visibility::Unhide}104			/ "::" {expr::Visibility::Hidden}105			/ ":" {expr::Visibility::Normal}106		pub rule field(s: &ParserSettings) -> expr::FieldMember107			= name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{108				name,109				plus: plus.is_some(),110				params: None,111				visibility,112				value,113			}}114			/ name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{115				name,116				plus: false,117				params: Some(params),118				visibility,119				value,120			}}121		pub rule obj_local(s: &ParserSettings) -> BindSpec122			= keyword("local") _ bind:bind(s) {bind}123		pub rule member(s: &ParserSettings) -> expr::Member124			= bind:obj_local(s) {expr::Member::BindStmt(bind)}125			/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}126			/ field:field(s) {expr::Member::Field(field)}127		pub rule objinside(s: &ParserSettings) -> expr::ObjBody128			= 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})? {129				let mut compspecs = vec![CompSpec::ForSpec(forspec)];130				compspecs.extend(others.unwrap_or_default());131				expr::ObjBody::ObjComp(expr::ObjComp{132					pre_locals,133					key,134					value,135					post_locals,136					compspecs,137				})138			}139			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}140		pub rule ifspec(s: &ParserSettings) -> IfSpecData141			= keyword("if") _ expr:expr(s) {IfSpecData(expr)}142		pub rule forspec(s: &ParserSettings) -> ForSpecData143			= keyword("for") _ id:$(id()) _ keyword("in") _ cond:expr(s) {ForSpecData(id.into(), cond)}144		pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>145			= s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}146		pub rule local_expr(s: &ParserSettings) -> LocExpr147			= l(s,<keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }>)148		pub rule string_expr(s: &ParserSettings) -> LocExpr149			= l(s, <s:string() {Expr::Str(s.into())}>)150		pub rule obj_expr(s: &ParserSettings) -> LocExpr151			= l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)152		pub rule array_expr(s: &ParserSettings) -> LocExpr153			= l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)154		pub rule array_comp_expr(s: &ParserSettings) -> LocExpr155			= l(s,<"[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {156				let mut specs = vec![CompSpec::ForSpec(forspec)];157				specs.extend(others.unwrap_or_default());158				Expr::ArrComp(expr, specs)159			}>)160		pub rule number_expr(s: &ParserSettings) -> LocExpr161			= l(s,<n:number() { expr::Expr::Num(n) }>)162		pub rule var_expr(s: &ParserSettings) -> LocExpr163			= l(s,<n:$(id()) { expr::Expr::Var(n.into()) }>)164		pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr165			= l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{166				cond,167				cond_then,168				cond_else,169			}}>)170171		pub rule literal(s: &ParserSettings) -> LocExpr172			= l(s,<v:(173				keyword("null") {LiteralType::Null}174				/ keyword("true") {LiteralType::True}175				/ keyword("false") {LiteralType::False}176				/ keyword("self") {LiteralType::This}177				/ keyword("$") {LiteralType::Dollar}178				/ keyword("super") {LiteralType::Super}179			) {Expr::Literal(v)}>)180181		pub rule expr_basic(s: &ParserSettings) -> LocExpr182			= literal(s)183184			/ string_expr(s) / number_expr(s)185			/ array_expr(s)186			/ obj_expr(s)187			/ array_expr(s)188			/ array_comp_expr(s)189190			/ l(s,<keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}>)191			/ l(s,<keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}>)192193			/ var_expr(s)194			/ local_expr(s)195			/ if_then_else_expr(s)196197			/ l(s,<keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}>)198			/ l(s,<assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }>)199200			/ l(s,<keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }>)201202		rule slice_part(s: &ParserSettings) -> Option<LocExpr>203			= e:(_ e:expr(s) _{e})? {e}204		pub rule slice_desc(s: &ParserSettings) -> SliceDesc205			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {206				let (end, step) = if let Some((end, step)) = pair {207					(end, step)208				}else{209					(None, None)210				};211212				SliceDesc { start, end, step }213			}214215		rule expr(s: &ParserSettings) -> LocExpr216			= start:position!() a:precedence! {217				a:(@) _ "||" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}218				--219				a:(@) _ "&&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}220				--221				a:(@) _ "|" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}222				--223				a:@ _ "^" _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}224				--225				a:(@) _ "&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}226				--227				a:(@) _ "==" _ b:@ {loc_expr_todo!(Expr::Apply(228					el!(Expr::Index(229						el!(Expr::Var("std".into())),230						el!(Expr::Str("equals".into()))231					)),232					ArgsDesc(vec![Arg(None, a), Arg(None, b)]),233					true234				))}235				a:(@) _ "!=" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, el!(Expr::Apply(236					el!(Expr::Index(237						el!(Expr::Var("std".into())),238						el!(Expr::Str("equals".into()))239					)),240					ArgsDesc(vec![Arg(None, a), Arg(None, b)]),241					true242				))))}243				--244				a:(@) _ "<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}245				a:(@) _ ">" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}246				a:(@) _ "<=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}247				a:(@) _ ">=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}248				a:(@) _ keyword("in") _ b:@ {loc_expr_todo!(Expr::Apply(249					el!(Expr::Index(250						el!(Expr::Var("std".into())),251						el!(Expr::Str("objectHasEx".into()))252					)), ArgsDesc(vec![Arg(None, b), Arg(None, a), Arg(None, el!(Expr::Literal(LiteralType::True)))]),253					true254				))}255				--256				a:(@) _ "<<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}257				a:(@) _ ">>" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}258				--259				a:(@) _ "+" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}260				a:(@) _ "-" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}261				--262				a:(@) _ "*" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}263				a:(@) _ "/" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}264				a:(@) _ "%" _ b:@ {loc_expr_todo!(Expr::Apply(265					el!(Expr::Index(266						el!(Expr::Var("std".into())),267						el!(Expr::Str("mod".into()))268					)), ArgsDesc(vec![Arg(None, a), Arg(None, b)]),269					false270				))}271				--272						"-" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}273						"!" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}274						"~" _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }275				--276				a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Apply(277					el!(Expr::Index(278						el!(Expr::Var("std".into())),279						el!(Expr::Str("slice".into())),280					)),281					ArgsDesc(vec![282						Arg(None, a),283						Arg(None, s.start.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),284						Arg(None, s.end.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),285						Arg(None, s.step.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),286					]),287					true,288				))}289				a:(@) _ "." _ s:$(id()) {loc_expr_todo!(Expr::Index(a, el!(Expr::Str(s.into()))))}290				a:(@) _ "[" _ s:expr(s) _ "]" {loc_expr_todo!(Expr::Index(a, s))}291				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {loc_expr_todo!(Expr::Apply(a, args, ts.is_some()))}292				a:(@) _ "{" _ body:objinside(s) _ "}" {loc_expr_todo!(Expr::ObjExtend(a, body))}293				--294				e:expr_basic(s) {e}295				"(" _ e:expr(s) _ ")" {loc_expr_todo!(Expr::Parened(e))}296			} end:position!() {297				let LocExpr(e, _) = a;298				LocExpr(e, if s.loc_data {299					Some(ExprLocation(s.file_name.clone(), start, end))300				} else {301					None302				})303			}304			/ e:expr_basic(s) {e}305306		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}307	}308}309310pub type ParseError = peg::error::ParseError<peg::str::LineCol>;311pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {312	jsonnet_parser::jsonnet(str, settings)313}314315#[macro_export]316macro_rules! el {317	($expr:expr) => {318		LocExpr(std::rc::Rc::new($expr), None)319	};320}321322#[cfg(test)]323pub mod tests {324	use super::{expr::*, parse};325	use crate::ParserSettings;326	use std::path::PathBuf;327	use std::rc::Rc;328329	macro_rules! parse {330		($s:expr) => {331			parse(332				$s,333				&ParserSettings {334					loc_data: false,335					file_name: Rc::new(PathBuf::from("/test.jsonnet")),336					},337				)338			.unwrap()339		};340	}341342	mod expressions {343		use super::*;344345		pub fn basic_math() -> LocExpr {346			el!(Expr::BinaryOp(347				el!(Expr::Num(2.0)),348				BinaryOpType::Add,349				el!(Expr::BinaryOp(350					el!(Expr::Num(2.0)),351					BinaryOpType::Mul,352					el!(Expr::Num(2.0)),353				)),354			))355		}356	}357358	#[test]359	fn multiline_string() {360		assert_eq!(361			parse!("|||\n    Hello world!\n     a\n|||"),362			el!(Expr::Str("Hello world!\n a\n".into())),363		);364		assert_eq!(365			parse!("|||\n  Hello world!\n   a\n|||"),366			el!(Expr::Str("Hello world!\n a\n".into())),367		);368		assert_eq!(369			parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),370			el!(Expr::Str("Hello world!\n\ta\n".into())),371		);372		assert_eq!(373			parse!("|||\n   Hello world!\n    a\n |||"),374			el!(Expr::Str("Hello world!\n a\n".into())),375		);376	}377378	#[test]379	fn slice() {380		parse!("a[1:]");381		parse!("a[1::]");382		parse!("a[:1:]");383		parse!("a[::1]");384		parse!("str[:len - 1]");385	}386387	#[test]388	fn string_escaping() {389		assert_eq!(390			parse!(r#""Hello, \"world\"!""#),391			el!(Expr::Str(r#"Hello, "world"!"#.into())),392		);393		assert_eq!(394			parse!(r#"'Hello \'world\'!'"#),395			el!(Expr::Str("Hello 'world'!".into())),396		);397		assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into())),);398	}399400	#[test]401	fn string_unescaping() {402		assert_eq!(403			parse!(r#""Hello\nWorld""#),404			el!(Expr::Str("Hello\nWorld".into())),405		);406	}407408	#[test]409	fn string_verbantim() {410		assert_eq!(411			parse!(r#"@"Hello\n""World""""#),412			el!(Expr::Str("Hello\\n\"World\"".into())),413		);414	}415416	#[test]417	fn imports() {418		assert_eq!(419			parse!("import \"hello\""),420			el!(Expr::Import(PathBuf::from("hello"))),421		);422		assert_eq!(423			parse!("importstr \"garnish.txt\""),424			el!(Expr::ImportStr(PathBuf::from("garnish.txt")))425		);426	}427428	#[test]429	fn empty_object() {430		assert_eq!(parse!("{}"), el!(Expr::Obj(ObjBody::MemberList(vec![]))));431	}432433	#[test]434	fn basic_math() {435		assert_eq!(436			parse!("2+2*2"),437			el!(Expr::BinaryOp(438				el!(Expr::Num(2.0)),439				BinaryOpType::Add,440				el!(Expr::BinaryOp(441					el!(Expr::Num(2.0)),442					BinaryOpType::Mul,443					el!(Expr::Num(2.0))444				))445			))446		);447	}448449	#[test]450	fn basic_math_with_indents() {451		assert_eq!(parse!("2	+ 	  2	  *	2   	"), expressions::basic_math());452	}453454	#[test]455	fn basic_math_parened() {456		assert_eq!(457			parse!("2+(2+2*2)"),458			el!(Expr::BinaryOp(459				el!(Expr::Num(2.0)),460				BinaryOpType::Add,461				el!(Expr::Parened(expressions::basic_math())),462			))463		);464	}465466	/// Comments should not affect parsing467	#[test]468	fn comments() {469		assert_eq!(470			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),471			el!(Expr::BinaryOp(472				el!(Expr::Num(2.0)),473				BinaryOpType::Add,474				el!(Expr::BinaryOp(475					el!(Expr::Num(3.0)),476					BinaryOpType::Mul,477					el!(Expr::Num(4.0))478				))479			))480		);481	}482483	/// Comments should be able to be escaped484	#[test]485	fn comment_escaping() {486		assert_eq!(487			parse!("2/*\\*/+*/ - 22"),488			el!(Expr::BinaryOp(489				el!(Expr::Num(2.0)),490				BinaryOpType::Sub,491				el!(Expr::Num(22.0))492			))493		);494	}495496	#[test]497	fn suffix() {498		// assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));499		// assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));500		// assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));501		// assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))502	}503504	#[test]505	fn array_comp() {506		use Expr::*;507		assert_eq!(508			parse!("[std.deepJoin(x) for x in arr]"),509			el!(ArrComp(510				el!(Apply(511					el!(Index(el!(Var("std".into())), el!(Str("deepJoin".into())))),512					ArgsDesc(vec![Arg(None, el!(Var("x".into())))]),513					false,514				)),515				vec![CompSpec::ForSpec(ForSpecData(516					"x".into(),517					el!(Var("arr".into()))518				))]519			)),520		)521	}522523	#[test]524	fn reserved() {525		use Expr::*;526		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null)));527		assert_eq!(parse!("nulla"), el!(Var("nulla".into())));528	}529530	#[test]531	fn multiple_args_buf() {532		parse!("a(b, null_fields)");533	}534535	#[test]536	fn infix_precedence() {537		use Expr::*;538		assert_eq!(539			parse!("!a && !b"),540			el!(BinaryOp(541				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),542				BinaryOpType::And,543				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))544			))545		);546	}547548	#[test]549	fn infix_precedence_division() {550		use Expr::*;551		assert_eq!(552			parse!("!a / !b"),553			el!(BinaryOp(554				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),555				BinaryOpType::Div,556				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))557			))558		);559	}560561	#[test]562	fn double_negation() {563		use Expr::*;564		assert_eq!(565			parse!("!!a"),566			el!(UnaryOp(567				UnaryOpType::Not,568				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()))))569			))570		)571	}572573	#[test]574	fn array_test_error() {575		parse!("[a for a in b if c for e in f]");576		//                    ^^^^ failed code577	}578579	#[test]580	fn can_parse_stdlib() {581		parse!(jrsonnet_stdlib::STDLIB_STR);582	}583584	use test::Bencher;585586	// From source code587	#[bench]588	fn bench_parse_peg(b: &mut Bencher) {589		b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))590	}591}
after · crates/jrsonnet-parser/src/lib.rs
1use peg::parser;2use std::{path::PathBuf, rc::Rc};3mod expr;4pub use expr::*;5pub use peg;67#[derive(Default)]8pub struct ParserSettings {9	pub loc_data: bool,10	pub file_name: Rc<PathBuf>,11}1213parser! {14	grammar jsonnet_parser() for str {15		use peg::ParseLiteral;1617		/// Standard C-like comments18		rule comment()19			= "//" (!['\n'][_])* "\n"20			/ "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"21			/ "#" (!['\n'][_])* "\n"2223		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")24		rule _() = single_whitespace()*2526		/// For comma-delimited elements27		rule comma() = quiet!{_ "," _} / expected!("<comma>")28		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}29		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}30		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']31		/// Sequence of digits32		rule uint() -> u64 = a:$(digit()+) { a.parse().unwrap() }33		/// Number in scientific notation format34		rule number() -> f64 = quiet!{a:$(uint() ("." uint())? (['e'|'E'] (s:['+'|'-'])? uint())?) { a.parse().unwrap() }} / expected!("<number>")3536		/// Reserved word followed by any non-alphanumberic37		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()38		rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")3940		rule keyword(id: &'static str)41			= ##parse_string_literal(id) end_of_ident()42		// Adds location data information to existing expression43		rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr44			= start:position!() v:x() end:position!() {loc_expr!(v, s.loc_data, (s.file_name.clone(), start, end))}4546		pub rule param(s: &ParserSettings) -> expr::Param = name:$(id()) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name.into(), expr) }47		pub rule params(s: &ParserSettings) -> expr::ParamsDesc48			= params:param(s) ** comma() comma()? {49				let mut defaults_started = false;50				for param in &params {51					defaults_started = defaults_started || param.1.is_some();52					assert_eq!(defaults_started, param.1.is_some(), "defauld parameters should be used after all positionals");53				}54				expr::ParamsDesc(Rc::new(params))55			}56			/ { expr::ParamsDesc(Rc::new(Vec::new())) }5758		pub rule arg(s: &ParserSettings) -> expr::Arg59			= name:$(id()) _ "=" _ expr:expr(s) {expr::Arg(Some(name.into()), expr)}60			/ expr:expr(s) {expr::Arg(None, expr)}61		pub rule args(s: &ParserSettings) -> expr::ArgsDesc62			= args:arg(s) ** comma() comma()? {63				let mut named_started = false;64				for arg in &args {65					named_started = named_started || arg.0.is_some();66					assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");67				}68				expr::ArgsDesc(args)69			}70			/ { expr::ArgsDesc(Vec::new()) }7172		pub rule bind(s: &ParserSettings) -> expr::BindSpec73			= name:$(id()) _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: None, value: expr}}74			/ name:$(id()) _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: Some(params), value: expr}}75		pub rule assertion(s: &ParserSettings) -> expr::AssertStmt76			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }7778		pub rule whole_line() -> &'input str79			= str:$((!['\n'][_])* "\n") {str}80		pub rule string_block() -> String81			= "|||" (!['\n']single_whitespace())* "\n"82			  prefix:[' ' | '\t']+ first_line:whole_line()83			  lines:([' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*84			  [' ' | '\t']*<, {prefix.len() - 1}> "|||"85			  {let mut l = first_line.to_owned(); l.extend(lines); l}86		pub rule string() -> String87			= "\"" str:$(("\\\"" / "\\\\" / (!['"'][_]))*) "\"" {unescape::unescape(str).unwrap()}88			/ "'" str:$(("\\'" / "\\\\" / (!['\''][_]))*) "'" {unescape::unescape(str).unwrap()}89			/ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}90			/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}91			/ string_block()9293		pub rule field_name(s: &ParserSettings) -> expr::FieldName94			= name:$(id()) {expr::FieldName::Fixed(name.into())}95			/ name:string() {expr::FieldName::Fixed(name.into())}96			/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}97		pub rule visibility() -> expr::Visibility98			= ":::" {expr::Visibility::Unhide}99			/ "::" {expr::Visibility::Hidden}100			/ ":" {expr::Visibility::Normal}101		pub rule field(s: &ParserSettings) -> expr::FieldMember102			= name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{103				name,104				plus: plus.is_some(),105				params: None,106				visibility,107				value,108			}}109			/ name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{110				name,111				plus: false,112				params: Some(params),113				visibility,114				value,115			}}116		pub rule obj_local(s: &ParserSettings) -> BindSpec117			= keyword("local") _ bind:bind(s) {bind}118		pub rule member(s: &ParserSettings) -> expr::Member119			= bind:obj_local(s) {expr::Member::BindStmt(bind)}120			/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}121			/ field:field(s) {expr::Member::Field(field)}122		pub rule objinside(s: &ParserSettings) -> expr::ObjBody123			= 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})? {124				let mut compspecs = vec![CompSpec::ForSpec(forspec)];125				compspecs.extend(others.unwrap_or_default());126				expr::ObjBody::ObjComp(expr::ObjComp{127					pre_locals,128					key,129					value,130					post_locals,131					compspecs,132				})133			}134			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}135		pub rule ifspec(s: &ParserSettings) -> IfSpecData136			= keyword("if") _ expr:expr(s) {IfSpecData(expr)}137		pub rule forspec(s: &ParserSettings) -> ForSpecData138			= keyword("for") _ id:$(id()) _ keyword("in") _ cond:expr(s) {ForSpecData(id.into(), cond)}139		pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>140			= s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}141		pub rule local_expr(s: &ParserSettings) -> LocExpr142			= l(s,<keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }>)143		pub rule string_expr(s: &ParserSettings) -> LocExpr144			= l(s, <s:string() {Expr::Str(s.into())}>)145		pub rule obj_expr(s: &ParserSettings) -> LocExpr146			= l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)147		pub rule array_expr(s: &ParserSettings) -> LocExpr148			= l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)149		pub rule array_comp_expr(s: &ParserSettings) -> LocExpr150			= l(s,<"[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {151				let mut specs = vec![CompSpec::ForSpec(forspec)];152				specs.extend(others.unwrap_or_default());153				Expr::ArrComp(expr, specs)154			}>)155		pub rule number_expr(s: &ParserSettings) -> LocExpr156			= l(s,<n:number() { expr::Expr::Num(n) }>)157		pub rule var_expr(s: &ParserSettings) -> LocExpr158			= l(s,<n:$(id()) { expr::Expr::Var(n.into()) }>)159		pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr160			= l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{161				cond,162				cond_then,163				cond_else,164			}}>)165166		pub rule literal(s: &ParserSettings) -> LocExpr167			= l(s,<v:(168				keyword("null") {LiteralType::Null}169				/ keyword("true") {LiteralType::True}170				/ keyword("false") {LiteralType::False}171				/ keyword("self") {LiteralType::This}172				/ keyword("$") {LiteralType::Dollar}173				/ keyword("super") {LiteralType::Super}174			) {Expr::Literal(v)}>)175176		pub rule expr_basic(s: &ParserSettings) -> LocExpr177			= literal(s)178179			/ string_expr(s) / number_expr(s)180			/ array_expr(s)181			/ obj_expr(s)182			/ array_expr(s)183			/ array_comp_expr(s)184185			/ l(s,<keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}>)186			/ l(s,<keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}>)187188			/ var_expr(s)189			/ local_expr(s)190			/ if_then_else_expr(s)191192			/ l(s,<keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}>)193			/ l(s,<assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }>)194195			/ l(s,<keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }>)196197		rule slice_part(s: &ParserSettings) -> Option<LocExpr>198			= e:(_ e:expr(s) _{e})? {e}199		pub rule slice_desc(s: &ParserSettings) -> SliceDesc200			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {201				let (end, step) = if let Some((end, step)) = pair {202					(end, step)203				}else{204					(None, None)205				};206207				SliceDesc { start, end, step }208			}209210		rule expr(s: &ParserSettings) -> LocExpr211			= start:position!() a:precedence! {212				a:(@) _ "||" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}213				--214				a:(@) _ "&&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}215				--216				a:(@) _ "|" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}217				--218				a:@ _ "^" _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}219				--220				a:(@) _ "&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}221				--222				a:(@) _ "==" _ b:@ {loc_expr_todo!(Expr::Apply(223					el!(Expr::Index(224						el!(Expr::Var("std".into())),225						el!(Expr::Str("equals".into()))226					)),227					ArgsDesc(vec![Arg(None, a), Arg(None, b)]),228					true229				))}230				a:(@) _ "!=" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, el!(Expr::Apply(231					el!(Expr::Index(232						el!(Expr::Var("std".into())),233						el!(Expr::Str("equals".into()))234					)),235					ArgsDesc(vec![Arg(None, a), Arg(None, b)]),236					true237				))))}238				--239				a:(@) _ "<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}240				a:(@) _ ">" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}241				a:(@) _ "<=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}242				a:(@) _ ">=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}243				a:(@) _ keyword("in") _ b:@ {loc_expr_todo!(Expr::Apply(244					el!(Expr::Index(245						el!(Expr::Var("std".into())),246						el!(Expr::Str("objectHasEx".into()))247					)), ArgsDesc(vec![Arg(None, b), Arg(None, a), Arg(None, el!(Expr::Literal(LiteralType::True)))]),248					true249				))}250				--251				a:(@) _ "<<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}252				a:(@) _ ">>" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}253				--254				a:(@) _ "+" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}255				a:(@) _ "-" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}256				--257				a:(@) _ "*" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}258				a:(@) _ "/" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}259				a:(@) _ "%" _ b:@ {loc_expr_todo!(Expr::Apply(260					el!(Expr::Index(261						el!(Expr::Var("std".into())),262						el!(Expr::Str("mod".into()))263					)), ArgsDesc(vec![Arg(None, a), Arg(None, b)]),264					false265				))}266				--267						"-" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}268						"!" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}269						"~" _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }270				--271				a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Apply(272					el!(Expr::Index(273						el!(Expr::Var("std".into())),274						el!(Expr::Str("slice".into())),275					)),276					ArgsDesc(vec![277						Arg(None, a),278						Arg(None, s.start.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),279						Arg(None, s.end.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),280						Arg(None, s.step.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),281					]),282					true,283				))}284				a:(@) _ "." _ s:$(id()) {loc_expr_todo!(Expr::Index(a, el!(Expr::Str(s.into()))))}285				a:(@) _ "[" _ s:expr(s) _ "]" {loc_expr_todo!(Expr::Index(a, s))}286				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {loc_expr_todo!(Expr::Apply(a, args, ts.is_some()))}287				a:(@) _ "{" _ body:objinside(s) _ "}" {loc_expr_todo!(Expr::ObjExtend(a, body))}288				--289				e:expr_basic(s) {e}290				"(" _ e:expr(s) _ ")" {loc_expr_todo!(Expr::Parened(e))}291			} end:position!() {292				let LocExpr(e, _) = a;293				LocExpr(e, if s.loc_data {294					Some(ExprLocation(s.file_name.clone(), start, end))295				} else {296					None297				})298			}299			/ e:expr_basic(s) {e}300301		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}302	}303}304305pub type ParseError = peg::error::ParseError<peg::str::LineCol>;306pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {307	jsonnet_parser::jsonnet(str, settings)308}309310#[macro_export]311macro_rules! el {312	($expr:expr) => {313		LocExpr(std::rc::Rc::new($expr), None)314	};315}316317#[cfg(test)]318pub mod tests {319	use super::{expr::*, parse};320	use crate::ParserSettings;321	use std::path::PathBuf;322	use std::rc::Rc;323324	macro_rules! parse {325		($s:expr) => {326			parse(327				$s,328				&ParserSettings {329					loc_data: false,330					file_name: Rc::new(PathBuf::from("/test.jsonnet")),331					},332				)333			.unwrap()334		};335	}336337	mod expressions {338		use super::*;339340		pub fn basic_math() -> LocExpr {341			el!(Expr::BinaryOp(342				el!(Expr::Num(2.0)),343				BinaryOpType::Add,344				el!(Expr::BinaryOp(345					el!(Expr::Num(2.0)),346					BinaryOpType::Mul,347					el!(Expr::Num(2.0)),348				)),349			))350		}351	}352353	#[test]354	fn multiline_string() {355		assert_eq!(356			parse!("|||\n    Hello world!\n     a\n|||"),357			el!(Expr::Str("Hello world!\n a\n".into())),358		);359		assert_eq!(360			parse!("|||\n  Hello world!\n   a\n|||"),361			el!(Expr::Str("Hello world!\n a\n".into())),362		);363		assert_eq!(364			parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),365			el!(Expr::Str("Hello world!\n\ta\n".into())),366		);367		assert_eq!(368			parse!("|||\n   Hello world!\n    a\n |||"),369			el!(Expr::Str("Hello world!\n a\n".into())),370		);371	}372373	#[test]374	fn slice() {375		parse!("a[1:]");376		parse!("a[1::]");377		parse!("a[:1:]");378		parse!("a[::1]");379		parse!("str[:len - 1]");380	}381382	#[test]383	fn string_escaping() {384		assert_eq!(385			parse!(r#""Hello, \"world\"!""#),386			el!(Expr::Str(r#"Hello, "world"!"#.into())),387		);388		assert_eq!(389			parse!(r#"'Hello \'world\'!'"#),390			el!(Expr::Str("Hello 'world'!".into())),391		);392		assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into())),);393	}394395	#[test]396	fn string_unescaping() {397		assert_eq!(398			parse!(r#""Hello\nWorld""#),399			el!(Expr::Str("Hello\nWorld".into())),400		);401	}402403	#[test]404	fn string_verbantim() {405		assert_eq!(406			parse!(r#"@"Hello\n""World""""#),407			el!(Expr::Str("Hello\\n\"World\"".into())),408		);409	}410411	#[test]412	fn imports() {413		assert_eq!(414			parse!("import \"hello\""),415			el!(Expr::Import(PathBuf::from("hello"))),416		);417		assert_eq!(418			parse!("importstr \"garnish.txt\""),419			el!(Expr::ImportStr(PathBuf::from("garnish.txt")))420		);421	}422423	#[test]424	fn empty_object() {425		assert_eq!(parse!("{}"), el!(Expr::Obj(ObjBody::MemberList(vec![]))));426	}427428	#[test]429	fn basic_math() {430		assert_eq!(431			parse!("2+2*2"),432			el!(Expr::BinaryOp(433				el!(Expr::Num(2.0)),434				BinaryOpType::Add,435				el!(Expr::BinaryOp(436					el!(Expr::Num(2.0)),437					BinaryOpType::Mul,438					el!(Expr::Num(2.0))439				))440			))441		);442	}443444	#[test]445	fn basic_math_with_indents() {446		assert_eq!(parse!("2	+ 	  2	  *	2   	"), expressions::basic_math());447	}448449	#[test]450	fn basic_math_parened() {451		assert_eq!(452			parse!("2+(2+2*2)"),453			el!(Expr::BinaryOp(454				el!(Expr::Num(2.0)),455				BinaryOpType::Add,456				el!(Expr::Parened(expressions::basic_math())),457			))458		);459	}460461	/// Comments should not affect parsing462	#[test]463	fn comments() {464		assert_eq!(465			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),466			el!(Expr::BinaryOp(467				el!(Expr::Num(2.0)),468				BinaryOpType::Add,469				el!(Expr::BinaryOp(470					el!(Expr::Num(3.0)),471					BinaryOpType::Mul,472					el!(Expr::Num(4.0))473				))474			))475		);476	}477478	/// Comments should be able to be escaped479	#[test]480	fn comment_escaping() {481		assert_eq!(482			parse!("2/*\\*/+*/ - 22"),483			el!(Expr::BinaryOp(484				el!(Expr::Num(2.0)),485				BinaryOpType::Sub,486				el!(Expr::Num(22.0))487			))488		);489	}490491	#[test]492	fn suffix() {493		// assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));494		// assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));495		// assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));496		// assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))497	}498499	#[test]500	fn array_comp() {501		use Expr::*;502		assert_eq!(503			parse!("[std.deepJoin(x) for x in arr]"),504			el!(ArrComp(505				el!(Apply(506					el!(Index(el!(Var("std".into())), el!(Str("deepJoin".into())))),507					ArgsDesc(vec![Arg(None, el!(Var("x".into())))]),508					false,509				)),510				vec![CompSpec::ForSpec(ForSpecData(511					"x".into(),512					el!(Var("arr".into()))513				))]514			)),515		)516	}517518	#[test]519	fn reserved() {520		use Expr::*;521		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null)));522		assert_eq!(parse!("nulla"), el!(Var("nulla".into())));523	}524525	#[test]526	fn multiple_args_buf() {527		parse!("a(b, null_fields)");528	}529530	#[test]531	fn infix_precedence() {532		use Expr::*;533		assert_eq!(534			parse!("!a && !b"),535			el!(BinaryOp(536				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),537				BinaryOpType::And,538				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))539			))540		);541	}542543	#[test]544	fn infix_precedence_division() {545		use Expr::*;546		assert_eq!(547			parse!("!a / !b"),548			el!(BinaryOp(549				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),550				BinaryOpType::Div,551				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))552			))553		);554	}555556	#[test]557	fn double_negation() {558		use Expr::*;559		assert_eq!(560			parse!("!!a"),561			el!(UnaryOp(562				UnaryOpType::Not,563				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()))))564			))565		)566	}567568	#[test]569	fn array_test_error() {570		parse!("[a for a in b if c for e in f]");571		//                    ^^^^ failed code572	}573574	#[test]575	fn can_parse_stdlib() {576		parse!(jrsonnet_stdlib::STDLIB_STR);577	}578579	// From source code580	/*581	#[bench]582	fn bench_parse_peg(b: &mut Bencher) {583		b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))584	}585	*/586}