v4: Fix LookaheadStream ability to seek forward past the current location (via consume())

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9395]
This commit is contained in:
sharwell 2011-11-18 22:54:07 -08:00
parent 7774405ee3
commit f64ecebd34
1 changed files with 9 additions and 3 deletions

View File

@ -145,12 +145,18 @@ public abstract class LookaheadStream<T> extends FastQueue<T> {
*/ */
public void seek(int index) { public void seek(int index) {
int bufferStartIndex = currentElementIndex - p; int bufferStartIndex = currentElementIndex - p;
if (index < bufferStartIndex || index > currentElementIndex) { if (index < bufferStartIndex) {
throw new UnsupportedOperationException("Cannot seek to the specified index."); throw new UnsupportedOperationException("Cannot seek to the specified index.");
} }
currentElementIndex = index; if (index > currentElementIndex) {
p = index - bufferStartIndex; for (int i = 0; i < currentElementIndex - index; i++) {
consume();
}
} else {
currentElementIndex = index;
p = index - bufferStartIndex;
}
} }
protected T LB(int k) { protected T LB(int k) {