Modified:
/branches/0.5/java2python/compiler/__init__.py
/branches/0.5/java2python/compiler/block.py
/branches/0.5/java2python/compiler/template.py
/branches/0.5/java2python/config/__init__.py
/branches/0.5/java2python/config/default.py
/branches/0.5/java2python/lib/__init__.py
/branches/0.5/java2python/mod/transforms.py
=======================================
--- /branches/0.5/java2python/compiler/__init__.py Mon Jul 19 15:02:15 2010
+++ /branches/0.5/java2python/compiler/__init__.py Mon Jul 19 16:53:47 2010
@@ -8,35 +8,29 @@
)
-def buildAST(source, config=None, debug=False):
- sourceStream = LocalSourceStream(source)
- sourceLexer = Lexer(sourceStream)
- tokenStream = LocalTokenStream(sourceLexer)
- sourceParser = Parser(tokenStream, state=None)
-
- def callback(node):
- node.parser = sourceParser
- node.lexer = sourceLexer
-
- treeAdaptor = LocalTreeAdaptor(callback)
- sourceParser.setTreeAdaptor(treeAdaptor)
- returnScope = sourceParser.javaSource()
-
- if debug:
- for idx, tok in enumerate(sourceParser.input.tokens):
- print '{0} {1}'.format(idx, tok)
- print
-
- return returnScope.tree
+def buildAST(source, config=None):
+ lexer = Lexer(LocalSourceStream(source))
+ parser = Parser(LocalTokenStream(lexer))
+
+ def setNodeRecognizers(node):
+ node.parser = parser
+ node.lexer = lexer
+
+ parser.setTreeAdaptor(LocalTreeAdaptor(setNodeRecognizers))
+ scope = parser.javaSource()
+ return scope.tree
def transformAST(tree, config):
for selector, call in config.handlers('transforms'):
for node in walkTreeSelector(tree, selector):
- call(node)
+ call(node, config)
if __name__ == '__main__':
import sys
- tree = buildAST(open(sys.argv[1]).read(), debug=True)
+ 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)
=======================================
--- /branches/0.5/java2python/compiler/block.py Sat Jul 17 04:01:55 2010
+++ /branches/0.5/java2python/compiler/block.py Mon Jul 19 16:53:47 2010
@@ -85,8 +85,9 @@
def iterPrologue(self):
""" Yields the items in the prologue of this template. """
- handler = self.configHandler('Base', default=lambda v:v.bases)
- bases = ', '.join(ifilter(None, handler(self)))
+ def iterBases():
+ return chain(*(h(self) for h in self.configHandlers('Base')))
+ bases = ', '.join(ifilter(None, iterBases()))
bases = '({0})'.format(bases) if bases else ''
for deco in self.iterDecorators():
yield '@{0}'.format(deco)
@@ -345,7 +346,6 @@
dtype = decl.firstChildOfType(tokens.TYPE)
tnames = dtype.findChildrenOfType(tokens.IDENT)
cname = '.'.join(n.text for n in tnames)
- cname = self.config.combined('exceptionSubMap').get(cname, cname)
cvar = decl.firstChildOfType(tokens.IDENT)
block = node.firstChildOfType(tokens.BLOCK_SCOPE)
self.expr.fs = FS.lsrc
@@ -496,7 +496,7 @@
ptype = list(ptype.findChildrenOfType(tokens.IDENT))[0].text
except:
ptype = ptype.firstChild().text
- ptype = self.config.combined('typeSubstitutionMap').get(ptype, ptype)
+ #ptype = self.config.last('typeSubs').get(ptype, ptype)
self.parameters.append(self.makeParam(ident.text, ptype))
return self
=======================================
--- /branches/0.5/java2python/compiler/template.py Wed Jul 14 12:03:07 2010
+++ /branches/0.5/java2python/compiler/template.py Mon Jul 19 16:53:47 2010
@@ -138,7 +138,7 @@
@property
def indent(self):
""" Returns the indent string for this item. """
- return self.config.last('leadingIndent', ' ')
+ return self.config.last('indentPrefix', ' ')
@property
def isPublic(self):
=======================================
--- /branches/0.5/java2python/config/__init__.py Fri Jul 2 23:54:05 2010
+++ /branches/0.5/java2python/config/__init__.py Mon Jul 19 16:53:47 2010
@@ -1,10 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
-from java2python.lib import getModule, getModuleValue
-
-
-def valueOf(item):
- return getModuleValue(item) if isinstance(item, (basestring, )) else
item
class Config(object):
@@ -12,7 +7,7 @@
"""
def __init__(self, names):
- self.configs = [getModule(name) for name in names]
+ self.configs = [self.load(name) for name in names]
def __iter__(self):
return iter(self.configs)
@@ -26,19 +21,24 @@
return getattr(config, key)
return default
- def combined(self, key):
- combined = {}
- for mapping in self.every(key, {}):
- combined.update(mapping)
- return combined
-
def handler(self, key, default=None):
handler = self.last(key, default)
- return valueOf(handler) if handler is not default else handler
+ return handler
def handlers(self, key, default=None):
- groups = self.every(key, default)
- if groups is not default:
- for group in groups:
- for handler in group or ():
- yield valueOf(handler)
+ handlers = self.last(key, default)
+ if handlers is default:
+ return
+ for handler in handlers:
+ yield handler
+
+ @staticmethod
+ def load(name):
+ """ load(name) -> import and return a module by name in dotted form.
+
+ Copied from the Python lib docs.
+ """
+ mod = __import__(name)
+ for comp in name.split('.')[1:]:
+ mod = getattr(mod, comp)
+ return mod
=======================================
--- /branches/0.5/java2python/config/default.py Mon Jul 19 15:02:15 2010
+++ /branches/0.5/java2python/config/default.py Mon Jul 19 16:53:47 2010
@@ -1,44 +1,22 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
-
-##
#
# This is the default configuration file for java2python. Unless
# explicity disabled with the '-n' or '--nodefaults' options, the j2py
# script will reference this file for runtime configuration.
#
-# The j2py script accepts additional configuration modules with the
-# '-c' or '--config' arguments. These arguments may be repeated.
-#
-# There are three flavors of options: every, last, and combined. The
-# semantics are:
-#
-# * every means the value is loaded from each configuration module and
-# returned as a sequence.
-#
-# * last means the value is selected from the last configuration
-# module specified. if no configuration modules are specified (and
-# if the default is not disabled), the last value will be the value
-# in this module.
-#
-# * combined means that the values (dictionaries) will be merged in
-# order and returned as a single dictionary.
-#
-# In some cases, noted below, the value or values may be a dotted path
-# to a python value. In these cases, the value is imported as needed.
-
import java2python.mod
-## leading indent characters (or character). this is a "last" option.
-leadingIndent = ' '
+## leading indent character or characters.
+indentPrefix = ' '
-## prefix for comments. this is a "last" option.
+## prefix for comments.
commentPrefix = '## '
-## this is an 'every' handler sequence
+## generator-functions that yield lines for a module prologue.
modulePrologueHandlers = [
java2python.mod.simpleShebang,
java2python.mod.simpleDocString,
@@ -47,43 +25,62 @@
java2python.mod.commentedPackageName,
]
-## this is an 'every' handler sequence
+
+## generator-functions that yield lines for a module epilogue.
moduleEpilogueHandlers = [
java2python.mod.scriptMainStanza,
]
-## ???
+
+## generator-functions that yield (possibly modified) source strings
+## for a module.
moduleOutputHandlers = [
java2python.mod.outputSubs,
]
-## handlers for doc strings.
+
+## generator-functions that yield doc strings for classes.
classDocStringHandlers = [
java2python.mod.simpleDocString,
]
+
+
+## generator-functions that yield doc strings for enums.
enumDocStringHandlers = [
java2python.mod.simpleDocString,
]
+
+
+## generator-functions that yield doc strings for methods.
methodDocStringHandlers = [
java2python.mod.simpleDocString,
]
-## extra decorator methods
+## generator-functions that yield extra method decorators.
methodExtraDecoratorHandlers = [
java2python.mod.maybeClassMethod,
java2python.mod.overloadedClassMethods,
]
+## generator-functions that yield base types for classes.
+classBaseHandlers = [
+ java2python.mod.mapClassType,
+]
-
-## these next 4 handler values should get morphed into lists.
-
-classBaseHandler = java2python.mod.mapClassType
-enumBaseHandler = java2python.mod.mapClassType
-interfaceBaseHandler = java2python.mod.mapClassType
+## generator-functions that yield base types for enums.
+enumBaseHandlers = [
+ java2python.mod.mapClassType
+]
+
+
+## generator-functions that yield base types for interfaces.
+interfaceBaseHandlers = [
+ java2python.mod.mapClassType
+]
+
##
# Note that the following two enum value handlers are only called for
@@ -105,7 +102,6 @@
## not implemented:
-## interfaceWhateverHandlers = []
## move inner class definitions to the top of their outer class.
## allows the outer class to reference the inner class definition
#bubbleInnerClasses = True
@@ -146,34 +142,26 @@
]
-## TODO: stop using 'combined' to define this.
-exceptionSubMap = {
- 'IndexOutOfBoundsException' : 'IndexError',
-}
-
-## TODO: stop using 'combined' to define this.
-typeSubstitutionMap = {
+typeSubs = {
'Boolean' : 'bool',
'Object' : 'object',
'String' : 'str',
'char' : 'str',
'double' : 'float',
'java.lang.String' : 'str',
-}
-
-
-
-
-
+ 'IndexOutOfBoundsException' : 'IndexError',
+}
from java2python.lang.selector import *
from java2python.mod import transforms
+
transforms = [
(Type('NULL'), transforms.null2None),
(Type('FALSE'), transforms.false2False),
(Type('TRUE'), transforms.true2True),
(Type('IDENT'), transforms.keywordSafeIdent),
(Type('FLOATING_POINT_LITERAL'), transforms.syntaxSafeFloatLiteral),
+ (Type('TYPE') > Type('QUALIFIED_TYPE_IDENT') > Type('IDENT'),
transforms.typeSub)
]
=======================================
--- /branches/0.5/java2python/lib/__init__.py Sat Jul 17 04:01:55 2010
+++ /branches/0.5/java2python/lib/__init__.py Mon Jul 19 16:53:47 2010
@@ -2,36 +2,6 @@
# -*- coding: utf-8 -*-
-def getModule(name, reloaded=False):
- """ getModule(name) -> import and return a module by name in dotted
form
-
- Copied from the Python lib docs.
-
- @param name module or package name in dotted form
- @return module object
- """
- mod = __import__(name)
- if reloaded:
- reload(mod)
- for comp in name.split('.')[1:]:
- mod = getattr(mod, comp)
- if reloaded:
- reload(mod)
- return mod
-
-
-def getModuleValue(name, reloaded=False):
- """ import an item from a module by dotted name
-
- @param name module and attribute string, i.e., foo.bar.baz
- @return value of name from module
- """
- names = name.split('.')
- modname, itemname = names[0:-1], names[-1]
- mod = getModule(str.join('.', modname), reloaded=reloaded)
- return getattr(mod, itemname)
-
-
class FS(object):
l = '{left}'
r = '{right}'
=======================================
--- /branches/0.5/java2python/mod/transforms.py Mon Jul 19 15:02:15 2010
+++ /branches/0.5/java2python/mod/transforms.py Mon Jul 19 16:53:47 2010
@@ -14,14 +14,14 @@
renameIdents = renameIdents()
-def keywordSafeIdent(node):
+def keywordSafeIdent(node, config):
ident = node.token.text
if ident in renameIdents:
node.token.text = '%s_' % ident
def constXform(v):
- def xform(node):
+ def xform(node, config):
node.token.text = v
return xform
@@ -31,7 +31,7 @@
true2True = constXform('True')
-def syntaxSafeFloatLiteral(node):
+def syntaxSafeFloatLiteral(node, config):
value = node.token.text
if value.startswith('.'):
value = '0' + value
@@ -42,3 +42,8 @@
node.token.text = value
+def typeSub(node, config):
+ ident = node.token.text
+ subs = config.last('typeSubs')
+ if ident in subs:
+ node.token.text = subs[ident]