Skip to content

Commit

Permalink
[ruby/prism] Better error message on statement inside argument list
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton authored and matzbot committed Apr 12, 2024
1 parent 10d0abb commit 3c07567
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
9 changes: 7 additions & 2 deletions prism/prism.c
Expand Up @@ -14335,9 +14335,14 @@ parse_arguments_list(pm_parser_t *parser, pm_arguments_t *arguments, bool accept
} else {
pm_accepts_block_stack_push(parser, true);
parse_arguments(parser, arguments, true, PM_TOKEN_PARENTHESIS_RIGHT);
expect1(parser, PM_TOKEN_PARENTHESIS_RIGHT, PM_ERR_ARGUMENT_TERM_PAREN);
pm_accepts_block_stack_pop(parser);

if (!accept1(parser, PM_TOKEN_PARENTHESIS_RIGHT)) {
PM_PARSER_ERR_TOKEN_FORMAT(parser, parser->current, PM_ERR_ARGUMENT_TERM_PAREN, pm_token_type_human(parser->current.type));
parser->previous.start = parser->previous.end;
parser->previous.type = PM_TOKEN_MISSING;
}

pm_accepts_block_stack_pop(parser);
arguments->closing_loc = PM_LOCATION_TOKEN_VALUE(&parser->previous);
}
} else if (accepts_command_call && (token_begins_expression_p(parser->current.type) || match3(parser, PM_TOKEN_USTAR, PM_TOKEN_USTAR_STAR, PM_TOKEN_UAMPERSAND)) && !match1(parser, PM_TOKEN_BRACE_LEFT)) {
Expand Down
2 changes: 1 addition & 1 deletion prism/templates/src/diagnostic.c.erb
Expand Up @@ -105,7 +105,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
[PM_ERR_ARGUMENT_NO_FORWARDING_STAR_STAR] = { "unexpected `**`; no anonymous keyword rest parameter", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_ARGUMENT_SPLAT_AFTER_ASSOC_SPLAT] = { "unexpected `*` splat argument after a `**` keyword splat argument", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_ARGUMENT_SPLAT_AFTER_SPLAT] = { "unexpected `*` splat argument after a `*` splat argument", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_ARGUMENT_TERM_PAREN] = { "expected a `)` to close the arguments", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_ARGUMENT_TERM_PAREN] = { "unexpected %s; expected a `)` to close the arguments", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_ARGUMENT_UNEXPECTED_BLOCK] = { "unexpected `{` after a method call without parenthesis", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_ARRAY_ELEMENT] = { "expected an element for the array", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_ARRAY_EXPRESSION] = { "expected an expression for the array element", PM_ERROR_LEVEL_SYNTAX },
Expand Down
2 changes: 1 addition & 1 deletion prism/templates/src/token_type.c.erb
Expand Up @@ -208,7 +208,7 @@ pm_token_type_human(pm_token_type_t token_type) {
case PM_TOKEN_KEYWORD_RESCUE:
return "'rescue'";
case PM_TOKEN_KEYWORD_RESCUE_MODIFIER:
return "'rescue'";
return "'rescue' modifier";
case PM_TOKEN_KEYWORD_RETRY:
return "'retry'";
case PM_TOKEN_KEYWORD_RETURN:
Expand Down
2 changes: 1 addition & 1 deletion test/prism/errors_test.rb
Expand Up @@ -411,7 +411,7 @@ def test_arguments_after_block

def test_arguments_binding_power_for_and
assert_error_messages "foo(*bar and baz)", [
"expected a `)` to close the arguments",
"unexpected 'and'; expected a `)` to close the arguments",
"unexpected ')', expecting end-of-input",
"unexpected ')', ignoring it"
]
Expand Down

0 comments on commit 3c07567

Please sign in to comment.