Skip to content

Commit 88f84ab

Browse files
author
Troy Melhase
committed
Fiddles.
1 parent a041ba3 commit 88f84ab

File tree

6 files changed

+72
-49
lines changed

6 files changed

+72
-49
lines changed

java2python/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
3+
""" java2python -> a package to compile java source code to python. """
44
##
55
# Top-level package marker for java2python.
66
#

java2python/compiler/__init__.py

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,10 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3+
""" java2python.compiler package marker. """
4+
##
5+
# This module provides a simpler facade over the rest of the compiler
6+
# subpackage. Client code should use the values in this module
7+
# instead of using directly referencing items within the subpackage.
38

49
from java2python.compiler.block import Module
5-
from java2python.lang import (
6-
Lexer, Parser, StringStream, TokenStream, TreeAdaptor, walkTreeSelector,
7-
)
8-
9-
10-
def buildAST(source, config=None):
11-
lexer = Lexer(StringStream(source))
12-
parser = Parser(TokenStream(lexer))
13-
adapter = TreeAdaptor(lexer, parser)
14-
parser.setTreeAdaptor(adapter)
15-
scope = parser.javaSource()
16-
return scope.tree
17-
18-
19-
def transformAST(tree, config):
20-
for selector, call in config.last('astTransforms', ()):
21-
for node in walkTreeSelector(tree, selector):
22-
call(node, config)
23-
24-
25-
def buildJavaDocAST(source):
26-
from java2python.lang.JavaDocLexer import JavaDocLexer
27-
from java2python.lang.JavaDocParser import JavaDocParser
28-
lexer = JavaDocLexer(StringStream(source))
29-
parser = JavaDocParser(TokenStream(lexer))
30-
scope = parser.commentBody()
31-
return scope.tree
32-
33-
34-
def walkJavaDoc(tree, callback=lambda x:None):
35-
pass
36-
37-
38-
if __name__ == '__main__':
39-
import sys
40-
tree = buildAST(open(sys.argv[1]).read())
41-
for idx, tok in enumerate(tree.parser.input.tokens):
42-
print '{0} {1}'.format(idx, tok)
43-
print
44-
tree.dump(sys.stdout)
10+
from java2python.compiler.tool import buildAST, transformAST

java2python/compiler/template.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
##
55
# This module defines templates, blocks of Python source code, that
66
# can be easily manipulated and written. Each base provides string
7-
# methods (__str__, dump, dumps) serializing instances as source code.
8-
# The base types also provide many utility methods.
7+
# methods (__str__, dump, dumps) for serializing instances as source
8+
# code. The base types also provide many utility methods.
99
#
1010
# The Factory class is used to to provide runtime lookup of concrete
1111
# classes; this was necessary to accommodate splitting the behavior of
@@ -94,7 +94,10 @@ def altIdent(self, name):
9494
""" Returns an alternate identifier for the one given. """
9595
for klass in self.parents(lambda v:v.isClass):
9696
if name in klass.variables:
97-
method = self.parents(lambda v:v.isMethod).next()
97+
try:
98+
method = self.parents(lambda v:v.isMethod).next()
99+
except (StopIteration, ):
100+
return name
98101
if name in [p['name'] for p in method.parameters]:
99102
return name
100103
return ('cls' if method.isStatic else 'self') + '.' + name

java2python/compiler/tool.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
""" java2python.compiler.tool -> """
4+
##
5+
# This module can be run as a script, e.g.:
6+
#
7+
# $ python -m java2python.compiler.tool ./SomeClass.java
8+
9+
from java2python.lang import (
10+
Lexer, Parser, StringStream, TokenStream, TreeAdaptor, walkTreeSelector,
11+
)
12+
13+
14+
def buildAST(source, config=None):
15+
lexer = Lexer(StringStream(source))
16+
parser = Parser(TokenStream(lexer))
17+
adapter = TreeAdaptor(lexer, parser)
18+
parser.setTreeAdaptor(adapter)
19+
scope = parser.javaSource()
20+
return scope.tree
21+
22+
23+
def transformAST(tree, config):
24+
for selector, call in config.last('astTransforms', ()):
25+
for node in walkTreeSelector(tree, selector):
26+
call(node, config)
27+
28+
29+
def buildJavaDocAST(source):
30+
from java2python.lang.JavaDocLexer import JavaDocLexer
31+
from java2python.lang.JavaDocParser import JavaDocParser
32+
lexer = JavaDocLexer(StringStream(source))
33+
parser = JavaDocParser(TokenStream(lexer))
34+
scope = parser.commentBody()
35+
return scope.tree
36+
37+
38+
def walkJavaDoc(tree, callback=lambda x:None):
39+
pass
40+
41+
42+
if __name__ == '__main__':
43+
import sys
44+
tree = buildAST(open(sys.argv[1]).read())
45+
for idx, tok in enumerate(tree.parser.input.tokens):
46+
print '{0} {1}'.format(idx, tok)
47+
print
48+
tree.dump(sys.stdout)

java2python/config/default.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103

104104
# This handler can be used instead to create __init__.py files for
105105
# 'namespace packages' via pkgutil.
106-
modulePackageDeclarationHandler = basic.namespacePackages
106+
# modulePackageDeclarationHandler = basic.namespacePackages
107107

108108

109109
moduleImportDeclarationHandler = basic.commentedImports

java2python/lib/__init__.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44

55
class FS(object):
6+
""" Format string abbreviations.
7+
8+
"""
69
l = '{left}'
710
r = '{right}'
811
c = ':'
@@ -11,11 +14,14 @@ class FS(object):
1114
lsr = l + ' ' + r
1215
lsrc = lsr + c
1316

14-
#instance = 'isinstance(' + l + ', (' + t + ', ))'
1517
@classmethod
1618
def op(cls, op):
19+
""" Returns a format string for the given operation.
20+
21+
"""
22+
l, r = cls.l, cls.r
1723
if op == '>>>':
18-
return '({left} & (2**32+{left})) >> {right}'
24+
return '(' + l + ' & (2**32+' + l + ')) >> ' + r
1925
if op == '>>>=':
20-
return '{left} = bsr({left}, {right})'
21-
return cls.l + ' ' + op + ' ' + cls.r
26+
return l + ' = bsr(' + l + ', ' + r + ')'
27+
return l + ' ' + op + ' ' + r

0 commit comments

Comments
 (0)