Fix test for reserved words error

This commit is contained in:
Greg Shuflin 2021-11-19 18:15:02 -08:00
parent bb26d9e674
commit ec5bf12a65
2 changed files with 26 additions and 23 deletions

View File

@ -116,14 +116,16 @@ pub fn program(input: Span) -> ParseResult<AST> {
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<InvocationArgument> {
fn primary_expr(allow_struct: bool) -> impl FnMut(Span) -> ParseResult<ExpressionKind> {
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<ExpressionKind> {
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<Span> {
}
fn identifier_span(input: Span) -> ParseResult<Span> {
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<ExpressionKind> {

View File

@ -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]