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

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

Introduction

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

Prototype

public int getMinElement() 

Source Link

Document

Returns the minimum value contained in the set if not isNil().

Usage

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

/**
 * Conjure up a missing token during error recovery.
 *
 * The recognizer attempts to recover from single missing symbols. But,
 * actions might refer to that missing symbol. For example, x=ID {f($x);}.
 * The action clearly assumes that there has been an identifier matched
 * previously and that $x points at that token. If that token is missing,
 * but the next token in the stream is what we want we assume that this
 * token is missing and we keep going. Because we have to return some token
 * to replace the missing token, we have to conjure one up. This method
 * gives the user control over the tokens returned for missing tokens.
 * Mostly, you will want to create something special for identifier tokens.
 * For literals such as '{' and ',', the default action in the parser or
 * tree parser works. It simply creates a CommonToken of the appropriate
 * type. The text will be the token. If you change what tokens must be
 * created by the lexer, override this method to create the appropriate
 * tokens.//w ww .  ja v  a 2s  . c o  m
 */
@NotNull
protected Token getMissingSymbol(@NotNull Parser recognizer) {
    Token currentSymbol = recognizer.getCurrentToken();
    IntervalSet expecting = getExpectedTokens(recognizer);
    int expectedTokenType = expecting.getMinElement(); // get any element
    String tokenText;
    if (expectedTokenType == Token.EOF)
        tokenText = "<missing EOF>";
    else
        tokenText = "<missing " + recognizer.getTokenNames()[expectedTokenType] + ">";
    Token current = currentSymbol;
    Token lookback = recognizer.getInputStream().LT(-1);
    if (current.getType() == Token.EOF && lookback != null) {
        current = lookback;
    }
    return recognizer.getTokenFactory()
            .create(new Pair<TokenSource, CharStream>(current.getTokenSource(),
                    current.getTokenSource().getInputStream()), expectedTokenType, tokenText,
                    Token.DEFAULT_CHANNEL, -1, -1, current.getLine(), current.getCharPositionInLine());
}