Skip to content

Commit fdff9c4

Browse files
author
Troy Melhase
committed
Corrects expression parents in switch blocks, which allows for finding alternate idents.
1 parent 612bcb9 commit fdff9c4

File tree

6 files changed

+53
-5
lines changed

6 files changed

+53
-5
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
*.py[co]
2+
3+
# Packages
4+
*.egg
5+
*.egg-info
6+
7+
.DS_Store
8+
9+
java2python/lang/Java.tokens
10+
java2python/lang/JavaLexer.py
11+
java2python/lang/JavaParser.py

java2python/compiler/visitor.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def acceptFunctionMethodDecl(self, node, memo):
250250
ident = node.firstChildOfType(tokens.IDENT)
251251
type = node.firstChildOfType(tokens.TYPE).children[0].text
252252
mods = node.firstChildOfType(tokens.MODIFIER_LIST)
253-
return self.factory.method(name=ident.text, type=type, parent=self)
253+
return self.factory.method(name=ident.text, type=type, parent=self)
254254

255255
def acceptVoidMethodDecl(self, node, memo):
256256
""" Accept and process a void method declaration. """
@@ -456,7 +456,7 @@ def acceptSwitch(self, node, memo):
456456
if not len(caseNodes):
457457
return
458458
# we have at least one node...
459-
parExpr = self.factory.expr()
459+
parExpr = self.factory.expr(parent=self)
460460
parExpr.walk(parNode)
461461
eqFs = FS.l + '==' + FS.r
462462
for caseIdx, caseNode in enumerate(caseNodes):
@@ -470,7 +470,7 @@ def acceptSwitch(self, node, memo):
470470
caseExpr = self.factory.statement('else', fs=FS.lc, parent=self)
471471

472472
if not isDefault:
473-
right = self.factory.expr()
473+
right = self.factory.expr(parent=parExpr)
474474
right.walk(caseNode.firstChildOfType(tokens.EXPR))
475475
caseExpr.expr.right = self.factory.expr(left=parExpr, right=right, fs=eqFs)
476476
caseContent = self.factory.methodContent(parent=self)
@@ -486,6 +486,7 @@ def acceptSwitch(self, node, memo):
486486
caseContent.walk(child, memo)
487487
if not caseNode.children:
488488
self.factory.expr(left='pass', parent=caseContent)
489+
self.children.remove(parExpr)
489490

490491
def acceptThrow(self, node, memo):
491492
""" Accept and process a throw statement. """

java2python/config/default.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,19 @@
141141
(Type('TRUE'), transform.true2True),
142142
(Type('IDENT'), transform.keywordSafeIdent),
143143

144+
144145
(Type('FLOATING_POINT_LITERAL'),
145146
transform.syntaxSafeFloatLiteral),
146147

148+
(Type('TYPE') > Type('BOOLEAN'),
149+
transform.typeSub),
150+
151+
(Type('TYPE') > Type('DOUBLE'),
152+
transform.typeSub),
153+
147154
(Type('TYPE') > Type('QUALIFIED_TYPE_IDENT') > Type('IDENT'),
148-
transform.typeSub)
155+
transform.typeSub),
156+
149157
]
150158

151159

@@ -184,6 +192,7 @@
184192

185193
typeSubs = {
186194
'Boolean' : 'bool',
195+
'boolean' : 'bool',
187196
'IndexOutOfBoundsException' : 'IndexError',
188197
'Integer' : 'int',
189198
'Object' : 'object',

java2python/mod/basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def simpleImports(module, expr):
4343

4444

4545
def commentedPackages(module, expr):
46-
module.factory.comment(parent=module, left=expr, fs='package: {left}')
46+
module.factory.comment(parent=module, left=expr, fs='# package: {left}')
4747

4848

4949
def namespacePackages(module, expr):

test/Switch5.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// tests switches with class attributes
2+
3+
class Switch5 {
4+
public static final int RT_VOLUME = 48;
5+
6+
public static String getField( int val) {
7+
switch( val) {
8+
case RT_VOLUME: return "rt volume";
9+
default: return "unknown";
10+
11+
}
12+
}
13+
14+
public static void main(String[] args) {
15+
Switch5 c = new Switch5();
16+
System.out.println( c.getField(48) );
17+
System.out.println( c.getField(49) );
18+
}
19+
20+
21+
}

test/TypeTransform.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class TypeTransform {
2+
public boolean b;
3+
public double d;
4+
5+
public static void main(String[] args) {}
6+
}

0 commit comments

Comments
 (0)