Top Description Fields Methods
org.antlr.v4.runtime

public Interface TokenStream

extends IntStream
Known Direct Implementers
org.antlr.v4.runtime.UnbufferedTokenStream, org.antlr.v4.runtime.BufferedTokenStream
Imports
org.antlr.v4.runtime.misc.Interval, .NotNull

An IntStream whose symbols are Token instances.

Field Summary

Inherited from org.antlr.v4.runtime.IntStream:
EOFUNKNOWN_SOURCE_NAME

Method Summary

Modifier and TypeMethod and Description
public Token
get(int i)

Gets the Token at the specified index in the stream.

public String

Returns:

The text of all tokens within the specified interval in this stream.
getText
(Interval
The interval of tokens within this stream to get text for.
interval
)

Return the text of all tokens within the specified interval.

public String

Returns:

The text of all tokens in the stream.
getText
()

Return the text of all tokens in the stream.

public String

Returns:

The text of all tokens within the source interval of ctx.
getText
(RuleContext
The context providing the source interval of tokens to get text for.
ctx
)

Return the text of all tokens in the source interval of the specified context.

public String

Returns:

The text of all tokens lying between the specified start and stop tokens.
getText
(Object
The first token in the interval to get text for.
start
,
Object
The last token in the interval to get text for (inclusive).
stop
)

Return the text of all tokens in this stream between start and stop (inclusive).

public TokenSource
getTokenSource()

Gets the underlying TokenSource which provides tokens for this stream.

public Token
LT(int k)

Get the Token instance associated with the value returned by LA(k).

Inherited from org.antlr.v4.runtime.IntStream:
consumegetSourceNameindexLAmarkreleaseseeksize

Method Detail

getback to summary
public Token get(int i)

Gets the Token at the specified index in the stream. When the preconditions of this method are met, the return value is non-null.

The preconditions for this method are the same as the preconditions of IntStream#seek. If the behavior of seek(index) is unspecified for the current state and given index, then the behavior of this method is also unspecified.

The symbol referred to by index differs from seek() only in the case of filtering streams where index lies before the end of the stream. Unlike seek(), this method does not adjust index to point to a non-ignored symbol.

Annotations
@NotNull
Exceptions
IllegalArgumentException:
if {code index} is less than 0
UnsupportedOperationException:
if the stream does not support retrieving the token at the specified index
getTextback to summary
public String getText(Interval interval)

Return the text of all tokens within the specified interval. This method behaves like the following code (including potential exceptions for violating preconditions of get, but may be optimized by the specific implementation.

TokenStream stream = ...;
String text = "";
for (int i = interval.a; i <= interval.b; i++) {
  text += stream.get(i).getText();
}
Parameters
interval:Interval

The interval of tokens within this stream to get text for.

Returns:String

The text of all tokens within the specified interval in this stream.

Annotations
@NotNull
Exceptions
NullPointerException:
if interval is null
getTextback to summary
public String getText()

Return the text of all tokens in the stream. This method behaves like the following code, including potential exceptions from the calls to IntStream#size and getText(Interval), but may be optimized by the specific implementation.

TokenStream stream = ...;
String text = stream.getText(new Interval(0, stream.size()));
Returns:String

The text of all tokens in the stream.

Annotations
@NotNull
getTextback to summary
public String getText(RuleContext ctx)

Return the text of all tokens in the source interval of the specified context. This method behaves like the following code, including potential exceptions from the call to getText(Interval), but may be optimized by the specific implementation.

If ctx.getSourceInterval() does not return a valid interval of tokens provided by this stream, the behavior is unspecified.

TokenStream stream = ...;
String text = stream.getText(ctx.getSourceInterval());
Parameters
ctx:RuleContext

The context providing the source interval of tokens to get text for.

Returns:String

The text of all tokens within the source interval of ctx.

Annotations
@NotNull
getTextback to summary
public String getText(Object start, Object stop)

Return the text of all tokens in this stream between start and stop (inclusive).

If the specified start or stop token was not provided by this stream, or if the stop occurred before the start token, the behavior is unspecified.

For streams which ensure that the Token#getTokenIndex method is accurate for all of its provided tokens, this method behaves like the following code. Other streams may implement this method in other ways provided the behavior is consistent with this at a high level.

TokenStream stream = ...;
String text = "";
for (int i = start.getTokenIndex(); i <= stop.getTokenIndex(); i++) {
  text += stream.get(i).getText();
}
Parameters
start:Object

The first token in the interval to get text for.

stop:Object

The last token in the interval to get text for (inclusive).

Returns:String

The text of all tokens lying between the specified start and stop tokens.

Annotations
@NotNull
Exceptions
UnsupportedOperationException:
if this stream does not support this method for the specified tokens
getTokenSourceback to summary
public TokenSource getTokenSource()

Gets the underlying TokenSource which provides tokens for this stream.

Annotations
@NotNull
LTback to summary
public Token LT(int k)

Get the Token instance associated with the value returned by LA(k). This method has the same pre- and post-conditions as IntStream#LA. In addition, when the preconditions of this method are met, the return value is non-null and the value of LT(k).getType()==LA(k).

Annotations
@NotNull
See Also
IntStream#LA