From: <fwi...@us...> - 2008-08-03 14:42:47
|
Revision: 5067 http://jython.svn.sourceforge.net/jython/?rev=5067&view=rev Author: fwierzbicki Date: 2008-08-03 14:42:44 +0000 (Sun, 03 Aug 2008) Log Message: ----------- Passed filename along to parsers and PythonTokenSource. created a PyIndentationError to allow me derive IndentationErrors from ParseException. Added a "type" to ParseException to recognize when an IndentationError is needed. Changed test_traceback slightly because I believe Antlr comes up with a better placement for the carot vs CPython (at the indent and not end of line for indent errors. Modified Paths: -------------- branches/asm/Lib/test/test_traceback.py branches/asm/src/org/python/antlr/BaseParser.java branches/asm/src/org/python/antlr/ExpressionParser.java branches/asm/src/org/python/antlr/InteractiveParser.java branches/asm/src/org/python/antlr/ModuleParser.java branches/asm/src/org/python/antlr/ParseException.java branches/asm/src/org/python/antlr/PythonTokenSource.java branches/asm/src/org/python/core/ParserFacade.java branches/asm/src/org/python/core/PySyntaxError.java branches/asm/tests/java/org/python/antlr/PythonTreeTester.java Added Paths: ----------- branches/asm/src/org/python/core/PyIndentationError.java Modified: branches/asm/Lib/test/test_traceback.py =================================================================== --- branches/asm/Lib/test/test_traceback.py 2008-08-03 09:50:01 UTC (rev 5066) +++ branches/asm/Lib/test/test_traceback.py 2008-08-03 14:42:44 UTC (rev 5067) @@ -50,7 +50,9 @@ self.assert_(len(err) == 4) self.assert_(err[1].strip() == "print 2") self.assert_("^" in err[2]) - self.assert_(err[1].find("2") == err[2].find("^")) + # Antlr thinks the error is at the indentation, while CPython points at + # the end of the line. I am agreeing with Antlr over CPython here. + self.assert_(err[1].find("p") -1 == err[2].find("^")) def test_bug737473(self): import sys, os, tempfile, time Modified: branches/asm/src/org/python/antlr/BaseParser.java =================================================================== --- branches/asm/src/org/python/antlr/BaseParser.java 2008-08-03 09:50:01 UTC (rev 5066) +++ branches/asm/src/org/python/antlr/BaseParser.java 2008-08-03 14:42:44 UTC (rev 5067) @@ -28,6 +28,7 @@ protected CharStream charStream; protected boolean partial; + protected String filename; protected ErrorHandler errorHandler = new FailFastHandler(); public void setAntlrErrorHandler(ErrorHandler eh) { Modified: branches/asm/src/org/python/antlr/ExpressionParser.java =================================================================== --- branches/asm/src/org/python/antlr/ExpressionParser.java 2008-08-03 09:50:01 UTC (rev 5066) +++ branches/asm/src/org/python/antlr/ExpressionParser.java 2008-08-03 14:42:44 UTC (rev 5067) @@ -16,8 +16,9 @@ public class ExpressionParser extends BaseParser { - public ExpressionParser(CharStream cs) { + public ExpressionParser(CharStream cs, String filename) { this.charStream = cs; + this.filename = filename; } public modType parse() { @@ -26,7 +27,7 @@ lexer.setErrorHandler(errorHandler); CommonTokenStream tokens = new CommonTokenStream(lexer); tokens.discardOffChannelTokens(true); - PythonTokenSource indentedSource = new PythonTokenSource(tokens); + PythonTokenSource indentedSource = new PythonTokenSource(tokens, filename); tokens = new CommonTokenStream(indentedSource); PythonParser parser = new PythonParser(tokens); parser.setErrorHandler(errorHandler); Modified: branches/asm/src/org/python/antlr/InteractiveParser.java =================================================================== --- branches/asm/src/org/python/antlr/InteractiveParser.java 2008-08-03 09:50:01 UTC (rev 5066) +++ branches/asm/src/org/python/antlr/InteractiveParser.java 2008-08-03 14:42:44 UTC (rev 5067) @@ -32,8 +32,9 @@ } } - public InteractiveParser(BufferedReader br) { + public InteractiveParser(BufferedReader br, String filename) { this.bufreader = br; + this.filename = filename; } public modType parse() throws IOException { @@ -44,7 +45,7 @@ lexer.inSingle = true; CommonTokenStream tokens = new CommonTokenStream(lexer); tokens.discardOffChannelTokens(true); - PythonTokenSource indentedSource = new PythonTokenSource(tokens); + PythonTokenSource indentedSource = new PythonTokenSource(tokens, filename); tokens = new CommonTokenStream(indentedSource); PythonParser parser = new PythonParser(tokens); parser.setErrorHandler(errorHandler); Modified: branches/asm/src/org/python/antlr/ModuleParser.java =================================================================== --- branches/asm/src/org/python/antlr/ModuleParser.java 2008-08-03 09:50:01 UTC (rev 5066) +++ branches/asm/src/org/python/antlr/ModuleParser.java 2008-08-03 14:42:44 UTC (rev 5067) @@ -15,13 +15,14 @@ import org.python.antlr.ast.stmtType; public class ModuleParser extends BaseParser { - public ModuleParser(CharStream cs) { - this(cs, false); + public ModuleParser(CharStream cs, String filename) { + this(cs, filename, false); } - public ModuleParser(CharStream cs, boolean partial) { + public ModuleParser(CharStream cs, String filename, boolean partial) { this.partial = partial; this.charStream = cs; + this.filename = filename; } public modType file_input() { @@ -30,7 +31,7 @@ lexer.setErrorHandler(errorHandler); CommonTokenStream tokens = new CommonTokenStream(lexer); tokens.discardOffChannelTokens(true); - PythonTokenSource indentedSource = new PythonTokenSource(tokens); + PythonTokenSource indentedSource = new PythonTokenSource(tokens, filename); tokens = new CommonTokenStream(indentedSource); PythonParser parser = new PythonParser(tokens); parser.setErrorHandler(errorHandler); Modified: branches/asm/src/org/python/antlr/ParseException.java =================================================================== --- branches/asm/src/org/python/antlr/ParseException.java 2008-08-03 09:50:01 UTC (rev 5066) +++ branches/asm/src/org/python/antlr/ParseException.java 2008-08-03 14:42:44 UTC (rev 5067) @@ -1,6 +1,9 @@ package org.python.antlr; import org.python.antlr.PythonTree; +import org.python.core.Py; +import org.python.core.PyObject; + import org.antlr.runtime.*; public class ParseException extends RuntimeException { @@ -13,28 +16,49 @@ public int charPositionInLine; public boolean approximateLineInfo; + private PyObject type = Py.SyntaxError; + public ParseException() { super(); } - public ParseException(String message) { + public ParseException(String message, int lin, int charPos) { super(message); + this.line = lin; + this.charPositionInLine = charPos; } - public ParseException(String message, PythonTree node) { - super(message); + public ParseException(String message) { + this(message, 0, 0); } + /** + * n must not be null to use this constructor + */ + public ParseException(String message, PythonTree n) { + this(message, n.getLine(), n.getCharPositionInLine()); + this.node = n; + this.token = n.token; + } + public ParseException(String message, RecognitionException r) { super(message); - input = r.input; - index = r.index; - token = r.token; - node = r.node; - c = r.c; - line = r.line; - charPositionInLine = r.charPositionInLine; - approximateLineInfo = r.approximateLineInfo; + this.input = r.input; + this.index = r.index; + this.token = r.token; + this.node = r.node; + this.c = r.c; + this.line = r.line; + this.charPositionInLine = r.charPositionInLine; + this.approximateLineInfo = r.approximateLineInfo; } + public void setType(PyObject t) { + this.type = t; + } + + public PyObject getType() { + return this.type; + } + } Modified: branches/asm/src/org/python/antlr/PythonTokenSource.java =================================================================== --- branches/asm/src/org/python/antlr/PythonTokenSource.java 2008-08-03 09:50:01 UTC (rev 5066) +++ branches/asm/src/org/python/antlr/PythonTokenSource.java 2008-08-03 14:42:44 UTC (rev 5067) @@ -1,5 +1,7 @@ package org.python.antlr; +import org.python.core.Py; + /* [The "BSD licence"] Copyright (c) 2004 Terence Parr and Loring Craymer @@ -88,11 +90,14 @@ int lastTokenAddedIndex = -1; + String filename; + public PythonTokenSource(PythonLexer lexer) { } - public PythonTokenSource(CommonTokenStream stream) { + public PythonTokenSource(CommonTokenStream stream, String filename) { this.stream = stream; + this.filename = filename; // "state" of indent level is FIRST_CHAR_POSITION push(FIRST_CHAR_POSITION); } @@ -265,9 +270,8 @@ if (i == -1 || i == -2) { return FIRST_CHAR_POSITION; } - ParseException p = new ParseException("unindent does not match any outer indentation level"); - p.line = t.getLine(); - p.charPositionInLine = t.getCharPositionInLine(); + ParseException p = new ParseException("unindent does not match any outer indentation level", t.getLine(), t.getCharPositionInLine()); + p.setType(Py.IndentationError); throw p; } @@ -282,7 +286,7 @@ //FIXME: needed this for the Antlr 3.1b interface change. public String getSourceName() { - return "XXX-need-real-name.py"; + return filename; } } Modified: branches/asm/src/org/python/core/ParserFacade.java =================================================================== --- branches/asm/src/org/python/core/ParserFacade.java 2008-08-03 09:50:01 UTC (rev 5066) +++ branches/asm/src/org/python/core/ParserFacade.java 2008-08-03 14:42:44 UTC (rev 5067) @@ -46,15 +46,15 @@ if (reader == null) { return ""; } + String text = null; try { - String text=null; for(int i=0; i < line; i++) { text = reader.readLine(); } return text; } catch (IOException ioe) { - return null; } + return text; } // if reader != null, reset it @@ -80,12 +80,9 @@ } String text=getLine(reader, line); String msg = e.getMessage(); - if (msg == null) { - msg = "XXX: missing msg"; + if (e.getType() == Py.IndentationError) { + return new PyIndentationError(msg, line, col, text, filename); } - if (text == null) { - text = "XXX: missing text"; - } return new PySyntaxError(msg, line, col, text, filename); } else return Py.JavaError(t); @@ -108,16 +105,16 @@ if (kind.equals("eval")) { bufreader = prepBufreader(new LeadingSpaceSkippingStream(bstream), cflags, filename); CharStream cs = new NoCloseReaderStream(bufreader); - ExpressionParser e = new ExpressionParser(cs); + ExpressionParser e = new ExpressionParser(cs, filename); node = e.parse(); } else if (kind.equals("single")) { bufreader = prepBufreader(bstream, cflags, filename); - InteractiveParser i = new InteractiveParser(bufreader); + InteractiveParser i = new InteractiveParser(bufreader, filename); node = i.parse(); } else if (kind.equals("exec")) { bufreader = prepBufreader(bstream, cflags, filename); CharStream cs = new NoCloseReaderStream(bufreader); - ModuleParser g = new ModuleParser(cs); + ModuleParser g = new ModuleParser(cs, filename); node = g.file_input(); } else { throw Py.ValueError("parse kind must be eval, exec, " + "or single"); @@ -150,7 +147,7 @@ StringUtil.toBytes(string)); BufferedInputStream bstream = bstream = new BufferedInputStream(bi); bufreader = prepBufreader(bstream, cflags, filename); - InteractiveParser i = new InteractiveParser(bufreader); + InteractiveParser i = new InteractiveParser(bufreader, filename); node = i.parse(); } else { throw Py.ValueError("parse kind must be eval, exec, " + "or single"); Added: branches/asm/src/org/python/core/PyIndentationError.java =================================================================== --- branches/asm/src/org/python/core/PyIndentationError.java (rev 0) +++ branches/asm/src/org/python/core/PyIndentationError.java 2008-08-03 14:42:44 UTC (rev 5067) @@ -0,0 +1,33 @@ +// Copyright (c) Corporation for National Research Initiatives +package org.python.core; + +/** + * A convenience class for creating Indentation errors. Note that the + * syntax error is still taken from Py.IndentationError. + * <p> + * Generally subclassing from PyException is not the right way + * of creating new exception classes. + */ + +public class PyIndentationError extends PyException { + int lineno, column; + String text; + String filename; + + public PyIndentationError(String s, int line, int column, String text, + String filename) + { + super(Py.IndentationError); + PyObject[] tmp = new PyObject[] { + new PyString(filename), new PyInteger(line), + new PyInteger(column), new PyString(text) + }; + + this.value = new PyTuple(new PyString(s), new PyTuple(tmp)); + + this.lineno = line; + this.column = column; + this.text = text; + this.filename = filename; + } +} Modified: branches/asm/src/org/python/core/PySyntaxError.java =================================================================== --- branches/asm/src/org/python/core/PySyntaxError.java 2008-08-03 09:50:01 UTC (rev 5066) +++ branches/asm/src/org/python/core/PySyntaxError.java 2008-08-03 14:42:44 UTC (rev 5067) @@ -2,7 +2,7 @@ package org.python.core; /** - * A convience class for creating Syntax errors. Note that the + * A convenience class for creating Syntax errors. Note that the * syntax error is still taken from Py.SyntaxError. * <p> * Generally subclassing from PyException is not the right way @@ -10,7 +10,8 @@ */ public class PySyntaxError extends PyException { - int lineno, column; + int lineno; + int column; String text; String filename; Modified: branches/asm/tests/java/org/python/antlr/PythonTreeTester.java =================================================================== --- branches/asm/tests/java/org/python/antlr/PythonTreeTester.java 2008-08-03 09:50:01 UTC (rev 5066) +++ branches/asm/tests/java/org/python/antlr/PythonTreeTester.java 2008-08-03 14:42:44 UTC (rev 5067) @@ -27,13 +27,17 @@ public PythonTree parse(String[] args) throws Exception { PythonTree result = null; + //ErrorHandler eh = new ListErrorHandler(); + ErrorHandler eh = new FailFastHandler(); CharStream input = new ANTLRFileStream(args[0]); PythonLexer lexer = new ModuleParser.PyLexer(input); + lexer.setErrorHandler(eh); CommonTokenStream tokens = new CommonTokenStream(lexer); tokens.discardOffChannelTokens(true); - PythonTokenSource indentedSource = new PythonTokenSource(tokens); + PythonTokenSource indentedSource = new PythonTokenSource(tokens, args[0]); tokens = new CommonTokenStream(indentedSource); PythonParser parser = new PythonParser(tokens); + parser.setErrorHandler(eh); parser.setTreeAdaptor(new PythonTreeAdaptor()); Tree r = null; switch (_block) { @@ -54,6 +58,7 @@ CommonTreeNodeStream nodes = new CommonTreeNodeStream(r); nodes.setTokenStream(tokens); PythonWalker walker = new PythonWalker(nodes); + walker.setErrorHandler(eh); switch (_block) { case MODULE : result = walker.module(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |