Given an identifier token, obtain the string for it.
If the token uses @"" syntax, parses as a string, reports errors if applicable,
and allocates the result within astgen.arena.
Otherwise, returns a reference to the source code bytes directly.
See also appendIdentStr and parseStrLit.
fn identifierTokenString(astgen: *AstGen, token: Ast.TokenIndex) InnerError![]const u8
fn identifierTokenString(astgen: *AstGen, token: Ast.TokenIndex) InnerError![]const u8 {
const tree = astgen.tree;
assert(tree.tokenTag(token) == .identifier);
const ident_name = tree.tokenSlice(token);
if (!mem.startsWith(u8, ident_name, "@")) {
return ident_name;
}
var buf: ArrayList(u8) = .empty;
defer buf.deinit(astgen.gpa);
try astgen.parseStrLit(token, &buf, ident_name, 1);
if (mem.findScalar(u8, buf.items, 0) != null) {
return astgen.failTok(token, "identifier cannot contain null bytes", .{});
} else if (buf.items.len == 0) {
return astgen.failTok(token, "identifier cannot be empty", .{});
}
const duped = try astgen.arena.dupe(u8, buf.items);
return duped;
}