GeezerVison is showing github.com — AST transforms for .length() method call. Fixes #17. · jcoder2012/java2python@6548316
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: java2python/config/default.py +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 159 | 159 | (Type('TYPE') > Type('LONG'), transform.typeSub), | 160 | 160 | (Type('TYPE') > Type('DOUBLE'), transform.typeSub), | 161 | 161 |
| | 162 | + (Type('METHOD_CALL') > Type('DOT') > Type('IDENT', 'length'), | | 163 | + transform.lengthToLen), | | 164 | + | 162 | 165 | (Type('TYPE') > Type('QUALIFIED_TYPE_IDENT') > Type('IDENT'), | 163 | 166 | transform.typeSub), | 164 | 167 |
| | 193 | 196 | (r'(\s)(\S*?)(\.toString\(\))', r'\1\2.__str__()'), | 194 | 197 | (r'(\s)def toString', r'\1def __str__'), | 195 | 198 | (r'(\s)(\S*?)(\.toLowerCase\(\))', r'\1\2.lower()'), | 196 | | - (r'(\s)(\S*?)(\.length\(\))', r'\1len(\2)'), | 197 | 199 | (r'(.*?)IndexOutOfBoundsException\((.*?)\)', r'\1IndexError(\2)'), | 198 | 200 | (r'\.__class__\.getName\(\)', '.__class__.__name__'), | 199 | 201 | (r'\.getClass\(\)', '.__class__'), | |
Collapse file Copy file name to clipboardExpand all lines: java2python/mod/transform.py +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 14 | 14 | import keyword | 15 | 15 | import types | 16 | 16 |
| | 17 | +from java2python.lang import tokens | | 18 | + | 17 | 19 |
| 18 | 20 | def invalidPythonNames(): | 19 | 21 | """ Creates a list of valid Java identifiers that are invalid in Python. """ | @@ -55,6 +57,38 @@ def syntaxSafeFloatLiteral(node, config): | 55 | 57 | node.token.text = value | 56 | 58 |
| 57 | 59 |
| | 60 | +def lengthToLen(node, config): | | 61 | + """ Transforms expressions like 'value.length()' to 'len(value)'. | | 62 | +
| | 63 | + This method changes AST branches like this: | | 64 | +
| | 65 | + METHOD_CALL [start=45, stop=49] | | 66 | + DOT . [line=4, start=45, stop=47] | | 67 | + IDENT foo [line=4, start=45] | | 68 | + IDENT length [line=4, start=47] | | 69 | + ARGUMENT_LIST [line=4, start=48, stop=49] | | 70 | +
| | 71 | + Into branches like this: | | 72 | +
| | 73 | + IDENT len(foo) [line=4, start=45] | | 74 | +
| | 75 | + Notice that the resulting IDENT node text is invalid. We can't use a | | 76 | + METHOD_CALL token because those are always bound to a class or instance. | | 77 | + It would be best to add a new token type, and that option will be explored | | 78 | + if we run into this problem again. | | 79 | +
| | 80 | + """ | | 81 | + dot = node.parent | | 82 | + method = dot.parent | | 83 | + | | 84 | + ident = dot.firstChildOfType(tokens.IDENT) | | 85 | + ident.token.text = 'len({})'.format(ident.text) | | 86 | + | | 87 | + expr = method.parent | | 88 | + expr.children.remove(method) | | 89 | + expr.addChild(ident) | | 90 | + | | 91 | + | 58 | 92 | def typeSub(node, config): | 59 | 93 | """ Maps specific, well-known Java types to their Python counterparts. | 60 | 94 |
| |
Collapse file Copy file name to clipboard +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +class Length0 { | | 2 | + public static int dummy(int v) { | | 3 | + return v + 1; | | 4 | + } | | 5 | + | | 6 | + public static void main(String[] args) { | | 7 | + String foo = "asdf"; | | 8 | + System.out.println( dummy(foo.length() ) ); | | 9 | + } | | 10 | + | | 11 | +} |
|