Top Description Fields Constructors Methods
org.antlr.v4.runtime

public Class BufferedTokenStream

extends Object
implements TokenStream
Class Inheritance
All Implemented Interfaces
org.antlr.v4.runtime.TokenStream, org.antlr.v4.runtime.IntStream
Known Direct Subclasses
org.antlr.v4.runtime.CommonTokenStream
Imports
org.antlr.v4.runtime.misc.Interval, java.util.ArrayList, .HashSet, .List, .Set

This implementation of TokenStream loads tokens from a TokenSource on-demand, and places the tokens in a buffer to provide access to any previous token by index.

This token stream ignores the value of Token#getChannel. If your parser requires the token stream filter tokens to only those on a particular channel, such as Token#DEFAULT_CHANNEL or Token#HIDDEN_CHANNEL, use a filtering token stream such a CommonTokenStream.

Field Summary

Modifier and TypeField and Description
protected boolean
fetchedEOF

Indicates whether the Token#EOF token has been fetched from tokenSource and added to tokens.

protected int
p

The index into tokens of the current token (next token to consume).

protected List<Token>
tokens

A collection of all tokens fetched from the token source.

protected TokenSource
tokenSource

The TokenSource from which tokens for this stream are fetched.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected int

Returns:

The adjusted target token index.
adjustSeekIndex
(int
The target token index.
i
)

Allowed derived classes to modify the behavior of operations which change the current stream position by adjusting the target token index of a seek operation.

public void
consume()

Implements org.antlr.v4.runtime.IntStream.consume.

Consumes the current symbol in the stream.
protected int

Returns:

The actual number of elements added to the buffer.
fetch
(int n)

Add n elements to buffer.

public void
fill()

Get all tokens from lexer until EOF

protected List<Token>
filterForChannel(int from, int to, int channel)

public Token
get(int i)

Implements org.antlr.v4.runtime.TokenStream.get.

Gets the Token at the specified index in the stream.
public List<Token>
get(int start, int stop)

Get all tokens from start..stop inclusively

public List<Token>
getHiddenTokensToLeft(int tokenIndex, int channel)

Collect all tokens on specified channel to the left of the current token up until we see a token on DEFAULT_TOKEN_CHANNEL.

public List<Token>
getHiddenTokensToLeft(int tokenIndex)

Collect all hidden tokens (any off-default channel) to the left of the current token up until we see a token on DEFAULT_TOKEN_CHANNEL.

public List<Token>
getHiddenTokensToRight(int tokenIndex, int channel)

Collect all tokens on specified channel to the right of the current token up until we see a token on DEFAULT_TOKEN_CHANNEL or EOF.

public List<Token>
getHiddenTokensToRight(int tokenIndex)

Collect all hidden tokens (any off-default channel) to the right of the current token up until we see a token on DEFAULT_TOKEN_CHANNEL or EOF.

public String
getSourceName()

Implements org.antlr.v4.runtime.IntStream.getSourceName.

Gets the name of the underlying symbol source.
public String
getText()

Implements org.antlr.v4.runtime.TokenStream.getText.

Get the text of all tokens in this buffer.
public String
getText(Interval
The interval of tokens within this stream to get text for.
interval
)

Implements org.antlr.v4.runtime.TokenStream.getText.

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

Implements org.antlr.v4.runtime.TokenStream.getText.

Return the text of all tokens in the source interval of the specified context.
public String
getText(Token
The first token in the interval to get text for.
start
,
Token
The last token in the interval to get text for (inclusive).
stop
)

Implements org.antlr.v4.runtime.TokenStream.getText.

Return the text of all tokens in this stream between start and stop (inclusive).
public List<Token>
public List<Token>
getTokens(int start, int stop)

public List<Token>
getTokens(int start, int stop, Set<Integer> types)

Given a start and stop index, return a List of all tokens in the token type BitSet.

public List<Token>
getTokens(int start, int stop, int ttype)

public TokenSource
getTokenSource()

Implements org.antlr.v4.runtime.TokenStream.getTokenSource.

Gets the underlying TokenSource which provides tokens for this stream.
public int
index()

Implements org.antlr.v4.runtime.IntStream.index.

Return the index into the stream of the input symbol referred to by LA(1).
public int
LA(int i)

Implements org.antlr.v4.runtime.IntStream.LA.

Gets the value of the symbol at offset i from the current position.
protected final void
protected Token
LB(int k)

public Token
LT(int k)

Implements org.antlr.v4.runtime.TokenStream.LT.

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

Implements org.antlr.v4.runtime.IntStream.mark.

A mark provides a guarantee that seek() operations will be valid over a "marked range" extending from the index where mark() was called to the current index().
protected int
nextTokenOnChannel(int i, int channel)

Given a starting index, return the index of the next token on channel.

protected int
previousTokenOnChannel(int i, int channel)

Given a starting index, return the index of the previous token on channel.

public void
release(int
A marker returned by a call to mark().
marker
)

Implements org.antlr.v4.runtime.IntStream.release.

This method releases a marked range created by a call to mark().
public void
reset()

Deprecated Use seek(0) instead.
This method resets the token stream back to the first token in the buffer.
public void
seek(int
The absolute index to seek to.
index
)

Implements org.antlr.v4.runtime.IntStream.seek.

Set the input cursor to the position indicated by index.
public void
setTokenSource(TokenSource tokenSource)

Reset this token stream by setting its token source.

protected void
public int
size()

Implements org.antlr.v4.runtime.IntStream.size.

Returns the total number of symbols in the stream, including a single EOF symbol.
protected boolean

Returns:

true if a token is located at index i, otherwise false.
sync
(int i)

Make sure index i in tokens has a token.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

fetchedEOFback to summary
protected boolean fetchedEOF

Indicates whether the Token#EOF token has been fetched from tokenSource and added to tokens. This field improves performance for the following cases:

  • consume: The lookahead check in consume to prevent consuming the EOF symbol is optimized by checking the values of fetchedEOF and p instead of calling LA.
  • fetch: The check to prevent adding multiple EOF symbols into tokens is trivial with this field.
pback to summary
protected int p

The index into tokens of the current token (next token to consume). tokens[p] should be LT(1).

This field is set to -1 when the stream is first constructed or when setTokenSource is called, indicating that the first token has not yet been fetched from the token source. For additional information, see the documentation of IntStream for a description of Initializing Methods.

tokensback to summary
protected List<Token> tokens

A collection of all tokens fetched from the token source. The list is considered a complete view of the input once fetchedEOF is set to true.

tokenSourceback to summary
protected TokenSource tokenSource

The TokenSource from which tokens for this stream are fetched.

Constructor Detail

BufferedTokenStreamback to summary
public BufferedTokenStream(TokenSource tokenSource)

Method Detail

adjustSeekIndexback to summary
protected int adjustSeekIndex(int i)

Allowed derived classes to modify the behavior of operations which change the current stream position by adjusting the target token index of a seek operation. The default implementation simply returns i. If an exception is thrown in this method, the current stream index should not be changed.

For example, CommonTokenStream overrides this method to ensure that the seek target is always an on-channel token.

Parameters
i:int

The target token index.

Returns:int

The adjusted target token index.

consumeback to summary
public void consume()

Implements org.antlr.v4.runtime.IntStream.consume.

Doc from org.antlr.v4.runtime.IntStream.consume.

Consumes the current symbol in the stream. This method has the following effects:

  • Forward movement: The value of index() before calling this method is less than the value of index() after calling this method.
  • Ordered lookahead: The value of LA(1) before calling this method becomes the value of LA(-1) after calling this method.
Note that calling this method does not guarantee that index() is incremented by exactly 1, as that would preclude the ability to implement filtering streams (e.g. CommonTokenStream which distinguishes between "on-channel" and "off-channel" tokens).
Annotations
@Override
fetchback to summary
protected int fetch(int n)

Add n elements to buffer.

Returns:int

The actual number of elements added to the buffer.

fillback to summary
public void fill()

Get all tokens from lexer until EOF

filterForChannelback to summary
protected List<Token> filterForChannel(int from, int to, int channel)
getback to summary
public Token get(int i)

Implements org.antlr.v4.runtime.TokenStream.get.

Doc from org.antlr.v4.runtime.TokenStream.get.

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
@Override
getback to summary
public List<Token> get(int start, int stop)

Get all tokens from start..stop inclusively

getHiddenTokensToLeftback to summary
public List<Token> getHiddenTokensToLeft(int tokenIndex, int channel)

Collect all tokens on specified channel to the left of the current token up until we see a token on DEFAULT_TOKEN_CHANNEL. If channel is -1, find any non default channel token.

getHiddenTokensToLeftback to summary
public List<Token> getHiddenTokensToLeft(int tokenIndex)

Collect all hidden tokens (any off-default channel) to the left of the current token up until we see a token on DEFAULT_TOKEN_CHANNEL.

getHiddenTokensToRightback to summary
public List<Token> getHiddenTokensToRight(int tokenIndex, int channel)

Collect all tokens on specified channel to the right of the current token up until we see a token on DEFAULT_TOKEN_CHANNEL or EOF. If channel is -1, find any non default channel token.

getHiddenTokensToRightback to summary
public List<Token> getHiddenTokensToRight(int tokenIndex)

Collect all hidden tokens (any off-default channel) to the right of the current token up until we see a token on DEFAULT_TOKEN_CHANNEL or EOF.

getSourceNameback to summary
public String getSourceName()

Implements org.antlr.v4.runtime.IntStream.getSourceName.

Doc from org.antlr.v4.runtime.IntStream.getSourceName.

Gets the name of the underlying symbol source. This method returns a non-null, non-empty string. If such a name is not known, this method returns UNKNOWN_SOURCE_NAME.

Annotations
@Override
getTextback to summary
public String getText()

Implements org.antlr.v4.runtime.TokenStream.getText.

Get the text of all tokens in this buffer.

Returns:String

Doc from org.antlr.v4.runtime.TokenStream.getText.

The text of all tokens in the stream.

Annotations
@Override
getTextback to summary
public String getText(Interval interval)

Implements org.antlr.v4.runtime.TokenStream.getText.

Doc from org.antlr.v4.runtime.TokenStream.getText.

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
@Override
getTextback to summary
public String getText(RuleContext ctx)

Implements org.antlr.v4.runtime.TokenStream.getText.

Doc from org.antlr.v4.runtime.TokenStream.getText.

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
@Override
getTextback to summary
public String getText(Token start, Token stop)

Implements org.antlr.v4.runtime.TokenStream.getText.

Doc from org.antlr.v4.runtime.TokenStream.getText.

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:Token

The first token in the interval to get text for.

stop:Token

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
@Override
getTokensback to summary
public List<Token> getTokens()
getTokensback to summary
public List<Token> getTokens(int start, int stop)
getTokensback to summary
public List<Token> getTokens(int start, int stop, Set<Integer> types)

Given a start and stop index, return a List of all tokens in the token type BitSet. Return null if no tokens were found. This method looks at both on and off channel tokens.

getTokensback to summary
public List<Token> getTokens(int start, int stop, int ttype)
getTokenSourceback to summary
public TokenSource getTokenSource()

Implements org.antlr.v4.runtime.TokenStream.getTokenSource.

Doc from org.antlr.v4.runtime.TokenStream.getTokenSource.

Gets the underlying TokenSource which provides tokens for this stream.

Annotations
@Override
indexback to summary
public int index()

Implements org.antlr.v4.runtime.IntStream.index.

Doc from org.antlr.v4.runtime.IntStream.index.

Return the index into the stream of the input symbol referred to by LA(1).

The behavior of this method is unspecified if no call to an initializing method has occurred after this stream was constructed.

Annotations
@Override
LAback to summary
public int LA(int i)

Implements org.antlr.v4.runtime.IntStream.LA.

Doc from org.antlr.v4.runtime.IntStream.LA.

Gets the value of the symbol at offset i from the current position. When i==1, this method returns the value of the current symbol in the stream (which is the next symbol to be consumed). When i==-1, this method returns the value of the previously read symbol in the stream. It is not valid to call this method with i==0, but the specific behavior is unspecified because this method is frequently called from performance-critical code.

This method is guaranteed to succeed if any of the following are true:

  • i>0
  • i==-1 and index() returns a value greater than the value of index() after the stream was constructed and LA(1) was called in that order. Specifying the current index() relative to the index after the stream was created allows for filtering implementations that do not return every symbol from the underlying source. Specifying the call to LA(1) allows for lazily initialized streams.
  • LA(i) refers to a symbol consumed within a marked region that has not yet been released.

If i represents a position at or beyond the end of the stream, this method returns EOF.

The return value is unspecified if i<0 and fewer than -i calls to consume() have occurred from the beginning of the stream before calling this method.

Annotations
@Override
lazyInitback to summary
protected final void lazyInit()
LBback to summary
protected Token LB(int k)
LTback to summary
public Token LT(int k)

Implements org.antlr.v4.runtime.TokenStream.LT.

Doc from org.antlr.v4.runtime.TokenStream.LT.

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
@Override
markback to summary
public int mark()

Implements org.antlr.v4.runtime.IntStream.mark.

Doc from org.antlr.v4.runtime.IntStream.mark.

A mark provides a guarantee that seek() operations will be valid over a "marked range" extending from the index where mark() was called to the current index(). This allows the use of streaming input sources by specifying the minimum buffering requirements to support arbitrary lookahead during prediction.

The returned mark is an opaque handle (type int) which is passed to release() when the guarantees provided by the marked range are no longer necessary. When calls to mark()/release() are nested, the marks must be released in reverse order of which they were obtained. Since marked regions are used during performance-critical sections of prediction, the specific behavior of invalid usage is unspecified (i.e. a mark is not released, or a mark is released twice, or marks are not released in reverse order from which they were created).

The behavior of this method is unspecified if no call to an initializing method has occurred after this stream was constructed.

This method does not change the current position in the input stream.

The following example shows the use of mark(), release(mark), index(), and seek(index) as part of an operation to safely work within a marked region, then restore the stream position to its original value and release the mark.

IntStream stream = ...;
int index = -1;
int mark = stream.mark();
try {
  index = stream.index();
  // perform work here...
} finally {
  if (index != -1) {
    stream.seek(index);
  }
  stream.release(mark);
}
Returns:int

An opaque marker which should be passed to release() when the marked range is no longer required.

Annotations
@Override
nextTokenOnChannelback to summary
protected int nextTokenOnChannel(int i, int channel)

Given a starting index, return the index of the next token on channel. Return i if tokens[i] is on channel. Return the index of the EOF token if there are no tokens on channel between i and EOF.

previousTokenOnChannelback to summary
protected int previousTokenOnChannel(int i, int channel)

Given a starting index, return the index of the previous token on channel. Return i if tokens[i] is on channel. Return -1 if there are no tokens on channel between i and 0.

If i specifies an index at or after the EOF token, the EOF token index is returned. This is due to the fact that the EOF token is treated as though it were on every channel.

releaseback to summary
public void release(int marker)

Implements org.antlr.v4.runtime.IntStream.release.

Doc from org.antlr.v4.runtime.IntStream.release.

This method releases a marked range created by a call to mark(). Calls to release() must appear in the reverse order of the corresponding calls to mark(). If a mark is released twice, or if marks are not released in reverse order of the corresponding calls to mark(), the behavior is unspecified.

For more information and an example, see mark.

Parameters
marker:int

A marker returned by a call to mark().

Annotations
@Override
resetback to summary
public void reset()

Deprecated

Use seek(0) instead.

This method resets the token stream back to the first token in the buffer. It is equivalent to calling seek(0).

Annotations
@Deprecated
See Also
setTokenSource(TokenSource)
seekback to summary
public void seek(int index)

Implements org.antlr.v4.runtime.IntStream.seek.

Doc from org.antlr.v4.runtime.IntStream.seek.

Set the input cursor to the position indicated by index. If the specified index lies past the end of the stream, the operation behaves as though index was the index of the EOF symbol. After this method returns without throwing an exception, then at least one of the following will be true.

  • index() will return the index of the first symbol appearing at or after the specified index. Specifically, implementations which filter their sources should automatically adjust index forward the minimum amount required for the operation to target a non-ignored symbol.
  • LA(1) returns EOF
This operation is guaranteed to not throw an exception if index lies within a marked region. For more information on marked regions, see mark. The behavior of this method is unspecified if no call to an initializing method has occurred after this stream was constructed.
Parameters
index:int

The absolute index to seek to.

Annotations
@Override
setTokenSourceback to summary
public void setTokenSource(TokenSource tokenSource)

Reset this token stream by setting its token source.

setupback to summary
protected void setup()
sizeback to summary
public int size()

Implements org.antlr.v4.runtime.IntStream.size.

Doc from org.antlr.v4.runtime.IntStream.size.

Returns the total number of symbols in the stream, including a single EOF symbol.

Annotations
@Override
syncback to summary
protected boolean sync(int i)

Make sure index i in tokens has a token.

Returns:boolean

true if a token is located at index i, otherwise false.

See Also
get(int i)