https://github.com/bonede/tree-sitter-ng
Next generation Tree Sitter Java binding.
https://github.com/bonede/tree-sitter-ng
java jni tree-sitter
Last synced: 4 days ago
JSON representation
Next generation Tree Sitter Java binding.
- Host: GitHub
- URL: https://github.com/bonede/tree-sitter-ng
- Owner: bonede
- License: mit
- Created: 2024-01-03T02:20:44.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2026-01-31T08:52:05.000Z (5 days ago)
- Last Synced: 2026-01-31T21:40:11.065Z (4 days ago)
- Topics: java, jni, tree-sitter
- Language: C
- Homepage:
- Size: 140 MB
- Stars: 130
- Watchers: 1
- Forks: 27
- Open Issues: 30
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Tree Sitter NG
Next generation Tree Sitter Java binding.
[](https://github.com/bonede/tree-sitter-ng/actions)
[](https://central.sonatype.com/artifact/io.github.bonede/tree-sitter)
# Getting started
Add dependencies to your `build.gradle` or `pom.xml`.
```groovy
// Gradle
dependencies {
// add tree sitter
implementation 'io.github.bonede:tree-sitter:0.25.3'
// add json parser
implementation 'io.github.bonede:tree-sitter-json:0.24.8'
}
```
```xml
io.github.bonede
tree-sitter
0.25.3
io.github.bonede
tree-sitter-json
0.24.8
```
Start hacking!
```java
// imports are omitted
class Main {
public static void main(String[] args) {
TSParser parser = new TSParser();
TSLanguage json = new TreeSitterJson();
parser.setLanguage(json);
TSTree tree = parser.parseString(null, "[1, null]");
TSNode rootNode = tree.getRootNode();
TSNode arrayNode = rootNode.getNamedChild(0);
TSNode numberNode = arrayNode.getNamedChild(0);
}
}
```
# Features
- 100% [Tree Sitter API](https://github.com/tree-sitter/tree-sitter/blob/master/lib/include/tree_sitter/api.h) coverage.
- Easy to bootstrap cross compiling environments powered by [Zig](https://ziglang.org/).
- Include only parsers you need in your project.
# Supported CPUs and OSes
- x86_64-windows
- x86_64-macos
- aarch64-macos
- x86_64-linux
- aarch64-linux
# Supported parsers
| Name | Version |
|---------------------------------|---------------------------------------------------------------------------------------------------------|
| `tree-sitter-ada` |  |
| `tree-sitter-agda` |  |
| `tree-sitter-apex` |  |
| `tree-sitter-bash` |  |
| `tree-sitter-beancount` |  |
| `tree-sitter-c` |  |
| `tree-sitter-c-sharp` |  |
| `tree-sitter-capnp` |  |
| `tree-sitter-clojure` |  |
| `tree-sitter-cmake` |  |
| `tree-sitter-comment` |  |
| `tree-sitter-commonlisp` |  |
| `tree-sitter-cpp` |  |
| `tree-sitter-css` |  |
| `tree-sitter-cuda` |  |
| `tree-sitter-d` |  |
| `tree-sitter-dart` |  |
| `tree-sitter-dockerfile` |  |
| `tree-sitter-dot` |  |
| `tree-sitter-elisp` |  |
| `tree-sitter-elixir` |  |
| `tree-sitter-elm` |  |
| `tree-sitter-embedded-template` |  |
| `tree-sitter-eno` |  |
| `tree-sitter-erlang` |  |
| `tree-sitter-fennel` |  |
| `tree-sitter-fish` |  |
| `tree-sitter-formula` |  |
| `tree-sitter-fortran` |  |
| `tree-sitter-gitattributes` |  |
| `tree-sitter-gitignore` |  |
| `tree-sitter-gleam` |  |
| `tree-sitter-glsl` |  |
| `tree-sitter-go` |  |
| `tree-sitter-go-mod` |  |
| `tree-sitter-go-work` |  |
| `tree-sitter-graphql` |  |
| `tree-sitter-hack` |  |
| `tree-sitter-haskell` |  |
| `tree-sitter-hcl` |  |
| `tree-sitter-hocon` |  |
| `tree-sitter-html` |  |
| `tree-sitter-java` |  |
| `tree-sitter-javascript` |  |
| `tree-sitter-jq` |  |
| `tree-sitter-json` |  |
| `tree-sitter-json5` |  |
| `tree-sitter-julia` |  |
| `tree-sitter-kotlin` |  |
| `tree-sitter-lalrpop` |  |
| `tree-sitter-latex` |  |
| `tree-sitter-lean` |  |
| `tree-sitter-llvm` |  |
| `tree-sitter-llvm-mir` |  |
| `tree-sitter-lua` |  |
| `tree-sitter-m68k` |  |
| `tree-sitter-make` |  |
| `tree-sitter-markdown` |  |
| `tree-sitter-meson` |  |
| `tree-sitter-nix` |  |
| `tree-sitter-nginx` |  |
| `tree-sitter-nim` |  |
| `tree-sitter-objc` |  |
| `tree-sitter-ocaml` |  |
| `tree-sitter-ohm` |  |
| `tree-sitter-org` |  |
| `tree-sitter-p4` |  |
| `tree-sitter-pascal` |  |
| `tree-sitter-perl` |  |
| `tree-sitter-pgn` |  |
| `tree-sitter-php` |  |
| `tree-sitter-pod` |  |
| `tree-sitter-proto` |  |
| `tree-sitter-python` |  |
| `tree-sitter-qmljs` |  |
| `tree-sitter-query` |  |
| `tree-sitter-r` |  |
| `tree-sitter-racket` |  |
| `tree-sitter-rasi` |  |
| `tree-sitter-re2c` |  |
| `tree-sitter-regex` |  |
| `tree-sitter-rego` |  |
| `tree-sitter-rst` |  |
| `tree-sitter-ruby` |  |
| `tree-sitter-rust` |  |
| `tree-sitter-tact` |  |
| `tree-sitter-scala` |  |
| `tree-sitter-scheme` |  |
| `tree-sitter-scss` |  |
| `tree-sitter-sexp` |  |
| `tree-sitter-smali` |  |
| `tree-sitter-sourcepawn` |  |
| `tree-sitter-sparql` |  |
| `tree-sitter-sql` |  |
| `tree-sitter-sql-bigquery` |  |
| `tree-sitter-sqlite` |  |
| `tree-sitter-ssh-client-config` |  |
| `tree-sitter-svelte` |  |
| `tree-sitter-swift` |  |
| `tree-sitter-tablegen` |  |
| `tree-sitter-thrift` |  |
| `tree-sitter-toml` |  |
| `tree-sitter-turtle` |  |
| `tree-sitter-twig` |  |
| `tree-sitter-typescript` |  |
| `tree-sitter-verilog` |  |
| `tree-sitter-vhdl` |  |
| `tree-sitter-vue` |  |
| `tree-sitter-wast` |  |
| `tree-sitter-wat` |  |
| `tree-sitter-wgsl` |  |
| `tree-sitter-yaml` |  |
| `tree-sitter-yang` |  |
| `tree-sitter-zig` |  |
# API Tour
```java
class Main {
public static void main(String[] args) {
String jsonSource = "[1, null]";
TSParser parser = new TSParser();
TSLanguage json = new TreeSitterJson();
// set language parser
parser.setLanguage(json);
// parser with string input
parser.parseString(null, jsonSource);
parser.reset();
// or parser with encoding
parser.parseStringEncoding(null, JSON_SRC, TSInputEncoding.TSInputEncodingUTF8);
parser.reset();
// or parser with custom reader
byte[] buffer = new byte[1024];
TSReader reader = (buf, offset, position) -> {
if(offset >= jsonSource.length()){
return 0;
}
ByteBuffer charBuffer = ByteBuffer.wrap(buf);
charBuffer.put(jsonSource.getBytes());
return jsonSource.length();
};
TSTree tree = parser.parse(buffer, null, reader, TSInputEncoding.TSInputEncodingUTF8);
// traverse the AST tree with DOM like APIs
TSNode rootNode = tree.getRootNode();
TSNode arrayNode = rootNode.getNamedChild(0);
// or travers the AST with cursor
TSTreeCursor rootCursor = new TSTreeCursor(rootNode);
rootCursor.gotoFirstChild();
// or query the AST with S-expression
TreeSitterQuery query = new TSQuery(json, "((document) @root)");
TSQueryCursor cursor = new TSQueryCursor();
cursor.exec(query, rootNode);
SQueryMatch match = new TSQueryMatch();
while(cursor.nextMatch(match)){
// do something with the match
}
// debug the parser with a logger
TSLogger logger = (type, message) -> {
System.out.println(message);
};
parser.setLogger(logger);
// or output the AST tree as DOT graph
File dotFile = File.createTempFile("json", ".dot");
parser.printDotGraphs(dotFile);
}
}
```
# How to add new parser
Use generator script to generate a new parser subproject.
You can edit the `build.gradle` to customize the native library build process.
```console
./gradlew gen --parser-name= --parser-version= --parser-zip=
# build native library
./gradlew tree-sitter-:buildNative
# run tests
./gradlew tree-sitter-:test
```
# TODO
- [ ] Utilize test cases in parser's repository