Package io.github.treesitter.jtreesitter
package io.github.treesitter.jtreesitter
Java bindings to the tree-sitter parsing library.
Requirements
- JDK 22 (for the Foreign Function and Memory API )
- Tree-sitter shared library
- Generated bindings for languages
- Shared libraries for languages
Basic Usage
Language language = new Language(TreeSitterJava.language());
try (Parser parser = new Parser(language)) {
try (Tree tree = parser.parse("void main() {}", InputEncoding.UTF_8).orElseThrow()) {
Node rootNode = tree.getRootNode();
assert rootNode.getType().equals("program");
assert rootNode.getStartPoint().column() == 0;
assert rootNode.getEndPoint().column() == 14;
}
}
Library Loading
There are three ways to load the shared libraries:-
The libraries can be installed in the OS-specific library search path or in
java.library.path
. The search path can be amended using theLD_LIBRARY_PATH
environment variable on Linux,DYLD_LIBRARY_PATH
on macOS, orPATH
on Windows. The libraries will be loaded automatically bySymbolLookup.libraryLookup(String, Arena)
RESTRICTED. -
The libraries can be loaded manually by calling
System.loadLibrary(String)
, if the library is installed injava.library.path
, orSystem.load(String)
. -
The libraries can be loaded manually by registering a custom implementation of
NativeLibraryLookup
. This can be used, for example, to load libraries from inside a JAR file.
-
ClassDescriptionAn edit to a text document.The encoding of source code.A class that defines how to parse a particular language.The metadata associated with a Language.The Semantic Version of the Language.A function that logs parsing results.The type of a log message.A class that is used to look up valid symbols in a specific parse state.A class that pairs a symbol ID with its name.An interface implemented by clients that wish to customize the
SymbolLookup
used for the tree-sitter native library.A single node within a syntax tree.A function that retrieves a chunk of text at a given byte offset and point.A class that is used to produce a syntax tree from source code.Deprecated.A class representing the parser options.A class representing the current state of the parser.A position in a text document in terms of rows and columns.A class that represents a set of patterns which match nodes in a syntax tree.ANode
that was captured with a certain capture name.A class that can be used to execute a query on a syntax tree.A class representing the query cursor options.A class representing the current state of the query cursor.Any error that occurred while instantiating aQuery
.A capture name error.A field name error.A node type error.A query predicate error.A pattern structure error.A query syntax error.A match that corresponds to a certain pattern in the query.A query predicate that associates conditions (or arbitrary metadata) with a pattern.Handles the following predicates:
#any-of?
,#not-any-of?
Handles the following predicates:
#eq?
,#not-eq?
,#any-eq?
,#any-not-eq?
Handles the following predicates:
#match?
,#not-match?
,#any-match?
,#any-not-match?
An argument to aQueryPredicate
.A capture argument (@value
).A literal string argument ("value"
).A range of positions in a text document, both in terms of bytes and of row-column points.A class that represents a syntax tree.A class that can be used to efficiently walk a syntax tree.Specifies that the value is of an unsigned data type.
Parser.Options
instead.