Class BufferedTokenStream
- java.lang.Object
- 
- org.antlr.v4.runtime.BufferedTokenStream
 
- 
- All Implemented Interfaces:
- IntStream,- TokenStream
 - Direct Known Subclasses:
- CommonTokenStream
 
 public class BufferedTokenStream extends Object implements TokenStream This implementation ofTokenStreamloads tokens from aTokenSourceon-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 asToken.DEFAULT_CHANNELorToken.HIDDEN_CHANNEL, use a filtering token stream such aCommonTokenStream.
- 
- 
Field SummaryFields Modifier and Type Field Description protected booleanfetchedEOFprotected intpprotected List<Token>tokensA collection of all tokens fetched from the token source.protected TokenSourcetokenSourceTheTokenSourcefrom which tokens for this stream are fetched.- 
Fields inherited from interface org.antlr.v4.runtime.IntStreamEOF, UNKNOWN_SOURCE_NAME
 
- 
 - 
Constructor SummaryConstructors Constructor Description BufferedTokenStream(TokenSource tokenSource)
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected intadjustSeekIndex(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.voidconsume()Consumes the current symbol in the stream.protected intfetch(int n)Addnelements to buffer.voidfill()Get all tokens from lexer until EOFprotected List<Token>filterForChannel(int from, int to, int channel)Tokenget(int i)Gets theTokenat the specifiedindexin the stream.List<Token>get(int start, int stop)Get all tokens from start..stop inclusivelyList<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.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.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.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.StringgetSourceName()Gets the name of the underlying symbol source.StringgetText()Get the text of all tokens in this buffer.StringgetText(Interval interval)Return the text of all tokens within the specifiedinterval.StringgetText(RuleContext ctx)Return the text of all tokens in the source interval of the specified context.StringgetText(Token start, Token stop)Return the text of all tokens in this stream betweenstartandstop(inclusive).List<Token>getTokens()List<Token>getTokens(int start, int stop)List<Token>getTokens(int start, int stop, int ttype)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.TokenSourcegetTokenSource()Gets the underlyingTokenSourcewhich provides tokens for this stream.intindex()Return the index into the stream of the input symbol referred to byLA(1).intLA(int i)Gets the value of the symbol at offsetifrom the current position.protected voidlazyInit()protected TokenLB(int k)TokenLT(int k)intmark()protected intnextTokenOnChannel(int i, int channel)Given a starting index, return the index of the next token on channel.protected intpreviousTokenOnChannel(int i, int channel)Given a starting index, return the index of the previous token on channel.voidrelease(int marker)This method releases a marked range created by a call tomark().voidreset()Deprecated.Useseek(0)instead.voidseek(int index)Set the input cursor to the position indicated byindex.voidsetTokenSource(TokenSource tokenSource)Reset this token stream by setting its token source.protected voidsetup()intsize()Returns the total number of symbols in the stream, including a single EOF symbol.protected booleansync(int i)Make sure indexiin tokens has a token.
 
- 
- 
- 
Field Detail- 
tokenSourceprotected TokenSource tokenSource TheTokenSourcefrom which tokens for this stream are fetched.
 - 
tokensprotected List<Token> tokens A collection of all tokens fetched from the token source. The list is considered a complete view of the input oncefetchedEOFis set totrue.
 - 
pprotected int p The index intotokensof the current token (next token toconsume()).tokens[p]should beLT(1).This field is set to -1 when the stream is first constructed or when setTokenSource(org.antlr.v4.runtime.TokenSource)is called, indicating that the first token has not yet been fetched from the token source. For additional information, see the documentation ofIntStreamfor a description of Initializing Methods.
 - 
fetchedEOFprotected boolean fetchedEOF Indicates whether theToken.EOFtoken has been fetched fromtokenSourceand added totokens. 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- fetchedEOFand- pinstead of calling- LA(int).
- fetch(int): The check to prevent adding multiple EOF symbols into- tokensis trivial with this field.
 
 
- 
 - 
Constructor Detail- 
BufferedTokenStreampublic BufferedTokenStream(TokenSource tokenSource) 
 
- 
 - 
Method Detail- 
getTokenSourcepublic TokenSource getTokenSource() Description copied from interface:TokenStreamGets the underlyingTokenSourcewhich provides tokens for this stream.- Specified by:
- getTokenSourcein interface- TokenStream
 
 - 
indexpublic int index() Description copied from interface:IntStreamReturn the index into the stream of the input symbol referred to byLA(1).The behavior of this method is unspecified if no call to an initializing methodhas occurred after this stream was constructed.
 - 
markpublic int mark() Description copied from interface:IntStreamA mark provides a guarantee thatseek()operations will be valid over a "marked range" extending from the index wheremark()was called to the currentindex(). 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 torelease()when the guarantees provided by the marked range are no longer necessary. When calls tomark()/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 methodhas 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(), andseek(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); }
 - 
releasepublic void release(int marker) Description copied from interface:IntStreamThis method releases a marked range created by a call tomark(). Calls torelease()must appear in the reverse order of the corresponding calls tomark(). If a mark is released twice, or if marks are not released in reverse order of the corresponding calls tomark(), the behavior is unspecified.For more information and an example, see IntStream.mark().- Specified by:
- releasein interface- IntStream
- Parameters:
- marker- A marker returned by a call to- mark().
- See Also:
- IntStream.mark()
 
 - 
reset@Deprecated public void reset() Deprecated.Useseek(0)instead.This method resets the token stream back to the first token in the buffer. It is equivalent to callingseek(int)(0).- See Also:
- setTokenSource(TokenSource)
 
 - 
seekpublic void seek(int index) Description copied from interface:IntStreamSet the input cursor to the position indicated byindex. If the specified index lies past the end of the stream, the operation behaves as thoughindexwas 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- indexforward the minimum amount required for the operation to target a non-ignored symbol.
- LA(1)returns- IntStream.EOF
 indexlies within a marked region. For more information on marked regions, seeIntStream.mark(). The behavior of this method is unspecified if no call to aninitializing methodhas occurred after this stream was constructed.
 - 
sizepublic int size() Description copied from interface:IntStreamReturns the total number of symbols in the stream, including a single EOF symbol.
 - 
consumepublic void consume() Description copied from interface:IntStreamConsumes 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 ofindex()after calling this method.
- Ordered lookahead: The value of LA(1)before calling this method becomes the value ofLA(-1)after calling this method.
 index()is incremented by exactly 1, as that would preclude the ability to implement filtering streams (e.g.CommonTokenStreamwhich distinguishes between "on-channel" and "off-channel" tokens).
- Forward movement: The value of 
 - 
syncprotected boolean sync(int i) Make sure indexiin tokens has a token.- Returns:
- trueif a token is located at index- i, otherwise- false.
- See Also:
- get(int i)
 
 - 
fetchprotected int fetch(int n) Addnelements to buffer.- Returns:
- The actual number of elements added to the buffer.
 
 - 
getpublic Token get(int i) Description copied from interface:TokenStreamGets theTokenat the specifiedindexin 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(int). If the behavior ofseek(index)is unspecified for the current state and givenindex, then the behavior of this method is also unspecified.The symbol referred to by indexdiffers fromseek()only in the case of filtering streams whereindexlies before the end of the stream. Unlikeseek(), this method does not adjustindexto point to a non-ignored symbol.- Specified by:
- getin interface- TokenStream
 
 - 
LApublic int LA(int i) Description copied from interface:IntStreamGets the value of the symbol at offsetifrom the current position. Wheni==1, this method returns the value of the current symbol in the stream (which is the next symbol to be consumed). Wheni==-1, this method returns the value of the previously read symbol in the stream. It is not valid to call this method withi==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==-1and- 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 irepresents a position at or beyond the end of the stream, this method returnsIntStream.EOF.The return value is unspecified if i<0and fewer than-icalls toconsume()have occurred from the beginning of the stream before calling this method.
 - 
LBprotected Token LB(int k) 
 - 
LTpublic Token LT(int k) Description copied from interface:TokenStreamGet theTokeninstance associated with the value returned byLA(k). This method has the same pre- and post-conditions asIntStream.LA(int). In addition, when the preconditions of this method are met, the return value is non-null and the value ofLT(k).getType()==LA(k).- Specified by:
- LTin interface- TokenStream
- See Also:
- IntStream.LA(int)
 
 - 
adjustSeekIndexprotected 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 returnsi. If an exception is thrown in this method, the current stream index should not be changed.For example, CommonTokenStreamoverrides this method to ensure that the seek target is always an on-channel token.- Parameters:
- i- The target token index.
- Returns:
- The adjusted target token index.
 
 - 
lazyInitprotected final void lazyInit() 
 - 
setupprotected void setup() 
 - 
setTokenSourcepublic void setTokenSource(TokenSource tokenSource) Reset this token stream by setting its token source.
 - 
getTokenspublic 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.
 - 
nextTokenOnChannelprotected int nextTokenOnChannel(int i, int channel)Given a starting index, return the index of the next token on channel. Returniiftokens[i]is on channel. Return the index of the EOF token if there are no tokens on channel betweeniand EOF.
 - 
previousTokenOnChannelprotected int previousTokenOnChannel(int i, int channel)Given a starting index, return the index of the previous token on channel. Returniiftokens[i]is on channel. Return -1 if there are no tokens on channel betweeniand 0.If ispecifies 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.
 - 
getHiddenTokensToRightpublic 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.
 - 
getHiddenTokensToRightpublic 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.
 - 
getHiddenTokensToLeftpublic 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.
 - 
getHiddenTokensToLeftpublic 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.
 - 
getSourceNamepublic String getSourceName() Description copied from interface:IntStreamGets 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 returnsIntStream.UNKNOWN_SOURCE_NAME.- Specified by:
- getSourceNamein interface- IntStream
 
 - 
getTextpublic String getText() Get the text of all tokens in this buffer.- Specified by:
- getTextin interface- TokenStream
- Returns:
- The text of all tokens in the stream.
 
 - 
getTextpublic String getText(Interval interval) Description copied from interface:TokenStreamReturn the text of all tokens within the specifiedinterval. This method behaves like the following code (including potential exceptions for violating preconditions ofTokenStream.get(int), 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(); }- Specified by:
- getTextin interface- TokenStream
- Parameters:
- interval- The interval of tokens within this stream to get text for.
- Returns:
- The text of all tokens within the specified interval in this stream.
 
 - 
getTextpublic String getText(RuleContext ctx) Description copied from interface:TokenStreamReturn 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 toTokenStream.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()); - Specified by:
- getTextin interface- TokenStream
- Parameters:
- ctx- The context providing the source interval of tokens to get text for.
- Returns:
- The text of all tokens within the source interval of ctx.
 
 - 
getTextpublic String getText(Token start, Token stop) Description copied from interface:TokenStreamReturn the text of all tokens in this stream betweenstartandstop(inclusive).If the specified startorstoptoken was not provided by this stream, or if thestopoccurred before thestarttoken, 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(); }- Specified by:
- getTextin interface- TokenStream
- Parameters:
- start- The first token in the interval to get text for.
- stop- The last token in the interval to get text for (inclusive).
- Returns:
- The text of all tokens lying between the specified startandstoptokens.
 
 - 
fillpublic void fill() Get all tokens from lexer until EOF
 
- 
 
-