Class Language

java.lang.Object
io.github.treesitter.jtreesitter.Language
All Implemented Interfaces:
Cloneable

@NullMarked public final class Language extends Object implements Cloneable
A class that defines how to parse a particular language.
  • Field Details

    • LANGUAGE_VERSION

      @Unsigned public static final int LANGUAGE_VERSION
      The latest ABI version that is supported by the current version of the library.
      API Note:
      The Tree-sitter library is generally backwards-compatible with languages generated using older CLI versions, but is not forwards-compatible.
    • MIN_COMPATIBLE_LANGUAGE_VERSION

      @Unsigned public static final int MIN_COMPATIBLE_LANGUAGE_VERSION
      The earliest ABI version that is supported by the current version of the library.
  • Constructor Details

  • Method Details

    • load

      public static Language load(SymbolLookup symbols, String language) throws RuntimeException
      Load a language by looking for its function in the given symbols.

      Example

      String library = System.mapLibraryName("tree-sitter-java");
      SymbolLookup symbols = SymbolLookup.libraryLookup(library, Arena.global());
      Language language = Language.load(symbols, "tree_sitter_java");
      

      The Arena used to load the language must not be closed while the language is being used.

      Throws:
      UnsatisfiedLinkError - If the language symbol could not be found.
      RuntimeException - If the language could not be loaded.
      Since:
      0.23.1
    • getAbiVersion

      @Unsigned public int getAbiVersion()
      Get the ABI version number for this language.

      This version number is used to ensure that languages were generated by a compatible version of Tree-sitter.

      Since:
      0.25.0
    • getVersion

      @Deprecated(since="0.25.0", forRemoval=true) @Unsigned public int getVersion()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use getAbiVersion() instead.
      Get the ABI version number for this language.
    • getName

      public @Nullable String getName()
      Get the name of this language, if available.
    • getMetadata

      public @Nullable LanguageMetadata getMetadata()
      Get the metadata for this language, if available.
      Since:
      0.25.0
      API Note:
      This information is generated by the Tree-sitter CLI and relies on the language author providing the correct metadata in the language's tree-sitter.json file.
    • getSymbolCount

      @Unsigned public int getSymbolCount()
      Get the number of distinct node types in this language.
    • getStateCount

      @Unsigned public int getStateCount()
      Get the number of valid states in this language
    • getFieldCount

      @Unsigned public int getFieldCount()
      Get the number of distinct field names in this language
    • getSupertypes

      @Unsigned public short[] getSupertypes()
      Get all supertype symbols for the language.
      Since:
      0.25.0
    • getSubtypes

      @Unsigned public short[] getSubtypes(@Unsigned short supertype)
      Get all symbols for a given supertype symbol.
      Since:
      0.25.0
      See Also:
    • getSymbolName

      public @Nullable String getSymbolName(@Unsigned short symbol)
      Get the node type for the given numerical ID.
    • getSymbolForName

      @Unsigned public short getSymbolForName(String name, boolean isNamed)
      Get the numerical ID for the given node type, or 0 if not found.
    • isNamed

      public boolean isNamed(@Unsigned short symbol)
      Check if the node for the given numerical ID is named.
      See Also:
    • isVisible

      public boolean isVisible(@Unsigned short symbol)
      Check if the node for the given numerical ID is visible.
    • isSupertype

      public boolean isSupertype(@Unsigned short symbol)
      Check if the node for the given numerical ID is a supertype.
      Since:
      0.24.0
    • getFieldNameForId

      public @Nullable String getFieldNameForId(@Unsigned short id)
      Get the field name for the given numerical id.
    • getFieldIdForName

      @Unsigned public short getFieldIdForName(String name)
      Get the numerical ID for the given field name.
    • nextState

      @Unsigned public short nextState(@Unsigned short state, @Unsigned short symbol)
      Get the next parse state.

      short state = language.nextState(node.getParseState(), node.getGrammarSymbol());
      

      Combine this with lookaheadIterator(state) to generate completion suggestions or valid symbols in ERROR nodes.

    • lookaheadIterator

      public LookaheadIterator lookaheadIterator(@Unsigned short state) throws IllegalArgumentException
      Create a new lookahead iterator for the given parse state.
      Throws:
      IllegalArgumentException - If the state is invalid for this language.
    • query

      @Deprecated(since="0.25.0") public Query query(String source) throws QueryError
      Deprecated.
      Use the Query constructor instead.
      Create a new query from a string containing one or more S-expression patterns.
      Throws:
      QueryError - If an error occurred while creating the query.
    • clone

      public Language clone()
      Get another reference to the language.
      Since:
      0.24.0
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object