git.delta.rocks / jrsonnet / refs/commits / 0374cd028e16

difftreelog

refactor virtual file handling

Yaroslav Bolyukin2022-05-26parent: #c11576e.patch.diff
in: master

4 files changed

modifiedcrates/jrsonnet-parser/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-parser/Cargo.toml
+++ b/crates/jrsonnet-parser/Cargo.toml
@@ -11,6 +11,7 @@
 
 [dependencies]
 jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.2" }
+static_assertions = "1.1.0"
 
 peg = "0.8.0"
 
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -1,7 +1,6 @@
 use std::{
-	fmt::{Debug, Display},
+	fmt::{self, Debug, Display},
 	ops::Deref,
-	path::{Path, PathBuf},
 	rc::Rc,
 };
 
@@ -10,6 +9,8 @@
 #[cfg(feature = "serde")]
 use serde::{Deserialize, Serialize};
 
+use crate::source::Source;
+
 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
 #[derive(Debug, PartialEq, Trace)]
 pub enum FieldName {
@@ -68,7 +69,7 @@
 }
 
 impl Display for UnaryOpType {
-	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 		use UnaryOpType::*;
 		write!(
 			f,
@@ -118,7 +119,7 @@
 }
 
 impl Display for BinaryOpType {
-	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 		use BinaryOpType::*;
 		write!(
 			f,
@@ -326,11 +327,11 @@
 	LocalExpr(Vec<BindSpec>, LocExpr),
 
 	/// import "hello"
-	Import(PathBuf),
+	Import(IStr),
 	/// importStr "file.txt"
-	ImportStr(PathBuf),
+	ImportStr(IStr),
 	/// importBin "file.txt"
-	ImportBin(PathBuf),
+	ImportBin(IStr),
 	/// error "I'm broken"
 	ErrorStmt(LocExpr),
 	/// a(b, c)
@@ -358,15 +359,19 @@
 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
 #[derive(Clone, PartialEq, Trace)]
 #[skip_trace]
-pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);
+#[repr(C)]
+pub struct ExprLocation(pub Source, pub u32, pub u32);
 impl ExprLocation {
 	pub fn belongs_to(&self, other: &ExprLocation) -> bool {
 		other.0 == self.0 && other.1 <= self.1 && other.2 >= self.2
 	}
 }
 
+#[cfg(target_pointer_width = "64")]
+static_assertions::assert_eq_size!(ExprLocation, [u8; 16]);
+
 impl Debug for ExprLocation {
-	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 		write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)
 	}
 }
@@ -376,8 +381,11 @@
 #[derive(Clone, PartialEq, Trace)]
 pub struct LocExpr(pub Rc<Expr>, pub ExprLocation);
 
+#[cfg(target_pointer_width = "64")]
+static_assertions::assert_eq_size!(LocExpr, [u8; 24]);
+
 impl Debug for LocExpr {
-	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 		if f.alternate() {
 			write!(f, "{:#?}", self.0)?;
 		} else {
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-parser/src/lib.rs
1#![allow(clippy::redundant_closure_call)]23use std::{4	path::{Path, PathBuf},5	rc::Rc,6};78use peg::parser;9mod expr;10pub use expr::*;11pub use jrsonnet_interner::IStr;12pub use peg;13mod unescape;1415pub struct ParserSettings {16	pub file_name: Rc<Path>,17}1819macro_rules! expr_bin {20	($a:ident $op:ident $b:ident) => {21		Expr::BinaryOp($a, $op, $b)22	};23}24macro_rules! expr_un {25	($op:ident $a:ident) => {26		Expr::UnaryOp($op, $a)27	};28}2930parser! {31	grammar jsonnet_parser() for str {32		use peg::ParseLiteral;3334		rule eof() = quiet!{![_]} / expected!("<eof>")35		rule eol() = "\n" / eof()3637		/// Standard C-like comments38		rule comment()39			= "//" (!eol()[_])* eol()40			/ "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"41			/ "#" (!eol()[_])* eol()4243		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")44		rule _() = quiet!{([' ' | '\r' | '\n' | '\t']+) / comment()}* / expected!("<whitespace>")4546		/// For comma-delimited elements47		rule comma() = quiet!{_ "," _} / expected!("<comma>")48		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}49		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}50		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']51		/// Sequence of digits52		rule uint_str() -> &'input str = a:$(digit()+) { a }53		/// Number in scientific notation format54		rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")5556		/// Reserved word followed by any non-alphanumberic57		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()58		rule id() -> IStr = v:$(quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")) { v.into() }5960		rule keyword(id: &'static str) -> ()61			= ##parse_string_literal(id) end_of_ident()6263		pub rule param(s: &ParserSettings) -> expr::Param = name:id() expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr) }64		pub rule params(s: &ParserSettings) -> expr::ParamsDesc65			= params:param(s) ** comma() comma()? { expr::ParamsDesc(Rc::new(params)) }66			/ { expr::ParamsDesc(Rc::new(Vec::new())) }6768		pub rule arg(s: &ParserSettings) -> (Option<IStr>, LocExpr)69			= quiet! { name:(s:id() _ "=" !['='] _ {s})? expr:expr(s) {(name, expr)} }70			/ expected!("<argument>")7172		pub rule args(s: &ParserSettings) -> expr::ArgsDesc73			= args:arg(s)**comma() comma()? {?74				let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();75				let mut unnamed = Vec::with_capacity(unnamed_count);76				let mut named = Vec::with_capacity(args.len() - unnamed_count);77				let mut named_started = false;78				for (name, value) in args {79					if let Some(name) = name {80						named_started = true;81						named.push((name, value));82					} else {83						if named_started {84							return Err("<named argument>")85						}86						unnamed.push(value);87					}88				}89				Ok(expr::ArgsDesc::new(unnamed, named))90			}9192		pub rule destruct_rest() -> expr::DestructRest93			= "..." into:(_ into:id() {into})? {if let Some(into) = into {94				expr::DestructRest::Keep(into)95			} else {expr::DestructRest::Drop}}96		pub rule destruct_array(s: &ParserSettings) -> expr::Destruct97			= "[" _ start:destruct(s)**comma() rest:(98				comma() _ rest:destruct_rest()? end:(99					comma() end:destruct(s)**comma() (_ comma())? {end}100					/ comma()? {Vec::new()}101				) {(rest, end)}102				/ comma()? {(None, Vec::new())}103			) _ "]" {?104				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Array {105					start,106					rest: rest.0,107					end: rest.1,108				});109				#[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")110			}111		pub rule destruct_object(s: &ParserSettings) -> expr::Destruct112			= "{" _113				fields:(name:id() _ into:(":" _ into:destruct(s) {into})? {(name, into)})**comma()114				rest:(115					comma() rest:destruct_rest()? {rest}116					/ comma()? {None}117				)118			_ "}" {?119				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Object {120					fields,121					rest,122				});123				#[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")124			}125		pub rule destruct(s: &ParserSettings) -> expr::Destruct126			= v:id() {expr::Destruct::Full(v)}127			/ "?" {?128				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Skip);129				#[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")130			}131			/ arr:destruct_array(s) {arr}132			/ obj:destruct_object(s) {obj}133134		pub rule bind(s: &ParserSettings) -> expr::BindSpec135			= into:destruct(s) _ "=" _ expr:expr(s) {expr::BindSpec::Field{into, value: expr}}136			/ name:id() _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec::Function{name, params, value: expr}}137138		pub rule assertion(s: &ParserSettings) -> expr::AssertStmt139			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }140141		pub rule whole_line() -> &'input str142			= str:$((!['\n'][_])* "\n") {str}143		pub rule string_block() -> String144			= "|||" (!['\n']single_whitespace())* "\n"145			  empty_lines:$(['\n']*)146			  prefix:[' ' | '\t']+ first_line:whole_line()147			  lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*148			  [' ' | '\t']*<, {prefix.len() - 1}> "|||"149			  {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}150151		rule hex_char()152			= quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")153154		rule string_char(c: rule<()>)155			= (!['\\']!c()[_])+156			/ "\\\\"157			/ "\\u" hex_char() hex_char() hex_char() hex_char()158			/ "\\x" hex_char() hex_char()159			/ ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't' | '"' | '\''] } / expected!("<escape character>"))160		pub rule string() -> String161			= ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}162			/ ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}163			/ quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}164			/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}165			/ string_block() } / expected!("<string>")166167		pub rule field_name(s: &ParserSettings) -> expr::FieldName168			= name:id() {expr::FieldName::Fixed(name)}169			/ name:string() {expr::FieldName::Fixed(name.into())}170			/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}171		pub rule visibility() -> expr::Visibility172			= ":::" {expr::Visibility::Unhide}173			/ "::" {expr::Visibility::Hidden}174			/ ":" {expr::Visibility::Normal}175		pub rule field(s: &ParserSettings) -> expr::FieldMember176			= name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{177				name,178				plus: plus.is_some(),179				params: None,180				visibility,181				value,182			}}183			/ name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{184				name,185				plus: false,186				params: Some(params),187				visibility,188				value,189			}}190		pub rule obj_local(s: &ParserSettings) -> BindSpec191			= keyword("local") _ bind:bind(s) {bind}192		pub rule member(s: &ParserSettings) -> expr::Member193			= bind:obj_local(s) {expr::Member::BindStmt(bind)}194			/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}195			/ field:field(s) {expr::Member::Field(field)}196		pub rule objinside(s: &ParserSettings) -> expr::ObjBody197			= 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})? {198				let mut compspecs = vec![CompSpec::ForSpec(forspec)];199				compspecs.extend(others.unwrap_or_default());200				expr::ObjBody::ObjComp(expr::ObjComp{201					pre_locals,202					key,203					plus: plus.is_some(),204					value,205					post_locals,206					compspecs,207				})208			}209			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}210		pub rule ifspec(s: &ParserSettings) -> IfSpecData211			= keyword("if") _ expr:expr(s) {IfSpecData(expr)}212		pub rule forspec(s: &ParserSettings) -> ForSpecData213			= keyword("for") _ id:id() _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}214		pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>215			= s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}216		pub rule local_expr(s: &ParserSettings) -> Expr217			= keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }218		pub rule string_expr(s: &ParserSettings) -> Expr219			= s:string() {Expr::Str(s.into())}220		pub rule obj_expr(s: &ParserSettings) -> Expr221			= "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}222		pub rule array_expr(s: &ParserSettings) -> Expr223			= "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}224		pub rule array_comp_expr(s: &ParserSettings) -> Expr225			= "[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {226				let mut specs = vec![CompSpec::ForSpec(forspec)];227				specs.extend(others.unwrap_or_default());228				Expr::ArrComp(expr, specs)229			}230		pub rule number_expr(s: &ParserSettings) -> Expr231			= n:number() { expr::Expr::Num(n) }232		pub rule var_expr(s: &ParserSettings) -> Expr233			= n:id() { expr::Expr::Var(n) }234		pub rule id_loc(s: &ParserSettings) -> LocExpr235			= a:position!() n:id() b:position!() { LocExpr(Rc::new(expr::Expr::Str(n)), ExprLocation(s.file_name.clone(), a,b)) }236		pub rule if_then_else_expr(s: &ParserSettings) -> Expr237			= cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{238				cond,239				cond_then,240				cond_else,241			}}242243		pub rule literal(s: &ParserSettings) -> Expr244			= v:(245				keyword("null") {LiteralType::Null}246				/ keyword("true") {LiteralType::True}247				/ keyword("false") {LiteralType::False}248				/ keyword("self") {LiteralType::This}249				/ keyword("$") {LiteralType::Dollar}250				/ keyword("super") {LiteralType::Super}251			) {Expr::Literal(v)}252253		pub rule expr_basic(s: &ParserSettings) -> Expr254			= literal(s)255256			/ quiet!{"$intrinsicThisFile" {Expr::IntrinsicThisFile}}257			/ quiet!{"$intrinsicId" {Expr::IntrinsicId}}258			/ quiet!{"$intrinsic(" name:id() ")" {Expr::Intrinsic(name)}}259260			/ string_expr(s) / number_expr(s)261			/ array_expr(s)262			/ obj_expr(s)263			/ array_expr(s)264			/ array_comp_expr(s)265266			/ keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}267			/ keyword("importbin") _ path:string() {Expr::ImportBin(PathBuf::from(path))}268			/ keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}269270			/ var_expr(s)271			/ local_expr(s)272			/ if_then_else_expr(s)273274			/ keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}275			/ assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }276277			/ keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }278279		rule slice_part(s: &ParserSettings) -> Option<LocExpr>280			= _ e:(e:expr(s) _{e})? {e}281		pub rule slice_desc(s: &ParserSettings) -> SliceDesc282			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {283				let (end, step) = if let Some((end, step)) = pair {284					(end, step)285				}else{286					(None, None)287				};288289				SliceDesc { start, end, step }290			}291292		rule binop(x: rule<()>) -> ()293			= quiet!{ x() } / expected!("<binary op>")294		rule unaryop(x: rule<()>) -> ()295			= quiet!{ x() } / expected!("<unary op>")296297298		use BinaryOpType::*;299		use UnaryOpType::*;300		rule expr(s: &ParserSettings) -> LocExpr301			= precedence! {302				start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.file_name.clone(), start, end)) }303				--304				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}305				--306				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}307				--308				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}309				--310				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}311				--312				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}313				--314				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}315				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}316				--317				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}318				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}319				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}320				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}321				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}322				--323				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}324				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}325				--326				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}327				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}328				--329				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}330				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}331				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}332				--333						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}334						unaryop(<"!">) _ b:@ {expr_un!(Not b)}335						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}336				--337				a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}338				a:(@) _ "." _ a:position!() e:id_loc(s) b:position!() {Expr::Index(a, e)}339				a:(@) _ "[" _ e:expr(s) _ "]" {Expr::Index(a, e)}340				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}341				a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}342				--343				e:expr_basic(s) {e}344				"(" _ e:expr(s) _ ")" {Expr::Parened(e)}345			}346347		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}348	}349}350351pub type ParseError = peg::error::ParseError<peg::str::LineCol>;352pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {353	jsonnet_parser::jsonnet(str, settings)354}355/// Used for importstr values356pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {357	let len = str.len();358	LocExpr(359		Rc::new(Expr::Str(str)),360		ExprLocation(settings.file_name.clone(), 0, len),361	)362}363364#[cfg(test)]365pub mod tests {366	use std::path::PathBuf;367368	use BinaryOpType::*;369370	use super::{expr::*, parse};371	use crate::ParserSettings;372373	macro_rules! parse {374		($s:expr) => {375			parse(376				$s,377				&ParserSettings {378					file_name: PathBuf::from("test.jsonnet").into(),379				},380			)381			.unwrap()382		};383	}384385	macro_rules! el {386		($expr:expr, $from:expr, $to:expr$(,)?) => {387			LocExpr(388				std::rc::Rc::new($expr),389				ExprLocation(PathBuf::from("test.jsonnet").into(), $from, $to),390			)391		};392	}393394	#[test]395	fn multiline_string() {396		assert_eq!(397			parse!("|||\n    Hello world!\n     a\n|||"),398			el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),399		);400		assert_eq!(401			parse!("|||\n  Hello world!\n   a\n|||"),402			el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),403		);404		assert_eq!(405			parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),406			el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),407		);408		assert_eq!(409			parse!("|||\n   Hello world!\n    a\n |||"),410			el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),411		);412	}413414	#[test]415	fn slice() {416		parse!("a[1:]");417		parse!("a[1::]");418		parse!("a[:1:]");419		parse!("a[::1]");420		parse!("str[:len - 1]");421	}422423	#[test]424	fn string_escaping() {425		assert_eq!(426			parse!(r#""Hello, \"world\"!""#),427			el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),428		);429		assert_eq!(430			parse!(r#"'Hello \'world\'!'"#),431			el!(Expr::Str("Hello 'world'!".into()), 0, 18),432		);433		assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));434	}435436	#[test]437	fn string_unescaping() {438		assert_eq!(439			parse!(r#""Hello\nWorld""#),440			el!(Expr::Str("Hello\nWorld".into()), 0, 14),441		);442	}443444	#[test]445	fn string_verbantim() {446		assert_eq!(447			parse!(r#"@"Hello\n""World""""#),448			el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),449		);450	}451452	#[test]453	fn imports() {454		assert_eq!(455			parse!("import \"hello\""),456			el!(Expr::Import(PathBuf::from("hello")), 0, 14),457		);458		assert_eq!(459			parse!("importstr \"garnish.txt\""),460			el!(Expr::ImportStr(PathBuf::from("garnish.txt")), 0, 23)461		);462	}463464	#[test]465	fn empty_object() {466		assert_eq!(467			parse!("{}"),468			el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)469		);470	}471472	#[test]473	fn basic_math() {474		assert_eq!(475			parse!("2+2*2"),476			el!(477				Expr::BinaryOp(478					el!(Expr::Num(2.0), 0, 1),479					Add,480					el!(481						Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),482						2,483						5484					)485				),486				0,487				5488			)489		);490	}491492	#[test]493	fn basic_math_with_indents() {494		assert_eq!(495			parse!("2	+ 	  2	  *	2   	"),496			el!(497				Expr::BinaryOp(498					el!(Expr::Num(2.0), 0, 1),499					Add,500					el!(501						Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),502						7,503						14504					),505				),506				0,507				14508			)509		);510	}511512	#[test]513	fn basic_math_parened() {514		assert_eq!(515			parse!("2+(2+2*2)"),516			el!(517				Expr::BinaryOp(518					el!(Expr::Num(2.0), 0, 1),519					Add,520					el!(521						Expr::Parened(el!(522							Expr::BinaryOp(523								el!(Expr::Num(2.0), 3, 4),524								Add,525								el!(526									Expr::BinaryOp(527										el!(Expr::Num(2.0), 5, 6),528										Mul,529										el!(Expr::Num(2.0), 7, 8),530									),531									5,532									8533								),534							),535							3,536							8537						)),538						2,539						9540					),541				),542				0,543				9544			)545		);546	}547548	/// Comments should not affect parsing549	#[test]550	fn comments() {551		assert_eq!(552			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),553			el!(554				Expr::BinaryOp(555					el!(Expr::Num(2.0), 0, 1),556					Add,557					el!(558						Expr::BinaryOp(559							el!(Expr::Num(3.0), 22, 23),560							Mul,561							el!(Expr::Num(4.0), 40, 41)562						),563						22,564						41565					)566				),567				0,568				41569			)570		);571	}572573	/// Comments should be able to be escaped574	#[test]575	fn comment_escaping() {576		assert_eq!(577			parse!("2/*\\*/+*/ - 22"),578			el!(579				Expr::BinaryOp(el!(Expr::Num(2.0), 0, 1), Sub, el!(Expr::Num(22.0), 12, 14)),580				0,581				14582			)583		);584	}585586	#[test]587	fn suffix() {588		// assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));589		// assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));590		// assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));591		// assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))592	}593594	#[test]595	fn array_comp() {596		use Expr::*;597		/*598		`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`,599		`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`600				*/601		assert_eq!(602			parse!("[std.deepJoin(x) for x in arr]"),603			el!(604				ArrComp(605					el!(606						Apply(607							el!(608								Index(609									el!(Var("std".into()), 1, 4),610									el!(Str("deepJoin".into()), 5, 13)611								),612								1,613								13614							),615							ArgsDesc::new(vec![el!(Var("x".into()), 14, 15)], vec![]),616							false,617						),618						1,619						16620					),621					vec![CompSpec::ForSpec(ForSpecData(622						"x".into(),623						el!(Var("arr".into()), 26, 29)624					))]625				),626				0,627				30628			),629		)630	}631632	#[test]633	fn reserved() {634		use Expr::*;635		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null), 0, 4));636		assert_eq!(parse!("nulla"), el!(Var("nulla".into()), 0, 5));637	}638639	#[test]640	fn multiple_args_buf() {641		parse!("a(b, null_fields)");642	}643644	#[test]645	fn infix_precedence() {646		use Expr::*;647		assert_eq!(648			parse!("!a && !b"),649			el!(650				BinaryOp(651					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),652					And,653					el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 7, 8)), 6, 8)654				),655				0,656				8657			)658		);659	}660661	#[test]662	fn infix_precedence_division() {663		use Expr::*;664		assert_eq!(665			parse!("!a / !b"),666			el!(667				BinaryOp(668					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),669					Div,670					el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 6, 7)), 5, 7)671				),672				0,673				7674			)675		);676	}677678	#[test]679	fn double_negation() {680		use Expr::*;681		assert_eq!(682			parse!("!!a"),683			el!(684				UnaryOp(685					UnaryOpType::Not,686					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 2, 3)), 1, 3)687				),688				0,689				3690			)691		)692	}693694	#[test]695	fn array_test_error() {696		parse!("[a for a in b if c for e in f]");697		//                    ^^^^ failed code698	}699700	#[test]701	fn missing_newline_between_comment_and_eof() {702		parse!(703			"{a:1}704705			//+213"706		);707	}708709	#[test]710	fn default_param_before_nondefault() {711		parse!("local x(foo = 'foo', bar) = null; null");712	}713714	#[test]715	fn can_parse_stdlib() {716		parse!(jrsonnet_stdlib::STDLIB_STR);717	}718719	#[test]720	fn add_location_info_to_all_sub_expressions() {721		use Expr::*;722723		let file_name: std::rc::Rc<std::path::Path> = PathBuf::from("test.jsonnet").into();724		let expr = parse(725			"{} { local x = 1, x: x } + {}",726			&ParserSettings {727				file_name: file_name.clone(),728			},729		)730		.unwrap();731		assert_eq!(732			expr,733			el!(734				BinaryOp(735					el!(736						ObjExtend(737							el!(Obj(ObjBody::MemberList(vec![])), 0, 2),738							ObjBody::MemberList(vec![739								Member::BindStmt(BindSpec::Field {740									into: Destruct::Full("x".into()),741									value: el!(Num(1.0), 15, 16)742								}),743								Member::Field(FieldMember {744									name: FieldName::Fixed("x".into()),745									plus: false,746									params: None,747									visibility: Visibility::Normal,748									value: el!(Var("x".into()), 21, 22),749								})750							])751						),752						0,753						24754					),755					BinaryOpType::Add,756					el!(Obj(ObjBody::MemberList(vec![])), 27, 29),757				),758				0,759				29760			),761		);762	}763	// From source code764	/*765	#[bench]766	fn bench_parse_peg(b: &mut Bencher) {767		b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))768	}769	*/770}
addedcrates/jrsonnet-parser/src/source.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-parser/src/source.rs
@@ -0,0 +1,93 @@
+use std::{
+	borrow::Cow,
+	fmt,
+	path::{Component, Path, PathBuf},
+	rc::Rc,
+};
+
+use gcmodule::{Trace, Tracer};
+#[cfg(feature = "serde")]
+use serde::{Deserialize, Serialize};
+
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[derive(PartialEq, Eq, Debug, Hash)]
+enum Inner {
+	Real(PathBuf),
+	Virtual(Cow<'static, str>),
+}
+
+/// Either real file, or virtual
+/// Hash of FileName always have same value as raw Path, to make it possible to use with raw_entry_mut
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[derive(Clone, PartialEq, Eq, Debug)]
+pub struct Source(Rc<Inner>);
+static_assertions::assert_eq_size!(Source, *const ());
+
+impl Trace for Source {
+	fn trace(&self, _tracer: &mut Tracer) {}
+
+	fn is_type_tracked() -> bool {
+		false
+	}
+}
+
+impl Source {
+	/// Fails when path contains inner /../ or /./ references, or not absolute
+	pub fn new(path: PathBuf) -> Option<Self> {
+		if !path.is_absolute()
+			|| path
+				.components()
+				.any(|c| matches!(c, Component::CurDir | Component::ParentDir))
+		{
+			return None;
+		}
+		Some(Self(Rc::new(Inner::Real(path))))
+	}
+
+	pub fn new_virtual(n: Cow<'static, str>) -> Self {
+		Self(Rc::new(Inner::Virtual(n)))
+	}
+
+	pub fn short_display(&self) -> ShortDisplay {
+		ShortDisplay(self.clone())
+	}
+	pub fn full_path(&self) -> String {
+		match self.inner() {
+			Inner::Real(r) => r.display().to_string(),
+			Inner::Virtual(v) => v.to_string(),
+		}
+	}
+
+	/// Returns None if file is virtual
+	pub fn path(&self) -> Option<&Path> {
+		match self.inner() {
+			Inner::Real(r) => Some(r),
+			Inner::Virtual(_) => None,
+		}
+	}
+	pub fn repr(&self) -> Result<&Path, &str> {
+		match self.inner() {
+			Inner::Real(r) => Ok(r),
+			Inner::Virtual(v) => Err(v.as_ref()),
+		}
+	}
+
+	fn inner(&self) -> &Inner {
+		&self.0 as &Inner
+	}
+}
+pub struct ShortDisplay(Source);
+impl fmt::Display for ShortDisplay {
+	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+		match &self.0 .0 as &Inner {
+			Inner::Real(r) => {
+				write!(
+					f,
+					"{}",
+					r.file_name().expect("path is valid").to_string_lossy()
+				)
+			}
+			Inner::Virtual(n) => write!(f, "{}", n),
+		}
+	}
+}