Parser โ
Overview โ
Parser owns the LR engine state: stacks, scratch buffers, reuse candidates, ranges, and the client allocator. One long-lived parser serves an entire editing session.
Import: treesitter.Parser. Facade source: src/parser/parser.zig.
Lifecycle โ
var parser = treesitter.Parser.init(allocator);
defer parser.deinit();Methods โ
init โ
pub fn init(gpa: std.mem.Allocator) ParserCreates a parser holding gpa for all internal buffers. The only allocator handoff in typical use.
deinit โ
pub fn deinit(self: *Parser) voidFrees stacks, scratch, candidates, and ranges.
setLanguage โ
pub fn setLanguage(self: *Parser, language: Language) Language.Error!voidLoads grammar tables after ABI validation (IncompatibleAbiVersion on mismatch). Clears parser state.
getLanguage โ
pub fn getLanguage(self: *const Parser) ?LanguagesetLogger / setIncludedRanges / includedRanges โ
pub fn setLogger(self: *Parser, logger: Logger) void
pub fn setIncludedRanges(self: *Parser, ranges: []const Range) (Allocator.Error || error{InvalidRange})!void
pub fn includedRanges(self: *const Parser) []const RangeRanges must be sorted and non-overlapping (error.InvalidRange otherwise) and are enforced by the lexer โ see Included Ranges.
reset โ
pub fn reset(self: *Parser) voidDrops retained scratch capacity between unrelated workloads.
parseString โ
pub fn parseString(self: *Parser, source: []const u8) ParserError!TreeFull parse; the tree copies the source.
parse โ
pub fn parse(self: *Parser, old_tree: ?*const Tree, edit: ?InputEdit, source: []const u8) ParserError!Treenull old tree means a fresh parse. With an old tree plus edit, unaffected subtrees are reused; see reused_node_count.
parseWithInput โ
pub fn parseWithInput(self: *Parser, old_tree: ?*const Tree, edit: ?InputEdit, reader_input: Input) ParserError!TreePulls callback chunks, buffers them, and parses identically to memory input.
parseStream โ
pub fn parseStream(self: *Parser, old_tree: ?*const Tree, edit: ?InputEdit, reader_input: Input) ParserError!TreeTrue streaming: bytes are pulled on demand as the lexer advances โ no pre-buffering โ and the tree takes ownership of exactly the consumed prefix. With an old tree the input is buffered once so subtree reuse still applies. See Custom Input.
queryCursor / compileQuery โ
pub fn queryCursor(self: *Parser) QueryCursor
pub fn compileQuery(self: *Parser, source: []const u8) QueryError!QueryAllocator-inheriting conveniences (compileQuery returns InvalidLanguage when no language is set).
reused_node_count โ
reused_node_count: usize = 0Subtrees spliced from the old tree by the most recent parse.
Errors โ
ParserError = Allocator.Error || error{ NoLanguage, Aborted, InvalidRange }. Syntax problems never surface here โ they become ERROR/MISSING nodes in the tree.
Ownership โ
The parser borrows the allocator and the Language tables; it owns all internal buffers.
Thread safety โ
Not synchronized. Use one parser per thread.
Example โ
See Your First Parser and Incremental Parsing.
