Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

identifierTokenString

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.

AstGen.identifierTokenString
fn identifierTokenString(astgen: *AstGen, token: Ast.TokenIndex) InnerError![]const u8

File

lib/std/zig/AstGen.zig:10693

Code

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;
}