diff --git a/cli/src/init.rs b/cli/src/init.rs index 4d0b56e8..f935f05b 100644 --- a/cli/src/init.rs +++ b/cli/src/init.rs @@ -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(()) diff --git a/cli/src/templates/build.zig b/cli/src/templates/build.zig index 9d237f4e..65c3b837 100644 --- a/cli/src/templates/build.zig +++ b/cli/src/templates/build.zig @@ -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 { diff --git a/cli/src/templates/build.zig.zon b/cli/src/templates/build.zig.zon index eff723d7..1f4b3907 100644 --- a/cli/src/templates/build.zig.zon +++ b/cli/src/templates/build.zig.zon @@ -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", diff --git a/cli/src/templates/root.zig b/cli/src/templates/root.zig new file mode 100644 index 00000000..26a6cbef --- /dev/null +++ b/cli/src/templates/root.zig @@ -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()); +} +