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 ofTokenSourceas a wrapper around a list ofTokenobjects.If the final token in the list is an
Token.EOFtoken, 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 TokeneofTokenThis field caches the EOF token for the token source.protected intiThe index intotokensof token to return by the next call tonextToken().protected List<? extends Token>tokensThe wrapped collection ofTokenobjects to return.
-
Constructor Summary
Constructors Constructor Description ListTokenSource(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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intgetCharPositionInLine()Get the index into the current line for the current position in the input stream.CharStreamgetInputStream()Get theCharStreamfrom which this token source is currently providing tokens.intgetLine()Get the line number for the current position in the input stream.StringgetSourceName()Gets the name of the underlying input source.TokenFactory<?>getTokenFactory()Gets theTokenFactorythis token source is currently using for creatingTokenobjects from the input.TokennextToken()Return aTokenobject from your input stream (usually aCharStream).voidsetTokenFactory(TokenFactory<?> factory)Set theTokenFactorythis token source should use for creatingTokenobjects from the input.
-
-
-
Field Detail
-
tokens
protected final List<? extends Token> tokens
The wrapped collection ofTokenobjects to return.
-
i
protected int i
The 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
protected Token eofToken
This field caches the EOF token for the token source.
-
-
Constructor Detail
-
ListTokenSource
public ListTokenSource(List<? extends Token> tokens)
Constructs a newListTokenSourceinstance from the specified collection ofTokenobjects.- Parameters:
tokens- The collection ofTokenobjects to provide as aTokenSource.- Throws:
NullPointerException- iftokensisnull
-
ListTokenSource
public ListTokenSource(List<? extends Token> tokens, String sourceName)
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 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:
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
public Token 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
public CharStream 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
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:
getSourceNamein interfaceTokenSource
-
setTokenFactory
public void setTokenFactory(TokenFactory<?> factory)
Set theTokenFactorythis token source should use for creatingTokenobjects from the input.- Specified by:
setTokenFactoryin interfaceTokenSource- Parameters:
factory- TheTokenFactoryto use for creating tokens.
-
getTokenFactory
public TokenFactory<?> 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.
-
-