Package org.antlr.v4.runtime
Class ListTokenSource
java.lang.Object
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 TypeFieldDescriptionprivate TokenFactory
<?> This is the backing field forgetTokenFactory()
andsetTokenFactory(org.antlr.v4.runtime.TokenFactory<?>)
.protected Token
This field caches the EOF token for the token source.protected int
The index intotokens
of token to return by the next call tonextToken()
.private final String
The name of the input source.The wrapped collection ofToken
objects to return. -
Constructor Summary
ConstructorsConstructorDescriptionListTokenSource
(List<? extends Token> tokens) Constructs a newListTokenSource
instance from the specified collection ofToken
objects.ListTokenSource
(List<? extends Token> tokens, String sourceName) Constructs a newListTokenSource
instance from the specified collection ofToken
objects and source name. -
Method Summary
Modifier and TypeMethodDescriptionint
Get the index into the current line for the current position in the input stream.Get theCharStream
from which this token source is currently providing tokens.int
getLine()
Get the line number for the current position in the input stream.Gets the name of the underlying input source.TokenFactory
<?> Gets theTokenFactory
this token source is currently using for creatingToken
objects from the input.Return aToken
object from your input stream (usually aCharStream
).void
setTokenFactory
(TokenFactory<?> factory) Set theTokenFactory
this token source should use for creatingToken
objects from the input.
-
Field Details
-
tokens
The wrapped collection ofToken
objects to return. -
sourceName
The name of the input source. If this value isnull
, a call togetSourceName()
should return the source name used to create the the next token intokens
(or the previous token if the end of the input has been reached). -
i
protected int iThe index intotokens
of 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. -
_factory
This is the backing field forgetTokenFactory()
andsetTokenFactory(org.antlr.v4.runtime.TokenFactory<?>)
.
-
-
Constructor Details
-
ListTokenSource
Constructs a newListTokenSource
instance from the specified collection ofToken
objects.- Parameters:
tokens
- The collection ofToken
objects to provide as aTokenSource
.- Throws:
NullPointerException
- iftokens
isnull
-
ListTokenSource
Constructs a newListTokenSource
instance from the specified collection ofToken
objects and source name.- Parameters:
tokens
- The collection ofToken
objects 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
- iftokens
isnull
-
-
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:
getCharPositionInLine
in 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 aToken
object 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:
nextToken
in 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:
getLine
in 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 theCharStream
from which this token source is currently providing tokens.- Specified by:
getInputStream
in interfaceTokenSource
- Returns:
- The
CharStream
associated with the current position in the input, ornull
if 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:
getSourceName
in interfaceTokenSource
-
setTokenFactory
Set theTokenFactory
this token source should use for creatingToken
objects from the input.- Specified by:
setTokenFactory
in interfaceTokenSource
- Parameters:
factory
- TheTokenFactory
to use for creating tokens.
-
getTokenFactory
Gets theTokenFactory
this token source is currently using for creatingToken
objects from the input.- Specified by:
getTokenFactory
in interfaceTokenSource
- Returns:
- The
TokenFactory
currently used by this token source.
-