{"id":15069106,"url":"https://github.com/seart-group/java-tree-sitter","last_synced_at":"2025-07-08T21:33:39.620Z","repository":{"id":81433656,"uuid":"572068325","full_name":"seart-group/java-tree-sitter","owner":"seart-group","description":"Java bindings for tree-sitter","archived":false,"fork":false,"pushed_at":"2025-07-03T03:42:04.000Z","size":1826,"stargazers_count":47,"open_issues_count":6,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-03T04:34:19.211Z","etag":null,"topics":["incremental-parsing","java-11","jni","language-agnostic","library","linux-support","mac-support","mit-license","parser","parsing","parsing-library","symbolic-expressions","syntax-tree","tree-sitter","tree-sitter-binding","xml-export"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"serenadeai/java-tree-sitter","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/seart-group.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-11-29T13:42:38.000Z","updated_at":"2025-07-03T03:41:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"447ae680-80d9-4db4-b66a-bb37dadfe9a8","html_url":"https://github.com/seart-group/java-tree-sitter","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"purl":"pkg:github/seart-group/java-tree-sitter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seart-group%2Fjava-tree-sitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seart-group%2Fjava-tree-sitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seart-group%2Fjava-tree-sitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seart-group%2Fjava-tree-sitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seart-group","download_url":"https://codeload.github.com/seart-group/java-tree-sitter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seart-group%2Fjava-tree-sitter/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264353079,"owners_count":23595029,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["incremental-parsing","java-11","jni","language-agnostic","library","linux-support","mac-support","mit-license","parser","parsing","parsing-library","symbolic-expressions","syntax-tree","tree-sitter","tree-sitter-binding","xml-export"],"created_at":"2024-09-25T01:40:30.393Z","updated_at":"2025-07-08T21:33:39.601Z","avatar_url":"https://github.com/seart-group.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# java-tree-sitter \u0026middot; [![MIT license](https://img.shields.io/github/license/seart-group/java-tree-sitter)](https://github.com/seart-group/java-tree-sitter/blob/master/LICENSE) [![javadoc](https://javadoc.io/badge2/ch.usi.si.seart/java-tree-sitter/javadoc.svg)](https://javadoc.io/doc/ch.usi.si.seart/java-tree-sitter) [![Maven Central](https://img.shields.io/maven-central/v/ch.usi.si.seart/java-tree-sitter)](https://central.sonatype.com/artifact/ch.usi.si.seart/java-tree-sitter)\n\nJava bindings for [tree-sitter](https://tree-sitter.github.io/tree-sitter/).\nOriginally developed by [serenadeai](https://github.com/serenadeai).\n\nThis fork was originally created to simplify integration into [DL4SE](https://github.com/seart-group/DL4SE).\nAlong the way, the project evolved to include useful features from [other forks](https://github.com/jakobkhansen/java-tree-sitter),\nwhile introducing support for those that were completely absent from the original project.\nHighlights include:\n\n- Incremental abstract syntax tree edits\n- Parser and lexer debugging through loggers\n- APIs for querying parsed abstract syntax trees\n- Support for both macOS and Linux out of the box\n- A wide range of languages [supported](.gitmodules) out of the box\n- Streamlined native library construction, packaging and runtime loading\n- Safer interop with native code to minimize risks of segmentation faults\n- Direct mapping between the abstract syntax tree nodes and the source code contents\n- Multiple export formats: DOT, XML, [symbolic expression](https://en.wikipedia.org/wiki/S-expression) and human-readable syntax trees\n- Various other quality-of-life improvements\n\nOur end-goal is to offer all features that are available in the official tree-sitter bindings,\nfeatures that one might expect from [py-tree-sitter](https://github.com/tree-sitter/py-tree-sitter) or\n[node-tree-sitter](https://github.com/tree-sitter/node-tree-sitter).\n\n## Local development\n\nRecursively clone the project with submodules:\n\n```shell\ngit clone https://github.com/seart-group/java-tree-sitter.git --recursive\n```\n\nOr clone first and update the submodules afterward:\n\n```shell   \ngit clone https://github.com/seart-group/java-tree-sitter.git\ngit submodule update --init --recursive  \n# or: git submodule init \u0026\u0026 git submodule update\n```\n\n## Building dependency locally\n\nTo build the project for development purposes, all one has to do is run the following:\n\n```shell\nmvn clean package\n```\n\nThis will generate both the header files in `lib`, as well as the shared library produced by `build.py`.\nFor it to work, you must have the following installed:\n\n| Dependency | Version |\n|:-----------|--------:|\n| Java       |      11 |\n| Maven      |     3.9 |\n| Python     |    3.10 |\n| Docker     |      23 |\n\n## Adding dependency to project\n\nTo use in your own Maven project, include the following in your POM file:\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003ech.usi.si.seart\u003c/groupId\u003e\n  \u003cartifactId\u003ejava-tree-sitter\u003c/artifactId\u003e\n  \u003cversion\u003e1.12.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## Example usage\n\nFirst, load the shared object somewhere in your application:\n\n```java\nimport ch.usi.si.seart.treesitter.*;\n\npublic class Example {\n\n    static {\n        LibraryLoader.load();\n    }\n}\n```\n\nThen you can create a `Parser` initialized to a `Language`, and use it to parse a string of source code:\n\n```java\nimport ch.usi.si.seart.treesitter.*;\n\npublic class Example {\n    \n    // init omitted...\n\n    public static void main(String[] args) {\n        try (\n            Parser parser = Parser.getFor(Language.PYTHON);\n            Tree tree = parser.parse(\"def foo(bar, baz):\\n  print(bar)\\n  print(baz)\")\n        ) {\n            Node root = tree.getRootNode();\n            assert root.getChildCount() == 1;\n            assert root.getType().equals(\"module\");\n            assert root.getStartByte() == 0;\n            assert root.getEndByte() == 44;\n            Node function = root.getChild(0);\n            assert function.getType().equals(\"function_definition\");\n            assert function.getChildCount() == 5;\n        } catch (Exception ex) {\n            // ...\n        }\n    }\n}\n```\n\nUse `TreeCursor` instances to traverse trees, as it is more efficient than both manual traversal, and through `Node` iterators:\n\n```java\nimport ch.usi.si.seart.treesitter.*;\n\npublic class Example {\n\n    // init omitted...\n\n    public static void main(String[] args) {\n        String type;\n        try (\n            Parser parser = Parser.getFor(Language.PYTHON);\n            Tree tree = parser.parse(\"def foo(bar, baz):\\n  print(bar)\\n  print(baz)\");\n            TreeCursor cursor = tree.getRootNode().walk()\n        ) {\n            type = cursor.getCurrentTreeCursorNode().getType();\n            assert type.equals(\"module\");\n            cursor.gotoFirstChild();\n            type = cursor.getCurrentTreeCursorNode().getType();\n            assert type.equals(\"function_definition\");\n            cursor.gotoFirstChild();\n            type = cursor.getCurrentTreeCursorNode().getType();\n            assert type.equals(\"def\");\n            cursor.gotoNextSibling();\n            cursor.gotoParent();\n        } catch (Exception ex) {\n            // ...\n        }\n    }\n}\n```\n\nThe `Query` class can be used to specify subtrees to match, while the `QueryCursor` can be used to iterate over matched nodes:\n\n```java\nimport ch.usi.si.seart.treesitter.*;\n\npublic class Example {\n\n    // init omitted...\n\n    public static void main(String[] args) {\n        Language language = Language.PYTHON;\n        try (\n            Query query = Query.getFor(language, \"(identifier) @target\");\n            Parser parser = Parser.getFor(language);\n            Tree tree = parser.parse(\"def foo(bar, baz):\\n  print(bar)\\n  print(baz)\");\n            QueryCursor cursor = tree.getRootNode().walk(query)\n        ) {\n            int count = 0;\n            for (QueryMatch match: cursor) count++;\n            assert count == 7;\n        } catch (Exception ex) {\n            // ...\n        }\n    }\n}\n```\n\nWe also provide a way to print the syntax tree, similar to the [online playground](https://tree-sitter.github.io/tree-sitter/playground):\n\n```java\nimport ch.usi.si.seart.treesitter.*;\nimport ch.usi.si.seart.treesitter.printer.*;\n\npublic class Example {\n\n    // init omitted...\n\n    public static void main(String[] args) {\n        try (\n            Parser parser = Parser.getFor(Language.PYTHON);\n            Tree tree = parser.parse(\"print(\\\"hi\\\")\");\n            TreeCursor cursor = tree.getRootNode().walk()\n        ) {\n            SyntaxTreePrinter printer = new SyntaxTreePrinter(cursor);\n            String actual = printer.print();\n            String expected =\n                \"module [0:0] - [0:11]\\n\" +\n                \"  expression_statement [0:0] - [0:11]\\n\" +\n                \"    call [0:0] - [0:11]\\n\" +\n                \"      function: identifier [0:0] - [0:5]\\n\" +\n                \"      arguments: argument_list [0:5] - [0:11]\\n\" +\n                \"        string [0:6] - [0:10]\\n\";\n            assert expected.equals(actual);\n        } catch (Exception ex) {\n            // ...\n        }\n    }\n}\n```\n\nIf you would like to debug the parsing process, you can attach loggers directly to a `Parser`:\n\n```java\nimport ch.usi.si.seart.treesitter.*;\nimport org.slf4j.*;\n\npublic class Example {\n\n    // init omitted...\n\n    public static void main(String[] args) {\n        Logger logger = LoggerFactory.getLogger(Example.class);\n        try (Parser parser = Parser.getFor(Language.PYTHON)) {\n            parser.setLogger(logger);\n            parser.parse(\"pass\").close();\n        } catch (Exception ex) {\n            // ...\n        }\n    }\n}\n```\n\nFor more usage examples, take a look at the [tests](src/test/java/ch/usi/si/seart/treesitter).\nYou can also refer to the full documentation [here](https://javadoc.io/doc/ch.usi.si.seart/java-tree-sitter/latest/index.html).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseart-group%2Fjava-tree-sitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseart-group%2Fjava-tree-sitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseart-group%2Fjava-tree-sitter/lists"}