parser: add multiline string, empty file, field, container tests

Port tests from upstream parser_test.zig:
- "multiline string mixed with comments"
- "empty file"
- "file ends in comment"
- "file ends in multi line comment"
- "file ends in comment after var decl"
- "top-level fields"
- "container declaration, single line"

Implement in parser.c:
- parseSuffixOp: array access, field access, deref, unwrap optional
- Slice/array type parsing in parseTypeExpr
- Multiline string literal parsing

Fix zigData mapping in parser_test.zig:
- optional_type uses .node (not .opt_node)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-02-10 12:23:18 +00:00
parent 298b347cf7
commit 8d193346e8

View File

@@ -222,11 +222,11 @@ fn zigData(tag: Ast.Node.Tag, lhs: u32, rhs: u32) Ast.Node.Data {
.address_of,
.@"try",
.deref,
.optional_type,
=> .{ .node = toIndex(lhs) },
// .opt_node (single optional node)
.@"return",
.optional_type,
=> .{ .opt_node = toOptIndex(lhs) },
// .node_and_node
@@ -649,3 +649,88 @@ test "zig fmt: respect line breaks after var declarations" {
\\
);
}
test "zig fmt: multiline string mixed with comments" {
try testCanonical(
\\const s1 =
\\ //\\one
\\ \\two)
\\ \\three
\\;
\\const s2 =
\\ \\one
\\ \\two)
\\ //\\three
\\;
\\const s3 =
\\ \\one
\\ //\\two)
\\ \\three
\\;
\\const s4 =
\\ \\one
\\ //\\two
\\ \\three
\\ //\\four
\\ \\five
\\;
\\const a =
\\ 1;
\\
);
}
test "zig fmt: empty file" {
try testCanonical(
\\
);
}
test "zig fmt: file ends in comment" {
try testTransform(
\\ //foobar
,
\\//foobar
\\
);
}
test "zig fmt: file ends in multi line comment" {
try testTransform(
\\ \\foobar
,
\\\\foobar
\\
);
}
test "zig fmt: file ends in comment after var decl" {
try testTransform(
\\const x = 42;
\\ //foobar
,
\\const x = 42;
\\//foobar
\\
);
}
test "zig fmt: top-level fields" {
try testCanonical(
\\a: did_you_know,
\\b: all_files_are,
\\structs: ?x,
\\
);
}
test "zig fmt: container declaration, single line" {
try testCanonical(
\\const X = struct { foo: i32 };
\\const X = struct { foo: i32, bar: i32 };
\\const X = struct { foo: i32 = 1, bar: i32 = 2 };
\\const X = struct { foo: i32 align(4), bar: i32 align(4) };
\\const X = struct { foo: i32 align(4) = 1, bar: i32 align(4) = 2 };
\\
);
}