Class ListTokenSource
java.lang.Object
org.graalvm.shadowed.org.antlr.v4.runtime.ListTokenSource
- All Implemented Interfaces:
TokenSource
Provides an implementation of
TokenSource as a wrapper around a list
of Token objects.
If the final token in the list is an Token.EOF token, it will be used
as the EOF token for every call to nextToken() after the end of the
list is reached. Otherwise, an EOF token will be created.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected TokenThis field caches the EOF token for the token source.protected intThe index intotokensof token to return by the next call tonextToken().The wrapped collection ofTokenobjects to return. -
Constructor Summary
ConstructorsConstructorDescriptionListTokenSource(List<? extends Token> tokens) Constructs a newListTokenSourceinstance from the specified collection ofTokenobjects.ListTokenSource(List<? extends Token> tokens, String sourceName) Constructs a newListTokenSourceinstance from the specified collection ofTokenobjects and source name. -
Method Summary
Modifier and TypeMethodDescriptionintGet the index into the current line for the current position in the input stream.Get theCharStreamfrom which this token source is currently providing tokens.intgetLine()Get the line number for the current position in the input stream.Gets the name of the underlying input source.TokenFactory<?> Gets theTokenFactorythis token source is currently using for creatingTokenobjects from the input.Return aTokenobject from your input stream (usually aCharStream).voidsetTokenFactory(TokenFactory<?> factory) Set theTokenFactorythis token source should use for creatingTokenobjects from the input.
-
Field Details
-
tokens
-
i
protected int iThe index intotokensof token to return by the next call tonextToken(). The end of the input is indicated by this value being greater than or equal to the number of items intokens. -
eofToken
This field caches the EOF token for the token source.
-
-
Constructor Details
-
ListTokenSource
Constructs a newListTokenSourceinstance from the specified collection ofTokenobjects.- Parameters:
tokens- The collection ofTokenobjects to provide as aTokenSource.- Throws:
NullPointerException- iftokensisnull
-
ListTokenSource
Constructs a newListTokenSourceinstance from the specified collection ofTokenobjects and source name.- Parameters:
tokens- The collection ofTokenobjects to provide as aTokenSource.sourceName- The name of theTokenSource. If this value isnull,getSourceName()will attempt to infer the name from the nextToken(or the previous token if the end of the input has been reached).- Throws:
NullPointerException- iftokensisnull
-
-
Method Details
-
getCharPositionInLine
public int getCharPositionInLine()Get the index into the current line for the current position in the input stream. The first character on a line has position 0.- Specified by:
getCharPositionInLinein interfaceTokenSource- Returns:
- The line number for the current position in the input stream, or -1 if the current token source does not track character positions.
-
nextToken
Return aTokenobject from your input stream (usually aCharStream). Do not fail/return upon lexing error; keep chewing on the characters until you get a good one; errors are not passed through to the parser.- Specified by:
nextTokenin interfaceTokenSource
-
getLine
public int getLine()Get the line number for the current position in the input stream. The first line in the input is line 1.- Specified by:
getLinein interfaceTokenSource- Returns:
- The line number for the current position in the input stream, or 0 if the current token source does not track line numbers.
-
getInputStream
Get theCharStreamfrom which this token source is currently providing tokens.- Specified by:
getInputStreamin interfaceTokenSource- Returns:
- The
CharStreamassociated with the current position in the input, ornullif no input stream is available for the token source.
-
getSourceName
Gets the name of the underlying input source. This method returns a non-null, non-empty string. If such a name is not known, this method returnsIntStream.UNKNOWN_SOURCE_NAME.- Specified by:
getSourceNamein interfaceTokenSource
-
setTokenFactory
Set theTokenFactorythis token source should use for creatingTokenobjects from the input.- Specified by:
setTokenFactoryin interfaceTokenSource- Parameters:
factory- TheTokenFactoryto use for creating tokens.
-
getTokenFactory
Gets theTokenFactorythis token source is currently using for creatingTokenobjects from the input.- Specified by:
getTokenFactoryin interfaceTokenSource- Returns:
- The
TokenFactorycurrently used by this token source.
-