public class UnbufferedTokenStream<T extends Token> extends Object implements TokenStream
Modifier and Type | Field and Description |
---|---|
protected int |
currentTokenIndex
Absolute token index.
|
protected Token |
lastToken
This is the
LT(-1) token for the current position. |
protected Token |
lastTokenBufferStart
|
protected int |
n
The number of tokens currently in
tokens . |
protected int |
numMarkers
|
protected int |
p
0..n-1 index into
tokens of next token. |
protected Token[] |
tokens
A moving window buffer of the data being scanned.
|
protected TokenSource |
tokenSource |
EOF, UNKNOWN_SOURCE_NAME
Constructor and Description |
---|
UnbufferedTokenStream(TokenSource tokenSource) |
UnbufferedTokenStream(TokenSource tokenSource,
int bufferSize) |
Modifier and Type | Method and Description |
---|---|
protected void |
add(Token t) |
void |
consume()
Consumes the current symbol in the stream.
|
protected int |
fill(int n)
Add
n elements to the buffer. |
Token |
get(int i)
Gets the
Token at the specified index in the stream. |
protected int |
getBufferStartIndex() |
String |
getSourceName()
Gets the name of the underlying symbol source.
|
String |
getText()
Return the text of all tokens in the stream.
|
String |
getText(Interval interval)
Return the text of all tokens within the specified
interval . |
String |
getText(RuleContext ctx)
Return the text of all tokens in the source interval of the specified
context.
|
String |
getText(Token start,
Token stop)
Return the text of all tokens in this stream between
start and
stop (inclusive). |
TokenSource |
getTokenSource()
Gets the underlying
TokenSource which provides tokens for this
stream. |
int |
index()
Return the index into the stream of the input symbol referred to by
LA(1) . |
int |
LA(int i)
Gets the value of the symbol at offset
i from the current
position. |
Token |
LT(int i)
|
int |
mark()
Return a marker that we can release later.
|
void |
release(int marker)
This method releases a marked range created by a call to
mark() . |
void |
seek(int index)
Set the input cursor to the position indicated by
index . |
int |
size()
Returns the total number of symbols in the stream, including a single EOF
symbol.
|
protected void |
sync(int want)
Make sure we have 'need' elements from current position
p . |
protected TokenSource tokenSource
protected Token[] tokens
consume()
resets so
we start filling at index 0 again.protected int n
tokens
.
This is not the buffer capacity, that's tokens.length
.protected int p
tokens
of next token.
The LT(1)
token is tokens[p]
. If p == n
, we are
out of buffered tokens.protected int numMarkers
protected Token lastToken
LT(-1)
token for the current position.protected Token lastTokenBufferStart
numMarkers > 0
, this is the LT(-1)
token for the
first token in tokens
. Otherwise, this is null
.protected int currentTokenIndex
LT(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
.public UnbufferedTokenStream(TokenSource tokenSource)
public UnbufferedTokenStream(TokenSource tokenSource, int bufferSize)
public Token get(int i)
TokenStream
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(int)
. 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.get
in interface TokenStream
public Token LT(int i)
TokenStream
Token
instance associated with the value returned by
LA(k)
. This method has the same pre- and post-conditions as
IntStream.LA(int)
. 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)
.LT
in interface TokenStream
IntStream.LA(int)
public int LA(int i)
IntStream
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.i
represents a position at or beyond the end of the stream,
this method returns IntStream.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.public TokenSource getTokenSource()
TokenStream
TokenSource
which provides tokens for this
stream.getTokenSource
in interface TokenStream
@NotNull public String getText()
TokenStream
IntStream.size()
and TokenStream.getText(Interval)
, but may be
optimized by the specific implementation.
TokenStream stream = ...; String text = stream.getText(new Interval(0, stream.size()));
getText
in interface TokenStream
@NotNull public String getText(RuleContext ctx)
TokenStream
TokenStream.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());
getText
in interface TokenStream
ctx
- The context providing the source interval of tokens to get
text for.ctx
.@NotNull public String getText(Token start, Token stop)
TokenStream
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(); }
getText
in interface TokenStream
start
- The first token in the interval to get text for.stop
- The last token in the interval to get text for (inclusive).start
and stop
tokens.public void consume()
IntStream
index()
before calling this method is less than the value of index()
after calling this method.LA(1)
before
calling this method becomes the value of LA(-1)
after calling
this method.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).protected void sync(int want)
p
. Last valid
p
index is tokens.length-1
. p+need-1
is the tokens index 'need' elements
ahead. If we need 1 element, (p+1-1)==p
must be less than tokens.length
.protected int fill(int n)
n
elements to the buffer. Returns the number of tokens
actually added to the buffer. If the return value is less than n
,
then EOF was reached before n
tokens could be added.public int mark()
seek()
is called on a mark or
release()
is called in the wrong order.public void release(int marker)
IntStream
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 IntStream.mark()
.release
in interface IntStream
marker
- A marker returned by a call to mark()
.IntStream.mark()
public int index()
IntStream
LA(1)
.
The behavior of this method is unspecified if no call to an
initializing method
has occurred after this stream was
constructed.public void seek(int index)
IntStream
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, the 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 IntStream.EOF
index
lies within a marked region. For more information on marked regions, see
IntStream.mark()
. The behavior of this method is unspecified if no call to
an initializing method
has occurred after this stream
was constructed.public int size()
IntStream
public String getSourceName()
IntStream
IntStream.UNKNOWN_SOURCE_NAME
.getSourceName
in interface IntStream
@NotNull public String getText(Interval interval)
TokenStream
interval
. This
method behaves like the following code (including potential exceptions
for violating preconditions of TokenStream.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(); }
getText
in interface TokenStream
interval
- The interval of tokens within this stream to get text
for.protected final int getBufferStartIndex()
Copyright © 1992-2013 ANTLR. All Rights Reserved.