Class LookaheadIterator

java.lang.Object
io.github.treesitter.jtreesitter.LookaheadIterator
All Implemented Interfaces:
AutoCloseable, Iterator<LookaheadIterator.Symbol>

@NullMarked public final class LookaheadIterator extends Object implements AutoCloseable, Iterator<LookaheadIterator.Symbol>
A class that is used to look up valid symbols in a specific parse state.

Lookahead iterators can be useful to generate suggestions and improve syntax error diagnostics.
To get symbols valid in an ERROR node, use the lookahead iterator on its first leaf node state.
For MISSING nodes, a lookahead iterator created on the previous non-extra leaf node may be appropriate.

See Also:
  • Method Details

    • getLanguage

      public Language getLanguage()
      Get the current language of the lookahead iterator.
    • getCurrentSymbol

      @Unsigned public short getCurrentSymbol()
      Get the current symbol ID.
      API Note:
      The ID of the ERROR symbol is equal to -1.
    • getCurrentSymbolName

      public String getCurrentSymbolName()
      The current symbol name.
      API Note:
      Newly created lookahead iterators will contain the ERROR symbol.
    • reset

      public boolean reset(@Unsigned short state)
      Reset the lookahead iterator to the given state.
      Returns:
      true if the iterator was reset successfully or false if it failed.
    • reset

      public boolean reset(@Unsigned short state, Language language)
      Reset the lookahead iterator to the given state and another language.
      Returns:
      true if the iterator was reset successfully or false if it failed.
    • hasNext

      public boolean hasNext()
      Check if the lookahead iterator has more symbols.
      Specified by:
      hasNext in interface Iterator<LookaheadIterator.Symbol>
    • next

      Advance the lookahead iterator to the next symbol.
      Specified by:
      next in interface Iterator<LookaheadIterator.Symbol>
      Throws:
      NoSuchElementException - If there are no more symbols.
    • symbols

      @Unsigned public Stream<Short> symbols()
      Iterate over the symbol IDs.
      Implementation Note:
      Calling this method will reset the iterator to its original state.
    • names

      public Stream<String> names()
      Iterate over the symbol names.
      Implementation Note:
      Calling this method will reset the iterator to its original state.
    • close

      public void close() throws RuntimeException
      Specified by:
      close in interface AutoCloseable
      Throws:
      RuntimeException