Skip to content

Commit 452c7da

Browse files
gh-168: Reject multiple statements on one logical line.
1 parent 1e2aa64 commit 452c7da

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

src/parser.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,24 @@ Stmt* parser_parse(Parser* parser) {
10421042
while (parser->current_token.type != TOKEN_EOF) {
10431043
Stmt* stmt = parse_statement(parser);
10441044
if (stmt) {
1045+
/* Enforce specification: top-level expression statements and
1046+
* assignments must occupy their own logical line. If we parsed
1047+
* such a statement but the next token is not a logical newline
1048+
* (TOKEN_NEWLINE) or EOF, report a parse error and synthesize
1049+
* a THROW so runtime TRY/CATCH can observe it. Then
1050+
* synchronize to the next logical newline. */
1051+
if ((stmt->type == STMT_EXPR || stmt->type == STMT_ASSIGN) &&
1052+
parser->current_token.type != TOKEN_NEWLINE &&
1053+
parser->current_token.type != TOKEN_EOF) {
1054+
report_error(parser, "Expression statement or assignment must occupy its own logical line");
1055+
append_parse_error_throw_stmt(parser, program);
1056+
while (parser->current_token.type != TOKEN_EOF && parser->current_token.type != TOKEN_NEWLINE) {
1057+
advance(parser);
1058+
}
1059+
skip_newlines(parser);
1060+
continue;
1061+
}
1062+
10451063
char* line_text = lexer_get_line(parser->lexer, stmt->line);
10461064
stmt_set_src(stmt, line_text);
10471065
free(line_text);

0 commit comments

Comments
 (0)