diff --git a/schala-lang/language/src/parsing/new.rs b/schala-lang/language/src/parsing/new.rs index bb89645..b2ebf78 100644 --- a/schala-lang/language/src/parsing/new.rs +++ b/schala-lang/language/src/parsing/new.rs @@ -15,12 +15,14 @@ enum ExtendedPart<'a> { peg::parser! { pub grammar schala_parser() for str { - rule whitespace() = [' ' | '\t' ]* - rule whitespace_or_newline() = [' ' | '\t' | '\n' ]* + rule whitespace() = [' ' | '\t' ] + rule whitespace_or_newline() = [' ' | '\t' | '\n' ] - rule _ = quiet!{ whitespace() } + rule _ = quiet!{ (block_comment() / whitespace())* } - rule __ = quiet!{ whitespace_or_newline() } + rule __ = quiet!{ (block_comment() / whitespace_or_newline())* } + + rule block_comment() = "/*" (block_comment() / !"*/" [_])* "*/" pub rule program() -> AST = __ statements:(statement() ** delimiter() ) __ { AST { id: Default::default(), statements: statements.into() } } @@ -210,7 +212,7 @@ peg::parser! { //TODO make the definition of operators more complex rule operator() -> &'input str = - quiet!{$( ['+' | '-' | '*' | '/' | '%' | '<' | '>' | '=' | '!' | '$' | '&' | '|' | '?' | '^' | '`']+ )} / + quiet!{!"*/" s:$( ['+' | '-' | '*' | '/' | '%' | '<' | '>' | '=' | '!' | '$' | '&' | '|' | '?' | '^' | '`']+ ) { s } } / expected!("operator") rule extended_expr(struct_ok: bool) -> ExpressionKind = diff --git a/schala-lang/language/src/parsing/test.rs b/schala-lang/language/src/parsing/test.rs index 3a46264..7950b64 100644 --- a/schala-lang/language/src/parsing/test.rs +++ b/schala-lang/language/src/parsing/test.rs @@ -1291,3 +1291,18 @@ fn blocks() { assert_eq!(block.unwrap(), vec![exst(Value(qn!(a)))].into()); } } + +#[test] +fn comments() { + use ExpressionKind::*; + + let source = "1 + /* hella /* bro */ */ 2"; + assert_expr2!(source, binop("+", expr(NatLiteral(1)), expr(NatLiteral(2)))); + + //TODO make sure this error message makes sense + let source = "1 + /* hella /* bro */ 2"; + assert_fail_expr2!(source, "foo"); + + let source = "1 + /* hella */ bro */ 2"; + assert_fail_expr2!(source, binop("+", expr(NatLiteral(1)), expr(NatLiteral(2)))); +}