From 2c447a2b7533926d200f3650f57847f316bae5ef Mon Sep 17 00:00:00 2001 From: Ben Hamilton Date: Fri, 10 Feb 2017 13:11:24 -0800 Subject: [PATCH] Python 2 and Python 3 support for lexer and parser output to a file instead of stdout --- .../test/runtime/templates/Python2.test.stg | 40 ++++++++++++------- .../test/runtime/templates/Python3.test.stg | 40 ++++++++++++------- runtime/Python2/src/antlr4/Lexer.py | 9 +++-- runtime/Python2/src/antlr4/Parser.py | 17 ++++---- runtime/Python3/src/antlr4/Lexer.py | 9 +++-- runtime/Python3/src/antlr4/Parser.py | 17 ++++---- .../templates/codegen/Python2/Python2.stg | 10 +++-- .../templates/codegen/Python3/Python3.stg | 12 ++++-- 8 files changed, 95 insertions(+), 59 deletions(-) diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Python2.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Python2.test.stg index 186e30b01..cc0d9d9c2 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Python2.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Python2.test.stg @@ -1,6 +1,6 @@ -writeln(s) ::= <)>> -write(s) ::= <,end='')>> -writeList(s) ::= <)}; separator="+">)>> +writeln(s) ::= <, file=self._output)>> +write(s) ::= <,end='', file=self._output)>> +writeList(s) ::= <)}; separator="+">, file=self._output)>> False() ::= "False" @@ -152,14 +152,16 @@ else: from Listener import Listener class LeafListener(TListener): + def __init__(self, output): + self._output = output def visitTerminal(self, node): - print(node.symbol.text) + print(node.symbol.text, file=self._output) } >> WalkListener(s) ::= << walker = ParseTreeWalker() -walker.walk(TParser.LeafListener(), ) +walker.walk(TParser.LeafListener(self._output), ) >> TreeNodeWithAltNumField(X) ::= << @@ -183,11 +185,13 @@ else: from Listener import Listener class LeafListener(TListener): + def __init__(self, output): + self._output = output def exitA(self, ctx): if ctx.getChildCount()==2: - print(ctx.INT(0).symbol.text + ' ' + ctx.INT(1).symbol.text + ' ' + str_list(ctx.INT())) + print(ctx.INT(0).symbol.text + ' ' + ctx.INT(1).symbol.text + ' ' + str_list(ctx.INT()), file=self._output) else: - print(str(ctx.ID().symbol)) + print(str(ctx.ID().symbol), file=self._output) } >> @@ -199,11 +203,13 @@ else: from Listener import Listener class LeafListener(TListener): + def __init__(self, output): + self._output = output def exitA(self, ctx): if ctx.getChildCount()==2: - print(ctx.b(0).start.text + ' ' + ctx.b(1).start.text + ' ' + ctx.b()[0].start.text) + print(ctx.b(0).start.text + ' ' + ctx.b(1).start.text + ' ' + ctx.b()[0].start.text, file=self._output) else: - print(ctx.b(0).start.text) + print(ctx.b(0).start.text, file=self._output) } >> @@ -216,11 +222,13 @@ else: from Listener import Listener class LeafListener(TListener): + def __init__(self, output): + self._output = output def exitE(self, ctx): if ctx.getChildCount()==3: - print(ctx.e(0).start.text + ' ' + ctx.e(1).start.text + ' ' + ctx.e()[0].start.text) + print(ctx.e(0).start.text + ' ' + ctx.e(1).start.text + ' ' + ctx.e()[0].start.text, file=self._output) else: - print(ctx.INT().symbol.text) + print(ctx.INT().symbol.text, file=self._output) } >> @@ -232,10 +240,12 @@ else: from Listener import Listener class LeafListener(TListener): + def __init__(self, output): + self._output = output def exitCall(self, ctx): - print(ctx.e().start.text + ' ' + str(ctx.eList())) + print(ctx.e().start.text + ' ' + str(ctx.eList()), file=self._output) def exitInt(self, ctx): - print(ctx.INT().symbol.text) + print(ctx.INT().symbol.text, file=self._output) } >> @@ -247,13 +257,13 @@ def foo(): >> Declare_foo() ::= <> Invoke_foo() ::= "self.foo()" Declare_pred() ::= <> -writeList(s) ::= <)}; separator="+">)>> +writeln(s) ::= <, file=self._output)>> +write(s) ::= <,end='',file=self._output)>> +writeList(s) ::= <)}; separator="+">, file=self._output)>> False() ::= "False" @@ -152,8 +152,10 @@ def isIdentifierChar(c): BasicListener(X) ::= << @parser::members { class LeafListener(MockListener): + def __init__(self, output): + self._output = output def visitTerminal(self, node): - print(node.symbol.text) + print(node.symbol.text, file=self._output) } >> @@ -164,7 +166,7 @@ else: from TListener import TListener TParser.LeafListener.__bases__ = (TListener,) walker = ParseTreeWalker() -walker.walk(TParser.LeafListener(), ) +walker.walk(TParser.LeafListener(self._output), ) >> TreeNodeWithAltNumField(X) ::= << @@ -183,22 +185,26 @@ class MyRuleNode(ParserRuleContext): TokenGetterListener(X) ::= << @parser::members { class LeafListener(MockListener): + def __init__(self, output): + self._output = output def exitA(self, ctx): if ctx.getChildCount()==2: - print(ctx.INT(0).symbol.text + ' ' + ctx.INT(1).symbol.text + ' ' + str_list(ctx.INT())) + print(ctx.INT(0).symbol.text + ' ' + ctx.INT(1).symbol.text + ' ' + str_list(ctx.INT()), file=self._output) else: - print(str(ctx.ID().symbol)) + print(str(ctx.ID().symbol), file=self._output) } >> RuleGetterListener(X) ::= << @parser::members { class LeafListener(MockListener): + def __init__(self, output): + self._output = output def exitA(self, ctx): if ctx.getChildCount()==2: - print(ctx.b(0).start.text + ' ' + ctx.b(1).start.text + ' ' + ctx.b()[0].start.text) + print(ctx.b(0).start.text + ' ' + ctx.b(1).start.text + ' ' + ctx.b()[0].start.text, file=self._output) else: - print(ctx.b(0).start.text) + print(ctx.b(0).start.text, file=self._output) } >> @@ -206,21 +212,25 @@ class LeafListener(MockListener): LRListener(X) ::= << @parser::members { class LeafListener(MockListener): + def __init__(self, output): + self._output = output def exitE(self, ctx): if ctx.getChildCount()==3: - print(ctx.e(0).start.text + ' ' + ctx.e(1).start.text + ' ' + ctx.e()[0].start.text) + print(ctx.e(0).start.text + ' ' + ctx.e(1).start.text + ' ' + ctx.e()[0].start.text, file=self._output) else: - print(ctx.INT().symbol.text) + print(ctx.INT().symbol.text, file=self._output) } >> LRWithLabelsListener(X) ::= << @parser::members { class LeafListener(MockListener): + def __init__(self, output): + self._output = output def exitCall(self, ctx): - print(ctx.e().start.text + ' ' + str(ctx.eList())) + print(ctx.e().start.text + ' ' + str(ctx.eList()), file=self._output) def exitInt(self, ctx): - print(ctx.INT().symbol.text) + print(ctx.INT().symbol.text, file=self._output) } >> @@ -232,13 +242,13 @@ def foo(): >> Declare_foo() ::= <> Invoke_foo() ::= "self.foo()" Declare_pred() ::= < @@ -198,8 +199,8 @@ def sempred(self, localctx, ruleIndex, predIndex): >> parser_ctor(p) ::= << -def __init__(self, input): - super(, self).__init__(input) +def __init__(self, input, output=sys.stdout): + super(, self).__init__(input, output=output) self.checkVersion("") self._interp = ParserATNSimulator(self, self.atn, self.decisionsToDFA, self.sharedContextCache) self._predicates = None @@ -740,6 +741,7 @@ LexerFile(lexerFile, lexer, namedActions) ::= << from __future__ import print_function from antlr4 import * from io import StringIO +import sys @@ -780,8 +782,8 @@ class (Lexer): grammarFileName = u"" - def __init__(self, input=None): - super(, self).__init__(input) + def __init__(self, input=None, output=sys.stdout): + super(, self).__init__(input, output=output) self.checkVersion("") self._interp = LexerATNSimulator(self, self.atn, self.decisionsToDFA, PredictionContextCache()) self._actions = None diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Python3/Python3.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Python3/Python3.stg index e56f3eab1..782b4f9ed 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Python3/Python3.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Python3/Python3.stg @@ -51,6 +51,8 @@ ParserFile(file, parser, namedActions, contextSuperClass) ::= << # encoding: utf-8 from antlr4 import * from io import StringIO +from typing.io import TextIO +import sys @@ -206,8 +208,8 @@ def sempred(self, localctx:RuleContext, ruleIndex:int, predIndex:int): >> parser_ctor(p) ::= << -def __init__(self, input:TokenStream): - super().__init__(input) +def __init__(self, input:TokenStream, output:TextIO = sys.stdout): + super().__init__(input, output) self.checkVersion("") self._interp = ParserATNSimulator(self, self.atn, self.decisionsToDFA, self.sharedContextCache) self._predicates = None @@ -745,6 +747,8 @@ LexerFile(lexerFile, lexer, namedActions) ::= << from antlr4 import * from io import StringIO +from typing.io import TextIO +import sys @@ -785,8 +789,8 @@ class (Lexer): grammarFileName = "" - def __init__(self, input=None): - super().__init__(input) + def __init__(self, input=None, output:TextIO = sys.stdout): + super().__init__(input, output) self.checkVersion("") self._interp = LexerATNSimulator(self, self.atn, self.decisionsToDFA, PredictionContextCache()) self._actions = None