Skip to content

tree-sitter/zig-tree-sitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zig Tree-sitter

CI docs

Zig bindings to the tree-sitter parsing library.

Usage

build.zig
const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{
        .name = "tree-sitter-temp",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });
    b.installArtifact(exe);

    const tree_sitter = b.dependency("tree_sitter", .{
        .target = target,
        .optimize = optimize,
    });
    exe.root_module.addImport("tree_sitter", tree_sitter.module("tree_sitter"));

    const tree_sitter_zig = b.dependency("tree_sitter_zig", .{
        .target = target,
        .optimize = optimize,
    });
    exe.linkLibrary(tree_sitter_zig.artifact("tree-sitter-zig"));

    const run_cmd = b.addRunArtifact(exe);
    run_cmd.step.dependOn(b.getInstallStep());
    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
}
src/main.zig
const std = @import("std");
const ts = @import("tree_sitter");

extern fn tree_sitter_zig() callconv(.C) *ts.Language;

pub fn main() !void {
    // Create a parser for the zig language
    const language = tree_sitter_zig();
    defer language.destroy();

    const parser = ts.Parser.create();
    defer parser.destroy();
    try parser.setLanguage(language);

    // Parse some source code and get the root node
    const tree = try parser.parseBuffer("pub fn main() !void {}", null, null);
    defer tree.destroy();

    const node = tree.rootNode();
    std.debug.assert(std.mem.eql(u8, node.@"type"(), "source_file"));
    std.debug.assert(node.endPoint().cmp(.{ .row = 0, .column = 22 }) == 0);

    // Create a query and execute it
    var error_offset: u32 = 0;
    const query = try ts.Query.create(language, "name: (identifier) @name", &error_offset);
    defer query.destroy();

    const cursor = ts.QueryCursor.create();
    defer cursor.destroy();
    cursor.exec(query, node);

    // Get the captured node of the first match
    const match = cursor.nextMatch().?;
    const capture = match.captures[0].node;
    std.debug.assert(std.mem.eql(u8, capture.@"type"(), "identifier"));
}