Skip to content

Commit 1a29aae

Browse files
author
Troy Melhase
committed
Initial checkin. Copied from ibpy.melhase.net ast_branch.
0 parents  commit 1a29aae

File tree

12 files changed

+2934
-0
lines changed

12 files changed

+2934
-0
lines changed

java2python/LICENSE

Lines changed: 340 additions & 0 deletions
Large diffs are not rendered by default.

java2python/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.PHONY: all clean lib
2+
3+
4+
all: lib
5+
6+
7+
clean:
8+
rm -f *.pyo
9+
rm -f *.pyc
10+
cd lib && $(MAKE) clean
11+
12+
lib:
13+
cd lib && $(MAKE)

java2python/README

Whitespace-only changes.

java2python/bin/java_to_python

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
import optparse
4+
import os.path
5+
import sys
6+
7+
8+
from lib.lexer import Lexer
9+
from lib.parser import Parser
10+
from lib.walker import Walker
11+
12+
from lib.sourcetypes import Module, set_config
13+
14+
try:
15+
import psyco
16+
except (ImportError, ):
17+
pass
18+
else:
19+
psyco.full()
20+
21+
22+
def transform(options):
23+
lexer_args = [options.inputfile, ] if options.inputfile else []
24+
L = Lexer(*lexer_args)
25+
P = Parser(L)
26+
W = Walker()
27+
28+
try:
29+
P.compilationUnit()
30+
except (Exception, ), exc:
31+
print '*** exception while parsing:'
32+
print exc
33+
return 1
34+
35+
ast = P.getAST()
36+
if not ast:
37+
print '*** error: no AST generated.'
38+
return 2
39+
40+
filein = fileout = filedefault = '-'
41+
if options.inputfile:
42+
filein = os.path.basename(options.inputfile)
43+
if options.outputfile:
44+
fileout = os.path.basename(options.outputfile)
45+
elif fileout != filedefault:
46+
fileout = '%s.py' % (os.path.splitext(filein)[0])
47+
48+
if fileout == filedefault:
49+
output = sys.stdout
50+
else:
51+
output = open(fileout, 'w')
52+
53+
set_config(options.configs)
54+
M = Module(filein, fileout)
55+
W.walk(ast, M)
56+
print >> output, M
57+
return 0
58+
59+
60+
def cli_options(argv):
61+
parser = optparse.OptionParser(version='%prog 0.1')
62+
parser.add_option('-i', '--input', dest='inputfile',
63+
help='read INPUTFILE',
64+
metavar='INPUTFILE', default=None)
65+
parser.add_option('-o', '--output', dest='outputfile',
66+
help='write OUTPUTFILE',
67+
metavar='OUTPUTFILE', default=None)
68+
parser.add_option('-c', '--config', dest='configs',
69+
help='use CONFIG (multiple allowed)',
70+
metavar='CONFIG', default=[],
71+
action='append')
72+
73+
options, args = parser.parse_args(argv)
74+
return options, args
75+
76+
if __name__ == '__main__':
77+
options, args = cli_options(sys.argv)
78+
ret = transform(options)
79+
sys.exit(ret)
80+

java2python/bin/print_java_ast

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from antlr import ASTVisitor
5+
from lib import lexer
6+
from lib import parser
7+
8+
9+
class Printer(ASTVisitor):
10+
def visit(self, node, level=0):
11+
if not node:
12+
return
13+
txt = node.getText()
14+
typ = node.getType()
15+
print '%s%s:%s' % (' '*level, typ, txt, )
16+
self.visit(node.getFirstChild(), level+1)
17+
if typ in (parser.CLASS_DEF, parser.PACKAGE_DEF, parser.IMPORT):
18+
print
19+
self.visit(node.getNextSibling(), level)
20+
21+
22+
def show(filename):
23+
if not filename:
24+
args = []
25+
else:
26+
args = [filename, ]
27+
28+
L = lexer.Lexer(*args)
29+
P = parser.Parser(L)
30+
P.setFilename(L.getFilename())
31+
32+
try:
33+
P.compilationUnit()
34+
except (Exception, ), exc:
35+
print '*** exception while parsing:'
36+
print exc
37+
return 1
38+
39+
ast = P.getAST()
40+
ast = P.getAST()
41+
if not ast:
42+
print '*** error: no AST generated.'
43+
return 2
44+
45+
printer = Printer()
46+
printer.visit(ast)
47+
return 0
48+
49+
50+
if __name__ == '__main__':
51+
import sys
52+
import psyco
53+
psyco.full()
54+
55+
try:
56+
name = sys.argv[1]
57+
except (IndexError, ):
58+
name = None
59+
ret = show(name)
60+
sys.exit(ret)

java2python/lib/Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.PHONY: all clean
2+
3+
4+
all: parser.py lexer.py walker.py
5+
6+
7+
lexer.py: java.g
8+
antlr java.g
9+
10+
11+
parser.py: java.g
12+
antlr java.g
13+
14+
15+
walker.py: walker.g lexer.py
16+
antlr walker.g
17+
18+
19+
clean:
20+
rm -f *.pyo
21+
rm -f *.pyc
22+
rm -rf lexer.py
23+
rm -rf parser.py
24+
rm -rf walker.py
25+
rm -rf walkerTokenTypes.txt
26+
rm -rf JavaTokenTypes.txt

java2python/lib/__init__.py

Whitespace-only changes.

java2python/lib/defaultconfig.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
headings = {
5+
'Contract.java' : [
6+
'from helpers import *',
7+
],
8+
9+
'ContractDetails.java' : [
10+
'from overloading import overloaded',
11+
'from Contract import Contract',
12+
],
13+
14+
'ScannerSubscription.java' : [
15+
'from helpers import *',
16+
],
17+
18+
'ExecutionFilter.java': [
19+
'from overloading import overloaded',
20+
],
21+
22+
}
23+
24+
25+
features = {
26+
'ContractDetails.java' : 'rename strip frobinate',
27+
}
28+
29+
30+
defaults = {
31+
'writemods':False
32+
}
33+
34+
35+
36+
globalSubs = [
37+
(r'(\.self\.)', '.'),
38+
(r'String\.valueOf\((.*?)\)', r'str(\1)'),
39+
(r'System\.out\.println\((.*?)\)', r'print \1'),
40+
(r'(.*?)\.equals\((.*?)\)', r'\1 == \2'),
41+
(r'(.*?)\.equalsIgnoreCase\((.*?)\)', r'\1.lower() == \2.lower()'),
42+
(r'if self == p_other:', r'if self is p_other:'),
43+
(r'([\w.]+)\.size\(\)', r'len(\1)'),
44+
(r'(\w+)\.get\((.*?)\)', r'\1[\2]'),
45+
]

0 commit comments

Comments
 (0)