LA method
Gets the value of the symbol at offset i
from the current
position. When {@code i==1}, this method returns the value of the current
symbol in the stream (which is the next symbol to be consumed). When
{@code i==-1}, this method returns the value of the previously read
symbol in the stream. It is not valid to call this method with
{@code 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:
- {@code i>0}
- {@code i==-1} and {@link #index index()} returns a value greater than the value of {@code index()} after the stream was constructed and {@code LA(1)} was called in that order. Specifying the current {@code 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 {@code LA(1)} allows for lazily initialized streams.
- {@code 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 {@link #EOF}.
The return value is unspecified if {@code i<0} and fewer than {@code -i} calls to {@link #consume consume()} 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
Implementation
@override
int LA(int offset) {
if (offset == 0) {
return 0; // undefined
}
if (offset < 0) {
offset += 1; // e.g., translate LA(-1) to use offset=0
}
final pos = _index + offset - 1;
if (pos < 0 || pos >= size) {
// invalid
return Token.EOF;
}
return data[pos];
}