parser: port fn param and container initializer tests
Port tests: - "doc comments on param decl" - "pointer of unknown length" - "call expression" - "anytype type" - "container initializers" Handle anytype keyword in function parameter lists. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
12
parser.c
12
parser.c
@@ -1113,7 +1113,7 @@ static SmallSpan parseParamDeclList(Parser* p) {
|
||||
&& p->token_tags[p->tok_i + 1] == TOKEN_COLON) {
|
||||
p->tok_i += 2; // consume name and colon
|
||||
} else if (p->token_tags[p->tok_i] == TOKEN_ELLIPSIS3) {
|
||||
// anytype (...) varargs
|
||||
// varargs (...)
|
||||
p->tok_i++;
|
||||
if (eatToken(p, TOKEN_R_PAREN) != null_token)
|
||||
break;
|
||||
@@ -1121,6 +1121,16 @@ static SmallSpan parseParamDeclList(Parser* p) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// anytype params are omitted from the AST
|
||||
if (eatToken(p, TOKEN_KEYWORD_ANYTYPE) != null_token) {
|
||||
if (p->token_tags[p->tok_i] == TOKEN_COMMA) {
|
||||
p->tok_i++;
|
||||
continue;
|
||||
}
|
||||
expectToken(p, TOKEN_R_PAREN);
|
||||
break;
|
||||
}
|
||||
|
||||
const AstNodeIndex type_expr = parseTypeExpr(p);
|
||||
if (type_expr != 0)
|
||||
SLICE_APPEND(AstNodeIndex, &p->scratch, type_expr);
|
||||
|
||||
@@ -2098,6 +2098,73 @@ test "zig fmt: comment after params" {
|
||||
);
|
||||
}
|
||||
|
||||
test "zig fmt: doc comments on param decl" {
|
||||
try testCanonical(
|
||||
\\pub const Allocator = struct {
|
||||
\\ shrinkFn: fn (
|
||||
\\ self: Allocator,
|
||||
\\ /// Guaranteed to be the same as what was returned from most recent call to
|
||||
\\ /// `allocFn`, `reallocFn`, or `shrinkFn`.
|
||||
\\ old_mem: []u8,
|
||||
\\ /// Guaranteed to be the same as what was returned from most recent call to
|
||||
\\ /// `allocFn`, `reallocFn`, or `shrinkFn`.
|
||||
\\ old_alignment: u29,
|
||||
\\ /// Guaranteed to be less than or equal to `old_mem.len`.
|
||||
\\ new_byte_count: usize,
|
||||
\\ /// Guaranteed to be less than or equal to `old_alignment`.
|
||||
\\ new_alignment: u29,
|
||||
\\ ) []u8,
|
||||
\\};
|
||||
\\
|
||||
);
|
||||
}
|
||||
|
||||
test "zig fmt: pointer of unknown length" {
|
||||
try testCanonical(
|
||||
\\fn foo(ptr: [*]u8) void {}
|
||||
\\
|
||||
);
|
||||
}
|
||||
|
||||
test "zig fmt: call expression" {
|
||||
try testCanonical(
|
||||
\\test "test calls" {
|
||||
\\ a();
|
||||
\\ a(1);
|
||||
\\ a(1, 2);
|
||||
\\ a(1, 2) + a(1, 2);
|
||||
\\}
|
||||
\\
|
||||
);
|
||||
}
|
||||
|
||||
test "zig fmt: anytype type" {
|
||||
try testCanonical(
|
||||
\\fn print(args: anytype) @This() {}
|
||||
\\
|
||||
);
|
||||
}
|
||||
|
||||
test "zig fmt: container initializers" {
|
||||
try testCanonical(
|
||||
\\const a0 = []u8{};
|
||||
\\const a1 = []u8{1};
|
||||
\\const a2 = []u8{
|
||||
\\ 1,
|
||||
\\ 2,
|
||||
\\ 3,
|
||||
\\ 4,
|
||||
\\};
|
||||
\\const s0 = S{};
|
||||
\\const s1 = S{ .a = 1 };
|
||||
\\const s2 = S{
|
||||
\\ .a = 1,
|
||||
\\ .b = 2,
|
||||
\\};
|
||||
\\
|
||||
);
|
||||
}
|
||||
|
||||
test "zig fmt: sentinel-terminated array type" {
|
||||
try testCanonical(
|
||||
\\pub fn cStrToPrefixedFileW(s: [*:0]const u8) ![PATH_MAX_WIDE:0]u16 {
|
||||
|
||||
Reference in New Issue
Block a user