difftreelog
feat sync jsonnet stdlib changes
in: master
8 files changed
crates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -413,18 +413,19 @@
val.value_type()
)
};
- if !arr.is_empty() {
- for (i, v) in arr.iter().enumerate() {
- let v = v.with_description(|| format!("elem <{i}> evaluation"))?;
- out.push_str("---\n");
- in_description_frame(
- || format!("elem <{i}> manifestification"),
- || self.inner.manifest_buf(v, out),
- )?;
+ for (i, v) in arr.iter().enumerate() {
+ if i != 0 {
out.push('\n');
}
+ let v = v.with_description(|| format!("elem <{i}> evaluation"))?;
+ out.push_str("---\n");
+ in_description_frame(
+ || format!("elem <{i}> manifestification"),
+ || self.inner.manifest_buf(v, out),
+ )?;
}
if self.c_document_end {
+ out.push('\n');
out.push_str("...");
}
if self.end_newline {
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() { expr::Expr::Num(n) }241 pub rule var_expr(s: &ParserSettings) -> Expr242 = n:id() { expr::Expr::Var(n) }243 pub rule id_loc(s: &ParserSettings) -> LocExpr244 = a:position!() n:id() b:position!() { LocExpr::new(expr::Expr::Str(n), Span(s.source.clone(), a as u32,b as u32)) }245 pub rule if_then_else_expr(s: &ParserSettings) -> Expr246 = cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{247 cond,248 cond_then,249 cond_else,250 }}251252 pub rule literal(s: &ParserSettings) -> Expr253 = v:(254 keyword("null") {LiteralType::Null}255 / keyword("true") {LiteralType::True}256 / keyword("false") {LiteralType::False}257 / keyword("self") {LiteralType::This}258 / keyword("$") {LiteralType::Dollar}259 / keyword("super") {LiteralType::Super}260 ) {Expr::Literal(v)}261262 pub rule expr_basic(s: &ParserSettings) -> Expr263 = literal(s)264265 / string_expr(s) / number_expr(s)266 / array_expr(s)267 / obj_expr(s)268 / array_expr(s)269 / array_comp_expr(s)270271 / keyword("importstr") _ path:expr(s) {Expr::ImportStr(path)}272 / keyword("importbin") _ path:expr(s) {Expr::ImportBin(path)}273 / keyword("import") _ path:expr(s) {Expr::Import(path)}274275 / var_expr(s)276 / local_expr(s)277 / if_then_else_expr(s)278279 / keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}280 / assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }281282 / keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }283284 rule slice_part(s: &ParserSettings) -> Option<LocExpr>285 = _ e:(e:expr(s) _{e})? {e}286 pub rule slice_desc(s: &ParserSettings) -> SliceDesc287 = start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {288 let (end, step) = if let Some((end, step)) = pair {289 (end, step)290 }else{291 (None, None)292 };293294 SliceDesc { start, end, step }295 }296297 rule binop(x: rule<()>) -> ()298 = quiet!{ x() } / expected!("<binary op>")299 rule unaryop(x: rule<()>) -> ()300 = quiet!{ x() } / expected!("<unary op>")301302 rule ensure_null_coaelse()303 = "" {?304 #[cfg(not(feature = "exp-null-coaelse"))] return Err("!!!experimental null coaelscing was not enabled");305 #[cfg(feature = "exp-null-coaelse")] Ok(())306 }307 use BinaryOpType::*;308 use UnaryOpType::*;309 rule expr(s: &ParserSettings) -> LocExpr310 = precedence! {311 start:position!() v:@ end:position!() { LocExpr::new(v, Span(s.source.clone(), start as u32, end as u32)) }312 --313 a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}314 a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {315 #[cfg(feature = "exp-null-coaelse")] return expr_bin!(a NullCoaelse b);316 unreachable!("ensure_null_coaelse will fail if feature is not enabled")317 }318 --319 a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}320 --321 a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}322 --323 a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}324 --325 a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}326 --327 a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}328 a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}329 --330 a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}331 a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}332 a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}333 a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}334 a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}335 --336 a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}337 a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}338 --339 a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}340 a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}341 --342 a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}343 a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}344 a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}345 --346 unaryop(<"+">) _ b:@ {expr_un!(Plus b)}347 unaryop(<"-">) _ b:@ {expr_un!(Minus b)}348 unaryop(<"!">) _ b:@ {expr_un!(Not b)}349 unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}350 --351 a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}352 indexable:(@) _ parts:index_part(s)+ {Expr::Index{indexable, parts}}353 a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}354 a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}355 --356 e:expr_basic(s) {e}357 "(" _ e:expr(s) _ ")" {Expr::Parened(e)}358 }359 pub rule index_part(s: &ParserSettings) -> IndexPart360 = n:("?" _ ensure_null_coaelse())? "." _ value:id_loc(s) {IndexPart {361 value,362 #[cfg(feature = "exp-null-coaelse")]363 null_coaelse: n.is_some(),364 }}365 / n:("?" _ "." _ ensure_null_coaelse())? "[" _ value:expr(s) _ "]" {IndexPart {366 value,367 #[cfg(feature = "exp-null-coaelse")]368 null_coaelse: n.is_some(),369 }}370371 pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}372 }373}374375pub type ParseError = peg::error::ParseError<peg::str::LineCol>;376pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {377 jsonnet_parser::jsonnet(str, settings)378}379/// Used for importstr values380pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {381 let len = str.len();382 LocExpr::new(Expr::Str(str), Span(settings.source.clone(), 0, len as u32))383}384385#[cfg(test)]386pub mod tests {387 use jrsonnet_interner::IStr;388 use BinaryOpType::*;389390 use super::{expr::*, parse};391 use crate::{source::Source, ParserSettings};392393 macro_rules! parse {394 ($s:expr) => {395 parse(396 $s,397 &ParserSettings {398 source: Source::new_virtual("<test>".into(), IStr::empty()),399 },400 )401 .unwrap()402 };403 }404405 macro_rules! el {406 ($expr:expr, $from:expr, $to:expr$(,)?) => {407 LocExpr::new(408 $expr,409 Span(410 Source::new_virtual("<test>".into(), IStr::empty()),411 $from,412 $to,413 ),414 )415 };416 }417418 #[test]419 fn multiline_string() {420 assert_eq!(421 parse!("|||\n Hello world!\n a\n|||"),422 el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),423 );424 assert_eq!(425 parse!("|||\n Hello world!\n a\n|||"),426 el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),427 );428 assert_eq!(429 parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),430 el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),431 );432 assert_eq!(433 parse!("|||\n Hello world!\n a\n |||"),434 el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),435 );436 }437438 #[test]439 fn slice() {440 parse!("a[1:]");441 parse!("a[1::]");442 parse!("a[:1:]");443 parse!("a[::1]");444 parse!("str[:len - 1]");445 }446447 #[test]448 fn string_escaping() {449 assert_eq!(450 parse!(r#""Hello, \"world\"!""#),451 el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),452 );453 assert_eq!(454 parse!(r#"'Hello \'world\'!'"#),455 el!(Expr::Str("Hello 'world'!".into()), 0, 18),456 );457 assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));458 }459460 #[test]461 fn string_unescaping() {462 assert_eq!(463 parse!(r#""Hello\nWorld""#),464 el!(Expr::Str("Hello\nWorld".into()), 0, 14),465 );466 }467468 #[test]469 fn string_verbantim() {470 assert_eq!(471 parse!(r#"@"Hello\n""World""""#),472 el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),473 );474 }475476 #[test]477 fn imports() {478 assert_eq!(479 parse!("import \"hello\""),480 el!(Expr::Import(el!(Expr::Str("hello".into()), 7, 14)), 0, 14),481 );482 assert_eq!(483 parse!("importstr \"garnish.txt\""),484 el!(485 Expr::ImportStr(el!(Expr::Str("garnish.txt".into()), 10, 23)),486 0,487 23488 )489 );490 assert_eq!(491 parse!("importbin \"garnish.bin\""),492 el!(493 Expr::ImportBin(el!(Expr::Str("garnish.bin".into()), 10, 23)),494 0,495 23496 )497 );498 }499500 #[test]501 fn empty_object() {502 assert_eq!(503 parse!("{}"),504 el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)505 );506 }507508 #[test]509 fn basic_math() {510 assert_eq!(511 parse!("2+2*2"),512 el!(513 Expr::BinaryOp(514 el!(Expr::Num(2.0), 0, 1),515 Add,516 el!(517 Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),518 2,519 5520 )521 ),522 0,523 5524 )525 );526 }527528 #[test]529 fn basic_math_with_indents() {530 assert_eq!(531 parse!("2 + 2 * 2 "),532 el!(533 Expr::BinaryOp(534 el!(Expr::Num(2.0), 0, 1),535 Add,536 el!(537 Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),538 7,539 14540 ),541 ),542 0,543 14544 )545 );546 }547548 #[test]549 fn basic_math_parened() {550 assert_eq!(551 parse!("2+(2+2*2)"),552 el!(553 Expr::BinaryOp(554 el!(Expr::Num(2.0), 0, 1),555 Add,556 el!(557 Expr::Parened(el!(558 Expr::BinaryOp(559 el!(Expr::Num(2.0), 3, 4),560 Add,561 el!(562 Expr::BinaryOp(563 el!(Expr::Num(2.0), 5, 6),564 Mul,565 el!(Expr::Num(2.0), 7, 8),566 ),567 5,568 8569 ),570 ),571 3,572 8573 )),574 2,575 9576 ),577 ),578 0,579 9580 )581 );582 }583584 /// Comments should not affect parsing585 #[test]586 fn comments() {587 assert_eq!(588 parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),589 el!(590 Expr::BinaryOp(591 el!(Expr::Num(2.0), 0, 1),592 Add,593 el!(594 Expr::BinaryOp(595 el!(Expr::Num(3.0), 22, 23),596 Mul,597 el!(Expr::Num(4.0), 40, 41)598 ),599 22,600 41601 )602 ),603 0,604 41605 )606 );607 }608609 #[test]610 fn suffix() {611 // assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));612 // assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));613 // assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));614 // assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))615 }616617 #[test]618 fn array_comp() {619 use Expr::*;620 /*621 `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`,622 `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`623 */624 assert_eq!(625 parse!("[std.deepJoin(x) for x in arr]"),626 el!(627 ArrComp(628 el!(629 Apply(630 el!(631 Index {632 indexable: el!(Var("std".into()), 1, 4),633 parts: vec![IndexPart {634 value: el!(Str("deepJoin".into()), 5, 13),635 #[cfg(feature = "exp-null-coaelse")]636 null_coaelse: false,637 }],638 },639 1,640 13641 ),642 ArgsDesc::new(vec![el!(Var("x".into()), 14, 15)], vec![]),643 false,644 ),645 1,646 16647 ),648 vec![CompSpec::ForSpec(ForSpecData(649 Destruct::Full("x".into()),650 el!(Var("arr".into()), 26, 29)651 ))]652 ),653 0,654 30655 ),656 )657 }658659 #[test]660 fn reserved() {661 use Expr::*;662 assert_eq!(parse!("null"), el!(Literal(LiteralType::Null), 0, 4));663 assert_eq!(parse!("nulla"), el!(Var("nulla".into()), 0, 5));664 }665666 #[test]667 fn multiple_args_buf() {668 parse!("a(b, null_fields)");669 }670671 #[test]672 fn infix_precedence() {673 use Expr::*;674 assert_eq!(675 parse!("!a && !b"),676 el!(677 BinaryOp(678 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),679 And,680 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 7, 8)), 6, 8)681 ),682 0,683 8684 )685 );686 }687688 #[test]689 fn infix_precedence_division() {690 use Expr::*;691 assert_eq!(692 parse!("!a / !b"),693 el!(694 BinaryOp(695 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),696 Div,697 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 6, 7)), 5, 7)698 ),699 0,700 7701 )702 );703 }704705 #[test]706 fn double_negation() {707 use Expr::*;708 assert_eq!(709 parse!("!!a"),710 el!(711 UnaryOp(712 UnaryOpType::Not,713 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 2, 3)), 1, 3)714 ),715 0,716 3717 )718 )719 }720721 #[test]722 fn array_test_error() {723 parse!("[a for a in b if c for e in f]");724 // ^^^^ failed code725 }726727 #[test]728 fn missing_newline_between_comment_and_eof() {729 parse!(730 "{a:1}731732 //+213"733 );734 }735736 #[test]737 fn default_param_before_nondefault() {738 parse!("local x(foo = 'foo', bar) = null; null");739 }740741 #[test]742 fn add_location_info_to_all_sub_expressions() {743 use Expr::*;744745 let file_name = Source::new_virtual("<test>".into(), IStr::empty());746 let expr = parse(747 "{} { local x = 1, x: x } + {}",748 &ParserSettings { source: file_name },749 )750 .unwrap();751 assert_eq!(752 expr,753 el!(754 BinaryOp(755 el!(756 ObjExtend(757 el!(Obj(ObjBody::MemberList(vec![])), 0, 2),758 ObjBody::MemberList(vec![759 Member::BindStmt(BindSpec::Field {760 into: Destruct::Full("x".into()),761 value: el!(Num(1.0), 15, 16)762 }),763 Member::Field(FieldMember {764 name: FieldName::Fixed("x".into()),765 plus: false,766 params: None,767 visibility: Visibility::Normal,768 value: el!(Var("x".into()), 21, 22),769 })770 ])771 ),772 0,773 24774 ),775 BinaryOpType::Add,776 el!(Obj(ObjBody::MemberList(vec![])), 27, 29),777 ),778 0,779 29780 ),781 );782 }783}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 = "|||" 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}crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -3,6 +3,7 @@
use std::{
cell::{Ref, RefCell, RefMut},
collections::HashMap,
+ f64,
rc::Rc,
};
@@ -14,6 +15,7 @@
error::{ErrorKind::*, Result},
function::{CallLocation, FuncVal, TlaArg},
trace::PathResolver,
+ val::NumValue,
ContextBuilder, IStr, ObjValue, ObjValueBuilder, Thunk, Val,
};
use jrsonnet_gcmodule::{Acyclic, Cc, Trace};
@@ -63,6 +65,7 @@
("isObject", builtin_is_object::INST),
("isArray", builtin_is_array::INST),
("isFunction", builtin_is_function::INST),
+ ("isNull", builtin_is_null::INST),
// Arrays
("makeArray", builtin_make_array::INST),
("repeat", builtin_repeat::INST),
@@ -104,6 +107,8 @@
("floor", builtin_floor::INST),
("ceil", builtin_ceil::INST),
("log", builtin_log::INST),
+ ("log2", builtin_log2::INST),
+ ("log10", builtin_log10::INST),
("pow", builtin_pow::INST),
("sqrt", builtin_sqrt::INST),
("sin", builtin_sin::INST),
@@ -121,6 +126,9 @@
("isOdd", builtin_is_odd::INST),
("isInteger", builtin_is_integer::INST),
("isDecimal", builtin_is_decimal::INST),
+ ("deg2rad", builtin_deg2rad::INST),
+ ("rad2deg", builtin_rad2deg::INST),
+ ("hypot", builtin_hypot::INST),
// Operator
("mod", builtin_mod::INST),
("primitiveEquals", builtin_primitive_equals::INST),
@@ -201,6 +209,7 @@
("lstripChars", builtin_lstrip_chars::INST),
("rstripChars", builtin_rstrip_chars::INST),
("stripChars", builtin_strip_chars::INST),
+ ("trim", builtin_trim::INST),
// Misc
("length", builtin_length::INST),
("get", builtin_get::INST),
@@ -248,6 +257,10 @@
builder.method("trace", builtin_trace { settings });
builder.method("id", FuncVal::Id);
+ builder.field("pi").hide().value(Val::Num(
+ NumValue::new(f64::consts::PI).expect("pi is finite"),
+ ));
+
#[cfg(feature = "exp-regex")]
{
// Regex
crates/jrsonnet-stdlib/src/math.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/math.rs
+++ b/crates/jrsonnet-stdlib/src/math.rs
@@ -1,3 +1,5 @@
+use std::f64;
+
use jrsonnet_evaluator::{function::builtin, typed::PositiveF64};
#[builtin]
@@ -56,6 +58,16 @@
}
#[builtin]
+pub fn builtin_log2(x: f64) -> f64 {
+ x.log2()
+}
+
+#[builtin]
+pub fn builtin_log10(x: f64) -> f64 {
+ x.log10()
+}
+
+#[builtin]
pub fn builtin_pow(x: f64, n: f64) -> f64 {
x.powf(n)
}
@@ -153,3 +165,18 @@
pub fn builtin_is_decimal(x: f64) -> bool {
builtin_round(x) != x
}
+
+#[builtin]
+pub fn builtin_deg2rad(x: f64) -> f64 {
+ x * f64::consts::PI / 180.0
+}
+
+#[builtin]
+pub fn builtin_rad2deg(x: f64) -> f64 {
+ x * 180.0 / f64::consts::PI
+}
+
+#[builtin]
+pub fn builtin_hypot(x: f64, y: f64) -> f64 {
+ x.hypot(y)
+}
crates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/misc.rs
+++ b/crates/jrsonnet-stdlib/src/misc.rs
@@ -156,8 +156,16 @@
#[cfg(feature = "exp-preserve-order")]
true,
);
- let a = a.manifest(&format).description("<a> manifestification")?;
- let b = b.manifest(&format).description("<b> manifestification")?;
+ let a = if let Some(a) = a.as_str() {
+ format!("<A>\n{a}\n</A>")
+ } else {
+ a.manifest(&format).description("<a> manifestification")?
+ };
+ let b = if let Some(b) = b.as_str() {
+ format!("<B>\n{b}\n</B>")
+ } else {
+ b.manifest(&format).description("<b> manifestification")?
+ };
bail!("assertion failed: A != B\nA: {a}\nB: {b}")
}
@@ -166,9 +174,7 @@
let Some(patch) = patch.as_obj() else {
return Ok(patch);
};
- let Some(target) = target.as_obj() else {
- return Ok(Val::Obj(patch));
- };
+ let target = target.as_obj().unwrap_or_else(|| ObjValue::new_empty());
let target_fields = target
.fields(
// FIXME: Makes no sense to preserve order for BTreeSet, it would be better to use IndexSet here?
@@ -203,10 +209,7 @@
if matches!(field_patch, Val::Null) {
continue;
}
- let Some(field_target) = target.get(field.clone())? else {
- out.field(field.clone()).value(field_patch);
- continue;
- };
+ let field_target = target.get(field.clone())?.unwrap_or(Val::Null);
out.field(field.clone())
.value(builtin_merge_patch(field_target, field_patch)?);
}
crates/jrsonnet-stdlib/src/objects.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/objects.rs
+++ b/crates/jrsonnet-stdlib/src/objects.rs
@@ -1,7 +1,8 @@
use jrsonnet_evaluator::{
function::builtin,
+ rustc_hash::FxHashSet,
val::{ArrValue, Val},
- IStr, ObjValue, ObjValueBuilder,
+ IStr, MaybeUnbound, ObjValue, ObjValueBuilder, Thunk,
};
#[builtin]
@@ -166,14 +167,31 @@
preserve_order: bool,
) -> ObjValue {
let mut new_obj = ObjValueBuilder::with_capacity(obj.len() - 1);
- for (k, v) in obj.iter(
+ let all_fields = obj.fields_ex(
+ true,
#[cfg(feature = "exp-preserve-order")]
preserve_order,
- ) {
- if k == key {
+ );
+ let visible_fields = obj
+ .fields_ex(
+ false,
+ #[cfg(feature = "exp-preserve-order")]
+ preserve_order,
+ )
+ .into_iter()
+ .collect::<FxHashSet<_>>();
+
+ for field in &all_fields {
+ if *field == key {
continue;
}
- new_obj.field(k).value(v.unwrap());
+ let mut b = new_obj.field(field.clone());
+ if !visible_fields.contains(&field) {
+ b = b.hide();
+ }
+ let _ = b.binding(MaybeUnbound::Bound(Thunk::result(
+ obj.get(field.clone()).transpose().expect("field exists"),
+ )));
}
new_obj.build()
crates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/strings.rs
+++ b/crates/jrsonnet-stdlib/src/strings.rs
@@ -254,6 +254,19 @@
Ok(str.as_str().trim_matches(pattern).into())
}
+#[builtin]
+pub fn builtin_trim(str: IStr) -> String {
+ let filter =
+ |v: char| {
+ v == ' '
+ || v == '\t' || v == '\n'
+ || v == '\u{000c}'
+ || v == '\r' || v == '\u{0085}'
+ || v == '\u{00a0}'
+ };
+ str.as_str().trim_matches(filter).to_string()
+}
+
fn new_trim_pattern(chars: IndexableVal) -> Result<impl Fn(char) -> bool> {
let chars: BTreeSet<char> = match chars {
IndexableVal::Str(chars) => chars.chars().collect(),
crates/jrsonnet-stdlib/src/types.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/types.rs
+++ b/crates/jrsonnet-stdlib/src/types.rs
@@ -29,3 +29,7 @@
pub fn builtin_is_function(v: Val) -> bool {
matches!(v, Val::Func(_))
}
+#[builtin]
+pub fn builtin_is_null(v: Val) -> bool {
+ matches!(v, Val::Null)
+}