Skip to content

Basics โ€‹

What you'll learn โ€‹

  • Parsing a string, reading the root node, and detecting errors.

When to use this โ€‹

First contact with the library, smoke tests, and language experiments.

Complete example โ€‹

examples/basic_parse.zig:

zig
var parser = treesitter.Parser.init(gpa);
defer parser.deinit();
try parser.setLanguage(treesitter.expression_language);

var tree = try parser.parseString("1 + 2 * 3");
defer tree.deinit();

const root = tree.rootNode();
std.debug.print("root: {s} [{d}, {d}]\n", .{ root.nodeType(), root.startByte(), root.endByte() });
std.debug.print("has error: {}\n", .{tree.hasError()});
std.debug.print("text: {s}\n", .{root.text()});

Running the example โ€‹

sh
zig build run-basic_parse

Expected output โ€‹

text
root: program [0, 9]
has error: false
text: 1 + 2 * 3

How it works โ€‹

parseString lexes and reduces 1 + 2 * 3 into a program node spanning the whole input. hasError() reads the root's aggregated flag โ€” false here because every token had a valid table action. text() slices the tree's owned source copy.

API used โ€‹

Memory ownership โ€‹

Parser owns scratch; tree owns pool and source; handles borrow. Three deinit calls release everything.

Released under the MIT License.