Interface IntStream
-
- All Known Subinterfaces:
CharStream
,TokenStream
- All Known Implementing Classes:
ANTLRFileStream
,ANTLRInputStream
,BufferedTokenStream
,CodePointCharStream
,CommonTokenStream
,UnbufferedCharStream
,UnbufferedTokenStream
public interface IntStream
A simple stream of symbols whose values are represented as integers. This interface provides marked ranges with support for a minimum level of buffering necessary to implement arbitrary lookahead during prediction. For more information on marked ranges, seemark()
.Initializing Methods: Some methods in this interface have unspecified behavior if no call to an initializing method has occurred after the stream was constructed. The following is a list of initializing methods:
-
-
Field Summary
Fields Modifier and Type Field Description static int
EOF
The value returned byLA()
when the end of the stream is reached.static String
UNKNOWN_SOURCE_NAME
The value returned bygetSourceName()
when the actual name of the underlying source is not known.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
consume()
Consumes the current symbol in the stream.String
getSourceName()
Gets the name of the underlying symbol source.int
index()
Return the index into the stream of the input symbol referred to byLA(1)
.int
LA(int i)
Gets the value of the symbol at offseti
from the current position.int
mark()
void
release(int marker)
This method releases a marked range created by a call tomark()
.void
seek(int index)
Set the input cursor to the position indicated byindex
.int
size()
Returns the total number of symbols in the stream, including a single EOF symbol.
-
-
-
Field Detail
-
EOF
static final int EOF
The value returned byLA()
when the end of the stream is reached.- See Also:
- Constant Field Values
-
UNKNOWN_SOURCE_NAME
static final String UNKNOWN_SOURCE_NAME
The value returned bygetSourceName()
when the actual name of the underlying source is not known.- See Also:
- Constant Field Values
-
-
Method Detail
-
consume
void 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 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.CommonTokenStream
which distinguishes between "on-channel" and "off-channel" tokens).- Throws:
IllegalStateException
- if an attempt is made to consume the end of the stream (i.e. ifLA(1)==
EOF
before callingconsume
).
- Forward movement: The value of
-
LA
int LA(int i)
Gets the value of the symbol at offseti
from 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==-1
andindex()
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
i
represents a position at or beyond the end of the stream, this method returnsEOF
.The return value is unspecified if
i<0
and fewer than-i
calls toconsume()
have occurred from the beginning of the stream before calling this method.- Throws:
UnsupportedOperationException
- if the stream does not support retrieving the value of the specified symbol
-
mark
int mark()
A 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 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()
, 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); }
- Returns:
- An opaque marker which should be passed to
release()
when the marked range is no longer required.
-
release
void release(int marker)
This 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
mark()
.- Parameters:
marker
- A marker returned by a call tomark()
.- See Also:
mark()
-
index
int index()
Return 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 method
has occurred after this stream was constructed.
-
seek
void seek(int index)
Set the input cursor to the position indicated byindex
. If the specified index lies past the end of the stream, the operation behaves as thoughindex
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 specifiedindex
. Specifically, implementations which filter their sources should automatically adjustindex
forward the minimum amount required for the operation to target a non-ignored symbol.LA(1)
returnsEOF
index
lies within a marked region. For more information on marked regions, seemark()
. The behavior of this method is unspecified if no call to aninitializing method
has occurred after this stream was constructed.- Parameters:
index
- The absolute index to seek to.- Throws:
IllegalArgumentException
- ifindex
is less than 0UnsupportedOperationException
- if the stream does not support seeking to the specified index
-
size
int size()
Returns the total number of symbols in the stream, including a single EOF symbol.- Throws:
UnsupportedOperationException
- if the size of the stream is unknown.
-
getSourceName
String 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 returnsUNKNOWN_SOURCE_NAME
.
-
-