1#![feature(box_syntax)]2#![feature(test)]34extern crate test;56use peg::parser;7use std::rc::Rc;8mod expr;9pub use expr::*;1011enum Suffix {12 String(String),13 Slice(SliceDesc),14 Expression(LocExpr),15 Apply(expr::ArgsDesc),16 Extend(expr::ObjBody),17}18struct LocSuffix(Suffix, ExprLocation);1920pub struct ParserSettings {21 pub loc_data: bool,22 pub file_name: String,23}2425parser! {26 grammar jsonnet_parser() for str {27 use peg::ParseLiteral;2829 30 rule comment()31 = "//" (!['\n'][_])* "\n"32 / "/*" ((!("*/")[_][_])/("\\" "*/"))* "*/"33 / "#" (!['\n'][_])* "\n"3435 rule _() = ([' ' | '\n' | '\t'] / comment())*3637 38 rule comma() = quiet!{_ "," _} / expected!("<comma>")39 rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}40 rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}41 rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']42 43 rule uint() -> u32 = a:$(digit()+) { a.parse().unwrap() }44 45 rule number() -> f64 = quiet!{a:$(uint() ("." uint())? (['e'|'E'] (s:['+'|'-'])? uint())?) { a.parse().unwrap() }} / expected!("<number>")4647 48 rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()49 rule id() -> String = quiet!{ !reserved() s:$(alpha() (alpha() / digit())*) {s.to_owned()}} / expected!("<identifier>")5051 rule keyword(id: &'static str)52 = ##parse_string_literal(id) end_of_ident()53 54 rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr55 = start:position!() v:x() end:position!() {loc_expr!(v, s.loc_data, (s.file_name.clone(), start, end))}5657 pub rule param(s: &ParserSettings) -> expr::Param = name:id() expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr) }58 pub rule params(s: &ParserSettings) -> expr::ParamsDesc59 = params:(param(s) ** comma()) {60 let mut defaults_started = false;61 for param in ¶ms {62 defaults_started = defaults_started || param.1.is_some();63 assert_eq!(defaults_started, param.1.is_some(), "defauld parameters should be used after all positionals");64 }65 expr::ParamsDesc(params)66 }67 / { expr::ParamsDesc(Vec::new()) }6869 pub rule arg(s: &ParserSettings) -> expr::Arg70 = name:id() _ "=" _ expr:expr(s) {expr::Arg(Some(name), expr)}71 / expr:expr(s) {expr::Arg(None, expr)}72 pub rule args(s: &ParserSettings) -> expr::ArgsDesc73 = args:arg(s) ** comma() comma()? {74 let mut named_started = false;75 for arg in &args {76 named_started = named_started || arg.0.is_some();77 assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");78 }79 expr::ArgsDesc(args)80 }81 / { expr::ArgsDesc(Vec::new()) }8283 pub rule bind(s: &ParserSettings) -> expr::BindSpec84 = name:id() _ "=" _ expr:expr(s) {expr::BindSpec{name, params: None, value: expr}}85 / name:id() _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name, params: Some(params), value: expr}}86 pub rule assertion(s: &ParserSettings) -> expr::AssertStmt87 = keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }88 pub rule string() -> String89 = v:("\"" str:$(("\\\"" / !['"'][_])*) "\"" {str.to_owned()}90 / "'" str:$((!['\''][_])*) "'" {str.to_owned()}) {v.replace("\\n", "\n")}91 pub rule field_name(s: &ParserSettings) -> expr::FieldName92 = name:id() {expr::FieldName::Fixed(name)}93 / name:string() {expr::FieldName::Fixed(name)}94 / "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}95 pub rule visibility() -> expr::Visibility96 = ":::" {expr::Visibility::Unhide}97 / "::" {expr::Visibility::Hidden}98 / ":" {expr::Visibility::Normal}99 pub rule field(s: &ParserSettings) -> expr::FieldMember100 = name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{101 name,102 plus: plus.is_some(),103 params: None,104 visibility,105 value,106 }}107 / name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{108 name,109 plus: false,110 params: Some(params),111 visibility,112 value,113 }}114 pub rule obj_local(s: &ParserSettings) -> BindSpec115 = keyword("local") _ bind:bind(s) {bind}116 pub rule member(s: &ParserSettings) -> expr::Member117 = bind:obj_local(s) {expr::Member::BindStmt(bind)}118 / assertion:assertion(s) {expr::Member::AssertStmt(assertion)}119 / field:field(s) {expr::Member::Field(field)}120 pub rule objinside(s: &ParserSettings) -> expr::ObjBody121 = pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ first:forspec(s) rest:(_ rest:compspec(s) {rest})? {122 expr::ObjBody::ObjComp {123 pre_locals,124 key,125 value,126 post_locals,127 first,128 rest: rest.unwrap_or_default(),129 }130 }131 / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}132 pub rule ifspec(s: &ParserSettings) -> IfSpecData133 = keyword("if") _ expr:expr(s) {IfSpecData(expr)}134 pub rule forspec(s: &ParserSettings) -> ForSpecData135 = keyword("for") _ id:id() _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}136 pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>137 = s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}138 pub rule local_expr(s: &ParserSettings) -> LocExpr139 = l(s,<keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }>)140 pub rule string_expr(s: &ParserSettings) -> LocExpr141 = l(s, <s:string() {Expr::Str(s)}>)142 pub rule obj_expr(s: &ParserSettings) -> LocExpr143 = l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)144 pub rule array_expr(s: &ParserSettings) -> LocExpr145 = l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)146 pub rule array_comp_expr(s: &ParserSettings) -> LocExpr147 = l(s,<"[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {Expr::ArrComp(expr, [vec![CompSpec::ForSpec(forspec)], others.unwrap_or_default()].concat())}>)148 pub rule number_expr(s: &ParserSettings) -> LocExpr149 = l(s,<n:number() { expr::Expr::Num(n) }>)150 pub rule var_expr(s: &ParserSettings) -> LocExpr151 = l(s,<n:id() { expr::Expr::Var(n) }>)152 pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr153 = l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{154 cond,155 cond_then,156 cond_else,157 }}>)158159 pub rule literal(s: &ParserSettings) -> LocExpr160 = l(s,<v:(161 keyword("null") {LiteralType::Null}162 / keyword("true") {LiteralType::True}163 / keyword("false") {LiteralType::False}164 / keyword("self") {LiteralType::This}165 / keyword("$") {LiteralType::Dollar}166 / keyword("super") {LiteralType::Super}167 ) {Expr::Literal(v)}>)168169 pub rule expr_basic(s: &ParserSettings) -> LocExpr170 = literal(s)171172 / string_expr(s) / number_expr(s)173 / array_expr(s)174 / obj_expr(s)175 / array_expr(s)176 / array_comp_expr(s)177178 / var_expr(s)179 / local_expr(s)180 / if_then_else_expr(s)181182 / l(s,<keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}>)183 / l(s,<assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }>)184185 / l(s,<keyword("error") _ expr:expr(s) { Expr::Error(expr) }>)186187 rule expr_basic_with_suffix(s: &ParserSettings) -> LocExpr188 = a:expr_basic(s) suffixes:(_ suffix:l_expr_suffix(s) {suffix})* {189 let mut cur = a;190 for suffix in suffixes {191 let LocSuffix(suffix, location) = suffix;192 cur = LocExpr(Rc::new(match suffix {193 Suffix::String(index) => Expr::Index(cur, loc_expr!(Expr::Str(index), s.loc_data, (s.file_name.clone(), location.1, location.2))),194 Suffix::Slice(desc) => Expr::Slice(cur, desc),195 Suffix::Expression(index) => Expr::Index(cur, index),196 Suffix::Apply(args) => Expr::Apply(cur, args),197 Suffix::Extend(body) => Expr::ObjExtend(cur, body),198 }), if s.loc_data { Some(Rc::new(location)) } else { None })199 }200 cur201 }202203 pub rule slice_desc(s: &ParserSettings) -> SliceDesc204 = start:expr(s)? _ ":" _ pair:(end:expr(s)? _ step:(":" _ e:expr(s) {e})? {(end, step)})? {205 if let Some((end, step)) = pair {206 SliceDesc { start, end, step }207 }else{208 SliceDesc { start, end: None, step: None }209 }210 }211212 rule expr_suffix(s: &ParserSettings) -> Suffix213 = "." _ s:id() { Suffix::String(s) }214 / "[" _ s:slice_desc(s) _ "]" { Suffix::Slice(s) }215 / "[" _ s:expr(s) _ "]" { Suffix::Expression(s) }216 / "(" _ args:args(s) _ ")" (_ keyword("tailstrict"))? { Suffix::Apply(args) }217 / "{" _ body:objinside(s) _ "}" { Suffix::Extend(body) }218 rule l_expr_suffix(s: &ParserSettings) -> LocSuffix219 = start:position!() suffix:expr_suffix(s) end:position!() {LocSuffix(suffix, ExprLocation(s.file_name.clone(), start, end))}220221 rule expr(s: &ParserSettings) -> LocExpr222 = start:position!() a:precedence! {223 a:(@) _ "||" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}224 --225 a:(@) _ "&&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}226 --227 a:(@) _ "|" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}228 --229 a:@ _ "^" _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}230 --231 a:(@) _ "&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}232 --233 a:(@) _ "==" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Eq, b))}234 a:(@) _ "!=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Ne, b))}235 --236 a:(@) _ "<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}237 a:(@) _ ">" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}238 a:(@) _ "<=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}239 a:(@) _ ">=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}240 --241 a:(@) _ "<<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}242 a:(@) _ ">>" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}243 --244 a:(@) _ "+" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}245 a:(@) _ "-" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}246 --247 a:(@) _ "*" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}248 a:(@) _ "/" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}249 a:(@) _ "%" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mod, b))}250 --251 "-" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}252 "!" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}253 "~" _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }254 --255 e:expr_basic_with_suffix(s) {e}256 "(" _ e:expr(s) _ ")" {loc_expr_todo!(Expr::Parened(e))}257 } end:position!() {258 let LocExpr(e, _) = a;259 LocExpr(e, if s.loc_data {260 Some(Rc::new(ExprLocation(s.file_name.to_owned(), start, end)))261 } else {262 None263 })264 }265 / e:expr_basic_with_suffix(s) {e}266267 pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}268 }269}270271pub fn parse(272 str: &str,273 settings: &ParserSettings,274) -> Result<LocExpr, peg::error::ParseError<peg::str::LineCol>> {275 jsonnet_parser::jsonnet(str, settings)276}277278#[macro_export]279macro_rules! el {280 ($expr:expr) => {281 LocExpr(std::rc::Rc::new($expr), None)282 };283}284285#[cfg(test)]286pub mod tests {287 use super::{expr::*, parse};288 use crate::ParserSettings;289290 macro_rules! parse {291 ($s:expr) => {292 parse(293 $s,294 &ParserSettings {295 loc_data: false,296 file_name: "test.jsonnet".to_owned(),297 },298 )299 .unwrap()300 };301 }302303 mod expressions {304 use super::*;305306 pub fn basic_math() -> LocExpr {307 el!(Expr::BinaryOp(308 el!(Expr::Num(2.0)),309 BinaryOpType::Add,310 el!(Expr::BinaryOp(311 el!(Expr::Num(2.0)),312 BinaryOpType::Mul,313 el!(Expr::Num(2.0)),314 )),315 ))316 }317 }318319 #[test]320 fn empty_object() {321 assert_eq!(parse!("{}"), el!(Expr::Obj(ObjBody::MemberList(vec![]))));322 }323324 #[test]325 fn basic_math() {326 assert_eq!(327 parse!("2+2*2"),328 el!(Expr::BinaryOp(329 el!(Expr::Num(2.0)),330 BinaryOpType::Add,331 el!(Expr::BinaryOp(332 el!(Expr::Num(2.0)),333 BinaryOpType::Mul,334 el!(Expr::Num(2.0))335 ))336 ))337 );338 }339340 #[test]341 fn basic_math_with_indents() {342 assert_eq!(parse!("2 + 2 * 2 "), expressions::basic_math());343 }344345 #[test]346 fn basic_math_parened() {347 assert_eq!(348 parse!("2+(2+2*2)"),349 el!(Expr::BinaryOp(350 el!(Expr::Num(2.0)),351 BinaryOpType::Add,352 el!(Expr::Parened(expressions::basic_math())),353 ))354 );355 }356357 358 #[test]359 fn comments() {360 assert_eq!(361 parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),362 el!(Expr::BinaryOp(363 el!(Expr::Num(2.0)),364 BinaryOpType::Add,365 el!(Expr::BinaryOp(366 el!(Expr::Num(3.0)),367 BinaryOpType::Mul,368 el!(Expr::Num(4.0))369 ))370 ))371 );372 }373374 375 #[test]376 fn comment_escaping() {377 assert_eq!(378 parse!("2/*\\*/+*/ - 22"),379 el!(Expr::BinaryOp(380 el!(Expr::Num(2.0)),381 BinaryOpType::Sub,382 el!(Expr::Num(22.0))383 ))384 );385 }386387 #[test]388 fn suffix_comparsion() {389 use Expr::*;390 assert_eq!(391 parse!("std.type(a) == \"string\""),392 el!(BinaryOp(393 el!(Apply(394 el!(Index(395 el!(Var("std".to_owned())),396 el!(Str("type".to_owned()))397 )),398 ArgsDesc(vec![Arg(None, el!(Var("a".to_owned())))])399 )),400 BinaryOpType::Eq,401 el!(Str("string".to_owned()))402 ))403 );404 }405406 #[test]407 fn array_comp() {408 use Expr::*;409 assert_eq!(410 parse!("[std.deepJoin(x) for x in arr]"),411 el!(ArrComp(412 el!(Apply(413 el!(Index(414 el!(Var("std".to_owned())),415 el!(Str("deepJoin".to_owned()))416 )),417 ArgsDesc(vec![Arg(None, el!(Var("x".to_owned())))])418 )),419 vec![CompSpec::ForSpec(ForSpecData(420 "x".to_owned(),421 el!(Var("arr".to_owned()))422 ))]423 )),424 )425 }426427 #[test]428 fn array_comp_with_ifs() {429 use Expr::*;430 assert_eq!(431 parse!("[k for k in std.objectFields(patch) if patch[k] == null]"),432 el!(ArrComp(433 el!(Var("k".to_owned())),434 vec![435 CompSpec::ForSpec(ForSpecData(436 "k".to_owned(),437 el!(Apply(438 el!(Index(439 el!(Var("std".to_owned())),440 el!(Str("objectFields".to_owned()))441 )),442 ArgsDesc(vec![Arg(None, el!(Var("patch".to_owned())))])443 ))444 )),445 CompSpec::IfSpec(IfSpecData(el!(BinaryOp(446 el!(Index(447 el!(Var("patch".to_owned())),448 el!(Var("k".to_owned()))449 )),450 BinaryOpType::Eq,451 el!(Literal(LiteralType::Null))452 ))))453 ]454 ))455 );456 }457458 #[test]459 fn reserved() {460 use Expr::*;461 assert_eq!(parse!("null"), el!(Literal(LiteralType::Null)));462 assert_eq!(parse!("nulla"), el!(Var("nulla".to_owned())));463 }464465 #[test]466 fn multiple_args_buf() {467 parse!("a(b, null_fields)");468 }469470 #[test]471 fn infix_precedence() {472 use Expr::*;473 assert_eq!(474 parse!("!a && !b"),475 el!(BinaryOp(476 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned())))),477 BinaryOpType::And,478 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".to_owned()))))479 ))480 );481 }482483 #[test]484 fn infix_precedence_division() {485 use Expr::*;486 assert_eq!(487 parse!("!a / !b"),488 el!(BinaryOp(489 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned())))),490 BinaryOpType::Div,491 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".to_owned()))))492 ))493 );494 }495496 #[test]497 fn double_negation() {498 use Expr::*;499 assert_eq!(500 parse!("!!a"),501 el!(UnaryOp(502 UnaryOpType::Not,503 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned()))))504 ))505 )506 }507508 #[test]509 fn array_test_error() {510 parse!("[a for a in b if c for e in f]");511 512 }513514 #[test]515 fn can_parse_stdlib() {516 parse!(jsonnet_stdlib::STDLIB_STR);517 }518519 use test::Bencher;520521 522 #[bench]523 fn bench_parse_peg(b: &mut Bencher) {524 b.iter(|| parse!(jsonnet_stdlib::STDLIB_STR))525 }526527 528 #[bench]529 fn bench_parse_serde_bincode(b: &mut Bencher) {530 let serialized = bincode::serialize(&parse!(jsonnet_stdlib::STDLIB_STR)).unwrap();531 b.iter(|| bincode::deserialize::<LocExpr>(&serialized))532 }533}