1414
1515from functools import reduce , partial
1616from itertools import ifilter , ifilterfalse , izip , tee
17- from logging import debug , warn
17+ from logging import debug , warn , warning
1818from re import compile as recompile , sub as resub
19+ from traceback import format_exc
1920
2021from java2python .lang import tokens
2122from java2python .lib import FS
2223
2324
25+
2426class Memo (object ):
2527 """ Memo -> AST walking luggage. """
2628
@@ -35,7 +37,11 @@ class Base(object):
3537
3638 def accept (self , node , memo ):
3739 """ 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
3945 missing = lambda node , memo :self
4046 call = getattr (self , 'accept{0}' .format (tokens .title (tokType )), missing )
4147 if call is missing :
@@ -79,7 +85,11 @@ def walk(self, tree, memo=None):
7985 return
8086 memo = Memo () if memo is None else memo
8187 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
8393 visitor = self .accept (tree , memo )
8494 if visitor :
8595 for child in tree .children :
@@ -129,12 +139,7 @@ def acceptType(self, node, memo):
129139 acceptAt = makeAcceptType ('at' )
130140 acceptClass = makeAcceptType ('klass' )
131141 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' )
138143
139144
140145class Module (TypeAcceptor , Base ):
@@ -233,10 +238,7 @@ def acceptVarDeclaration(self, node, memo):
233238 if node .firstChildOfType (tokens .TYPE ).firstChildOfType (tokens .ARRAY_DECLARATOR_LIST ):
234239 val = assgnExp .pushRight ('[]' )
235240 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 ))
240242 return self
241243
242244
@@ -366,7 +368,7 @@ class Interface(Class):
366368 """ Interface -> accepts AST branches for Java interfaces. """
367369
368370
369- class MethodContent (VarAcceptor , Base ):
371+ class MethodContent (Base ):
370372 """ MethodContent -> accepts trees for blocks within methods. """
371373
372374 def acceptAssert (self , node , memo ):
@@ -407,10 +409,6 @@ def acceptCatch(self, node, memo):
407409
408410 def acceptContinue (self , node , memo ):
409411 """ 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 )
414412 contStat = self .factory .statement ('continue' , fs = FS .lsr , parent = self )
415413 if len (node .children ):
416414 warn ('Detected unhandled continue statement with label; generated code incorrect.' )
@@ -452,7 +450,7 @@ def acceptFor(self, node, memo):
452450 else :
453451 whileStat .expr .walk (cond , memo )
454452 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 :
456454 self .factory .expr (left = 'pass' , parent = whileBlock )
457455 else :
458456 whileBlock .walk (node .firstChildOfType (tokens .BLOCK_SCOPE ), memo )
@@ -524,12 +522,12 @@ def acceptSwitch(self, node, memo):
524522 lblNode = node .firstChildOfType (tokens .SWITCH_BLOCK_LABEL_LIST )
525523 caseNodes = lblNode .children
526524 # empty switch statement
527- if not len ( caseNodes ) :
525+ if not caseNodes :
528526 return
529527 # we have at least one node...
530528 parExpr = self .factory .expr (parent = self )
531529 parExpr .walk (parNode , memo )
532- eqFs = FS .l + ' == ' + FS .r
530+ eqFs = FS .l + '== ' + FS .r
533531 for caseIdx , caseNode in enumerate (caseNodes ):
534532 isDefault , isFirst = caseNode .type == tokens .DEFAULT , caseIdx == 0
535533
@@ -547,7 +545,7 @@ def acceptSwitch(self, node, memo):
547545 caseContent = self .factory .methodContent (parent = self )
548546 for child in caseNode .children [1 :]:
549547 caseContent .walk (child , memo )
550- if not caseNode .children [1 :]:
548+ if not caseNode .children or not caseNode . children [1 :]:
551549 self .factory .expr (left = 'pass' , parent = caseContent )
552550 if isDefault :
553551 if isFirst :
@@ -619,13 +617,13 @@ def acceptWhile(self, node, memo):
619617 parNode , blkNode = node .children
620618 whileStat = self .factory .statement ('while' , fs = FS .lsrc , parent = self )
621619 whileStat .expr .walk (parNode , memo )
622- if not blkNode .children :
620+ if not blkNode or not blkNode .children :
623621 self .factory .expr (left = 'pass' , parent = whileStat )
624622 else :
625623 whileStat .walk (blkNode , memo )
626624
627625
628- class Method (ModifiersAcceptor , MethodContent ):
626+ class Method (VarAcceptor , ModifiersAcceptor , MethodContent ):
629627 """ Method -> accepts AST branches for method-level objects. """
630628
631629 def acceptFormalParamStdDecl (self , node , memo ):
@@ -847,7 +845,7 @@ def acceptThisConstructorCall(self, node, memo):
847845
848846 def acceptStaticArrayCreator (self , node , memo ):
849847 """ 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}' )
851849 self .right .left = self .factory .expr ()
852850 self .right .left .walk (node .firstChildOfType (tokens .EXPR ), memo )
853851
0 commit comments