parser: add zig fmt on/off and slice operator tests

Port tests from upstream parser_test.zig:
- "comment to disable/enable zig fmt"
- "line comment following 'zig fmt: off'"
- "doc comment following 'zig fmt: off'"
- "alternating 'zig fmt: off' and 'zig fmt: on'"
- "spaces around slice operator"

Fix parsePrimaryTypeExpr: don't reject identifier followed by colon;
the colon may be part of slice sentinel syntax, not a label.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-02-10 13:08:47 +00:00
parent 1a8bb5ac10
commit c67c54c3fb
2 changed files with 64 additions and 6 deletions

View File

@@ -509,12 +509,6 @@ static AstNodeIndex parsePrimaryTypeExpr(Parser* p) {
});
}
case TOKEN_IDENTIFIER:
if (p->token_tags[p->tok_i + 1] == TOKEN_COLON) {
fprintf(stderr,
"parsePrimaryTypeExpr does not support identifier followed by "
"colon\n");
exit(1);
}
return addNode(&p->nodes,
(AstNodeItem) {
.tag = AST_NODE_IDENTIFIER,

View File

@@ -1432,3 +1432,67 @@ test "zig fmt: builtin call with trailing comma" {
\\
);
}
test "zig fmt: comment to disable/enable zig fmt" {
try testTransform(
\\const a = b;
\\// zig fmt: off
\\const c = d;
\\// zig fmt: on
\\const e = f;
,
\\const a = b;
\\// zig fmt: off
\\const c = d;
\\// zig fmt: on
\\const e = f;
\\
);
}
test "zig fmt: line comment following 'zig fmt: off'" {
try testCanonical(
\\// zig fmt: off
\\// Test
\\const e = f;
);
}
test "zig fmt: doc comment following 'zig fmt: off'" {
try testCanonical(
\\// zig fmt: off
\\/// test
\\const e = f;
);
}
test "zig fmt: alternating 'zig fmt: off' and 'zig fmt: on'" {
try testCanonical(
\\// zig fmt: off
\\// zig fmt: on
\\// zig fmt: off
\\const e = f;
\\// zig fmt: off
\\// zig fmt: on
\\// zig fmt: off
\\const a = b;
\\// zig fmt: on
\\const c = d;
\\// zig fmt: on
\\
);
}
test "zig fmt: spaces around slice operator" {
try testCanonical(
\\var a = b[c..d];
\\var a = b[c..d :0];
\\var a = b[c + 1 .. d];
\\var a = b[c + 1 ..];
\\var a = b[c .. d + 1];
\\var a = b[c .. d + 1 :0];
\\var a = b[c.a..d.e];
\\var a = b[c.a..d.e :0];
\\
);
}