14
14
15
15
from functools import reduce , partial
16
16
from itertools import ifilter , ifilterfalse , izip , tee
17
- from logging import debug , warn
17
+ from logging import debug , warn , warning
18
18
from re import compile as recompile , sub as resub
19
+ from traceback import format_exc
19
20
20
21
from java2python .lang import tokens
21
22
from java2python .lib import FS
22
23
23
24
25
+
24
26
class Memo (object ):
25
27
""" Memo -> AST walking luggage. """
26
28
@@ -35,7 +37,11 @@ class Base(object):
35
37
36
38
def accept (self , node , memo ):
37
39
""" Accept a node, possibly creating a child visitor. """
38
- tokType = tokens .map .get (node .token .type )
40
+ if node and node .token :
41
+ tokType = tokens .map .get (node .token .type )
42
+ else :
43
+ warning (format_exc ())
44
+ return
39
45
missing = lambda node , memo :self
40
46
call = getattr (self , 'accept{0}' .format (tokens .title (tokType )), missing )
41
47
if call is missing :
@@ -79,7 +85,11 @@ def walk(self, tree, memo=None):
79
85
return
80
86
memo = Memo () if memo is None else memo
81
87
comIns = self .insertComments
82
- comIns (self , tree , tree .tokenStartIndex , memo )
88
+ try :
89
+ comIns (self , tree , tree .tokenStartIndex , memo )
90
+ except :
91
+ warning (format_exc ())
92
+ pass
83
93
visitor = self .accept (tree , memo )
84
94
if visitor :
85
95
for child in tree .children :
@@ -129,12 +139,7 @@ def acceptType(self, node, memo):
129
139
acceptAt = makeAcceptType ('at' )
130
140
acceptClass = makeAcceptType ('klass' )
131
141
acceptEnum = makeAcceptType ('enum' )
132
- _acceptInterface = makeAcceptType ('interface' )
133
-
134
- def acceptInterface (self , node , memo ):
135
- module = self .parents (lambda x :x .isModule ).next ()
136
- module .needsAbstractHelpers = True
137
- return self ._acceptInterface (node , memo )
142
+ acceptInterface = makeAcceptType ('interface' )
138
143
139
144
140
145
class Module (TypeAcceptor , Base ):
@@ -233,10 +238,7 @@ def acceptVarDeclaration(self, node, memo):
233
238
if node .firstChildOfType (tokens .TYPE ).firstChildOfType (tokens .ARRAY_DECLARATOR_LIST ):
234
239
val = assgnExp .pushRight ('[]' )
235
240
else :
236
- if node .firstChildOfType (tokens .TYPE ).firstChild ().type != tokens .QUALIFIED_TYPE_IDENT :
237
- val = assgnExp .pushRight ('{0}()' .format (identExp .type ))
238
- else :
239
- val = assgnExp .pushRight ('None' )
241
+ val = assgnExp .pushRight ('{0}()' .format (identExp .type ))
240
242
return self
241
243
242
244
@@ -366,7 +368,7 @@ class Interface(Class):
366
368
""" Interface -> accepts AST branches for Java interfaces. """
367
369
368
370
369
- class MethodContent (VarAcceptor , Base ):
371
+ class MethodContent (Base ):
370
372
""" MethodContent -> accepts trees for blocks within methods. """
371
373
372
374
def acceptAssert (self , node , memo ):
@@ -407,10 +409,6 @@ def acceptCatch(self, node, memo):
407
409
408
410
def acceptContinue (self , node , memo ):
409
411
""" Accept and process a continue statement. """
410
- parent = node .parents (lambda x : x .type in {tokens .FOR , tokens .FOR_EACH , tokens .DO , tokens .WHILE }).next ()
411
- if parent .type == tokens .FOR :
412
- updateStat = self .factory .expr (parent = self )
413
- updateStat .walk (parent .firstChildOfType (tokens .FOR_UPDATE ), memo )
414
412
contStat = self .factory .statement ('continue' , fs = FS .lsr , parent = self )
415
413
if len (node .children ):
416
414
warn ('Detected unhandled continue statement with label; generated code incorrect.' )
@@ -452,7 +450,7 @@ def acceptFor(self, node, memo):
452
450
else :
453
451
whileStat .expr .walk (cond , memo )
454
452
whileBlock = self .factory .methodContent (parent = self )
455
- if not node .firstChildOfType (tokens .BLOCK_SCOPE ).children :
453
+ if not node .firstChildOfType (tokens .BLOCK_SCOPE ) or not node . firstChildOfType ( tokens . BLOCK_SCOPE ) .children :
456
454
self .factory .expr (left = 'pass' , parent = whileBlock )
457
455
else :
458
456
whileBlock .walk (node .firstChildOfType (tokens .BLOCK_SCOPE ), memo )
@@ -524,12 +522,12 @@ def acceptSwitch(self, node, memo):
524
522
lblNode = node .firstChildOfType (tokens .SWITCH_BLOCK_LABEL_LIST )
525
523
caseNodes = lblNode .children
526
524
# empty switch statement
527
- if not len ( caseNodes ) :
525
+ if not caseNodes :
528
526
return
529
527
# we have at least one node...
530
528
parExpr = self .factory .expr (parent = self )
531
529
parExpr .walk (parNode , memo )
532
- eqFs = FS .l + ' == ' + FS .r
530
+ eqFs = FS .l + '== ' + FS .r
533
531
for caseIdx , caseNode in enumerate (caseNodes ):
534
532
isDefault , isFirst = caseNode .type == tokens .DEFAULT , caseIdx == 0
535
533
@@ -547,7 +545,7 @@ def acceptSwitch(self, node, memo):
547
545
caseContent = self .factory .methodContent (parent = self )
548
546
for child in caseNode .children [1 :]:
549
547
caseContent .walk (child , memo )
550
- if not caseNode .children [1 :]:
548
+ if not caseNode .children or not caseNode . children [1 :]:
551
549
self .factory .expr (left = 'pass' , parent = caseContent )
552
550
if isDefault :
553
551
if isFirst :
@@ -619,13 +617,13 @@ def acceptWhile(self, node, memo):
619
617
parNode , blkNode = node .children
620
618
whileStat = self .factory .statement ('while' , fs = FS .lsrc , parent = self )
621
619
whileStat .expr .walk (parNode , memo )
622
- if not blkNode .children :
620
+ if not blkNode or not blkNode .children :
623
621
self .factory .expr (left = 'pass' , parent = whileStat )
624
622
else :
625
623
whileStat .walk (blkNode , memo )
626
624
627
625
628
- class Method (ModifiersAcceptor , MethodContent ):
626
+ class Method (VarAcceptor , ModifiersAcceptor , MethodContent ):
629
627
""" Method -> accepts AST branches for method-level objects. """
630
628
631
629
def acceptFormalParamStdDecl (self , node , memo ):
@@ -847,7 +845,7 @@ def acceptThisConstructorCall(self, node, memo):
847
845
848
846
def acceptStaticArrayCreator (self , node , memo ):
849
847
""" Accept and process a static array expression. """
850
- self .right = self .factory .expr (fs = '[None] * {left}' )
848
+ self .right = self .factory .expr (fs = '[None]* {left}' )
851
849
self .right .left = self .factory .expr ()
852
850
self .right .left .walk (node .firstChildOfType (tokens .EXPR ), memo )
853
851
0 commit comments