forked from jasder/antlr
Simplify Parser.getExpectedTokens and DefaultErrorStrategy.getErrorRecoverySet
This commit is contained in:
parent
a70479ad0c
commit
d33172dce5
|
@ -34,7 +34,6 @@ import org.antlr.v4.runtime.atn.ATNState;
|
||||||
import org.antlr.v4.runtime.atn.BlockStartState;
|
import org.antlr.v4.runtime.atn.BlockStartState;
|
||||||
import org.antlr.v4.runtime.atn.PlusBlockStartState;
|
import org.antlr.v4.runtime.atn.PlusBlockStartState;
|
||||||
import org.antlr.v4.runtime.atn.PlusLoopbackState;
|
import org.antlr.v4.runtime.atn.PlusLoopbackState;
|
||||||
import org.antlr.v4.runtime.atn.RuleTransition;
|
|
||||||
import org.antlr.v4.runtime.atn.StarLoopEntryState;
|
import org.antlr.v4.runtime.atn.StarLoopEntryState;
|
||||||
import org.antlr.v4.runtime.atn.StarLoopbackState;
|
import org.antlr.v4.runtime.atn.StarLoopbackState;
|
||||||
import org.antlr.v4.runtime.misc.IntervalSet;
|
import org.antlr.v4.runtime.misc.IntervalSet;
|
||||||
|
@ -522,19 +521,8 @@ public class DefaultErrorStrategy implements ANTLRErrorStrategy {
|
||||||
*/
|
*/
|
||||||
protected IntervalSet getErrorRecoverySet(Parser recognizer) {
|
protected IntervalSet getErrorRecoverySet(Parser recognizer) {
|
||||||
ATN atn = recognizer.getInterpreter().atn;
|
ATN atn = recognizer.getInterpreter().atn;
|
||||||
RuleContext ctx = recognizer._ctx;
|
ParserRuleContext ctx = recognizer._ctx;
|
||||||
IntervalSet recoverSet = new IntervalSet();
|
return atn.nextTokens(atn.states.get(ctx.s), ctx);
|
||||||
while ( ctx!=null && ctx.invokingState>=0 ) {
|
|
||||||
// compute what follows who invoked us
|
|
||||||
ATNState invokingState = atn.states.get(ctx.invokingState);
|
|
||||||
RuleTransition rt = (RuleTransition)invokingState.transition(0);
|
|
||||||
IntervalSet follow = atn.nextTokens(rt.followState);
|
|
||||||
recoverSet.addAll(follow);
|
|
||||||
ctx = ctx.parent;
|
|
||||||
}
|
|
||||||
recoverSet.remove(Token.EPSILON);
|
|
||||||
// System.out.println("recover set "+recoverSet.toString(recognizer.getTokenNames()));
|
|
||||||
return recoverSet;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Consume tokens until one matches the given token set */
|
/** Consume tokens until one matches the given token set */
|
||||||
|
|
|
@ -547,24 +547,7 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator> {
|
||||||
ATN atn = getInterpreter().atn;
|
ATN atn = getInterpreter().atn;
|
||||||
ParserRuleContext ctx = _ctx;
|
ParserRuleContext ctx = _ctx;
|
||||||
ATNState s = atn.states.get(ctx.s);
|
ATNState s = atn.states.get(ctx.s);
|
||||||
IntervalSet following = atn.nextTokens(s);
|
return atn.nextTokens(s, ctx);
|
||||||
// System.out.println("following "+s+"="+following);
|
|
||||||
if ( !following.contains(Token.EPSILON) ) return following;
|
|
||||||
IntervalSet expected = new IntervalSet();
|
|
||||||
expected.addAll(following);
|
|
||||||
expected.remove(Token.EPSILON);
|
|
||||||
while ( ctx!=null && ctx.invokingState>=0 && following.contains(Token.EPSILON) ) {
|
|
||||||
ATNState invokingState = atn.states.get(ctx.invokingState);
|
|
||||||
RuleTransition rt = (RuleTransition)invokingState.transition(0);
|
|
||||||
following = atn.nextTokens(rt.followState);
|
|
||||||
expected.addAll(following);
|
|
||||||
expected.remove(Token.EPSILON);
|
|
||||||
ctx = (ParserRuleContext)ctx.parent;
|
|
||||||
}
|
|
||||||
if ( following.contains(Token.EPSILON) ) {
|
|
||||||
expected.add(Token.EOF);
|
|
||||||
}
|
|
||||||
return expected;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IntervalSet getExpectedTokensWithinCurrentRule() {
|
public IntervalSet getExpectedTokensWithinCurrentRule() {
|
||||||
|
|
Loading…
Reference in New Issue