Example usage for org.antlr.v4.runtime.atn ATN nextTokens

List of usage examples for org.antlr.v4.runtime.atn ATN nextTokens

Introduction

In this page you can find the example usage for org.antlr.v4.runtime.atn ATN nextTokens.

Prototype

public IntervalSet nextTokens(ATNState s, RuleContext ctx) 

Source Link

Document

Compute the set of valid tokens that can occur starting in state s .

Usage

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

/**
 * This method implements the single-token insertion inline error recovery
 * strategy. It is called by {@link #recoverInline} if the single-token
 * deletion strategy fails to recover from the mismatched input. If this
 * method returns {@code true}, {@code recognizer} will be in error recovery
 * mode.//w w w.  j  a v  a 2 s .  c o m
 *
 * <p>
 * This method determines whether or not single-token insertion is viable by
 * checking if the {@code LA(1)} input symbol could be successfully matched
 * if it were instead the {@code LA(2)} symbol. If this method returns
 * {@code true}, the caller is responsible for creating and inserting a
 * token with the correct type to produce this behavior.
 * </p>
 *
 * @param recognizer
 *            the parser instance
 * @return {@code true} if single-token insertion is a viable recovery
 *         strategy for the current mismatched input, otherwise
 *         {@code false}
 */
protected boolean singleTokenInsertion(@NotNull Parser recognizer) {
    int currentSymbolType = recognizer.getInputStream().LA(1);
    // if current token is consistent with what could come after current
    // ATN state, then we know we're missing a token; error recovery
    // is free to conjure up and insert the missing token
    ATNState currentState = recognizer.getInterpreter().atn.states.get(recognizer.getState());
    ATNState next = currentState.transition(0).target;
    ATN atn = recognizer.getInterpreter().atn;
    IntervalSet expectingAtLL2 = atn.nextTokens(next, recognizer.getContext());
    // System.out.println("LT(2) set="+expectingAtLL2.toString(recognizer.getTokenNames()));
    if (expectingAtLL2.contains(currentSymbolType)) {
        reportMissingToken(recognizer);
        return true;
    }
    return false;
}