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

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

Introduction

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

Prototype

public Token getOffendingToken() 

Source Link

Usage

From source file:com.antsdb.saltedfish.sql.Session.java

License:Open Source License

public Script parse(CharStream cs) {
    if (this.isClosed) {
        throw new OrcaException("session is closed");
    }/* w ww. j  av a 2  s  . co m*/

    startTrx();

    // check the global statement cache

    Script script = this.getOrca().getCachedStatement(cs.toString());

    // parse it for real if it is not cached

    if (script == null) {
        try {
            script = this.fac.parse(this, cs);
        } catch (ParseCancellationException x) {
            if (x.getCause() instanceof InputMismatchException) {
                InputMismatchException xx = (InputMismatchException) x.getCause();
                String offend = xx.getOffendingToken().getText();
                throw new OrcaException(x, "Invalid SQL. Offending token: {}", offend);
            }
            if (x.getCause() instanceof NoViableAltException) {
                NoViableAltException xx = (NoViableAltException) x.getCause();
                String offend = xx.getOffendingToken().getText();
                throw new OrcaException(x, "Invalid SQL. Offending token: {}", offend);
            }
            throw new OrcaException("Invalid SQL", x);
        } catch (CompileDdlException x) {
            script = new Script(null, x.nParameters, 100, cs.toString());
        }
        if (_log.isTraceEnabled()) {
            _log.trace("{}-{}: {}", getId(), script.hashCode(), cs.toString());
        }
        if (script.worthCache()) {
            this.getOrca().cacheStatement(script);
        }
    }

    return script;
}

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  www .j a va2 s .  c om
 * @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.nextbreakpoint.nextfractal.contextfree.compiler.CompilerErrorStrategy.java

License:Open Source License

@Override
protected void reportInputMismatch(Parser recognizer, InputMismatchException e) {
    String message = generateErrorMessage("Input mismatch", recognizer);
    CompilerError error = new CompilerError(CompilerError.ErrorType.CFDG_COMPILER,
            e.getOffendingToken().getLine(), e.getOffendingToken().getCharPositionInLine(),
            e.getOffendingToken().getStartIndex(),
            recognizer.getCurrentToken().getStopIndex() - recognizer.getCurrentToken().getStartIndex(),
            message);//from   www .  j  av a  2 s  .  co m
    logger.log(Level.FINE, error.toString(), e);
    errors.add(error);
}

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

License:Open Source License

@Override
protected void reportInputMismatch(Parser recognizer, InputMismatchException e) {
    String message = generateErrorMessage("Input mismatch", recognizer);
    CompilerError error = new CompilerError(CompilerError.ErrorType.M_COMPILER, e.getOffendingToken().getLine(),
            e.getOffendingToken().getCharPositionInLine(), e.getOffendingToken().getStartIndex(),
            recognizer.getCurrentToken().getStopIndex() - recognizer.getCurrentToken().getStartIndex(),
            message);//from w  ww . j  av a2 s .  com
    logger.log(Level.FINE, error.toString(), e);
    errors.add(error);
}

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

/**
 * This is called by {@link #reportError} when the exception is an
 * {@link InputMismatchException}.//ww  w .  ja v  a  2s .  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./*  ww w  .  j a  v a2 s  .co 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());//from   w  w  w  . j a v a 2s  .co 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}.//w w w .  jav a  2 s . c o  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 the following symbols: " + "\n"
            + e.getExpectedTokens().toString(recognizer.getTokenNames());
    recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
}