feat(lang): backtick raw-identifier escape + #import c foreign-name exemption [F0.6]
Reserved type-name spellings (s1, s2, u8, …) can now be used as value identifiers two ways, resolving issue 0089: 1. Backtick raw identifier: a leading backtick (`s2) lexes to an .identifier token carrying a new Token.is_raw flag, with the backtick excluded from the text. A raw identifier is never type-classified — the parser skips Type.fromName for it — so it is always a value identifier. The flag threads to VarDecl.is_raw / Param.is_raw at binding sites, and the reserved-type-name check (UnknownTypeChecker) skips raw bindings. Because the token tag stays .identifier, the escape works in every position (local, global, param, field, fn name, struct member, later reference) with no per-site parser change. 2. #import c exemption: c_import.zig synthesizes foreign decls with Param.is_raw = true, so generated C param names that collide with reserved type names (s1, s2) import unedited. A bare reserved-name binding in sx still errors (issue 0076 preserved): the is_raw-gated skip only fires for backtick / foreign names, and a raw binding's address-of / autoref lowering stays correct because every occurrence is an .identifier, never a .type_expr. Tests: examples/0151 (backtick, every position), examples/1220 (foreign exemption, compiled+run), lexer unit tests. 1119 (bare-binding rejection) stays green. specs.md + readme.md updated.
This commit is contained in:
@@ -50,6 +50,24 @@ pub const Lexer = struct {
|
||||
return self.lexString(start);
|
||||
}
|
||||
|
||||
// Raw-identifier escape: `ident — a leading backtick forces the
|
||||
// following identifier to be RAW (never type-classified, never
|
||||
// reserved-checked). The emitted token's span excludes the backtick, so
|
||||
// its text is the bare name, and a backticked keyword spelling
|
||||
// (`` `s2 ``, `` `string ``) is still an `.identifier`, never a keyword.
|
||||
if (c == '`') {
|
||||
const id_start = start + 1;
|
||||
if (id_start < self.source.len and isIdentStart(self.source[id_start])) {
|
||||
self.index = id_start;
|
||||
var tok = self.lexIdentifier(id_start);
|
||||
tok.tag = .identifier;
|
||||
tok.is_raw = true;
|
||||
return tok;
|
||||
}
|
||||
self.index += 1;
|
||||
return self.makeToken(.invalid, start, self.index);
|
||||
}
|
||||
|
||||
|
||||
// Directives: #import, #insert, #run, #builtin, #foreign, #library, #string
|
||||
if (c == '#') {
|
||||
@@ -485,6 +503,38 @@ test "lex type-like identifiers" {
|
||||
}
|
||||
}
|
||||
|
||||
test "lex backtick raw identifier" {
|
||||
const source: [:0]const u8 = "`s2 `string `for";
|
||||
var lex = Lexer.init(source);
|
||||
// Each is an `.identifier` carrying `is_raw`, even a keyword spelling
|
||||
// (`for`), with text that excludes the leading backtick.
|
||||
const t1 = lex.next();
|
||||
try std.testing.expectEqual(Tag.identifier, t1.tag);
|
||||
try std.testing.expect(t1.is_raw);
|
||||
try std.testing.expectEqualStrings("s2", t1.slice(source));
|
||||
const t2 = lex.next();
|
||||
try std.testing.expectEqual(Tag.identifier, t2.tag);
|
||||
try std.testing.expect(t2.is_raw);
|
||||
try std.testing.expectEqualStrings("string", t2.slice(source));
|
||||
const t3 = lex.next();
|
||||
try std.testing.expectEqual(Tag.identifier, t3.tag);
|
||||
try std.testing.expect(t3.is_raw);
|
||||
try std.testing.expectEqualStrings("for", t3.slice(source));
|
||||
try std.testing.expectEqual(Tag.eof, lex.next().tag);
|
||||
}
|
||||
|
||||
test "lex bare identifier is not raw" {
|
||||
var lex = Lexer.init("s2");
|
||||
const tok = lex.next();
|
||||
try std.testing.expectEqual(Tag.identifier, tok.tag);
|
||||
try std.testing.expect(!tok.is_raw);
|
||||
}
|
||||
|
||||
test "lex lone backtick is invalid" {
|
||||
var lex = Lexer.init("` 5");
|
||||
try std.testing.expectEqual(Tag.invalid, lex.next().tag);
|
||||
}
|
||||
|
||||
test "lex hash_run" {
|
||||
var lex = Lexer.init("#run");
|
||||
try std.testing.expectEqual(Tag.hash_run, lex.next().tag);
|
||||
|
||||
Reference in New Issue
Block a user