Coder Social home page Coder Social logo

java-tree-sitter's Introduction

java-tree-sitter

Java bindings for tree-sitter.

Preparing

Recursively clone the project with submodules:

git clone https://github.com/serenadeai/java-tree-sitter.git --recursive

Or clone first and update the submodules then:

git clone https://github.com/serenadeai/java-tree-sitter.git
git submodule update --init --recursive  
# or:  git submodule init && git submodule update

Installing

To add this library to a Gradle project:

allprojects {
    repositories {
        maven { 
            url 'https://jitpack.io'
        }
    }
}

dependencies {
    implementation "com.github.serenadeai:java-tree-sitter:1.1.2"
}

Building

Before you can start using java-tree-sitter, you need to build a shared library that Java can load using the build.py script. The first argument is the output file (libjava-tree-sitter by default), followed by all of the tree-sitter repositories (already downloaded) that you want to include:

./build.py -o libjava-tree-sitter path-to-tree-sitter-css path-to-tree-sitter-python ...

Examples

First, load the shared object somewhere in your application:

public class App {
  static {
    // or on a Mac, libjava-tree-sitter.dylib
    System.load("./path/to/libjava-tree-sitter.so");
  }
}

Then, you can create a Parser, set the language, and parse a string:

try (Parser parser = new Parser()) {
  parser.setLanguage(Languages.python());
  try (Tree tree = parser.parseString("def foo(bar, baz):\n  print(bar)\n  print(baz)")) {
    Node root = tree.getRootNode();
    assertEquals(1, root.getChildCount());
    assertEquals("module", root.getType());
    assertEquals(0, root.getStartByte());
    assertEquals(44, root.getEndByte());

    Node function = root.getChild(0);
    assertEquals("function_definition", function.getType());
    assertEquals(5, function.getChildCount());
  }
}

For debugging, it can be helpful to see a string of the tree:

try (Parser parser = new Parser()) {
  parser.setLanguage(Languages.python());
  try (Tree tree = parser.parseString("print(\"hi\")")) {
    assertEquals(
      "(module (expression_statement (call function: (identifier) arguments: (argument_list (string)))))",
      tree.getRootNode().getNodeString()
    );
  }
}

If you're going to be traversing a tree, then you can use the walk method, which is much more efficient than the above getters:

try (Parser parser = new Parser()) {
  parser.setLanguage(Languages.python());
  try (Tree tree = parser.parseString("def foo(bar, baz):\n  print(bar)\n  print(baz)")) {
    try (TreeCursor cursor = tree.getRootNode().walk()) {
      assertEquals("module", cursor.getCurrentTreeCursorNode().getType());
      cursor.gotoFirstChild();
      assertEquals("function_definition", cursor.getCurrentTreeCursorNode().getType());
      cursor.gotoFirstChild();
      assertEquals("def", cursor.getCurrentTreeCursorNode().getType());
      cursor.gotoNextSibling();
      cursor.gotoParent();
    }
  }
}

For more examples, see the tests in src/test/java/ai/serenade/treesitter.

java-tree-sitter's People

Contributors

tmacwill avatar symbolk avatar sam-dixon avatar tautologico avatar enginuity avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.