ww source: align UTF-8 BOM placement

This commit is contained in:
2026-08-21 20:34:10 +09:00
parent ee4573bf55
commit 9381f8fb8e
11 changed files with 764 additions and 5 deletions

View File

@@ -61,6 +61,7 @@ struct row { const char *src, *expect; };
static const struct row rows[] = {
{ "", "" },
{ "\xef\xbb\xbf" "package main;", "package IDENT(main) ;" },
{ " \t\n ", "" },
{ "// comment\n", "" },
{ "/* a /b/ c */", "" },
@@ -172,7 +173,29 @@ runrewind(void)
return ok;
}
static int
runbompos(void)
{
const char *src = "\xef\xbb\xbf" "package main;";
Arena *a = newarena();
Lex l;
lexinit(&l, a, "<test>", src, strlen(src));
Tok t = lexnext(&l);
int ok = t.kind == TK_MODULE && t.pos.line == 1 && t.pos.col == 4;
if (!ok)
fprintf(stderr, "leading BOM position: want 1:4 got %d:%d\n",
t.pos.line, t.pos.col);
freearena(a);
return ok;
}
static const char *const errrows[] = {
"\xef\xbb\xbf" "\xef\xbb\xbf" "package main;",
"package \xef\xbb\xbf" "main;",
"// \xef\xbb\xbf\n",
"/* \xef\xbb\xbf */",
"\"\xef\xbb\xbf\"",
"'\xef\xbb\xbf'",
"'\\uZ'", /* unexpected rune scanning for escape */
"\"\\u00g0\"", /* non-hex digit inside a string escape */
"'\\u00", /* unexpected EOF scanning for escape */
@@ -194,6 +217,8 @@ main(void)
}
if (!runrewind())
fail++;
if (!runbompos())
fail++;
for (size_t i = 0; i < sizeof errrows / sizeof errrows[0]; i++) {
if (!runerr(errrows[i])) {
fprintf(stderr, "errrow %zu failed\n", i);