Skip to content

bonede/tree-sitter-ng

Folders and files

Image for: Folders and files
NameName
Last commit message
Last commit date

Latest commit

Image for: Latest commit
 

History

Image for: History
180 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Image for: Repository files navigation

Tree Sitter NG

Next generation Tree Sitter Java binding.

Getting started

Add dependencies to your build.gradle or pom.xml.

// 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'
}
<!-- Maven -->
<dpendencies>
    <!-- add tree sitter -->
    <dependency>
        <groupId>io.github.bonede</groupId>
        <artifactId>tree-sitter</artifactId>
        <version>0.25.3</version>
    </dependency>
    <!-- add json parser -->
    <dependency>
        <groupId>io.github.bonede</groupId>
        <artifactId>tree-sitter-json</artifactId>
        <version>0.24.8</version>
    </dependency>
</dpendencies>

Start hacking!

// 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 coverage.
  • Easy to bootstrap cross compiling environments powered by Zig.
  • 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

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.

./gradlew gen --parser-name=<parser name> --parser-version=<parser version> --parser-zip=<parer zip download url>
# build native library
./gradlew tree-sitter-<parser name>:buildNative
# run tests
./gradlew tree-sitter-<parser name>:test

TODO

  • Utilize test cases in parser's repository

About

Image for: About

Next generation Tree Sitter Java binding.

Topics

Resources

License

Stars

Watchers

Forks

Packages

Image for: Packages 0
No packages published

Contributors 6

Image for: Contributors 6