From: <fwi...@us...> - 2009-08-04 19:54:23
|
Revision: 6629 http://jython.svn.sourceforge.net/jython/?rev=6629&view=rev Author: fwierzbicki Date: 2009-08-04 19:54:15 +0000 (Tue, 04 Aug 2009) Log Message: ----------- cleanup of antlr/adapter/*Adapter classes in prep to fix http://bugs.jython.org/issue1415. Checked in change to asdl_antlr.py but haven't applied it yet (since this generates lots of changes in the check in that obscures the real changes). Next checkin will apply the codegen. Modified Paths: -------------- trunk/jython/ast/asdl_antlr.py trunk/jython/src/org/python/antlr/adapter/AliasAdapter.java trunk/jython/src/org/python/antlr/adapter/AstAdapters.java trunk/jython/src/org/python/antlr/adapter/CmpopAdapter.java trunk/jython/src/org/python/antlr/adapter/ComprehensionAdapter.java trunk/jython/src/org/python/antlr/adapter/ExcepthandlerAdapter.java trunk/jython/src/org/python/antlr/adapter/ExprAdapter.java trunk/jython/src/org/python/antlr/adapter/IdentifierAdapter.java trunk/jython/src/org/python/antlr/adapter/KeywordAdapter.java trunk/jython/src/org/python/antlr/adapter/SliceAdapter.java trunk/jython/src/org/python/antlr/adapter/StmtAdapter.java Modified: trunk/jython/ast/asdl_antlr.py =================================================================== --- trunk/jython/ast/asdl_antlr.py 2009-08-04 00:06:16 UTC (rev 6628) +++ trunk/jython/ast/asdl_antlr.py 2009-08-04 19:54:15 UTC (rev 6629) @@ -481,10 +481,10 @@ self.emit("@ExposedMethod", depth) self.emit("public void %s___init__(PyObject[] args, String[] keywords) {" % clsname, depth) self.emit('ArgParser ap = new ArgParser("%s", args, keywords, new String[]' % clsname, depth + 1) - self.emit('{%s}, %s);' % (fpargs, len(fields)), depth + 2) + self.emit('{%s}, 0);' % fpargs, depth + 2) i = 0 for f in fields: - self.emit("set%s(ap.getPyObject(%s));" % (self.processFieldName(f.name), + self.emit("set%s(ap.getPyObject(%s, Py.None));" % (self.processFieldName(f.name), str(i)), depth+1) i += 1 if str(name) in ('stmt', 'expr', 'excepthandler'): Modified: trunk/jython/src/org/python/antlr/adapter/AliasAdapter.java =================================================================== --- trunk/jython/src/org/python/antlr/adapter/AliasAdapter.java 2009-08-04 00:06:16 UTC (rev 6628) +++ trunk/jython/src/org/python/antlr/adapter/AliasAdapter.java 2009-08-04 19:54:15 UTC (rev 6629) @@ -10,24 +10,25 @@ public class AliasAdapter implements AstAdapter { public Object py2ast(PyObject o) { - if (o == null) { - return o; - } if (o instanceof alias) { return o; } - //FIXME: investigate the right exception - throw Py.TypeError("Can't convert " + o.getClass().getName() + " to alias node"); + return null; } public PyObject ast2py(Object o) { + if (o == null) { + return Py.None; + } return (PyObject)o; } public List iter2ast(PyObject iter) { List<alias> aliases = new ArrayList<alias>(); - for(Object o : (Iterable)iter) { - aliases.add((alias)py2ast((PyObject)o)); + if (iter != Py.None) { + for(Object o : (Iterable)iter) { + aliases.add((alias)py2ast((PyObject)o)); + } } return aliases; } Modified: trunk/jython/src/org/python/antlr/adapter/AstAdapters.java =================================================================== --- trunk/jython/src/org/python/antlr/adapter/AstAdapters.java 2009-08-04 00:06:16 UTC (rev 6628) +++ trunk/jython/src/org/python/antlr/adapter/AstAdapters.java 2009-08-04 19:54:15 UTC (rev 6629) @@ -60,12 +60,11 @@ return (expr)exprAdapter.py2ast(o); } - public static int py2int(Object o) { + public static Integer py2int(Object o) { if (o == null || o instanceof Integer) { return (Integer)o; } - //FIXME: investigate the right exception - throw Py.TypeError("Can't convert " + o.getClass().getName() + " to int node"); + return null; } public static String py2identifier(PyObject o) { @@ -75,7 +74,8 @@ public static expr_contextType py2expr_context(Object o) { if (o == null || o instanceof expr_contextType) { return (expr_contextType)o; - } else if (o instanceof PyObject) { + } + if (o instanceof PyObject && o != Py.None) { switch (((PyObject)o).asInt()) { case 1: return expr_contextType.Load; @@ -89,10 +89,11 @@ return expr_contextType.AugStore; case 6: return expr_contextType.Param; + default: + return expr_contextType.UNDEFINED; } } - //FIXME: investigate the right exception - throw Py.TypeError("Can't convert " + o.getClass().getName() + " to expr_context node"); + return expr_contextType.UNDEFINED; } public static slice py2slice(PyObject o) { @@ -105,17 +106,16 @@ //XXX: Unnecessary but needs to be fixed in the code generation of asdl_antlr.py public static Object py2string(Object o) { - if (o == null || o instanceof PyString) { + if (o instanceof PyString) { return o; } - //FIXME: investigate the right exception - throw Py.TypeError("Can't convert " + o.getClass().getName() + " to string node"); + return null; } public static operatorType py2operator(Object o) { if (o == null || o instanceof operatorType) { return (operatorType)o; - } else if (o instanceof PyObject) { + } else if (o instanceof PyObject && o != Py.None) { switch (((PyObject)o).asInt()) { case 1: return operatorType.Add; @@ -141,10 +141,11 @@ return operatorType.BitAnd; case 12: return operatorType.FloorDiv; + default: + return operatorType.UNDEFINED; } } - //FIXME: investigate the right exception - throw Py.TypeError("Can't convert " + o.getClass().getName() + " to operator node"); + return operatorType.UNDEFINED; } public static PyObject operator2py(operatorType o) { @@ -173,8 +174,9 @@ return new BitAnd(); case FloorDiv: return new FloorDiv(); + default: + return Py.None; } - return Py.None; } public static PyObject boolop2py(boolopType o) { @@ -183,8 +185,9 @@ return new And(); case Or: return new Or(); + default: + return Py.None; } - return Py.None; } public static PyObject cmpop2py(cmpopType o) { @@ -209,8 +212,9 @@ return new In(); case NotIn: return new NotIn(); + default: + return Py.None; } - return Py.None; } public static PyObject unaryop2py(unaryopType o) { @@ -223,8 +227,9 @@ return new UAdd(); case USub: return new USub(); + default: + return Py.None; } - return Py.None; } @@ -242,31 +247,33 @@ return new AugStore(); case Param: return new Param(); + default: + return Py.None; } - return Py.None; } public static boolopType py2boolop(Object o) { if (o == null || o instanceof boolopType) { return (boolopType)o; - } else if (o instanceof PyObject) { + } + if (o instanceof PyObject && o != Py.None) { switch (((PyObject)o).asInt()) { case 1: return boolopType.And; case 2: return boolopType.Or; + default: + return boolopType.UNDEFINED; } } - //FIXME: investigate the right exception - throw Py.TypeError("Can't convert " + o.getClass().getName() + " to boolop node"); + return boolopType.UNDEFINED; } public static arguments py2arguments(Object o) { - if (o == null || o instanceof arguments) { + if (o instanceof arguments) { return (arguments)o; } - //FIXME: investigate the right exception - throw Py.TypeError("Can't convert " + o.getClass().getName() + " to arguments node"); + return null; } //XXX: clearly this isn't necessary -- need to adjust the code generation. @@ -275,18 +282,17 @@ } public static Boolean py2bool(Object o) { - if (o == null || o instanceof Boolean) { + if (o instanceof Boolean) { return (Boolean)o; } - //FIXME: investigate the right exception - throw Py.TypeError("Can't convert " + o.getClass().getName() + " to Boolean node"); + return null; } public static unaryopType py2unaryop(Object o) { if (o == null || o instanceof unaryopType) { return (unaryopType)o; } - if (o instanceof PyObject) { + if (o instanceof PyObject && o != Py.None) { switch (((PyObject)o).asInt()) { case 1: return unaryopType.Invert; @@ -296,10 +302,10 @@ return unaryopType.UAdd; case 4: return unaryopType.USub; + default: + return unaryopType.UNDEFINED; } } - //FIXME: investigate the right exception - throw Py.TypeError("Can't convert " + o.getClass().getName() + " to unaryop node"); + return unaryopType.UNDEFINED; } - } Modified: trunk/jython/src/org/python/antlr/adapter/CmpopAdapter.java =================================================================== --- trunk/jython/src/org/python/antlr/adapter/CmpopAdapter.java 2009-08-04 00:06:16 UTC (rev 6628) +++ trunk/jython/src/org/python/antlr/adapter/CmpopAdapter.java 2009-08-04 19:54:15 UTC (rev 6629) @@ -20,34 +20,39 @@ public class CmpopAdapter implements AstAdapter { public Object py2ast(PyObject o) { - switch ((o).asInt()) { - case 1: - return cmpopType.Eq; - case 2: - return cmpopType.NotEq; - case 3: - return cmpopType.Lt; - case 4: - return cmpopType.LtE; - case 5: - return cmpopType.Gt; - case 6: - return cmpopType.GtE; - case 7: - return cmpopType.Is; - case 8: - return cmpopType.IsNot; - case 9: - return cmpopType.In; - case 10: - return cmpopType.NotIn; + if (o != Py.None) { + switch ((o).asInt()) { + case 1: + return cmpopType.Eq; + case 2: + return cmpopType.NotEq; + case 3: + return cmpopType.Lt; + case 4: + return cmpopType.LtE; + case 5: + return cmpopType.Gt; + case 6: + return cmpopType.GtE; + case 7: + return cmpopType.Is; + case 8: + return cmpopType.IsNot; + case 9: + return cmpopType.In; + case 10: + return cmpopType.NotIn; + default: + return cmpopType.UNDEFINED; + } } - - //FIXME: investigate the right exception - throw Py.TypeError("Can't convert " + o.getClass().getName() + " to cmpop node"); + return cmpopType.UNDEFINED; } public PyObject ast2py(Object o) { + if (o == null) { + return Py.None; + } switch ((cmpopType)o) { case Eq: return new Eq(); @@ -69,14 +74,17 @@ return new In(); case NotIn: return new NotIn(); + default: + return Py.None; } - return Py.None; } public List iter2ast(PyObject iter) { List<cmpopType> cmpops = new ArrayList<cmpopType>(); - for(Object o : (Iterable)iter) { - cmpops.add((cmpopType)py2ast((PyObject)o)); + if (iter != Py.None) { + for(Object o : (Iterable)iter) { + cmpops.add((cmpopType)py2ast((PyObject)o)); + } } return cmpops; } Modified: trunk/jython/src/org/python/antlr/adapter/ComprehensionAdapter.java =================================================================== --- trunk/jython/src/org/python/antlr/adapter/ComprehensionAdapter.java 2009-08-04 00:06:16 UTC (rev 6628) +++ trunk/jython/src/org/python/antlr/adapter/ComprehensionAdapter.java 2009-08-04 19:54:15 UTC (rev 6629) @@ -10,24 +10,25 @@ public class ComprehensionAdapter implements AstAdapter { public Object py2ast(PyObject o) { - if (o == null) { - return o; - } if (o instanceof comprehension) { return o; } - //FIXME: investigate the right exception - throw Py.TypeError("Can't convert " + o.getClass().getName() + " to comprehension node"); + return null; } public PyObject ast2py(Object o) { + if (o == null) { + return Py.None; + } return (PyObject)o; } public List iter2ast(PyObject iter) { List<comprehension> comprehensions = new ArrayList<comprehension>(); - for(Object o : (Iterable)iter) { - comprehensions.add((comprehension)py2ast((PyObject)o)); + if (iter != Py.None) { + for(Object o : (Iterable)iter) { + comprehensions.add((comprehension)py2ast((PyObject)o)); + } } return comprehensions; } Modified: trunk/jython/src/org/python/antlr/adapter/ExcepthandlerAdapter.java =================================================================== --- trunk/jython/src/org/python/antlr/adapter/ExcepthandlerAdapter.java 2009-08-04 00:06:16 UTC (rev 6628) +++ trunk/jython/src/org/python/antlr/adapter/ExcepthandlerAdapter.java 2009-08-04 19:54:15 UTC (rev 6629) @@ -10,27 +10,26 @@ public class ExcepthandlerAdapter implements AstAdapter { public Object py2ast(PyObject o) { - if (o == null) { - return o; - } if (o instanceof ExceptHandler) { return o; } - - //FIXME: investigate the right exception - throw Py.TypeError("Can't convert " + o.getClass().getName() + " to excepthandler node"); + return null; } public PyObject ast2py(Object o) { + if (o == null) { + return Py.None; + } return (PyObject)o; } public List iter2ast(PyObject iter) { List<ExceptHandler> excepthandlers = new ArrayList<ExceptHandler>(); - for(Object o : (Iterable)iter) { - excepthandlers.add((ExceptHandler)py2ast((PyObject)o)); + if (iter != Py.None) { + for(Object o : (Iterable)iter) { + excepthandlers.add((ExceptHandler)py2ast((PyObject)o)); + } } return excepthandlers; } - } Modified: trunk/jython/src/org/python/antlr/adapter/ExprAdapter.java =================================================================== --- trunk/jython/src/org/python/antlr/adapter/ExprAdapter.java 2009-08-04 00:06:16 UTC (rev 6628) +++ trunk/jython/src/org/python/antlr/adapter/ExprAdapter.java 2009-08-04 19:54:15 UTC (rev 6629) @@ -18,30 +18,32 @@ public class ExprAdapter implements AstAdapter { public Object py2ast(PyObject o) { - if (o == null || o instanceof expr) { + if (o instanceof expr) { return o; - } else if (o instanceof PyInteger || o instanceof PyLong || o instanceof PyFloat || o instanceof PyComplex) { + } + if (o instanceof PyInteger || o instanceof PyLong || o instanceof PyFloat || o instanceof PyComplex) { return new Num(o); - } else if (o instanceof PyString || o instanceof PyUnicode) { + } + if (o instanceof PyString || o instanceof PyUnicode) { return new Str(o); - } else if (o == Py.None) { - return null; } - - //FIXME: investigate the right exception - throw Py.TypeError("Can't convert " + o.getClass().getName() + " to expr node"); + return null; } public PyObject ast2py(Object o) { + if (o == null) { + return Py.None; + } return (PyObject)o; } public List iter2ast(PyObject iter) { List<expr> exprs = new ArrayList<expr>(); - for(Object o : (Iterable)iter) { - exprs.add((expr)py2ast((PyObject)o)); + if (iter != Py.None) { + for(Object o : (Iterable)iter) { + exprs.add((expr)py2ast((PyObject)o)); + } } return exprs; } - } Modified: trunk/jython/src/org/python/antlr/adapter/IdentifierAdapter.java =================================================================== --- trunk/jython/src/org/python/antlr/adapter/IdentifierAdapter.java 2009-08-04 00:06:16 UTC (rev 6628) +++ trunk/jython/src/org/python/antlr/adapter/IdentifierAdapter.java 2009-08-04 19:54:15 UTC (rev 6629) @@ -3,25 +3,33 @@ import java.util.ArrayList; import java.util.List; +import org.python.core.Py; import org.python.core.PyObject; import org.python.core.PyString; public class IdentifierAdapter implements AstAdapter { public Object py2ast(PyObject o) { + if (o == null || o == Py.None) { + return null; + } return o.toString(); } public PyObject ast2py(Object o) { + if (o == null) { + return Py.None; + } return new PyString(o.toString()); } public List iter2ast(PyObject iter) { List<String> identifiers = new ArrayList<String>(); - for(Object o : (Iterable)iter) { - identifiers.add((String)py2ast((PyObject)o)); + if (iter != Py.None) { + for(Object o : (Iterable)iter) { + identifiers.add((String)py2ast((PyObject)o)); + } } return identifiers; } - } Modified: trunk/jython/src/org/python/antlr/adapter/KeywordAdapter.java =================================================================== --- trunk/jython/src/org/python/antlr/adapter/KeywordAdapter.java 2009-08-04 00:06:16 UTC (rev 6628) +++ trunk/jython/src/org/python/antlr/adapter/KeywordAdapter.java 2009-08-04 19:54:15 UTC (rev 6629) @@ -10,25 +10,25 @@ public class KeywordAdapter implements AstAdapter { public Object py2ast(PyObject o) { - if (o == null) { - return o; - } if (o instanceof keyword) { return o; } - - //FIXME: investigate the right exception - throw Py.TypeError("Can't convert " + o.getClass().getName() + " to keyword node"); + return null; } public PyObject ast2py(Object o) { + if (o == null) { + return Py.None; + } return (PyObject)o; } public List iter2ast(PyObject iter) { List<keyword> keywords = new ArrayList<keyword>(); - for(Object o : (Iterable)iter) { - keywords.add((keyword)py2ast((PyObject)o)); + if (iter != Py.None) { + for(Object o : (Iterable)iter) { + keywords.add((keyword)py2ast((PyObject)o)); + } } return keywords; } Modified: trunk/jython/src/org/python/antlr/adapter/SliceAdapter.java =================================================================== --- trunk/jython/src/org/python/antlr/adapter/SliceAdapter.java 2009-08-04 00:06:16 UTC (rev 6628) +++ trunk/jython/src/org/python/antlr/adapter/SliceAdapter.java 2009-08-04 19:54:15 UTC (rev 6629) @@ -10,27 +10,26 @@ public class SliceAdapter implements AstAdapter { public Object py2ast(PyObject o) { - if (o == null) { - return o; - } if (o instanceof slice) { return o; } - - //FIXME: investigate the right exception - throw Py.TypeError("Can't convert " + o.getClass().getName() + " to slice node"); + return null; } public PyObject ast2py(Object o) { + if (o == null) { + return Py.None; + } return (PyObject)o; } public List iter2ast(PyObject iter) { List<slice> slices = new ArrayList<slice>(); - for(Object o : (Iterable)iter) { - slices.add((slice)py2ast((PyObject)o)); + if (iter != Py.None) { + for(Object o : (Iterable)iter) { + slices.add((slice)py2ast((PyObject)o)); + } } return slices; } - } Modified: trunk/jython/src/org/python/antlr/adapter/StmtAdapter.java =================================================================== --- trunk/jython/src/org/python/antlr/adapter/StmtAdapter.java 2009-08-04 00:06:16 UTC (rev 6628) +++ trunk/jython/src/org/python/antlr/adapter/StmtAdapter.java 2009-08-04 19:54:15 UTC (rev 6629) @@ -10,24 +10,25 @@ public class StmtAdapter implements AstAdapter { public Object py2ast(PyObject o) { - if (o == null) { - return o; - } if (o instanceof stmt) { return o; } - //FIXME: investigate the right exception - throw Py.TypeError("Can't convert " + o.getClass().getName() + " to stmt node"); + return null; } public PyObject ast2py(Object o) { + if (o == null) { + return Py.None; + } return (PyObject)o; } public List iter2ast(PyObject iter) { List<stmt> stmts = new ArrayList<stmt>(); - for(Object o : (Iterable)iter) { - stmts.add((stmt)py2ast((PyObject)o)); + if (iter != Py.None) { + for(Object o : (Iterable)iter) { + stmts.add((stmt)py2ast((PyObject)o)); + } } return stmts; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |