feat: add zig bindings

This commit is contained in:
2025-10-17 21:56:38 +08:00
parent 65da635196
commit 2b8388e276
4 changed files with 120 additions and 0 deletions

5
bindings/zig/root.zig generated Normal file
View File

@@ -0,0 +1,5 @@
extern fn tree_sitter_emm() callconv(.c) *const anyopaque;
pub fn language() *const anyopaque {
return tree_sitter_emm();
}

17
bindings/zig/test.zig generated Normal file
View File

@@ -0,0 +1,17 @@
const testing = @import("std").testing;
const ts = @import("tree_sitter");
const root = @import("tree_sitter_emm");
const Language = ts.Language;
const Parser = ts.Parser;
test "can load grammar" {
const parser = Parser.create();
defer parser.destroy();
const lang: *const ts.Language = @ptrCast(root.language());
defer lang.destroy();
try testing.expectEqual(void{}, parser.setLanguage(lang));
try testing.expectEqual(lang, parser.getLanguage());
}

85
build.zig generated Normal file
View File

@@ -0,0 +1,85 @@
const std = @import("std");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const shared = b.option(bool, "build-shared", "Build a shared library") orelse true;
const reuse_alloc = b.option(bool, "reuse-allocator", "Reuse the library allocator") orelse false;
const lib = b.addLibrary(.{
.name = "tree-sitter-emm",
.linkage = if (shared) .dynamic else .static,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.pic = if (shared) true else null,
}),
});
lib.addCSourceFile(.{
.file = b.path("src/parser.c"),
.flags = &.{"-std=c11"},
});
if (fileExists(b, "src/scanner.c")) {
lib.addCSourceFile(.{
.file = b.path("src/scanner.c"),
.flags = &.{"-std=c11"},
});
}
if (reuse_alloc) {
lib.root_module.addCMacro("TREE_SITTER_REUSE_ALLOCATOR", "");
}
if (optimize == .Debug) {
lib.root_module.addCMacro("TREE_SITTER_DEBUG", "");
}
lib.addIncludePath(b.path("src"));
b.installArtifact(lib);
b.installFile("src/node-types.json", "node-types.json");
if (fileExists(b, "queries")) {
b.installDirectory(.{
.source_dir = b.path("queries"),
.install_dir = .prefix,
.install_subdir = "queries",
.include_extensions = &.{"scm"},
});
}
const module = b.addModule("tree_sitter_emm", .{
.root_source_file = b.path("bindings/zig/root.zig"),
.target = target,
.optimize = optimize,
});
module.linkLibrary(lib);
const tree_sitter = b.dependency("tree_sitter", .{
.target = target,
.optimize = optimize,
});
module.addImport("tree_sitter", tree_sitter.module("tree_sitter"));
const tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("bindings/zig/test.zig"),
.target = target,
.optimize = optimize,
}),
});
tests.root_module.addImport("tree_sitter", tree_sitter.module("tree_sitter"));
tests.root_module.addImport("tree_sitter_emm", module);
const run_tests = b.addRunArtifact(tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_tests.step);
}
inline fn fileExists(b: *std.Build, filename: []const u8) bool {
const dir = b.build_root.handle;
dir.access(filename, .{}) catch return false;
return true;
}

13
build.zig.zon generated Normal file
View File

@@ -0,0 +1,13 @@
.{
.name = .tree_sitter_emm,
.version = "0.0.1",
.dependencies = .{
.tree_sitter = .{
.url = "git+https://github.com/tree-sitter/zig-tree-sitter#4c5d3a45a9828b8fddd3d59132c4346182fc23b7",
.hash = "tree_sitter-0.25.0-8heIf4KZAQCmEhQwLPPtKBsU3BBkohvUJpfDXc7vmDM7",
},
},
.minimum_zig_version = "0.16.0-dev.732+2f3234c76",
.paths = .{""},
.fingerprint = 0x2013c1379932809e,
}