difftreelog
fix ignore jpath when resolving filename passed to jrsonnet
in: master
4 files changed
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -11,6 +11,7 @@
error::{Error as JrError, ErrorKind},
ResultExt, State, Val,
};
+use jrsonnet_parser::{SourceDefaultIgnoreJpath, SourcePath};
#[cfg(feature = "mimalloc")]
#[global_allocator]
@@ -182,7 +183,7 @@
let input_str = std::str::from_utf8(&input)?;
s.evaluate_snippet("<stdin>".to_owned(), input_str)?
} else {
- s.import(input.as_str())?
+ s.import_from(&SourcePath::new(SourceDefaultIgnoreJpath), input.as_str())?
};
let tla = opts.tla.tla_opts()?;
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -10,7 +10,9 @@
use fs::File;
use jrsonnet_gcmodule::Acyclic;
use jrsonnet_interner::IBytes;
-use jrsonnet_parser::{IStr, SourceDirectory, SourceFifo, SourceFile, SourcePath};
+use jrsonnet_parser::{
+ IStr, SourceDefaultIgnoreJpath, SourceDirectory, SourceFifo, SourceFile, SourcePath,
+};
use crate::{
bail,
@@ -183,6 +185,13 @@
o
} else if let Some(d) = from.downcast_ref::<SourceDirectory>() {
d.path().to_owned()
+ } else if from.downcast_ref::<SourceDefaultIgnoreJpath>().is_some() {
+ let mut direct = current_dir().map_err(|e| ImportIo(e.to_string()))?;
+ direct.push(path);
+ if let Some(direct) = check_path(&direct)? {
+ return Ok(direct);
+ }
+ bail!(ImportFileNotFound(from.clone(), path.to_owned()))
} else if from.is_default() {
current_dir().map_err(|e| ImportIo(e.to_string()))?
} else {
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth1#![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 = "|||" chomped:"-"? (!['\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 {152 let mut l = empty_lines.to_owned();153 l.push_str(first_line);154 l.extend(lines);155 if chomped.is_some() {156 debug_assert!(l.ends_with('\n'));157 l.truncate(l.len() - 1);158 }159 l160 }161162 rule hex_char()163 = quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")164165 rule string_char(c: rule<()>)166 = (!['\\']!c()[_])+167 / "\\\\"168 / "\\u" hex_char() hex_char() hex_char() hex_char()169 / "\\x" hex_char() hex_char()170 / ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't' | '"' | '\''] } / expected!("<escape character>"))171 pub rule string() -> String172 = ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}173 / ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}174 / quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}175 / "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}176 / string_block() } / expected!("<string>")177178 pub rule field_name(s: &ParserSettings) -> expr::FieldName179 = name:id() {expr::FieldName::Fixed(name)}180 / name:string() {expr::FieldName::Fixed(name.into())}181 / "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}182 pub rule visibility() -> expr::Visibility183 = ":::" {expr::Visibility::Unhide}184 / "::" {expr::Visibility::Hidden}185 / ":" {expr::Visibility::Normal}186 pub rule field(s: &ParserSettings) -> expr::FieldMember187 = name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{188 name,189 plus: plus.is_some(),190 params: None,191 visibility,192 value,193 }}194 / name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{195 name,196 plus: false,197 params: Some(params),198 visibility,199 value,200 }}201 pub rule obj_local(s: &ParserSettings) -> BindSpec202 = keyword("local") _ bind:bind(s) {bind}203 pub rule member(s: &ParserSettings) -> expr::Member204 = bind:obj_local(s) {expr::Member::BindStmt(bind)}205 / assertion:assertion(s) {expr::Member::AssertStmt(assertion)}206 / field:field(s) {expr::Member::Field(field)}207 pub rule objinside(s: &ParserSettings) -> expr::ObjBody208 = 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})? {209 let mut compspecs = vec![CompSpec::ForSpec(forspec)];210 compspecs.extend(others.unwrap_or_default());211 expr::ObjBody::ObjComp(expr::ObjComp{212 pre_locals,213 field,214 post_locals,215 compspecs,216 })217 }218 / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}219 pub rule ifspec(s: &ParserSettings) -> IfSpecData220 = keyword("if") _ expr:expr(s) {IfSpecData(expr)}221 pub rule forspec(s: &ParserSettings) -> ForSpecData222 = keyword("for") _ id:destruct(s) _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}223 pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>224 = s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}225 pub rule local_expr(s: &ParserSettings) -> Expr226 = keyword("local") _ binds:bind(s) ** comma() (_ ",")? _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }227 pub rule string_expr(s: &ParserSettings) -> Expr228 = s:string() {Expr::Str(s.into())}229 pub rule obj_expr(s: &ParserSettings) -> Expr230 = "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}231 pub rule array_expr(s: &ParserSettings) -> Expr232 = "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}233 pub rule array_comp_expr(s: &ParserSettings) -> Expr234 = "[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {235 let mut specs = vec![CompSpec::ForSpec(forspec)];236 specs.extend(others.unwrap_or_default());237 Expr::ArrComp(expr, specs)238 }239 pub rule number_expr(s: &ParserSettings) -> Expr240 = n:number() {? if n.is_finite() {241 Ok(expr::Expr::Num(n))242 } else {243 Err("!!!numbers are finite")244 }}245 pub rule var_expr(s: &ParserSettings) -> Expr246 = n:id() { expr::Expr::Var(n) }247 pub rule id_loc(s: &ParserSettings) -> LocExpr248 = a:position!() n:id() b:position!() { LocExpr::new(expr::Expr::Str(n), Span(s.source.clone(), a as u32,b as u32)) }249 pub rule if_then_else_expr(s: &ParserSettings) -> Expr250 = cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{251 cond,252 cond_then,253 cond_else,254 }}255256 pub rule literal(s: &ParserSettings) -> Expr257 = v:(258 keyword("null") {LiteralType::Null}259 / keyword("true") {LiteralType::True}260 / keyword("false") {LiteralType::False}261 / keyword("self") {LiteralType::This}262 / keyword("$") {LiteralType::Dollar}263 / keyword("super") {LiteralType::Super}264 ) {Expr::Literal(v)}265266 pub rule expr_basic(s: &ParserSettings) -> Expr267 = literal(s)268269 / string_expr(s) / number_expr(s)270 / array_expr(s)271 / obj_expr(s)272 / array_expr(s)273 / array_comp_expr(s)274275 / keyword("importstr") _ path:expr(s) {Expr::ImportStr(path)}276 / keyword("importbin") _ path:expr(s) {Expr::ImportBin(path)}277 / keyword("import") _ path:expr(s) {Expr::Import(path)}278279 / var_expr(s)280 / local_expr(s)281 / if_then_else_expr(s)282283 / keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}284 / assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }285286 / keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }287288 rule slice_part(s: &ParserSettings) -> Option<LocExpr>289 = _ e:(e:expr(s) _{e})? {e}290 pub rule slice_desc(s: &ParserSettings) -> SliceDesc291 = start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {292 let (end, step) = if let Some((end, step)) = pair {293 (end, step)294 }else{295 (None, None)296 };297298 SliceDesc { start, end, step }299 }300301 rule binop(x: rule<()>) -> ()302 = quiet!{ x() } / expected!("<binary op>")303 rule unaryop(x: rule<()>) -> ()304 = quiet!{ x() } / expected!("<unary op>")305306 rule ensure_null_coaelse()307 = "" {?308 #[cfg(not(feature = "exp-null-coaelse"))] return Err("!!!experimental null coaelscing was not enabled");309 #[cfg(feature = "exp-null-coaelse")] Ok(())310 }311 use BinaryOpType::*;312 use UnaryOpType::*;313 rule expr(s: &ParserSettings) -> LocExpr314 = precedence! {315 start:position!() v:@ end:position!() { LocExpr::new(v, Span(s.source.clone(), start as u32, end as u32)) }316 --317 a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}318 a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {319 #[cfg(feature = "exp-null-coaelse")] return expr_bin!(a NullCoaelse b);320 unreachable!("ensure_null_coaelse will fail if feature is not enabled")321 }322 --323 a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}324 --325 a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}326 --327 a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}328 --329 a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}330 --331 a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}332 a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}333 --334 a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}335 a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}336 a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}337 a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}338 a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}339 --340 a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}341 a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}342 --343 a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}344 a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}345 --346 a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}347 a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}348 a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}349 --350 unaryop(<"+">) _ b:@ {expr_un!(Plus b)}351 unaryop(<"-">) _ b:@ {expr_un!(Minus b)}352 unaryop(<"!">) _ b:@ {expr_un!(Not b)}353 unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}354 --355 a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}356 indexable:(@) _ parts:index_part(s)+ {Expr::Index{indexable, parts}}357 a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}358 a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}359 --360 e:expr_basic(s) {e}361 "(" _ e:expr(s) _ ")" {Expr::Parened(e)}362 }363 pub rule index_part(s: &ParserSettings) -> IndexPart364 = n:("?" _ ensure_null_coaelse())? "." _ value:id_loc(s) {IndexPart {365 value,366 #[cfg(feature = "exp-null-coaelse")]367 null_coaelse: n.is_some(),368 }}369 / n:("?" _ "." _ ensure_null_coaelse())? "[" _ value:expr(s) _ "]" {IndexPart {370 value,371 #[cfg(feature = "exp-null-coaelse")]372 null_coaelse: n.is_some(),373 }}374375 pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}376 }377}378379pub type ParseError = peg::error::ParseError<peg::str::LineCol>;380pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {381 jsonnet_parser::jsonnet(str, settings)382}383/// Used for importstr values384pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {385 let len = str.len();386 LocExpr::new(Expr::Str(str), Span(settings.source.clone(), 0, len as u32))387}388389#[cfg(test)]390pub mod tests {391 use jrsonnet_interner::IStr;392 use BinaryOpType::*;393394 use super::{expr::*, parse};395 use crate::{source::Source, ParserSettings};396397 macro_rules! parse {398 ($s:expr) => {399 parse(400 $s,401 &ParserSettings {402 source: Source::new_virtual("<test>".into(), IStr::empty()),403 },404 )405 .unwrap()406 };407 }408409 macro_rules! el {410 ($expr:expr, $from:expr, $to:expr$(,)?) => {411 LocExpr::new(412 $expr,413 Span(414 Source::new_virtual("<test>".into(), IStr::empty()),415 $from,416 $to,417 ),418 )419 };420 }421422 #[test]423 fn multiline_string() {424 assert_eq!(425 parse!("|||\n Hello world!\n a\n|||"),426 el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),427 );428 assert_eq!(429 parse!("|||\n Hello world!\n a\n|||"),430 el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),431 );432 assert_eq!(433 parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),434 el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),435 );436 assert_eq!(437 parse!("|||\n Hello world!\n a\n |||"),438 el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),439 );440 }441442 #[test]443 fn slice() {444 parse!("a[1:]");445 parse!("a[1::]");446 parse!("a[:1:]");447 parse!("a[::1]");448 parse!("str[:len - 1]");449 }450451 #[test]452 fn string_escaping() {453 assert_eq!(454 parse!(r#""Hello, \"world\"!""#),455 el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),456 );457 assert_eq!(458 parse!(r#"'Hello \'world\'!'"#),459 el!(Expr::Str("Hello 'world'!".into()), 0, 18),460 );461 assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));462 }463464 #[test]465 fn string_unescaping() {466 assert_eq!(467 parse!(r#""Hello\nWorld""#),468 el!(Expr::Str("Hello\nWorld".into()), 0, 14),469 );470 }471472 #[test]473 fn string_verbantim() {474 assert_eq!(475 parse!(r#"@"Hello\n""World""""#),476 el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),477 );478 }479480 #[test]481 fn imports() {482 assert_eq!(483 parse!("import \"hello\""),484 el!(Expr::Import(el!(Expr::Str("hello".into()), 7, 14)), 0, 14),485 );486 assert_eq!(487 parse!("importstr \"garnish.txt\""),488 el!(489 Expr::ImportStr(el!(Expr::Str("garnish.txt".into()), 10, 23)),490 0,491 23492 )493 );494 assert_eq!(495 parse!("importbin \"garnish.bin\""),496 el!(497 Expr::ImportBin(el!(Expr::Str("garnish.bin".into()), 10, 23)),498 0,499 23500 )501 );502 }503504 #[test]505 fn empty_object() {506 assert_eq!(507 parse!("{}"),508 el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)509 );510 }511512 #[test]513 fn basic_math() {514 assert_eq!(515 parse!("2+2*2"),516 el!(517 Expr::BinaryOp(518 el!(Expr::Num(2.0), 0, 1),519 Add,520 el!(521 Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),522 2,523 5524 )525 ),526 0,527 5528 )529 );530 }531532 #[test]533 fn basic_math_with_indents() {534 assert_eq!(535 parse!("2 + 2 * 2 "),536 el!(537 Expr::BinaryOp(538 el!(Expr::Num(2.0), 0, 1),539 Add,540 el!(541 Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),542 7,543 14544 ),545 ),546 0,547 14548 )549 );550 }551552 #[test]553 fn basic_math_parened() {554 assert_eq!(555 parse!("2+(2+2*2)"),556 el!(557 Expr::BinaryOp(558 el!(Expr::Num(2.0), 0, 1),559 Add,560 el!(561 Expr::Parened(el!(562 Expr::BinaryOp(563 el!(Expr::Num(2.0), 3, 4),564 Add,565 el!(566 Expr::BinaryOp(567 el!(Expr::Num(2.0), 5, 6),568 Mul,569 el!(Expr::Num(2.0), 7, 8),570 ),571 5,572 8573 ),574 ),575 3,576 8577 )),578 2,579 9580 ),581 ),582 0,583 9584 )585 );586 }587588 /// Comments should not affect parsing589 #[test]590 fn comments() {591 assert_eq!(592 parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),593 el!(594 Expr::BinaryOp(595 el!(Expr::Num(2.0), 0, 1),596 Add,597 el!(598 Expr::BinaryOp(599 el!(Expr::Num(3.0), 22, 23),600 Mul,601 el!(Expr::Num(4.0), 40, 41)602 ),603 22,604 41605 )606 ),607 0,608 41609 )610 );611 }612613 #[test]614 fn suffix() {615 // assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));616 // assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));617 // assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));618 // assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))619 }620621 #[test]622 fn array_comp() {623 use Expr::*;624 /*625 `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`,626 `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`627 */628 assert_eq!(629 parse!("[std.deepJoin(x) for x in arr]"),630 el!(631 ArrComp(632 el!(633 Apply(634 el!(635 Index {636 indexable: el!(Var("std".into()), 1, 4),637 parts: vec![IndexPart {638 value: el!(Str("deepJoin".into()), 5, 13),639 #[cfg(feature = "exp-null-coaelse")]640 null_coaelse: false,641 }],642 },643 1,644 13645 ),646 ArgsDesc::new(vec![el!(Var("x".into()), 14, 15)], vec![]),647 false,648 ),649 1,650 16651 ),652 vec![CompSpec::ForSpec(ForSpecData(653 Destruct::Full("x".into()),654 el!(Var("arr".into()), 26, 29)655 ))]656 ),657 0,658 30659 ),660 )661 }662663 #[test]664 fn reserved() {665 use Expr::*;666 assert_eq!(parse!("null"), el!(Literal(LiteralType::Null), 0, 4));667 assert_eq!(parse!("nulla"), el!(Var("nulla".into()), 0, 5));668 }669670 #[test]671 fn multiple_args_buf() {672 parse!("a(b, null_fields)");673 }674675 #[test]676 fn infix_precedence() {677 use Expr::*;678 assert_eq!(679 parse!("!a && !b"),680 el!(681 BinaryOp(682 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),683 And,684 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 7, 8)), 6, 8)685 ),686 0,687 8688 )689 );690 }691692 #[test]693 fn infix_precedence_division() {694 use Expr::*;695 assert_eq!(696 parse!("!a / !b"),697 el!(698 BinaryOp(699 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),700 Div,701 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 6, 7)), 5, 7)702 ),703 0,704 7705 )706 );707 }708709 #[test]710 fn double_negation() {711 use Expr::*;712 assert_eq!(713 parse!("!!a"),714 el!(715 UnaryOp(716 UnaryOpType::Not,717 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 2, 3)), 1, 3)718 ),719 0,720 3721 )722 )723 }724725 #[test]726 fn array_test_error() {727 parse!("[a for a in b if c for e in f]");728 // ^^^^ failed code729 }730731 #[test]732 fn missing_newline_between_comment_and_eof() {733 parse!(734 "{a:1}735736 //+213"737 );738 }739740 #[test]741 fn default_param_before_nondefault() {742 parse!("local x(foo = 'foo', bar) = null; null");743 }744745 #[test]746 fn add_location_info_to_all_sub_expressions() {747 use Expr::*;748749 let file_name = Source::new_virtual("<test>".into(), IStr::empty());750 let expr = parse(751 "{} { local x = 1, x: x } + {}",752 &ParserSettings { source: file_name },753 )754 .unwrap();755 assert_eq!(756 expr,757 el!(758 BinaryOp(759 el!(760 ObjExtend(761 el!(Obj(ObjBody::MemberList(vec![])), 0, 2),762 ObjBody::MemberList(vec![763 Member::BindStmt(BindSpec::Field {764 into: Destruct::Full("x".into()),765 value: el!(Num(1.0), 15, 16)766 }),767 Member::Field(FieldMember {768 name: FieldName::Fixed("x".into()),769 plus: false,770 params: None,771 visibility: Visibility::Normal,772 value: el!(Var("x".into()), 21, 22),773 })774 ])775 ),776 0,777 24778 ),779 BinaryOpType::Add,780 el!(Obj(ObjBody::MemberList(vec![])), 27, 29),781 ),782 0,783 29784 ),785 );786 }787}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, SourceDefaultIgnoreJpath, SourceDirectory, SourceFifo, SourceFile, SourcePath,16 SourcePathT, SourceVirtual,17};1819pub struct ParserSettings {20 pub source: Source,21}2223macro_rules! expr_bin {24 ($a:ident $op:ident $b:ident) => {25 Expr::BinaryOp($a, $op, $b)26 };27}28macro_rules! expr_un {29 ($op:ident $a:ident) => {30 Expr::UnaryOp($op, $a)31 };32}3334parser! {35 grammar jsonnet_parser() for str {36 use peg::ParseLiteral;3738 rule eof() = quiet!{![_]} / expected!("<eof>")39 rule eol() = "\n" / eof()4041 /// Standard C-like comments42 rule comment()43 = "//" (!eol()[_])* eol()44 / "/*" (!("*/")[_])* "*/"45 / "#" (!eol()[_])* eol()4647 rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")48 rule _() = quiet!{([' ' | '\r' | '\n' | '\t']+) / comment()}* / expected!("<whitespace>")4950 /// For comma-delimited elements51 rule comma() = quiet!{_ "," _} / expected!("<comma>")52 rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}53 rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}54 rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']55 /// Sequence of digits56 rule uint_str() -> &'input str = a:$(digit()+) { a }57 /// Number in scientific notation format58 rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")5960 /// Reserved word followed by any non-alphanumberic61 rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()62 rule id() -> IStr = v:$(quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")) { v.into() }6364 rule keyword(id: &'static str) -> ()65 = ##parse_string_literal(id) end_of_ident()6667 pub rule param(s: &ParserSettings) -> expr::Param = name:destruct(s) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr) }68 pub rule params(s: &ParserSettings) -> expr::ParamsDesc69 = params:param(s) ** comma() comma()? { expr::ParamsDesc(Rc::new(params)) }70 / { expr::ParamsDesc(Rc::new(Vec::new())) }7172 pub rule arg(s: &ParserSettings) -> (Option<IStr>, LocExpr)73 = name:(quiet! { (s:id() _ "=" !['='] _ {s})? } / expected!("<argument name>")) expr:expr(s) {(name, expr)}7475 pub rule args(s: &ParserSettings) -> expr::ArgsDesc76 = args:arg(s)**comma() comma()? {?77 let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();78 let mut unnamed = Vec::with_capacity(unnamed_count);79 let mut named = Vec::with_capacity(args.len() - unnamed_count);80 let mut named_started = false;81 for (name, value) in args {82 if let Some(name) = name {83 named_started = true;84 named.push((name, value));85 } else {86 if named_started {87 return Err("<named argument>")88 }89 unnamed.push(value);90 }91 }92 Ok(expr::ArgsDesc::new(unnamed, named))93 }9495 pub rule destruct_rest() -> expr::DestructRest96 = "..." into:(_ into:id() {into})? {if let Some(into) = into {97 expr::DestructRest::Keep(into)98 } else {expr::DestructRest::Drop}}99 pub rule destruct_array(s: &ParserSettings) -> expr::Destruct100 = "[" _ start:destruct(s)**comma() rest:(101 comma() _ rest:destruct_rest()? end:(102 comma() end:destruct(s)**comma() (_ comma())? {end}103 / comma()? {Vec::new()}104 ) {(rest, end)}105 / comma()? {(None, Vec::new())}106 ) _ "]" {?107 #[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Array {108 start,109 rest: rest.0,110 end: rest.1,111 });112 #[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")113 }114 pub rule destruct_object(s: &ParserSettings) -> expr::Destruct115 = "{" _116 fields:(name:id() into:(_ ":" _ into:destruct(s) {into})? default:(_ "=" _ v:expr(s) {v})? {(name, into, default)})**comma()117 rest:(118 comma() rest:destruct_rest()? {rest}119 / comma()? {None}120 )121 _ "}" {?122 #[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Object {123 fields,124 rest,125 });126 #[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")127 }128 pub rule destruct(s: &ParserSettings) -> expr::Destruct129 = v:id() {expr::Destruct::Full(v)}130 / "?" {?131 #[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Skip);132 #[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")133 }134 / arr:destruct_array(s) {arr}135 / obj:destruct_object(s) {obj}136137 pub rule bind(s: &ParserSettings) -> expr::BindSpec138 = into:destruct(s) _ "=" _ expr:expr(s) {expr::BindSpec::Field{into, value: expr}}139 / name:id() _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec::Function{name, params, value: expr}}140141 pub rule assertion(s: &ParserSettings) -> expr::AssertStmt142 = keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }143144 pub rule whole_line() -> &'input str145 = str:$((!['\n'][_])* "\n") {str}146 pub rule string_block() -> String147 = "|||" chomped:"-"? (!['\n']single_whitespace())* "\n"148 empty_lines:$(['\n']*)149 prefix:[' ' | '\t']+ first_line:whole_line()150 lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*151 [' ' | '\t']*<, {prefix.len() - 1}> "|||"152 {153 let mut l = empty_lines.to_owned();154 l.push_str(first_line);155 l.extend(lines);156 if chomped.is_some() {157 debug_assert!(l.ends_with('\n'));158 l.truncate(l.len() - 1);159 }160 l161 }162163 rule hex_char()164 = quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")165166 rule string_char(c: rule<()>)167 = (!['\\']!c()[_])+168 / "\\\\"169 / "\\u" hex_char() hex_char() hex_char() hex_char()170 / "\\x" hex_char() hex_char()171 / ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't' | '"' | '\''] } / expected!("<escape character>"))172 pub rule string() -> String173 = ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}174 / ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}175 / quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}176 / "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}177 / string_block() } / expected!("<string>")178179 pub rule field_name(s: &ParserSettings) -> expr::FieldName180 = name:id() {expr::FieldName::Fixed(name)}181 / name:string() {expr::FieldName::Fixed(name.into())}182 / "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}183 pub rule visibility() -> expr::Visibility184 = ":::" {expr::Visibility::Unhide}185 / "::" {expr::Visibility::Hidden}186 / ":" {expr::Visibility::Normal}187 pub rule field(s: &ParserSettings) -> expr::FieldMember188 = name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{189 name,190 plus: plus.is_some(),191 params: None,192 visibility,193 value,194 }}195 / name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{196 name,197 plus: false,198 params: Some(params),199 visibility,200 value,201 }}202 pub rule obj_local(s: &ParserSettings) -> BindSpec203 = keyword("local") _ bind:bind(s) {bind}204 pub rule member(s: &ParserSettings) -> expr::Member205 = bind:obj_local(s) {expr::Member::BindStmt(bind)}206 / assertion:assertion(s) {expr::Member::AssertStmt(assertion)}207 / field:field(s) {expr::Member::Field(field)}208 pub rule objinside(s: &ParserSettings) -> expr::ObjBody209 = 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})? {210 let mut compspecs = vec![CompSpec::ForSpec(forspec)];211 compspecs.extend(others.unwrap_or_default());212 expr::ObjBody::ObjComp(expr::ObjComp{213 pre_locals,214 field,215 post_locals,216 compspecs,217 })218 }219 / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}220 pub rule ifspec(s: &ParserSettings) -> IfSpecData221 = keyword("if") _ expr:expr(s) {IfSpecData(expr)}222 pub rule forspec(s: &ParserSettings) -> ForSpecData223 = keyword("for") _ id:destruct(s) _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}224 pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>225 = s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}226 pub rule local_expr(s: &ParserSettings) -> Expr227 = keyword("local") _ binds:bind(s) ** comma() (_ ",")? _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }228 pub rule string_expr(s: &ParserSettings) -> Expr229 = s:string() {Expr::Str(s.into())}230 pub rule obj_expr(s: &ParserSettings) -> Expr231 = "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}232 pub rule array_expr(s: &ParserSettings) -> Expr233 = "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}234 pub rule array_comp_expr(s: &ParserSettings) -> Expr235 = "[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {236 let mut specs = vec![CompSpec::ForSpec(forspec)];237 specs.extend(others.unwrap_or_default());238 Expr::ArrComp(expr, specs)239 }240 pub rule number_expr(s: &ParserSettings) -> Expr241 = n:number() {? if n.is_finite() {242 Ok(expr::Expr::Num(n))243 } else {244 Err("!!!numbers are finite")245 }}246 pub rule var_expr(s: &ParserSettings) -> Expr247 = n:id() { expr::Expr::Var(n) }248 pub rule id_loc(s: &ParserSettings) -> LocExpr249 = a:position!() n:id() b:position!() { LocExpr::new(expr::Expr::Str(n), Span(s.source.clone(), a as u32,b as u32)) }250 pub rule if_then_else_expr(s: &ParserSettings) -> Expr251 = cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{252 cond,253 cond_then,254 cond_else,255 }}256257 pub rule literal(s: &ParserSettings) -> Expr258 = v:(259 keyword("null") {LiteralType::Null}260 / keyword("true") {LiteralType::True}261 / keyword("false") {LiteralType::False}262 / keyword("self") {LiteralType::This}263 / keyword("$") {LiteralType::Dollar}264 / keyword("super") {LiteralType::Super}265 ) {Expr::Literal(v)}266267 pub rule expr_basic(s: &ParserSettings) -> Expr268 = literal(s)269270 / string_expr(s) / number_expr(s)271 / array_expr(s)272 / obj_expr(s)273 / array_expr(s)274 / array_comp_expr(s)275276 / keyword("importstr") _ path:expr(s) {Expr::ImportStr(path)}277 / keyword("importbin") _ path:expr(s) {Expr::ImportBin(path)}278 / keyword("import") _ path:expr(s) {Expr::Import(path)}279280 / var_expr(s)281 / local_expr(s)282 / if_then_else_expr(s)283284 / keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}285 / assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }286287 / keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }288289 rule slice_part(s: &ParserSettings) -> Option<LocExpr>290 = _ e:(e:expr(s) _{e})? {e}291 pub rule slice_desc(s: &ParserSettings) -> SliceDesc292 = start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {293 let (end, step) = if let Some((end, step)) = pair {294 (end, step)295 }else{296 (None, None)297 };298299 SliceDesc { start, end, step }300 }301302 rule binop(x: rule<()>) -> ()303 = quiet!{ x() } / expected!("<binary op>")304 rule unaryop(x: rule<()>) -> ()305 = quiet!{ x() } / expected!("<unary op>")306307 rule ensure_null_coaelse()308 = "" {?309 #[cfg(not(feature = "exp-null-coaelse"))] return Err("!!!experimental null coaelscing was not enabled");310 #[cfg(feature = "exp-null-coaelse")] Ok(())311 }312 use BinaryOpType::*;313 use UnaryOpType::*;314 rule expr(s: &ParserSettings) -> LocExpr315 = precedence! {316 start:position!() v:@ end:position!() { LocExpr::new(v, Span(s.source.clone(), start as u32, end as u32)) }317 --318 a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}319 a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {320 #[cfg(feature = "exp-null-coaelse")] return expr_bin!(a NullCoaelse b);321 unreachable!("ensure_null_coaelse will fail if feature is not enabled")322 }323 --324 a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}325 --326 a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}327 --328 a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}329 --330 a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}331 --332 a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}333 a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}334 --335 a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}336 a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}337 a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}338 a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}339 a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}340 --341 a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}342 a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}343 --344 a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}345 a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}346 --347 a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}348 a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}349 a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}350 --351 unaryop(<"+">) _ b:@ {expr_un!(Plus b)}352 unaryop(<"-">) _ b:@ {expr_un!(Minus b)}353 unaryop(<"!">) _ b:@ {expr_un!(Not b)}354 unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}355 --356 a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}357 indexable:(@) _ parts:index_part(s)+ {Expr::Index{indexable, parts}}358 a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}359 a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}360 --361 e:expr_basic(s) {e}362 "(" _ e:expr(s) _ ")" {Expr::Parened(e)}363 }364 pub rule index_part(s: &ParserSettings) -> IndexPart365 = n:("?" _ ensure_null_coaelse())? "." _ value:id_loc(s) {IndexPart {366 value,367 #[cfg(feature = "exp-null-coaelse")]368 null_coaelse: n.is_some(),369 }}370 / n:("?" _ "." _ ensure_null_coaelse())? "[" _ value:expr(s) _ "]" {IndexPart {371 value,372 #[cfg(feature = "exp-null-coaelse")]373 null_coaelse: n.is_some(),374 }}375376 pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}377 }378}379380pub type ParseError = peg::error::ParseError<peg::str::LineCol>;381pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {382 jsonnet_parser::jsonnet(str, settings)383}384/// Used for importstr values385pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {386 let len = str.len();387 LocExpr::new(Expr::Str(str), Span(settings.source.clone(), 0, len as u32))388}389390#[cfg(test)]391pub mod tests {392 use jrsonnet_interner::IStr;393 use BinaryOpType::*;394395 use super::{expr::*, parse};396 use crate::{source::Source, ParserSettings};397398 macro_rules! parse {399 ($s:expr) => {400 parse(401 $s,402 &ParserSettings {403 source: Source::new_virtual("<test>".into(), IStr::empty()),404 },405 )406 .unwrap()407 };408 }409410 macro_rules! el {411 ($expr:expr, $from:expr, $to:expr$(,)?) => {412 LocExpr::new(413 $expr,414 Span(415 Source::new_virtual("<test>".into(), IStr::empty()),416 $from,417 $to,418 ),419 )420 };421 }422423 #[test]424 fn multiline_string() {425 assert_eq!(426 parse!("|||\n Hello world!\n a\n|||"),427 el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),428 );429 assert_eq!(430 parse!("|||\n Hello world!\n a\n|||"),431 el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),432 );433 assert_eq!(434 parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),435 el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),436 );437 assert_eq!(438 parse!("|||\n Hello world!\n a\n |||"),439 el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),440 );441 }442443 #[test]444 fn slice() {445 parse!("a[1:]");446 parse!("a[1::]");447 parse!("a[:1:]");448 parse!("a[::1]");449 parse!("str[:len - 1]");450 }451452 #[test]453 fn string_escaping() {454 assert_eq!(455 parse!(r#""Hello, \"world\"!""#),456 el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),457 );458 assert_eq!(459 parse!(r#"'Hello \'world\'!'"#),460 el!(Expr::Str("Hello 'world'!".into()), 0, 18),461 );462 assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));463 }464465 #[test]466 fn string_unescaping() {467 assert_eq!(468 parse!(r#""Hello\nWorld""#),469 el!(Expr::Str("Hello\nWorld".into()), 0, 14),470 );471 }472473 #[test]474 fn string_verbantim() {475 assert_eq!(476 parse!(r#"@"Hello\n""World""""#),477 el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),478 );479 }480481 #[test]482 fn imports() {483 assert_eq!(484 parse!("import \"hello\""),485 el!(Expr::Import(el!(Expr::Str("hello".into()), 7, 14)), 0, 14),486 );487 assert_eq!(488 parse!("importstr \"garnish.txt\""),489 el!(490 Expr::ImportStr(el!(Expr::Str("garnish.txt".into()), 10, 23)),491 0,492 23493 )494 );495 assert_eq!(496 parse!("importbin \"garnish.bin\""),497 el!(498 Expr::ImportBin(el!(Expr::Str("garnish.bin".into()), 10, 23)),499 0,500 23501 )502 );503 }504505 #[test]506 fn empty_object() {507 assert_eq!(508 parse!("{}"),509 el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)510 );511 }512513 #[test]514 fn basic_math() {515 assert_eq!(516 parse!("2+2*2"),517 el!(518 Expr::BinaryOp(519 el!(Expr::Num(2.0), 0, 1),520 Add,521 el!(522 Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),523 2,524 5525 )526 ),527 0,528 5529 )530 );531 }532533 #[test]534 fn basic_math_with_indents() {535 assert_eq!(536 parse!("2 + 2 * 2 "),537 el!(538 Expr::BinaryOp(539 el!(Expr::Num(2.0), 0, 1),540 Add,541 el!(542 Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),543 7,544 14545 ),546 ),547 0,548 14549 )550 );551 }552553 #[test]554 fn basic_math_parened() {555 assert_eq!(556 parse!("2+(2+2*2)"),557 el!(558 Expr::BinaryOp(559 el!(Expr::Num(2.0), 0, 1),560 Add,561 el!(562 Expr::Parened(el!(563 Expr::BinaryOp(564 el!(Expr::Num(2.0), 3, 4),565 Add,566 el!(567 Expr::BinaryOp(568 el!(Expr::Num(2.0), 5, 6),569 Mul,570 el!(Expr::Num(2.0), 7, 8),571 ),572 5,573 8574 ),575 ),576 3,577 8578 )),579 2,580 9581 ),582 ),583 0,584 9585 )586 );587 }588589 /// Comments should not affect parsing590 #[test]591 fn comments() {592 assert_eq!(593 parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),594 el!(595 Expr::BinaryOp(596 el!(Expr::Num(2.0), 0, 1),597 Add,598 el!(599 Expr::BinaryOp(600 el!(Expr::Num(3.0), 22, 23),601 Mul,602 el!(Expr::Num(4.0), 40, 41)603 ),604 22,605 41606 )607 ),608 0,609 41610 )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}crates/jrsonnet-parser/src/source.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/source.rs
+++ b/crates/jrsonnet-parser/src/source.rs
@@ -134,6 +134,23 @@
any_ext_impl!(SourcePathT);
}
+#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]
+pub struct SourceDefaultIgnoreJpath;
+impl Display for SourceDefaultIgnoreJpath {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "<default (ignoring jpath)>")
+ }
+}
+impl SourcePathT for SourceDefaultIgnoreJpath {
+ fn is_default(&self) -> bool {
+ true
+ }
+ fn path(&self) -> Option<&Path> {
+ None
+ }
+ any_ext_impl!(SourcePathT);
+}
+
/// Represents path to the file on the disk
/// Directories shouldn't be put here, as resolution for files differs from resolution for directories:
///