From 02fc76c8fc447dc728f046f8c6e0c3240e17b47f Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sat, 6 Nov 2021 20:33:33 -0700 Subject: [PATCH] Fix issue with block formatting --- schala-lang/language/src/parsing/new.rs | 8 +++++++- schala-lang/language/src/parsing/test.rs | 16 +++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/schala-lang/language/src/parsing/new.rs b/schala-lang/language/src/parsing/new.rs index 32bf1e4..bc05a26 100644 --- a/schala-lang/language/src/parsing/new.rs +++ b/schala-lang/language/src/parsing/new.rs @@ -19,7 +19,13 @@ peg::parser! { id: Default::default(), location: Default::default(), kind: StatementKind::Expression(expr) } } - rule block() -> Block = "{" _ items:(statement() ** delimiter()) _ "}" { items.into() } + //Note - this is a hack, ideally the rule `rule block() -> Block = "{" _ items:(statement() ** + //delimiter()) _ "}" { items.into() }` would've worked, but it doesn't. + pub rule block() -> Block = "{" _ items:block_item()* _ "}" { items.into() } / + "{" _ stmt:statement() _ "}" { vec![stmt].into() } + + rule block_item() -> Statement = + stmt:statement() delimiter()+ { stmt } pub rule expression() -> Expression = _ kind:expression_kind() { Expression { id: Default::default(), type_anno: None, kind: kind } } diff --git a/schala-lang/language/src/parsing/test.rs b/schala-lang/language/src/parsing/test.rs index 8299367..ec9593f 100644 --- a/schala-lang/language/src/parsing/test.rs +++ b/schala-lang/language/src/parsing/test.rs @@ -151,7 +151,6 @@ macro_rules! assert_fail_expr2 { //assert_eq!(err.to_string(), $failure); }; } - #[test] fn basic_literals() { use ExpressionKind::*; @@ -388,8 +387,7 @@ fn for_expression() { ); assert_expr2!( - //TODO make "for n <- someRange { f(n); }" work!! - "for n <- someRange { f(n) }", + "for n <- someRange { f(n) ; }", expr(ForExpression { enumerators: vec![Enumerator { id: rc("n"), generator: expr(Value(qn!(someRange))) }], body: bx(ForBody::StatementBlock( @@ -1295,3 +1293,15 @@ fn flow_control() { )] ); } + +#[test] +fn blocks() { + use ExpressionKind::*; + + let cases = ["{ a }", "{ a; }", "{a}", "{ a\n }", "{ a\n\n }", "{ a;\n\n; }"]; + + for case in cases.iter() { + let block = schala_parser::block(case); + assert_eq!(block.unwrap(), vec![exst(Value(qn!(a)))].into()); + } +}