From: <fwi...@us...> - 2008-12-03 17:59:23
|
Revision: 5683 http://jython.svn.sourceforge.net/jython/?rev=5683&view=rev Author: fwierzbicki Date: 2008-12-03 17:59:13 +0000 (Wed, 03 Dec 2008) Log Message: ----------- Now working on Python 2.6 style test_ast again. Modified Paths: -------------- branches/astwrite/Lib/test/test_ast.py branches/astwrite/ast/asdl_antlr.py branches/astwrite/src/org/python/antlr/AST.java branches/astwrite/src/org/python/antlr/PythonTree.java branches/astwrite/src/org/python/antlr/adapter/AstAdapters.java branches/astwrite/src/org/python/antlr/ast/Add.java branches/astwrite/src/org/python/antlr/ast/And.java branches/astwrite/src/org/python/antlr/ast/Assert.java branches/astwrite/src/org/python/antlr/ast/Assign.java branches/astwrite/src/org/python/antlr/ast/AstModule.java branches/astwrite/src/org/python/antlr/ast/Attribute.java branches/astwrite/src/org/python/antlr/ast/AugAssign.java branches/astwrite/src/org/python/antlr/ast/AugLoad.java branches/astwrite/src/org/python/antlr/ast/AugStore.java branches/astwrite/src/org/python/antlr/ast/BinOp.java branches/astwrite/src/org/python/antlr/ast/BitAnd.java branches/astwrite/src/org/python/antlr/ast/BitOr.java branches/astwrite/src/org/python/antlr/ast/BitXor.java branches/astwrite/src/org/python/antlr/ast/BoolOp.java branches/astwrite/src/org/python/antlr/ast/Break.java branches/astwrite/src/org/python/antlr/ast/Call.java branches/astwrite/src/org/python/antlr/ast/ClassDef.java branches/astwrite/src/org/python/antlr/ast/Compare.java branches/astwrite/src/org/python/antlr/ast/Continue.java branches/astwrite/src/org/python/antlr/ast/Del.java branches/astwrite/src/org/python/antlr/ast/Delete.java branches/astwrite/src/org/python/antlr/ast/Dict.java branches/astwrite/src/org/python/antlr/ast/Div.java branches/astwrite/src/org/python/antlr/ast/Ellipsis.java branches/astwrite/src/org/python/antlr/ast/Eq.java branches/astwrite/src/org/python/antlr/ast/ExceptHandler.java branches/astwrite/src/org/python/antlr/ast/Exec.java branches/astwrite/src/org/python/antlr/ast/Expr.java branches/astwrite/src/org/python/antlr/ast/Expression.java branches/astwrite/src/org/python/antlr/ast/ExtSlice.java branches/astwrite/src/org/python/antlr/ast/FloorDiv.java branches/astwrite/src/org/python/antlr/ast/For.java branches/astwrite/src/org/python/antlr/ast/FunctionDef.java branches/astwrite/src/org/python/antlr/ast/GeneratorExp.java branches/astwrite/src/org/python/antlr/ast/Global.java branches/astwrite/src/org/python/antlr/ast/Gt.java branches/astwrite/src/org/python/antlr/ast/GtE.java branches/astwrite/src/org/python/antlr/ast/If.java branches/astwrite/src/org/python/antlr/ast/IfExp.java branches/astwrite/src/org/python/antlr/ast/Import.java branches/astwrite/src/org/python/antlr/ast/ImportFrom.java branches/astwrite/src/org/python/antlr/ast/In.java branches/astwrite/src/org/python/antlr/ast/Index.java branches/astwrite/src/org/python/antlr/ast/Interactive.java branches/astwrite/src/org/python/antlr/ast/Invert.java branches/astwrite/src/org/python/antlr/ast/Is.java branches/astwrite/src/org/python/antlr/ast/IsNot.java branches/astwrite/src/org/python/antlr/ast/LShift.java branches/astwrite/src/org/python/antlr/ast/Lambda.java branches/astwrite/src/org/python/antlr/ast/List.java branches/astwrite/src/org/python/antlr/ast/ListComp.java branches/astwrite/src/org/python/antlr/ast/Load.java branches/astwrite/src/org/python/antlr/ast/Lt.java branches/astwrite/src/org/python/antlr/ast/LtE.java branches/astwrite/src/org/python/antlr/ast/Mod.java branches/astwrite/src/org/python/antlr/ast/Module.java branches/astwrite/src/org/python/antlr/ast/Mult.java branches/astwrite/src/org/python/antlr/ast/Name.java branches/astwrite/src/org/python/antlr/ast/Not.java branches/astwrite/src/org/python/antlr/ast/NotEq.java branches/astwrite/src/org/python/antlr/ast/NotIn.java branches/astwrite/src/org/python/antlr/ast/Num.java branches/astwrite/src/org/python/antlr/ast/Or.java branches/astwrite/src/org/python/antlr/ast/Param.java branches/astwrite/src/org/python/antlr/ast/Pass.java branches/astwrite/src/org/python/antlr/ast/Pow.java branches/astwrite/src/org/python/antlr/ast/Print.java branches/astwrite/src/org/python/antlr/ast/RShift.java branches/astwrite/src/org/python/antlr/ast/Raise.java branches/astwrite/src/org/python/antlr/ast/Repr.java branches/astwrite/src/org/python/antlr/ast/Return.java branches/astwrite/src/org/python/antlr/ast/Slice.java branches/astwrite/src/org/python/antlr/ast/Store.java branches/astwrite/src/org/python/antlr/ast/Str.java branches/astwrite/src/org/python/antlr/ast/Sub.java branches/astwrite/src/org/python/antlr/ast/Subscript.java branches/astwrite/src/org/python/antlr/ast/Suite.java branches/astwrite/src/org/python/antlr/ast/TryExcept.java branches/astwrite/src/org/python/antlr/ast/TryFinally.java branches/astwrite/src/org/python/antlr/ast/Tuple.java branches/astwrite/src/org/python/antlr/ast/UAdd.java branches/astwrite/src/org/python/antlr/ast/USub.java branches/astwrite/src/org/python/antlr/ast/UnaryOp.java branches/astwrite/src/org/python/antlr/ast/VisitorBase.java branches/astwrite/src/org/python/antlr/ast/While.java branches/astwrite/src/org/python/antlr/ast/With.java branches/astwrite/src/org/python/antlr/ast/Yield.java branches/astwrite/src/org/python/antlr/ast/aliasType.java branches/astwrite/src/org/python/antlr/ast/argumentsType.java branches/astwrite/src/org/python/antlr/ast/boolopType.java branches/astwrite/src/org/python/antlr/ast/cmpopType.java branches/astwrite/src/org/python/antlr/ast/comprehensionType.java branches/astwrite/src/org/python/antlr/ast/excepthandlerType.java branches/astwrite/src/org/python/antlr/ast/exprType.java branches/astwrite/src/org/python/antlr/ast/expr_contextType.java branches/astwrite/src/org/python/antlr/ast/keywordType.java branches/astwrite/src/org/python/antlr/ast/modType.java branches/astwrite/src/org/python/antlr/ast/operatorType.java branches/astwrite/src/org/python/antlr/ast/sliceType.java branches/astwrite/src/org/python/antlr/ast/stmtType.java branches/astwrite/src/org/python/antlr/ast/unaryopType.java branches/astwrite/src/org/python/core/AstList.java Modified: branches/astwrite/Lib/test/test_ast.py =================================================================== --- branches/astwrite/Lib/test/test_ast.py 2008-12-03 14:43:11 UTC (rev 5682) +++ branches/astwrite/Lib/test/test_ast.py 2008-12-03 17:59:13 UTC (rev 5683) @@ -1,21 +1,22 @@ -#Taken and modified from CPython's release25-maint branch, revision 62446. -import sys,os, itertools +import sys, itertools, unittest +from test import test_support import ast def to_tuple(t): if t is None or isinstance(t, (basestring, int, long, complex)): return t - elif hasattr(t, '__iter__'): + elif hasattr(t, "__iter__"): return [to_tuple(e) for e in t] result = [t.__class__.__name__] if hasattr(t, 'lineno') and hasattr(t, 'col_offset'): result.append((t.lineno, t.col_offset)) - if not hasattr(t, '_fields') or t._fields is None: + if t._fields is None: return tuple(result) for f in t._fields: result.append(to_tuple(getattr(t, f))) return tuple(result) + # These tests are compiled through "exec" # There should be atleast one test per statement exec_tests = [ @@ -119,49 +120,179 @@ # TODO: expr_context, slice, boolop, operator, unaryop, cmpop, comprehension # excepthandler, arguments, keywords, alias -if __name__=='__main__' and sys.argv[1:] == ['-g']: - for statements, kind in ((exec_tests, "exec"), (single_tests, "single"), - (eval_tests, "eval")): - print kind+"_results = [" - for s in statements: - print repr(to_tuple(compile(s, "?", kind, 0x400)))+"," - print "]" - print "run_tests()" - raise SystemExit +class AST_Tests(unittest.TestCase): -def test_order(ast_node, parent_pos): - - if (not isinstance(ast_node, ast.AST) - or not hasattr(ast_node, '_fields') - or ast_node._fields == None): + def _assert_order(self, ast_node, parent_pos): + if not isinstance(ast_node, ast.AST) or ast_node._fields is None: return - if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)): - node_pos = (ast_node.lineno, ast_node.col_offset) - assert node_pos >= parent_pos, (node_pos, parent_pos) - parent_pos = (ast_node.lineno, ast_node.col_offset) - for name in ast_node._fields: - value = getattr(ast_node, name) - if hasattr(value, '__iter__'): - for child in value: - test_order(child, parent_pos) - elif value != None: - test_order(value, parent_pos) + if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)): + node_pos = (ast_node.lineno, ast_node.col_offset) + self.assert_(node_pos >= parent_pos) + parent_pos = (ast_node.lineno, ast_node.col_offset) + for name in ast_node._fields: + value = getattr(ast_node, name) + if hasattr(value, "__iter__"): + for child in value: + self._assert_order(child, parent_pos) + elif value is not None: + self._assert_order(value, parent_pos) -def run_tests(): - for input, output, kind in ((exec_tests, exec_results, "exec"), - (single_tests, single_results, "single"), - (eval_tests, eval_results, "eval")): - for i, o in itertools.izip(input, output): - ast_tree = compile(i, "?", kind, 0x400) - assert to_tuple(ast_tree) == o, "expected %s, got %s" % ( - o, to_tuple(ast_tree)) - test_order(ast_tree, (0, 0)) + def test_snippets(self): + for input, output, kind in ((exec_tests, exec_results, "exec"), + (single_tests, single_results, "single"), + (eval_tests, eval_results, "eval")): + for i, o in itertools.izip(input, output): + ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST) + self.assertEquals(to_tuple(ast_tree), o) + self._assert_order(ast_tree, (0, 0)) -# XXX: AugStore added for Jython. Short term it is too hard to emit just "Store" as CPython does. + def test_nodeclasses(self): + x = ast.BinOp(1, 2, 3, lineno=0) + self.assertEquals(x.left.n, 1) + self.assertEquals(int(x.op), 2) + self.assertEquals(x.right.n, 3) + self.assertEquals(x.lineno, 0) + + # node raises exception when not given enough arguments + self.assertRaises(TypeError, ast.BinOp, 1, 2) + + # can set attributes through kwargs too + x = ast.BinOp(left=1, op=2, right=3, lineno=0) + self.assertEquals(x.left.n, 1) + self.assertEquals(int(x.op), 2) + self.assertEquals(x.right.n, 3) + self.assertEquals(x.lineno, 0) + + # this used to fail because Sub._fields was None + x = ast.Sub() + + def test_pickling(self): + import pickle + mods = [pickle] + try: + import cPickle + mods.append(cPickle) + except ImportError: + pass + protocols = [0, 1, 2] + for mod in mods: + for protocol in protocols: + for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests): + ast2 = mod.loads(mod.dumps(ast, protocol)) + self.assertEquals(to_tuple(ast2), to_tuple(ast)) + + +class ASTHelpers_Test(unittest.TestCase): + + def test_parse(self): + a = ast.parse('foo(1 + 1)') + b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST) + self.assertEqual(ast.dump(a), ast.dump(b)) + + def test_dump(self): + node = ast.parse('spam(eggs, "and cheese")') + self.assertEqual(ast.dump(node), + "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), " + "args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], " + "keywords=[], starargs=None, kwargs=None))])" + ) + self.assertEqual(ast.dump(node, annotate_fields=False), + "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), " + "Str('and cheese')], [], None, None))])" + ) + self.assertEqual(ast.dump(node, include_attributes=True), + "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), " + "lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), " + "lineno=1, col_offset=5), Str(s='and cheese', lineno=1, " + "col_offset=11)], keywords=[], starargs=None, kwargs=None, " + "lineno=1, col_offset=0), lineno=1, col_offset=0)])" + ) + + def test_copy_location(self): + src = ast.parse('1 + 1', mode='eval') + src.body.right = ast.copy_location(ast.Num(2), src.body.right) + self.assertEqual(ast.dump(src, include_attributes=True), + 'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), ' + 'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, ' + 'col_offset=0))' + ) + + def test_fix_missing_locations(self): + src = ast.parse('write("spam")') + src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()), + [ast.Str('eggs')], [], None, None))) + self.assertEqual(src, ast.fix_missing_locations(src)) + self.assertEqual(ast.dump(src, include_attributes=True), + "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), " + "lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, " + "col_offset=6)], keywords=[], starargs=None, kwargs=None, " + "lineno=1, col_offset=0), lineno=1, col_offset=0), " + "Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, " + "col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], " + "keywords=[], starargs=None, kwargs=None, lineno=1, " + "col_offset=0), lineno=1, col_offset=0)])" + ) + + def test_increment_lineno(self): + src = ast.parse('1 + 1', mode='eval') + self.assertEqual(ast.increment_lineno(src, n=3), src) + self.assertEqual(ast.dump(src, include_attributes=True), + 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), ' + 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, ' + 'col_offset=0))' + ) + + def test_iter_fields(self): + node = ast.parse('foo()', mode='eval') + d = dict(ast.iter_fields(node.body)) + self.assertEqual(d.pop('func').id, 'foo') + self.assertEqual(d, {'keywords': [], 'kwargs': None, + 'args': [], 'starargs': None}) + + def test_iter_child_nodes(self): + node = ast.parse("spam(23, 42, eggs='leek')", mode='eval') + self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4) + iterator = ast.iter_child_nodes(node.body) + self.assertEqual(next(iterator).id, 'spam') + self.assertEqual(next(iterator).n, 23) + self.assertEqual(next(iterator).n, 42) + self.assertEqual(ast.dump(next(iterator)), + "keyword(arg='eggs', value=Str(s='leek'))" + ) + + def test_get_docstring(self): + node = ast.parse('def foo():\n """line one\n line two"""') + self.assertEqual(ast.get_docstring(node.body[0]), + 'line one\nline two') + + def test_literal_eval(self): + self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3]) + self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42}) + self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None)) + self.assertRaises(ValueError, ast.literal_eval, 'foo()') + + +def test_main(): + test_support.run_unittest(AST_Tests, ASTHelpers_Test) + +def main(): + if __name__ != '__main__': + return + if sys.argv[1:] == ['-g']: + for statements, kind in ((exec_tests, "exec"), (single_tests, "single"), + (eval_tests, "eval")): + print kind+"_results = [" + for s in statements: + print repr(to_tuple(compile(s, "?", kind, 0x400)))+"," + print "]" + print "main()" + raise SystemExit + test_main() + #### EVERYTHING BELOW IS GENERATED ##### exec_results = [ ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Pass', (1, 9))], [])]), -('Module', [('ClassDef', (1, 0), 'C', [], [('Pass', (1, 8))],[])]), +('Module', [('ClassDef', (1, 0), 'C', [], [('Pass', (1, 8))], [])]), ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, []), [('Return', (1, 8), ('Num', (1, 15), 1))], [])]), ('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]), ('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]), @@ -206,4 +337,4 @@ ('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))), ('Expression', ('Call', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 8), ('Attribute', (1, 8), ('Name', (1, 8), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Num', (1, 12), 1), ('Num', (1, 14), 2), None), ('Load',))], [], None, None)), ] -run_tests() +main() Modified: branches/astwrite/ast/asdl_antlr.py =================================================================== --- branches/astwrite/ast/asdl_antlr.py 2008-12-03 14:43:11 UTC (rev 5682) +++ branches/astwrite/ast/asdl_antlr.py 2008-12-03 17:59:13 UTC (rev 5683) @@ -9,7 +9,7 @@ import asdl TABSIZE = 4 -MAX_COL = 80 +MAX_COL = 100 def reflow_lines(s, depth): """Reflow the line s indented depth tabs. @@ -58,6 +58,7 @@ if refersToPythonTree: print >> self.file, 'import org.antlr.runtime.CommonToken;' print >> self.file, 'import org.antlr.runtime.Token;' + print >> self.file, 'import org.python.antlr.AST;' print >> self.file, 'import org.python.antlr.PythonTree;' print >> self.file, 'import org.python.antlr.adapter.AstAdapters;' print >> self.file, 'import org.python.core.AstList;' @@ -154,6 +155,7 @@ self.open("%sType" % name, refersToPythonTree=0) self.emit('import org.python.antlr.AST;', depth) self.emit('', 0) + self.emit("public enum %(name)sType {" % locals(), depth) self.emit("UNDEFINED,", depth + 1) for i in range(len(sum.types) - 1): @@ -161,11 +163,6 @@ self.emit("%s," % type.name, depth + 1) self.emit("%s;" % sum.types[len(sum.types) - 1].name, depth + 1) - #XXX - #self.attributes(sum, depth, True); - - self.emit("public %sType __call__() { return this; }" % name, depth + 1) - self.emit("}", depth) self.close() @@ -174,63 +171,91 @@ type = sum.types[i] self.open("%s" % type.name, refersToPythonTree=0) self.emit('import org.python.antlr.AST;', depth) + self.emit('import org.python.antlr.PythonTree;', depth) + self.emit('import org.python.core.Py;', depth) self.emit('import org.python.core.PyObject;', depth) + self.emit('import org.python.core.PyString;', depth) self.emit('import org.python.core.PyType;', depth) + self.emit('import org.python.expose.ExposedGet;', depth) + self.emit('import org.python.expose.ExposedMethod;', depth) + self.emit('import org.python.expose.ExposedNew;', depth) + self.emit('import org.python.expose.ExposedSet;', depth) self.emit('import org.python.expose.ExposedType;', depth) self.emit('', 0) - self.emit('@ExposedType(name = "_ast.%s", base = PyObject.class)' % type.name, depth) - self.emit("public class %s extends AST {" % type.name, depth) + + self.emit('@ExposedType(name = "_ast.%s", base = AST.class)' % type.name, depth) + self.emit("public class %s extends PythonTree {" % type.name, depth) self.emit('public static final PyType TYPE = PyType.fromClass(%s.class);' % type.name, depth + 1) + self.emit('', 0) - self.emit('public int asIndex() {', depth + 1) - self.emit('return asIndex(null);', depth + 2) + self.emit("public %s() {" % (type.name), depth) + self.emit("}", depth) + self.emit('', 0) + + self.emit("public %s(PyType subType) {" % (type.name), depth) + self.emit("super(subType);", depth + 1) + self.emit("}", depth) + self.emit('', 0) + + self.emit("@ExposedNew", depth) + self.emit("@ExposedMethod", depth) + self.emit("public void %s___init__(PyObject[] args, String[] keywords) {}" % type.name, depth) + self.emit('', 0) + + self.attributes(type, depth); + + self.emit('@ExposedMethod', depth + 1) + self.emit('public PyObject __int__() {', depth + 1) + self.emit("return %s___int__();" % type.name, depth + 2) self.emit("}", depth + 1) + self.emit('', 0) - self.emit('public int asIndex(PyObject error) {', depth + 1) - self.emit('return %s;' % str(i + 1), depth + 2) + self.emit("final PyObject %s___int__() {" % type.name, depth + 1) + self.emit('return Py.newInteger(%s);' % str(i + 1), depth + 2) self.emit("}", depth + 1) + self.emit('', 0) self.emit("}", depth) self.close() - def attributes(self, obj, depth, always_emit=False): + def attributes(self, obj, depth): field_list = [] if hasattr(obj, "fields"): for f in obj.fields: - field_list.append('"%s"' % f.name) + field_list.append('new PyString("%s")' % f.name) if len(field_list) > 0: - self.emit("private final static String[] fields = new String[] {%s};" % - ", ".join(field_list), depth+1) - self.emit('@ExposedGet(name = "_fields")', depth) - self.emit("public String[] get_fields() { return fields; }", depth+1) + self.emit("private final static PyString[] fields =", depth + 1) + self.emit("new PyString[] {%s};" % ", ".join(field_list), depth+1) + self.emit('@ExposedGet(name = "_fields")', depth + 1) + self.emit("public PyString[] get_fields() { return fields; }", depth+1) self.emit("", 0) - elif always_emit: - self.emit("private final static String[] fields = new String[0];", depth+1) - self.emit('@ExposedGet(name = "_fields")', depth) - self.emit("public String[] get_fields() { return fields; }", depth+1) + else: + self.emit("private final static PyString[] fields = new PyString[0];", depth+1) + self.emit('@ExposedGet(name = "_fields")', depth + 1) + self.emit("public PyString[] get_fields() { return fields; }", depth+1) self.emit("", 0) att_list = [] if hasattr(obj, "attributes"): for a in obj.attributes: - att_list.append('"%s"' % a.name) + att_list.append('new PyString("%s")' % a.name) if len(att_list) > 0: - self.emit("private final static String[] attributes = new String[] {%s};" % - ", ".join(att_list), depth+1) - self.emit('@ExposedGet(name = "_attributes")', depth) - self.emit("public String[] get_attributes() { return attributes; }", depth+1) + self.emit("private final static PyString[] attributes =", depth + 1) + self.emit("new PyString[] {%s};" % ", ".join(att_list), depth + 1) + self.emit('@ExposedGet(name = "_attributes")', depth + 1) + self.emit("public PyString[] get_attributes() { return attributes; }", depth + 1) self.emit("", 0) - elif always_emit: - self.emit("private final static String[] attributes = new String[0];", depth+1) - self.emit('@ExposedGet(name = "_attributes")', depth) - self.emit("public String[] get_attributes() { return attributes; }", depth+1) + else: + self.emit("private final static PyString[] attributes = new PyString[0];", depth+1) + self.emit('@ExposedGet(name = "_attributes")', depth + 1) + self.emit("public PyString[] get_attributes() { return attributes; }", depth+1) self.emit("", 0) def sum_with_constructor(self, sum, name, depth): self.open("%sType" % name) - self.emit('@ExposedType(name = "_ast.%s", base = PyObject.class)' % name, depth) + self.emit('@ExposedType(name = "_ast.%s", base = AST.class)' % name, depth) self.emit("public abstract class %(name)sType extends PythonTree {" % locals(), depth) self.emit("", 0) @@ -269,7 +294,7 @@ def visitProduct(self, product, name, depth): self.open("%sType" % name, useDataOutput=1) - self.emit('@ExposedType(name = "_ast.%s", base = PyObject.class)' % name, depth) + self.emit('@ExposedType(name = "_ast.%s", base = AST.class)' % name, depth) self.emit("public class %(name)sType extends PythonTree {" % locals(), depth) self.emit("public static final PyType TYPE = PyType.fromClass(%sType.class);" % name, depth + 1); for f in product.fields: @@ -294,7 +319,7 @@ s = "implements %s " % ", ".join(ifaces) else: s = "" - self.emit('@ExposedType(name = "_ast.%s", base = PyObject.class)' % cons.name, depth); + self.emit('@ExposedType(name = "_ast.%s", base = AST.class)' % cons.name, depth); self.emit("public class %s extends %sType %s{" % (cons.name, name, s), depth) self.emit("public static final PyType TYPE = PyType.fromClass(%s.class);" % cons.name, depth); @@ -315,7 +340,7 @@ if str(name) in ('stmt', 'expr', 'excepthandler'): # The lineno property self.emit("private int lineno = -1;", depth + 1) - self.emit('@ExposedGet(name = "lineno")', depth) + self.emit('@ExposedGet(name = "lineno")', depth + 1) self.emit("public int getLineno() {", depth + 1) self.emit("if (lineno != -1) {", depth + 2); self.emit("return lineno;", depth + 3); @@ -323,7 +348,7 @@ self.emit('return getLine();', depth + 2) self.emit("}", depth + 1) self.emit("", 0) - self.emit('@ExposedSet(name = "lineno")', depth) + self.emit('@ExposedSet(name = "lineno")', depth + 1) self.emit("public void setLineno(int num) {", depth + 1) self.emit("lineno = num;", depth + 2); self.emit("}", depth + 1) @@ -331,7 +356,7 @@ # The col_offset property self.emit("private int col_offset = -1;", depth + 1) - self.emit('@ExposedGet(name = "col_offset")', depth) + self.emit('@ExposedGet(name = "col_offset")', depth + 1) self.emit("public int getCol_offset() {", depth + 1) self.emit("if (col_offset != -1) {", depth + 2); self.emit("return col_offset;", depth + 3); @@ -339,7 +364,7 @@ self.emit('return getCharPositionInLine();', depth + 2) self.emit("}", depth + 1) self.emit("", 0) - self.emit('@ExposedSet(name = "col_offset")', depth) + self.emit('@ExposedSet(name = "col_offset")', depth + 1) self.emit("public void setCol_offset(int num) {", depth + 1) self.emit("col_offset = num;", depth + 2); self.emit("}", depth + 1) @@ -443,7 +468,7 @@ self.emit("@ExposedNew", depth) self.emit("@ExposedMethod", depth) - self.emit("public void Module___init__(PyObject[] args, String[] keywords) {}", depth) + self.emit("public void %s___init__(PyObject[] args, String[] keywords) {}" % ctorname, depth) fpargs = ", ".join(["PyObject %s" % f.name for f in fields]) self.emit("public %s(%s) {" % (ctorname, fpargs), depth) @@ -503,7 +528,7 @@ elif str(field.type) == 'int': self.emit("return Py.newInteger(%s);" % field.name, depth+1) elif field.typedef.simple: - self.emit("return AstAdapters.%s2py(%s);" % (field.name, field.name), depth+1) + self.emit("return AstAdapters.%s2py(%s);" % (str(field.type), field.name), depth+1) else: self.emit("return %s;" % field.name, depth+1) #self.emit("return Py.None;", depth+1) Modified: branches/astwrite/src/org/python/antlr/AST.java =================================================================== --- branches/astwrite/src/org/python/antlr/AST.java 2008-12-03 14:43:11 UTC (rev 5682) +++ branches/astwrite/src/org/python/antlr/AST.java 2008-12-03 17:59:13 UTC (rev 5683) @@ -5,8 +5,14 @@ import org.python.expose.ExposedType; @ExposedType(name = "_ast.AST", base = PyObject.class) -public abstract class AST extends PyObject { +public class AST extends PyObject { public static final PyType TYPE = PyType.fromClass(AST.class); - public static String[] emptyStringArray = new String[0]; + + public AST() { + } + public AST(PyType objtype) { + super(objtype); + } + } Modified: branches/astwrite/src/org/python/antlr/PythonTree.java =================================================================== --- branches/astwrite/src/org/python/antlr/PythonTree.java 2008-12-03 14:43:11 UTC (rev 5682) +++ branches/astwrite/src/org/python/antlr/PythonTree.java 2008-12-03 17:59:13 UTC (rev 5683) @@ -250,14 +250,6 @@ throw new RuntimeException("Cannot traverse node: " + this); } - public String[] get_fields() { - return emptyStringArray; - } - - public String[] get_attributes() { - return emptyStringArray; - } - //Copied from org.antlr.runtime.tree.BaseTree protected List children; Modified: branches/astwrite/src/org/python/antlr/adapter/AstAdapters.java =================================================================== --- branches/astwrite/src/org/python/antlr/adapter/AstAdapters.java 2008-12-03 14:43:11 UTC (rev 5682) +++ branches/astwrite/src/org/python/antlr/adapter/AstAdapters.java 2008-12-03 17:59:13 UTC (rev 5683) @@ -97,12 +97,19 @@ public static operatorType py2operator(Object o) { if (o == null || o instanceof operatorType) { return (operatorType)o; + } else if (o instanceof PyObject) { + switch (((PyObject)o).asInt()) { + case 1: + return operatorType.Add; + case 2: + return operatorType.Sub; + } } //FIXME: investigate the right exception throw Py.TypeError("Can't convert " + o.getClass().getName() + " to operator node"); } - public static PyObject op2py(operatorType o) { + public static PyObject operator2py(operatorType o) { switch (o) { case Add: return new Add(); @@ -132,7 +139,7 @@ return Py.None; } - public static PyObject op2py(boolopType o) { + public static PyObject boolop2py(boolopType o) { switch (o) { case And: return new And(); @@ -142,11 +149,11 @@ return Py.None; } - public static PyObject op2py(cmpopType o) { + public static PyObject cmpop2py(cmpopType o) { return cmpopAdapter.ast2py(o); } - public static PyObject op2py(unaryopType o) { + public static PyObject unaryop2py(unaryopType o) { switch (o) { case Invert: return new Invert(); @@ -161,7 +168,7 @@ } - public static PyObject ctx2py(expr_contextType o) { + public static PyObject expr_context2py(expr_contextType o) { switch (o) { case Load: return new Load(); Modified: branches/astwrite/src/org/python/antlr/ast/Add.java =================================================================== --- branches/astwrite/src/org/python/antlr/ast/Add.java 2008-12-03 14:43:11 UTC (rev 5682) +++ branches/astwrite/src/org/python/antlr/ast/Add.java 2008-12-03 17:59:13 UTC (rev 5683) @@ -2,17 +2,47 @@ package org.python.antlr.ast; import org.python.antlr.AST; +import org.python.antlr.PythonTree; +import org.python.core.Py; import org.python.core.PyObject; +import org.python.core.PyString; import org.python.core.PyType; +import org.python.expose.ExposedGet; +import org.python.expose.ExposedMethod; +import org.python.expose.ExposedNew; +import org.python.expose.ExposedSet; import org.python.expose.ExposedType; -@ExposedType(name = "_ast.Add", base = PyObject.class) -public class Add extends AST { +@ExposedType(name = "_ast.Add", base = AST.class) +public class Add extends PythonTree { public static final PyType TYPE = PyType.fromClass(Add.class); - public int asIndex() { - return asIndex(null); + +public Add() { +} + +public Add(PyType subType) { + super(subType); +} + +@ExposedNew +@ExposedMethod +public void Add___init__(PyObject[] args, String[] keywords) {} + + private final static PyString[] fields = new PyString[0]; + @ExposedGet(name = "_fields") + public PyString[] get_fields() { return fields; } + + private final static PyString[] attributes = new PyString[0]; + @ExposedGet(name = "_attributes") + public PyString[] get_attributes() { return attributes; } + + @ExposedMethod + public PyObject __int__() { + return Add___int__(); } - public int asIndex(PyObject error) { - return 1; + + final PyObject Add___int__() { + return Py.newInteger(1); } + } Modified: branches/astwrite/src/org/python/antlr/ast/And.java =================================================================== --- branches/astwrite/src/org/python/antlr/ast/And.java 2008-12-03 14:43:11 UTC (rev 5682) +++ branches/astwrite/src/org/python/antlr/ast/And.java 2008-12-03 17:59:13 UTC (rev 5683) @@ -2,17 +2,47 @@ package org.python.antlr.ast; import org.python.antlr.AST; +import org.python.antlr.PythonTree; +import org.python.core.Py; import org.python.core.PyObject; +import org.python.core.PyString; import org.python.core.PyType; +import org.python.expose.ExposedGet; +import org.python.expose.ExposedMethod; +import org.python.expose.ExposedNew; +import org.python.expose.ExposedSet; import org.python.expose.ExposedType; -@ExposedType(name = "_ast.And", base = PyObject.class) -public class And extends AST { +@ExposedType(name = "_ast.And", base = AST.class) +public class And extends PythonTree { public static final PyType TYPE = PyType.fromClass(And.class); - public int asIndex() { - return asIndex(null); + +public And() { +} + +public And(PyType subType) { + super(subType); +} + +@ExposedNew +@ExposedMethod +public void And___init__(PyObject[] args, String[] keywords) {} + + private final static PyString[] fields = new PyString[0]; + @ExposedGet(name = "_fields") + public PyString[] get_fields() { return fields; } + + private final static PyString[] attributes = new PyString[0]; + @ExposedGet(name = "_attributes") + public PyString[] get_attributes() { return attributes; } + + @ExposedMethod + public PyObject __int__() { + return And___int__(); } - public int asIndex(PyObject error) { - return 1; + + final PyObject And___int__() { + return Py.newInteger(1); } + } Modified: branches/astwrite/src/org/python/antlr/ast/Assert.java =================================================================== --- branches/astwrite/src/org/python/antlr/ast/Assert.java 2008-12-03 14:43:11 UTC (rev 5682) +++ branches/astwrite/src/org/python/antlr/ast/Assert.java 2008-12-03 17:59:13 UTC (rev 5683) @@ -2,6 +2,7 @@ package org.python.antlr.ast; import org.antlr.runtime.CommonToken; import org.antlr.runtime.Token; +import org.python.antlr.AST; import org.python.antlr.PythonTree; import org.python.antlr.adapter.AstAdapters; import org.python.core.AstList; @@ -18,7 +19,7 @@ import java.io.IOException; import java.util.ArrayList; -@ExposedType(name = "_ast.Assert", base = PyObject.class) +@ExposedType(name = "_ast.Assert", base = AST.class) public class Assert extends stmtType { public static final PyType TYPE = PyType.fromClass(Assert.class); private exprType test; @@ -48,10 +49,15 @@ } - private final static String[] fields = new String[] {"test", "msg"}; -@ExposedGet(name = "_fields") - public String[] get_fields() { return fields; } + private final static PyString[] fields = + new PyString[] {new PyString("test"), new PyString("msg")}; + @ExposedGet(name = "_fields") + public PyString[] get_fields() { return fields; } + private final static PyString[] attributes = new PyString[0]; + @ExposedGet(name = "_attributes") + public PyString[] get_attributes() { return attributes; } + public Assert() { this(TYPE); } @@ -60,7 +66,7 @@ } @ExposedNew @ExposedMethod - public void Module___init__(PyObject[] args, String[] keywords) {} + public void Assert___init__(PyObject[] args, String[] keywords) {} public Assert(PyObject test, PyObject msg) { setTest(test); setMsg(msg); @@ -119,7 +125,7 @@ } private int lineno = -1; -@ExposedGet(name = "lineno") + @ExposedGet(name = "lineno") public int getLineno() { if (lineno != -1) { return lineno; @@ -127,13 +133,13 @@ return getLine(); } -@ExposedSet(name = "lineno") + @ExposedSet(name = "lineno") public void setLineno(int num) { lineno = num; } private int col_offset = -1; -@ExposedGet(name = "col_offset") + @ExposedGet(name = "col_offset") public int getCol_offset() { if (col_offset != -1) { return col_offset; @@ -141,7 +147,7 @@ return getCharPositionInLine(); } -@ExposedSet(name = "col_offset") + @ExposedSet(name = "col_offset") public void setCol_offset(int num) { col_offset = num; } Modified: branches/astwrite/src/org/python/antlr/ast/Assign.java =================================================================== --- branches/astwrite/src/org/python/antlr/ast/Assign.java 2008-12-03 14:43:11 UTC (rev 5682) +++ branches/astwrite/src/org/python/antlr/ast/Assign.java 2008-12-03 17:59:13 UTC (rev 5683) @@ -2,6 +2,7 @@ package org.python.antlr.ast; import org.antlr.runtime.CommonToken; import org.antlr.runtime.Token; +import org.python.antlr.AST; import org.python.antlr.PythonTree; import org.python.antlr.adapter.AstAdapters; import org.python.core.AstList; @@ -18,7 +19,7 @@ import java.io.IOException; import java.util.ArrayList; -@ExposedType(name = "_ast.Assign", base = PyObject.class) +@ExposedType(name = "_ast.Assign", base = AST.class) public class Assign extends stmtType { public static final PyType TYPE = PyType.fromClass(Assign.class); private java.util.List<exprType> targets; @@ -48,10 +49,15 @@ } - private final static String[] fields = new String[] {"targets", "value"}; -@ExposedGet(name = "_fields") - public String[] get_fields() { return fields; } + private final static PyString[] fields = + new PyString[] {new PyString("targets"), new PyString("value")}; + @ExposedGet(name = "_fields") + public PyString[] get_fields() { return fields; } + private final static PyString[] attributes = new PyString[0]; + @ExposedGet(name = "_attributes") + public PyString[] get_attributes() { return attributes; } + public Assign() { this(TYPE); } @@ -60,14 +66,13 @@ } @ExposedNew @ExposedMethod - public void Module___init__(PyObject[] args, String[] keywords) {} + public void Assign___init__(PyObject[] args, String[] keywords) {} public Assign(PyObject targets, PyObject value) { setTargets(targets); setValue(value); } - public Assign(Token token, java.util.List<exprType> targets, exprType - value) { + public Assign(Token token, java.util.List<exprType> targets, exprType value) { super(token); this.targets = targets; if (targets == null) { @@ -80,8 +85,7 @@ addChild(value); } - public Assign(Integer ttype, Token token, java.util.List<exprType> targets, - exprType value) { + public Assign(Integer ttype, Token token, java.util.List<exprType> targets, exprType value) { super(ttype, token); this.targets = targets; if (targets == null) { @@ -94,8 +98,7 @@ addChild(value); } - public Assign(PythonTree tree, java.util.List<exprType> targets, exprType - value) { + public Assign(PythonTree tree, java.util.List<exprType> targets, exprType value) { super(tree); this.targets = targets; if (targets == null) { @@ -141,7 +144,7 @@ } private int lineno = -1; -@ExposedGet(name = "lineno") + @ExposedGet(name = "lineno") public int getLineno() { if (lineno != -1) { return lineno; @@ -149,13 +152,13 @@ return getLine(); } -@ExposedSet(name = "lineno") + @ExposedSet(name = "lineno") public void setLineno(int num) { lineno = num; } private int col_offset = -1; -@ExposedGet(name = "col_offset") + @ExposedGet(name = "col_offset") public int getCol_offset() { if (col_offset != -1) { return col_offset; @@ -163,7 +166,7 @@ return getCharPositionInLine(); } -@ExposedSet(name = "col_offset") + @ExposedSet(name = "col_offset") public void setCol_offset(int num) { col_offset = num; } Modified: branches/astwrite/src/org/python/antlr/ast/AstModule.java =================================================================== --- branches/astwrite/src/org/python/antlr/ast/AstModule.java 2008-12-03 14:43:11 UTC (rev 5682) +++ branches/astwrite/src/org/python/antlr/ast/AstModule.java 2008-12-03 17:59:13 UTC (rev 5683) @@ -90,48 +90,48 @@ dict.__setitem__("stmt", stmtType.TYPE); dict.__setitem__("operator", imp.importName("org.python.antlr.ast.operatorType", false)); - dict.__setitem__("Add", imp.importName("org.python.antlr.ast.operatorType.Add", false)); - dict.__setitem__("Sub", imp.importName("org.python.antlr.ast.operatorType.Sub", false)); - dict.__setitem__("Mult", imp.importName("org.python.antlr.ast.operatorType.Mult", false)); - dict.__setitem__("Div", imp.importName("org.python.antlr.ast.operatorType.Div", false)); - dict.__setitem__("FloorDiv", imp.importName("org.python.antlr.ast.operatorType.FloorDiv", false)); - dict.__setitem__("Mod", imp.importName("org.python.antlr.ast.operatorType.Mod", false)); - dict.__setitem__("LShift", imp.importName("org.python.antlr.ast.operatorType.LShift", false)); - dict.__setitem__("RShift", imp.importName("org.python.antlr.ast.operatorType.RShift", false)); - dict.__setitem__("BitOr", imp.importName("org.python.antlr.ast.operatorType.BitOr", false)); - dict.__setitem__("BitAnd", imp.importName("org.python.antlr.ast.operatorType.BitAnd", false)); - dict.__setitem__("BitXor", imp.importName("org.python.antlr.ast.operatorType.BitXor", false)); - dict.__setitem__("Pow", imp.importName("org.python.antlr.ast.operatorType.Pow", false)); + dict.__setitem__("Add", Add.TYPE); + dict.__setitem__("Sub", Sub.TYPE); + dict.__setitem__("Mult", Mult.TYPE); + dict.__setitem__("Div", Div.TYPE); + dict.__setitem__("FloorDiv", FloorDiv.TYPE); + dict.__setitem__("Mod", Mod.TYPE); + dict.__setitem__("LShift", LShift.TYPE); + dict.__setitem__("RShift", RShift.TYPE); + dict.__setitem__("BitOr", BitOr.TYPE); + dict.__setitem__("BitAnd", BitAnd.TYPE); + dict.__setitem__("BitXor", BitXor.TYPE); + dict.__setitem__("Pow", Pow.TYPE); dict.__setitem__("boolop", imp.importName("org.python.antlr.ast.boolopType", false)); - dict.__setitem__("And", imp.importName("org.python.antlr.ast.boolopType.And", false)); - dict.__setitem__("Or", imp.importName("org.python.antlr.ast.boolopType.Or", false)); + dict.__setitem__("And", And.TYPE); + dict.__setitem__("Or", Or.TYPE); dict.__setitem__("cmpop", imp.importName("org.python.antlr.ast.cmpopType", false)); - dict.__setitem__("Eq", imp.importName("org.python.antlr.ast.cmpopType.Eq", false)); - dict.__setitem__("Gt", imp.importName("org.python.antlr.ast.cmpopType.Gt", false)); - dict.__setitem__("GtE", imp.importName("org.python.antlr.ast.cmpopType.GtE", false)); - dict.__setitem__("In", imp.importName("org.python.antlr.ast.cmpopType.In", false)); - dict.__setitem__("Is", imp.importName("org.python.antlr.ast.cmpopType.Is", false)); - dict.__setitem__("IsNot", imp.importName("org.python.antlr.ast.cmpopType.IsNot", false)); - dict.__setitem__("Lt", imp.importName("org.python.antlr.ast.cmpopType.Lt", false)); - dict.__setitem__("LtE", imp.importName("org.python.antlr.ast.cmpopType.LtE", false)); - dict.__setitem__("NotEq", imp.importName("org.python.antlr.ast.cmpopType.NotEq", false)); - dict.__setitem__("NotIn", imp.importName("org.python.antlr.ast.cmpopType.NotIn", false)); + dict.__setitem__("Eq", Eq.TYPE); + dict.__setitem__("Gt", Gt.TYPE); + dict.__setitem__("GtE", GtE.TYPE); + dict.__setitem__("In", In.TYPE); + dict.__setitem__("Is", Is.TYPE); + dict.__setitem__("IsNot", IsNot.TYPE); + dict.__setitem__("Lt", Lt.TYPE); + dict.__setitem__("LtE", LtE.TYPE); + dict.__setitem__("NotEq", NotEq.TYPE); + dict.__setitem__("NotIn", NotIn.TYPE); dict.__setitem__("expr_context", imp.importName("org.python.antlr.ast.expr_contextType", false)); - dict.__setitem__("Load", imp.importName("org.python.antlr.ast.expr_contextType.Load", false)); - dict.__setitem__("Store", imp.importName("org.python.antlr.ast.expr_contextType.Store", false)); - dict.__setitem__("Del", imp.importName("org.python.antlr.ast.expr_contextType.Del", false)); - dict.__setitem__("AugLoad", imp.importName("org.python.antlr.ast.expr_contextType.AugLoad", false)); - dict.__setitem__("AugStore", imp.importName("org.python.antlr.ast.expr_contextType.AugStore", false)); - dict.__setitem__("Param", imp.importName("org.python.antlr.ast.expr_contextType.Param", false)); + dict.__setitem__("Load", Load.TYPE); + dict.__setitem__("Store", Store.TYPE); + dict.__setitem__("Del", Del.TYPE); + dict.__setitem__("AugLoad", AugLoad.TYPE); + dict.__setitem__("AugStore", AugStore.TYPE); + dict.__setitem__("Param", Param.TYPE); dict.__setitem__("unaryop", imp.importName("org.python.antlr.ast.unaryopType", false)); - dict.__setitem__("Invert", imp.importName("org.python.antlr.ast.unaryopType.Invert", false)); - dict.__setitem__("Not", imp.importName("org.python.antlr.ast.unaryopType.Not", false)); - dict.__setitem__("UAdd", imp.importName("org.python.antlr.ast.unaryopType.UAdd", false)); - dict.__setitem__("USub", imp.importName("org.python.antlr.ast.unaryopType.USub", false)); + dict.__setitem__("Invert", Invert.TYPE); + dict.__setitem__("Not", Not.TYPE); + dict.__setitem__("UAdd", UAdd.TYPE); + dict.__setitem__("USub", USub.TYPE); dict.__setitem__("classDictInit", null); } Modified: branches/astwrite/src/org/python/antlr/ast/Attribute.java =================================================================== --- branches/astwrite/src/org/python/antlr/ast/Attribute.java 2008-12-03 14:43:11 UTC (rev 5682) +++ branches/astwrite/src/org/python/antlr/ast/Attribute.java 2008-12-03 17:59:13 UTC (rev 5683) @@ -2,6 +2,7 @@ package org.python.antlr.ast; import org.antlr.runtime.CommonToken; import org.antlr.runtime.Token; +import org.python.antlr.AST; import org.python.antlr.PythonTree; import org.python.antlr.adapter.AstAdapters; import org.python.core.AstList; @@ -18,7 +19,7 @@ import java.io.IOException; import java.util.ArrayList; -@ExposedType(name = "_ast.Attribute", base = PyObject.class) +@ExposedType(name = "_ast.Attribute", base = AST.class) public class Attribute extends exprType implements Context { public static final PyType TYPE = PyType.fromClass(Attribute.class); private exprType value; @@ -54,7 +55,7 @@ } @ExposedGet(name = "ctx") public PyObject getCtx() { - return AstAdapters.ctx2py(ctx); + return AstAdapters.expr_context2py(ctx); } @ExposedSet(name = "ctx") public void setCtx(PyObject ctx) { @@ -62,11 +63,15 @@ } - private final static String[] fields = new String[] {"value", "attr", - "ctx"}; -@ExposedGet(name = "_fields") - public String[] get_fields() { return fields; } + private final static PyString[] fields = + new PyString[] {new PyString("value"), new PyString("attr"), new PyString("ctx")}; + @ExposedGet(name = "_fields") + public PyString[] get_fields() { return fields; } + private final static PyString[] attributes = new PyString[0]; + @ExposedGet(name = "_attributes") + public PyString[] get_attributes() { return attributes; } + public Attribute() { this(TYPE); } @@ -75,15 +80,14 @@ } @ExposedNew @ExposedMethod - public void Module___init__(PyObject[] args, String[] keywords) {} + public void Attribute___init__(PyObject[] args, String[] keywords) {} public Attribute(PyObject value, PyObject attr, PyObject ctx) { setValue(value); setAttr(attr); setCtx(ctx); } - public Attribute(Token token, exprType value, String attr, expr_contextType - ctx) { + public Attribute(Token token, exprType value, String attr, expr_contextType ctx) { super(token); this.value = value; addChild(value); @@ -91,8 +95,8 @@ this.ctx = ctx; } - public Attribute(Integer ttype, Token token, exprType value, String attr, - expr_contextType ctx) { + public Attribute(Integer ttype, Token token, exprType value, String attr, expr_contextType ctx) + { super(ttype, token); this.value = value; addChild(value); @@ -100,8 +104,7 @@ this.ctx = ctx; } - public Attribute(PythonTree tree, exprType value, String attr, - expr_contextType ctx) { + public Attribute(PythonTree tree, exprType value, String attr, expr_contextType ctx) { super(tree); this.value = value; addChild(value); @@ -143,7 +146,7 @@ } private int lineno = -1; -@ExposedGet(name = "lineno") + @ExposedGet(name = "lineno") public int getLineno() { if (lineno != -1) { return lineno; @@ -151,13 +154,13 @@ return getLine(); } -@ExposedSet(name = "lineno") + @ExposedSet(name = "lineno") public void setLineno(int num) { lineno = num; } private int col_offset = -1; -@ExposedGet(name = "col_offset") + @ExposedGet(name = "col_offset") public int getCol_offset() { if (col_offset != -1) { return col_offset; @@ -165,7 +168,7 @@ return getCharPositionInLine(); } -@ExposedSet(name = "col_offset") + @ExposedSet(name = "col_offset") public void setCol_offset(int num) { col_offset = num; } Modified: branches/astwrite/src/org/python/antlr/ast/AugAssign.java =================================================================== --- branches/astwrite/src/org/python/antlr/ast/AugAssign.java 2008-12-03 14:43:11 UTC (rev 5682) +++ branches/astwrite/src/org/python/antlr/ast/AugAssign.java 2008-12-03 17:59:13 UTC (rev 5683) @@ -2,6 +2,7 @@ package org.python.antlr.ast; import org.antlr.runtime.CommonToken; import org.antlr.runtime.Token; +import org.python.antlr.AST; import org.python.antlr.PythonTree; import org.python.antlr.adapter.AstAdapters; import org.python.core.AstList; @@ -18,7 +19,7 @@ import java.io.IOException; import java.util.ArrayList; -@ExposedType(name = "_ast.AugAssign", base = PyObject.class) +@ExposedType(name = "_ast.AugAssign", base = AST.class) public class AugAssign extends stmtType { public static final PyType TYPE = PyType.fromClass(AugAssign.class); private exprType target; @@ -40,7 +41,7 @@ } @ExposedGet(name = "op") public PyObject getOp() { - return AstAdapters.op2py(op); + return AstAdapters.operator2py(op); } @ExposedSet(name = "op") public void setOp(PyObject op) { @@ -61,11 +62,15 @@ } - private final static String[] fields = new String[] {"target", "op", - "value"}; -@ExposedGet(name = "_fields") - public String[] get_fields() { return fields; } + private final static PyString[] fields = + new PyString[] {new PyString("target"), new PyString("op"), new PyString("value")}; + @ExposedGet(name = "_fields") + public PyString[] get_fields() { return fields; } + private final static PyString[] attributes = new PyString[0]; + @ExposedGet(name = "_attributes") + public PyString[] get_attributes() { return attributes; } + public AugAssign() { this(TYPE); } @@ -74,15 +79,14 @@ } @ExposedNew @ExposedMethod - public void Module___init__(PyObject[] args, String[] keywords) {} + public void AugAssign___init__(PyObject[] args, String[] keywords) {} public AugAssign(PyObject target, PyObject op, PyObject value) { setTarget(target); setOp(op); setValue(value); } - public AugAssign(Token token, exprType target, operatorType op, exprType - value) { + public AugAssign(Token token, exprType target, operatorType op, exprType value) { super(token); this.target = target; addChild(target); @@ -91,8 +95,7 @@ addChild(value); } - public AugAssign(Integer ttype, Token token, exprType target, operatorType - op, exprType value) { + public AugAssign(Integer ttype, Token token, exprType target, operatorType op, exprType value) { super(ttype, token); this.target = target; addChild(target); @@ -101,8 +104,7 @@ addChild(value); } - public AugAssign(PythonTree tree, exprType target, operatorType op, - exprType value) { + public AugAssign(PythonTree tree, exprType target, operatorType op, exprType value) { super(tree); this.target = target; addChild(target); @@ -143,7 +145,7 @@ } private int lineno = -1; -@ExposedGet(name = "lineno") + @ExposedGet(name = "lineno") public int getLineno() { if (lineno != -1) { return lineno; @@ -151,13 +153,13 @@ return getLine(); } -@ExposedSet(name = "lineno") + @ExposedSet(name = "lineno") public void setLineno(int num) { lineno = num; } private int col_offset = -1; -@ExposedGet(name = "col_offset") + @ExposedGet(name = "col_offset") public int getCol_offset() { if (col_offset != -1) { return col_offset; @@ -165,7 +167,7 @@ return getCharPositionInLine(); } -@ExposedSet(name = "col_offset") + @ExposedSet(name = "col_offset") public void setCol_offset(int num) { col_offset = num; } Modified: branches/astwrite/src/org/python/antlr/ast/AugLoad.java =================================================================== --- branches/astwrite/src/org/python/antlr/ast/AugLoad.java 2008-12-03 14:43:11 UTC (rev 5682) +++ branches/astwrite/src/org/python/antlr/ast/AugLoad.java 2008-12-03 17:59:13 UTC (rev 5683) @@ -2,17 +2,47 @@ package org.python.antlr.ast; import org.python.antlr.AST; +import org.python.antlr.PythonTree; +import org.python.core.Py; import org.python.core.PyObject; +import org.python.core.PyString; import org.python.core.PyType; +import org.python.expose.ExposedGet; +import org.python.expose.ExposedMethod; +import org.python.expose.ExposedNew; +import org.python.expose.ExposedSet; import org.python.expose.ExposedType; -@ExposedType(name = "_ast.AugLoad", base = PyObject.class) -public class AugLoad extends AST { +@ExposedType(name = "_ast.AugLoad", base = AST.class) +public class AugLoad extends PythonTree { public static final PyType TYPE = PyType.fromClass(AugLoad.class); - public int asIndex() { - return asIndex(null); + +public AugLoad() { +} + +public AugLoad(PyType subType) { + super(subType); +} + +@ExposedNew +@ExposedMethod +public void AugLoad___init__(PyObject[] args, String[] keywords) {} + + private final static PyString[] fields = new PyString[0]; + @ExposedGet(name = "_fields") + public PyString[] get_fields() { return fields; } + + private final static PyString[] attributes = new PyString[0]; + @ExposedGet(name = "_attributes") + public PyString[] get_attributes() { return attributes; } + + @ExposedMethod + public PyObject __int__() { + return AugLoad___int__(); } - public int asIndex(PyObject error) { - return 4; + + final PyObject AugLoad___int__() { + return Py.newInteger(4); } + } Modified: branches/astwrite/src/org/python/antlr/ast/AugStore.java =================================================================== --- branches/astwrite/src/org/python/antlr/ast/AugStore.java 2008-12-03 14:43:11 UTC (rev 5682) +++ branches/astwrite/src/org/python/antlr/ast/AugStore.java 2008-12-03 17:59:13 UTC (rev 5683) @@ -2,17 +2,47 @@ package org.python.antlr.ast; import org.python.antlr.AST; +import org.python.antlr.PythonTree; +import org.python.core.Py; import org.python.core.PyObject; +import org.python.core.PyString; import org.python.core.PyType; +import org.python.expose.ExposedGet; +import org.python.expose.ExposedMethod; +import org.python.expose.ExposedNew; +import org.python.expose.ExposedSet; import org.python.expose.ExposedType; -@ExposedType(name = "_ast.AugStore", base = PyObject.class) -public class AugStore extends AST { +@ExposedType(name = "_ast.AugStore", base = AST.class) +public class AugStore extends PythonTree { public static final PyType TYPE = PyType.fromClass(AugStore.class); - public int asIndex() { - return asIndex(null); + +public AugStore() { +} + +public AugStore(PyType subType) { + super(subType); +} + +@ExposedNew +@ExposedMethod +public void AugStore___init__(PyObject[] args, String[] keywords) {} + + private final static PyString[] fields = new PyString[0]; + @ExposedGet(name = "_fields") + public PyString[] get_fields() { return fields; } + + private final static PyString[] attributes = new PyString[0]; + @ExposedGet(name = "_attributes") + public PyString[] get_attributes() { return attributes; } + + @ExposedMethod + public PyObject __int__() { + return AugStore___int__(); } - public int asIndex(PyObject error) { - return 5; + + final PyObject AugStore___int__() { + return Py.newInteger(5); } + } Modified: branches/astwrite/src/org/python/antlr/... [truncated message content] |