Class UnbufferedTokenStream<T extends Token>
- java.lang.Object
-
- org.antlr.v4.runtime.UnbufferedTokenStream<T>
-
- All Implemented Interfaces:
IntStream,TokenStream
public class UnbufferedTokenStream<T extends Token> extends Object implements TokenStream
-
-
Field Summary
Fields Modifier and Type Field Description protected intcurrentTokenIndexAbsolute token index.protected TokenlastTokenThis is theLT(-1)token for the current position.protected TokenlastTokenBufferStartprotected intnThe number of tokens currently intokens.protected intnumMarkersprotected intp0..n-1 index intotokensof next token.protected Token[]tokensA moving window buffer of the data being scanned.protected TokenSourcetokenSource-
Fields inherited from interface org.antlr.v4.runtime.IntStream
EOF, UNKNOWN_SOURCE_NAME
-
-
Constructor Summary
Constructors Constructor Description UnbufferedTokenStream(TokenSource tokenSource)UnbufferedTokenStream(TokenSource tokenSource, int bufferSize)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidadd(Token t)voidconsume()Consumes the current symbol in the stream.protected intfill(int n)Addnelements to the buffer.Tokenget(int i)Gets theTokenat the specifiedindexin the stream.protected intgetBufferStartIndex()StringgetSourceName()Gets the name of the underlying symbol source.StringgetText()Return the text of all tokens in the stream.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).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.TokenLT(int i)intmark()Return a marker that we can release later.voidrelease(int marker)This method releases a marked range created by a call tomark().voidseek(int index)Set the input cursor to the position indicated byindex.intsize()Returns the total number of symbols in the stream, including a single EOF symbol.protected voidsync(int want)Make sure we have 'need' elements from current positionp.
-
-
-
Field Detail
-
tokenSource
protected TokenSource tokenSource
-
tokens
protected Token[] tokens
A moving window buffer of the data being scanned. While there's a marker, we keep adding to buffer. Otherwise,consume()resets so we start filling at index 0 again.
-
n
protected int n
The number of tokens currently intokens.This is not the buffer capacity, that's
tokens.length.
-
p
protected int p
0..n-1 index intotokensof next token.The
LT(1)token istokens[p]. Ifp == n, we are out of buffered tokens.
-
numMarkers
protected int numMarkers
-
lastToken
protected Token lastToken
This is theLT(-1)token for the current position.
-
lastTokenBufferStart
protected Token lastTokenBufferStart
WhennumMarkers > 0, this is theLT(-1)token for the first token intokens. Otherwise, this isnull.
-
currentTokenIndex
protected int currentTokenIndex
Absolute token index. It's the index of the token about to be read viaLT(1). Goes from 0 to the number of tokens in the entire stream, although the stream size is unknown before the end is reached.This value is used to set the token indexes if the stream provides tokens that implement
WritableToken.
-
-
Constructor Detail
-
UnbufferedTokenStream
public UnbufferedTokenStream(TokenSource tokenSource)
-
UnbufferedTokenStream
public UnbufferedTokenStream(TokenSource tokenSource, int bufferSize)
-
-
Method Detail
-
get
public 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 interfaceTokenStream
-
LT
public Token LT(int i)
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 interfaceTokenStream- See Also:
IntStream.LA(int)
-
LA
public 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>0i==-1andindex()returns a value greater than the value ofindex()after the stream was constructed andLA(1)was called in that order. Specifying the currentindex()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 toLA(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.
-
getTokenSource
public TokenSource getTokenSource()
Description copied from interface:TokenStreamGets the underlyingTokenSourcewhich provides tokens for this stream.- Specified by:
getTokenSourcein interfaceTokenStream
-
getText
public String getText()
Description copied from interface:TokenStreamReturn the text of all tokens in the stream. This method behaves like the following code, including potential exceptions from the calls toIntStream.size()andTokenStream.getText(Interval), but may be optimized by the specific implementation.TokenStream stream = ...; String text = stream.getText(new Interval(0, stream.size()));
- Specified by:
getTextin interfaceTokenStream- Returns:
- The text of all tokens in the stream.
-
getText
public 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 interfaceTokenStream- 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.
-
getText
public 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 interfaceTokenStream- 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.
-
consume
public 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
-
sync
protected void sync(int want)
Make sure we have 'need' elements from current positionp. Last validpindex istokens.length-1.p+need-1is the tokens index 'need' elements ahead. If we need 1 element,(p+1-1)==pmust be less thantokens.length.
-
fill
protected int fill(int n)
Addnelements to the buffer. Returns the number of tokens actually added to the buffer. If the return value is less thann, then EOF was reached beforentokens could be added.
-
add
protected void add(Token t)
-
mark
public int mark()
Return a marker that we can release later.The specific marker value used for this class allows for some level of protection against misuse where
seek()is called on a mark orrelease()is called in the wrong order.
-
release
public 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 interfaceIntStream- Parameters:
marker- A marker returned by a call tomark().- See Also:
IntStream.mark()
-
index
public 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.
-
seek
public 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 specifiedindex. Specifically, implementations which filter their sources should automatically adjustindexforward the minimum amount required for the operation to target a non-ignored symbol.LA(1)returnsIntStream.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.
-
size
public int size()
Description copied from interface:IntStreamReturns the total number of symbols in the stream, including a single EOF symbol.
-
getSourceName
public 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 interfaceIntStream
-
getText
public 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 interfaceTokenStream- 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.
-
getBufferStartIndex
protected final int getBufferStartIndex()
-
-