From: <fwi...@us...> - 2008-10-21 20:02:06
|
Revision: 5499 http://jython.svn.sourceforge.net/jython/?rev=5499&view=rev Author: fwierzbicki Date: 2008-10-21 20:02:03 +0000 (Tue, 21 Oct 2008) Log Message: ----------- Allow error handling to be customized in the case of token mismatch calls during parsing. Modified Paths: -------------- trunk/jython/grammar/Python.g trunk/jython/src/org/python/antlr/ErrorHandler.java trunk/jython/src/org/python/antlr/FailFastHandler.java trunk/jython/src/org/python/antlr/ListErrorHandler.java Modified: trunk/jython/grammar/Python.g =================================================================== --- trunk/jython/grammar/Python.g 2008-10-21 18:44:08 UTC (rev 5498) +++ trunk/jython/grammar/Python.g 2008-10-21 20:02:03 UTC (rev 5499) @@ -165,21 +165,19 @@ } protected void mismatch(IntStream input, int ttype, BitSet follow) throws RecognitionException { - if (errorHandler.isRecoverable()) { + if (errorHandler.mismatch(this, input, ttype, follow)) { super.mismatch(input, ttype, follow); - } else { - throw new MismatchedTokenException(ttype, input); } } protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow) - throws RecognitionException - { - if (errorHandler.isRecoverable()) { - return super.recoverFromMismatchedToken(input, ttype, follow); + throws RecognitionException { + + Object o = errorHandler.recoverFromMismatchedToken(this, input, ttype, follow); + if (o != null) { + return o; } - mismatch(input, ttype, follow); - return null; + return super.recoverFromMismatchedToken(input, ttype, follow); } } Modified: trunk/jython/src/org/python/antlr/ErrorHandler.java =================================================================== --- trunk/jython/src/org/python/antlr/ErrorHandler.java 2008-10-21 18:44:08 UTC (rev 5498) +++ trunk/jython/src/org/python/antlr/ErrorHandler.java 2008-10-21 20:02:03 UTC (rev 5499) @@ -1,6 +1,7 @@ package org.python.antlr; import org.antlr.runtime.BaseRecognizer; +import org.antlr.runtime.BitSet; import org.antlr.runtime.IntStream; import org.antlr.runtime.Lexer; import org.antlr.runtime.RecognitionException; @@ -13,7 +14,19 @@ void reportError(BaseRecognizer br, RecognitionException re); void recover(BaseRecognizer br, IntStream input, RecognitionException re); void recover(Lexer lex, RecognitionException re); - boolean isRecoverable(); + + /** + * @return True if the caller should handle the mismatch + */ + boolean mismatch(BaseRecognizer br, IntStream input, int ttype, BitSet follow) + throws RecognitionException; + + /** + * @return null if the caller should handle the mismatch + */ + Object recoverFromMismatchedToken(BaseRecognizer br, IntStream input, int ttype, BitSet follow) + throws RecognitionException; + //exprType, modType, sliceType, stmtType exprType errorExpr(PythonTree t); modType errorMod(PythonTree t); Modified: trunk/jython/src/org/python/antlr/FailFastHandler.java =================================================================== --- trunk/jython/src/org/python/antlr/FailFastHandler.java 2008-10-21 18:44:08 UTC (rev 5498) +++ trunk/jython/src/org/python/antlr/FailFastHandler.java 2008-10-21 20:02:03 UTC (rev 5499) @@ -1,8 +1,10 @@ package org.python.antlr; import org.antlr.runtime.BaseRecognizer; +import org.antlr.runtime.BitSet; import org.antlr.runtime.IntStream; import org.antlr.runtime.Lexer; +import org.antlr.runtime.MismatchedTokenException; import org.antlr.runtime.RecognitionException; import org.python.antlr.ast.ErrorMod; import org.python.antlr.ast.exprType; @@ -27,10 +29,17 @@ throw new ParseException(message(br,re), re); } - public boolean isRecoverable() { - return false; + public boolean mismatch(BaseRecognizer br, IntStream input, int ttype, BitSet follow) + throws RecognitionException { + + throw new MismatchedTokenException(ttype, input); } + public Object recoverFromMismatchedToken(BaseRecognizer br, IntStream input, int ttype, + BitSet follow) throws RecognitionException { + throw new MismatchedTokenException(ttype, input); + } + public exprType errorExpr(PythonTree t) { throw new ParseException("Bad Expr Node", t); } Modified: trunk/jython/src/org/python/antlr/ListErrorHandler.java =================================================================== --- trunk/jython/src/org/python/antlr/ListErrorHandler.java 2008-10-21 18:44:08 UTC (rev 5498) +++ trunk/jython/src/org/python/antlr/ListErrorHandler.java 2008-10-21 20:02:03 UTC (rev 5499) @@ -1,6 +1,7 @@ package org.python.antlr; import org.antlr.runtime.BaseRecognizer; +import org.antlr.runtime.BitSet; import org.antlr.runtime.IntStream; import org.antlr.runtime.Lexer; import org.antlr.runtime.RecognitionException; @@ -27,10 +28,14 @@ br.recover(input, re); } - public boolean isRecoverable() { + public boolean mismatch(BaseRecognizer br, IntStream input, int ttype, BitSet follow) { return true; } + public Object recoverFromMismatchedToken(BaseRecognizer br, IntStream input, int ttype, BitSet follow) { + return null; + } + public exprType errorExpr(PythonTree t) { return new ErrorExpr(t); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |