Result should be freed with tree.deinit() when there are no more references to any of the tokens or nodes.
pub fn parse(gpa: Allocator, source: [:0]const u8, options: ParseOptions) Allocator.Error!Ast
pub fn parse(gpa: Allocator, source: [:0]const u8, options: ParseOptions) Allocator.Error!Ast {
var tokens = Ast.TokenList{};
defer tokens.deinit(gpa);
// Empirically, the zig std lib has an 8:1 ratio of source bytes to token count.
const estimated_token_count = source.len / 8;
try tokens.ensureTotalCapacity(gpa, estimated_token_count);
var tokenizer = std.zig.Tokenizer.init(source);
while (true) {
const token = tokenizer.next();
try tokens.append(gpa, .{
.tag = token.tag,
.start = @intCast(token.loc.start),
});
if (token.tag == .eof) break;
}
var tokens_slice = tokens.toOwnedSlice();
errdefer tokens_slice.deinit(gpa);
return parseTokens(gpa, source, tokens_slice, options);
}