Package io.github.treesitter.jtreesitter
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.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final record
A class that pairs a symbol ID with its name. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
short
Get the current symbol ID.The current symbol name.Get the current language of the lookahead iterator.boolean
hasNext()
Check if the lookahead iterator has more symbols.names()
Iterate over the symbol names.next()
Advance the lookahead iterator to the next symbol.boolean
reset
(short state) Reset the lookahead iterator to the given state.boolean
Reset the lookahead iterator to the given state and another language.symbols()
Iterate over the symbol IDs.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Iterator
forEachRemaining
-
Method Details
-
getLanguage
Get the current language of the lookahead iterator. -
getCurrentSymbol
Get the current symbol ID.- API Note:
- The ID of the ERROR symbol is equal to
-1
.
-
getCurrentSymbolName
The current symbol name.- API Note:
- Newly created lookahead iterators will contain the ERROR symbol.
-
reset
Reset the lookahead iterator to the given state.- Returns:
true
if the iterator was reset successfully orfalse
if it failed.
-
reset
-
hasNext
public boolean hasNext()Check if the lookahead iterator has more symbols.- Specified by:
hasNext
in interfaceIterator<LookaheadIterator.Symbol>
-
next
Advance the lookahead iterator to the next symbol.- Specified by:
next
in interfaceIterator<LookaheadIterator.Symbol>
- Throws:
NoSuchElementException
- If there are no more symbols.
-
symbols
-
names
-
close
- Specified by:
close
in interfaceAutoCloseable
- Throws:
RuntimeException
-