From: <fwi...@us...> - 2008-12-06 02:49:56
|
Revision: 5706 http://jython.svn.sourceforge.net/jython/?rev=5706&view=rev Author: fwierzbicki Date: 2008-12-06 02:49:45 +0000 (Sat, 06 Dec 2008) Log Message: ----------- Added better lineno and col_offset support to a couple of ast nodes, also finished slice support in AstList. Modified Paths: -------------- trunk/jython/ast/asdl_antlr.py trunk/jython/src/org/python/antlr/ast/Break.java trunk/jython/src/org/python/antlr/ast/Continue.java trunk/jython/src/org/python/antlr/ast/Ellipsis.java trunk/jython/src/org/python/antlr/ast/Pass.java trunk/jython/src/org/python/core/AstList.java Modified: trunk/jython/ast/asdl_antlr.py =================================================================== --- trunk/jython/ast/asdl_antlr.py 2008-12-06 02:35:31 UTC (rev 5705) +++ trunk/jython/ast/asdl_antlr.py 2008-12-06 02:49:45 UTC (rev 5706) @@ -470,45 +470,47 @@ self.emit("public %s() {" % (clsname), depth) self.emit("this(TYPE);", depth + 1) self.emit("}", depth) - fnames = ['"%s"' % f.name for f in fields] - if str(name) in ('stmt', 'expr', 'excepthandler'): - fnames.extend(['"lineno"', '"col_offset"']) - fpargs = ", ".join(fnames) - self.emit("@ExposedNew", depth) - 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) - i = 0 - for f in fields: - self.emit("set%s(ap.getPyObject(%s));" % (str(f.name).capitalize(), - str(i)), depth+1) - i += 1 - if str(name) in ('stmt', 'expr', 'excepthandler'): - self.emit("int lin = ap.getInt(%s, -1);" % str(i), depth + 1) - self.emit("if (lin != -1) {", depth + 1) - self.emit("setLineno(lin);", depth + 2) - self.emit("}", depth + 1) - self.emit("", 0) + else: + fnames = [] - self.emit("int col = ap.getInt(%s, -1);" % str(i+1), depth + 1) - self.emit("if (col != -1) {", depth + 1) - self.emit("setLineno(col);", depth + 2) - self.emit("}", depth + 1) - self.emit("", 0) - - self.emit("}", depth) + if str(name) in ('stmt', 'expr', 'excepthandler'): + fnames.extend(['"lineno"', '"col_offset"']) + fpargs = ", ".join(fnames) + self.emit("@ExposedNew", depth) + 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) + i = 0 + for f in fields: + self.emit("set%s(ap.getPyObject(%s));" % (str(f.name).capitalize(), + str(i)), depth+1) + i += 1 + if str(name) in ('stmt', 'expr', 'excepthandler'): + self.emit("int lin = ap.getInt(%s, -1);" % str(i), depth + 1) + self.emit("if (lin != -1) {", depth + 1) + self.emit("setLineno(lin);", depth + 2) + self.emit("}", depth + 1) self.emit("", 0) - fpargs = ", ".join(["PyObject %s" % f.name for f in fields]) - self.emit("public %s(%s) {" % (clsname, fpargs), depth) - for f in fields: - self.emit("set%s(%s);" % (str(f.name).capitalize(), - f.name), depth+1) - self.emit("}", depth) + self.emit("int col = ap.getInt(%s, -1);" % str(i+1), depth + 1) + self.emit("if (col != -1) {", depth + 1) + self.emit("setLineno(col);", depth + 2) + self.emit("}", depth + 1) self.emit("", 0) + self.emit("}", depth) + self.emit("", 0) + + fpargs = ", ".join(["PyObject %s" % f.name for f in fields]) + self.emit("public %s(%s) {" % (clsname, fpargs), depth) + for f in fields: + self.emit("set%s(%s);" % (str(f.name).capitalize(), + f.name), depth+1) + self.emit("}", depth) + self.emit("", 0) + token = asdl.Field('Token', 'token') token.typedef = False fpargs = ", ".join([self.fieldDef(f) for f in [token] + fields]) Modified: trunk/jython/src/org/python/antlr/ast/Break.java =================================================================== --- trunk/jython/src/org/python/antlr/ast/Break.java 2008-12-06 02:35:31 UTC (rev 5705) +++ trunk/jython/src/org/python/antlr/ast/Break.java 2008-12-06 02:49:45 UTC (rev 5706) @@ -41,6 +41,26 @@ public Break(PyType subType) { super(subType); } + @ExposedNew + @ExposedMethod + public void Break___init__(PyObject[] args, String[] keywords) { + ArgParser ap = new ArgParser("Break", args, keywords, new String[] + {"lineno", "col_offset"}, 0); + int lin = ap.getInt(0, -1); + if (lin != -1) { + setLineno(lin); + } + + int col = ap.getInt(1, -1); + if (col != -1) { + setLineno(col); + } + + } + + public Break() { + } + public Break(Token token) { super(token); } Modified: trunk/jython/src/org/python/antlr/ast/Continue.java =================================================================== --- trunk/jython/src/org/python/antlr/ast/Continue.java 2008-12-06 02:35:31 UTC (rev 5705) +++ trunk/jython/src/org/python/antlr/ast/Continue.java 2008-12-06 02:49:45 UTC (rev 5706) @@ -41,6 +41,26 @@ public Continue(PyType subType) { super(subType); } + @ExposedNew + @ExposedMethod + public void Continue___init__(PyObject[] args, String[] keywords) { + ArgParser ap = new ArgParser("Continue", args, keywords, new String[] + {"lineno", "col_offset"}, 0); + int lin = ap.getInt(0, -1); + if (lin != -1) { + setLineno(lin); + } + + int col = ap.getInt(1, -1); + if (col != -1) { + setLineno(col); + } + + } + + public Continue() { + } + public Continue(Token token) { super(token); } Modified: trunk/jython/src/org/python/antlr/ast/Ellipsis.java =================================================================== --- trunk/jython/src/org/python/antlr/ast/Ellipsis.java 2008-12-06 02:35:31 UTC (rev 5705) +++ trunk/jython/src/org/python/antlr/ast/Ellipsis.java 2008-12-06 02:49:45 UTC (rev 5706) @@ -40,6 +40,16 @@ public Ellipsis(PyType subType) { super(subType); } + @ExposedNew + @ExposedMethod + public void Ellipsis___init__(PyObject[] args, String[] keywords) { + ArgParser ap = new ArgParser("Ellipsis", args, keywords, new String[] + {}, 0); + } + + public Ellipsis() { + } + public Ellipsis(Token token) { super(token); } Modified: trunk/jython/src/org/python/antlr/ast/Pass.java =================================================================== --- trunk/jython/src/org/python/antlr/ast/Pass.java 2008-12-06 02:35:31 UTC (rev 5705) +++ trunk/jython/src/org/python/antlr/ast/Pass.java 2008-12-06 02:49:45 UTC (rev 5706) @@ -41,6 +41,26 @@ public Pass(PyType subType) { super(subType); } + @ExposedNew + @ExposedMethod + public void Pass___init__(PyObject[] args, String[] keywords) { + ArgParser ap = new ArgParser("Pass", args, keywords, new String[] + {"lineno", "col_offset"}, 0); + int lin = ap.getInt(0, -1); + if (lin != -1) { + setLineno(lin); + } + + int col = ap.getInt(1, -1); + if (col != -1) { + setLineno(col); + } + + } + + public Pass() { + } + public Pass(Token token) { super(token); } Modified: trunk/jython/src/org/python/core/AstList.java =================================================================== --- trunk/jython/src/org/python/core/AstList.java 2008-12-06 02:35:31 UTC (rev 5705) +++ trunk/jython/src/org/python/core/AstList.java 2008-12-06 02:49:45 UTC (rev 5706) @@ -455,9 +455,62 @@ } protected void setslice(int start, int stop, int step, PyObject value) { - //FIXME + if(stop < start) { + stop = start; + } + if (value instanceof PySequence) { + PySequence sequence = (PySequence) value; + setslicePySequence(start, stop, step, sequence); + } else if (value instanceof List) { + List list = (List)value.__tojava__(List.class); + if(list != null && list != Py.NoConversion) { + setsliceList(start, stop, step, list); + } + } else { + setsliceIterable(start, stop, step, value); + } } - + + protected void setslicePySequence(int start, int stop, int step, PySequence value) { + if (step != 0) { + if(value == this) { + PyList newseq = new PyList(); + PyObject iter = value.__iter__(); + for(PyObject item = null; (item = iter.__iternext__()) != null;) { + newseq.append(item); + } + value = newseq; + } + int n = value.__len__(); + for (int i = 0, j = start; i < n; i++, j += step) { + pyset(j, value.pyget(i)); + } + } + } + + protected void setsliceList(int start, int stop, int step, List value) { + if(step != 1) { + throw Py.TypeError("setslice with java.util.List and step != 1 not supported yet"); + } + int n = value.size(); + for(int i = 0; i < n; i++) { + data.add(i + start, value.get(i)); + } + } + + protected void setsliceIterable(int start, int stop, int step, PyObject value) { + PyObject[] seq; + try { + seq = Py.make_array(value); + } catch (PyException pye) { + if (Py.matchException(pye, Py.TypeError)) { + throw Py.TypeError("can only assign an iterable"); + } + throw pye; + } + setslicePySequence(start, stop, step, new PyList(seq)); + } + public void add(int index, Object element) { data.add(index, element); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |