From: <fwi...@us...> - 2008-05-26 17:26:18
|
Revision: 4456 http://jython.svn.sourceforge.net/jython/?rev=4456&view=rev Author: fwierzbicki Date: 2008-05-26 10:26:16 -0700 (Mon, 26 May 2008) Log Message: ----------- Better exception handling in antlr parser (fail fast in PythonWalker, and new class antlr/ParseException. Modified Paths: -------------- trunk/jython/grammar/PythonWalker.g trunk/jython/src/org/python/core/antlr.java Added Paths: ----------- trunk/jython/src/org/python/antlr/ParseException.java Modified: trunk/jython/grammar/PythonWalker.g =================================================================== --- trunk/jython/grammar/PythonWalker.g 2008-05-26 13:57:54 UTC (rev 4455) +++ trunk/jython/grammar/PythonWalker.g 2008-05-26 17:26:16 UTC (rev 4456) @@ -93,6 +93,15 @@ } } + protected void mismatch(IntStream input, int ttype, BitSet follow) throws RecognitionException { + throw new MismatchedTokenException(ttype, input); + } + + protected void mismatch(IntStream input, RecognitionException e, BitSet follow) throws RecognitionException { + throw e; + } + + String name = "Test"; //XXX: Not sure I need any below... @@ -350,6 +359,13 @@ } } +@rulecatch { +catch (RecognitionException e) { + throw e; +} +} + + expression returns [modType mod] : ^(Expression test[expr_contextType.Load]) { $mod = makeExpression($Expression, $test.etype); } ; Added: trunk/jython/src/org/python/antlr/ParseException.java =================================================================== --- trunk/jython/src/org/python/antlr/ParseException.java (rev 0) +++ trunk/jython/src/org/python/antlr/ParseException.java 2008-05-26 17:26:16 UTC (rev 4456) @@ -0,0 +1,21 @@ +package org.python.antlr; + +import org.python.antlr.PythonTree; + +public class ParseException extends Exception { + + public PythonTree currentToken = null; + + public ParseException() { + super(); + } + + public ParseException(String message) { + super(message); + } + + public ParseException(String message, PythonTree node) { + super(message); + this.currentToken = node; + } +} Modified: trunk/jython/src/org/python/core/antlr.java =================================================================== --- trunk/jython/src/org/python/core/antlr.java 2008-05-26 13:57:54 UTC (rev 4455) +++ trunk/jython/src/org/python/core/antlr.java 2008-05-26 17:26:16 UTC (rev 4456) @@ -15,8 +15,10 @@ import org.antlr.runtime.*; import org.python.antlr.ExpressionParser; import org.python.antlr.LeadingSpaceSkippingStream; +import org.python.antlr.ParseException; import org.python.antlr.PythonGrammar; import org.python.antlr.PythonParser; +import org.python.antlr.PythonTree; import org.python.core.util.StringUtil; import org.python.antlr.IParserHost; import org.python.antlr.PythonTree; @@ -58,7 +60,20 @@ } } - if (t instanceof RecognitionException) { + if (t instanceof ParseException) { + ParseException e = (ParseException)t; + PythonTree tok = e.currentToken; + int line=0; + int col=0; + if (tok != null) { + line = tok.getLine(); + col = tok.getCharPositionInLine(); + } + String text=getLine(reader, line); + return new PySyntaxError(e.getMessage(), line, col, + text, filename); + } + else if (t instanceof RecognitionException) { RecognitionException e = (RecognitionException)t; String msg = e.getMessage(); String tokenNames[] = PythonParser.tokenNames; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |