From ac3fe8b16104bf2f1f285fafe395ba7fd386781b Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Thu, 21 Nov 2024 07:53:38 +0100 Subject: [PATCH] Refine newline handling further for the Python3 target Signed-off-by: Phil Elson --- .../Python3/src/antlr4/xpath/XPathLexer.py | 48 +++++------- .../templates/codegen/Python3/Python3.stg | 73 ++++++++----------- .../v4/codegen/target/Python3Target.java | 2 + 3 files changed, 50 insertions(+), 73 deletions(-) diff --git a/runtime/Python3/src/antlr4/xpath/XPathLexer.py b/runtime/Python3/src/antlr4/xpath/XPathLexer.py index ee8aa12ce7..31e74eab29 100644 --- a/runtime/Python3/src/antlr4/xpath/XPathLexer.py +++ b/runtime/Python3/src/antlr4/xpath/XPathLexer.py @@ -7,37 +7,27 @@ def serializedATN(): return [ - 4, 0, 8, 50, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, - 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 1, 0, 1, 0, 1, - 0, 1, 1, 1, 1, 1, 2, 1, 2, 1, 3, 1, 3, 1, 4, 1, 4, 5, 4, 29, 8, - 4, 10, 4, 12, 4, 32, 9, 4, 1, 4, 1, 4, 1, 5, 1, 5, 3, 5, 38, 8, - 5, 1, 6, 1, 6, 1, 7, 1, 7, 5, 7, 44, 8, 7, 10, 7, 12, 7, 47, 9, - 7, 1, 7, 1, 7, 1, 45, 0, 8, 1, 3, 3, 4, 5, 5, 7, 6, 9, 7, 11, 0, - 13, 0, 15, 8, 1, 0, 2, 5, 0, 48, 57, 95, 95, 183, 183, 768, 879, - 8255, 8256, 13, 0, 65, 90, 97, 122, 192, 214, 216, 246, 248, 767, - 880, 893, 895, 8191, 8204, 8205, 8304, 8591, 11264, 12271, 12289, - 55295, 63744, 64975, 65008, 65533, 50, 0, 1, 1, 0, 0, 0, 0, 3, 1, - 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 9, 1, 0, 0, 0, 0, - 15, 1, 0, 0, 0, 1, 17, 1, 0, 0, 0, 3, 20, 1, 0, 0, 0, 5, 22, 1, - 0, 0, 0, 7, 24, 1, 0, 0, 0, 9, 26, 1, 0, 0, 0, 11, 37, 1, 0, 0, - 0, 13, 39, 1, 0, 0, 0, 15, 41, 1, 0, 0, 0, 17, 18, 5, 47, 0, 0, - 18, 19, 5, 47, 0, 0, 19, 2, 1, 0, 0, 0, 20, 21, 5, 47, 0, 0, 21, - 4, 1, 0, 0, 0, 22, 23, 5, 42, 0, 0, 23, 6, 1, 0, 0, 0, 24, 25, 5, - 33, 0, 0, 25, 8, 1, 0, 0, 0, 26, 30, 3, 13, 6, 0, 27, 29, 3, 11, - 5, 0, 28, 27, 1, 0, 0, 0, 29, 32, 1, 0, 0, 0, 30, 28, 1, 0, 0, 0, - 30, 31, 1, 0, 0, 0, 31, 33, 1, 0, 0, 0, 32, 30, 1, 0, 0, 0, 33, - 34, 6, 4, 0, 0, 34, 10, 1, 0, 0, 0, 35, 38, 3, 13, 6, 0, 36, 38, - 7, 0, 0, 0, 37, 35, 1, 0, 0, 0, 37, 36, 1, 0, 0, 0, 38, 12, 1, 0, - 0, 0, 39, 40, 7, 1, 0, 0, 40, 14, 1, 0, 0, 0, 41, 45, 5, 39, 0, - 0, 42, 44, 9, 0, 0, 0, 43, 42, 1, 0, 0, 0, 44, 47, 1, 0, 0, 0, 45, - 46, 1, 0, 0, 0, 45, 43, 1, 0, 0, 0, 46, 48, 1, 0, 0, 0, 47, 45, - 1, 0, 0, 0, 48, 49, 5, 39, 0, 0, 49, 16, 1, 0, 0, 0, 4, 0, 30, 37, - 45, 1, 1, 4, 0 - ] + 4,0,8,50,6,-1,2,0,7,0,2,1,7,1,2,2,7,2,2,3,7,3,2,4,7,4,2,5,7,5,2,6,7,6,2, + 7,7,7,1,0,1,0,1,0,1,1,1,1,1,2,1,2,1,3,1,3,1,4,1,4,5,4,29,8,4,10,4,12,4, + 32,9,4,1,4,1,4,1,5,1,5,3,5,38,8,5,1,6,1,6,1,7,1,7,5,7,44,8,7,10,7,12,7, + 47,9,7,1,7,1,7,1,45,0,8,1,3,3,4,5,5,7,6,9,7,11,0,13,0,15,8,1,0,2,5,0,48, + 57,95,95,183,183,768,879,8255,8256,13,0,65,90,97,122,192,214,216,246,248, + 767,880,893,895,8191,8204,8205,8304,8591,11264,12271,12289,55295,63744, + 64975,65008,65533,50,0,1,1,0,0,0,0,3,1,0,0,0,0,5,1,0,0,0,0,7,1,0,0,0,0, + 9,1,0,0,0,0,15,1,0,0,0,1,17,1,0,0,0,3,20,1,0,0,0,5,22,1,0,0,0,7,24,1,0, + 0,0,9,26,1,0,0,0,11,37,1,0,0,0,13,39,1,0,0,0,15,41,1,0,0,0,17,18,5,47,0, + 0,18,19,5,47,0,0,19,2,1,0,0,0,20,21,5,47,0,0,21,4,1,0,0,0,22,23,5,42,0, + 0,23,6,1,0,0,0,24,25,5,33,0,0,25,8,1,0,0,0,26,30,3,13,6,0,27,29,3,11,5, + 0,28,27,1,0,0,0,29,32,1,0,0,0,30,28,1,0,0,0,30,31,1,0,0,0,31,33,1,0,0,0, + 32,30,1,0,0,0,33,34,6,4,0,0,34,10,1,0,0,0,35,38,3,13,6,0,36,38,7,0,0,0, + 37,35,1,0,0,0,37,36,1,0,0,0,38,12,1,0,0,0,39,40,7,1,0,0,40,14,1,0,0,0,41, + 45,5,39,0,0,42,44,9,0,0,0,43,42,1,0,0,0,44,47,1,0,0,0,45,46,1,0,0,0,45, + 43,1,0,0,0,46,48,1,0,0,0,47,45,1,0,0,0,48,49,5,39,0,0,49,16,1,0,0,0,4,0, + 30,37,45,1,1,4,0 + ] # fmt: skip class XPathLexer(Lexer): - atn = ATNDeserializer().deserialize(serializedATN()) decisionsToDFA = [DFA(ds, i) for i, ds in enumerate(atn.decisionToState)] @@ -95,7 +85,7 @@ class XPathLexer(Lexer): def __init__( self, - input = None, + input=None, output: typing.TextIO = sys.stdout, ) -> None: super().__init__(input, output) 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 1a0ec0d493..86d3dcbede 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 @@ -55,6 +55,7 @@ import typing from antlr4 import * + @@ -69,16 +70,17 @@ else: from import
-# This class defines a complete listener for a parse tree produced by . + class Listener(ParseTreeListener): + # This class defines a complete listener for a parse tree produced by . #. def enter(self, ctx: .Context) -> None: + # Enter a parse tree produced by #. pass -# Exit a parse tree produced by #. def exit(self, ctx: .Context) -> None: + # Exit a parse tree produced by #. pass }; separator="\n"> @@ -97,15 +99,14 @@ else: from import
-# This class defines a complete generic visitor for a parse tree produced by . class Visitor(ParseTreeVisitor): + # This class defines a complete generic visitor for a parse tree produced by . #. def visit(self, ctx: .Context): + # Visit a parse tree produced by #. return self.visitChildren(ctx) - }; separator="\n"> del @@ -169,7 +170,6 @@ class (Parser): - def sempred(self, localctx: RuleContext, ruleIndex: int, predIndex: int): if self._predicates is None: @@ -185,8 +185,6 @@ class (Parser): - - >> dumpActions(recog, argFuncs, actionFuncs, sempredFuncs) ::= << @@ -252,7 +250,7 @@ def _action(self, localctx: , actionIndex: int) -> None: elif actionIndex == : - }; separator="\n\n"> + }; separator="\n"> >> /* This generates a private method since the predIndex is generated, making an @@ -262,23 +260,19 @@ RuleSempredFunction(r, actions) ::= << def _sempred(self, localctx: , predIndex: int) -> int: - if predIndex == : - return +if predIndex == : + return - elif predIndex == : - return - }; separator="\n"> +elif predIndex == : + return + }; separator=""> >> RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAction,postamble,exceptions) ::= << - - }; separator="\n"> - def (self: }>): - localctx = .( self, self._ctx, @@ -309,10 +303,8 @@ def (self }; separator="\n"> - def (self, _p: int = 0, }>) -> .: _parentctx = self._ctx _parentState = self.state @@ -360,7 +352,6 @@ token = self._input.LA(1) pass}; separator="\nel"> else: - >> LL1OptionalBlock(choice, alts, error) ::= << @@ -380,7 +371,6 @@ self._errHandler.sync(self) if : - ) ) !> >> @@ -394,7 +384,6 @@ while : self.state = self._errHandler.sync(self) - >> LL1PlusBlockSingleAlt(choice, loopExpr, alts, preamble, iteration) ::= << @@ -408,7 +397,6 @@ while True: if not (): break - >> // LL(*) stuff @@ -424,7 +412,6 @@ if la_ == : pass }; separator="\nel"> - >> OptionalBlock(choice, alts, error) ::= << @@ -435,21 +422,19 @@ la_ = self._interp.adaptivePredict(self._input, , self._ctx) if la_ == +1: }; separator="\nel"> - >> StarBlock(choice, alts, sync, iteration) ::= << self.state = self._errHandler.sync(self) _alt = self._interp.adaptivePredict(self._input, , self._ctx) -while _alt!= and _alt != ATN.INVALID_ALT_NUMBER: +while _alt != and _alt != ATN.INVALID_ALT_NUMBER: if _alt == 1 + 1: self.state = self._errHandler.sync(self) _alt = self._interp.adaptivePredict(self._input, , self._ctx) - >> PlusBlock(choice, alts, error) ::= << @@ -466,7 +451,6 @@ if _alt == + 1: self.state = self._errHandler.sync(self) _alt = self._interp.adaptivePredict(self._input, , self._ctx) - >> Sync(s) ::= "sync()" @@ -525,7 +509,7 @@ CommonSetStuff(m, expr, capture, invert) ::= << self.state = = }>self._input.LT(1) -if \<= 0 or if not(): +if \<= 0 or if not (): = }> self._errHandler.recoverInline(self) else: self._errHandler.reportMatch(self) @@ -606,21 +590,23 @@ SetNonLocalAttr(s, rhsChunks) ::= "self.getInvokingContext().(self): return self.getToken(., 0) + >> // should never be called ContextTokenListGetterDecl(t) ::= << def _list(self): return self.getTokens(.) + >> ContextTokenListIndexedGetterDecl(t) ::= << @@ -629,6 +615,7 @@ def (self, i: typing.Optional[int] = None): return self.getTokens(.) else: return self.getToken(., i) + >> ContextRuleGetterDecl(r) ::= << @@ -683,8 +670,7 @@ class (Parse }; separator="\n"> = }; separator="\n"> - }; separator="\n\n"> - + }; separator="\n"> def getRuleIndex(self): return .RULE_ @@ -695,6 +681,7 @@ class (Parse + >> @@ -719,7 +706,7 @@ class (Context): ListenerDispatchMethod(method) ::= << def enterexitRule(self, listener: ParseTreeListener): - if hasattr( listener, "enterexit" ): + if hasattr(listener, "enterexit"): listener.enterexit(self) >> @@ -730,7 +717,6 @@ def accept(self, visitor: ParseTreeVisitor): return visitor.visit(self) else: return visitor.visitChildren(self) - >> AttributeDecl(d) ::= "self. = None" @@ -807,7 +793,6 @@ else: class (Lexer): - atn = ATNDeserializer().deserialize(serializedATN()) decisionsToDFA = [DFA(ds, i) for i, ds in enumerate(atn.decisionToState)] @@ -852,7 +837,7 @@ class (Lexer): def __init__( self, - input = None, + input=None, output: typing.TextIO = sys.stdout, ) -> None: super().__init__(input, output) @@ -875,8 +860,8 @@ class (Lexer): SerializedATN(model) ::= << def serializedATN(): return [ - }; separator=", ", wrap> - ] + }; separator=",", wrap> + ] # fmt: skip >> /** Using a type to init value map, try to init a type; if not in table diff --git a/tool/src/org/antlr/v4/codegen/target/Python3Target.java b/tool/src/org/antlr/v4/codegen/target/Python3Target.java index 56ead8c2c9..09b2e41052 100644 --- a/tool/src/org/antlr/v4/codegen/target/Python3Target.java +++ b/tool/src/org/antlr/v4/codegen/target/Python3Target.java @@ -62,6 +62,8 @@ public class Python3Target extends Target { public Python3Target(CodeGenerator gen) { super(gen); + // note that lineWidth isn't a hard upper limit. + gen.lineWidth = 79; } @Override