diff --git a/schala-lang/src/parsing/combinator.rs b/schala-lang/src/parsing/combinator.rs index 18bea8d..ebe36ab 100644 --- a/schala-lang/src/parsing/combinator.rs +++ b/schala-lang/src/parsing/combinator.rs @@ -116,14 +116,16 @@ pub fn program(input: Span) -> ParseResult { let (rest, statements) = context( "AST", terminated( - map( - tuple(( - many0(statement_delimiter), - separated_list0(many1(statement_delimiter), statement), - many0(statement_delimiter), - )), - |(_, items, _)| items.into(), - ), tok(eof)), + map( + tuple(( + many0(statement_delimiter), + separated_list0(many1(statement_delimiter), statement), + many0(statement_delimiter), + )), + |(_, items, _)| items.into(), + ), + tok(eof), + ), )(input)?; let ast = AST { id, statements }; @@ -528,9 +530,9 @@ fn invocation_argument(input: Span) -> ParseResult { fn primary_expr(allow_struct: bool) -> impl FnMut(Span) -> ParseResult { move |input: Span| { if allow_struct { - context("primary-expr", alt((named_struct, primary_expr_no_struct)))(input) + context("primary-expr", alt((primary_expr_no_struct, named_struct, identifier_expr)))(input) } else { - context("primary-expr", primary_expr_no_struct)(input) + context("primary-expr", alt((primary_expr_no_struct, identifier_expr)))(input) } } } @@ -545,11 +547,10 @@ fn primary_expr_no_struct(input: Span) -> ParseResult { lambda_expr, list_expr, paren_expr, - string_literal, + bool_literal, float_literal, number_literal, - bool_literal, - identifier_expr, + string_literal, )), )(input) } @@ -783,17 +784,16 @@ fn identifier(input: Span) -> ParseResult { } fn identifier_span(input: Span) -> ParseResult { - fn check(input: &Span) -> bool { - !is_keyword(input.fragment()) + let (rest, parsed) = recognize(tuple(( + alt((tag("_"), alpha1)), + take_while(|ch: char| is_alphanumeric(ch as u8) || ch == '_'), + )))(input.clone())?; + + if is_keyword(parsed.fragment()) { + return Err(nom::Err::Failure(VerboseError::from_error_kind(input, nom::error::ErrorKind::Verify))); } - verify( - recognize(tuple(( - alt((tag("_"), alpha1)), - take_while(|ch: char| is_alphanumeric(ch as u8) || ch == '_'), - ))), - check, - )(input) + Ok((rest, parsed)) } fn bool_literal(input: Span) -> ParseResult { diff --git a/schala-lang/src/parsing/test.rs b/schala-lang/src/parsing/test.rs index 36a775b..fc10b70 100644 --- a/schala-lang/src/parsing/test.rs +++ b/schala-lang/src/parsing/test.rs @@ -555,7 +555,10 @@ fn complex_lambdas() { #[test] fn reserved_words() { - assert_fail!("module::item::call()", "error at 1:7: expected ['a' ..= 'z' | 'A' ..= 'Z' | '_']"); + let err = "0: at line 1, in Verify:\nmodule::item::call()\n^\n\n1: at line 1, in tok:\nmodule::item::call()\n^\n\n2: at line 1, in tok:\nmodule::item::call()\n^\n\n3: at line 1, in primary-expr-no-struct:\nmodule::item::call()\n^\n\n4: at line 1, in primary-expr:\nmodule::item::call()\n^\n\n5: at line 1, in extended-expr:\nmodule::item::call()\n^\n\n6: at line 1, in prefix-expr:\nmodule::item::call()\n^\n\n7: at line 1, in expression-kind:\nmodule::item::call()\n^\n\n8: at line 1, in Parsing-statement:\nmodule::item::call()\n^\n\n9: at line 1, in AST:\nmodule::item::call()\n^\n\n"; + assert_fail_comb!("module::item::call()", err); + + assert_expr!("modulek::item", expr(ExpressionKind::Value(qn!(modulek, item)))); } #[test]