Example usage for org.antlr.v4.runtime Parser getExpectedTokens

List of usage examples for org.antlr.v4.runtime Parser getExpectedTokens

Introduction

In this page you can find the example usage for org.antlr.v4.runtime Parser getExpectedTokens.

Prototype

public IntervalSet getExpectedTokens() 

Source Link

Document

Computes the set of input symbols which could follow the current parser state and context, as given by #getState and #getContext , respectively.

Usage

From source file:com.nextbreakpoint.nextfractal.contextfree.compiler.CompilerErrorStrategy.java

License:Open Source License

private String generateErrorMessage(String message, Parser recognizer) {
    StringBuilder builder = new StringBuilder();
    builder.append(message);//from  w w  w.  j  a  va 2 s  .  c  o m
    IntervalSet tokens = recognizer.getExpectedTokens();
    boolean first = true;
    for (Entry<String, Integer> entry : recognizer.getTokenTypeMap().entrySet()) {
        if (tokens.contains(entry.getValue())) {
            if (first) {
                first = false;
                if (message.length() > 0 && !message.endsWith(".")) {
                    builder.append(". ");
                }
                builder.append("Expected tokens: ");
            } else {
                builder.append(", ");
            }
            builder.append(entry.getKey());
        }
    }
    return builder.toString();
}

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

/**
 * The default implementation of {@link ANTLRErrorStrategy#sync} makes sure
 * that the current lookahead symbol is consistent with what were expecting
 * at this point in the ATN. You can call this anytime but ANTLR only
 * generates code to check before subrules/loops and each iteration.
 *
 * <p>/* w w  w . ja  va 2 s . co m*/
 * Implements Jim Idle's magic sync mechanism in closures and optional
 * subrules. E.g.,
 * </p>
 *
 * <pre>
 * a : sync ( stuff sync )* ;
 * sync : {consume to what can follow sync} ;
 * </pre>
 *
 * At the start of a sub rule upon error, {@link #sync} performs single
 * token deletion, if possible. If it can't do that, it bails on the current
 * rule and uses the default error recovery, which consumes until the
 * resynchronization set of the current rule.
 *
 * <p>
 * If the sub rule is optional ({@code (...)?}, {@code (...)*}, or block
 * with an empty alternative), then the expected set includes what follows
 * the subrule.
 * </p>
 *
 * <p>
 * During loop iteration, it consumes until it sees a token that can start a
 * sub rule or what follows loop. Yes, that is pretty aggressive. We opt to
 * stay in the loop as long as possible.
 * </p>
 *
 * <p>
 * <strong>ORIGINS</strong>
 * </p>
 *
 * <p>
 * Previous versions of ANTLR did a poor job of their recovery within loops.
 * A single mismatch token or missing token would force the parser to bail
 * out of the entire rules surrounding the loop. So, for rule
 * </p>
 *
 * <pre>
 * classDef : 'class' ID '{' member* '}'
 * </pre>
 *
 * input with an extra token between members would force the parser to
 * consume until it found the next class definition rather than the next
 * member definition of the current class.
 *
 * <p>
 * This functionality cost a little bit of effort because the parser has to
 * compare token set at the start of the loop and at each iteration. If for
 * some reason speed is suffering for you, you can turn off this
 * functionality by simply overriding this method as a blank { }.
 * </p>
 */
@Override
public void sync(Parser recognizer) throws RecognitionException {
    ATNState s = recognizer.getInterpreter().atn.states.get(recognizer.getState());
    // System.err.println("sync @ "+s.stateNumber+"="+s.getClass().getSimpleName());
    // If already recovering, don't try to sync
    if (inErrorRecoveryMode(recognizer)) {
        return;
    }

    TokenStream tokens = recognizer.getInputStream();
    int la = tokens.LA(1);

    // try cheaper subset first; might get lucky. seems to shave a wee bit
    // off
    if (recognizer.getATN().nextTokens(s).contains(la) || la == Token.EOF)
        return;

    // Return but don't end recovery. only do that upon valid token match
    if (recognizer.isExpectedToken(la)) {
        return;
    }

    switch (s.getStateType()) {
    case ATNState.BLOCK_START:
    case ATNState.STAR_BLOCK_START:
    case ATNState.PLUS_BLOCK_START:
    case ATNState.STAR_LOOP_ENTRY:
        // report error and recover if possible
        if (singleTokenDeletion(recognizer) != null) {
            return;
        }

        throw new InputMismatchException(recognizer);

    case ATNState.PLUS_LOOP_BACK:
    case ATNState.STAR_LOOP_BACK:
        // System.err.println("at loop back: "+s.getClass().getSimpleName());
        reportUnwantedToken(recognizer);
        IntervalSet expecting = recognizer.getExpectedTokens();
        IntervalSet whatFollowsLoopIterationOrRule = expecting.or(getErrorRecoverySet(recognizer));
        consumeUntil(recognizer, whatFollowsLoopIterationOrRule);
        break;

    default:
        // do nothing if we can't identify the exact kind of ATN state
        break;
    }
}

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

@NotNull
protected IntervalSet getExpectedTokens(@NotNull Parser recognizer) {
    return recognizer.getExpectedTokens();
}

From source file:org.apache.lucene.expressions.js.JavascriptParserErrorStrategy.java

License:Apache License

/**
 * Ensures the ANTLR parser will throw an exception after the first error
 *
 * @param recognizer the parser being used
 * @return no actual return value// w  ww . ja  va2 s .  co m
 * @throws RecognitionException not used as a ParseException wrapped in a RuntimeException is thrown instead
 */
@Override
public Token recoverInline(Parser recognizer) throws RecognitionException {
    Token token = recognizer.getCurrentToken();
    String message = "unexpected token " + getTokenErrorDisplay(token) + " on line (" + token.getLine()
            + ") position (" + token.getCharPositionInLine() + ")" + " was expecting one of "
            + recognizer.getExpectedTokens().toString(recognizer.getVocabulary());
    ParseException parseException = new ParseException(message, token.getStartIndex());
    throw new RuntimeException(parseException);
}

From source file:org.ballerinalang.composer.service.workspace.suggetions.CapturePossibleTokenStrategy.java

License:Open Source License

@Override
public void reportMissingToken(Parser parser) {
    fetchPossibleTokens(parser, parser.getCurrentToken(), parser.getExpectedTokens());
}

From source file:org.ballerinalang.composer.service.workspace.suggetions.CapturePossibleTokenStrategy.java

License:Open Source License

@Override
public void reportUnwantedToken(Parser parser) {
    fetchPossibleTokens(parser, parser.getCurrentToken(), parser.getExpectedTokens());
}

From source file:org.elasticsearch.painless.ParserErrorStrategy.java

License:Apache License

@Override
public Token recoverInline(Parser recognizer) throws RecognitionException {
    Token token = recognizer.getCurrentToken();
    String message = "Error[" + token.getLine() + ":" + token.getCharPositionInLine() + "]:"
            + " unexpected token [" + getTokenErrorDisplay(token) + "]" + " was expecting one of ["
            + recognizer.getExpectedTokens().toString(recognizer.getVocabulary()) + "].";
    ParseException parseException = new ParseException(message, token.getStartIndex());
    throw new RuntimeException(parseException);
}