[java2python] r179 committed - Fiddles.

4 views
Skip to first unread message

java2...@googlecode.com

unread,
Oct 15, 2011, 6:18:06 PM10/15/11
to java2pyth...@googlegroups.com
Revision: 179
Author: troy.melhase
Date: Sat Oct 15 15:17:26 2011
Log: Fiddles.
http://code.google.com/p/java2python/source/detail?r=179

Added:
/trunk/java2python/compiler/tool.py
Modified:
/trunk/java2python/__init__.py
/trunk/java2python/compiler/__init__.py
/trunk/java2python/compiler/template.py
/trunk/java2python/config/default.py
/trunk/java2python/lib/__init__.py

=======================================
--- /dev/null
+++ /trunk/java2python/compiler/tool.py Sat Oct 15 15:17:26 2011
@@ -0,0 +1,48 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+""" java2python.compiler.tool -> """
+##
+# This module can be run as a script, e.g.:
+#
+# $ python -m java2python.compiler.tool ./SomeClass.java
+
+from java2python.lang import (
+ Lexer, Parser, StringStream, TokenStream, TreeAdaptor,
walkTreeSelector,
+ )
+
+
+def buildAST(source, config=None):
+ lexer = Lexer(StringStream(source))
+ parser = Parser(TokenStream(lexer))
+ adapter = TreeAdaptor(lexer, parser)
+ parser.setTreeAdaptor(adapter)
+ scope = parser.javaSource()
+ return scope.tree
+
+
+def transformAST(tree, config):
+ for selector, call in config.last('astTransforms', ()):
+ for node in walkTreeSelector(tree, selector):
+ call(node, config)
+
+
+def buildJavaDocAST(source):
+ from java2python.lang.JavaDocLexer import JavaDocLexer
+ from java2python.lang.JavaDocParser import JavaDocParser
+ lexer = JavaDocLexer(StringStream(source))
+ parser = JavaDocParser(TokenStream(lexer))
+ scope = parser.commentBody()
+ return scope.tree
+
+
+def walkJavaDoc(tree, callback=lambda x:None):
+ pass
+
+
+if __name__ == '__main__':
+ import sys
+ tree = buildAST(open(sys.argv[1]).read())
+ for idx, tok in enumerate(tree.parser.input.tokens):
+ print '{0} {1}'.format(idx, tok)
+ print
+ tree.dump(sys.stdout)
=======================================
--- /trunk/java2python/__init__.py Sat Jul 17 04:01:55 2010
+++ /trunk/java2python/__init__.py Sat Oct 15 15:17:26 2011
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
-
+""" java2python -> a package to compile java source code to python. """
##
# Top-level package marker for java2python.
#
=======================================
--- /trunk/java2python/compiler/__init__.py Fri Jul 30 23:35:02 2010
+++ /trunk/java2python/compiler/__init__.py Sat Oct 15 15:17:26 2011
@@ -1,44 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
+""" java2python.compiler package marker. """
+##
+# This module provides a simpler facade over the rest of the compiler
+# subpackage. Client code should use the values in this module
+# instead of using directly referencing items within the subpackage.

from java2python.compiler.block import Module
-from java2python.lang import (
- Lexer, Parser, StringStream, TokenStream, TreeAdaptor,
walkTreeSelector,
- )
-
-
-def buildAST(source, config=None):
- lexer = Lexer(StringStream(source))
- parser = Parser(TokenStream(lexer))
- adapter = TreeAdaptor(lexer, parser)
- parser.setTreeAdaptor(adapter)
- scope = parser.javaSource()
- return scope.tree
-
-
-def transformAST(tree, config):
- for selector, call in config.last('astTransforms', ()):
- for node in walkTreeSelector(tree, selector):
- call(node, config)
-
-
-def buildJavaDocAST(source):
- from java2python.lang.JavaDocLexer import JavaDocLexer
- from java2python.lang.JavaDocParser import JavaDocParser
- lexer = JavaDocLexer(StringStream(source))
- parser = JavaDocParser(TokenStream(lexer))
- scope = parser.commentBody()
- return scope.tree
-
-
-def walkJavaDoc(tree, callback=lambda x:None):
- pass
-
-
-if __name__ == '__main__':
- import sys
- tree = buildAST(open(sys.argv[1]).read())
- for idx, tok in enumerate(tree.parser.input.tokens):
- print '{0} {1}'.format(idx, tok)
- print
- tree.dump(sys.stdout)
+from java2python.compiler.tool import buildAST, transformAST
=======================================
--- /trunk/java2python/compiler/template.py Thu Aug 12 11:20:01 2010
+++ /trunk/java2python/compiler/template.py Sat Oct 15 15:17:26 2011
@@ -4,8 +4,8 @@
##
# This module defines templates, blocks of Python source code, that
# can be easily manipulated and written. Each base provides string
-# methods (__str__, dump, dumps) serializing instances as source code.
-# The base types also provide many utility methods.
+# methods (__str__, dump, dumps) for serializing instances as source
+# code. The base types also provide many utility methods.
#
# The Factory class is used to to provide runtime lookup of concrete
# classes; this was necessary to accommodate splitting the behavior of
@@ -94,7 +94,10 @@
""" Returns an alternate identifier for the one given. """
for klass in self.parents(lambda v:v.isClass):
if name in klass.variables:
- method = self.parents(lambda v:v.isMethod).next()
+ try:
+ method = self.parents(lambda v:v.isMethod).next()
+ except (StopIteration, ):
+ return name
if name in [p['name'] for p in method.parameters]:
return name
return ('cls' if method.isStatic else 'self') + '.' + name
=======================================
--- /trunk/java2python/config/default.py Thu Aug 12 11:20:01 2010
+++ /trunk/java2python/config/default.py Sat Oct 15 15:17:26 2011
@@ -103,7 +103,7 @@

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


moduleImportDeclarationHandler = basic.commentedImports
=======================================
--- /trunk/java2python/lib/__init__.py Mon Jul 19 16:53:47 2010
+++ /trunk/java2python/lib/__init__.py Sat Oct 15 15:17:26 2011
@@ -3,6 +3,9 @@


class FS(object):
+ """ Format string abbreviations.
+
+ """
l = '{left}'
r = '{right}'
c = ':'
@@ -11,11 +14,14 @@
lsr = l + ' ' + r
lsrc = lsr + c

- #instance = 'isinstance(' + l + ', (' + t + ', ))'
@classmethod
def op(cls, op):
+ """ Returns a format string for the given operation.
+
+ """
+ l, r = cls.l, cls.r
if op == '>>>':
- return '({left} & (2**32+{left})) >> {right}'
+ return '(' + l + ' & (2**32+' + l + ')) >> ' + r
if op == '>>>=':
- return '{left} = bsr({left}, {right})'
- return cls.l + ' ' + op + ' ' + cls.r
+ return l + ' = bsr(' + l + ', ' + r + ')'
+ return l + ' ' + op + ' ' + r

Reply all
Reply to author
Forward
0 new messages