From a17ef2dd6ca796842824a7c9f337455a519d0156 Mon Sep 17 00:00:00 2001 From: Лач Date: Thu, 04 Jun 2020 16:36:57 +0000 Subject: [PATCH] fix(parser): infix operator precedence, ifcomp --- --- a/crates/jsonnet-parser/src/lib.rs +++ b/crates/jsonnet-parser/src/lib.rs @@ -134,7 +134,7 @@ pub rule forspec(s: &ParserSettings) -> ForSpecData = keyword("for") _ id:id() _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)} pub rule compspec(s: &ParserSettings) -> Vec - = s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} )+ {s} + = s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s} pub rule local_expr(s: &ParserSettings) -> LocExpr = l(s,) pub rule string_expr(s: &ParserSettings) -> LocExpr @@ -248,10 +248,11 @@ a:(@) _ "/" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))} a:(@) _ "%" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mod, b))} -- + "-" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))} + "!" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))} + "~" _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) } + -- e:expr_basic_with_suffix(s) {e} - "-" _ expr:expr(s) { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, expr)) } - "!" _ expr:expr(s) { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, expr)) } - "~" _ expr:expr(s) { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, expr)) } "(" _ e:expr(s) _ ")" {loc_expr_todo!(Expr::Parened(e))} } end:position!() { let LocExpr(e, _) = a; @@ -480,6 +481,19 @@ } #[test] + fn infix_precedence_division() { + use Expr::*; + assert_eq!( + parse!("!a / !b"), + el!(BinaryOp( + el!(UnaryOp(UnaryOpType::Not, el!(Var("a".to_owned())))), + BinaryOpType::Div, + el!(UnaryOp(UnaryOpType::Not, el!(Var("b".to_owned())))) + )) + ); + } + + #[test] fn double_negation() { use Expr::*; assert_eq!( @@ -492,6 +506,12 @@ } #[test] + fn array_test_error() { + parse!("[a for a in b if c for e in f]"); + // ^^^^ failed code + } + + #[test] fn can_parse_stdlib() { parse!(jsonnet_stdlib::STDLIB_STR); } -- gitstuff