Skip to content

Commit 3294b04

Browse files
author
Troy Melhase
committed
Corrects empty while loops.
Corrects break statement removal on switch-to-if statements. Corrects comment placement within expressions.
1 parent f6bcd6c commit 3294b04

File tree

5 files changed

+75
-7
lines changed

5 files changed

+75
-7
lines changed

java2python/compiler/visitor.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,21 @@ def insertComments(self, tmpl, tree, index, memo):
4141
prefix = self.config.last('commentPrefix', '# ')
4242
cache, parser, comTypes = memo.comments, tree.parser, tokens.commentTypes
4343
comNew = lambda t:t.type in comTypes and t.index not in cache
44+
4445
for tok in ifilter(comNew, parser.input.tokens[memo.last:index]):
4546
cache.add(tok.index)
46-
if tmpl.isExpression and tok.line==parser.input.tokens[index].line:
47-
tmpl.tail += prefix if not tmpl.tail.startswith(prefix) else ''
48-
tmpl.tail += ''.join(self.stripComment(tok.text))
47+
48+
# loop over parents until we find the top expression
49+
base = tmpl
50+
while base:
51+
if base and base.parent and base.parent.isExpression:
52+
base = base.parent
53+
else:
54+
break
55+
56+
if hasattr(base, 'tail') and tok.line==parser.input.tokens[index].line:
57+
base.tail += prefix if not base.tail.startswith(prefix) else ''
58+
base.tail += ''.join(self.stripComment(tok.text))
4959
else:
5060
for line in self.stripComment(tok.text):
5161
self.factory.comment(left=prefix, right=line, parent=self)
@@ -250,12 +260,14 @@ def acceptFunctionMethodDecl(self, node, memo):
250260
ident = node.firstChildOfType(tokens.IDENT)
251261
type = node.firstChildOfType(tokens.TYPE).children[0].text
252262
mods = node.firstChildOfType(tokens.MODIFIER_LIST)
263+
self.variables.append(ident.text)
253264
return self.factory.method(name=ident.text, type=type, parent=self)
254265

255266
def acceptVoidMethodDecl(self, node, memo):
256267
""" Accept and process a void method declaration. """
257268
ident = node.firstChildOfType(tokens.IDENT)
258269
mods = node.firstChildOfType(tokens.MODIFIER_LIST)
270+
self.variables.append(ident.text)
259271
return self.factory.method(name=ident.text, type='void', parent=self)
260272

261273

@@ -352,9 +364,17 @@ def acceptAssert(self, node, memo):
352364

353365
def acceptBreak(self, node, memo):
354366
""" Accept and process a break statement. """
355-
if node.parent and node.parent.type in (tokens.CASE, tokens.DEFAULT):
356-
return
357-
breakStat = self.factory.statement('break', parent=self)
367+
# possible parents of a break statement: switch, while, do, for
368+
# we want to skip inserting a break statement if we're inside a switch.
369+
insert, ok_types = True, [tokens.WHILE, tokens.DO, tokens.FOR]
370+
for parent in node.parents():
371+
if parent.type == tokens.SWITCH:
372+
insert = False
373+
break
374+
if parent.type in ok_types:
375+
break
376+
if insert:
377+
breakStat = self.factory.statement('break', parent=self)
358378

359379
def acceptCatch(self, node, memo):
360380
""" Accept and process a catch statement. """
@@ -536,7 +556,10 @@ def acceptWhile(self, node, memo):
536556
parNode, blkNode = node.children
537557
whileStat = self.factory.statement('while', fs=FS.lsrc, parent=self)
538558
whileStat.expr.walk(parNode, memo)
539-
whileStat.walk(blkNode, memo)
559+
if not blkNode.children:
560+
self.factory.expr(left='pass', parent=whileStat)
561+
else:
562+
whileStat.walk(blkNode, memo)
540563

541564

542565
class Method(VarAcceptor, ModifiersAcceptor, MethodContent):
@@ -720,6 +743,7 @@ def acceptMethodCall(self, node, memo):
720743
arg.left.walk(child, memo)
721744
arg.right = arg = expr(parent=self)
722745

746+
723747
def acceptThisConstructorCall(self, node, memo):
724748
""" Accept and process a 'this(...)' constructor call. """
725749
self.acceptMethodCall(node, memo)

java2python/lang/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ def parentType(self):
237237
""" Returns the type of the parent tree. """
238238
return self.parent.type
239239

240+
def parents(self, pred=lambda v:True):
241+
""" Yield each parent in the family tree. """
242+
while self:
243+
if pred(self):
244+
yield self
245+
self = self.parent
246+
240247
@property
241248
def parserTokens(self):
242249
""" Returns the sequence of tokens used to create this tree. """

test/Comments3.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Comments3 {
2+
3+
static void bar(int x) {
4+
System.out.println(x);
5+
}
6+
public static void main(String[] args) {
7+
int foo = 100;
8+
if (foo < 38) {
9+
// will never happen
10+
bar(/* funk */ 0);
11+
}
12+
bar(1);
13+
}
14+
}

test/Switch6.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Switch6 {
2+
3+
public static void main(String[] args) {
4+
int x = 0;
5+
6+
switch (x) {
7+
case 0:
8+
if (x==0) {
9+
System.out.println(x);
10+
break;
11+
}
12+
}
13+
14+
}
15+
}

test/While3.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class While3 {
2+
public static void main(String[] args) {
3+
int x = 10;
4+
while (x>10) {
5+
}
6+
System.out.println("ok");
7+
}
8+
}

0 commit comments

Comments
 (0)