Skip to content

gitignore .pyc; make vistory.py more robust to lack of node children #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
*.egg-info
24 changes: 17 additions & 7 deletions java2python/compiler/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. """

Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -440,7 +450,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)
Expand Down Expand Up @@ -512,7 +522,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)
Expand All @@ -535,7 +545,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:
Expand Down Expand Up @@ -607,7 +617,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)
Expand Down
2 changes: 1 addition & 1 deletion java2python/lang/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down