Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# Rust artifacts
target/
Cargo.lock

# Node artifacts
build/
prebuilds/
node_modules/
package-lock.json

# Swift artifacts
.build/
Package.resolved

# Go artifacts
_obj/
Expand All @@ -25,6 +28,13 @@ dist/
*.dylib
*.dll
*.pc
*.exp
*.lib

# Zig artifacts
.zig-cache/
zig-cache/
zig-out/

# Example dirs
/examples/*/
Expand Down
5 changes: 5 additions & 0 deletions bindings/zig/root.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extern fn tree_sitter_cpp() callconv(.c) *const anyopaque;

pub fn language() *const anyopaque {
return tree_sitter_cpp();
}
16 changes: 16 additions & 0 deletions bindings/zig/test.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const testing = @import("std").testing;

const ts = @import("tree-sitter");
const root = @import("tree-sitter-cpp");
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());
}
67 changes: 67 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 library_name = "tree-sitter-cpp";

const grammar = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.pic = if (shared) true else null,
});
const lib: *std.Build.Step.Compile = b.addLibrary(.{
.name = library_name,
.linkage = if (shared) .dynamic else .static,
.root_module = grammar,
});

grammar.addCSourceFiles(.{
.files = &.{ "src/parser.c", "src/scanner.c" },
.flags = &.{"-std=c11"},
});

if (reuse_alloc) {
grammar.addCMacro("TREE_SITTER_REUSE_ALLOCATOR", "");
}
if (optimize == .Debug) {
grammar.addCMacro("TREE_SITTER_DEBUG", "");
}

grammar.addIncludePath(b.path("src"));

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"},
});

const module = b.addModule(library_name, .{
.root_source_file = b.path("bindings/zig/root.zig"),
.target = target,
.optimize = optimize,
});
module.linkLibrary(lib);

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(library_name, module);

const run_tests = b.addRunArtifact(tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_tests.step);
}
22 changes: 22 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.{
.name = .tree_sitter_cpp,
.fingerprint = 0xb8f4bd40e27ed859,
.minimum_zig_version = "0.16.0",
.version = "0.23.4",
.dependencies = .{
.tree_sitter = .{
.url = "git+https://github.com/tree-sitter/zig-tree-sitter#b4b72c903e69998fc88e27e154a5e3cc9166551b",
.hash = "tree_sitter-0.25.0-8heIf51vAQConvVIgvm-9mVIbqh7yabZYqPXfOpS3YoG",
.lazy = true,
},
},
.paths = .{
"build.zig",
"build.zig.zon",
"bindings/zig",
"src",
"queries",
"LICENSE",
},
}