lfcm64/z-tree-sitter
Zig wrapper for tree-sitter C API
A Zig package that provides a complete wrapper around tree-sitter API, along with built-in support for well-known languages grammar (see supported languages grammar). The current version of z-tree-sitter supports tree-sitter 0.23.0.
You can find documentation directly from the tree-sitter API header or on the tree-sitter website.
All wrapper functions and types are demonstrated in the tests file, where you can see Zig equivalents of the C tree-sitter API.
zig build test -- --all-language
zig build example-<name> -- --language zig
Example code is included in the examples directory.
The --all-languages argument includes all built-in language grammars in z-tree-sitter, which is necessary for running tests.
The --language argument installs only the language grammar specified after it (usefull for running examples).
To integrate z-tree-sitter into your project, you can either find the appropriate version archive in this github repo, or simply run: zig fetch --save git+https://github.com/lfcm64/z-tree-sitter
to add the most recent commit of ziglua to your build.zig.zon
file.
Then in your build.zig
file you can use the dependency:
pub fn build(b: *std.Build) void {
// ... snip ...
const zts = b.dependency("zts", .{
.target = target,
.optimize = optimize,
});
// ... snip ...
// add the z-tree-sitter module
exe.root_module.addImport("zts", zts.module("zts"));
}
This will compile the C tree-sitter library and link it with your project.
To compile and use tree-sitter grammar with z-tree-sitter, pass the relevant options in the b.dependency()
call in your build.zig
file.
For example, here is a b.dependency()
call that links erlang and javascript grammar to z-tree-sitter:
const zts = b.dependency("zts", .{
.target = target,
.optimize = optimize,
.erlang = true,
.javascript = true,
});
You can then load and use the imported grammar languages by calling zts.loadLanguage()
, see this example for more details.
Here is a list of all available languages grammar:
bash
c
css
cpp
c-sharp
elixir
elm
erlang
fsharp
go
haskell
java
javascript
json
julia
kotlin
lua
markdown
nim
ocaml
perl
php
python
ruby
rust
scala
toml
typescript
zig