From 66d055a57204fa002e1ec9c632fe1fc2e9a2763d Mon Sep 17 00:00:00 2001 From: Hobson Lane Date: Mon, 21 Apr 2014 22:08:17 -0700 Subject: [PATCH 1/3] gitignore .pyc and fix vistory.py to not fail when no node children --- .gitignore | 2 ++ java2python/compiler/visitor.py | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7fdea58 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +*.egg-info diff --git a/java2python/compiler/visitor.py b/java2python/compiler/visitor.py index 4109883..1493046 100644 --- a/java2python/compiler/visitor.py +++ b/java2python/compiler/visitor.py @@ -440,7 +440,7 @@ def acceptFor(self, node, memo): else: whileStat.expr.walk(cond, memo) whileBlock = self.factory.methodContent(parent=self) - if not node.firstChildOfType(tokens.BLOCK_SCOPE).children: + if not node.firstChildOfType(tokens.BLOCK_SCOPE) or not node.firstChildOfType(tokens.BLOCK_SCOPE).children: self.factory.expr(left='pass', parent=whileBlock) else: whileBlock.walk(node.firstChildOfType(tokens.BLOCK_SCOPE), memo) @@ -512,7 +512,7 @@ def acceptSwitch(self, node, memo): lblNode = node.firstChildOfType(tokens.SWITCH_BLOCK_LABEL_LIST) caseNodes = lblNode.children # empty switch statement - if not len(caseNodes): + if not caseNodes: return # we have at least one node... parExpr = self.factory.expr(parent=self) @@ -535,7 +535,7 @@ def acceptSwitch(self, node, memo): caseContent = self.factory.methodContent(parent=self) for child in caseNode.children[1:]: caseContent.walk(child, memo) - if not caseNode.children[1:]: + if not caseNode.children or not caseNode.children[1:]: self.factory.expr(left='pass', parent=caseContent) if isDefault: if isFirst: @@ -607,7 +607,7 @@ def acceptWhile(self, node, memo): parNode, blkNode = node.children whileStat = self.factory.statement('while', fs=FS.lsrc, parent=self) whileStat.expr.walk(parNode, memo) - if not blkNode.children: + if not blkNode or not blkNode.children: self.factory.expr(left='pass', parent=whileStat) else: whileStat.walk(blkNode, memo) From 2dc1c67732dbb64e051d13bbb75435ad03ffcfd3 Mon Sep 17 00:00:00 2001 From: Hobson Lane Date: Mon, 21 Apr 2014 22:29:07 -0700 Subject: [PATCH 2/3] fix None.type error --- java2python/lang/selector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java2python/lang/selector.py b/java2python/lang/selector.py index 22b531a..099149d 100644 --- a/java2python/lang/selector.py +++ b/java2python/lang/selector.py @@ -160,7 +160,7 @@ def __init__(self, key, value=None): self.value = value def __call__(self, tree): - if tree.token.type == self.key: + if tree.token and tree.token.type == self.key: if self.value is None or self.value == tree.token.text: yield tree From e40c2c29e44d64eb8e717a1aa774baff44fa948a Mon Sep 17 00:00:00 2001 From: Hobson Lane Date: Sat, 31 May 2014 13:04:51 -0700 Subject: [PATCH 3/3] warnings instead of fatal exceptions --- java2python/compiler/visitor.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/java2python/compiler/visitor.py b/java2python/compiler/visitor.py index 1493046..ec6d13d 100644 --- a/java2python/compiler/visitor.py +++ b/java2python/compiler/visitor.py @@ -14,13 +14,15 @@ from functools import reduce, partial from itertools import ifilter, ifilterfalse, izip, tee -from logging import debug, warn +from logging import debug, warn, warning from re import compile as recompile, sub as resub +from traceback import format_exc from java2python.lang import tokens from java2python.lib import FS + class Memo(object): """ Memo -> AST walking luggage. """ @@ -35,7 +37,11 @@ class Base(object): def accept(self, node, memo): """ Accept a node, possibly creating a child visitor. """ - tokType = tokens.map.get(node.token.type) + if node and node.token: + tokType = tokens.map.get(node.token.type) + else: + warning(format_exc()) + return missing = lambda node, memo:self call = getattr(self, 'accept{0}'.format(tokens.title(tokType)), missing) if call is missing: @@ -79,7 +85,11 @@ def walk(self, tree, memo=None): return memo = Memo() if memo is None else memo comIns = self.insertComments - comIns(self, tree, tree.tokenStartIndex, memo) + try: + comIns(self, tree, tree.tokenStartIndex, memo) + except: + warning(format_exc()) + pass visitor = self.accept(tree, memo) if visitor: for child in tree.children: