From f64ecebd34c948432683e20e12c3cdde6429d99f Mon Sep 17 00:00:00 2001 From: sharwell Date: Fri, 18 Nov 2011 22:54:07 -0800 Subject: [PATCH] v4: Fix LookaheadStream ability to seek forward past the current location (via consume()) [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9395] --- .../org/antlr/v4/runtime/misc/LookaheadStream.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/misc/LookaheadStream.java b/runtime/Java/src/org/antlr/v4/runtime/misc/LookaheadStream.java index 8178f2fec..aeaa85720 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/misc/LookaheadStream.java +++ b/runtime/Java/src/org/antlr/v4/runtime/misc/LookaheadStream.java @@ -145,12 +145,18 @@ public abstract class LookaheadStream extends FastQueue { */ public void seek(int index) { int bufferStartIndex = currentElementIndex - p; - if (index < bufferStartIndex || index > currentElementIndex) { + if (index < bufferStartIndex) { throw new UnsupportedOperationException("Cannot seek to the specified index."); } - currentElementIndex = index; - p = index - bufferStartIndex; + if (index > currentElementIndex) { + for (int i = 0; i < currentElementIndex - index; i++) { + consume(); + } + } else { + currentElementIndex = index; + p = index - bufferStartIndex; + } } protected T LB(int k) {