Skip to content

Commit 743900a

Browse files
gh-83: Require parentheses for CONTINUE.
1 parent c75c626 commit 743900a

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/parser.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,9 +820,33 @@ static Stmt* parse_statement(Parser* parser) {
820820
case TOKEN_CONTINUE: {
821821
Token tok = parser->current_token;
822822
advance(parser);
823-
if (match(parser, TOKEN_LPAREN)) {
824-
consume(parser, TOKEN_RPAREN, "Expected ')' after CONTINUE");
823+
if (!consume(parser, TOKEN_LPAREN, "Expected '(' after CONTINUE")) {
824+
return stmt_continue(tok.line, tok.column);
825825
}
826+
827+
/* If the next token is a real expression-start token, report
828+
* that CONTINUE does not accept arguments and skip until the
829+
* closing ')'. If the token is something like '}' or EOF,
830+
* prefer the missing-')' message so the user sees the
831+
* syntactically relevant error.
832+
*/
833+
PTokenType t = parser->current_token.type;
834+
bool looks_like_expr = (t == TOKEN_NUMBER || t == TOKEN_FLOAT || t == TOKEN_STRING ||
835+
t == TOKEN_IDENT || t == TOKEN_AT || t == TOKEN_LPAREN ||
836+
t == TOKEN_LBRACKET || t == TOKEN_LANGLE || t == TOKEN_ASYNC ||
837+
t == TOKEN_LAMBDA || t == TOKEN_DASH);
838+
839+
if (looks_like_expr) {
840+
report_error(parser, "CONTINUE does not accept arguments");
841+
while (parser->current_token.type != TOKEN_RPAREN && parser->current_token.type != TOKEN_EOF) {
842+
advance(parser);
843+
}
844+
}
845+
846+
/* Require the closing paren; this will produce the standard
847+
* "Expected ')' after CONTINUE" message when appropriate.
848+
*/
849+
consume(parser, TOKEN_RPAREN, "Expected ')' after CONTINUE");
826850
return stmt_continue(tok.line, tok.column);
827851
}
828852
case TOKEN_GOTO: {

0 commit comments

Comments
 (0)