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

difftreelog

feat support imports from fifo on unix

Yaroslav Bolyukin2024-03-17parent: #d78cbb7.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -9,7 +9,8 @@
 
 use fs::File;
 use jrsonnet_gcmodule::Trace;
-use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};
+use jrsonnet_interner::IBytes;
+use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath, SourceFifo};
 
 use crate::{
 	bail,
@@ -82,6 +83,37 @@
 	}
 }
 
+/// Create `SourcePath` from path, handling directories/Fifo files (on unix)/etc
+fn check_path(path: &Path) -> Result<Option<SourcePath>> {
+	let meta = match fs::metadata(path) {
+		Ok(v) => v,
+		Err(e) if e.kind() == ErrorKind::NotFound => {
+			return Ok(None);
+		}
+		Err(e) => bail!(ImportIo(e.to_string())),
+	};
+	let ty = meta.file_type();
+	if ty.is_file() {
+		return Ok(Some(SourcePath::new(SourceFile::new(
+			path.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
+		))));
+	}
+	let ty = meta.file_type();
+	#[cfg(unix)]
+	{
+		use std::os::unix::fs::FileTypeExt;
+		if ty.is_fifo() {
+			let file = fs::read(path).map_err(|e| ImportIo(format!("FIFO read failed: {e}")))?;
+			return Ok(Some(SourcePath::new(SourceFifo(
+				format!("{}", path.display()),
+				IBytes::from(file.as_slice()),
+			))));
+		}
+	}
+	// Block device/some other magic thing.
+	Err(RuntimeError("special file can't be imported".into()).into())
+}
+
 impl ImportResolver for FileImportResolver {
 	fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
 		let mut direct = if let Some(f) = from.downcast_ref::<SourceFile>() {
@@ -95,50 +127,34 @@
 		} else {
 			unreachable!("resolver can't return this path")
 		};
+
 		direct.push(path);
-		if direct.is_file() {
-			Ok(SourcePath::new(SourceFile::new(
-				direct.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
-			)))
-		} else {
-			for library_path in self.library_paths.borrow().iter() {
-				let mut cloned = library_path.clone();
-				cloned.push(path);
-				if cloned.exists() {
-					return Ok(SourcePath::new(SourceFile::new(
-						cloned.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
-					)));
-				}
+		if let Some(direct) = check_path(&direct)? {
+			return Ok(direct);
+		}
+		for library_path in self.library_paths.borrow().iter() {
+			let mut cloned = library_path.clone();
+			cloned.push(path);
+			if let Some(cloned) = check_path(&cloned)? {
+				return Ok(cloned);
 			}
-			bail!(ImportFileNotFound(from.clone(), path.to_owned()))
 		}
+		bail!(ImportFileNotFound(from.clone(), path.to_owned()))
 	}
 	fn resolve(&self, path: &Path) -> Result<SourcePath> {
-		let meta = match fs::metadata(path) {
-			Ok(v) => v,
-			Err(e) if e.kind() == ErrorKind::NotFound => {
-				bail!(AbsoluteImportFileNotFound(path.to_owned()))
-			}
-			Err(e) => bail!(ImportIo(e.to_string())),
+		let Some(source) = check_path(path)? else {
+			bail!(AbsoluteImportFileNotFound(path.to_owned()))
 		};
-		if meta.is_file() {
-			Ok(SourcePath::new(SourceFile::new(
-				path.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
-			)))
-		} else if meta.is_dir() {
-			Ok(SourcePath::new(SourceDirectory::new(
-				path.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
-			)))
-		} else {
-			unreachable!("this can't be a symlink")
-		}
+		Ok(source)
 	}
 
 	fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {
 		let path = if let Some(f) = id.downcast_ref::<SourceFile>() {
 			f.path()
-		} else if id.downcast_ref::<SourceDirectory>().is_some() || id.is_default() {
+		} else if id.downcast_ref::<SourceDirectory>().is_some() {
 			bail!(ImportIsADirectory(id.clone()))
+		} else if let Some(f) = id.downcast_ref::<SourceFifo>() {
+			return Ok(f.1.to_vec());
 		} else {
 			unreachable!("other types are not supported in resolve");
 		};
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-parser/src/lib.rs
1#![allow(clippy::redundant_closure_call, clippy::derive_partial_eq_without_eq)]23use std::rc::Rc;45use peg::parser;6mod expr;7pub use expr::*;8pub use jrsonnet_interner::IStr;9pub use peg;10mod location;11mod source;12mod unescape;13pub use location::CodeLocation;14pub use source::{Source, SourceDirectory, SourceFile, SourcePath, SourcePathT, SourceVirtual};1516pub struct ParserSettings {17	pub source: Source,18}1920macro_rules! expr_bin {21	($a:ident $op:ident $b:ident) => {22		Expr::BinaryOp($a, $op, $b)23	};24}25macro_rules! expr_un {26	($op:ident $a:ident) => {27		Expr::UnaryOp($op, $a)28	};29}3031parser! {32	grammar jsonnet_parser() for str {33		use peg::ParseLiteral;3435		rule eof() = quiet!{![_]} / expected!("<eof>")36		rule eol() = "\n" / eof()3738		/// Standard C-like comments39		rule comment()40			= "//" (!eol()[_])* eol()41			/ "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"42			/ "#" (!eol()[_])* eol()4344		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")45		rule _() = quiet!{([' ' | '\r' | '\n' | '\t']+) / comment()}* / expected!("<whitespace>")4647		/// For comma-delimited elements48		rule comma() = quiet!{_ "," _} / expected!("<comma>")49		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}50		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}51		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']52		/// Sequence of digits53		rule uint_str() -> &'input str = a:$(digit()+) { a }54		/// Number in scientific notation format55		rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")5657		/// Reserved word followed by any non-alphanumberic58		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()59		rule id() -> IStr = v:$(quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")) { v.into() }6061		rule keyword(id: &'static str) -> ()62			= ##parse_string_literal(id) end_of_ident()6364		pub rule param(s: &ParserSettings) -> expr::Param = name:destruct(s) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr) }65		pub rule params(s: &ParserSettings) -> expr::ParamsDesc66			= params:param(s) ** comma() comma()? { expr::ParamsDesc(Rc::new(params)) }67			/ { expr::ParamsDesc(Rc::new(Vec::new())) }6869		pub rule arg(s: &ParserSettings) -> (Option<IStr>, LocExpr)70			= name:(quiet! { (s:id() _ "=" !['='] _ {s})? } / expected!("<argument name>")) expr:expr(s) {(name, expr)}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})? default:(_ "=" _ v:expr(s) {v})? {(name, into, default)})**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})* &"[" field:field(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					field,203					post_locals,204					compspecs,205				})206			}207			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}208		pub rule ifspec(s: &ParserSettings) -> IfSpecData209			= keyword("if") _ expr:expr(s) {IfSpecData(expr)}210		pub rule forspec(s: &ParserSettings) -> ForSpecData211			= keyword("for") _ id:destruct(s) _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}212		pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>213			= s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}214		pub rule local_expr(s: &ParserSettings) -> Expr215			= keyword("local") _ binds:bind(s) ** comma() (_ ",")? _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }216		pub rule string_expr(s: &ParserSettings) -> Expr217			= s:string() {Expr::Str(s.into())}218		pub rule obj_expr(s: &ParserSettings) -> Expr219			= "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}220		pub rule array_expr(s: &ParserSettings) -> Expr221			= "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}222		pub rule array_comp_expr(s: &ParserSettings) -> Expr223			= "[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {224				let mut specs = vec![CompSpec::ForSpec(forspec)];225				specs.extend(others.unwrap_or_default());226				Expr::ArrComp(expr, specs)227			}228		pub rule number_expr(s: &ParserSettings) -> Expr229			= n:number() { expr::Expr::Num(n) }230		pub rule var_expr(s: &ParserSettings) -> Expr231			= n:id() { expr::Expr::Var(n) }232		pub rule id_loc(s: &ParserSettings) -> LocExpr233			= a:position!() n:id() b:position!() { LocExpr(Rc::new(expr::Expr::Str(n)), ExprLocation(s.source.clone(), a as u32,b as u32)) }234		pub rule if_then_else_expr(s: &ParserSettings) -> Expr235			= cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{236				cond,237				cond_then,238				cond_else,239			}}240241		pub rule literal(s: &ParserSettings) -> Expr242			= v:(243				keyword("null") {LiteralType::Null}244				/ keyword("true") {LiteralType::True}245				/ keyword("false") {LiteralType::False}246				/ keyword("self") {LiteralType::This}247				/ keyword("$") {LiteralType::Dollar}248				/ keyword("super") {LiteralType::Super}249			) {Expr::Literal(v)}250251		pub rule expr_basic(s: &ParserSettings) -> Expr252			= literal(s)253254			/ string_expr(s) / number_expr(s)255			/ array_expr(s)256			/ obj_expr(s)257			/ array_expr(s)258			/ array_comp_expr(s)259260			/ keyword("importstr") _ path:expr(s) {Expr::ImportStr(path)}261			/ keyword("importbin") _ path:expr(s) {Expr::ImportBin(path)}262			/ keyword("import") _ path:expr(s) {Expr::Import(path)}263264			/ var_expr(s)265			/ local_expr(s)266			/ if_then_else_expr(s)267268			/ keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}269			/ assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }270271			/ keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }272273		rule slice_part(s: &ParserSettings) -> Option<LocExpr>274			= _ e:(e:expr(s) _{e})? {e}275		pub rule slice_desc(s: &ParserSettings) -> SliceDesc276			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {277				let (end, step) = if let Some((end, step)) = pair {278					(end, step)279				}else{280					(None, None)281				};282283				SliceDesc { start, end, step }284			}285286		rule binop(x: rule<()>) -> ()287			= quiet!{ x() } / expected!("<binary op>")288		rule unaryop(x: rule<()>) -> ()289			= quiet!{ x() } / expected!("<unary op>")290291		rule ensure_null_coaelse()292			= "" {?293				#[cfg(not(feature = "exp-null-coaelse"))] return Err("!!!experimental null coaelscing was not enabled");294				#[cfg(feature = "exp-null-coaelse")] Ok(())295			}296		use BinaryOpType::*;297		use UnaryOpType::*;298		rule expr(s: &ParserSettings) -> LocExpr299			= precedence! {300				start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.source.clone(), start as u32, end as u32)) }301				--302				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}303				a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {304					#[cfg(feature = "exp-null-coaelse")] return expr_bin!(a NullCoaelse b);305					unreachable!("ensure_null_coaelse will fail if feature is not enabled")306				}307				--308				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}309				--310				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}311				--312				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}313				--314				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}315				--316				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}317				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}318				--319				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}320				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}321				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}322				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}323				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}324				--325				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}326				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}327				--328				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}329				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}330				--331				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}332				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}333				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}334				--335						unaryop(<"+">) _ b:@ {expr_un!(Plus b)}336						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}337						unaryop(<"!">) _ b:@ {expr_un!(Not b)}338						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}339				--340				a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}341				indexable:(@) _ parts:index_part(s)+ {Expr::Index{indexable, parts}}342				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}343				a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}344				--345				e:expr_basic(s) {e}346				"(" _ e:expr(s) _ ")" {Expr::Parened(e)}347			}348		pub rule index_part(s: &ParserSettings) -> IndexPart349		= n:("?" _ ensure_null_coaelse())? "." _ value:id_loc(s) {IndexPart {350			value,351			#[cfg(feature = "exp-null-coaelse")]352			null_coaelse: n.is_some(),353		}}354		/ n:("?" _ "." _ ensure_null_coaelse())? "[" _ value:expr(s) _ "]" {IndexPart {355			value,356			#[cfg(feature = "exp-null-coaelse")]357			null_coaelse: n.is_some(),358		}}359360		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}361	}362}363364pub type ParseError = peg::error::ParseError<peg::str::LineCol>;365pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {366	jsonnet_parser::jsonnet(str, settings)367}368/// Used for importstr values369pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {370	let len = str.len();371	LocExpr(372		Rc::new(Expr::Str(str)),373		ExprLocation(settings.source.clone(), 0, len as u32),374	)375}376377#[cfg(test)]378pub mod tests {379	use jrsonnet_interner::IStr;380	use BinaryOpType::*;381382	use super::{expr::*, parse};383	use crate::{source::Source, ParserSettings};384385	macro_rules! parse {386		($s:expr) => {387			parse(388				$s,389				&ParserSettings {390					source: Source::new_virtual("<test>".into(), IStr::empty()),391				},392			)393			.unwrap()394		};395	}396397	macro_rules! el {398		($expr:expr, $from:expr, $to:expr$(,)?) => {399			LocExpr(400				std::rc::Rc::new($expr),401				ExprLocation(402					Source::new_virtual("<test>".into(), IStr::empty()),403					$from,404					$to,405				),406			)407		};408	}409410	#[test]411	fn multiline_string() {412		assert_eq!(413			parse!("|||\n    Hello world!\n     a\n|||"),414			el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),415		);416		assert_eq!(417			parse!("|||\n  Hello world!\n   a\n|||"),418			el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),419		);420		assert_eq!(421			parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),422			el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),423		);424		assert_eq!(425			parse!("|||\n   Hello world!\n    a\n |||"),426			el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),427		);428	}429430	#[test]431	fn slice() {432		parse!("a[1:]");433		parse!("a[1::]");434		parse!("a[:1:]");435		parse!("a[::1]");436		parse!("str[:len - 1]");437	}438439	#[test]440	fn string_escaping() {441		assert_eq!(442			parse!(r#""Hello, \"world\"!""#),443			el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),444		);445		assert_eq!(446			parse!(r#"'Hello \'world\'!'"#),447			el!(Expr::Str("Hello 'world'!".into()), 0, 18),448		);449		assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));450	}451452	#[test]453	fn string_unescaping() {454		assert_eq!(455			parse!(r#""Hello\nWorld""#),456			el!(Expr::Str("Hello\nWorld".into()), 0, 14),457		);458	}459460	#[test]461	fn string_verbantim() {462		assert_eq!(463			parse!(r#"@"Hello\n""World""""#),464			el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),465		);466	}467468	#[test]469	fn imports() {470		assert_eq!(471			parse!("import \"hello\""),472			el!(Expr::Import(el!(Expr::Str("hello".into()), 7, 14)), 0, 14),473		);474		assert_eq!(475			parse!("importstr \"garnish.txt\""),476			el!(477				Expr::ImportStr(el!(Expr::Str("garnish.txt".into()), 10, 23)),478				0,479				23480			)481		);482		assert_eq!(483			parse!("importbin \"garnish.bin\""),484			el!(485				Expr::ImportBin(el!(Expr::Str("garnish.bin".into()), 10, 23)),486				0,487				23488			)489		);490	}491492	#[test]493	fn empty_object() {494		assert_eq!(495			parse!("{}"),496			el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)497		);498	}499500	#[test]501	fn basic_math() {502		assert_eq!(503			parse!("2+2*2"),504			el!(505				Expr::BinaryOp(506					el!(Expr::Num(2.0), 0, 1),507					Add,508					el!(509						Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),510						2,511						5512					)513				),514				0,515				5516			)517		);518	}519520	#[test]521	fn basic_math_with_indents() {522		assert_eq!(523			parse!("2	+ 	  2	  *	2   	"),524			el!(525				Expr::BinaryOp(526					el!(Expr::Num(2.0), 0, 1),527					Add,528					el!(529						Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),530						7,531						14532					),533				),534				0,535				14536			)537		);538	}539540	#[test]541	fn basic_math_parened() {542		assert_eq!(543			parse!("2+(2+2*2)"),544			el!(545				Expr::BinaryOp(546					el!(Expr::Num(2.0), 0, 1),547					Add,548					el!(549						Expr::Parened(el!(550							Expr::BinaryOp(551								el!(Expr::Num(2.0), 3, 4),552								Add,553								el!(554									Expr::BinaryOp(555										el!(Expr::Num(2.0), 5, 6),556										Mul,557										el!(Expr::Num(2.0), 7, 8),558									),559									5,560									8561								),562							),563							3,564							8565						)),566						2,567						9568					),569				),570				0,571				9572			)573		);574	}575576	/// Comments should not affect parsing577	#[test]578	fn comments() {579		assert_eq!(580			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),581			el!(582				Expr::BinaryOp(583					el!(Expr::Num(2.0), 0, 1),584					Add,585					el!(586						Expr::BinaryOp(587							el!(Expr::Num(3.0), 22, 23),588							Mul,589							el!(Expr::Num(4.0), 40, 41)590						),591						22,592						41593					)594				),595				0,596				41597			)598		);599	}600601	/// Comments should be able to be escaped602	#[test]603	fn comment_escaping() {604		assert_eq!(605			parse!("2/*\\*/+*/ - 22"),606			el!(607				Expr::BinaryOp(el!(Expr::Num(2.0), 0, 1), Sub, el!(Expr::Num(22.0), 12, 14)),608				0,609				14610			)611		);612	}613614	#[test]615	fn suffix() {616		// assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));617		// assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));618		// assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));619		// assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))620	}621622	#[test]623	fn array_comp() {624		use Expr::*;625		/*626		`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`,627		`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`628				*/629		assert_eq!(630			parse!("[std.deepJoin(x) for x in arr]"),631			el!(632				ArrComp(633					el!(634						Apply(635							el!(636								Index {637									indexable: el!(Var("std".into()), 1, 4),638									parts: vec![IndexPart {639										value: el!(Str("deepJoin".into()), 5, 13),640										#[cfg(feature = "exp-null-coaelse")]641										null_coaelse: false,642									}],643								},644								1,645								13646							),647							ArgsDesc::new(vec![el!(Var("x".into()), 14, 15)], vec![]),648							false,649						),650						1,651						16652					),653					vec![CompSpec::ForSpec(ForSpecData(654						Destruct::Full("x".into()),655						el!(Var("arr".into()), 26, 29)656					))]657				),658				0,659				30660			),661		)662	}663664	#[test]665	fn reserved() {666		use Expr::*;667		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null), 0, 4));668		assert_eq!(parse!("nulla"), el!(Var("nulla".into()), 0, 5));669	}670671	#[test]672	fn multiple_args_buf() {673		parse!("a(b, null_fields)");674	}675676	#[test]677	fn infix_precedence() {678		use Expr::*;679		assert_eq!(680			parse!("!a && !b"),681			el!(682				BinaryOp(683					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),684					And,685					el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 7, 8)), 6, 8)686				),687				0,688				8689			)690		);691	}692693	#[test]694	fn infix_precedence_division() {695		use Expr::*;696		assert_eq!(697			parse!("!a / !b"),698			el!(699				BinaryOp(700					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),701					Div,702					el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 6, 7)), 5, 7)703				),704				0,705				7706			)707		);708	}709710	#[test]711	fn double_negation() {712		use Expr::*;713		assert_eq!(714			parse!("!!a"),715			el!(716				UnaryOp(717					UnaryOpType::Not,718					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 2, 3)), 1, 3)719				),720				0,721				3722			)723		)724	}725726	#[test]727	fn array_test_error() {728		parse!("[a for a in b if c for e in f]");729		//                    ^^^^ failed code730	}731732	#[test]733	fn missing_newline_between_comment_and_eof() {734		parse!(735			"{a:1}736737			//+213"738		);739	}740741	#[test]742	fn default_param_before_nondefault() {743		parse!("local x(foo = 'foo', bar) = null; null");744	}745746	#[test]747	fn add_location_info_to_all_sub_expressions() {748		use Expr::*;749750		let file_name = Source::new_virtual("<test>".into(), IStr::empty());751		let expr = parse(752			"{} { local x = 1, x: x } + {}",753			&ParserSettings { source: file_name },754		)755		.unwrap();756		assert_eq!(757			expr,758			el!(759				BinaryOp(760					el!(761						ObjExtend(762							el!(Obj(ObjBody::MemberList(vec![])), 0, 2),763							ObjBody::MemberList(vec![764								Member::BindStmt(BindSpec::Field {765									into: Destruct::Full("x".into()),766									value: el!(Num(1.0), 15, 16)767								}),768								Member::Field(FieldMember {769									name: FieldName::Fixed("x".into()),770									plus: false,771									params: None,772									visibility: Visibility::Normal,773									value: el!(Var("x".into()), 21, 22),774								})775							])776						),777						0,778						24779					),780					BinaryOpType::Add,781					el!(Obj(ObjBody::MemberList(vec![])), 27, 29),782				),783				0,784				29785			),786		);787	}788}
after · crates/jrsonnet-parser/src/lib.rs
1#![allow(clippy::redundant_closure_call, clippy::derive_partial_eq_without_eq)]23use std::rc::Rc;45use peg::parser;6mod expr;7pub use expr::*;8pub use jrsonnet_interner::IStr;9pub use peg;10mod location;11mod source;12mod unescape;13pub use location::CodeLocation;14pub use source::{15	Source, SourceDirectory, SourceFifo, SourceFile, SourcePath, SourcePathT, SourceVirtual,16};1718pub struct ParserSettings {19	pub source: Source,20}2122macro_rules! expr_bin {23	($a:ident $op:ident $b:ident) => {24		Expr::BinaryOp($a, $op, $b)25	};26}27macro_rules! expr_un {28	($op:ident $a:ident) => {29		Expr::UnaryOp($op, $a)30	};31}3233parser! {34	grammar jsonnet_parser() for str {35		use peg::ParseLiteral;3637		rule eof() = quiet!{![_]} / expected!("<eof>")38		rule eol() = "\n" / eof()3940		/// Standard C-like comments41		rule comment()42			= "//" (!eol()[_])* eol()43			/ "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"44			/ "#" (!eol()[_])* eol()4546		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")47		rule _() = quiet!{([' ' | '\r' | '\n' | '\t']+) / comment()}* / expected!("<whitespace>")4849		/// For comma-delimited elements50		rule comma() = quiet!{_ "," _} / expected!("<comma>")51		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}52		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}53		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']54		/// Sequence of digits55		rule uint_str() -> &'input str = a:$(digit()+) { a }56		/// Number in scientific notation format57		rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")5859		/// Reserved word followed by any non-alphanumberic60		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()61		rule id() -> IStr = v:$(quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")) { v.into() }6263		rule keyword(id: &'static str) -> ()64			= ##parse_string_literal(id) end_of_ident()6566		pub rule param(s: &ParserSettings) -> expr::Param = name:destruct(s) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr) }67		pub rule params(s: &ParserSettings) -> expr::ParamsDesc68			= params:param(s) ** comma() comma()? { expr::ParamsDesc(Rc::new(params)) }69			/ { expr::ParamsDesc(Rc::new(Vec::new())) }7071		pub rule arg(s: &ParserSettings) -> (Option<IStr>, LocExpr)72			= name:(quiet! { (s:id() _ "=" !['='] _ {s})? } / expected!("<argument name>")) expr:expr(s) {(name, expr)}7374		pub rule args(s: &ParserSettings) -> expr::ArgsDesc75			= args:arg(s)**comma() comma()? {?76				let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();77				let mut unnamed = Vec::with_capacity(unnamed_count);78				let mut named = Vec::with_capacity(args.len() - unnamed_count);79				let mut named_started = false;80				for (name, value) in args {81					if let Some(name) = name {82						named_started = true;83						named.push((name, value));84					} else {85						if named_started {86							return Err("<named argument>")87						}88						unnamed.push(value);89					}90				}91				Ok(expr::ArgsDesc::new(unnamed, named))92			}9394		pub rule destruct_rest() -> expr::DestructRest95			= "..." into:(_ into:id() {into})? {if let Some(into) = into {96				expr::DestructRest::Keep(into)97			} else {expr::DestructRest::Drop}}98		pub rule destruct_array(s: &ParserSettings) -> expr::Destruct99			= "[" _ start:destruct(s)**comma() rest:(100				comma() _ rest:destruct_rest()? end:(101					comma() end:destruct(s)**comma() (_ comma())? {end}102					/ comma()? {Vec::new()}103				) {(rest, end)}104				/ comma()? {(None, Vec::new())}105			) _ "]" {?106				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Array {107					start,108					rest: rest.0,109					end: rest.1,110				});111				#[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")112			}113		pub rule destruct_object(s: &ParserSettings) -> expr::Destruct114			= "{" _115				fields:(name:id() into:(_ ":" _ into:destruct(s) {into})? default:(_ "=" _ v:expr(s) {v})? {(name, into, default)})**comma()116				rest:(117					comma() rest:destruct_rest()? {rest}118					/ comma()? {None}119				)120			_ "}" {?121				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Object {122					fields,123					rest,124				});125				#[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")126			}127		pub rule destruct(s: &ParserSettings) -> expr::Destruct128			= v:id() {expr::Destruct::Full(v)}129			/ "?" {?130				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Skip);131				#[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")132			}133			/ arr:destruct_array(s) {arr}134			/ obj:destruct_object(s) {obj}135136		pub rule bind(s: &ParserSettings) -> expr::BindSpec137			= into:destruct(s) _ "=" _ expr:expr(s) {expr::BindSpec::Field{into, value: expr}}138			/ name:id() _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec::Function{name, params, value: expr}}139140		pub rule assertion(s: &ParserSettings) -> expr::AssertStmt141			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }142143		pub rule whole_line() -> &'input str144			= str:$((!['\n'][_])* "\n") {str}145		pub rule string_block() -> String146			= "|||" (!['\n']single_whitespace())* "\n"147			  empty_lines:$(['\n']*)148			  prefix:[' ' | '\t']+ first_line:whole_line()149			  lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*150			  [' ' | '\t']*<, {prefix.len() - 1}> "|||"151			  {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}152153		rule hex_char()154			= quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")155156		rule string_char(c: rule<()>)157			= (!['\\']!c()[_])+158			/ "\\\\"159			/ "\\u" hex_char() hex_char() hex_char() hex_char()160			/ "\\x" hex_char() hex_char()161			/ ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't' | '"' | '\''] } / expected!("<escape character>"))162		pub rule string() -> String163			= ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}164			/ ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}165			/ quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}166			/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}167			/ string_block() } / expected!("<string>")168169		pub rule field_name(s: &ParserSettings) -> expr::FieldName170			= name:id() {expr::FieldName::Fixed(name)}171			/ name:string() {expr::FieldName::Fixed(name.into())}172			/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}173		pub rule visibility() -> expr::Visibility174			= ":::" {expr::Visibility::Unhide}175			/ "::" {expr::Visibility::Hidden}176			/ ":" {expr::Visibility::Normal}177		pub rule field(s: &ParserSettings) -> expr::FieldMember178			= name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{179				name,180				plus: plus.is_some(),181				params: None,182				visibility,183				value,184			}}185			/ name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{186				name,187				plus: false,188				params: Some(params),189				visibility,190				value,191			}}192		pub rule obj_local(s: &ParserSettings) -> BindSpec193			= keyword("local") _ bind:bind(s) {bind}194		pub rule member(s: &ParserSettings) -> expr::Member195			= bind:obj_local(s) {expr::Member::BindStmt(bind)}196			/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}197			/ field:field(s) {expr::Member::Field(field)}198		pub rule objinside(s: &ParserSettings) -> expr::ObjBody199			= pre_locals:(b: obj_local(s) comma() {b})* &"[" field:field(s) post_locals:(comma() b:obj_local(s) {b})* _ ("," _)? forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {200				let mut compspecs = vec![CompSpec::ForSpec(forspec)];201				compspecs.extend(others.unwrap_or_default());202				expr::ObjBody::ObjComp(expr::ObjComp{203					pre_locals,204					field,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:destruct(s) _ 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.source.clone(), a as u32,b as u32)) }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			/ string_expr(s) / number_expr(s)257			/ array_expr(s)258			/ obj_expr(s)259			/ array_expr(s)260			/ array_comp_expr(s)261262			/ keyword("importstr") _ path:expr(s) {Expr::ImportStr(path)}263			/ keyword("importbin") _ path:expr(s) {Expr::ImportBin(path)}264			/ keyword("import") _ path:expr(s) {Expr::Import(path)}265266			/ var_expr(s)267			/ local_expr(s)268			/ if_then_else_expr(s)269270			/ keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}271			/ assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }272273			/ keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }274275		rule slice_part(s: &ParserSettings) -> Option<LocExpr>276			= _ e:(e:expr(s) _{e})? {e}277		pub rule slice_desc(s: &ParserSettings) -> SliceDesc278			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {279				let (end, step) = if let Some((end, step)) = pair {280					(end, step)281				}else{282					(None, None)283				};284285				SliceDesc { start, end, step }286			}287288		rule binop(x: rule<()>) -> ()289			= quiet!{ x() } / expected!("<binary op>")290		rule unaryop(x: rule<()>) -> ()291			= quiet!{ x() } / expected!("<unary op>")292293		rule ensure_null_coaelse()294			= "" {?295				#[cfg(not(feature = "exp-null-coaelse"))] return Err("!!!experimental null coaelscing was not enabled");296				#[cfg(feature = "exp-null-coaelse")] Ok(())297			}298		use BinaryOpType::*;299		use UnaryOpType::*;300		rule expr(s: &ParserSettings) -> LocExpr301			= precedence! {302				start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.source.clone(), start as u32, end as u32)) }303				--304				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}305				a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {306					#[cfg(feature = "exp-null-coaelse")] return expr_bin!(a NullCoaelse b);307					unreachable!("ensure_null_coaelse will fail if feature is not enabled")308				}309				--310				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}311				--312				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}313				--314				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}315				--316				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}317				--318				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}319				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}320				--321				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}322				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}323				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}324				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}325				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}326				--327				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}328				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}329				--330				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}331				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}332				--333				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}334				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}335				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}336				--337						unaryop(<"+">) _ b:@ {expr_un!(Plus b)}338						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}339						unaryop(<"!">) _ b:@ {expr_un!(Not b)}340						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}341				--342				a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}343				indexable:(@) _ parts:index_part(s)+ {Expr::Index{indexable, parts}}344				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}345				a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}346				--347				e:expr_basic(s) {e}348				"(" _ e:expr(s) _ ")" {Expr::Parened(e)}349			}350		pub rule index_part(s: &ParserSettings) -> IndexPart351		= n:("?" _ ensure_null_coaelse())? "." _ value:id_loc(s) {IndexPart {352			value,353			#[cfg(feature = "exp-null-coaelse")]354			null_coaelse: n.is_some(),355		}}356		/ n:("?" _ "." _ ensure_null_coaelse())? "[" _ value:expr(s) _ "]" {IndexPart {357			value,358			#[cfg(feature = "exp-null-coaelse")]359			null_coaelse: n.is_some(),360		}}361362		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}363	}364}365366pub type ParseError = peg::error::ParseError<peg::str::LineCol>;367pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {368	jsonnet_parser::jsonnet(str, settings)369}370/// Used for importstr values371pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {372	let len = str.len();373	LocExpr(374		Rc::new(Expr::Str(str)),375		ExprLocation(settings.source.clone(), 0, len as u32),376	)377}378379#[cfg(test)]380pub mod tests {381	use jrsonnet_interner::IStr;382	use BinaryOpType::*;383384	use super::{expr::*, parse};385	use crate::{source::Source, ParserSettings};386387	macro_rules! parse {388		($s:expr) => {389			parse(390				$s,391				&ParserSettings {392					source: Source::new_virtual("<test>".into(), IStr::empty()),393				},394			)395			.unwrap()396		};397	}398399	macro_rules! el {400		($expr:expr, $from:expr, $to:expr$(,)?) => {401			LocExpr(402				std::rc::Rc::new($expr),403				ExprLocation(404					Source::new_virtual("<test>".into(), IStr::empty()),405					$from,406					$to,407				),408			)409		};410	}411412	#[test]413	fn multiline_string() {414		assert_eq!(415			parse!("|||\n    Hello world!\n     a\n|||"),416			el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),417		);418		assert_eq!(419			parse!("|||\n  Hello world!\n   a\n|||"),420			el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),421		);422		assert_eq!(423			parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),424			el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),425		);426		assert_eq!(427			parse!("|||\n   Hello world!\n    a\n |||"),428			el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),429		);430	}431432	#[test]433	fn slice() {434		parse!("a[1:]");435		parse!("a[1::]");436		parse!("a[:1:]");437		parse!("a[::1]");438		parse!("str[:len - 1]");439	}440441	#[test]442	fn string_escaping() {443		assert_eq!(444			parse!(r#""Hello, \"world\"!""#),445			el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),446		);447		assert_eq!(448			parse!(r#"'Hello \'world\'!'"#),449			el!(Expr::Str("Hello 'world'!".into()), 0, 18),450		);451		assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));452	}453454	#[test]455	fn string_unescaping() {456		assert_eq!(457			parse!(r#""Hello\nWorld""#),458			el!(Expr::Str("Hello\nWorld".into()), 0, 14),459		);460	}461462	#[test]463	fn string_verbantim() {464		assert_eq!(465			parse!(r#"@"Hello\n""World""""#),466			el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),467		);468	}469470	#[test]471	fn imports() {472		assert_eq!(473			parse!("import \"hello\""),474			el!(Expr::Import(el!(Expr::Str("hello".into()), 7, 14)), 0, 14),475		);476		assert_eq!(477			parse!("importstr \"garnish.txt\""),478			el!(479				Expr::ImportStr(el!(Expr::Str("garnish.txt".into()), 10, 23)),480				0,481				23482			)483		);484		assert_eq!(485			parse!("importbin \"garnish.bin\""),486			el!(487				Expr::ImportBin(el!(Expr::Str("garnish.bin".into()), 10, 23)),488				0,489				23490			)491		);492	}493494	#[test]495	fn empty_object() {496		assert_eq!(497			parse!("{}"),498			el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)499		);500	}501502	#[test]503	fn basic_math() {504		assert_eq!(505			parse!("2+2*2"),506			el!(507				Expr::BinaryOp(508					el!(Expr::Num(2.0), 0, 1),509					Add,510					el!(511						Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),512						2,513						5514					)515				),516				0,517				5518			)519		);520	}521522	#[test]523	fn basic_math_with_indents() {524		assert_eq!(525			parse!("2	+ 	  2	  *	2   	"),526			el!(527				Expr::BinaryOp(528					el!(Expr::Num(2.0), 0, 1),529					Add,530					el!(531						Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),532						7,533						14534					),535				),536				0,537				14538			)539		);540	}541542	#[test]543	fn basic_math_parened() {544		assert_eq!(545			parse!("2+(2+2*2)"),546			el!(547				Expr::BinaryOp(548					el!(Expr::Num(2.0), 0, 1),549					Add,550					el!(551						Expr::Parened(el!(552							Expr::BinaryOp(553								el!(Expr::Num(2.0), 3, 4),554								Add,555								el!(556									Expr::BinaryOp(557										el!(Expr::Num(2.0), 5, 6),558										Mul,559										el!(Expr::Num(2.0), 7, 8),560									),561									5,562									8563								),564							),565							3,566							8567						)),568						2,569						9570					),571				),572				0,573				9574			)575		);576	}577578	/// Comments should not affect parsing579	#[test]580	fn comments() {581		assert_eq!(582			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),583			el!(584				Expr::BinaryOp(585					el!(Expr::Num(2.0), 0, 1),586					Add,587					el!(588						Expr::BinaryOp(589							el!(Expr::Num(3.0), 22, 23),590							Mul,591							el!(Expr::Num(4.0), 40, 41)592						),593						22,594						41595					)596				),597				0,598				41599			)600		);601	}602603	/// Comments should be able to be escaped604	#[test]605	fn comment_escaping() {606		assert_eq!(607			parse!("2/*\\*/+*/ - 22"),608			el!(609				Expr::BinaryOp(el!(Expr::Num(2.0), 0, 1), Sub, el!(Expr::Num(22.0), 12, 14)),610				0,611				14612			)613		);614	}615616	#[test]617	fn suffix() {618		// assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));619		// assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));620		// assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));621		// assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))622	}623624	#[test]625	fn array_comp() {626		use Expr::*;627		/*628		`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`,629		`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`630				*/631		assert_eq!(632			parse!("[std.deepJoin(x) for x in arr]"),633			el!(634				ArrComp(635					el!(636						Apply(637							el!(638								Index {639									indexable: el!(Var("std".into()), 1, 4),640									parts: vec![IndexPart {641										value: el!(Str("deepJoin".into()), 5, 13),642										#[cfg(feature = "exp-null-coaelse")]643										null_coaelse: false,644									}],645								},646								1,647								13648							),649							ArgsDesc::new(vec![el!(Var("x".into()), 14, 15)], vec![]),650							false,651						),652						1,653						16654					),655					vec![CompSpec::ForSpec(ForSpecData(656						Destruct::Full("x".into()),657						el!(Var("arr".into()), 26, 29)658					))]659				),660				0,661				30662			),663		)664	}665666	#[test]667	fn reserved() {668		use Expr::*;669		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null), 0, 4));670		assert_eq!(parse!("nulla"), el!(Var("nulla".into()), 0, 5));671	}672673	#[test]674	fn multiple_args_buf() {675		parse!("a(b, null_fields)");676	}677678	#[test]679	fn infix_precedence() {680		use Expr::*;681		assert_eq!(682			parse!("!a && !b"),683			el!(684				BinaryOp(685					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),686					And,687					el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 7, 8)), 6, 8)688				),689				0,690				8691			)692		);693	}694695	#[test]696	fn infix_precedence_division() {697		use Expr::*;698		assert_eq!(699			parse!("!a / !b"),700			el!(701				BinaryOp(702					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),703					Div,704					el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 6, 7)), 5, 7)705				),706				0,707				7708			)709		);710	}711712	#[test]713	fn double_negation() {714		use Expr::*;715		assert_eq!(716			parse!("!!a"),717			el!(718				UnaryOp(719					UnaryOpType::Not,720					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 2, 3)), 1, 3)721				),722				0,723				3724			)725		)726	}727728	#[test]729	fn array_test_error() {730		parse!("[a for a in b if c for e in f]");731		//                    ^^^^ failed code732	}733734	#[test]735	fn missing_newline_between_comment_and_eof() {736		parse!(737			"{a:1}738739			//+213"740		);741	}742743	#[test]744	fn default_param_before_nondefault() {745		parse!("local x(foo = 'foo', bar) = null; null");746	}747748	#[test]749	fn add_location_info_to_all_sub_expressions() {750		use Expr::*;751752		let file_name = Source::new_virtual("<test>".into(), IStr::empty());753		let expr = parse(754			"{} { local x = 1, x: x } + {}",755			&ParserSettings { source: file_name },756		)757		.unwrap();758		assert_eq!(759			expr,760			el!(761				BinaryOp(762					el!(763						ObjExtend(764							el!(Obj(ObjBody::MemberList(vec![])), 0, 2),765							ObjBody::MemberList(vec![766								Member::BindStmt(BindSpec::Field {767									into: Destruct::Full("x".into()),768									value: el!(Num(1.0), 15, 16)769								}),770								Member::Field(FieldMember {771									name: FieldName::Fixed("x".into()),772									plus: false,773									params: None,774									visibility: Visibility::Normal,775									value: el!(Var("x".into()), 21, 22),776								})777							])778						),779						0,780						24781					),782					BinaryOpType::Add,783					el!(Obj(ObjBody::MemberList(vec![])), 27, 29),784				),785				0,786				29787			),788		);789	}790}
modifiedcrates/jrsonnet-parser/src/source.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/source.rs
+++ b/crates/jrsonnet-parser/src/source.rs
@@ -7,7 +7,7 @@
 };
 
 use jrsonnet_gcmodule::{Trace, Tracer};
-use jrsonnet_interner::IStr;
+use jrsonnet_interner::{IBytes, IStr};
 #[cfg(feature = "serde")]
 use serde::{Deserialize, Serialize};
 #[cfg(feature = "structdump")]
@@ -75,6 +75,7 @@
 /// - [`SourceFile`] - for any file
 /// - [`SourceDirectory`] - for resolution from CWD
 /// - [`SourceVirtual`] - for stdlib/ext-str
+/// - [`SourceFifo`] - for /dev/fd/X (This path may appear with `jrsonnet <(command_that_produces_jsonnet)`)
 ///
 /// From all of those, only [`SourceVirtual`] may be constructed manually, any other path kind should be only obtained
 /// from assigned `ImportResolver`
@@ -254,6 +255,37 @@
 	any_ext_impl!(SourcePathT);
 }
 
+/// Represents resolved FIFO file, those files may only be read once, and this type is only used for
+/// unix, where user might want to do `jrsonnet <(command_that_produces_jsonnet_source)`
+/// In most cases, user most probably want to use `jrsonnet -` instead of `jrsonnet /dev/stdin`
+/// for better cross-platform support.
+// PartialEq is limited to ptr equality
+#[allow(clippy::derived_hash_with_manual_eq)]
+#[derive(Trace, Debug, Hash)]
+pub struct SourceFifo(pub String, pub IBytes);
+impl PartialEq for SourceFifo {
+	fn eq(&self, other: &Self) -> bool {
+		std::ptr::eq(self, other)
+	}
+}
+impl fmt::Display for SourceFifo {
+	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+		write!(f, "fifo({:?})", self.0)
+	}
+}
+impl SourcePathT for SourceFifo {
+	fn is_default(&self) -> bool {
+		// In case of FD input, user won't expect relative paths to be resolved from /dev/fd/
+		true
+	}
+
+	fn path(&self) -> Option<&Path> {
+		None
+	}
+
+	any_ext_impl!(SourcePathT);
+}
+
 /// 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 = "structdump", derive(Codegen))]