diff --git a/runtime/Cpp/runtime/src/BufferedTokenStream.cpp b/runtime/Cpp/runtime/src/BufferedTokenStream.cpp index d2a88dd6c..241dfe5c4 100755 --- a/runtime/Cpp/runtime/src/BufferedTokenStream.cpp +++ b/runtime/Cpp/runtime/src/BufferedTokenStream.cpp @@ -272,7 +272,7 @@ ssize_t BufferedTokenStream::previousTokenOnChannel(size_t i, size_t channel) { } if (i == 0) - break; + return -1; i--; } return i; @@ -358,17 +358,18 @@ std::string BufferedTokenStream::getSourceName() const } std::string BufferedTokenStream::getText() { + fill(); return getText(misc::Interval(0U, size() - 1)); } std::string BufferedTokenStream::getText(const misc::Interval &interval) { lazyInit(); - fill(); size_t start = interval.a; size_t stop = interval.b; if (start == INVALID_INDEX || stop == INVALID_INDEX) { return ""; } + sync(stop); if (stop >= _tokens.size()) { stop = _tokens.size() - 1; } diff --git a/runtime/Java/src/org/antlr/v4/runtime/BufferedTokenStream.java b/runtime/Java/src/org/antlr/v4/runtime/BufferedTokenStream.java index e1dc4b197..907d59a3b 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/BufferedTokenStream.java +++ b/runtime/Java/src/org/antlr/v4/runtime/BufferedTokenStream.java @@ -441,15 +441,17 @@ public class BufferedTokenStream implements TokenStream { @Override public String getText() { + fill(); return getText(Interval.of(0,size()-1)); } @Override public String getText(Interval interval) { + lazyInit(); int start = interval.a; int stop = interval.b; if ( start<0 || stop<0 ) return ""; - fill(); + sync(stop); if ( stop>=tokens.size() ) stop = tokens.size()-1; StringBuilder buf = new StringBuilder();