From 96d12f3659311b2935c477d8247e0afe964ca010 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Thu, 11 Nov 2021 22:24:35 -0800 Subject: [PATCH] Take care of all expression tests --- schala-lang/language/src/parsing/new.rs | 9 ++++----- schala-lang/language/src/parsing/test.rs | 15 ++++----------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/schala-lang/language/src/parsing/new.rs b/schala-lang/language/src/parsing/new.rs index b8dd5f4..5ee24a0 100644 --- a/schala-lang/language/src/parsing/new.rs +++ b/schala-lang/language/src/parsing/new.rs @@ -135,10 +135,10 @@ peg::parser! { ":" _ identifier:type_identifier() { identifier } pub rule expression() -> Expression = - _ kind:expression_kind() _ type_anno:type_anno()? { Expression { id: Default::default(), type_anno, kind: kind } } + __ kind:expression_kind() _ type_anno:type_anno()? { Expression { id: Default::default(), type_anno, kind } } rule expression_no_struct() -> Expression = - _ kind:expression_kind_no_struct() { Expression { id: Default::default(), type_anno: None, kind: kind } } + __ kind:expression_kind_no_struct() { Expression { id: Default::default(), type_anno: None, kind: kind } } rule expression_kind() -> ExpressionKind = precedence_expr(true) @@ -333,7 +333,7 @@ peg::parser! { } rule cond_block() -> IfExpressionBody = - "{" _ cond_arms:(cond_arm() ++ ",") _ "}" { IfExpressionBody::CondList(cond_arms) } + "{" __ cond_arms:(cond_arm() ++ (delimiter()+)) __ "}" { IfExpressionBody::CondList(cond_arms) } rule cond_arm() -> ConditionArm = _ "else" _ body:expr_or_block() { ConditionArm { condition: Condition::Else, guard: None, body } } / @@ -347,7 +347,6 @@ peg::parser! { rule condition_guard() -> Option = ("if" _ expr:expression() { expr } )? - rule expr_or_block() -> Block = block() / ex:expression() { Statement { id: Default::default(), location: Default::default(), @@ -388,7 +387,7 @@ peg::parser! { "true" { Pattern::Literal(PatternLiteral::BoolPattern(true)) } / "false" { Pattern::Literal(PatternLiteral::BoolPattern(false)) } / s:bare_string_literal() { Pattern::Literal(PatternLiteral::StringPattern(Rc::new(s.to_string()))) } / - sign:("-"?) num:nat_literal() { + sign:("-"?) num:(float_literal() / nat_literal()) { let neg = sign.is_some(); Pattern::Literal(PatternLiteral::NumPattern { neg, num }) } / diff --git a/schala-lang/language/src/parsing/test.rs b/schala-lang/language/src/parsing/test.rs index a219830..f6594e1 100644 --- a/schala-lang/language/src/parsing/test.rs +++ b/schala-lang/language/src/parsing/test.rs @@ -133,13 +133,6 @@ macro_rules! assert_fail2 { }; } -macro_rules! assert_expr { - ($input:expr, $correct:expr) => { - let mut parser = make_parser($input); - assert_eq!(parser.expression().unwrap(), $correct); - }; -} - macro_rules! assert_expr2 { ($input:expr, $correct:expr) => { let expr = schala_parser::expression($input); @@ -1103,7 +1096,7 @@ fn if_exprs() { }) ); - assert_expr!( + assert_expr2!( r#" if true then { let a = 10 @@ -1188,7 +1181,7 @@ fn pattern_matching() { ); assert_expr2!( - "if x { is 1 then 5, else 20 }", + "if x { is 1 then 5; else 20 }", expr(IfExpression { discriminator: Some(bx(expr(Value(qn!(x))))), body: bx(IfExpressionBody::CondList(vec![ @@ -1221,10 +1214,10 @@ fn pattern_matching() { }) ); - assert_expr! { + assert_expr2! { r#" if (45, "panda", false, 2.2) { - is (49, "pablo", _, 28.4) then "no" + is (49, "pablo", _, 28.4) then "no" is (_, "panda", _, -2.2) then "yes" is _ then "maybe" }"#,