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

difftreelog

fix correct string block parsing

Лач2020-06-14parent: #55e9769.patch.diff
in: master

2 files changed

modifiedcrates/jsonnet-parser/src/lib.rsdiffbeforeafterboth
before · crates/jsonnet-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;9mod string_processing;10pub use expr::*;11pub use peg;12use string_processing::deent;1314pub struct ParserSettings {15	pub loc_data: bool,16	pub file_name: PathBuf,17}1819parser! {20	grammar jsonnet_parser() for str {21		use peg::ParseLiteral;2223		/// Standard C-like comments24		rule comment()25			= "//" (!['\n'][_])* "\n"26			/ "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"27			/ "#" (!['\n'][_])* "\n"2829		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")30		rule _() = single_whitespace()*3132		/// For comma-delimited elements33		rule comma() = quiet!{_ "," _} / expected!("<comma>")34		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}35		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}36		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']37		/// Sequence of digits38		rule uint() -> u64 = a:$(digit()+) { a.parse().unwrap() }39		/// Number in scientific notation format40		rule number() -> f64 = quiet!{a:$(uint() ("." uint())? (['e'|'E'] (s:['+'|'-'])? uint())?) { a.parse().unwrap() }} / expected!("<number>")4142		/// Reserved word followed by any non-alphanumberic43		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()44		rule id() -> String = quiet!{ !reserved() s:$(alpha() (alpha() / digit())*) {s.to_owned()}} / expected!("<identifier>")4546		rule keyword(id: &'static str)47			= ##parse_string_literal(id) end_of_ident()48		// Adds location data information to existing expression49		rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr50			= start:position!() v:x() end:position!() {loc_expr!(v, s.loc_data, (s.file_name.clone(), start, end))}5152		pub rule param(s: &ParserSettings) -> expr::Param = name:id() expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr) }53		pub rule params(s: &ParserSettings) -> expr::ParamsDesc54			= params:(param(s) ** comma()) {55				let mut defaults_started = false;56				for param in &params {57					defaults_started = defaults_started || param.1.is_some();58					assert_eq!(defaults_started, param.1.is_some(), "defauld parameters should be used after all positionals");59				}60				expr::ParamsDesc(params)61			}62			/ { expr::ParamsDesc(Vec::new()) }6364		pub rule arg(s: &ParserSettings) -> expr::Arg65			= name:id() _ "=" _ expr:expr(s) {expr::Arg(Some(name), expr)}66			/ expr:expr(s) {expr::Arg(None, expr)}67		pub rule args(s: &ParserSettings) -> expr::ArgsDesc68			= args:arg(s) ** comma() comma()? {69				let mut named_started = false;70				for arg in &args {71					named_started = named_started || arg.0.is_some();72					assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");73				}74				expr::ArgsDesc(args)75			}76			/ { expr::ArgsDesc(Vec::new()) }7778		pub rule bind(s: &ParserSettings) -> expr::BindSpec79			= name:id() _ "=" _ expr:expr(s) {expr::BindSpec{name, params: None, value: expr}}80			/ name:id() _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name, params: Some(params), value: expr}}81		pub rule assertion(s: &ParserSettings) -> expr::AssertStmt82			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }8384		pub rule whole_line() -> String85			= str:$((!['\n'][_])* "\n") {str.to_owned()}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			// TODO: This is temporary workaround, i still dont know how to write this correctly btw.92			/ "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<1, 1> whole_line())+) " "*<0, 0> "|||" {deent(str)}93			/ "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<2, 2> whole_line())+) " "*<1, 1> "|||" {deent(str)}94			/ "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<3, 3> whole_line())+) " "*<2, 2> "|||" {deent(str)}95			/ "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<4, 4> whole_line())+) " "*<3, 3> "|||" {deent(str)}96			/ "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<5, 5> whole_line())+) " "*<4, 4> "|||" {deent(str)}97			/ "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<6, 6> whole_line())+) " "*<5, 5> "|||" {deent(str)}98			/ "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<7, 7> whole_line())+) " "*<6, 6> "|||" {deent(str)}99			/ "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<8, 8> whole_line())+) " "*<7, 7> "|||" {deent(str)}100			/ "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<9, 9> whole_line())+) " "*<8, 8> "|||" {deent(str)}101			/ "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<10, 10> whole_line())+) " "*<9, 9> "|||" {deent(str)}102			/ "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<11, 11> whole_line())+) " "*<10, 10> "|||" {deent(str)}103			/ "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<12, 12> whole_line())+) " "*<11, 10> "|||" {deent(str)}104105		pub rule field_name(s: &ParserSettings) -> expr::FieldName106			= name:id() {expr::FieldName::Fixed(name)}107			/ name:string() {expr::FieldName::Fixed(name)}108			/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}109		pub rule visibility() -> expr::Visibility110			= ":::" {expr::Visibility::Unhide}111			/ "::" {expr::Visibility::Hidden}112			/ ":" {expr::Visibility::Normal}113		pub rule field(s: &ParserSettings) -> expr::FieldMember114			= name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{115				name,116				plus: plus.is_some(),117				params: None,118				visibility,119				value,120			}}121			/ name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{122				name,123				plus: false,124				params: Some(params),125				visibility,126				value,127			}}128		pub rule obj_local(s: &ParserSettings) -> BindSpec129			= keyword("local") _ bind:bind(s) {bind}130		pub rule member(s: &ParserSettings) -> expr::Member131			= bind:obj_local(s) {expr::Member::BindStmt(bind)}132			/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}133			/ field:field(s) {expr::Member::Field(field)}134		pub rule objinside(s: &ParserSettings) -> expr::ObjBody135			= 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})? {136				expr::ObjBody::ObjComp {137					pre_locals,138					key,139					value,140					post_locals,141					compspecs: [vec![CompSpec::ForSpec(forspec)], others.unwrap_or_default()].concat(),142				}143			}144			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}145		pub rule ifspec(s: &ParserSettings) -> IfSpecData146			= keyword("if") _ expr:expr(s) {IfSpecData(expr)}147		pub rule forspec(s: &ParserSettings) -> ForSpecData148			= keyword("for") _ id:id() _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}149		pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>150			= s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}151		pub rule local_expr(s: &ParserSettings) -> LocExpr152			= l(s,<keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }>)153		pub rule string_expr(s: &ParserSettings) -> LocExpr154			= l(s, <s:string() {Expr::Str(s)}>)155		pub rule obj_expr(s: &ParserSettings) -> LocExpr156			= l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)157		pub rule array_expr(s: &ParserSettings) -> LocExpr158			= l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)159		pub rule array_comp_expr(s: &ParserSettings) -> LocExpr160			= 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())}>)161		pub rule number_expr(s: &ParserSettings) -> LocExpr162			= l(s,<n:number() { expr::Expr::Num(n) }>)163		pub rule var_expr(s: &ParserSettings) -> LocExpr164			= l(s,<n:id() { expr::Expr::Var(n) }>)165		pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr166			= l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{167				cond,168				cond_then,169				cond_else,170			}}>)171172		pub rule literal(s: &ParserSettings) -> LocExpr173			= l(s,<v:(174				keyword("null") {LiteralType::Null}175				/ keyword("true") {LiteralType::True}176				/ keyword("false") {LiteralType::False}177				/ keyword("self") {LiteralType::This}178				/ keyword("$") {LiteralType::Dollar}179				/ keyword("super") {LiteralType::Super}180			) {Expr::Literal(v)}>)181182		pub rule expr_basic(s: &ParserSettings) -> LocExpr183			= literal(s)184185			/ string_expr(s) / number_expr(s)186			/ array_expr(s)187			/ obj_expr(s)188			/ array_expr(s)189			/ array_comp_expr(s)190191			/ l(s,<keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}>)192			/ l(s,<keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}>)193194			/ var_expr(s)195			/ local_expr(s)196			/ if_then_else_expr(s)197198			/ l(s,<keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}>)199			/ l(s,<assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }>)200201			/ l(s,<keyword("error") _ expr:expr(s) { Expr::Error(expr) }>)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(s: &ParserSettings) -> LocExpr213			= start:position!() a:precedence! {214				a:(@) _ "||" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}215				--216				a:(@) _ "&&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}217				--218				a:(@) _ "|" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}219				--220				a:@ _ "^" _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}221				--222				a:(@) _ "&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}223				--224				a:(@) _ "==" _ b:@ {loc_expr_todo!(Expr::Apply(225					el!(Expr::Index(226						el!(Expr::Var("std".to_owned())),227						el!(Expr::Str("equals".to_owned()))228					)),229					ArgsDesc(vec![Arg(None, a), Arg(None, b)]),230					true231				))}232				a:(@) _ "!=" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, el!(Expr::Apply(233					el!(Expr::Index(234						el!(Expr::Var("std".to_owned())),235						el!(Expr::Str("equals".to_owned()))236					)),237					ArgsDesc(vec![Arg(None, a), Arg(None, b)]),238					true239				))))}240				--241				a:(@) _ "<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}242				a:(@) _ ">" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}243				a:(@) _ "<=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}244				a:(@) _ ">=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}245				a:(@) _ keyword("in") _ b:@ {loc_expr_todo!(Expr::Apply(246					el!(Expr::Index(247						el!(Expr::Var("std".to_owned())),248						el!(Expr::Str("objectHasEx".to_owned()))249					)), ArgsDesc(vec![Arg(None, b), Arg(None, a), Arg(None, el!(Expr::Literal(LiteralType::True)))]),250					true251				))}252				--253				a:(@) _ "<<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}254				a:(@) _ ">>" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}255				--256				a:(@) _ "+" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}257				a:(@) _ "-" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}258				--259				a:(@) _ "*" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}260				a:(@) _ "/" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}261				a:(@) _ "%" _ b:@ {loc_expr_todo!(Expr::Apply(262					el!(Expr::Index(263						el!(Expr::Var("std".to_owned())),264						el!(Expr::Str("mod".to_owned()))265					)), ArgsDesc(vec![Arg(None, a), Arg(None, b)]),266					true267				))}268				--269						"-" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}270						"!" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}271						"~" _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }272				--273				a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Slice(a, s))}274				a:(@) _ "." _ s:id() {loc_expr_todo!(Expr::Index(a, el!(Expr::Str(s))))}275				a:(@) _ "[" _ s:expr(s) _ "]" {loc_expr_todo!(Expr::Index(a, s))}276				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {loc_expr_todo!(Expr::Apply(a, args, ts.is_some()))}277				a:(@) _ "{" _ body:objinside(s) _ "}" {loc_expr_todo!(Expr::ObjExtend(a, body))}278				--279				e:expr_basic(s) {e}280				"(" _ e:expr(s) _ ")" {loc_expr_todo!(Expr::Parened(e))}281			} end:position!() {282				let LocExpr(e, _) = a;283				LocExpr(e, if s.loc_data {284					Some(Rc::new(ExprLocation(s.file_name.to_owned(), start, end)))285				} else {286					None287				})288			}289			/ e:expr_basic(s) {e}290291		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}292	}293}294295pub type ParseError = peg::error::ParseError<peg::str::LineCol>;296pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {297	jsonnet_parser::jsonnet(str, settings)298}299300#[macro_export]301macro_rules! el {302	($expr:expr) => {303		LocExpr(std::rc::Rc::new($expr), None)304	};305}306307#[cfg(test)]308pub mod tests {309	use super::{expr::*, parse};310	use crate::ParserSettings;311	use std::path::PathBuf;312313	macro_rules! parse {314		($s:expr) => {315			parse(316				$s,317				&ParserSettings {318					loc_data: false,319					file_name: PathBuf::from("/test.jsonnet"),320					},321				)322			.unwrap()323		};324	}325326	mod expressions {327		use super::*;328329		pub fn basic_math() -> LocExpr {330			el!(Expr::BinaryOp(331				el!(Expr::Num(2.0)),332				BinaryOpType::Add,333				el!(Expr::BinaryOp(334					el!(Expr::Num(2.0)),335					BinaryOpType::Mul,336					el!(Expr::Num(2.0)),337				)),338			))339		}340	}341342	#[test]343	fn multiline_string() {344		assert_eq!(345			parse!("|||\n      Hello world!\n  a\n|||"),346			el!(Expr::Str("    Hello world!\na\n".to_owned())),347		)348	}349350	#[test]351	fn string_escaping() {352		assert_eq!(353			parse!(r#""Hello, \"world\"!""#),354			el!(Expr::Str(r#"Hello, "world"!"#.to_owned())),355		);356		assert_eq!(357			parse!(r#"'Hello \'world\'!'"#),358			el!(Expr::Str("Hello 'world'!".to_owned())),359		);360		assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".to_owned())),);361	}362363	#[test]364	fn string_unescaping() {365		assert_eq!(366			parse!(r#""Hello\nWorld""#),367			el!(Expr::Str("Hello\nWorld".to_owned())),368		);369	}370371	#[test]372	fn string_verbantim() {373		assert_eq!(374			parse!(r#"@"Hello\n""World""""#),375			el!(Expr::Str("Hello\\n\"World\"".to_owned())),376		);377	}378379	#[test]380	fn imports() {381		assert_eq!(382			parse!("import \"hello\""),383			el!(Expr::Import(PathBuf::from("hello"))),384		);385		assert_eq!(386			parse!("importstr \"garnish.txt\""),387			el!(Expr::ImportStr(PathBuf::from("garnish.txt")))388		);389	}390391	#[test]392	fn empty_object() {393		assert_eq!(parse!("{}"), el!(Expr::Obj(ObjBody::MemberList(vec![]))));394	}395396	#[test]397	fn basic_math() {398		assert_eq!(399			parse!("2+2*2"),400			el!(Expr::BinaryOp(401				el!(Expr::Num(2.0)),402				BinaryOpType::Add,403				el!(Expr::BinaryOp(404					el!(Expr::Num(2.0)),405					BinaryOpType::Mul,406					el!(Expr::Num(2.0))407				))408			))409		);410	}411412	#[test]413	fn basic_math_with_indents() {414		assert_eq!(parse!("2	+ 	  2	  *	2   	"), expressions::basic_math());415	}416417	#[test]418	fn basic_math_parened() {419		assert_eq!(420			parse!("2+(2+2*2)"),421			el!(Expr::BinaryOp(422				el!(Expr::Num(2.0)),423				BinaryOpType::Add,424				el!(Expr::Parened(expressions::basic_math())),425			))426		);427	}428429	/// Comments should not affect parsing430	#[test]431	fn comments() {432		assert_eq!(433			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),434			el!(Expr::BinaryOp(435				el!(Expr::Num(2.0)),436				BinaryOpType::Add,437				el!(Expr::BinaryOp(438					el!(Expr::Num(3.0)),439					BinaryOpType::Mul,440					el!(Expr::Num(4.0))441				))442			))443		);444	}445446	/// Comments should be able to be escaped447	#[test]448	fn comment_escaping() {449		assert_eq!(450			parse!("2/*\\*/+*/ - 22"),451			el!(Expr::BinaryOp(452				el!(Expr::Num(2.0)),453				BinaryOpType::Sub,454				el!(Expr::Num(22.0))455			))456		);457	}458459	#[test]460	fn suffix() {461		// assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));462		// assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));463		// assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));464		// assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))465	}466467	#[test]468	fn array_comp() {469		use Expr::*;470		assert_eq!(471			parse!("[std.deepJoin(x) for x in arr]"),472			el!(ArrComp(473				el!(Apply(474					el!(Index(475						el!(Var("std".to_owned())),476						el!(Str("deepJoin".to_owned()))477					)),478					ArgsDesc(vec![Arg(None, el!(Var("x".to_owned())))]),479					false,480				)),481				vec![CompSpec::ForSpec(ForSpecData(482					"x".to_owned(),483					el!(Var("arr".to_owned()))484				))]485			)),486		)487	}488489	#[test]490	fn reserved() {491		use Expr::*;492		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null)));493		assert_eq!(parse!("nulla"), el!(Var("nulla".to_owned())));494	}495496	#[test]497	fn multiple_args_buf() {498		parse!("a(b, null_fields)");499	}500501	#[test]502	fn infix_precedence() {503		use Expr::*;504		assert_eq!(505			parse!("!a && !b"),506			el!(BinaryOp(507				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned())))),508				BinaryOpType::And,509				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".to_owned()))))510			))511		);512	}513514	#[test]515	fn infix_precedence_division() {516		use Expr::*;517		assert_eq!(518			parse!("!a / !b"),519			el!(BinaryOp(520				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned())))),521				BinaryOpType::Div,522				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".to_owned()))))523			))524		);525	}526527	#[test]528	fn double_negation() {529		use Expr::*;530		assert_eq!(531			parse!("!!a"),532			el!(UnaryOp(533				UnaryOpType::Not,534				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned()))))535			))536		)537	}538539	#[test]540	fn array_test_error() {541		parse!("[a for a in b if c for e in f]");542		//                    ^^^^ failed code543	}544545	#[test]546	fn can_parse_stdlib() {547		parse!(jsonnet_stdlib::STDLIB_STR);548	}549550	use test::Bencher;551552	// From source code553	#[bench]554	fn bench_parse_peg(b: &mut Bencher) {555		b.iter(|| parse!(jsonnet_stdlib::STDLIB_STR))556	}557558	// From serialized blob559	#[bench]560	fn bench_parse_serde_bincode(b: &mut Bencher) {561		let serialized = bincode::serialize(&parse!(jsonnet_stdlib::STDLIB_STR)).unwrap();562		b.iter(|| bincode::deserialize::<LocExpr>(&serialized))563	}564}
after · crates/jsonnet-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;1112pub struct ParserSettings {13	pub loc_data: bool,14	pub file_name: PathBuf,15}1617parser! {18	grammar jsonnet_parser() for str {19		use peg::ParseLiteral;2021		/// Standard C-like comments22		rule comment()23			= "//" (!['\n'][_])* "\n"24			/ "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"25			/ "#" (!['\n'][_])* "\n"2627		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")28		rule _() = single_whitespace()*2930		/// For comma-delimited elements31		rule comma() = quiet!{_ "," _} / expected!("<comma>")32		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}33		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}34		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']35		/// Sequence of digits36		rule uint() -> u64 = a:$(digit()+) { a.parse().unwrap() }37		/// Number in scientific notation format38		rule number() -> f64 = quiet!{a:$(uint() ("." uint())? (['e'|'E'] (s:['+'|'-'])? uint())?) { a.parse().unwrap() }} / expected!("<number>")3940		/// Reserved word followed by any non-alphanumberic41		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()42		rule id() -> String = quiet!{ !reserved() s:$(alpha() (alpha() / digit())*) {s.to_owned()}} / expected!("<identifier>")4344		rule keyword(id: &'static str)45			= ##parse_string_literal(id) end_of_ident()46		// Adds location data information to existing expression47		rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr48			= start:position!() v:x() end:position!() {loc_expr!(v, s.loc_data, (s.file_name.clone(), start, end))}4950		pub rule param(s: &ParserSettings) -> expr::Param = name:id() expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr) }51		pub rule params(s: &ParserSettings) -> expr::ParamsDesc52			= params:(param(s) ** comma()) {53				let mut defaults_started = false;54				for param in &params {55					defaults_started = defaults_started || param.1.is_some();56					assert_eq!(defaults_started, param.1.is_some(), "defauld parameters should be used after all positionals");57				}58				expr::ParamsDesc(params)59			}60			/ { expr::ParamsDesc(Vec::new()) }6162		pub rule arg(s: &ParserSettings) -> expr::Arg63			= name:id() _ "=" _ expr:expr(s) {expr::Arg(Some(name), expr)}64			/ expr:expr(s) {expr::Arg(None, expr)}65		pub rule args(s: &ParserSettings) -> expr::ArgsDesc66			= args:arg(s) ** comma() comma()? {67				let mut named_started = false;68				for arg in &args {69					named_started = named_started || arg.0.is_some();70					assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");71				}72				expr::ArgsDesc(args)73			}74			/ { expr::ArgsDesc(Vec::new()) }7576		pub rule bind(s: &ParserSettings) -> expr::BindSpec77			= name:id() _ "=" _ expr:expr(s) {expr::BindSpec{name, params: None, value: expr}}78			/ name:id() _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name, params: Some(params), value: expr}}79		pub rule assertion(s: &ParserSettings) -> expr::AssertStmt80			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }8182		pub rule whole_line() -> &'input str83			= str:$((!['\n'][_])* "\n") {str}84		pub rule string_block() -> String85			= "|||" (!['\n']single_whitespace())* "\n"86			  prefix:[' ']+ first_line:whole_line()87			  lines:([' ']*<{prefix.len()}> s:whole_line() {s})*88			  [' ']*<, {prefix.len() - 1}> "|||"89			  {let mut l = first_line.to_owned(); l.extend(lines); l}90		pub rule string() -> String91			= "\"" str:$(("\\\"" / "\\\\" / (!['"'][_]))*) "\"" {unescape::unescape(str).unwrap()}92			/ "'" str:$(("\\'" / "\\\\" / (!['\''][_]))*) "'" {unescape::unescape(str).unwrap()}93			/ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}94			/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}95			/ string_block()9697		pub rule field_name(s: &ParserSettings) -> expr::FieldName98			= name:id() {expr::FieldName::Fixed(name)}99			/ name:string() {expr::FieldName::Fixed(name)}100			/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}101		pub rule visibility() -> expr::Visibility102			= ":::" {expr::Visibility::Unhide}103			/ "::" {expr::Visibility::Hidden}104			/ ":" {expr::Visibility::Normal}105		pub rule field(s: &ParserSettings) -> expr::FieldMember106			= name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{107				name,108				plus: plus.is_some(),109				params: None,110				visibility,111				value,112			}}113			/ name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{114				name,115				plus: false,116				params: Some(params),117				visibility,118				value,119			}}120		pub rule obj_local(s: &ParserSettings) -> BindSpec121			= keyword("local") _ bind:bind(s) {bind}122		pub rule member(s: &ParserSettings) -> expr::Member123			= bind:obj_local(s) {expr::Member::BindStmt(bind)}124			/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}125			/ field:field(s) {expr::Member::Field(field)}126		pub rule objinside(s: &ParserSettings) -> expr::ObjBody127			= 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})? {128				expr::ObjBody::ObjComp {129					pre_locals,130					key,131					value,132					post_locals,133					compspecs: [vec![CompSpec::ForSpec(forspec)], others.unwrap_or_default()].concat(),134				}135			}136			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}137		pub rule ifspec(s: &ParserSettings) -> IfSpecData138			= keyword("if") _ expr:expr(s) {IfSpecData(expr)}139		pub rule forspec(s: &ParserSettings) -> ForSpecData140			= keyword("for") _ id:id() _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}141		pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>142			= s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}143		pub rule local_expr(s: &ParserSettings) -> LocExpr144			= l(s,<keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }>)145		pub rule string_expr(s: &ParserSettings) -> LocExpr146			= l(s, <s:string() {Expr::Str(s)}>)147		pub rule obj_expr(s: &ParserSettings) -> LocExpr148			= l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)149		pub rule array_expr(s: &ParserSettings) -> LocExpr150			= l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)151		pub rule array_comp_expr(s: &ParserSettings) -> LocExpr152			= 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())}>)153		pub rule number_expr(s: &ParserSettings) -> LocExpr154			= l(s,<n:number() { expr::Expr::Num(n) }>)155		pub rule var_expr(s: &ParserSettings) -> LocExpr156			= l(s,<n:id() { expr::Expr::Var(n) }>)157		pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr158			= l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{159				cond,160				cond_then,161				cond_else,162			}}>)163164		pub rule literal(s: &ParserSettings) -> LocExpr165			= l(s,<v:(166				keyword("null") {LiteralType::Null}167				/ keyword("true") {LiteralType::True}168				/ keyword("false") {LiteralType::False}169				/ keyword("self") {LiteralType::This}170				/ keyword("$") {LiteralType::Dollar}171				/ keyword("super") {LiteralType::Super}172			) {Expr::Literal(v)}>)173174		pub rule expr_basic(s: &ParserSettings) -> LocExpr175			= literal(s)176177			/ string_expr(s) / number_expr(s)178			/ array_expr(s)179			/ obj_expr(s)180			/ array_expr(s)181			/ array_comp_expr(s)182183			/ l(s,<keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}>)184			/ l(s,<keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}>)185186			/ var_expr(s)187			/ local_expr(s)188			/ if_then_else_expr(s)189190			/ l(s,<keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}>)191			/ l(s,<assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }>)192193			/ l(s,<keyword("error") _ expr:expr(s) { Expr::Error(expr) }>)194195		pub rule slice_desc(s: &ParserSettings) -> SliceDesc196			= start:expr(s)? _ ":" _ pair:(end:expr(s)? _ step:(":" _ e:expr(s) {e})? {(end, step)})? {197				if let Some((end, step)) = pair {198					SliceDesc { start, end, step }199				}else{200					SliceDesc { start, end: None, step: None }201				}202			}203204		rule expr(s: &ParserSettings) -> LocExpr205			= start:position!() a:precedence! {206				a:(@) _ "||" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}207				--208				a:(@) _ "&&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}209				--210				a:(@) _ "|" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}211				--212				a:@ _ "^" _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}213				--214				a:(@) _ "&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}215				--216				a:(@) _ "==" _ b:@ {loc_expr_todo!(Expr::Apply(217					el!(Expr::Index(218						el!(Expr::Var("std".to_owned())),219						el!(Expr::Str("equals".to_owned()))220					)),221					ArgsDesc(vec![Arg(None, a), Arg(None, b)]),222					true223				))}224				a:(@) _ "!=" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, el!(Expr::Apply(225					el!(Expr::Index(226						el!(Expr::Var("std".to_owned())),227						el!(Expr::Str("equals".to_owned()))228					)),229					ArgsDesc(vec![Arg(None, a), Arg(None, b)]),230					true231				))))}232				--233				a:(@) _ "<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}234				a:(@) _ ">" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}235				a:(@) _ "<=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}236				a:(@) _ ">=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}237				a:(@) _ keyword("in") _ b:@ {loc_expr_todo!(Expr::Apply(238					el!(Expr::Index(239						el!(Expr::Var("std".to_owned())),240						el!(Expr::Str("objectHasEx".to_owned()))241					)), ArgsDesc(vec![Arg(None, b), Arg(None, a), Arg(None, el!(Expr::Literal(LiteralType::True)))]),242					true243				))}244				--245				a:(@) _ "<<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}246				a:(@) _ ">>" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}247				--248				a:(@) _ "+" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}249				a:(@) _ "-" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}250				--251				a:(@) _ "*" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}252				a:(@) _ "/" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}253				a:(@) _ "%" _ b:@ {loc_expr_todo!(Expr::Apply(254					el!(Expr::Index(255						el!(Expr::Var("std".to_owned())),256						el!(Expr::Str("mod".to_owned()))257					)), ArgsDesc(vec![Arg(None, a), Arg(None, b)]),258					true259				))}260				--261						"-" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}262						"!" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}263						"~" _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }264				--265				a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Slice(a, s))}266				a:(@) _ "." _ s:id() {loc_expr_todo!(Expr::Index(a, el!(Expr::Str(s))))}267				a:(@) _ "[" _ s:expr(s) _ "]" {loc_expr_todo!(Expr::Index(a, s))}268				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {loc_expr_todo!(Expr::Apply(a, args, ts.is_some()))}269				a:(@) _ "{" _ body:objinside(s) _ "}" {loc_expr_todo!(Expr::ObjExtend(a, body))}270				--271				e:expr_basic(s) {e}272				"(" _ e:expr(s) _ ")" {loc_expr_todo!(Expr::Parened(e))}273			} end:position!() {274				let LocExpr(e, _) = a;275				LocExpr(e, if s.loc_data {276					Some(Rc::new(ExprLocation(s.file_name.to_owned(), start, end)))277				} else {278					None279				})280			}281			/ e:expr_basic(s) {e}282283		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}284	}285}286287pub type ParseError = peg::error::ParseError<peg::str::LineCol>;288pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {289	jsonnet_parser::jsonnet(str, settings)290}291292#[macro_export]293macro_rules! el {294	($expr:expr) => {295		LocExpr(std::rc::Rc::new($expr), None)296	};297}298299#[cfg(test)]300pub mod tests {301	use super::{expr::*, parse};302	use crate::ParserSettings;303	use std::path::PathBuf;304305	macro_rules! parse {306		($s:expr) => {307			parse(308				$s,309				&ParserSettings {310					loc_data: false,311					file_name: PathBuf::from("/test.jsonnet"),312					},313				)314			.unwrap()315		};316	}317318	mod expressions {319		use super::*;320321		pub fn basic_math() -> LocExpr {322			el!(Expr::BinaryOp(323				el!(Expr::Num(2.0)),324				BinaryOpType::Add,325				el!(Expr::BinaryOp(326					el!(Expr::Num(2.0)),327					BinaryOpType::Mul,328					el!(Expr::Num(2.0)),329				)),330			))331		}332	}333334	#[test]335	fn multiline_string() {336		assert_eq!(337			parse!("|||\n    Hello world!\n     a\n|||"),338			el!(Expr::Str("Hello world!\n a\n".to_owned())),339		)340	}341342	#[test]343	fn string_escaping() {344		assert_eq!(345			parse!(r#""Hello, \"world\"!""#),346			el!(Expr::Str(r#"Hello, "world"!"#.to_owned())),347		);348		assert_eq!(349			parse!(r#"'Hello \'world\'!'"#),350			el!(Expr::Str("Hello 'world'!".to_owned())),351		);352		assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".to_owned())),);353	}354355	#[test]356	fn string_unescaping() {357		assert_eq!(358			parse!(r#""Hello\nWorld""#),359			el!(Expr::Str("Hello\nWorld".to_owned())),360		);361	}362363	#[test]364	fn string_verbantim() {365		assert_eq!(366			parse!(r#"@"Hello\n""World""""#),367			el!(Expr::Str("Hello\\n\"World\"".to_owned())),368		);369	}370371	#[test]372	fn imports() {373		assert_eq!(374			parse!("import \"hello\""),375			el!(Expr::Import(PathBuf::from("hello"))),376		);377		assert_eq!(378			parse!("importstr \"garnish.txt\""),379			el!(Expr::ImportStr(PathBuf::from("garnish.txt")))380		);381	}382383	#[test]384	fn empty_object() {385		assert_eq!(parse!("{}"), el!(Expr::Obj(ObjBody::MemberList(vec![]))));386	}387388	#[test]389	fn basic_math() {390		assert_eq!(391			parse!("2+2*2"),392			el!(Expr::BinaryOp(393				el!(Expr::Num(2.0)),394				BinaryOpType::Add,395				el!(Expr::BinaryOp(396					el!(Expr::Num(2.0)),397					BinaryOpType::Mul,398					el!(Expr::Num(2.0))399				))400			))401		);402	}403404	#[test]405	fn basic_math_with_indents() {406		assert_eq!(parse!("2	+ 	  2	  *	2   	"), expressions::basic_math());407	}408409	#[test]410	fn basic_math_parened() {411		assert_eq!(412			parse!("2+(2+2*2)"),413			el!(Expr::BinaryOp(414				el!(Expr::Num(2.0)),415				BinaryOpType::Add,416				el!(Expr::Parened(expressions::basic_math())),417			))418		);419	}420421	/// Comments should not affect parsing422	#[test]423	fn comments() {424		assert_eq!(425			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),426			el!(Expr::BinaryOp(427				el!(Expr::Num(2.0)),428				BinaryOpType::Add,429				el!(Expr::BinaryOp(430					el!(Expr::Num(3.0)),431					BinaryOpType::Mul,432					el!(Expr::Num(4.0))433				))434			))435		);436	}437438	/// Comments should be able to be escaped439	#[test]440	fn comment_escaping() {441		assert_eq!(442			parse!("2/*\\*/+*/ - 22"),443			el!(Expr::BinaryOp(444				el!(Expr::Num(2.0)),445				BinaryOpType::Sub,446				el!(Expr::Num(22.0))447			))448		);449	}450451	#[test]452	fn suffix() {453		// assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));454		// assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));455		// assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));456		// assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))457	}458459	#[test]460	fn array_comp() {461		use Expr::*;462		assert_eq!(463			parse!("[std.deepJoin(x) for x in arr]"),464			el!(ArrComp(465				el!(Apply(466					el!(Index(467						el!(Var("std".to_owned())),468						el!(Str("deepJoin".to_owned()))469					)),470					ArgsDesc(vec![Arg(None, el!(Var("x".to_owned())))]),471					false,472				)),473				vec![CompSpec::ForSpec(ForSpecData(474					"x".to_owned(),475					el!(Var("arr".to_owned()))476				))]477			)),478		)479	}480481	#[test]482	fn reserved() {483		use Expr::*;484		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null)));485		assert_eq!(parse!("nulla"), el!(Var("nulla".to_owned())));486	}487488	#[test]489	fn multiple_args_buf() {490		parse!("a(b, null_fields)");491	}492493	#[test]494	fn infix_precedence() {495		use Expr::*;496		assert_eq!(497			parse!("!a && !b"),498			el!(BinaryOp(499				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned())))),500				BinaryOpType::And,501				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".to_owned()))))502			))503		);504	}505506	#[test]507	fn infix_precedence_division() {508		use Expr::*;509		assert_eq!(510			parse!("!a / !b"),511			el!(BinaryOp(512				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned())))),513				BinaryOpType::Div,514				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".to_owned()))))515			))516		);517	}518519	#[test]520	fn double_negation() {521		use Expr::*;522		assert_eq!(523			parse!("!!a"),524			el!(UnaryOp(525				UnaryOpType::Not,526				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned()))))527			))528		)529	}530531	#[test]532	fn array_test_error() {533		parse!("[a for a in b if c for e in f]");534		//                    ^^^^ failed code535	}536537	#[test]538	fn can_parse_stdlib() {539		parse!(jsonnet_stdlib::STDLIB_STR);540	}541542	use test::Bencher;543544	// From source code545	#[bench]546	fn bench_parse_peg(b: &mut Bencher) {547		b.iter(|| parse!(jsonnet_stdlib::STDLIB_STR))548	}549550	// From serialized blob551	#[bench]552	fn bench_parse_serde_bincode(b: &mut Bencher) {553		let serialized = bincode::serialize(&parse!(jsonnet_stdlib::STDLIB_STR)).unwrap();554		b.iter(|| bincode::deserialize::<LocExpr>(&serialized))555	}556}
deletedcrates/jsonnet-parser/src/string_processing.rsdiffbeforeafterboth
--- a/crates/jsonnet-parser/src/string_processing.rs
+++ /dev/null
@@ -1,29 +0,0 @@
-/// Returns string with stripped line padding characters
-pub fn deent(input: &str) -> String {
-	if input.is_empty() {
-		return "".to_owned();
-	}
-	let min_ident = input
-		.split('\n')
-		.filter(|s| !s.is_empty())
-		.map(|ss| ss.chars().take_while(|c| *c == ' ').count())
-		.min()
-		.unwrap();
-	input
-		.split('\n')
-		.map(|s| s.chars().skip(min_ident).collect::<String>())
-		.collect::<Vec<String>>()
-		.join("\n")
-}
-
-#[cfg(test)]
-pub mod tests {
-	use super::*;
-	#[test]
-	fn deent_tests() {
-		assert_eq!(deent("  aaa"), "aaa");
-		assert_eq!(deent("  aaa\n bbb"), " aaa\nbbb");
-		assert_eq!(deent(" aaa\n  bbb"), "aaa\n bbb");
-		assert_eq!(deent(" aaa\n\n  bbb"), "aaa\n\n bbb");
-	}
-}