Simplify FailedPredicateException constructors, encapsulate fields

This commit is contained in:
Sam Harwell 2012-10-22 09:18:29 -05:00
parent ed7d4b1dc1
commit 1ae3c0104c
3 changed files with 45 additions and 24 deletions

View File

@ -229,7 +229,7 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
throws RecognitionException throws RecognitionException
{ {
String ruleName = recognizer.getRuleNames()[recognizer._ctx.getRuleIndex()]; String ruleName = recognizer.getRuleNames()[recognizer._ctx.getRuleIndex()];
String msg = "rule "+ruleName+" "+e.msg; String msg = "rule "+ruleName+" "+e.getMessage();
recognizer.notifyErrorListeners((Token) e.offendingToken, msg, e); recognizer.notifyErrorListeners((Token) e.offendingToken, msg, e);
} }

View File

@ -30,6 +30,7 @@ package org.antlr.v4.runtime;
import org.antlr.v4.runtime.atn.ATNState; import org.antlr.v4.runtime.atn.ATNState;
import org.antlr.v4.runtime.atn.PredicateTransition; import org.antlr.v4.runtime.atn.PredicateTransition;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Nullable; import org.antlr.v4.runtime.misc.Nullable;
/** A semantic predicate failed during validation. Validation of predicates /** A semantic predicate failed during validation. Validation of predicates
@ -38,40 +39,50 @@ import org.antlr.v4.runtime.misc.Nullable;
* prediction. * prediction.
*/ */
public class FailedPredicateException extends RecognitionException { public class FailedPredicateException extends RecognitionException {
public int ruleIndex; private final int ruleIndex;
public int predIndex; private final int predicateIndex;
public String predicate; private final String predicate;
public String msg;
public FailedPredicateException(Parser recognizer) { public FailedPredicateException(@NotNull Parser recognizer) {
this(recognizer, null); this(recognizer, null);
} }
public FailedPredicateException(Parser recognizer, @Nullable String predicate) { public FailedPredicateException(@NotNull Parser recognizer, @Nullable String predicate) {
super(recognizer, recognizer.getInputStream(), recognizer._ctx); this(recognizer, predicate, null);
ATNState s = recognizer.getInterpreter().atn.states.get(recognizer._ctx.s);
PredicateTransition trans = (PredicateTransition)s.transition(0);
ruleIndex = trans.ruleIndex;
predIndex = trans.predIndex;
this.predicate = predicate;
this.msg = String.format("failed predicate: {%s}?", predicate);
Token la = recognizer.getCurrentToken();
this.offendingToken = la;
} }
public FailedPredicateException(Parser recognizer, public FailedPredicateException(@NotNull Parser recognizer,
@Nullable String predicate, @Nullable String predicate,
@Nullable String msg) @Nullable String message)
{ {
super(recognizer, recognizer.getInputStream(), recognizer._ctx); super(formatMessage(predicate, message), recognizer, recognizer.getInputStream(), recognizer._ctx);
ATNState s = recognizer.getInterpreter().atn.states.get(recognizer._ctx.s); ATNState s = recognizer.getInterpreter().atn.states.get(recognizer._ctx.s);
PredicateTransition trans = (PredicateTransition)s.transition(0); PredicateTransition trans = (PredicateTransition)s.transition(0);
ruleIndex = trans.ruleIndex; this.ruleIndex = trans.ruleIndex;
predIndex = trans.predIndex; this.predicateIndex = trans.predIndex;
this.predicate = predicate; this.predicate = predicate;
this.msg = msg; this.offendingToken = recognizer.getCurrentToken();
Token la = recognizer.getCurrentToken();
this.offendingToken = la;
} }
public int getRuleIndex() {
return ruleIndex;
}
public int getPredIndex() {
return predicateIndex;
}
@Nullable
public String getPredicate() {
return predicate;
}
@NotNull
private static String formatMessage(@Nullable String predicate, @Nullable String message) {
if (message != null) {
return message;
}
return String.format("failed predicate: {%s}?", predicate);
}
} }

View File

@ -66,6 +66,16 @@ public class RecognitionException extends RuntimeException {
if ( ctx!=null ) this.offendingState = ctx.s; if ( ctx!=null ) this.offendingState = ctx.s;
} }
public RecognitionException(String message, @Nullable Recognizer<?, ?> recognizer, IntStream input,
@Nullable ParserRuleContext ctx)
{
super(message);
this.recognizer = recognizer;
this.input = input;
this.ctx = ctx;
if ( ctx!=null ) this.offendingState = ctx.s;
}
/** Where was the parser in the ATN when the error occurred? /** Where was the parser in the ATN when the error occurred?
* For No viable alternative exceptions, this is the decision state number. * For No viable alternative exceptions, this is the decision state number.
* For others, it is the state whose emanating edge we couldn't match. * For others, it is the state whose emanating edge we couldn't match.