ANTLRErrorStrategy.beginErrorCondition and endErrorCondition were implementation details. Replaced them with the API-relevant alternatives reset and reportMatch

This commit is contained in:
Sam Harwell 2013-03-26 15:35:15 -05:00
parent 6c4f4181d9
commit 70452f7e4b
3 changed files with 50 additions and 18 deletions

View File

@ -55,6 +55,12 @@ import org.antlr.v4.runtime.misc.Nullable;
* TODO: what to do about lexers
*/
public interface ANTLRErrorStrategy {
/**
* Reset the error handler state for the specified {@code recognizer}.
* @param recognizer the parser instance
*/
void reset(@NotNull Parser recognizer);
/** When matching elements within alternative, use this method
* to recover. The default implementation uses single token
* insertion and deletion. If you want to change the way ANTLR
@ -112,13 +118,6 @@ public interface ANTLRErrorStrategy {
*/
void sync(@NotNull Parser recognizer);
/** Notify handler that parser has entered an error state. The
* parser currently doesn't call this--the handler itself calls this
* in report error methods. But, for symmetry with endErrorCondition,
* this method is in the interface.
*/
void beginErrorCondition(@NotNull Parser recognizer);
/** Is the parser in the process of recovering from an error? Upon
* a syntax error, the parser enters recovery mode and stays there until
* the next successful match of a token. In this way, we can
@ -127,11 +126,13 @@ public interface ANTLRErrorStrategy {
*/
boolean inErrorRecoveryMode(@NotNull Parser recognizer);
/** Reset the error handler. Call this when the parser
* matches a valid token (indicating no longer in recovery mode)
* and from its own reset method.
/**
* This method is called by when the parser successfully matches an input
* symbol.
*
* @param recognizer the parser instance
*/
void endErrorCondition(@NotNull Parser recognizer);
void reportMatch(@NotNull Parser recognizer);
/** Report any kind of RecognitionException. */
void reportError(@NotNull Parser recognizer,

View File

@ -63,8 +63,24 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
protected IntervalSet lastErrorStates;
/**
* {@inheritDoc}
* <p/>
* The default implementation simply calls {@link #endErrorCondition} to
* ensure that the handler is not in error recovery mode.
*/
@Override
public void beginErrorCondition(Parser recognizer) {
public void reset(Parser recognizer) {
endErrorCondition(recognizer);
}
/**
* This method is called to enter error recovery mode when a recognition
* exception is reported.
*
* @param recognizer the parser instance
*/
protected void beginErrorCondition(@NotNull Parser recognizer) {
errorRecoveryMode = true;
}
@ -73,13 +89,28 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
return errorRecoveryMode;
}
@Override
public void endErrorCondition(Parser recognizer) {
/**
* This method is called to leave error recovery mode after recovering from
* a recognition exception.
*
* @param recognizer
*/
protected void endErrorCondition(@NotNull Parser recognizer) {
errorRecoveryMode = false;
lastErrorStates = null;
lastErrorIndex = -1;
}
/**
* {@inheritDoc}
* <p/>
* The default implementation simply calls {@link #endErrorCondition}.
*/
@Override
public void reportMatch(Parser recognizer) {
endErrorCondition(recognizer);
}
@Override
public void reportError(Parser recognizer,
RecognitionException e)
@ -342,7 +373,7 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
recognizer.consume(); // simply delete extra token
// we want to return the token we're actually matching
Token matchedSymbol = recognizer.getCurrentToken();
endErrorCondition(recognizer); // we know current token is correct
reportMatch(recognizer); // we know current token is correct
return matchedSymbol;
}
return null;

View File

@ -132,7 +132,7 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator> {
/** reset the parser's state */
public void reset() {
if ( getInputStream()!=null ) getInputStream().seek(0);
_errHandler.endErrorCondition(this);
_errHandler.reset(this);
_ctx = null;
_syntaxErrors = 0;
_tracer = null;
@ -149,7 +149,7 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator> {
public Token match(int ttype) throws RecognitionException {
Token t = getCurrentToken();
if ( t.getType()==ttype ) {
_errHandler.endErrorCondition(this);
_errHandler.reportMatch(this);
consume();
}
else {
@ -167,7 +167,7 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator> {
public Token matchWildcard() throws RecognitionException {
Token t = getCurrentToken();
if (t.getType() > 0) {
_errHandler.endErrorCondition(this);
_errHandler.reportMatch(this);
consume();
}
else {