fix(bindings): correct Zig bindings to expose a language function

Instead of having users declare the extern function themselves, they can
pass in the language to `Language.create` in the zig bindings. If they
really want, they can always opt into the `extern fn tree_sitter_LANG()
*const ts.Language` approach.
This commit is contained in:
Amaan Qureshi 2025-02-01 16:34:47 -05:00
parent 9ad096ef22
commit eed662df98
No known key found for this signature in database
GPG key ID: E67890ADC4227273
4 changed files with 62 additions and 13 deletions

View file

@ -97,6 +97,7 @@ const TESTS_SWIFT_TEMPLATE: &str = include_str!("./templates/tests.swift");
const BUILD_ZIG_TEMPLATE: &str = include_str!("./templates/build.zig");
const BUILD_ZIG_ZON_TEMPLATE: &str = include_str!("./templates/build.zig.zon");
const ROOT_ZIG_TEMPLATE: &str = include_str!("./templates/root.zig");
const TREE_SITTER_JSON_SCHEMA: &str =
"https://tree-sitter.github.io/tree-sitter/assets/schemas/config.schema.json";
@ -700,6 +701,14 @@ pub fn generate_grammar_files(
missing_path(repo_path.join("build.zig.zon"), |path| {
generate_file(path, BUILD_ZIG_ZON_TEMPLATE, language_name, &generate_opts)
})?;
missing_path(bindings_dir.join("zig"), create_dir)?.apply(|path| {
missing_path(path.join("root.zig"), |path| {
generate_file(path, ROOT_ZIG_TEMPLATE, language_name, &generate_opts)
})?;
Ok(())
})?;
}
Ok(())

View file

@ -39,20 +39,38 @@ pub fn build(b: *std.Build) !void {
}
lib.addIncludePath(b.path("src"));
lib.installHeadersDirectory(b.path("bindings/c"), ".", .{});
b.installArtifact(lib);
b.installFile("src/node-types.json", "node-types.json");
b.installDirectory(.{
.source_dir = b.path("queries"),
.install_dir = .prefix,
.install_subdir = "queries",
.include_extensions = &.{"scm"}
});
b.installDirectory(.{ .source_dir = b.path("queries"), .install_dir = .prefix, .install_subdir = "queries", .include_extensions = &.{"scm"} });
const test_cmd = b.addSystemCommand(&.{"tree-sitter", "test"});
const test_step = b.step("test", "Run parser tests");
test_step.dependOn(&test_cmd.step);
const module = b.addModule("tree-sitter-PARSER_NAME", .{
.root_source_file = b.path("bindings/zig/root.zig"),
.target = target,
.optimize = optimize,
});
module.linkLibrary(lib);
const ts_dep = b.dependency("tree-sitter", .{});
const ts_mod = ts_dep.module("tree-sitter");
module.addImport("tree-sitter", ts_mod);
//
// Tests
//
const tests = b.addTest(.{
.root_source_file = b.path("bindings/zig/root.zig"),
.target = target,
.optimize = optimize,
});
tests.linkLibrary(lib);
tests.root_module.addImport("tree-sitter", ts_mod);
const run_tests = b.addRunArtifact(tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_tests.step);
}
inline fn hasScanner(dir: std.fs.Dir) bool {

View file

@ -1,11 +1,14 @@
.{
.name = "tree_sitter_PARSER_NAME",
.name = "tree-sitter-PARSER_NAME",
.version = "PARSER_VERSION",
.dependencies = .{},
.dependencies = .{ .@"tree-sitter" = .{
.url = "https://github.com/tree-sitter/zig-tree-sitter/archive/refs/tags/v0.25.0.tar.gz",
.hash = "12201a8d5e840678bbbf5128e605519c4024af422295d68e2ba2090e675328e5811d",
} },
.paths = .{
"build.zig",
"build.zig.zon",
"bindings/c",
"bindings/zig",
"src",
"queries",
"LICENSE",

View file

@ -0,0 +1,19 @@
const testing = @import("std").testing;
const ts = @import("tree-sitter");
const Language = ts.Language;
const Parser = ts.Parser;
pub extern fn tree_sitter_PARSER_NAME() callconv(.C) *const Language;
pub export fn language() *const Language {
return tree_sitter_PARSER_NAME();
}
test "can load grammar" {
const parser = Parser.create();
defer parser.destroy();
try testing.expectEqual(parser.setLanguage(language()), void{});
try testing.expectEqual(parser.getLanguage(), tree_sitter_PARSER_NAME());
}