Package org.antlr.v4.runtime
Class ListTokenSource
- java.lang.Object
-
- org.antlr.v4.runtime.ListTokenSource
-
- All Implemented Interfaces:
TokenSource
public class ListTokenSource extends Object implements TokenSource
Provides an implementation ofTokenSource
as a wrapper around a list ofToken
objects.If the final token in the list is an
Token.EOF
token, it will be used as the EOF token for every call tonextToken()
after the end of the list is reached. Otherwise, an EOF token will be created.
-
-
Field Summary
Fields Modifier and Type Field Description protected Token
eofToken
This field caches the EOF token for the token source.protected int
i
The index intotokens
of token to return by the next call tonextToken()
.protected List<? extends Token>
tokens
The wrapped collection ofToken
objects to return.
-
Constructor Summary
Constructors Constructor Description ListTokenSource(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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
getCharPositionInLine()
Get the index into the current line for the current position in the input stream.CharStream
getInputStream()
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.String
getSourceName()
Gets the name of the underlying input source.TokenFactory<?>
getTokenFactory()
Gets theTokenFactory
this token source is currently using for creatingToken
objects from the input.Token
nextToken()
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 Detail
-
tokens
protected final List<? extends Token> tokens
The wrapped collection ofToken
objects to return.
-
i
protected int i
The 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
protected Token eofToken
This field caches the EOF token for the token source.
-
-
Constructor Detail
-
ListTokenSource
public ListTokenSource(List<? extends Token> tokens)
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
public ListTokenSource(List<? extends Token> tokens, String sourceName)
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 Detail
-
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
public Token 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
public CharStream 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
public String 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
public void setTokenFactory(TokenFactory<?> factory)
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
public TokenFactory<?> 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.
-
-