Skip to content

Commit db738ff

Browse files
committed
Fixed
1 parent b803756 commit db738ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+66934
-5
lines changed

bin/j2py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/python2
22
# -*- coding: utf-8 -*-
33
""" j2py -> Java to Python compiler script.
44
@@ -136,7 +136,7 @@ def runTransform(options):
136136
timed['visit_finish']
137137

138138
timed['encode']
139-
source = unicode(module)
139+
source = unicode(str(module), 'utf8')
140140
timed['encode_finish']
141141
timed['overall_finish']
142142

@@ -159,7 +159,7 @@ def runTransform(options):
159159
else:
160160
output = open(fileout, 'w')
161161
module.name = path.splitext(filein)[0] if filein != '-' else '<stdin>'
162-
print >> output, source
162+
print >> output, source.encode('utf8')
163163

164164
if not options.skipcompile:
165165
try:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# java2python -> top-level package marker.
4+
5+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
# -*- 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.
8+
9+
from java2python.compiler.block import Module
10+
from java2python.lang import Lexer, Parser, StringStream, TokenStream, TreeAdaptor
11+
12+
13+
def buildAST(source):
14+
""" Returns an AST for the given source. """
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 buildJavaDocAST(source):
24+
""" Returns an AST for the given javadoc source. """
25+
from java2python.lang.JavaDocLexer import JavaDocLexer
26+
from java2python.lang.JavaDocParser import JavaDocParser
27+
lexer = JavaDocLexer(StringStream(source))
28+
parser = JavaDocParser(TokenStream(lexer))
29+
scope = parser.commentBody()
30+
return scope.tree
31+
32+
33+
def transformAST(tree, config):
34+
""" Walk the tree and apply the transforms in the config. """
35+
for selector, call in config.last('astTransforms', ()):
36+
for node in selector.walk(tree):
37+
call(node, config)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# java2python.compiler.block -> Visitors combined with templates.
4+
#
5+
# This module defines classes which combine AST walking with source
6+
# generation. We've put these two behaviors into separate modules,
7+
# java2python.compiler.template for creating source code, and
8+
# java2python.compiler.visitor for walking ANTLR trees.
9+
#
10+
# Each of the classes depends on the behavior of its counterpart.
11+
# This means they're very tightly coupled and that the classes are not
12+
# very reusable. The module split does allow for grouping of related
13+
# methods and does hide the cluttered code.
14+
15+
from sys import modules
16+
from java2python.compiler import template, visitor
17+
18+
19+
def addTypeToModule((className, factoryName)):
20+
""" Constructs and adds a new type to this module. """
21+
bases = (getattr(template, className), getattr(visitor, className))
22+
newType = type(className, bases, dict(factoryName=factoryName))
23+
setattr(modules[__name__], className, newType)
24+
25+
26+
map(addTypeToModule, (
27+
('Annotation', 'at'),
28+
('Class', 'klass'),
29+
('Comment', 'comment'),
30+
('Enum', 'enum'),
31+
('Expression', 'expr'),
32+
('Interface', 'interface'),
33+
('Method', 'method'),
34+
('MethodContent', 'methodContent'),
35+
('Module', 'module'),
36+
('Statement', 'statement'),
37+
)
38+
)

0 commit comments

Comments
 (0)