Skip to content

hcengineering/tree-sitter-ng

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tree Sitter NG

Next generation Tree Sitter Java binding.

Maven Central Maven Central

Getting started

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

// Gradle
dependencies {
    // add tree sitter
    implementation 'io.github.bonede:tree-sitter:0.24.4'
    // 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.24.4</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 Maven Central
tree-sitter-agda Maven Central
tree-sitter-apex Maven Central
tree-sitter-bash Maven Central
tree-sitter-beancount Maven Central
tree-sitter-c Maven Central
tree-sitter-c-sharp Maven Central
tree-sitter-capnp Maven Central
tree-sitter-clojure Maven Central
tree-sitter-cmake Maven Central
tree-sitter-comment Maven Central
tree-sitter-commonlisp Maven Central
tree-sitter-cpp Maven Central
tree-sitter-css Maven Central
tree-sitter-cuda Maven Central
tree-sitter-d Maven Central
tree-sitter-dart Maven Central
tree-sitter-dockerfile Maven Central
tree-sitter-dot Maven Central
tree-sitter-elisp Maven Central
tree-sitter-elixir Maven Central
tree-sitter-elm Maven Central
tree-sitter-embedded-template Maven Central
tree-sitter-eno Maven Central
tree-sitter-erlang Maven Central
tree-sitter-fennel Maven Central
tree-sitter-fish Maven Central
tree-sitter-formula Maven Central
tree-sitter-fortran Maven Central
tree-sitter-gitattributes Maven Central
tree-sitter-gitignore Maven Central
tree-sitter-gleam Maven Central
tree-sitter-glsl Maven Central
tree-sitter-go Maven Central
tree-sitter-go-mod Maven Central
tree-sitter-go-work Maven Central
tree-sitter-graphql Maven Central
tree-sitter-hack Maven Central
tree-sitter-haskell Maven Central
tree-sitter-hcl Maven Central
tree-sitter-hocon Maven Central
tree-sitter-html Maven Central
tree-sitter-java Maven Central
tree-sitter-javascript Maven Central
tree-sitter-jq Maven Central
tree-sitter-json Maven Central
tree-sitter-json5 Maven Central
tree-sitter-julia Maven Central
tree-sitter-kotlin Maven Central
tree-sitter-lalrpop Maven Central
tree-sitter-latex Maven Central
tree-sitter-lean Maven Central
tree-sitter-llvm Maven Central
tree-sitter-llvm-mir Maven Central
tree-sitter-lua Maven Central
tree-sitter-m68k Maven Central
tree-sitter-make Maven Central
tree-sitter-markdown Maven Central
tree-sitter-meson Maven Central
tree-sitter-nix Maven Central
tree-sitter-nginx Maven Central
tree-sitter-nim Maven Central
tree-sitter-objc Maven Central
tree-sitter-ocaml Maven Central
tree-sitter-ohm Maven Central
tree-sitter-org Maven Central
tree-sitter-p4 Maven Central
tree-sitter-pascal Maven Central
tree-sitter-perl Maven Central
tree-sitter-pgn Maven Central
tree-sitter-php Maven Central
tree-sitter-pod Maven Central
tree-sitter-proto Maven Central
tree-sitter-python Maven Central
tree-sitter-qmljs Maven Central
tree-sitter-query Maven Central
tree-sitter-r Maven Central
tree-sitter-racket Maven Central
tree-sitter-rasi Maven Central
tree-sitter-re2c Maven Central
tree-sitter-regex Maven Central
tree-sitter-rego Maven Central
tree-sitter-rst Maven Central
tree-sitter-ruby Maven Central
tree-sitter-rust Maven Central
tree-sitter-tact Maven Central
tree-sitter-scala Maven Central
tree-sitter-scheme Maven Central
tree-sitter-scss Maven Central
tree-sitter-sexp Maven Central
tree-sitter-smali Maven Central
tree-sitter-sourcepawn Maven Central
tree-sitter-sparql Maven Central
tree-sitter-sql Maven Central
tree-sitter-sql-bigquery Maven Central
tree-sitter-sqlite Maven Central
tree-sitter-ssh-client-config Maven Central
tree-sitter-svelte Maven Central
tree-sitter-swift Maven Central
tree-sitter-tablegen Maven Central
tree-sitter-thrift Maven Central
tree-sitter-toml Maven Central
tree-sitter-turtle Maven Central
tree-sitter-twig Maven Central
tree-sitter-typescript Maven Central
tree-sitter-verilog Maven Central
tree-sitter-vhdl Maven Central
tree-sitter-vue Maven Central
tree-sitter-wast Maven Central
tree-sitter-wat Maven Central
tree-sitter-wgsl Maven Central
tree-sitter-yaml Maven Central
tree-sitter-yang Maven Central
tree-sitter-zig Maven Central

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

Next generation Tree Sitter Java binding.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 28.0%
  • C 19.8%
  • CSS 10.9%
  • Java 8.2%
  • Go 7.6%
  • Python 6.5%
  • Other 19.0%