From 31f4dcc7cfe44e81ef00c6ee0aafb1581f30cadc Mon Sep 17 00:00:00 2001 From: WalterCouto Date: Wed, 17 Apr 2019 20:10:37 -0400 Subject: [PATCH] don't unnecessary fetch all tokens when grabbing an interval of text (match C# logic) When an exception is thrown, it calls getText to get the text of the tokens invloved but the current C++ and Java implemention first fetches the rest of the tokens instead of just using the tokens it already has. C#'s version is correct (fill() is called only when asking for the entire stream's text). --- runtime/Cpp/runtime/src/BufferedTokenStream.cpp | 5 +++-- .../Java/src/org/antlr/v4/runtime/BufferedTokenStream.java | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) 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();