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