Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions parser/c2_parser.c2
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,6 @@ fn Kind Parser.peekToken2(Parser* p, u32 n, Token* tok) {
fn void Parser.consumeSemicolon(Parser* p, bool need_semi) {
if (need_semi) {
p.expectAndConsume(Semicolon);
} else {
if (p.tok.kind == Semicolon) {
p.diags.error(p.tok.loc, "no semicolon allowed after initializer list");
p.consumeToken();
}
}
}

Expand All @@ -242,6 +237,14 @@ fn void Parser.expectAndConsume(Parser* p, Kind kind) {
p.expectError(kind);
}

fn void Parser.consumeRBrace(Parser* p, const char* context) {
p.expectAndConsume(RBrace);
if (p.tok.kind == Semicolon) {
p.diags.warn(p.tok.loc, "no semicolon allowed after %s", context);
p.consumeToken();
}
}

fn void Parser.expect(Parser* p, Kind kind) {
if (p.tok.kind == kind) return;

Expand Down Expand Up @@ -377,7 +380,7 @@ fn void Parser.parseImports(Parser* p) {
p.consumeToken();
}
}
p.expectAndConsume(RBrace);
p.consumeRBrace("import list");
} else {
p.expectAndConsume(Semicolon);
}
Expand Down Expand Up @@ -1103,7 +1106,7 @@ fn void Parser.parseExternDecl(Parser* p) {
p.parseTopLevel();
}
p.tokenizer.c_mode--; // must set before consuming the token
p.consumeToken();
p.consumeRBrace("extern \"C\" block");
} else {
p.parseTopLevel();
}
Expand Down
2 changes: 1 addition & 1 deletion parser/c2_parser_expr.c2
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ fn Expr* Parser.parseInitList(Parser* p) {
if (p.tok.kind == LBrace) p.expect(Comma); // always gives error

SrcLoc endLoc = p.tok.loc;
p.expectAndConsume(RBrace);
p.consumeRBrace("initializer list");
Expr* e = (Expr*)p.builder.actOnInitList(loc, endLoc, values.getExprs(), values.size());
values.free();
return e;
Expand Down
2 changes: 1 addition & 1 deletion parser/c2_parser_stmt.c2
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ fn CompoundStmt* Parser.parseCompoundStmt(Parser* p) {
}

SrcLoc endLoc = p.tok.loc + 1;
p.expectAndConsume(RBrace);
p.consumeRBrace("block");

CompoundStmt* s = p.builder.actOnCompoundStmt(endLoc, stmts.getData(), stmts.size());
p.putStmtList();
Expand Down
2 changes: 1 addition & 1 deletion parser/c2_parser_switch.c2
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn Stmt* Parser.parseSwitchStmt(Parser* p) {
cases.add(c);
}

p.expectAndConsume(RBrace);
p.consumeRBrace("switch block");

Stmt* s = p.builder.actOnSwitchStmt(loc, decl, cond, cases.getData(), cases.size(), has_default);
cases.free();
Expand Down
4 changes: 2 additions & 2 deletions parser/c2_parser_type.c2
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ fn void Parser.parseStructBlock(Parser* p, DeclList* members, bool is_public) {
p.expectAndConsume(Semicolon);
}

p.expectAndConsume(RBrace);
p.consumeRBrace("struct definition");
}

fn void Parser.parseEnumType(Parser* p, u32 name, SrcLoc loc, bool is_public) {
Expand Down Expand Up @@ -334,7 +334,7 @@ fn void Parser.parseEnumType(Parser* p, u32 name, SrcLoc loc, bool is_public) {
}
}

p.expectAndConsume(RBrace);
p.consumeRBrace("enum definition");

EnumTypeDecl* d = p.builder.actOnEnumType(name,
loc,
Expand Down
6 changes: 3 additions & 3 deletions test/parser/invalid_initlist.c2
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// @warnings{no-unused}
module test;

u32[] a1 = {1, 2, 3}; // @error{no semicolon allowed after initializer list}
u32[] a1 = {1, 2, 3}; // @warning{no semicolon allowed after initializer list}

u32[+] b;
a += {1, 2, 3}; // @error{no semicolon allowed after initializer list}
a += {1, 2, 3}; // @warning{no semicolon allowed after initializer list}

fn void fn1() {
u32[] a = {1, 2, 3}; // @error{no semicolon allowed after initializer list}
u32[] a = {1, 2, 3}; // @warning{no semicolon allowed after initializer list}
}

public fn i32 main() {
Expand Down