parser: add block, break, grouped expression, array type tests

Port tests from upstream parser_test.zig:
- "allow empty line before comment at start of block"
- "comptime struct field"
- "break from block"
- "grouped expressions (parentheses)"
- "array types last token"

Fix bugs in parser.c:
- parseBreakLabel: use null_token instead of null_node
- test decl: use null_token for unnamed tests

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-02-10 12:28:14 +00:00
parent b186471328
commit a61bf83a11
2 changed files with 62 additions and 3 deletions

View File

@@ -982,8 +982,8 @@ static AstNodeIndex parseVarDeclProto(Parser* p) {
}
static AstTokenIndex parseBreakLabel(Parser* p) {
if (eatToken(p, TOKEN_COLON) == null_node)
return null_node;
if (eatToken(p, TOKEN_COLON) == null_token)
return null_token;
return expectToken(p, TOKEN_IDENTIFIER);
}
@@ -1609,7 +1609,7 @@ static Members parseContainerMembers(Parser* p) {
= (p->token_tags[p->tok_i] == TOKEN_STRING_LITERAL
|| p->token_tags[p->tok_i] == TOKEN_IDENTIFIER)
? nextToken(p)
: 0;
: null_token;
const AstNodeIndex body = parseBlock(p);
assert(body != 0);
const AstNodeIndex test_decl = addNode(&p->nodes,

View File

@@ -873,3 +873,62 @@ test "zig fmt: remove empty lines at start/end of container decl" {
\\
);
}
test "zig fmt: allow empty line before comment at start of block" {
try testCanonical(
\\test {
\\
\\ // foo
\\ const x = 42;
\\}
\\
);
}
test "zig fmt: comptime struct field" {
try testCanonical(
\\const Foo = struct {
\\ a: i32,
\\ comptime b: i32 = 1234,
\\};
\\
);
}
test "zig fmt: break from block" {
try testCanonical(
\\const a = blk: {
\\ break :blk 42;
\\};
\\const b = blk: {
\\ break :blk;
\\};
\\const c = {
\\ break 42;
\\};
\\const d = {
\\ break;
\\};
\\
);
}
test "zig fmt: grouped expressions (parentheses)" {
try testCanonical(
\\const r = (x + y) * (a + b);
\\
);
}
test "zig fmt: array types last token" {
try testCanonical(
\\test {
\\ const x = [40]u32;
\\}
\\
\\test {
\\ const x = [40:0]u32;
\\}
\\
);
}