Skip to content

Commit

Permalink
Merge pull request #3094 from ruby/for-loop-require-delimiter
Browse files Browse the repository at this point in the history
Require a for loop delimiter
  • Loading branch information
kddnewton committed Sep 30, 2024
2 parents 638e009 + 56830bd commit bc40656
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ errors:
- EXPECT_EXPRESSION_AFTER_SPLAT
- EXPECT_EXPRESSION_AFTER_SPLAT_HASH
- EXPECT_EXPRESSION_AFTER_STAR
- EXPECT_FOR_DELIMITER
- EXPECT_IDENT_REQ_PARAMETER
- EXPECT_IN_DELIMITER
- EXPECT_LPAREN_REQ_PARAMETER
Expand Down
5 changes: 3 additions & 2 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -19452,10 +19452,11 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
do_keyword = parser->previous;
} else {
do_keyword = not_provided(parser);
if (!match2(parser, PM_TOKEN_SEMICOLON, PM_TOKEN_NEWLINE)) {
PM_PARSER_ERR_TOKEN_FORMAT(parser, parser->current, PM_ERR_EXPECT_FOR_DELIMITER, pm_token_type_human(parser->current.type));
}
}

accept2(parser, PM_TOKEN_SEMICOLON, PM_TOKEN_NEWLINE);

pm_statements_node_t *statements = NULL;
if (!match1(parser, PM_TOKEN_KEYWORD_END)) {
statements = parse_statements(parser, PM_CONTEXT_FOR, (uint16_t) (depth + 1));
Expand Down
1 change: 1 addition & 0 deletions templates/src/diagnostic.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
[PM_ERR_EXPECT_EXPRESSION_AFTER_SPLAT] = { "expected an expression after `*` splat in an argument", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_EXPECT_EXPRESSION_AFTER_SPLAT_HASH] = { "expected an expression after `**` in a hash", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_EXPECT_EXPRESSION_AFTER_STAR] = { "expected an expression after `*`", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_EXPECT_FOR_DELIMITER] = { "unexpected %s; expected a 'do', newline, or ';' after the 'for' loop collection", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_EXPECT_IDENT_REQ_PARAMETER] = { "expected an identifier for the required parameter", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_EXPECT_IN_DELIMITER] = { "expected a delimiter after the patterns of an `in` clause", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_EXPECT_LPAREN_REQ_PARAMETER] = { "expected a `(` to start a required parameter", PM_ERROR_LEVEL_SYNTAX },
Expand Down
3 changes: 3 additions & 0 deletions test/prism/errors/for_loop_delimiter.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for a in b end
^~~ unexpected 'end'; expected a 'do', newline, or ';' after the 'for' loop collection

1 change: 1 addition & 0 deletions test/prism/errors/for_loops_only_end.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ for end
^~~ expected an index after `for`
^ expected an `in` after the index in a `for` statement
^ expected a collection after the `in` in a `for` statement
^~~ unexpected 'end'; expected a 'do', newline, or ';' after the 'for' loop collection

0 comments on commit bc40656

Please sign in to comment.