Merge branch 'bail-error-propagation' of github.com:sharwell/antlr4 into main

This commit is contained in:
Terence Parr 2012-09-04 19:00:54 -07:00
commit 38e0ad238d
1 changed files with 16 additions and 6 deletions

View File

@ -30,16 +30,21 @@
package org.antlr.v4.runtime;
/** Bail out of parser at first syntax error. Do this to use it:
* myparser.setErrorHandler(new BailErrorStrategy<Token>());
* <p/>
* {@code myparser.setErrorHandler(new BailErrorStrategy());}
*/
public class BailErrorStrategy extends DefaultErrorStrategy {
/** Instead of recovering from exception e, Re-throw wrote it wrapped
* in a generic RuntimeException so it is not caught by the
* rule function catches. Exception e is the "cause" of the
* RuntimeException.
/** Instead of recovering from exception {@code e}, re-throw it wrapped
* in a generic {@link RuntimeException} so it is not caught by the
* rule function catches. Use {@link RuntimeException#getCause()} to get the
* original {@link RecognitionException}.
*/
@Override
public void recover(Parser recognizer, RecognitionException e) {
for (ParserRuleContext<?> context = recognizer.getContext(); context != null; context = context.getParent()) {
context.exception = e;
}
throw new RuntimeException(e);
}
@ -50,7 +55,12 @@ public class BailErrorStrategy extends DefaultErrorStrategy {
public Token recoverInline(Parser recognizer)
throws RecognitionException
{
throw new RuntimeException(new InputMismatchException(recognizer));
InputMismatchException e = new InputMismatchException(recognizer);
for (ParserRuleContext<?> context = recognizer.getContext(); context != null; context = context.getParent()) {
context.exception = e;
}
throw new RuntimeException(e);
}
/** Make sure we don't attempt to recover from problems in subrules. */