1use jrsonnet_gcmodule::Acyclic;2use jrsonnet_ir::{3 ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BindSpec, CompSpec, Destruct, DestructRest, Expr,4 ExprParam, ExprParams, FieldMember, FieldName, ForSpecData, IStr, IfElse, IfSpecData,5 ImportKind, IndexPart, LiteralType, Member, NumValue, ObjBody, ObjComp, ObjMembers, Slice,6 SliceDesc, Source, Span, Spanned, Visibility, unescape,7};8use peg::parser;910pub struct ParserSettings {11 pub source: Source,12}1314macro_rules! expr_bin {15 ($a:ident $op:ident $b:ident) => {16 Expr::BinaryOp(Box::new(BinaryOp {17 lhs: $a,18 op: $op,19 rhs: $b,20 }))21 };22}23macro_rules! expr_un {24 ($op:ident $a:ident) => {25 Expr::UnaryOp($op, Box::new($a))26 };27}2829parser! {30 pub grammar jsonnet_parser() for str {31 use peg::ParseLiteral;3233 rule eof() = quiet!{![_]} / expected!("<eof>")34 rule eol() = "\n" / eof()3536 37 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 46 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 51 rule uint_str() -> &'input str = a:$(digit()+ ("_" digit()+)*) { a }52 53 rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.replace('_',"").parse().map_err(|_| "<number>") }} / expected!("<number>")5455 56 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 = #{|input, pos| input.parse_string_literal(pos, id)} end_of_ident()6162 pub rule param(s: &ParserSettings) -> ExprParam = destruct:destruct(s) default:(_ "=" _ default:expr(s){default})? { ExprParam { destruct, default } }63 pub rule params(s: &ParserSettings) -> ExprParams64 = params:param(s) ** comma() comma()? { ExprParams::new(params) }65 / { ExprParams::new(Vec::new()) }6667 pub rule arg(s: &ParserSettings) -> (Option<IStr>, Expr)68 = name:(quiet! { (s:id() _ "=" !['='] _ {s})? } / expected!("<argument name>")) expr:expr(s) {(name, expr)}6970 pub rule args(s: &ParserSettings) -> ArgsDesc71 = args:arg(s)**comma() comma()? {?72 let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();73 let mut unnamed = Vec::with_capacity(unnamed_count);74 let mut names = Vec::with_capacity(args.len() - unnamed_count);75 let mut values = 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 names.push(name);81 values.push(value);82 } else {83 if named_started {84 return Err("<named argument>")85 }86 unnamed.push(value);87 }88 }89 Ok(ArgsDesc{unnamed, names, values})90 }9192 pub rule destruct_rest() -> DestructRest93 = "..." into:(_ into:id() {into})? {into.map_or_else(|| DestructRest::Drop, DestructRest::Keep)}94 pub rule destruct_array(s: &ParserSettings) -> Destruct95 = "[" _ start:destruct(s)**comma() rest:(96 comma() _ rest:destruct_rest()? end:(97 comma() end:destruct(s)**comma() (_ comma())? {end}98 / comma()? {Vec::new()}99 ) {(rest, end)}100 / comma()? {(None, Vec::new())}101 ) _ "]" {?102 #[cfg(feature = "exp-destruct")] return Ok(Destruct::Array {103 start,104 rest: rest.0,105 end: rest.1,106 });107 #[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")108 }109 pub rule destruct_object(s: &ParserSettings) -> Destruct110 = "{" _111 fields:(name:id() into:(_ ":" _ into:destruct(s) {into})? default:(_ "=" _ v:spanned(<expr(s)>, s) {v})? {(name, into, default)})**comma()112 rest:(113 comma() rest:destruct_rest()? {rest}114 / comma()? {None}115 )116 _ "}" {?117 #[cfg(feature = "exp-destruct")] return Ok(Destruct::Object {118 fields,119 rest,120 });121 #[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")122 }123 pub rule destruct(s: &ParserSettings) -> Destruct124 = v:spanned(<id()>, s) {Destruct::Full(v)}125 / "?" {?126 #[cfg(feature = "exp-destruct")] return Ok(Destruct::Skip);127 #[cfg(not(feature = "exp-destruct"))] Err("!!!experimental destructuring was not enabled")128 }129 / arr:destruct_array(s) {arr}130 / obj:destruct_object(s) {obj}131132 pub rule bind(s: &ParserSettings) -> BindSpec133 = into:destruct(s) _ "=" _ value:expr(s) {BindSpec::Field{into, value}}134 / name:id() _ "(" _ params:params(s) _ ")" _ "=" _ value:expr(s) {BindSpec::Function{name, params, value}}135136 pub rule assertion(s: &ParserSettings) -> AssertStmt137 = keyword("assert") _ assertion:spanned(<expr(s)>, s) message:(_ ":" _ e:expr(s) {e})? { AssertStmt{assertion, message} }138139 pub rule whole_line() -> &'input str140 = str:$((!['\n'][_])* "\n") {str}141 pub rule string_block() -> String142 = "|||" chomped:"-"? (!['\n']single_whitespace())* "\n"143 empty_lines:$(['\n']*)144 prefix:[' ' | '\t']+ first_line:whole_line()145 lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*146 [' ' | '\t']*<, {prefix.len() - 1}> "|||"147 {148 let mut l = empty_lines.to_owned();149 l.push_str(first_line);150 l.extend(lines);151 if chomped.is_some() {152 debug_assert!(l.ends_with('\n'));153 l.truncate(l.len() - 1);154 }155 l156 }157158 rule hex_char()159 = quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")160161 rule string_char(c: rule<()>)162 = (!['\\']!c()[_])+163 / "\\\\"164 / "\\u" hex_char() hex_char() hex_char() hex_char()165 / "\\x" hex_char() hex_char()166 / ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't' | '"' | '\''] } / expected!("<escape character>"))167 pub rule string() -> String168 = ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}169 / ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}170 / quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}171 / "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}172 / string_block() } / expected!("<string>")173174 pub rule field_name(s: &ParserSettings) -> FieldName175 = name:id() {FieldName::Fixed(name)}176 / name:string() {FieldName::Fixed(name.into())}177 / "[" _ expr:expr(s) _ "]" {FieldName::Dyn(expr)}178 pub rule visibility() -> Visibility179 = ":::" {Visibility::Unhide}180 / "::" {Visibility::Hidden}181 / ":" {Visibility::Normal}182 pub rule field(s: &ParserSettings) -> FieldMember183 = name:spanned(<field_name(s)>, s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {FieldMember{184 name,185 plus: plus.is_some(),186 params: None,187 visibility,188 value,189 }}190 / name:spanned(<field_name(s)>, s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {FieldMember{191 name,192 plus: false,193 params: Some(params),194 visibility,195 value,196 }}197 pub rule obj_local(s: &ParserSettings) -> BindSpec198 = keyword("local") _ bind:bind(s) {bind}199 pub rule member(s: &ParserSettings) -> Member200 = bind:obj_local(s) {Member::BindStmt(bind)}201 / assertion:assertion(s) {Member::AssertStmt(assertion)}202 / field:field(s) {Member::Field(field)}203 pub rule objinside(s: &ParserSettings) -> ObjBody204 = members:(member(s) ** comma()) comma()? _ compspecs:compspecs(s)? {?205 Ok(if let Some(compspecs) = compspecs {206 let mut locals = Vec::new();207 let mut field = None;208 for member in members {209 match member {210 Member::Field(field_member) => if field.replace(field_member).is_some() {211 return Err("<object comprehension can only contain one field>")212 },213 Member::BindStmt(bind_spec) => locals.push(bind_spec),214 Member::AssertStmt(assert_stmt) => return Err("<asserts are unsupported in object comprehension>"),215 }216 }217 ObjBody::ObjComp(ObjComp {218 locals,219 field: Box::new(field.ok_or("<missing object comprehension field>")?),220 compspecs221 })222 } else {223 let mut locals = Vec::new();224 let mut asserts = Vec::new();225 let mut fields = Vec::new();226 for member in members {227 match member {228 Member::Field(field_member) => fields.push(field_member),229 Member::BindStmt(bind_spec) => locals.push(bind_spec),230 Member::AssertStmt(assert_stmt) => asserts.push(assert_stmt),231 }232 }233 ObjBody::MemberList(ObjMembers {234 locals,235 asserts,236 fields237 })238 })239 }240 pub rule ifspec(s: &ParserSettings) -> IfSpecData241 = i:spanned(<keyword("if")>, s) _ cond:expr(s) {IfSpecData { span: i.span, cond }}242 pub rule forspec(s: &ParserSettings) -> ForSpecData243 = keyword("for") _ destruct:destruct(s) _ keyword("in") _ over:expr(s) { ForSpecData { destruct, over } }244 rule ensure_object_iteration()245 = "" {?246 #[cfg(not(feature = "exp-object-iteration"))] return Err("!!!experimental object iteration was not enabled");247 #[cfg(feature = "exp-object-iteration")] Ok(())248 }249 pub rule forobjspec(s: &ParserSettings) -> CompSpec250 = ensure_object_iteration() keyword("for") _ "[" _ key:id() _ "]" _ vis:visibility() _ value:destruct(s) _ keyword("in") _ over:expr(s) {251 #[cfg(feature = "exp-object-iteration")] return CompSpec::ForObjSpec(jrsonnet_ir::ForObjSpecData { key, visibility: vis, value, over });252 #[cfg(not(feature = "exp-object-iteration"))] unreachable!("ensure_object_iteration will fail if feature is not enabled")253 }254 rule compspec(s: &ParserSettings) -> CompSpec255 = i:ifspec(s) { CompSpec::IfSpec(i) } / f:forobjspec(s) { f } / f:forspec(s) {CompSpec::ForSpec(f)}256 pub rule compspecs(s: &ParserSettings) -> Vec<CompSpec>257 = specs:compspec(s) ++ _ {?258 if matches!(specs[0], CompSpec::IfSpec(_)) {259 return Err("<first compspec should be for>")260 }261 Ok(specs)262 }263 pub rule local_expr(s: &ParserSettings) -> Expr264 = keyword("local") _ binds:bind(s) ** comma() (_ ",")? _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, Box::new(expr)) }265 pub rule string_expr(s: &ParserSettings) -> Expr266 = s:string() {Expr::Str(s.into())}267 pub rule obj_expr(s: &ParserSettings) -> Expr268 = "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}269 pub rule array_expr(s: &ParserSettings) -> Expr270 = "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}271 pub rule array_comp_expr(s: &ParserSettings) -> Expr272 = "[" _ expr:expr(s) _ comma()? _ specs:(r: compspecs(s) _ {r}) "]" {273 Expr::ArrComp(Box::new(expr), specs)274 }275 pub rule number_expr(s: &ParserSettings) -> Expr276 = n:number() {? if let Some(n) = NumValue::new(n) {277 Ok(Expr::Num(n))278 } else {279 Err("!!!numbers are finite")280 }}281282 rule spanned<T: Acyclic>(x: rule<T>, s: &ParserSettings) -> Spanned<T>283 = a:position!() n:x() b:position!() { Spanned::new(n, Span(s.source.clone(), a as u32, b as u32)) }284285 pub rule var_expr(s: &ParserSettings) -> Expr286 = n:spanned(<id()>, s) { Expr::Var(n) }287 pub rule id_loc(s: &ParserSettings) -> Spanned<Expr>288 = a:position!() n:id() b:position!() { Spanned::new(Expr::Str(n), Span(s.source.clone(), a as u32,b as u32)) }289 pub rule if_then_else_expr(s: &ParserSettings) -> Expr290 = cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse(Box::new(IfElse{291 cond,292 cond_then,293 cond_else,294 }))}295296 pub rule literal(s: &ParserSettings) -> Expr297 = v:(298 keyword("null") {LiteralType::Null}299 / keyword("true") {LiteralType::True}300 / keyword("false") {LiteralType::False}301 / keyword("self") {LiteralType::This}302 / keyword("$") {LiteralType::Dollar}303 / keyword("super") {LiteralType::Super}304 ) {Expr::Literal(v)}305306 rule import_kind() -> ImportKind307 = keyword("importstr") { ImportKind::Str }308 / keyword("importbin") { ImportKind::Bin }309 / keyword("import") { ImportKind::Normal }310311 pub rule expr_basic(s: &ParserSettings) -> Expr312 = literal(s)313314 / string_expr(s) / number_expr(s)315 / array_expr(s)316 / obj_expr(s)317 / array_expr(s)318 / array_comp_expr(s)319320 / kind:spanned(<import_kind()>, s) _ path:expr(s) {Expr::Import(kind, Box::new(path))}321322 / var_expr(s)323 / local_expr(s)324 / if_then_else_expr(s)325326 / keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, Box::new(expr))}327 / assert:assertion(s) _ ";" _ rest:expr(s) { Expr::AssertExpr(Box::new(AssertExpr{328 assert, rest329 })) }330331 / err_kw:spanned(<keyword("error")>, s) _ expr:expr(s) { Expr::ErrorStmt(err_kw.span, Box::new(expr)) }332333 rule slice_part(s: &ParserSettings) -> Option<Spanned<Expr>>334 = _ e:(e:spanned(<expr(s)>, s) _{e})? {e}335 pub rule slice_desc(s: &ParserSettings) -> SliceDesc336 = start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {337 let (end, step) = if let Some((end, step)) = pair {338 (end, step)339 }else{340 (None, None)341 };342343 SliceDesc { start, end, step }344 }345346 rule binop(x: rule<()>) -> ()347 = quiet!{ x() } / expected!("<binary op>")348 rule unaryop(x: rule<()>) -> ()349 = quiet!{ x() } / expected!("<unary op>")350351 rule ensure_null_coaelse()352 = "" {?353 #[cfg(not(feature = "exp-null-coaelse"))] return Err("!!!experimental null coaelscing was not enabled");354 #[cfg(feature = "exp-null-coaelse")] Ok(())355 }356 use jrsonnet_ir::BinaryOpType::*;357 use jrsonnet_ir::UnaryOpType::*;358 rule expr(s: &ParserSettings) -> Expr359 = precedence! {360 a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}361 a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {362 #[cfg(feature = "exp-null-coaelse")] return expr_bin!(a NullCoaelse b);363 unreachable!("ensure_null_coaelse will fail if feature is not enabled")364 }365 --366 a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}367 --368 a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}369 --370 a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}371 --372 a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}373 --374 a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}375 a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}376 --377 a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}378 a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}379 a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}380 a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}381 a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}382 --383 a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}384 a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}385 --386 a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}387 a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}388 --389 a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}390 a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}391 a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}392 --393 unaryop(<"+">) _ b:@ {expr_un!(Plus b)}394 unaryop(<"-">) _ b:@ {expr_un!(Minus b)}395 unaryop(<"!">) _ b:@ {expr_un!(Not b)}396 unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}397 --398 value:(@) _ "[" _ slice:slice_desc(s) _ "]" {Expr::Slice(Box::new(Slice{value, slice}))}399 indexable:(@) _ parts:index_part(s)+ {Expr::Index{indexable: Box::new(indexable), parts}}400 a:(@) _ args:spanned(<"(" _ a:args(s) _ ")" {a}>, s) ts:(_ keyword("tailstrict"))? {Expr::Apply(Box::new(a), args, ts.is_some())}401 a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(Box::new(a), body)}402 --403 e:expr_basic(s) {e}404 "(" _ e:expr(s) _ ")" {e}405 }406 pub rule index_part(s: &ParserSettings) -> IndexPart407 = n:("?" _ ensure_null_coaelse())? "." _ value:id_loc(s) {IndexPart {408 span: value.span,409 value: value.value,410 #[cfg(feature = "exp-null-coaelse")]411 null_coaelse: n.is_some(),412 }}413 / n:("?" _ "." _ ensure_null_coaelse())? value:spanned(<"[" _ v:expr(s) _ "]" {v}>, s) {IndexPart {414 span: value.span,415 value: value.value,416 #[cfg(feature = "exp-null-coaelse")]417 null_coaelse: n.is_some(),418 }}419420 pub rule jsonnet(s: &ParserSettings) -> Expr = _ e:expr(s) _ {e}421 }422}423424pub type ParseError = peg::error::ParseError<peg::str::LineCol>;425pub fn parse(str: &str, settings: &ParserSettings) -> Result<Expr, ParseError> {426 jsonnet_parser::jsonnet(str, settings)427}428429pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> Spanned<Expr> {430 let len = str.len();431 Spanned::new(Expr::Str(str), Span(settings.source.clone(), 0, len as u32))432}433434#[cfg(test)]435mod tests {436 #[test]437 #[cfg(not(feature = "exp-null-coaelse"))]438 fn snapshots() {439 use std::fs;440441 use insta::{assert_snapshot, glob};442 use jrsonnet_ir::{IStr, Source};443444 use crate::{ParserSettings, parse};445446 glob!("tests/*.jsonnet", |path| {447 let input = fs::read_to_string(path).expect("read test file");448 let v = parse(449 &input,450 &ParserSettings {451 source: Source::new_virtual("<test>".into(), IStr::empty()),452 },453 )454 .unwrap();455 let v = format!("{v:#?}");456 assert_snapshot!(v);457 });458 }459}