Skip to content

Commit 7befea3

Browse files
author
Troy Melhase
committed
Better handling of all non-block types in and .
1 parent bfc8dec commit 7befea3

File tree

6 files changed

+34
-25
lines changed

6 files changed

+34
-25
lines changed

doc/translation.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ equivalents:
5858
The bit shift right (`>>>`)and bit shift assign right (`>>>=`) operators are
5959
mapped to a function; if java2python detects code that uses either of these, it
6060
replaces the operator with that function and includes the function within the
61-
output. This behavior is controlled by the `modulePrologueHandlers` config
61+
output. This behavior is controlled by the [`modulePrologueHandlers`][6] config
6262
handler.
6363

6464
#### Infix Operators
@@ -114,6 +114,9 @@ statements.
114114

115115
Java `if` statements are translated to equivalent Python `if` statements.
116116

117+
#### import
118+
119+
The processing import statements is delegated to the [`moduleImportDeclarationHandler`][9].
117120

118121
#### for
119122

@@ -183,7 +186,7 @@ The `this` Java keyword is translated to the Python pseudo keyword `self`.
183186

184187
#### instanceof
185188

186-
The `instance of` Java keyword is translated to the `isinstance(…)` Python
189+
The `instanceof` Java keyword is translated to the `isinstance(…)` Python
187190
function call.
188191

189192
#### super
@@ -205,7 +208,7 @@ the `void.class` form, the compiler translates the expression to
205208
### Annotations
206209

207210
Annotations are typically dropped by the compiler. The following Java
208-
annotations have little or no meaning in Python, and are discarded:
211+
annotations have little or no meaning in Python and are discarded:
209212

210213
public protected private abstract final native transient
211214
volatile strictfp
@@ -247,3 +250,4 @@ Java language specification: http://java.sun.com/docs/books/jls/third_edition/h
247250
[6]: https://github.com/natural/java2python/tree/master/doc/customization.md#modulePrologueHandlers
248251
[7]: https://github.com/natural/java2python/tree/master/doc/customization.md#methodPrologueHandlers
249252
[8]: https://github.com/natural/java2python/tree/master/doc/customization.md#commentPrefix
253+
[9]: https://github.com/natural/java2python/tree/master/doc/customization.md#moduleImportDeclarationHandler

doc/usage.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ and the name of an input file and output file:
1212
$ j2py [INPUT] [OUTPUT]
1313
```
1414

15-
Both are optional, but you'll usually supply an input file. For example:
15+
Both are optional, but you'll usually supply an input file:
1616

1717
```bash
1818
$ j2py SourceFile.java
@@ -75,7 +75,7 @@ page.
7575

7676
* `-k`, `--skip-compile`
7777

78-
Do not check the output for valid Python syntax by byte compiling it.
78+
Do not byte compile the output to test for valid Python syntax.
7979

8080
* `-n`, `--no-defaults`
8181

@@ -85,7 +85,7 @@ page.
8585

8686
Disable colorized output.
8787

88-
Colorized output is not available on Windows and this option has no effect
88+
Colorized output is not available on Windows and this option is ignored
8989
there.
9090

9191

java2python/compiler/visitor.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -448,41 +448,39 @@ def acceptForEach(self, node, memo):
448448

449449
def acceptIf(self, node, memo):
450450
""" Accept and process an if statement. """
451-
# the parser will feed us one of three forms:
452-
# bare if: PARENTESIZED_EXPR - BLOCK_SCOPE
453-
# if with else: PARENTESIZED_EXPR - BLOCK_SCOPE - BLOCK_SCOPE
454-
# if with else if: PARENTESIZED_EXPR - BLOCK_SCOPE - IF
455451
children = node.children
456452
ifStat = self.factory.statement('if', fs=FS.lsrc, parent=self)
457453
ifStat.expr.walk(children[0], memo)
458-
if node.children[1].type == tokens.BLOCK_SCOPE:
459-
ifBlock = self.factory.methodContent(parent=self)
460-
ifBlock.walk(node.children[1], memo)
461-
elif node.children[1].type == tokens.EXPR:
454+
455+
if node.children[1].type == tokens.EXPR:
462456
ifBlock = self.factory.expr(parent=ifStat)
463457
ifBlock.walk(node.children[1], memo)
458+
else:
459+
ifBlock = self.factory.methodContent(parent=self)
460+
ifBlock.walk(node.children[1], memo)
461+
464462
if len(children) == 3:
465463
nextNode = children[2]
466464
nextType = nextNode.type
467465

468466
while nextType == tokens.IF:
469467
nextStat = self.factory.statement('elif', fs=FS.lsrc, parent=self)
470468
nextStat.expr.walk(nextNode.children[0], memo)
471-
if nextNode.children[1].type == tokens.BLOCK_SCOPE:
472-
nextBlock = self.factory.methodContent(parent=self)
473-
else:
469+
if nextNode.children[1].type == tokens.EXPR:
474470
nextBlock = self.factory.expr(parent=nextStat)
471+
else:
472+
nextBlock = self.factory.methodContent(parent=self)
475473
nextBlock.walk(nextNode.children[1], memo)
476474
nextNode = nextNode.children[2]
477475
nextType = nextNode.type
478476

479-
if nextType == tokens.BLOCK_SCOPE:
480-
self.factory.statement('else', fs=FS.lc, parent=self)
481-
self.factory.methodContent(parent=self).walk(nextNode, memo)
482-
elif nextType == tokens.EXPR:
477+
if nextType == tokens.EXPR:
483478
elseStat = self.factory.statement('else', fs=FS.lc, parent=self)
484479
elseBlock = self.factory.expr(parent=elseStat)
485480
elseBlock.walk(nextNode, memo)
481+
else: # nextType != tokens.BLOCK_SCOPE:
482+
self.factory.statement('else', fs=FS.lc, parent=self)
483+
self.factory.methodContent(parent=self).walk(nextNode, memo)
486484

487485

488486
def acceptSwitch(self, node, memo):
@@ -582,6 +580,7 @@ def acceptReturn(self, node, memo):
582580
if node.children:
583581
expr.fs, expr.right = FS.lsr, self.factory.expr(parent=expr)
584582
expr.right.walk(node, memo)
583+
return expr
585584

586585
def acceptWhile(self, node, memo):
587586
""" Accept and process a while block. """

java2python/config/default.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181
(r'(.*?)\.equals\((.*?)\)', r'\1 == \2'),
182182
(r'(.*?)\.equalsIgnoreCase\((.*?)\)', r'\1.lower() == \2.lower()'),
183183
(r'([\w.]+)\.size\(\)', r'len(\1)'),
184-
(r'(\w+)\.get\((.*?)\)', r'\1[\2]'),
184+
#(r'(\w+)\.get\((.*?)\)', r'\1[\2]'),
185185
(r'(\s)(\S*?)(\.toString\(\))', r'\1\2.__str__()'),
186186
(r'(\s)def toString', r'\1def __str__'),
187187
(r'(\s)(\S*?)(\.toLowerCase\(\))', r'\1\2.lower()'),

java2python/mod/basic.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ def simpleDocString(obj):
3030
3131
This generator works for modules, classes, and functions.
3232
"""
33-
yield '""" generated source for {0} {1}'.format(obj.typeName, obj.name)
34-
yield ''
35-
yield '"""'
33+
yield '""" generated source for {0} {1} """'.format(obj.typeName, obj.name)
3634

3735

3836
def commentedImports(module, expr):

test/If5.java

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

0 commit comments

Comments
 (0)