zig0

my attempts at zig bootstrapping in C
Log | Files | Refs | README | LICENSE

commit 83b74c3d1ea1dca3c4e64f0bd0f61883fce829e2 (tree)
parent f3022bf793738507ec78c35f28c40392a4efb090
Author: Motiejus Jakštys <motiejus.jakstys@chronosphere.io>
Date:   Tue, 10 Feb 2026 19:13:53 +0000

parser: port error set, suspend, switch prong comment tests

Port tests:
- "error set declaration"
- "union(enum(u32)) with assigned enum values"
- "resume from suspend block"
- "comments before error set decl"
- "comments before switch prong"
- "array literal with 1 item on 1 line"
- "comments in statements"

Implement in parser.c:
- suspend statement in expectStatement
- Fix error set decl to store lbrace token (not 0)
- Fix comptime statement to wrap inner expression

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Diffstat:
Mparser.c | 13+++++++++++--
Mparser_test.zig | 108+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 119 insertions(+), 2 deletions(-)

diff --git a/parser.c b/parser.c @@ -567,7 +567,7 @@ static AstNodeIndex parsePrimaryTypeExpr(Parser* p) { } case TOKEN_L_BRACE: { const AstTokenIndex error_token = nextToken(p); - nextToken(p); // consume { + const AstTokenIndex lbrace = nextToken(p); while (p->token_tags[p->tok_i] != TOKEN_R_BRACE) p->tok_i++; const AstTokenIndex rbrace = nextToken(p); @@ -575,7 +575,7 @@ static AstNodeIndex parsePrimaryTypeExpr(Parser* p) { (AstNodeItem) { .tag = AST_NODE_ERROR_SET_DECL, .main_token = error_token, - .data = { .lhs = 0, .rhs = rbrace }, + .data = { .lhs = lbrace, .rhs = rbrace }, }); } default: @@ -2328,6 +2328,15 @@ static AstNodeIndex expectStatement(Parser* p, bool allow_defer_var) { }, }); case TOKEN_KEYWORD_SUSPEND: + return addNode(&p->nodes, + (AstNodeItem) { + .tag = AST_NODE_SUSPEND, + .main_token = nextToken(p), + .data = { + .lhs = expectBlockExprStatement(p), + .rhs = 0, + }, + }); case TOKEN_KEYWORD_ENUM: case TOKEN_KEYWORD_STRUCT: case TOKEN_KEYWORD_UNION:; diff --git a/parser_test.zig b/parser_test.zig @@ -3140,6 +3140,114 @@ test "zig fmt: first line comment in struct initializer" { ); } +test "zig fmt: error set declaration" { + try testCanonical( + \\const E = error{ + \\ A, + \\ B, + \\ + \\ C, + \\}; + \\ + \\const Error = error{ + \\ /// no more memory + \\ OutOfMemory, + \\}; + \\ + \\const Error = error{ + \\ /// no more memory + \\ OutOfMemory, + \\ + \\ /// another + \\ Another, + \\ + \\ // end + \\}; + \\ + \\const Error = error{OutOfMemory}; + \\const Error = error{}; + \\ + \\const Error = error{ OutOfMemory, OutOfTime }; + \\ + ); +} + +test "zig fmt: union(enum(u32)) with assigned enum values" { + try testCanonical( + \\const MultipleChoice = union(enum(u32)) { + \\ A = 20, + \\ B = 40, + \\ C = 60, + \\ D = 1000, + \\}; + \\ + ); +} + +test "zig fmt: resume from suspend block" { + try testCanonical( + \\fn foo() void { + \\ suspend { + \\ resume @frame(); + \\ } + \\} + \\ + ); +} + +test "zig fmt: comments before error set decl" { + try testCanonical( + \\const UnexpectedError = error{ + \\ /// The Operating System returned an undocumented error code. + \\ Unexpected, + \\ // another + \\ Another, + \\ + \\ // in between + \\ + \\ // at end + \\}; + \\ + ); +} + +test "zig fmt: comments before switch prong" { + try testCanonical( + \\test "" { + \\ switch (err) { + \\ error.PathAlreadyExists => continue, + \\ + \\ // comment 1 + \\ + \\ // comment 2 + \\ else => return err, + \\ // at end + \\ } + \\} + \\ + ); +} + +test "zig fmt: array literal with 1 item on 1 line" { + try testCanonical( + \\var s = []const u64{0} ** 25; + \\ + ); +} + +test "zig fmt: comments in statements" { + try testCanonical( + \\comptime { + \\ // a + \\ + \\ const x = 42; // b + \\ + \\ // c + \\} + \\ + ); +} + test "zig fmt: doc comments before struct field" { try testCanonical( \\pub const Allocator = struct {