commit e471e4564a27564ff4eb6db4a3d53070252a7341 (tree)
parent fb51ce64637fc7a5b66cd6ce13e1547f9b2dec55
Author: Motiejus Jakštys <motiejus.jakstys@chronosphere.io>
Date: Tue, 10 Feb 2026 12:28:14 +0000
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>
Diffstat:
2 files changed, 62 insertions(+), 3 deletions(-)
diff --git a/parser.c b/parser.c
@@ -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,
diff --git a/parser_test.zig b/parser_test.zig
@@ -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;
+ \\}
+ \\
+ );
+}