public class ListTokenSource extends java.lang.Object implements TokenSource
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.
Modifier and Type | Field and Description |
---|---|
protected Token |
eofToken
This field caches the EOF token for the token source.
|
protected int |
i
The index into
tokens of token to return by the next call to
nextToken() . |
protected java.util.List<? extends Token> |
tokens
The wrapped collection of
Token objects to return. |
Constructor and Description |
---|
ListTokenSource(java.util.List<? extends Token> tokens)
Constructs a new
ListTokenSource instance from the specified
collection of Token objects. |
ListTokenSource(java.util.List<? extends Token> tokens,
java.lang.String sourceName)
Constructs a new
ListTokenSource instance from the specified
collection of Token objects and source name. |
Modifier and Type | Method and Description |
---|---|
int |
getCharPositionInLine()
Get the index into the current line for the current position in the input
stream.
|
CharStream |
getInputStream()
Get the
CharStream from which this token source is currently
providing tokens. |
int |
getLine()
Get the line number for the current position in the input stream.
|
java.lang.String |
getSourceName()
Gets the name of the underlying input source.
|
TokenFactory<?> |
getTokenFactory()
Gets the
TokenFactory this token source is currently using for
creating Token objects from the input. |
Token |
nextToken()
Return a
Token object from your input stream (usually a
CharStream ). |
void |
setTokenFactory(TokenFactory<?> factory)
Set the
TokenFactory this token source should use for creating
Token objects from the input. |
protected final java.util.List<? extends Token> tokens
Token
objects to return.protected int i
tokens
of token to return by the next call to
nextToken()
. The end of the input is indicated by this value
being greater than or equal to the number of items in tokens
.protected Token eofToken
public ListTokenSource(java.util.List<? extends Token> tokens)
ListTokenSource
instance from the specified
collection of Token
objects.tokens
- The collection of Token
objects to provide as a
TokenSource
.java.lang.NullPointerException
- if tokens
is null
public ListTokenSource(java.util.List<? extends Token> tokens, java.lang.String sourceName)
ListTokenSource
instance from the specified
collection of Token
objects and source name.tokens
- The collection of Token
objects to provide as a
TokenSource
.sourceName
- The name of the TokenSource
. If this value is
null
, getSourceName()
will attempt to infer the name from
the next Token
(or the previous token if the end of the input has
been reached).java.lang.NullPointerException
- if tokens
is null
public int getCharPositionInLine()
getCharPositionInLine
in interface TokenSource
public Token nextToken()
Token
object from your input stream (usually a
CharStream
). 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.nextToken
in interface TokenSource
public int getLine()
getLine
in interface TokenSource
public CharStream getInputStream()
CharStream
from which this token source is currently
providing tokens.getInputStream
in interface TokenSource
CharStream
associated with the current position in
the input, or null
if no input stream is available for the token
source.public java.lang.String getSourceName()
IntStream.UNKNOWN_SOURCE_NAME
.getSourceName
in interface TokenSource
public void setTokenFactory(TokenFactory<?> factory)
TokenFactory
this token source should use for creating
Token
objects from the input.setTokenFactory
in interface TokenSource
factory
- The TokenFactory
to use for creating tokens.public TokenFactory<?> getTokenFactory()
TokenFactory
this token source is currently using for
creating Token
objects from the input.getTokenFactory
in interface TokenSource
TokenFactory
currently used by this token source.