From f50fb7df156bda289bd3b520df9cbc2ae344a986 Mon Sep 17 00:00:00 2001 From: Vladi Lyga Date: Wed, 30 Nov 2016 13:55:54 +0200 Subject: [PATCH] Fix error when Interval is rewritten with a tuple --- runtime/Python2/src/antlr4/BufferedTokenStream.py | 15 ++++++++++----- runtime/Python2/tests/TestTokenStreamRewriter.py | 4 +++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/runtime/Python2/src/antlr4/BufferedTokenStream.py b/runtime/Python2/src/antlr4/BufferedTokenStream.py index 17df81b86..78312d62a 100644 --- a/runtime/Python2/src/antlr4/BufferedTokenStream.py +++ b/runtime/Python2/src/antlr4/BufferedTokenStream.py @@ -39,6 +39,8 @@ # {@link Token#HIDDEN_CHANNEL}, use a filtering token stream such a # {@link CommonTokenStream}.

from io import StringIO + +from antlr4.IntervalSet import Interval from antlr4.Token import Token from antlr4.error.Errors import IllegalStateException @@ -293,18 +295,18 @@ class BufferedTokenStream(TokenStream): def getSourceName(self): return self.tokenSource.getSourceName() - # Get the text of all tokens in this buffer.#/ def getText(self, interval=None): """ + Get the text of all tokens in this buffer. :param interval: :type interval: antlr4.IntervalSet.Interval - :return: + :return: string """ self.lazyInit() self.fill() if interval is None: - interval = (0, len(self.tokens)-1) + interval = Interval(0, len(self.tokens)-1) start = interval.start if isinstance(start, Token): start = start.tokenIndex @@ -323,9 +325,12 @@ class BufferedTokenStream(TokenStream): buf.write(t.text) return buf.getvalue() - - # Get all tokens from lexer until EOF#/ def fill(self): + """ + Get all tokens from lexer until EOF + + :return: None + """ self.lazyInit() while self.fetch(1000)==1000: pass diff --git a/runtime/Python2/tests/TestTokenStreamRewriter.py b/runtime/Python2/tests/TestTokenStreamRewriter.py index b5a2dc886..b5ac26597 100644 --- a/runtime/Python2/tests/TestTokenStreamRewriter.py +++ b/runtime/Python2/tests/TestTokenStreamRewriter.py @@ -528,8 +528,10 @@ class TestTokenStreamRewriter(unittest.TestCase): self.assertEquals('afoofoo', rewriter.getDefaultText()) - # Test for fix for: https://github.com/antlr/antlr4/issues/550 def testPreservesOrderOfContiguousInserts(self): + """ + Test for fix for: https://github.com/antlr/antlr4/issues/550 + """ input = InputStream('aa') lexer = TestLexer(input) stream = CommonTokenStream(lexer=lexer)