1use jrsonnet_gcmodule::Acyclic;2use jrsonnet_ir::{3 unescape, ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BindSpec, CompSpec, Destruct,4 DestructRest, Expr, ExprParam, ExprParams, FieldMember, FieldName, ForSpecData, IStr, IfElse,5 IfSpecData, ImportKind, IndexPart, LiteralType, Member, ObjBody, ObjComp, ObjMembers, Slice,6 SliceDesc, Source, Span, Spanned, Visibility,7};8use peg::parser;9use std::rc::Rc;1011pub struct ParserSettings {12 pub source: Source,13}1415macro_rules! expr_bin {16 ($a:ident $op:ident $b:ident) => {17 Expr::BinaryOp(Box::new(BinaryOp {18 lhs: $a,19 op: $op,20 rhs: $b,21 }))22 };23}24macro_rules! expr_un {25 ($op:ident $a:ident) => {26 Expr::UnaryOp($op, Box::new($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 38 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 47 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 52 rule uint_str() -> &'input str = a:$(digit()+ ("_" digit()+)*) { a }53 54 rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.replace("_","").parse().map_err(|_| "<number>") }} / expected!("<number>")5556 57 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) -> ExprParam = destruct:destruct(s) expr:(_ "=" _ expr:expr(s){expr})? { ExprParam { destruct, default: expr.map(Rc::new) } }64 pub rule params(s: &ParserSettings) -> ExprParams65 = params:param(s) ** comma() comma()? { ExprParams::new(params) }66 / { ExprParams::new(Vec::new()) }6768 pub rule arg(s: &ParserSettings) -> (Option<IStr>, Rc<Expr>)69 = name:(quiet! { (s:id() _ "=" !['='] _ {s})? } / expected!("<argument name>")) expr:expr(s) {(name, Rc::new(expr))}7071 pub rule args(s: &ParserSettings) -> 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(ArgsDesc::new(unnamed, named))89 }9091 pub rule destruct_rest() -> DestructRest92 = "..." into:(_ into:id() {into})? {if let Some(into) = into {93 DestructRest::Keep(into)94 } else {DestructRest::Drop}}95 pub rule destruct_array(s: &ParserSettings) -> 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) -> Destruct111 = "{" _112 fields:(name:id() into:(_ ":" _ into:destruct(s) {into})? default:(_ "=" _ v:expr(s) {v})? {(name, into, default.map(Rc::new))})**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) -> Destruct125 = v:id() {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) -> BindSpec134 = into:destruct(s) _ "=" _ value:expr(s) {BindSpec::Field{into, value: Rc::new(value)}}135 / name:id() _ "(" _ params:params(s) _ ")" _ "=" _ value:expr(s) {BindSpec::Function{name, params, value: Rc::new(value)}}136137 pub rule assertion(s: &ParserSettings) -> AssertStmt138 = keyword("assert") _ cond:spanned(<expr(s)>, s) msg:(_ ":" _ e:spanned(<expr(s)>, s) {e})? { AssertStmt(cond, msg) }139140 pub rule whole_line() -> &'input str141 = str:$((!['\n'][_])* "\n") {str}142 pub rule string_block() -> String143 = "|||" chomped:"-"? (!['\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 {149 let mut l = empty_lines.to_owned();150 l.push_str(first_line);151 l.extend(lines);152 if chomped.is_some() {153 debug_assert!(l.ends_with('\n'));154 l.truncate(l.len() - 1);155 }156 l157 }158159 rule hex_char()160 = quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")161162 rule string_char(c: rule<()>)163 = (!['\\']!c()[_])+164 / "\\\\"165 / "\\u" hex_char() hex_char() hex_char() hex_char()166 / "\\x" hex_char() hex_char()167 / ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't' | '"' | '\''] } / expected!("<escape character>"))168 pub rule string() -> String169 = ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}170 / ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}171 / quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}172 / "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}173 / string_block() } / expected!("<string>")174175 pub rule field_name(s: &ParserSettings) -> FieldName176 = name:id() {FieldName::Fixed(name)}177 / name:string() {FieldName::Fixed(name.into())}178 / "[" _ expr:expr(s) _ "]" {FieldName::Dyn(expr)}179 pub rule visibility() -> Visibility180 = ":::" {Visibility::Unhide}181 / "::" {Visibility::Hidden}182 / ":" {Visibility::Normal}183 pub rule field(s: &ParserSettings) -> FieldMember184 = name:spanned(<field_name(s)>, s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {FieldMember{185 name,186 plus: plus.is_some(),187 params: None,188 visibility,189 value: Rc::new(value),190 }}191 / name:spanned(<field_name(s)>, s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {FieldMember{192 name,193 plus: false,194 params: Some(params),195 visibility,196 value: Rc::new(value),197 }}198 pub rule obj_local(s: &ParserSettings) -> BindSpec199 = keyword("local") _ bind:bind(s) {bind}200 pub rule member(s: &ParserSettings) -> Member201 = bind:obj_local(s) {Member::BindStmt(bind)}202 / assertion:assertion(s) {Member::AssertStmt(assertion)}203 / field:field(s) {Member::Field(field)}204 pub rule objinside(s: &ParserSettings) -> ObjBody205 = members:(member(s) ** comma()) comma()? _ compspecs:compspecs(s)? {?206 Ok(if let Some(compspecs) = compspecs {207 let mut locals = Vec::new();208 let mut field = None;209 for member in members {210 match member {211 Member::Field(field_member) => if field.replace(field_member).is_some() {212 return Err("<object comprehension can only contain one field>")213 },214 Member::BindStmt(bind_spec) => locals.push(bind_spec),215 Member::AssertStmt(assert_stmt) => return Err("<asserts are unsupported in object comprehension>"),216 }217 }218 ObjBody::ObjComp(ObjComp {219 locals: Rc::new(locals),220 field: field.map(Rc::new).ok_or("<missing object comprehension field>")?,221 compspecs222 })223 } else {224 let mut locals = Vec::new();225 let mut asserts = Vec::new();226 let mut fields = Vec::new();227 for member in members {228 match member {229 Member::Field(field_member) => fields.push(field_member),230 Member::BindStmt(bind_spec) => locals.push(bind_spec),231 Member::AssertStmt(assert_stmt) => asserts.push(assert_stmt),232 }233 }234 ObjBody::MemberList(ObjMembers {235 locals: Rc::new(locals),236 asserts: Rc::new(asserts),237 fields238 })239 })240 }241 pub rule ifspec(s: &ParserSettings) -> IfSpecData242 = i:spanned(<keyword("if")>, s) _ cond:expr(s) {IfSpecData { span: i.span, cond }}243 pub rule forspec(s: &ParserSettings) -> ForSpecData244 = keyword("for") _ destruct:destruct(s) _ keyword("in") _ over:expr(s) { ForSpecData { destruct, over } }245 rule compspec(s: &ParserSettings) -> CompSpec246 = i:ifspec(s) { CompSpec::IfSpec(i) } / f:forspec(s) {CompSpec::ForSpec(f)}247 pub rule compspecs(s: &ParserSettings) -> Vec<CompSpec>248 = specs:compspec(s) ++ _ {?249 if !matches!(specs[0], CompSpec::ForSpec(_)) {250 return Err("<first compspec should be for>")251 }252 Ok(specs)253 }254 pub rule local_expr(s: &ParserSettings) -> Expr255 = keyword("local") _ binds:bind(s) ** comma() (_ ",")? _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, Box::new(expr)) }256 pub rule string_expr(s: &ParserSettings) -> Expr257 = s:string() {Expr::Str(s.into())}258 pub rule obj_expr(s: &ParserSettings) -> Expr259 = "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}260 pub rule array_expr(s: &ParserSettings) -> Expr261 = "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(Rc::new(elems))}262 pub rule array_comp_expr(s: &ParserSettings) -> Expr263 = "[" _ expr:expr(s) _ comma()? _ specs:(r: compspecs(s) _ {r}) "]" {264 Expr::ArrComp(Rc::new(expr), specs)265 }266 pub rule number_expr(s: &ParserSettings) -> Expr267 = n:number() {? if n.is_finite() {268 Ok(Expr::Num(n))269 } else {270 Err("!!!numbers are finite")271 }}272273 rule spanned<T: Acyclic>(x: rule<T>, s: &ParserSettings) -> Spanned<T>274 = a:position!() n:x() b:position!() { Spanned::new(n, Span(s.source.clone(), a as u32, b as u32)) }275276 pub rule var_expr(s: &ParserSettings) -> Expr277 = n:spanned(<id()>, s) { Expr::Var(n) }278 pub rule id_loc(s: &ParserSettings) -> Spanned<Expr>279 = a:position!() n:id() b:position!() { Spanned::new(Expr::Str(n), Span(s.source.clone(), a as u32,b as u32)) }280 pub rule if_then_else_expr(s: &ParserSettings) -> Expr281 = cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse(Box::new(IfElse{282 cond,283 cond_then,284 cond_else,285 }))}286287 pub rule literal(s: &ParserSettings) -> Expr288 = v:(289 keyword("null") {LiteralType::Null}290 / keyword("true") {LiteralType::True}291 / keyword("false") {LiteralType::False}292 / keyword("self") {LiteralType::This}293 / keyword("$") {LiteralType::Dollar}294 / keyword("super") {LiteralType::Super}295 ) {Expr::Literal(v)}296297 rule import_kind() -> ImportKind298 = keyword("importstr") { ImportKind::Str }299 / keyword("importbin") { ImportKind::Bin }300 / keyword("import") { ImportKind::Normal }301302 pub rule expr_basic(s: &ParserSettings) -> Expr303 = literal(s)304305 / string_expr(s) / number_expr(s)306 / array_expr(s)307 / obj_expr(s)308 / array_expr(s)309 / array_comp_expr(s)310311 / kind:spanned(<import_kind()>, s) _ path:expr(s) {Expr::Import(kind, Box::new(path))}312313 / var_expr(s)314 / local_expr(s)315 / if_then_else_expr(s)316317 / keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, Rc::new(expr))}318 / assert:assertion(s) _ ";" _ rest:expr(s) { Expr::AssertExpr(Rc::new(AssertExpr{319 assert, rest320 })) }321322 / err_kw:spanned(<keyword("error")>, s) _ expr:expr(s) { Expr::ErrorStmt(err_kw.span, Box::new(expr)) }323324 rule slice_part(s: &ParserSettings) -> Option<Spanned<Expr>>325 = _ e:(e:spanned(<expr(s)>, s) _{e})? {e}326 pub rule slice_desc(s: &ParserSettings) -> SliceDesc327 = start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {328 let (end, step) = if let Some((end, step)) = pair {329 (end, step)330 }else{331 (None, None)332 };333334 SliceDesc { start, end, step }335 }336337 rule binop(x: rule<()>) -> ()338 = quiet!{ x() } / expected!("<binary op>")339 rule unaryop(x: rule<()>) -> ()340 = quiet!{ x() } / expected!("<unary op>")341342 rule ensure_null_coaelse()343 = "" {?344 #[cfg(not(feature = "exp-null-coaelse"))] return Err("!!!experimental null coaelscing was not enabled");345 #[cfg(feature = "exp-null-coaelse")] Ok(())346 }347 use jrsonnet_ir::BinaryOpType::*;348 use jrsonnet_ir::UnaryOpType::*;349 rule expr(s: &ParserSettings) -> Expr350 = precedence! {351 a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}352 a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {353 #[cfg(feature = "exp-null-coaelse")] return expr_bin!(a NullCoaelse b);354 unreachable!("ensure_null_coaelse will fail if feature is not enabled")355 }356 --357 a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}358 --359 a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}360 --361 a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}362 --363 a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}364 --365 a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}366 a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}367 --368 a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}369 a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}370 a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}371 a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}372 a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}373 --374 a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}375 a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}376 --377 a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}378 a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}379 --380 a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}381 a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}382 a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}383 --384 unaryop(<"+">) _ b:@ {expr_un!(Plus b)}385 unaryop(<"-">) _ b:@ {expr_un!(Minus b)}386 unaryop(<"!">) _ b:@ {expr_un!(Not b)}387 unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}388 --389 value:(@) _ "[" _ slice:slice_desc(s) _ "]" {Expr::Slice(Box::new(Slice{value, slice}))}390 indexable:(@) _ parts:index_part(s)+ {Expr::Index{indexable: Box::new(indexable), parts}}391 a:(@) _ args:spanned(<"(" _ a:args(s) _ ")" {a}>, s) ts:(_ keyword("tailstrict"))? {Expr::Apply(Box::new(a), args, ts.is_some())}392 a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(Rc::new(a), body)}393 --394 e:expr_basic(s) {e}395 "(" _ e:expr(s) _ ")" {e}396 }397 pub rule index_part(s: &ParserSettings) -> IndexPart398 = n:("?" _ ensure_null_coaelse())? "." _ value:id_loc(s) {IndexPart {399 span: value.span,400 value: value.value,401 #[cfg(feature = "exp-null-coaelse")]402 null_coaelse: n.is_some(),403 }}404 / n:("?" _ "." _ ensure_null_coaelse())? value:spanned(<"[" _ v:expr(s) _ "]" {v}>, s) {IndexPart {405 span: value.span,406 value: value.value,407 #[cfg(feature = "exp-null-coaelse")]408 null_coaelse: n.is_some(),409 }}410411 pub rule jsonnet(s: &ParserSettings) -> Expr = _ e:expr(s) _ {e}412 }413}414415pub type ParseError = peg::error::ParseError<peg::str::LineCol>;416pub fn parse(str: &str, settings: &ParserSettings) -> Result<Expr, ParseError> {417 jsonnet_parser::jsonnet(str, settings)418}419420pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> Spanned<Expr> {421 let len = str.len();422 Spanned::new(Expr::Str(str), Span(settings.source.clone(), 0, len as u32))423}424425#[cfg(test)]426mod tests {427 use std::fs;428429 use insta::{assert_snapshot, glob};430 use jrsonnet_ir::{IStr, Source};431432 use crate::{parse, ParserSettings};433434 #[test]435 fn snapshots() {436 glob!("tests/*.jsonnet", |path| {437 let input = fs::read_to_string(path).expect("read test file");438 let v = parse(439 &input,440 &ParserSettings {441 source: Source::new_virtual("<test>".into(), IStr::empty()),442 },443 )444 .unwrap();445 let v = format!("{v:#?}");446 assert_snapshot!(v);447 });448 }449}