1use peg::parser;2use std::{path::PathBuf, rc::Rc};3mod expr;4pub use expr::*;5pub use peg;67#[derive(Default)]8pub struct ParserSettings {9 pub loc_data: bool,10 pub file_name: Rc<PathBuf>,11}1213parser! {14 grammar jsonnet_parser() for str {15 use peg::ParseLiteral;1617 18 rule comment()19 = "//" (!['\n'][_])* "\n"20 / "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"21 / "#" (!['\n'][_])* "\n"2223 rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")24 rule _() = single_whitespace()*2526 27 rule comma() = quiet!{_ "," _} / expected!("<comma>")28 rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}29 rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}30 rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']31 32 rule uint() -> u64 = a:$(digit()+) { a.parse().unwrap() }33 34 rule number() -> f64 = quiet!{a:$(uint() ("." uint())? (['e'|'E'] (s:['+'|'-'])? uint())?) { a.parse().unwrap() }} / expected!("<number>")3536 37 rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()38 rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")3940 rule keyword(id: &'static str)41 = ##parse_string_literal(id) end_of_ident()42 43 rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr44 = start:position!() v:x() end:position!() {loc_expr!(v, s.loc_data, (s.file_name.clone(), start, end))}4546 pub rule param(s: &ParserSettings) -> expr::Param = name:$(id()) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name.into(), expr) }47 pub rule params(s: &ParserSettings) -> expr::ParamsDesc48 = params:param(s) ** comma() comma()? {49 let mut defaults_started = false;50 for param in ¶ms {51 defaults_started = defaults_started || param.1.is_some();52 assert_eq!(defaults_started, param.1.is_some(), "defauld parameters should be used after all positionals");53 }54 expr::ParamsDesc(Rc::new(params))55 }56 / { expr::ParamsDesc(Rc::new(Vec::new())) }5758 pub rule arg(s: &ParserSettings) -> expr::Arg59 = name:$(id()) _ "=" _ expr:expr(s) {expr::Arg(Some(name.into()), expr)}60 / expr:expr(s) {expr::Arg(None, expr)}61 pub rule args(s: &ParserSettings) -> expr::ArgsDesc62 = args:arg(s) ** comma() comma()? {63 let mut named_started = false;64 for arg in &args {65 named_started = named_started || arg.0.is_some();66 assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");67 }68 expr::ArgsDesc(args)69 }70 / { expr::ArgsDesc(Vec::new()) }7172 pub rule bind(s: &ParserSettings) -> expr::BindSpec73 = name:$(id()) _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: None, value: expr}}74 / name:$(id()) _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: Some(params), value: expr}}75 pub rule assertion(s: &ParserSettings) -> expr::AssertStmt76 = keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }7778 pub rule whole_line() -> &'input str79 = str:$((!['\n'][_])* "\n") {str}80 pub rule string_block() -> String81 = "|||" (!['\n']single_whitespace())* "\n"82 prefix:[' ' | '\t']+ first_line:whole_line()83 lines:([' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*84 [' ' | '\t']*<, {prefix.len() - 1}> "|||"85 {let mut l = first_line.to_owned(); l.extend(lines); l}86 pub rule string() -> String87 = "\"" str:$(("\\\"" / "\\\\" / (!['"'][_]))*) "\"" {unescape::unescape(str).unwrap()}88 / "'" str:$(("\\'" / "\\\\" / (!['\''][_]))*) "'" {unescape::unescape(str).unwrap()}89 / "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}90 / "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}91 / string_block()9293 pub rule field_name(s: &ParserSettings) -> expr::FieldName94 = name:$(id()) {expr::FieldName::Fixed(name.into())}95 / name:string() {expr::FieldName::Fixed(name.into())}96 / "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}97 pub rule visibility() -> expr::Visibility98 = ":::" {expr::Visibility::Unhide}99 / "::" {expr::Visibility::Hidden}100 / ":" {expr::Visibility::Normal}101 pub rule field(s: &ParserSettings) -> expr::FieldMember102 = name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{103 name,104 plus: plus.is_some(),105 params: None,106 visibility,107 value,108 }}109 / name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{110 name,111 plus: false,112 params: Some(params),113 visibility,114 value,115 }}116 pub rule obj_local(s: &ParserSettings) -> BindSpec117 = keyword("local") _ bind:bind(s) {bind}118 pub rule member(s: &ParserSettings) -> expr::Member119 = bind:obj_local(s) {expr::Member::BindStmt(bind)}120 / assertion:assertion(s) {expr::Member::AssertStmt(assertion)}121 / field:field(s) {expr::Member::Field(field)}122 pub rule objinside(s: &ParserSettings) -> expr::ObjBody123 = pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {124 let mut compspecs = vec![CompSpec::ForSpec(forspec)];125 compspecs.extend(others.unwrap_or_default());126 expr::ObjBody::ObjComp(expr::ObjComp{127 pre_locals,128 key,129 value,130 post_locals,131 compspecs,132 })133 }134 / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}135 pub rule ifspec(s: &ParserSettings) -> IfSpecData136 = keyword("if") _ expr:expr(s) {IfSpecData(expr)}137 pub rule forspec(s: &ParserSettings) -> ForSpecData138 = keyword("for") _ id:$(id()) _ keyword("in") _ cond:expr(s) {ForSpecData(id.into(), cond)}139 pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>140 = s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}141 pub rule local_expr(s: &ParserSettings) -> LocExpr142 = l(s,<keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }>)143 pub rule string_expr(s: &ParserSettings) -> LocExpr144 = l(s, <s:string() {Expr::Str(s.into())}>)145 pub rule obj_expr(s: &ParserSettings) -> LocExpr146 = l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)147 pub rule array_expr(s: &ParserSettings) -> LocExpr148 = l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)149 pub rule array_comp_expr(s: &ParserSettings) -> LocExpr150 = l(s,<"[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {151 let mut specs = vec![CompSpec::ForSpec(forspec)];152 specs.extend(others.unwrap_or_default());153 Expr::ArrComp(expr, specs)154 }>)155 pub rule number_expr(s: &ParserSettings) -> LocExpr156 = l(s,<n:number() { expr::Expr::Num(n) }>)157 pub rule var_expr(s: &ParserSettings) -> LocExpr158 = l(s,<n:$(id()) { expr::Expr::Var(n.into()) }>)159 pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr160 = l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{161 cond,162 cond_then,163 cond_else,164 }}>)165166 pub rule literal(s: &ParserSettings) -> LocExpr167 = l(s,<v:(168 keyword("null") {LiteralType::Null}169 / keyword("true") {LiteralType::True}170 / keyword("false") {LiteralType::False}171 / keyword("self") {LiteralType::This}172 / keyword("$") {LiteralType::Dollar}173 / keyword("super") {LiteralType::Super}174 ) {Expr::Literal(v)}>)175176 pub rule expr_basic(s: &ParserSettings) -> LocExpr177 = literal(s)178179 / string_expr(s) / number_expr(s)180 / array_expr(s)181 / obj_expr(s)182 / array_expr(s)183 / array_comp_expr(s)184185 / l(s,<keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}>)186 / l(s,<keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}>)187188 / var_expr(s)189 / local_expr(s)190 / if_then_else_expr(s)191192 / l(s,<keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}>)193 / l(s,<assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }>)194195 / l(s,<keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }>)196197 rule slice_part(s: &ParserSettings) -> Option<LocExpr>198 = e:(_ e:expr(s) _{e})? {e}199 pub rule slice_desc(s: &ParserSettings) -> SliceDesc200 = start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {201 let (end, step) = if let Some((end, step)) = pair {202 (end, step)203 }else{204 (None, None)205 };206207 SliceDesc { start, end, step }208 }209210 rule expr(s: &ParserSettings) -> LocExpr211 = start:position!() a:precedence! {212 a:(@) _ "||" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}213 --214 a:(@) _ "&&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}215 --216 a:(@) _ "|" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}217 --218 a:@ _ "^" _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}219 --220 a:(@) _ "&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}221 --222 a:(@) _ "==" _ b:@ {loc_expr_todo!(Expr::Apply(223 el!(Expr::Index(224 el!(Expr::Var("std".into())),225 el!(Expr::Str("equals".into()))226 )),227 ArgsDesc(vec![Arg(None, a), Arg(None, b)]),228 true229 ))}230 a:(@) _ "!=" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, el!(Expr::Apply(231 el!(Expr::Index(232 el!(Expr::Var("std".into())),233 el!(Expr::Str("equals".into()))234 )),235 ArgsDesc(vec![Arg(None, a), Arg(None, b)]),236 true237 ))))}238 --239 a:(@) _ "<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}240 a:(@) _ ">" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}241 a:(@) _ "<=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}242 a:(@) _ ">=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}243 a:(@) _ keyword("in") _ b:@ {loc_expr_todo!(Expr::Apply(244 el!(Expr::Index(245 el!(Expr::Var("std".into())),246 el!(Expr::Str("objectHasEx".into()))247 )), ArgsDesc(vec![Arg(None, b), Arg(None, a), Arg(None, el!(Expr::Literal(LiteralType::True)))]),248 true249 ))}250 --251 a:(@) _ "<<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}252 a:(@) _ ">>" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}253 --254 a:(@) _ "+" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}255 a:(@) _ "-" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}256 --257 a:(@) _ "*" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}258 a:(@) _ "/" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}259 a:(@) _ "%" _ b:@ {loc_expr_todo!(Expr::Apply(260 el!(Expr::Index(261 el!(Expr::Var("std".into())),262 el!(Expr::Str("mod".into()))263 )), ArgsDesc(vec![Arg(None, a), Arg(None, b)]),264 false265 ))}266 --267 "-" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}268 "!" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}269 "~" _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }270 --271 a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Apply(272 el!(Expr::Index(273 el!(Expr::Var("std".into())),274 el!(Expr::Str("slice".into())),275 )),276 ArgsDesc(vec![277 Arg(None, a),278 Arg(None, s.start.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),279 Arg(None, s.end.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),280 Arg(None, s.step.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),281 ]),282 true,283 ))}284 a:(@) _ "." _ s:$(id()) {loc_expr_todo!(Expr::Index(a, el!(Expr::Str(s.into()))))}285 a:(@) _ "[" _ s:expr(s) _ "]" {loc_expr_todo!(Expr::Index(a, s))}286 a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {loc_expr_todo!(Expr::Apply(a, args, ts.is_some()))}287 a:(@) _ "{" _ body:objinside(s) _ "}" {loc_expr_todo!(Expr::ObjExtend(a, body))}288 --289 e:expr_basic(s) {e}290 "(" _ e:expr(s) _ ")" {loc_expr_todo!(Expr::Parened(e))}291 } end:position!() {292 let LocExpr(e, _) = a;293 LocExpr(e, if s.loc_data {294 Some(ExprLocation(s.file_name.clone(), start, end))295 } else {296 None297 })298 }299 / e:expr_basic(s) {e}300301 pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}302 }303}304305pub type ParseError = peg::error::ParseError<peg::str::LineCol>;306pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {307 jsonnet_parser::jsonnet(str, settings)308}309310#[macro_export]311macro_rules! el {312 ($expr:expr) => {313 LocExpr(std::rc::Rc::new($expr), None)314 };315}316317#[cfg(test)]318pub mod tests {319 use super::{expr::*, parse};320 use crate::ParserSettings;321 use std::path::PathBuf;322 use std::rc::Rc;323324 macro_rules! parse {325 ($s:expr) => {326 parse(327 $s,328 &ParserSettings {329 loc_data: false,330 file_name: Rc::new(PathBuf::from("/test.jsonnet")),331 },332 )333 .unwrap()334 };335 }336337 mod expressions {338 use super::*;339340 pub fn basic_math() -> LocExpr {341 el!(Expr::BinaryOp(342 el!(Expr::Num(2.0)),343 BinaryOpType::Add,344 el!(Expr::BinaryOp(345 el!(Expr::Num(2.0)),346 BinaryOpType::Mul,347 el!(Expr::Num(2.0)),348 )),349 ))350 }351 }352353 #[test]354 fn multiline_string() {355 assert_eq!(356 parse!("|||\n Hello world!\n a\n|||"),357 el!(Expr::Str("Hello world!\n a\n".into())),358 );359 assert_eq!(360 parse!("|||\n Hello world!\n a\n|||"),361 el!(Expr::Str("Hello world!\n a\n".into())),362 );363 assert_eq!(364 parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),365 el!(Expr::Str("Hello world!\n\ta\n".into())),366 );367 assert_eq!(368 parse!("|||\n Hello world!\n a\n |||"),369 el!(Expr::Str("Hello world!\n a\n".into())),370 );371 }372373 #[test]374 fn slice() {375 parse!("a[1:]");376 parse!("a[1::]");377 parse!("a[:1:]");378 parse!("a[::1]");379 parse!("str[:len - 1]");380 }381382 #[test]383 fn string_escaping() {384 assert_eq!(385 parse!(r#""Hello, \"world\"!""#),386 el!(Expr::Str(r#"Hello, "world"!"#.into())),387 );388 assert_eq!(389 parse!(r#"'Hello \'world\'!'"#),390 el!(Expr::Str("Hello 'world'!".into())),391 );392 assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into())),);393 }394395 #[test]396 fn string_unescaping() {397 assert_eq!(398 parse!(r#""Hello\nWorld""#),399 el!(Expr::Str("Hello\nWorld".into())),400 );401 }402403 #[test]404 fn string_verbantim() {405 assert_eq!(406 parse!(r#"@"Hello\n""World""""#),407 el!(Expr::Str("Hello\\n\"World\"".into())),408 );409 }410411 #[test]412 fn imports() {413 assert_eq!(414 parse!("import \"hello\""),415 el!(Expr::Import(PathBuf::from("hello"))),416 );417 assert_eq!(418 parse!("importstr \"garnish.txt\""),419 el!(Expr::ImportStr(PathBuf::from("garnish.txt")))420 );421 }422423 #[test]424 fn empty_object() {425 assert_eq!(parse!("{}"), el!(Expr::Obj(ObjBody::MemberList(vec![]))));426 }427428 #[test]429 fn basic_math() {430 assert_eq!(431 parse!("2+2*2"),432 el!(Expr::BinaryOp(433 el!(Expr::Num(2.0)),434 BinaryOpType::Add,435 el!(Expr::BinaryOp(436 el!(Expr::Num(2.0)),437 BinaryOpType::Mul,438 el!(Expr::Num(2.0))439 ))440 ))441 );442 }443444 #[test]445 fn basic_math_with_indents() {446 assert_eq!(parse!("2 + 2 * 2 "), expressions::basic_math());447 }448449 #[test]450 fn basic_math_parened() {451 assert_eq!(452 parse!("2+(2+2*2)"),453 el!(Expr::BinaryOp(454 el!(Expr::Num(2.0)),455 BinaryOpType::Add,456 el!(Expr::Parened(expressions::basic_math())),457 ))458 );459 }460461 462 #[test]463 fn comments() {464 assert_eq!(465 parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),466 el!(Expr::BinaryOp(467 el!(Expr::Num(2.0)),468 BinaryOpType::Add,469 el!(Expr::BinaryOp(470 el!(Expr::Num(3.0)),471 BinaryOpType::Mul,472 el!(Expr::Num(4.0))473 ))474 ))475 );476 }477478 479 #[test]480 fn comment_escaping() {481 assert_eq!(482 parse!("2/*\\*/+*/ - 22"),483 el!(Expr::BinaryOp(484 el!(Expr::Num(2.0)),485 BinaryOpType::Sub,486 el!(Expr::Num(22.0))487 ))488 );489 }490491 #[test]492 fn suffix() {493 494 495 496 497 }498499 #[test]500 fn array_comp() {501 use Expr::*;502 assert_eq!(503 parse!("[std.deepJoin(x) for x in arr]"),504 el!(ArrComp(505 el!(Apply(506 el!(Index(el!(Var("std".into())), el!(Str("deepJoin".into())))),507 ArgsDesc(vec![Arg(None, el!(Var("x".into())))]),508 false,509 )),510 vec![CompSpec::ForSpec(ForSpecData(511 "x".into(),512 el!(Var("arr".into()))513 ))]514 )),515 )516 }517518 #[test]519 fn reserved() {520 use Expr::*;521 assert_eq!(parse!("null"), el!(Literal(LiteralType::Null)));522 assert_eq!(parse!("nulla"), el!(Var("nulla".into())));523 }524525 #[test]526 fn multiple_args_buf() {527 parse!("a(b, null_fields)");528 }529530 #[test]531 fn infix_precedence() {532 use Expr::*;533 assert_eq!(534 parse!("!a && !b"),535 el!(BinaryOp(536 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),537 BinaryOpType::And,538 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))539 ))540 );541 }542543 #[test]544 fn infix_precedence_division() {545 use Expr::*;546 assert_eq!(547 parse!("!a / !b"),548 el!(BinaryOp(549 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),550 BinaryOpType::Div,551 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))552 ))553 );554 }555556 #[test]557 fn double_negation() {558 use Expr::*;559 assert_eq!(560 parse!("!!a"),561 el!(UnaryOp(562 UnaryOpType::Not,563 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()))))564 ))565 )566 }567568 #[test]569 fn array_test_error() {570 parse!("[a for a in b if c for e in f]");571 572 }573574 #[test]575 fn can_parse_stdlib() {576 parse!(jrsonnet_stdlib::STDLIB_STR);577 }578579 580 581582583584585586}