Example usage for org.antlr.v4.runtime.misc IntervalSet IntervalSet

List of usage examples for org.antlr.v4.runtime.misc IntervalSet IntervalSet

Introduction

In this page you can find the example usage for org.antlr.v4.runtime.misc IntervalSet IntervalSet.

Prototype

IntervalSet

Source Link

Usage

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

/**
 * {@inheritDoc}/*www. j a  v a  2  s. c om*/
 *
 * <p>
 * The default implementation resynchronizes the parser by consuming tokens
 * until we find one in the resynchronization set--loosely the set of tokens
 * that can follow the current rule.
 * </p>
 */
@Override
public void recover(Parser recognizer, RecognitionException e) {
    // System.out.println("recover in "+recognizer.getRuleInvocationStack()+
    // " index="+recognizer.getInputStream().index()+
    // ", lastErrorIndex="+
    // lastErrorIndex+
    // ", states="+lastErrorStates);
    if (lastErrorIndex == recognizer.getInputStream().index() && lastErrorStates != null
            && lastErrorStates.contains(recognizer.getState())) {
        // uh oh, another error at same token index and previously-visited
        // state in ATN; must be a case where LT(1) is in the recovery
        // token set so nothing got consumed. Consume a single token
        // at least to prevent an infinite loop; this is a failsafe.
        // System.err.println("seen error condition before index="+
        // lastErrorIndex+", states="+lastErrorStates);
        // System.err.println("FAILSAFE consumes "+recognizer.getTokenNames()[recognizer.getInputStream().LA(1)]);
        recognizer.consume();
    }
    lastErrorIndex = recognizer.getInputStream().index();
    if (lastErrorStates == null)
        lastErrorStates = new IntervalSet();
    lastErrorStates.add(recognizer.getState());
}

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

@NotNull
protected IntervalSet getErrorRecoverySet(@NotNull Parser recognizer) {
    ATN atn = recognizer.getInterpreter().atn;
    RuleContext ctx = recognizer.getContext();
    IntervalSet recoverSet = new IntervalSet();
    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);//w ww . j  a v  a  2  s.  c o m
        ctx = ctx.parent;
    }
    recoverSet.remove(Token.EPSILON);
    // System.out.println("recover set "+recoverSet.toString(recognizer.getTokenNames()));
    return recoverSet;
}