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

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

Introduction

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

Prototype

public IntervalSet getExpectedTokens() 

Source Link

Document

Gets the set of input symbols which could potentially follow the previously matched symbol at the time this exception was thrown.

Usage

From source file:com.espertech.esper.epl.parse.ExceptionConvertor.java

License:Open Source License

/**
 * Converts from a syntax error to a nice exception.
 * @param e is the syntax error/*from ww  w  .ja v a 2s  .  c  o m*/
 * @param expression is the expression text
 * @param parser the parser that parsed the expression
 * @param addPleaseCheck indicates to add "please check" paraphrases
 * @return syntax exception
 */
public static UniformPair<String> convert(RecognitionException e, String expression, boolean addPleaseCheck,
        EsperEPL2GrammarParser parser) {
    if (expression.trim().length() == 0) {
        String message = "Unexpected " + END_OF_INPUT_TEXT;
        return new UniformPair<String>(message, expression);
    }

    Token t;
    Token tBeforeBefore = null;
    Token tBefore = null;
    Token tAfter = null;

    int tIndex = e.getOffendingToken() != null ? e.getOffendingToken().getTokenIndex() : Integer.MAX_VALUE;
    if (tIndex < parser.getTokenStream().size()) {
        t = parser.getTokenStream().get(tIndex);
        if ((tIndex + 1) < parser.getTokenStream().size()) {
            tAfter = parser.getTokenStream().get(tIndex + 1);
        }
        if (tIndex - 1 >= 0) {
            tBefore = parser.getTokenStream().get(tIndex - 1);
        }
        if (tIndex - 2 >= 0) {
            tBeforeBefore = parser.getTokenStream().get(tIndex - 2);
        }
    } else {
        if (parser.getTokenStream().size() >= 1) {
            tBeforeBefore = parser.getTokenStream().get(parser.getTokenStream().size() - 1);
        }
        if (parser.getTokenStream().size() >= 2) {
            tBefore = parser.getTokenStream().get(parser.getTokenStream().size() - 2);
        }
        t = parser.getTokenStream().get(parser.getTokenStream().size() - 1);
    }

    Token tEnd = null;
    if (parser.getTokenStream().size() > 0) {
        tEnd = parser.getTokenStream().get(parser.getTokenStream().size() - 1);
    }

    String positionInfo = getPositionInfo(t);
    String token = t.getType() == EsperEPL2GrammarParser.EOF ? "end-of-input" : "'" + t.getText() + "'";

    Stack stack = parser.getParaphrases();
    String check = "";
    boolean isSelect = stack.size() == 1 && stack.get(0).equals("select clause");
    if ((stack.size() > 0) && addPleaseCheck) {
        String delimiter = "";
        StringBuilder checkList = new StringBuilder();
        checkList.append(", please check the ");
        while (stack.size() != 0) {
            checkList.append(delimiter);
            checkList.append(stack.pop());
            delimiter = " within the ";
        }
        check = checkList.toString();
    }

    // check if token is a reserved keyword
    Set<String> keywords = parser.getKeywords();
    boolean reservedKeyword = false;
    if (keywords.contains(token.toLowerCase())) {
        token += " (a reserved keyword)";
        reservedKeyword = true;
    } else if (tAfter != null && keywords.contains("'" + tAfter.getText().toLowerCase() + "'")) {
        token += " ('" + tAfter.getText() + "' is a reserved keyword)";
        reservedKeyword = true;
    } else {
        if ((tBefore != null) && (tAfter != null)
                && (keywords.contains("'" + tBefore.getText().toLowerCase() + "'"))
                && (keywords.contains("'" + tAfter.getText().toLowerCase() + "'"))) {
            token += " ('" + tBefore.getText() + "' and '" + tAfter.getText() + "' are a reserved keyword)";
            reservedKeyword = true;
        } else if ((tBefore != null) && (keywords.contains("'" + tBefore.getText().toLowerCase() + "'"))) {
            token += " ('" + tBefore.getText() + "' is a reserved keyword)";
            reservedKeyword = true;
        } else if (tEnd != null && keywords.contains("'" + tEnd.getText().toLowerCase() + "'")) {
            token += " ('" + tEnd.getText() + "' is a reserved keyword)";
            reservedKeyword = true;
        }
    }

    // special handling for the select-clause "as" keyword, which is required
    if (isSelect && !reservedKeyword) {
        check += getSelectClauseAsText(tBeforeBefore, t);
    }

    String message = "Incorrect syntax near " + token + positionInfo + check;
    if (e instanceof NoViableAltException || e instanceof LexerNoViableAltException
            || checkForInputMismatchWithNoExpected(e)) {
        Token nvaeToken = e.getOffendingToken();
        int nvaeTokenType = nvaeToken != null ? nvaeToken.getType() : EsperEPL2GrammarLexer.EOF;

        if (nvaeTokenType == EsperEPL2GrammarLexer.EOF) {
            if (token.equals(END_OF_INPUT_TEXT)) {
                message = "Unexpected " + END_OF_INPUT_TEXT + positionInfo + check;
            } else {
                if (ParseHelper.hasControlCharacters(expression)) {
                    message = "Unrecognized control characters found in text" + positionInfo;
                } else {
                    message = "Unexpected " + END_OF_INPUT_TEXT + " near " + token + positionInfo + check;
                }
            }
        } else {
            if (parser.getParserTokenParaphrases().get(nvaeTokenType) != null) {
                message = "Incorrect syntax near " + token + positionInfo + check;
            } else {
                // find next keyword in the next 3 tokens
                int currentIndex = tIndex + 1;
                while ((currentIndex > 0) && (currentIndex < parser.getTokenStream().size() - 1)
                        && (currentIndex < tIndex + 3)) {
                    Token next = parser.getTokenStream().get(currentIndex);
                    currentIndex++;

                    String quotedToken = "'" + next.getText() + "'";
                    if (parser.getKeywords().contains(quotedToken)) {
                        check += " near reserved keyword '" + next.getText() + "'";
                        break;
                    }
                }
                message = "Incorrect syntax near " + token + positionInfo + check;
            }
        }
    } else if (e instanceof InputMismatchException) {
        InputMismatchException mismatched = (InputMismatchException) e;

        String expected;
        if (mismatched.getExpectedTokens().size() > 1) {
            StringWriter writer = new StringWriter();
            writer.append("any of the following tokens {");
            String delimiter = "";
            for (int i = 0; i < mismatched.getExpectedTokens().size(); i++) {
                writer.append(delimiter);
                if (i > 5) {
                    writer.append("...");
                    writer.append(Integer.toString(mismatched.getExpectedTokens().size() - 5));
                    writer.append(" more");
                    break;
                }
                delimiter = ", ";
                writer.append(getTokenText(parser, mismatched.getExpectedTokens().get(i)));
            }
            writer.append("}");
            expected = writer.toString();
        } else {
            expected = getTokenText(parser, mismatched.getExpectedTokens().get(0));
        }

        int offendingTokenType = mismatched.getOffendingToken().getType();
        String unexpected = getTokenText(parser, offendingTokenType);

        String expecting = " expecting " + expected.trim() + " but found " + unexpected.trim();
        message = "Incorrect syntax near " + token + expecting + positionInfo + check;
    }

    return new UniformPair<String>(message, expression);
}

From source file:com.github.jknack.handlebars.internal.HbsErrorStrategy.java

License:Apache License

@Override
public void reportInputMismatch(final Parser recognizer, final InputMismatchException e) {
    String[] displayNames = displayNames(recognizer);
    String msg = e.getExpectedTokens().toString(displayNames);
    recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

/**
 * This is called by {@link #reportError} when the exception is an
 * {@link InputMismatchException}.//from  w w w . j a  v  a2  s  .co  m
 *
 * @see #reportError
 *
 * @param recognizer
 *            the parser instance
 * @param e
 *            the recognition exception
 */
protected void reportInputMismatch(@NotNull Parser recognizer, @NotNull InputMismatchException e) {
    String msg = "mismatched input " + getTokenErrorDisplay(e.getOffendingToken()) + " expecting "
            + e.getExpectedTokens().toString(recognizer.getTokenNames());
    recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}

From source file:cx2x.translator.language.base.ClawLanguage.java

License:BSD License

/**
 * Analyze a raw string input and match it with the CLAW language definition.
 *
 * @param rawPragma A raw pragma statement to be analyzed against the CLAW
 *                  language.//from  w  w  w  . j  a  va2  s .c  o  m
 * @param lineno    Line number of the pragma statement.
 * @param generator Accelerator directive generator.
 * @param target    Target that influences the code transformation.
 * @return A ClawLanguage object with the corresponding extracted information.
 * @throws IllegalDirectiveException If directive does not follow the CLAW
 *                                   language specification.
 */
private static ClawLanguage analyze(String rawPragma, int lineno, AcceleratorGenerator generator, Target target)
        throws IllegalDirectiveException {
    // Remove additional claw keyword
    rawPragma = nakenize(rawPragma);

    // Discard the ignored code after the claw ignore directive
    if (rawPragma.toLowerCase().contains(IGNORE)) {
        rawPragma = rawPragma.substring(0, rawPragma.toLowerCase().indexOf(IGNORE) + IGNORE.length());
    }

    // Instantiate the lexer with the raw string input
    ClawLexer lexer = new ClawLexer(CharStreams.fromString(rawPragma));

    // Get a list of matched tokens
    CommonTokenStream tokens = new CommonTokenStream(lexer);

    // Pass the tokens to the parser
    ClawParser parser = new ClawParser(tokens);
    parser.setErrorHandler(new BailErrorStrategy());
    parser.removeErrorListeners();

    try {
        // Start the parser analysis from the "analyze" entry point
        ClawParser.AnalyzeContext ctx = parser.analyze();
        // Get the ClawLanguage object return by the parser after analysis.
        ctx.l.setAcceleratorGenerator(generator);
        ctx.l.setTarget(target);
        return ctx.l;
    } catch (ParseCancellationException pcex) {
        if (pcex.getCause() instanceof InputMismatchException) {
            InputMismatchException imex = (InputMismatchException) pcex.getCause();
            throw new IllegalDirectiveException(getTokens(imex.getExpectedTokens(), parser), lineno,
                    imex.getOffendingToken().getCharPositionInLine());
        } else if (pcex.getCause() instanceof NoViableAltException) {
            NoViableAltException nvex = (NoViableAltException) pcex.getCause();
            throw new IllegalDirectiveException(nvex.getOffendingToken(),
                    getTokens(nvex.getExpectedTokens(), parser), lineno,
                    nvex.getOffendingToken().getCharPositionInLine());
        }
        throw new IllegalDirectiveException(rawPragma, "Unsupported construct", lineno, 0);
    }
}

From source file:de.up.ling.irtg.codec.ExceptionErrorStrategy.java

@Override
public void reportInputMismatch(Parser recognizer, InputMismatchException e) throws RecognitionException {
    String msg = getTokenPosition(e.getOffendingToken()) + ": mismatched input "
            + getTokenErrorDisplay(e.getOffendingToken());
    msg += " expecting one of " + e.getExpectedTokens().toString(recognizer.getTokenNames());
    RecognitionException ex = new RecognitionException(msg, recognizer, recognizer.getInputStream(),
            recognizer.getContext());// w  ww.  j a v a  2s. c o m
    ex.initCause(e);
    throw ex;
}

From source file:kr.ac.korea.dbserver.parser.SQLErrorStrategy.java

License:Apache License

protected void reportInputMismatchException(@NotNull Parser recognizer, @NotNull InputMismatchException e) {
    String msg = "mismatched input " + getTokenErrorDisplay(e.getOffendingToken()) + " expecting "
            + e.getExpectedTokens().toString(recognizer.getTokenNames());
    recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}

From source file:net.openchrom.xxd.processor.supplier.rscripting.ui.editor.RErrorStrategy.java

License:Open Source License

/**
 * This is called by {@link #reportError} when the exception is an
 * {@link InputMismatchException}.//from   w  w  w.  j  a v  a2s.  com
 *
 * @see #reportError
 *
 * @param recognizer
 *            the parser instance
 * @param e
 *            the recognition exception
 */
protected void reportInputMismatch(@NotNull Parser recognizer, @NotNull InputMismatchException e) {

    String msg = "mismatched input " + getTokenErrorDisplay(e.getOffendingToken())
            + " expecting the following symbols: " + "\n"
            + e.getExpectedTokens().toString(recognizer.getTokenNames());
    recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}

From source file:org.ballerinalang.composer.service.workspace.rest.datamodel.BallerinaComposerErrorStrategy.java

License:Open Source License

@Override
public void reportInputMismatch(Parser parser, InputMismatchException e) {
    Token missingSymbol = getMissingSymbol(parser);
    int line = missingSymbol.getLine();
    int position = missingSymbol.getCharPositionInLine();
    String mismatchedToken = getTokenErrorDisplay(e.getOffendingToken());
    String expectedToken = e.getExpectedTokens().toString(parser.getVocabulary());
    String msg = getSourceLocation(parser, line, position) + "mismatched input " + mismatchedToken
            + ". Expecting one of " + expectedToken;
    // FixMe: This need to be handled by grammar itself
    if (!EOF.equals(mismatchedToken)) {
        errorTokens.add(createError(line, position, msg));
    }/* w ww . ja v  a2s  .c om*/
}

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

License:Open Source License

@Override
public void reportInputMismatch(Parser parser, InputMismatchException e) {
    fetchPossibleTokens(parser, e.getOffendingToken(), e.getExpectedTokens());
}

From source file:org.ballerinalang.util.parser.BallerinaParserErrorStrategy.java

License:Open Source License

@Override
public void reportInputMismatch(Parser parser, InputMismatchException e) {
    int line = getMissingSymbol(parser).getLine();
    int position = getMissingSymbol(parser).getCharPositionInLine();
    String mismatchedToken = getTokenErrorDisplay(e.getOffendingToken());
    String expectedToken = e.getExpectedTokens().toString(parser.getVocabulary());
    String msg = getSourceLocation(parser, line, position) + "mismatched input " + mismatchedToken
            + ". Expecting one of " + expectedToken;
    setContextException(parser);/*  w  w w . j  a v  a  2  s  .  com*/
    throw new ParseCancellationException(msg);
}