forked from jasder/antlr
Simplify FailedPredicateException constructors, encapsulate fields
This commit is contained in:
parent
ed7d4b1dc1
commit
1ae3c0104c
|
@ -229,7 +229,7 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
|
|||
throws RecognitionException
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@ package org.antlr.v4.runtime;
|
|||
|
||||
import org.antlr.v4.runtime.atn.ATNState;
|
||||
import org.antlr.v4.runtime.atn.PredicateTransition;
|
||||
import org.antlr.v4.runtime.misc.NotNull;
|
||||
import org.antlr.v4.runtime.misc.Nullable;
|
||||
|
||||
/** A semantic predicate failed during validation. Validation of predicates
|
||||
|
@ -38,40 +39,50 @@ import org.antlr.v4.runtime.misc.Nullable;
|
|||
* prediction.
|
||||
*/
|
||||
public class FailedPredicateException extends RecognitionException {
|
||||
public int ruleIndex;
|
||||
public int predIndex;
|
||||
public String predicate;
|
||||
public String msg;
|
||||
private final int ruleIndex;
|
||||
private final int predicateIndex;
|
||||
private final String predicate;
|
||||
|
||||
public FailedPredicateException(Parser recognizer) {
|
||||
public FailedPredicateException(@NotNull Parser recognizer) {
|
||||
this(recognizer, null);
|
||||
}
|
||||
|
||||
public FailedPredicateException(Parser recognizer, @Nullable String predicate) {
|
||||
super(recognizer, recognizer.getInputStream(), recognizer._ctx);
|
||||
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(@NotNull Parser recognizer, @Nullable String predicate) {
|
||||
this(recognizer, predicate, null);
|
||||
}
|
||||
|
||||
public FailedPredicateException(Parser recognizer,
|
||||
public FailedPredicateException(@NotNull Parser recognizer,
|
||||
@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);
|
||||
PredicateTransition trans = (PredicateTransition)s.transition(0);
|
||||
ruleIndex = trans.ruleIndex;
|
||||
predIndex = trans.predIndex;
|
||||
this.ruleIndex = trans.ruleIndex;
|
||||
this.predicateIndex = trans.predIndex;
|
||||
this.predicate = predicate;
|
||||
this.msg = msg;
|
||||
Token la = recognizer.getCurrentToken();
|
||||
this.offendingToken = la;
|
||||
this.offendingToken = recognizer.getCurrentToken();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -66,6 +66,16 @@ public class RecognitionException extends RuntimeException {
|
|||
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?
|
||||
* For No viable alternative exceptions, this is the decision state number.
|
||||
* For others, it is the state whose emanating edge we couldn't match.
|
||||
|
|
Loading…
Reference in New Issue