Skip to content

Commit ac3fe8b

Browse files
committed
Refine newline handling further for the Python3 target
Signed-off-by: Phil Elson <[email protected]>
1 parent bb10cd6 commit ac3fe8b

File tree

3 files changed

+50
-73
lines changed

3 files changed

+50
-73
lines changed

runtime/Python3/src/antlr4/xpath/XPathLexer.py

+19-29
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,27 @@
77

88
def serializedATN():
99
return [
10-
4, 0, 8, 50, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7,
11-
3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 1, 0, 1, 0, 1,
12-
0, 1, 1, 1, 1, 1, 2, 1, 2, 1, 3, 1, 3, 1, 4, 1, 4, 5, 4, 29, 8,
13-
4, 10, 4, 12, 4, 32, 9, 4, 1, 4, 1, 4, 1, 5, 1, 5, 3, 5, 38, 8,
14-
5, 1, 6, 1, 6, 1, 7, 1, 7, 5, 7, 44, 8, 7, 10, 7, 12, 7, 47, 9,
15-
7, 1, 7, 1, 7, 1, 45, 0, 8, 1, 3, 3, 4, 5, 5, 7, 6, 9, 7, 11, 0,
16-
13, 0, 15, 8, 1, 0, 2, 5, 0, 48, 57, 95, 95, 183, 183, 768, 879,
17-
8255, 8256, 13, 0, 65, 90, 97, 122, 192, 214, 216, 246, 248, 767,
18-
880, 893, 895, 8191, 8204, 8205, 8304, 8591, 11264, 12271, 12289,
19-
55295, 63744, 64975, 65008, 65533, 50, 0, 1, 1, 0, 0, 0, 0, 3, 1,
20-
0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 9, 1, 0, 0, 0, 0,
21-
15, 1, 0, 0, 0, 1, 17, 1, 0, 0, 0, 3, 20, 1, 0, 0, 0, 5, 22, 1,
22-
0, 0, 0, 7, 24, 1, 0, 0, 0, 9, 26, 1, 0, 0, 0, 11, 37, 1, 0, 0,
23-
0, 13, 39, 1, 0, 0, 0, 15, 41, 1, 0, 0, 0, 17, 18, 5, 47, 0, 0,
24-
18, 19, 5, 47, 0, 0, 19, 2, 1, 0, 0, 0, 20, 21, 5, 47, 0, 0, 21,
25-
4, 1, 0, 0, 0, 22, 23, 5, 42, 0, 0, 23, 6, 1, 0, 0, 0, 24, 25, 5,
26-
33, 0, 0, 25, 8, 1, 0, 0, 0, 26, 30, 3, 13, 6, 0, 27, 29, 3, 11,
27-
5, 0, 28, 27, 1, 0, 0, 0, 29, 32, 1, 0, 0, 0, 30, 28, 1, 0, 0, 0,
28-
30, 31, 1, 0, 0, 0, 31, 33, 1, 0, 0, 0, 32, 30, 1, 0, 0, 0, 33,
29-
34, 6, 4, 0, 0, 34, 10, 1, 0, 0, 0, 35, 38, 3, 13, 6, 0, 36, 38,
30-
7, 0, 0, 0, 37, 35, 1, 0, 0, 0, 37, 36, 1, 0, 0, 0, 38, 12, 1, 0,
31-
0, 0, 39, 40, 7, 1, 0, 0, 40, 14, 1, 0, 0, 0, 41, 45, 5, 39, 0,
32-
0, 42, 44, 9, 0, 0, 0, 43, 42, 1, 0, 0, 0, 44, 47, 1, 0, 0, 0, 45,
33-
46, 1, 0, 0, 0, 45, 43, 1, 0, 0, 0, 46, 48, 1, 0, 0, 0, 47, 45,
34-
1, 0, 0, 0, 48, 49, 5, 39, 0, 0, 49, 16, 1, 0, 0, 0, 4, 0, 30, 37,
35-
45, 1, 1, 4, 0
36-
]
10+
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,
11+
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,
12+
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,
13+
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,
14+
57,95,95,183,183,768,879,8255,8256,13,0,65,90,97,122,192,214,216,246,248,
15+
767,880,893,895,8191,8204,8205,8304,8591,11264,12271,12289,55295,63744,
16+
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,
17+
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,
18+
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,
19+
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,
20+
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,
21+
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,
22+
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,
23+
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,
24+
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,
25+
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,
26+
30,37,45,1,1,4,0
27+
] # fmt: skip
3728

3829

3930
class XPathLexer(Lexer):
40-
4131
atn = ATNDeserializer().deserialize(serializedATN())
4232

4333
decisionsToDFA = [DFA(ds, i) for i, ds in enumerate(atn.decisionToState)]
@@ -95,7 +85,7 @@ class XPathLexer(Lexer):
9585

9686
def __init__(
9787
self,
98-
input = None,
88+
input=None,
9989
output: typing.TextIO = sys.stdout,
10090
) -> None:
10191
super().__init__(input, output)

tool/resources/org/antlr/v4/tool/templates/codegen/Python3/Python3.stg

+29-44
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import typing
5555

5656
from antlr4 import *
5757

58+
5859
<namedActions.header>
5960
<parser>
6061

@@ -69,16 +70,17 @@ else:
6970
from <file.parserName> import <file.parserName>
7071
<header>
7172

72-
# This class defines a complete listener for a parse tree produced by <file.parserName>.
73+
7374
class <file.grammarName>Listener(ParseTreeListener):
75+
# This class defines a complete listener for a parse tree produced by <file.parserName>.
7476

7577
<file.listenerNames:{lname |
76-
# Enter a parse tree produced by <file.parserName>#<lname>.
7778
def enter<lname; format="cap">(self, ctx: <file.parserName>.<lname; format="cap">Context) -> None:
79+
# Enter a parse tree produced by <file.parserName>#<lname>.
7880
pass
7981

80-
# Exit a parse tree produced by <file.parserName>#<lname>.
8182
def exit<lname; format="cap">(self, ctx: <file.parserName>.<lname; format="cap">Context) -> None:
83+
# Exit a parse tree produced by <file.parserName>#<lname>.
8284
pass
8385

8486
}; separator="\n">
@@ -97,15 +99,14 @@ else:
9799
from <file.parserName> import <file.parserName>
98100
<header>
99101

100-
# This class defines a complete generic visitor for a parse tree produced by <file.parserName>.
101102

102103
class <file.grammarName>Visitor(ParseTreeVisitor):
104+
# This class defines a complete generic visitor for a parse tree produced by <file.parserName>.
103105

104106
<file.visitorNames:{lname |
105-
# Visit a parse tree produced by <file.parserName>#<lname>.
106107
def visit<lname; format="cap">(self, ctx: <file.parserName>.<lname; format="cap">Context):
108+
# Visit a parse tree produced by <file.parserName>#<lname>.
107109
return self.visitChildren(ctx)
108-
109110
}; separator="\n">
110111

111112
del <file.parserName>
@@ -169,7 +170,6 @@ class <parser.name>(<if(superClass)><superClass><else>Parser<endif>):
169170

170171
<funcs; separator="\n">
171172

172-
173173
<if(sempredFuncs)>
174174
def sempred(self, localctx: RuleContext, ruleIndex: int, predIndex: int):
175175
if self._predicates is None:
@@ -185,8 +185,6 @@ class <parser.name>(<if(superClass)><superClass><else>Parser<endif>):
185185
<sempredFuncs.values; separator="\n">
186186
<endif>
187187

188-
189-
190188
>>
191189

192190
dumpActions(recog, argFuncs, actionFuncs, sempredFuncs) ::= <<
@@ -252,7 +250,7 @@ def <r.name>_action(self, localctx: <r.ctxType>, actionIndex: int) -> None:
252250
<elseif(rest(actions))>
253251
elif actionIndex == <index>:
254252
<actions.(index)>
255-
<endif> }; separator="\n\n">
253+
<endif> }; separator="\n">
256254
>>
257255

258256
/* This generates a private method since the predIndex is generated, making an
@@ -262,23 +260,19 @@ RuleSempredFunction(r, actions) ::= <<
262260
def <r.name>_sempred(self, localctx: <r.ctxType>, predIndex: int) -> int:
263261
<actions:{index|
264262
<if(first(actions))>
265-
if predIndex == <index>:
266-
return <actions.(index)>
263+
if predIndex == <index>:
264+
return <actions.(index)>
267265
<elseif(rest(actions))>
268-
elif predIndex == <index>:
269-
return <actions.(index)>
270-
<endif> }; separator="\n">
266+
elif predIndex == <index>:
267+
return <actions.(index)>
268+
<endif> }; separator="">
271269

272270
>>
273271

274272
RuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,namedActions,finallyAction,postamble,exceptions) ::= <<
275-
276273
<ruleCtx>
277-
278274
<altLabelCtxs:{l | <altLabelCtxs.(l)>}; separator="\n">
279-
280275
def <currentRule.escapedName>(self<currentRule.args:{a | , <a.escapedName><if(a.type)>: <a.type><endif>}>):
281-
282276
localctx = <parser.name>.<currentRule.ctxType>(
283277
self,
284278
self._ctx,
@@ -309,10 +303,8 @@ def <currentRule.escapedName>(self<currentRule.args:{a | , <a.escapedName><if(a.
309303
LeftRecursiveRuleFunction(currentRule,args,code,locals,ruleCtx,altLabelCtxs,
310304
namedActions,finallyAction,postamble) ::=
311305
<<
312-
313306
<ruleCtx>
314307
<altLabelCtxs:{l | <altLabelCtxs.(l)>}; separator="\n">
315-
316308
def <currentRule.escapedName>(self, _p: int = 0<if(currentRule.args)>, <args:{a | , <a>}><endif>) -> <parser.name>.<currentRule.ctxType>:
317309
_parentctx = self._ctx
318310
_parentState = self.state
@@ -360,7 +352,6 @@ token = self._input.LA(1)
360352
pass}; separator="\nel">
361353
else:
362354
<error>
363-
364355
>>
365356

366357
LL1OptionalBlock(choice, alts, error) ::= <<
@@ -380,7 +371,6 @@ self._errHandler.sync(self)
380371
<preamble; separator="\n">
381372
if <expr>:
382373
<alts; separator="\n">
383-
384374
<!else if ( !(<followExpr>) ) <error>!>
385375
>>
386376

@@ -394,7 +384,6 @@ while <loopExpr>:
394384
self.state = <choice.loopBackStateNumber>
395385
self._errHandler.sync(self)
396386
<iteration>
397-
398387
>>
399388

400389
LL1PlusBlockSingleAlt(choice, loopExpr, alts, preamble, iteration) ::= <<
@@ -408,7 +397,6 @@ while True:
408397
<iteration>
409398
if not (<loopExpr>):
410399
break
411-
412400
>>
413401

414402
// LL(*) stuff
@@ -424,7 +412,6 @@ if la_ == <i>:
424412
<alt>
425413
pass
426414
}; separator="\nel">
427-
428415
>>
429416

430417
OptionalBlock(choice, alts, error) ::= <<
@@ -435,21 +422,19 @@ la_ = self._interp.adaptivePredict(self._input, <choice.decision>, self._ctx)
435422
if la_ == <i><if(!choice.ast.greedy)>+1<endif>:
436423
<alt>
437424
}; separator="\nel">
438-
439425
>>
440426

441427
StarBlock(choice, alts, sync, iteration) ::= <<
442428
self.state = <choice.stateNumber>
443429
self._errHandler.sync(self)
444430
_alt = self._interp.adaptivePredict(self._input, <choice.decision>, self._ctx)
445-
while _alt!=<choice.exitAlt> and _alt != ATN.INVALID_ALT_NUMBER:
431+
while _alt != <choice.exitAlt> and _alt != ATN.INVALID_ALT_NUMBER:
446432
if _alt == 1<if(!choice.ast.greedy)> + 1<endif>:
447433
<iteration>
448434
<alts> <! should only be one !>
449435
self.state = <choice.loopBackStateNumber>
450436
self._errHandler.sync(self)
451437
_alt = self._interp.adaptivePredict(self._input, <choice.decision>, self._ctx)
452-
453438
>>
454439

455440
PlusBlock(choice, alts, error) ::= <<
@@ -466,7 +451,6 @@ if _alt == <i><if(!choice.ast.greedy)> + 1<endif>:
466451
self.state = <choice.loopBackStateNumber> <! loopback/exit decision !>
467452
self._errHandler.sync(self)
468453
_alt = self._interp.adaptivePredict(self._input, <choice.decision>, self._ctx)
469-
470454
>>
471455

472456
Sync(s) ::= "sync(<s.expecting.name>)"
@@ -525,7 +509,7 @@ CommonSetStuff(m, expr, capture, invert) ::= <<
525509
self.state = <m.stateNumber>
526510
<if(m.labels)><m.labels:{l | <labelref(l)> = }>self._input.LT(1)<endif>
527511
<capture>
528-
<if(invert)>if <m.varName> \<= 0 or <expr><else>if not(<expr>)<endif>:
512+
<if(invert)>if <m.varName> \<= 0 or <expr><else>if not (<expr>)<endif>:
529513
<if(m.labels)><m.labels:{l | <labelref(l)> = }><else> <endif>self._errHandler.recoverInline(self)
530514
else:
531515
self._errHandler.reportMatch(self)
@@ -606,21 +590,23 @@ SetNonLocalAttr(s, rhsChunks) ::= "self.getInvokingContext(<s.ruleIndex>).<s.es
606590

607591
AddToLabelList(a) ::= "<ctx(a.label)>.<a.listName>.append(<labelref(a.label)>)"
608592

609-
TokenDecl(t) ::= "self.<t.escapedName> = None # <TokenLabelType()>"
610-
TokenTypeDecl(t) ::= "self.<t.escapedName> = 0 # <TokenLabelType()> type"
611-
TokenListDecl(t) ::= "self.<t.escapedName> = list() # of <TokenLabelType()>s"
612-
RuleContextDecl(r) ::= "self.<r.escapedName> = None # <r.ctxName>"
613-
RuleContextListDecl(rdecl) ::= "self.<rdecl.escapedName> = list() # of <rdecl.ctxName>s"
593+
TokenDecl(t) ::= "self.<t.escapedName> = None # <TokenLabelType()>"
594+
TokenTypeDecl(t) ::= "self.<t.escapedName> = 0 # <TokenLabelType()> type"
595+
TokenListDecl(t) ::= "self.<t.escapedName> = list() # of <TokenLabelType()>s"
596+
RuleContextDecl(r) ::= "self.<r.escapedName> = None # <r.ctxName>"
597+
RuleContextListDecl(rdecl) ::= "self.<rdecl.escapedName> = list() # of <rdecl.ctxName>s"
614598

615599
ContextTokenGetterDecl(t) ::= <<
616600
def <t.escapedName>(self):
617601
return self.getToken(<parser.name>.<t.name>, 0)
602+
618603
>>
619604

620605
// should never be called
621606
ContextTokenListGetterDecl(t) ::= <<
622607
def <t.name>_list(self):
623608
return self.getTokens(<parser.name>.<t.name>)
609+
624610
>>
625611

626612
ContextTokenListIndexedGetterDecl(t) ::= <<
@@ -629,6 +615,7 @@ def <t.escapedName>(self, i: typing.Optional[int] = None):
629615
return self.getTokens(<parser.name>.<t.name>)
630616
else:
631617
return self.getToken(<parser.name>.<t.name>, i)
618+
632619
>>
633620

634621
ContextRuleGetterDecl(r) ::= <<
@@ -683,8 +670,7 @@ class <struct.escapedName>(<if(contextSuperClass)><contextSuperClass><else>Parse
683670
<attrs:{a | <a>}; separator="\n">
684671
<struct.ctorAttrs:{a | self.<a.escapedName> = <a.escapedName>}; separator="\n">
685672

686-
<getters:{g | <g>}; separator="\n\n">
687-
673+
<getters:{g | <g>}; separator="\n">
688674
def getRuleIndex(self):
689675
return <parser.name>.RULE_<struct.derivedFromName>
690676

@@ -695,6 +681,7 @@ class <struct.escapedName>(<if(contextSuperClass)><contextSuperClass><else>Parse
695681

696682
<endif>
697683
<dispatchMethods; separator="\n">
684+
698685
<extensionMembers; separator="\n">
699686

700687
>>
@@ -719,7 +706,7 @@ class <struct.escapedName>(<currentRule.name; format="cap">Context):
719706

720707
ListenerDispatchMethod(method) ::= <<
721708
def <if(method.isEnter)>enter<else>exit<endif>Rule(self, listener: ParseTreeListener):
722-
if hasattr( listener, "<if(method.isEnter)>enter<else>exit<endif><struct.derivedFromName; format="cap">" ):
709+
if hasattr(listener, "<if(method.isEnter)>enter<else>exit<endif><struct.derivedFromName; format="cap">"):
723710
listener.<if(method.isEnter)>enter<else>exit<endif><struct.derivedFromName; format="cap">(self)
724711

725712
>>
@@ -730,7 +717,6 @@ def accept(self, visitor: ParseTreeVisitor):
730717
return visitor.visit<struct.derivedFromName; format="cap">(self)
731718
else:
732719
return visitor.visitChildren(self)
733-
734720
>>
735721

736722
AttributeDecl(d) ::= "self.<d.escapedName> = <if(d.initValue)><d.initValue><else>None<endif>"
@@ -807,7 +793,6 @@ else:
807793

808794

809795
class <lexer.name>(<if(superClass)><superClass><else>Lexer<endif>):
810-
811796
atn = ATNDeserializer().deserialize(serializedATN())
812797

813798
decisionsToDFA = [DFA(ds, i) for i, ds in enumerate(atn.decisionToState)]
@@ -852,7 +837,7 @@ class <lexer.name>(<if(superClass)><superClass><else>Lexer<endif>):
852837

853838
def __init__(
854839
self,
855-
input = None,
840+
input=None,
856841
output: typing.TextIO = sys.stdout,
857842
) -> None:
858843
super().__init__(input, output)
@@ -875,8 +860,8 @@ class <lexer.name>(<if(superClass)><superClass><else>Lexer<endif>):
875860
SerializedATN(model) ::= <<
876861
def serializedATN():
877862
return [
878-
<model.serialized: {s | <s>}; separator=", ", wrap>
879-
]
863+
<model.serialized: {s | <s>}; separator=",", wrap>
864+
] # fmt: skip
880865
>>
881866

882867
/** Using a type to init value map, try to init a type; if not in table

tool/src/org/antlr/v4/codegen/target/Python3Target.java

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public class Python3Target extends Target {
6262

6363
public Python3Target(CodeGenerator gen) {
6464
super(gen);
65+
// note that lineWidth isn't a hard upper limit.
66+
gen.lineWidth = 79;
6567
}
6668

6769
@Override

0 commit comments

Comments
 (0)