difftreelog
feat(parser) tailstrict call, multiline
in: master
3 files changed
crates/jsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jsonnet-parser/src/expr.rs
+++ b/crates/jsonnet-parser/src/expr.rs
@@ -196,7 +196,7 @@
/// error "I'm broken"
Error(LocExpr),
/// a(b, c)
- Apply(LocExpr, ArgsDesc),
+ Apply(LocExpr, ArgsDesc, bool),
///
Select(LocExpr, String),
/// a[b]
crates/jsonnet-parser/src/lib.rsdiffbeforeafterboth1#![feature(box_syntax)]2#![feature(test)]34extern crate test;56use peg::parser;7use std::{path::PathBuf, rc::Rc};8mod expr;9pub use expr::*;10pub use peg;1112pub struct ParserSettings {13 pub loc_data: bool,14 pub file_name: PathBuf,15}1617parser! {18 grammar jsonnet_parser() for str {19 use peg::ParseLiteral;2021 /// Standard C-like comments22 rule comment()23 = "//" (!['\n'][_])* "\n"24 / "/*" ((!("*/")[_][_])/("\\" "*/"))* "*/"25 / "#" (!['\n'][_])* "\n"2627 rule _() = ([' ' | '\n' | '\t'] / comment())*2829 /// For comma-delimited elements30 rule comma() = quiet!{_ "," _} / expected!("<comma>")31 rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}32 rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}33 rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']34 /// Sequence of digits35 rule uint() -> u64 = a:$(digit()+) { a.parse().unwrap() }36 /// Number in scientific notation format37 rule number() -> f64 = quiet!{a:$(uint() ("." uint())? (['e'|'E'] (s:['+'|'-'])? uint())?) { a.parse().unwrap() }} / expected!("<number>")3839 /// Reserved word followed by any non-alphanumberic40 rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()41 rule id() -> String = quiet!{ !reserved() s:$(alpha() (alpha() / digit())*) {s.to_owned()}} / expected!("<identifier>")4243 rule keyword(id: &'static str)44 = ##parse_string_literal(id) end_of_ident()45 // Adds location data information to existing expression46 rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr47 = start:position!() v:x() end:position!() {loc_expr!(v, s.loc_data, (s.file_name.clone(), start, end))}4849 pub rule param(s: &ParserSettings) -> expr::Param = name:id() expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr) }50 pub rule params(s: &ParserSettings) -> expr::ParamsDesc51 = params:(param(s) ** comma()) {52 let mut defaults_started = false;53 for param in ¶ms {54 defaults_started = defaults_started || param.1.is_some();55 assert_eq!(defaults_started, param.1.is_some(), "defauld parameters should be used after all positionals");56 }57 expr::ParamsDesc(params)58 }59 / { expr::ParamsDesc(Vec::new()) }6061 pub rule arg(s: &ParserSettings) -> expr::Arg62 = name:id() _ "=" _ expr:expr(s) {expr::Arg(Some(name), expr)}63 / expr:expr(s) {expr::Arg(None, expr)}64 pub rule args(s: &ParserSettings) -> expr::ArgsDesc65 = args:arg(s) ** comma() comma()? {66 let mut named_started = false;67 for arg in &args {68 named_started = named_started || arg.0.is_some();69 assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");70 }71 expr::ArgsDesc(args)72 }73 / { expr::ArgsDesc(Vec::new()) }7475 pub rule bind(s: &ParserSettings) -> expr::BindSpec76 = name:id() _ "=" _ expr:expr(s) {expr::BindSpec{name, params: None, value: expr}}77 / name:id() _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name, params: Some(params), value: expr}}78 pub rule assertion(s: &ParserSettings) -> expr::AssertStmt79 = keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }80 pub rule string() -> String81 = v:("\"" str:$(("\\\"" / !['"'][_])*) "\"" {str.to_owned()}82 / "'" str:$((!['\''][_])*) "'" {str.to_owned()}) {v.replace("\\n", "\n")}83 pub rule field_name(s: &ParserSettings) -> expr::FieldName84 = name:id() {expr::FieldName::Fixed(name)}85 / name:string() {expr::FieldName::Fixed(name)}86 / "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}87 pub rule visibility() -> expr::Visibility88 = ":::" {expr::Visibility::Unhide}89 / "::" {expr::Visibility::Hidden}90 / ":" {expr::Visibility::Normal}91 pub rule field(s: &ParserSettings) -> expr::FieldMember92 = name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{93 name,94 plus: plus.is_some(),95 params: None,96 visibility,97 value,98 }}99 / name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{100 name,101 plus: false,102 params: Some(params),103 visibility,104 value,105 }}106 pub rule obj_local(s: &ParserSettings) -> BindSpec107 = keyword("local") _ bind:bind(s) {bind}108 pub rule member(s: &ParserSettings) -> expr::Member109 = bind:obj_local(s) {expr::Member::BindStmt(bind)}110 / assertion:assertion(s) {expr::Member::AssertStmt(assertion)}111 / field:field(s) {expr::Member::Field(field)}112 pub rule objinside(s: &ParserSettings) -> expr::ObjBody113 = 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})? {114 expr::ObjBody::ObjComp {115 pre_locals,116 key,117 value,118 post_locals,119 rest: [vec![CompSpec::ForSpec(forspec)], others.unwrap_or_default()].concat(),120 }121 }122 / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}123 pub rule ifspec(s: &ParserSettings) -> IfSpecData124 = keyword("if") _ expr:expr(s) {IfSpecData(expr)}125 pub rule forspec(s: &ParserSettings) -> ForSpecData126 = keyword("for") _ id:id() _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}127 pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>128 = s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}129 pub rule local_expr(s: &ParserSettings) -> LocExpr130 = l(s,<keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }>)131 pub rule string_expr(s: &ParserSettings) -> LocExpr132 = l(s, <s:string() {Expr::Str(s)}>)133 pub rule obj_expr(s: &ParserSettings) -> LocExpr134 = l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)135 pub rule array_expr(s: &ParserSettings) -> LocExpr136 = l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)137 pub rule array_comp_expr(s: &ParserSettings) -> LocExpr138 = 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())}>)139 pub rule number_expr(s: &ParserSettings) -> LocExpr140 = l(s,<n:number() { expr::Expr::Num(n) }>)141 pub rule var_expr(s: &ParserSettings) -> LocExpr142 = l(s,<n:id() { expr::Expr::Var(n) }>)143 pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr144 = l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{145 cond,146 cond_then,147 cond_else,148 }}>)149150 pub rule literal(s: &ParserSettings) -> LocExpr151 = l(s,<v:(152 keyword("null") {LiteralType::Null}153 / keyword("true") {LiteralType::True}154 / keyword("false") {LiteralType::False}155 / keyword("self") {LiteralType::This}156 / keyword("$") {LiteralType::Dollar}157 / keyword("super") {LiteralType::Super}158 ) {Expr::Literal(v)}>)159160 pub rule expr_basic(s: &ParserSettings) -> LocExpr161 = literal(s)162163 / string_expr(s) / number_expr(s)164 / array_expr(s)165 / obj_expr(s)166 / array_expr(s)167 / array_comp_expr(s)168169 / var_expr(s)170 / local_expr(s)171 / if_then_else_expr(s)172173 / l(s,<keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}>)174 / l(s,<assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }>)175176 / l(s,<keyword("error") _ expr:expr(s) { Expr::Error(expr) }>)177178 pub rule slice_desc(s: &ParserSettings) -> SliceDesc179 = start:expr(s)? _ ":" _ pair:(end:expr(s)? _ step:(":" _ e:expr(s) {e})? {(end, step)})? {180 if let Some((end, step)) = pair {181 SliceDesc { start, end, step }182 }else{183 SliceDesc { start, end: None, step: None }184 }185 }186187 rule expr(s: &ParserSettings) -> LocExpr188 = start:position!() a:precedence! {189 a:(@) _ "||" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}190 --191 a:(@) _ "&&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}192 --193 a:(@) _ "|" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}194 --195 a:@ _ "^" _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}196 --197 a:(@) _ "&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}198 --199 a:(@) _ "==" _ b:@ {loc_expr_todo!(Expr::Apply(200 el!(Expr::Index(201 el!(Expr::Var("std".to_owned())),202 el!(Expr::Str("equals".to_owned()))203 )), ArgsDesc(vec![Arg(None, a), Arg(None, b)])204 ))}205 a:(@) _ "!=" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, el!(Expr::Apply(206 el!(Expr::Index(207 el!(Expr::Var("std".to_owned())),208 el!(Expr::Str("equals".to_owned()))209 )), ArgsDesc(vec![Arg(None, a), Arg(None, b)])210 ))))}211 --212 a:(@) _ "<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}213 a:(@) _ ">" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}214 a:(@) _ "<=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}215 a:(@) _ ">=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}216 --217 a:(@) _ "<<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}218 a:(@) _ ">>" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}219 --220 a:(@) _ "+" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}221 a:(@) _ "-" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}222 --223 a:(@) _ "*" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}224 a:(@) _ "/" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}225 a:(@) _ "%" _ b:@ {loc_expr_todo!(Expr::Apply(226 el!(Expr::Index(227 el!(Expr::Var("std".to_owned())),228 el!(Expr::Str("mod".to_owned()))229 )), ArgsDesc(vec![Arg(None, a), Arg(None, b)])230 ))}231 --232 "-" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}233 "!" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}234 "~" _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }235 --236 a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Slice(a, s))}237 a:(@) _ "." _ s:id() {loc_expr_todo!(Expr::Index(a, el!(Expr::Str(s))))}238 a:(@) _ "[" _ s:expr(s) _ "]" {loc_expr_todo!(Expr::Index(a, s))}239 a:(@) _ "(" _ args:args(s) _ ")" (_ keyword("tailstrict"))? {loc_expr_todo!(Expr::Apply(a, args))}240 a:(@) _ "{" _ body:objinside(s) _ "}" {loc_expr_todo!(Expr::ObjExtend(a, body))}241 --242 e:expr_basic(s) {e}243 "(" _ e:expr(s) _ ")" {loc_expr_todo!(Expr::Parened(e))}244 } end:position!() {245 let LocExpr(e, _) = a;246 LocExpr(e, if s.loc_data {247 Some(Rc::new(ExprLocation(s.file_name.to_owned(), start, end)))248 } else {249 None250 })251 }252 / e:expr_basic(s) {e}253254 pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}255 }256}257258pub type ParseError = peg::error::ParseError<peg::str::LineCol>;259pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {260 jsonnet_parser::jsonnet(str, settings)261}262263#[macro_export]264macro_rules! el {265 ($expr:expr) => {266 LocExpr(std::rc::Rc::new($expr), None)267 };268}269270#[cfg(test)]271pub mod tests {272 use super::{expr::*, parse};273 use crate::ParserSettings;274 use std::path::PathBuf;275276 macro_rules! parse {277 ($s:expr) => {278 parse(279 $s,280 &ParserSettings {281 loc_data: false,282 file_name: PathBuf::from("/test.jsonnet"),283 },284 )285 .unwrap()286 };287 }288289 mod expressions {290 use super::*;291292 pub fn basic_math() -> LocExpr {293 el!(Expr::BinaryOp(294 el!(Expr::Num(2.0)),295 BinaryOpType::Add,296 el!(Expr::BinaryOp(297 el!(Expr::Num(2.0)),298 BinaryOpType::Mul,299 el!(Expr::Num(2.0)),300 )),301 ))302 }303 }304305 #[test]306 fn empty_object() {307 assert_eq!(parse!("{}"), el!(Expr::Obj(ObjBody::MemberList(vec![]))));308 }309310 #[test]311 fn basic_math() {312 assert_eq!(313 parse!("2+2*2"),314 el!(Expr::BinaryOp(315 el!(Expr::Num(2.0)),316 BinaryOpType::Add,317 el!(Expr::BinaryOp(318 el!(Expr::Num(2.0)),319 BinaryOpType::Mul,320 el!(Expr::Num(2.0))321 ))322 ))323 );324 }325326 #[test]327 fn basic_math_with_indents() {328 assert_eq!(parse!("2 + 2 * 2 "), expressions::basic_math());329 }330331 #[test]332 fn basic_math_parened() {333 assert_eq!(334 parse!("2+(2+2*2)"),335 el!(Expr::BinaryOp(336 el!(Expr::Num(2.0)),337 BinaryOpType::Add,338 el!(Expr::Parened(expressions::basic_math())),339 ))340 );341 }342343 /// Comments should not affect parsing344 #[test]345 fn comments() {346 assert_eq!(347 parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),348 el!(Expr::BinaryOp(349 el!(Expr::Num(2.0)),350 BinaryOpType::Add,351 el!(Expr::BinaryOp(352 el!(Expr::Num(3.0)),353 BinaryOpType::Mul,354 el!(Expr::Num(4.0))355 ))356 ))357 );358 }359360 /// Comments should be able to be escaped361 #[test]362 fn comment_escaping() {363 assert_eq!(364 parse!("2/*\\*/+*/ - 22"),365 el!(Expr::BinaryOp(366 el!(Expr::Num(2.0)),367 BinaryOpType::Sub,368 el!(Expr::Num(22.0))369 ))370 );371 }372373 #[test]374 fn suffix() {375 // assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));376 // assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));377 // assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));378 // assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))379 }380381 #[test]382 fn array_comp() {383 use Expr::*;384 assert_eq!(385 parse!("[std.deepJoin(x) for x in arr]"),386 el!(ArrComp(387 el!(Apply(388 el!(Index(389 el!(Var("std".to_owned())),390 el!(Str("deepJoin".to_owned()))391 )),392 ArgsDesc(vec![Arg(None, el!(Var("x".to_owned())))])393 )),394 vec![CompSpec::ForSpec(ForSpecData(395 "x".to_owned(),396 el!(Var("arr".to_owned()))397 ))]398 )),399 )400 }401402 #[test]403 fn reserved() {404 use Expr::*;405 assert_eq!(parse!("null"), el!(Literal(LiteralType::Null)));406 assert_eq!(parse!("nulla"), el!(Var("nulla".to_owned())));407 }408409 #[test]410 fn multiple_args_buf() {411 parse!("a(b, null_fields)");412 }413414 #[test]415 fn infix_precedence() {416 use Expr::*;417 assert_eq!(418 parse!("!a && !b"),419 el!(BinaryOp(420 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned())))),421 BinaryOpType::And,422 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".to_owned()))))423 ))424 );425 }426427 #[test]428 fn infix_precedence_division() {429 use Expr::*;430 assert_eq!(431 parse!("!a / !b"),432 el!(BinaryOp(433 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned())))),434 BinaryOpType::Div,435 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".to_owned()))))436 ))437 );438 }439440 #[test]441 fn double_negation() {442 use Expr::*;443 assert_eq!(444 parse!("!!a"),445 el!(UnaryOp(446 UnaryOpType::Not,447 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned()))))448 ))449 )450 }451452 #[test]453 fn array_test_error() {454 parse!("[a for a in b if c for e in f]");455 // ^^^^ failed code456 }457458 #[test]459 fn can_parse_stdlib() {460 parse!(jsonnet_stdlib::STDLIB_STR);461 }462463 use test::Bencher;464465 // From source code466 #[bench]467 fn bench_parse_peg(b: &mut Bencher) {468 b.iter(|| parse!(jsonnet_stdlib::STDLIB_STR))469 }470471 // From serialized blob472 #[bench]473 fn bench_parse_serde_bincode(b: &mut Bencher) {474 let serialized = bincode::serialize(&parse!(jsonnet_stdlib::STDLIB_STR)).unwrap();475 b.iter(|| bincode::deserialize::<LocExpr>(&serialized))476 }477}1#![feature(box_syntax)]2#![feature(test)]34extern crate test;56use peg::parser;7use std::{path::PathBuf, rc::Rc};8mod expr;9mod string_processing;10pub use expr::*;11pub use peg;12use string_processing::deent;1314pub struct ParserSettings {15 pub loc_data: bool,16 pub file_name: PathBuf,17}1819parser! {20 grammar jsonnet_parser() for str {21 use peg::ParseLiteral;2223 /// Standard C-like comments24 rule comment()25 = "//" (!['\n'][_])* "\n"26 / "/*" ((!("*/")[_][_])/("\\" "*/"))* "*/"27 / "#" (!['\n'][_])* "\n"2829 rule _() = ([' ' | '\n' | '\t'] / comment())*3031 /// For comma-delimited elements32 rule comma() = quiet!{_ "," _} / expected!("<comma>")33 rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}34 rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}35 rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']36 /// Sequence of digits37 rule uint() -> u64 = a:$(digit()+) { a.parse().unwrap() }38 /// Number in scientific notation format39 rule number() -> f64 = quiet!{a:$(uint() ("." uint())? (['e'|'E'] (s:['+'|'-'])? uint())?) { a.parse().unwrap() }} / expected!("<number>")4041 /// Reserved word followed by any non-alphanumberic42 rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()43 rule id() -> String = quiet!{ !reserved() s:$(alpha() (alpha() / digit())*) {s.to_owned()}} / expected!("<identifier>")4445 rule keyword(id: &'static str)46 = ##parse_string_literal(id) end_of_ident()47 // Adds location data information to existing expression48 rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr49 = start:position!() v:x() end:position!() {loc_expr!(v, s.loc_data, (s.file_name.clone(), start, end))}5051 pub rule param(s: &ParserSettings) -> expr::Param = name:id() expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr) }52 pub rule params(s: &ParserSettings) -> expr::ParamsDesc53 = params:(param(s) ** comma()) {54 let mut defaults_started = false;55 for param in ¶ms {56 defaults_started = defaults_started || param.1.is_some();57 assert_eq!(defaults_started, param.1.is_some(), "defauld parameters should be used after all positionals");58 }59 expr::ParamsDesc(params)60 }61 / { expr::ParamsDesc(Vec::new()) }6263 pub rule arg(s: &ParserSettings) -> expr::Arg64 = name:id() _ "=" _ expr:expr(s) {expr::Arg(Some(name), expr)}65 / expr:expr(s) {expr::Arg(None, expr)}66 pub rule args(s: &ParserSettings) -> expr::ArgsDesc67 = args:arg(s) ** comma() comma()? {68 let mut named_started = false;69 for arg in &args {70 named_started = named_started || arg.0.is_some();71 assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");72 }73 expr::ArgsDesc(args)74 }75 / { expr::ArgsDesc(Vec::new()) }7677 pub rule bind(s: &ParserSettings) -> expr::BindSpec78 = name:id() _ "=" _ expr:expr(s) {expr::BindSpec{name, params: None, value: expr}}79 / name:id() _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name, params: Some(params), value: expr}}80 pub rule assertion(s: &ParserSettings) -> expr::AssertStmt81 = keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }8283 pub rule whole_line() -> String84 = str:$((!['\n'][_])* "\n") {str.to_owned()}85 pub rule string() -> String86 = "\"" str:$(("\\\"" / !['"'][_])*) "\"" {str.to_owned()}87 / "'" str:$((!['\''][_])*) "'" {str.to_owned()}88 // TODO: This is temporary workaround, i still dont know how to write this correctly btw.89 / "|||" "\n" str:$((" "*<1, 1> whole_line())+) " "*<0, 0> "|||" {deent(str)}90 / "|||" "\n" str:$((" "*<2, 2> whole_line())+) " "*<1, 1> "|||" {deent(str)}91 / "|||" "\n" str:$((" "*<3, 3> whole_line())+) " "*<2, 2> "|||" {deent(str)}92 / "|||" "\n" str:$((" "*<4, 4> whole_line())+) " "*<3, 3> "|||" {deent(str)}93 / "|||" "\n" str:$((" "*<5, 5> whole_line())+) " "*<4, 4> "|||" {deent(str)}94 / "|||" "\n" str:$((" "*<6, 6> whole_line())+) " "*<5, 5> "|||" {deent(str)}95 / "|||" "\n" str:$((" "*<7, 7> whole_line())+) " "*<6, 6> "|||" {deent(str)}96 / "|||" "\n" str:$((" "*<8, 8> whole_line())+) " "*<7, 7> "|||" {deent(str)}97 / "|||" "\n" str:$((" "*<9, 9> whole_line())+) " "*<8, 8> "|||" {deent(str)}98 / "|||" "\n" str:$((" "*<10, 10> whole_line())+) " "*<9, 9> "|||" {deent(str)}99 / "|||" "\n" str:$((" "*<11, 11> whole_line())+) " "*<10, 10> "|||" {deent(str)}100 / "|||" "\n" str:$((" "*<12, 12> whole_line())+) " "*<11, 10> "|||" {deent(str)}101102 pub rule field_name(s: &ParserSettings) -> expr::FieldName103 = name:id() {expr::FieldName::Fixed(name)}104 / name:string() {expr::FieldName::Fixed(name)}105 / "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}106 pub rule visibility() -> expr::Visibility107 = ":::" {expr::Visibility::Unhide}108 / "::" {expr::Visibility::Hidden}109 / ":" {expr::Visibility::Normal}110 pub rule field(s: &ParserSettings) -> expr::FieldMember111 = name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{112 name,113 plus: plus.is_some(),114 params: None,115 visibility,116 value,117 }}118 / name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{119 name,120 plus: false,121 params: Some(params),122 visibility,123 value,124 }}125 pub rule obj_local(s: &ParserSettings) -> BindSpec126 = keyword("local") _ bind:bind(s) {bind}127 pub rule member(s: &ParserSettings) -> expr::Member128 = bind:obj_local(s) {expr::Member::BindStmt(bind)}129 / assertion:assertion(s) {expr::Member::AssertStmt(assertion)}130 / field:field(s) {expr::Member::Field(field)}131 pub rule objinside(s: &ParserSettings) -> expr::ObjBody132 = 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})? {133 expr::ObjBody::ObjComp {134 pre_locals,135 key,136 value,137 post_locals,138 rest: [vec![CompSpec::ForSpec(forspec)], others.unwrap_or_default()].concat(),139 }140 }141 / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}142 pub rule ifspec(s: &ParserSettings) -> IfSpecData143 = keyword("if") _ expr:expr(s) {IfSpecData(expr)}144 pub rule forspec(s: &ParserSettings) -> ForSpecData145 = keyword("for") _ id:id() _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}146 pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>147 = s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}148 pub rule local_expr(s: &ParserSettings) -> LocExpr149 = l(s,<keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }>)150 pub rule string_expr(s: &ParserSettings) -> LocExpr151 = l(s, <s:string() {Expr::Str(s)}>)152 pub rule obj_expr(s: &ParserSettings) -> LocExpr153 = l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)154 pub rule array_expr(s: &ParserSettings) -> LocExpr155 = l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)156 pub rule array_comp_expr(s: &ParserSettings) -> LocExpr157 = 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())}>)158 pub rule number_expr(s: &ParserSettings) -> LocExpr159 = l(s,<n:number() { expr::Expr::Num(n) }>)160 pub rule var_expr(s: &ParserSettings) -> LocExpr161 = l(s,<n:id() { expr::Expr::Var(n) }>)162 pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr163 = l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{164 cond,165 cond_then,166 cond_else,167 }}>)168169 pub rule literal(s: &ParserSettings) -> LocExpr170 = l(s,<v:(171 keyword("null") {LiteralType::Null}172 / keyword("true") {LiteralType::True}173 / keyword("false") {LiteralType::False}174 / keyword("self") {LiteralType::This}175 / keyword("$") {LiteralType::Dollar}176 / keyword("super") {LiteralType::Super}177 ) {Expr::Literal(v)}>)178179 pub rule expr_basic(s: &ParserSettings) -> LocExpr180 = literal(s)181182 / string_expr(s) / number_expr(s)183 / array_expr(s)184 / obj_expr(s)185 / array_expr(s)186 / array_comp_expr(s)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::Error(expr) }>)196197 pub rule slice_desc(s: &ParserSettings) -> SliceDesc198 = start:expr(s)? _ ":" _ pair:(end:expr(s)? _ step:(":" _ e:expr(s) {e})? {(end, step)})? {199 if let Some((end, step)) = pair {200 SliceDesc { start, end, step }201 }else{202 SliceDesc { start, end: None, step: None }203 }204 }205206 rule expr(s: &ParserSettings) -> LocExpr207 = start:position!() a:precedence! {208 a:(@) _ "||" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}209 --210 a:(@) _ "&&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}211 --212 a:(@) _ "|" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}213 --214 a:@ _ "^" _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}215 --216 a:(@) _ "&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}217 --218 a:(@) _ "==" _ b:@ {loc_expr_todo!(Expr::Apply(219 el!(Expr::Index(220 el!(Expr::Var("std".to_owned())),221 el!(Expr::Str("equals".to_owned()))222 )),223 ArgsDesc(vec![Arg(None, a), Arg(None, b)]),224 true225 ))}226 a:(@) _ "!=" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, el!(Expr::Apply(227 el!(Expr::Index(228 el!(Expr::Var("std".to_owned())),229 el!(Expr::Str("equals".to_owned()))230 )),231 ArgsDesc(vec![Arg(None, a), Arg(None, b)]),232 true233 ))))}234 --235 a:(@) _ "<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}236 a:(@) _ ">" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}237 a:(@) _ "<=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}238 a:(@) _ ">=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}239 --240 a:(@) _ "<<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}241 a:(@) _ ">>" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}242 --243 a:(@) _ "+" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}244 a:(@) _ "-" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}245 --246 a:(@) _ "*" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}247 a:(@) _ "/" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}248 a:(@) _ "%" _ b:@ {loc_expr_todo!(Expr::Apply(249 el!(Expr::Index(250 el!(Expr::Var("std".to_owned())),251 el!(Expr::Str("mod".to_owned()))252 )), ArgsDesc(vec![Arg(None, a), Arg(None, b)]),253 true254 ))}255 --256 "-" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}257 "!" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}258 "~" _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }259 --260 a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Slice(a, s))}261 a:(@) _ "." _ s:id() {loc_expr_todo!(Expr::Index(a, el!(Expr::Str(s))))}262 a:(@) _ "[" _ s:expr(s) _ "]" {loc_expr_todo!(Expr::Index(a, s))}263 a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {loc_expr_todo!(Expr::Apply(a, args, ts.is_some()))}264 a:(@) _ "{" _ body:objinside(s) _ "}" {loc_expr_todo!(Expr::ObjExtend(a, body))}265 --266 e:expr_basic(s) {e}267 "(" _ e:expr(s) _ ")" {loc_expr_todo!(Expr::Parened(e))}268 } end:position!() {269 let LocExpr(e, _) = a;270 LocExpr(e, if s.loc_data {271 Some(Rc::new(ExprLocation(s.file_name.to_owned(), start, end)))272 } else {273 None274 })275 }276 / e:expr_basic(s) {e}277278 pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}279 }280}281282pub type ParseError = peg::error::ParseError<peg::str::LineCol>;283pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {284 jsonnet_parser::jsonnet(str, settings)285}286287#[macro_export]288macro_rules! el {289 ($expr:expr) => {290 LocExpr(std::rc::Rc::new($expr), None)291 };292}293294#[cfg(test)]295pub mod tests {296 use super::{expr::*, parse};297 use crate::ParserSettings;298 use std::path::PathBuf;299300 macro_rules! parse {301 ($s:expr) => {302 parse(303 $s,304 &ParserSettings {305 loc_data: false,306 file_name: PathBuf::from("/test.jsonnet"),307 },308 )309 .unwrap()310 };311 }312313 mod expressions {314 use super::*;315316 pub fn basic_math() -> LocExpr {317 el!(Expr::BinaryOp(318 el!(Expr::Num(2.0)),319 BinaryOpType::Add,320 el!(Expr::BinaryOp(321 el!(Expr::Num(2.0)),322 BinaryOpType::Mul,323 el!(Expr::Num(2.0)),324 )),325 ))326 }327 }328329 #[test]330 fn multiline_string() {331 assert_eq!(332 parse!("|||\n Hello world!\n a\n|||"),333 el!(Expr::Str(" Hello world!\na\n".to_owned())),334 )335 }336337 #[test]338 fn empty_object() {339 assert_eq!(parse!("{}"), el!(Expr::Obj(ObjBody::MemberList(vec![]))));340 }341342 #[test]343 fn basic_math() {344 assert_eq!(345 parse!("2+2*2"),346 el!(Expr::BinaryOp(347 el!(Expr::Num(2.0)),348 BinaryOpType::Add,349 el!(Expr::BinaryOp(350 el!(Expr::Num(2.0)),351 BinaryOpType::Mul,352 el!(Expr::Num(2.0))353 ))354 ))355 );356 }357358 #[test]359 fn basic_math_with_indents() {360 assert_eq!(parse!("2 + 2 * 2 "), expressions::basic_math());361 }362363 #[test]364 fn basic_math_parened() {365 assert_eq!(366 parse!("2+(2+2*2)"),367 el!(Expr::BinaryOp(368 el!(Expr::Num(2.0)),369 BinaryOpType::Add,370 el!(Expr::Parened(expressions::basic_math())),371 ))372 );373 }374375 /// Comments should not affect parsing376 #[test]377 fn comments() {378 assert_eq!(379 parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),380 el!(Expr::BinaryOp(381 el!(Expr::Num(2.0)),382 BinaryOpType::Add,383 el!(Expr::BinaryOp(384 el!(Expr::Num(3.0)),385 BinaryOpType::Mul,386 el!(Expr::Num(4.0))387 ))388 ))389 );390 }391392 /// Comments should be able to be escaped393 #[test]394 fn comment_escaping() {395 assert_eq!(396 parse!("2/*\\*/+*/ - 22"),397 el!(Expr::BinaryOp(398 el!(Expr::Num(2.0)),399 BinaryOpType::Sub,400 el!(Expr::Num(22.0))401 ))402 );403 }404405 #[test]406 fn suffix() {407 // assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));408 // assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));409 // assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));410 // assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))411 }412413 #[test]414 fn array_comp() {415 use Expr::*;416 assert_eq!(417 parse!("[std.deepJoin(x) for x in arr]"),418 el!(ArrComp(419 el!(Apply(420 el!(Index(421 el!(Var("std".to_owned())),422 el!(Str("deepJoin".to_owned()))423 )),424 ArgsDesc(vec![Arg(None, el!(Var("x".to_owned())))]),425 false,426 )),427 vec![CompSpec::ForSpec(ForSpecData(428 "x".to_owned(),429 el!(Var("arr".to_owned()))430 ))]431 )),432 )433 }434435 #[test]436 fn reserved() {437 use Expr::*;438 assert_eq!(parse!("null"), el!(Literal(LiteralType::Null)));439 assert_eq!(parse!("nulla"), el!(Var("nulla".to_owned())));440 }441442 #[test]443 fn multiple_args_buf() {444 parse!("a(b, null_fields)");445 }446447 #[test]448 fn infix_precedence() {449 use Expr::*;450 assert_eq!(451 parse!("!a && !b"),452 el!(BinaryOp(453 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned())))),454 BinaryOpType::And,455 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".to_owned()))))456 ))457 );458 }459460 #[test]461 fn infix_precedence_division() {462 use Expr::*;463 assert_eq!(464 parse!("!a / !b"),465 el!(BinaryOp(466 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned())))),467 BinaryOpType::Div,468 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".to_owned()))))469 ))470 );471 }472473 #[test]474 fn double_negation() {475 use Expr::*;476 assert_eq!(477 parse!("!!a"),478 el!(UnaryOp(479 UnaryOpType::Not,480 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned()))))481 ))482 )483 }484485 #[test]486 fn array_test_error() {487 parse!("[a for a in b if c for e in f]");488 // ^^^^ failed code489 }490491 #[test]492 fn can_parse_stdlib() {493 parse!(jsonnet_stdlib::STDLIB_STR);494 }495496 use test::Bencher;497498 // From source code499 #[bench]500 fn bench_parse_peg(b: &mut Bencher) {501 b.iter(|| parse!(jsonnet_stdlib::STDLIB_STR))502 }503504 // From serialized blob505 #[bench]506 fn bench_parse_serde_bincode(b: &mut Bencher) {507 let serialized = bincode::serialize(&parse!(jsonnet_stdlib::STDLIB_STR)).unwrap();508 b.iter(|| bincode::deserialize::<LocExpr>(&serialized))509 }510}crates/jsonnet-parser/src/string_processing.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jsonnet-parser/src/string_processing.rs
@@ -0,0 +1,29 @@
+/// Returns string with stripped line padding characters
+pub fn deent(input: &str) -> String {
+ if input.is_empty() {
+ return "".to_owned();
+ }
+ let min_ident = input
+ .split('\n')
+ .filter(|s| !s.is_empty())
+ .map(|ss| ss.chars().take_while(|c| *c == ' ').count())
+ .min()
+ .unwrap();
+ input
+ .split('\n')
+ .map(|s| s.chars().skip(min_ident).collect::<String>())
+ .collect::<Vec<String>>()
+ .join("\n")
+}
+
+#[cfg(test)]
+pub mod tests {
+ use super::*;
+ #[test]
+ fn deent_tests() {
+ assert_eq!(deent(" aaa"), "aaa");
+ assert_eq!(deent(" aaa\n bbb"), " aaa\nbbb");
+ assert_eq!(deent(" aaa\n bbb"), "aaa\n bbb");
+ assert_eq!(deent(" aaa\n\n bbb"), "aaa\n\n bbb");
+ }
+}