From 7a89e1586255c1160de1799dbc5751ac42262496 Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Mon, 30 Sep 2024 10:08:15 +0200 Subject: [PATCH] Refine the generated Python3 code Signed-off-by: Phil Elson --- .../Python3/src/antlr4/xpath/XPathLexer.py | 124 +++++++---- .../templates/codegen/Python3/Python3.stg | 201 +++++++++++------- 2 files changed, 205 insertions(+), 120 deletions(-) diff --git a/runtime/Python3/src/antlr4/xpath/XPathLexer.py b/runtime/Python3/src/antlr4/xpath/XPathLexer.py index d608d466a8..ee8aa12ce7 100644 --- a/runtime/Python3/src/antlr4/xpath/XPathLexer.py +++ b/runtime/Python3/src/antlr4/xpath/XPathLexer.py @@ -1,40 +1,46 @@ # Generated from XPathLexer.g4 by ANTLR 4.13.1 -from antlr4 import * -from io import StringIO import sys -if sys.version_info[1] > 5: - from typing import TextIO -else: - from typing.io import TextIO +import typing + +from antlr4 import * 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 ] + class XPathLexer(Lexer): atn = ATNDeserializer().deserialize(serializedATN()) - decisionsToDFA = [ DFA(ds, i) for i, ds in enumerate(atn.decisionToState) ] + decisionsToDFA = [DFA(ds, i) for i, ds in enumerate(atn.decisionToState)] TOKEN_REF = 1 RULE_REF = 2 @@ -45,43 +51,77 @@ class XPathLexer(Lexer): ID = 7 STRING = 8 - channelNames = [ u"DEFAULT_TOKEN_CHANNEL", u"HIDDEN" ] + channelNames = [ + "DEFAULT_TOKEN_CHANNEL", + "HIDDEN", + ] - modeNames = [ "DEFAULT_MODE" ] + modeNames = [ + "DEFAULT_MODE", + ] - literalNames = [ "", - "'//'", "'/'", "'*'", "'!'" ] + literalNames = [ + "", + "'//'", + "'/'", + "'*'", + "'!'", + ] - symbolicNames = [ "", - "TOKEN_REF", "RULE_REF", "ANYWHERE", "ROOT", "WILDCARD", "BANG", - "ID", "STRING" ] + symbolicNames = [ + "", + "TOKEN_REF", + "RULE_REF", + "ANYWHERE", + "ROOT", + "WILDCARD", + "BANG", + "ID", + "STRING", + ] - ruleNames = [ "ANYWHERE", "ROOT", "WILDCARD", "BANG", "ID", "NameChar", - "NameStartChar", "STRING" ] + ruleNames = [ + "ANYWHERE", + "ROOT", + "WILDCARD", + "BANG", + "ID", + "NameChar", + "NameStartChar", + "STRING", + ] grammarFileName = "XPathLexer.g4" - def __init__(self, input=None, output:TextIO = sys.stdout): + def __init__( + self, + input = None, + output: typing.TextIO = sys.stdout, + ) -> None: super().__init__(input, output) self.checkVersion("4.13.1") - self._interp = LexerATNSimulator(self, self.atn, self.decisionsToDFA, PredictionContextCache()) + self._interp = LexerATNSimulator( + self, + self.atn, + self.decisionsToDFA, + PredictionContextCache(), + ) self._actions = None self._predicates = None - def action(self, localctx:RuleContext, ruleIndex:int, actionIndex:int): + def action(self, localctx: RuleContext, ruleIndex: int, actionIndex: int) -> None: if self._actions is None: - actions = dict() - actions[4] = self.ID_action + actions = {} + actions[4] = self.ID_action self._actions = actions action = self._actions.get(ruleIndex, None) if action is not None: action(localctx, actionIndex) else: - raise Exception("No registered action for:" + str(ruleIndex)) - + raise Exception(f"No registered action for: {ruleIndex}") - def ID_action(self, localctx:RuleContext , actionIndex:int): + def ID_action(self, localctx: RuleContext, actionIndex: int) -> None: if actionIndex == 0: char = self.text[0] 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 972b88b6d0..08b8577593 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 @@ -48,14 +48,12 @@ pythonTypeInitMap ::= [ ParserFile(file, parser, namedActions, contextSuperClass) ::= << -# encoding: utf-8 -from antlr4 import * -from io import StringIO +from __future__ import annotations + import sys -if sys.version_info[1] > 5: - from typing import TextIO -else: - from typing.io import TextIO +import typing + +from antlr4 import * @@ -76,11 +74,11 @@ class Listener(ParseTreeListener): #. -def enter(self, ctx:.Context): +def enter(self, ctx: .Context) -> None: pass # Exit a parse tree produced by #. -def exit(self, ctx:.Context): +def exit(self, ctx: .Context) -> None: pass }; separator="\n"> @@ -104,7 +102,7 @@ class Visitor(ParseTreeVisitor): #. -def visit(self, ctx:.Context): +def visit(self, ctx: .Context): return self.visitChildren(ctx) }; separator="\n"> @@ -131,29 +129,36 @@ else: -class ( Parser ): + +class (Parser): grammarFileName = "" atn = ATNDeserializer().deserialize(serializedATN()) - decisionsToDFA = [ DFA(ds, i) for i, ds in enumerate(atn.decisionToState) ] + decisionsToDFA = [DFA(ds, i) for i, ds in enumerate(atn.decisionToState)] sharedContextCache = PredictionContextCache() - literalNames = [ }; null="\"\\"", separator=", ", wrap, anchor> ] + literalNames = [ + }; null="\"\\"", separator=",\n", anchor>, + ] - symbolicNames = [ }; null="\"\\"", separator=", ", wrap, anchor> ] + symbolicNames = [ + }; null="\"\\"", separator=",\n", anchor>, + ] = }; separator="\n", wrap, anchor> - ruleNames = [ "}; separator=", ", wrap, anchor> ] + ruleNames = [ + "}; separator=",\n", wrap, anchor>, + ] EOF = .EOF - =}; separator="\n", wrap, anchor> + = }; separator="\n", wrap, anchor> @@ -164,14 +169,14 @@ class ( Parser ): - def sempred(self, localctx:RuleContext, ruleIndex:int, predIndex:int): - if self._predicates == None: - self._predicates = dict() + def sempred(self, localctx: RuleContext, ruleIndex: int, predIndex: int): + if self._predicates is None: + self._predicates = {} ] = self._sempred}; separator="\n "> pred = self._predicates.get(ruleIndex, None) if pred is None: - raise Exception("No predicate with index:" + str(ruleIndex)) + raise Exception(f"No predicate with index: {ruleIndex}") else: return pred(localctx, predIndex) @@ -184,25 +189,25 @@ class ( Parser ): dumpActions(recog, argFuncs, actionFuncs, sempredFuncs) ::= << -def action(self, localctx:RuleContext, ruleIndex:int, actionIndex:int): +def action(self, localctx: RuleContext, ruleIndex: int, actionIndex: int) -> None: if self._actions is None: - actions = dict() + actions = {} ] = self._action }; separator="\n"> + actions[] = self._action}; separator="\n"> self._actions = actions action = self._actions.get(ruleIndex, None) if action is not None: action(localctx, actionIndex) else: - raise Exception("No registered action for:" + str(ruleIndex)) + raise Exception(f"No registered action for: {ruleIndex}") -def sempred(self, localctx:RuleContext, ruleIndex:int, predIndex:int): +def sempred(self, localctx: RuleContext, ruleIndex: int, predIndex: int): if self._predicates is None: - preds = dict() + preds = {} ] = self._sempred}; separator="\n"> self._predicates = preds @@ -210,27 +215,34 @@ def sempred(self, localctx:RuleContext, ruleIndex:int, predIndex:int): if pred is not None: return pred(localctx, predIndex) else: - raise Exception("No registered predicate for:" + str(ruleIndex)) + raise Exception(f"No registered predicate for: {ruleIndex}") >> parser_ctor(p) ::= << -def __init__(self, input:TokenStream, output:TextIO = sys.stdout): +def __init__( + self, + input: TokenStream, + output: typing.TextIO = sys.stdout, +) -> None: super().__init__(input, output) self.checkVersion("") - self._interp = ParserATNSimulator(self, self.atn, self.decisionsToDFA, self.sharedContextCache) - self._predicates = None - + self._interp = ParserATNSimulator( + self, + self.atn, + self.decisionsToDFA, + self.sharedContextCache, + ) + self._predicates: typing.Optional[typing.Dict[int, typing.Callable]] = None >> /* This generates a private method since the actionIndex is generated, making an * overriding implementation impossible to maintain. */ RuleActionFunction(r, actions) ::= << - -def _action(self, localctx: , actionIndex:int): +def _action(self, localctx: , actionIndex: int) -> None: if actionIndex == : @@ -238,14 +250,14 @@ def _action(self, localctx: , actionIndex:int): elif actionIndex == : - }; separator="\n"> + }; separator="\n\n"> >> /* This generates a private method since the predIndex is generated, making an * overriding implementation impossible to maintain. */ RuleSempredFunction(r, actions) ::= << -def _sempred(self, localctx:, predIndex:int): +def _sempred(self, localctx: , predIndex: int) -> int: if predIndex == : @@ -263,9 +275,13 @@ RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,fina }; separator="\n"> -def (self:}>): +def (self: }>): - localctx = .(self, self._ctx, self.state}>) + localctx = .( + self, + self._ctx, + self.state}>, + ) self.enterRule(localctx, , self.RULE_) @@ -295,7 +311,7 @@ LeftRecursiveRuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs, }; separator="\n"> -def (self, _p:int=0, }>): +def (self, _p: int = 0, }>) -> .: _parentctx = self._ctx _parentState = self.state localctx = .(self, self._ctx, _parentState}>) @@ -400,7 +416,7 @@ self.state = self._errHandler.sync(self) = _input.LT(1) -la_ = self._interp.adaptivePredict(self._input,,self._ctx) +la_ = self._interp.adaptivePredict(self._input, , self._ctx) : @@ -412,7 +428,7 @@ if la_ == : OptionalBlock(choice, alts, error) ::= << self.state = self._errHandler.sync(self) -la_ = self._interp.adaptivePredict(self._input,,self._ctx) +la_ = self._interp.adaptivePredict(self._input, , self._ctx) +1: @@ -423,31 +439,31 @@ if la_ == +1: 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: - if _alt==1+1: +_alt = self._interp.adaptivePredict(self._input, , self._ctx) +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) + _alt = self._interp.adaptivePredict(self._input, , self._ctx) >> PlusBlock(choice, alts, error) ::= << self.state = self._errHandler.sync(self) -_alt = 1+1 -while _alt!= and _alt!=ATN.INVALID_ALT_NUMBER: +_alt = 1 + 1 +while _alt != and _alt != ATN.INVALID_ALT_NUMBER: +1: +if _alt == + 1: }; separator="\nel"> else: self.state = self._errHandler.sync(self) - _alt = self._interp.adaptivePredict(self._input,,self._ctx) + _alt = self._interp.adaptivePredict(self._input, , self._ctx) >> @@ -482,7 +498,7 @@ offsetShiftType(shiftAmount, offset) ::= <% %> bitsetInlineComparison(s, bits) ::= <% -==}; separator=" or "> + == }; separator=" or "> %> cases(tokens) ::= << @@ -606,7 +622,7 @@ def _list(self): >> ContextTokenListIndexedGetterDecl(t) ::= << -def (self, i:int=None): +def (self, i: typing.Optional[int] = None): if i is None: return self.getTokens(.) else: @@ -615,7 +631,7 @@ def (self, i:int=None): ContextRuleGetterDecl(r) ::= << def (self): - return self.getTypedRuleContext(.,0) + return self.getTypedRuleContext(., 0) >> @@ -627,11 +643,11 @@ def _list(self): >> ContextRuleListIndexedGetterDecl(r) ::= << -def (self, i:int=None): +def (self, i: typing.Optional[int] = None): if i is None: return self.getTypedRuleContexts(.) else: - return self.getTypedRuleContext(.,i) + return self.getTypedRuleContext(., i) >> @@ -652,9 +668,14 @@ CaptureNextTokenType(d) ::= " = self._input.LA(1)" StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers,signatures) ::= << class (ParserRuleContext): - __slots__ = 'parser' - - def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1:=None}>): + __slots__ = "parser" + + def __init__( + self, + parser, + parent: typing.Optional[ParserRuleContext] = None, + invokingState: int = -1: = None}>, + ) -> None: super().__init__(parent, invokingState) self.parser = parser }; separator="\n"> @@ -666,7 +687,7 @@ class (Parse return .RULE_ - def copyFrom(self, ctx:ParserRuleContext): + def copyFrom(self, ctx: ParserRuleContext): super().copyFrom(ctx) = ctx.}; separator="\n"> @@ -679,7 +700,11 @@ class (Parse AltLabelStructDecl(struct,attrs,getters,dispatchMethods) ::= << class (Context): - def __init__(self, parser, ctx:ParserRuleContext): # actually a .Context + def __init__( + self, + parser, + ctx: ParserRuleContext, + ) -> None: super().__init__(parser) }; separator="\n"> self.copyFrom(ctx) @@ -691,15 +716,15 @@ class (Context): >> ListenerDispatchMethod(method) ::= << -def enterexitRule(self, listener:ParseTreeListener): +def enterexitRule(self, listener: ParseTreeListener): if hasattr( listener, "enterexit" ): listener.enterexit(self) >> VisitorDispatchMethod(method) ::= << -def accept(self, visitor:ParseTreeVisitor): - if hasattr( visitor, "visit" ): +def accept(self, visitor: ParseTreeVisitor): + if hasattr(visitor, "visit"): return visitor.visit(self) else: return visitor.visitChildren(self) @@ -758,13 +783,10 @@ _prevctx = localctx LexerFile(lexerFile, lexer, namedActions) ::= << -from antlr4 import * -from io import StringIO import sys -if sys.version_info[1] > 5: - from typing import TextIO -else: - from typing.io import TextIO +import typing + +from antlr4 import * @@ -781,11 +803,12 @@ else: + class (Lexer): atn = ATNDeserializer().deserialize(serializedATN()) - decisionsToDFA = [ DFA(ds, i) for i, ds in enumerate(atn.decisionToState) ] + decisionsToDFA = [DFA(ds, i) for i, ds in enumerate(atn.decisionToState)] = }; separator="\n"> @@ -799,24 +822,46 @@ class (Lexer): = }; separator="\n", wrap, anchor> - channelNames = [ u"DEFAULT_TOKEN_CHANNEL", u"HIDDEN", "}; separator=", ", wrap, anchor> ] + channelNames = [ + "DEFAULT_TOKEN_CHANNEL", + "HIDDEN", + "}; separator=",\n", anchor> + , + ] - modeNames = [ "}; separator=", ", wrap, anchor> ] + modeNames = [ + "}; separator=",\n", anchor>, + ] - literalNames = [ "\", - }; separator=", ", wrap, anchor> ] + literalNames = [ + "\", + }; separator=",\n", anchor>, + ] - symbolicNames = [ "\", - }; separator=", ", wrap, anchor> ] + symbolicNames = [ + "\", + }; separator=",\n", anchor>, + ] - ruleNames = [ "}; separator=", ", wrap, anchor> ] + ruleNames = [ + "}; separator=",\n", anchor>, + ] grammarFileName = "" - def __init__(self, input=None, output:TextIO = sys.stdout): + def __init__( + self, + input = None, + output: typing.TextIO = sys.stdout, + ) -> None: super().__init__(input, output) self.checkVersion("") - self._interp = LexerATNSimulator(self, self.atn, self.decisionsToDFA, PredictionContextCache()) + self._interp = LexerATNSimulator( + self, + self.atn, + self.decisionsToDFA, + PredictionContextCache(), + ) self._actions = None self._predicates = None @@ -829,7 +874,7 @@ class (Lexer): SerializedATN(model) ::= << def serializedATN(): return [ - }; separator=",", wrap> + }; separator=", ", wrap> ] >>