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

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

Introduction

In this page you can find the example usage for org.antlr.v4.runtime RecognitionException 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

private static boolean checkForInputMismatchWithNoExpected(RecognitionException e) {
    if (!(e instanceof InputMismatchException)) {
        return false;
    }/*from  w  w w.ja v a2 s. c o  m*/
    if (e.getExpectedTokens().size() > 1) {
        return false;
    }
    return e.getExpectedTokens().size() == 1 && e.getExpectedTokens().get(0) == -1;
}

From source file:mbtarranger.languages.mcrlOuter.ErrorReporter.java

License:Open Source License

private void toBadbank(Parser arg0, RecognitionException arg1) {
    // if (arg1 instanceof NoViableAltException){
    // NoViableAltException novex = (NoViableAltException) arg1;
    StringBuilder sb = new StringBuilder();
    for (int tk : arg1.getExpectedTokens().toArray()) {
        sb.append(arg0.getTokenNames()[tk]);
        sb.append(" ");
    }//from  w w w .  ja  v a2 s .  c o  m
    String positionInfo = "";
    try {
        positionInfo = "startline " +
        /* [@8,50:50='.',<94>,3:5] */
                arg1.getOffendingToken().toString().split(">,")[1].split("]")[0];
    } catch (Exception ex) {
    }
    String msg = String.format("%s, mal input: '%s', expected '%s' ", positionInfo,
            getTokenErrorDisplay(arg1.getOffendingToken()), sb.toString());
    BadBank.reportProblem(arg1.getOffendingToken().getLine(), arrFile, msg);
}

From source file:mbtarranger.languages.promelaOuter.ErrorReporter.java

License:Open Source License

private void toBadbank(Parser arg0, RecognitionException arg1) {
    //if (arg1 instanceof NoViableAltException){
    //NoViableAltException novex = (NoViableAltException) arg1;
    StringBuilder sb = new StringBuilder();
    for (int tk : arg1.getExpectedTokens().toArray()) {
        sb.append(arg0.getTokenNames()[tk]);
        sb.append(" ");
    }//from  w w w. j av a 2  s  . c o  m
    String positionInfo = "";
    try {
        positionInfo = "startline " +
        /* [@8,50:50='.',<94>,3:5] */
                arg1.getOffendingToken().toString().split(">,")[1].split("]")[0];
    } catch (Exception ex) {
    }
    String msg = String.format("%s, mal input: '%s', expected '%s' ", positionInfo,
            getTokenErrorDisplay(arg1.getOffendingToken()), sb.toString());
    BadBank.reportProblem(arg1.getOffendingToken().getLine(), arrFile, msg);
}

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
 * @param re the original exception from the parser
 *//* w w w  . j a v  a2s  .  c om*/
@Override
public void recover(Parser recognizer, RecognitionException re) {
    Token token = re.getOffendingToken();
    String message;

    if (token == null) {
        message = "error " + getTokenErrorDisplay(token);
    } else if (re instanceof InputMismatchException) {
        message = "unexpected token " + getTokenErrorDisplay(token) + " on line (" + token.getLine()
                + ") position (" + token.getCharPositionInLine() + ")" + " was expecting one of "
                + re.getExpectedTokens().toString(recognizer.getVocabulary());
    } else if (re instanceof NoViableAltException) {
        if (token.getType() == JavascriptParser.EOF) {
            message = "unexpected end of expression";
        } else {
            message = "invalid sequence of tokens near " + getTokenErrorDisplay(token) + " on line ("
                    + token.getLine() + ") position (" + token.getCharPositionInLine() + ")";
        }
    } else {
        message = " unexpected token near " + getTokenErrorDisplay(token) + " on line (" + token.getLine()
                + ") position (" + token.getCharPositionInLine() + ")";
    }

    ParseException parseException = new ParseException(message, token.getStartIndex());
    parseException.initCause(re);
    throw new RuntimeException(parseException);
}

From source file:org.eclipse.titan.common.parsers.ParserLogger.java

License:Open Source License

/**
 * Rule exception info in string format for logging purpose
 * @param aRule rule/*from  w  ww  . j  a va2s. co m*/
 * @param aTokenNameResolver resolver to get token name
 * @return exception stack trace + some other info from the exception object
 */
private static String getExceptionInfo(final ParserRuleContext aRule,
        final TokenNameResolver aTokenNameResolver) {
    final RecognitionException e = aRule.exception;
    if (e == null) {
        return "";
    }
    final StringBuilder sb = new StringBuilder();
    sb.append("\naRule.getText() == " + aRule.getText());
    sb.append("\ngetOffendingState() == " + e.getOffendingState());
    sb.append("\ngetExpectedTokens() == [");
    final List<Integer> expectedTokens = e.getExpectedTokens().toList();
    for (int i = 0; i < expectedTokens.size(); i++) {
        if (i > 0) {
            sb.append(", ");
        }
        final int tokenType = expectedTokens.get(i);
        sb.append(getTokenName(tokenType, aTokenNameResolver));
    }
    sb.append("]");
    if (e instanceof NoViableAltException) {
        NoViableAltException nvae = (NoViableAltException) e;
        sb.append("\ngetStartToken() == " + getTokenInfo(nvae.getStartToken(), aTokenNameResolver));
        sb.append("\ngetDeadEndConfigs() == " + nvae.getDeadEndConfigs());
    }

    final StringWriter errors = new StringWriter();
    e.printStackTrace(new PrintWriter(errors));
    sb.append("\n" + errors.toString());
    return sb.toString();
}

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

License:Apache License

@Override
public void recover(Parser recognizer, RecognitionException re) {
    Token token = re.getOffendingToken();
    String message;/*from www.j  a  v  a  2 s .c om*/

    if (token == null) {
        message = "Error: no parse token found.";
    } else if (re instanceof InputMismatchException) {
        message = "Error[" + token.getLine() + ":" + token.getCharPositionInLine() + "]:"
                + " unexpected token [" + getTokenErrorDisplay(token) + "]" + " was expecting one of ["
                + re.getExpectedTokens().toString(recognizer.getVocabulary()) + "].";
    } else if (re instanceof NoViableAltException) {
        if (token.getType() == PainlessParser.EOF) {
            message = "Error: unexpected end of script.";
        } else {
            message = "Error[" + token.getLine() + ":" + token.getCharPositionInLine() + "]:"
                    + "invalid sequence of tokens near [" + getTokenErrorDisplay(token) + "].";
        }
    } else {
        message = "Error[" + token.getLine() + ":" + token.getCharPositionInLine() + "]:"
                + " unexpected token near [" + getTokenErrorDisplay(token) + "].";
    }

    ParseException parseException = new ParseException(message, token == null ? -1 : token.getStartIndex());
    parseException.initCause(re);

    throw new RuntimeException(parseException);
}

From source file:org.elasticsearch.plan.a.ParserErrorStrategy.java

License:Apache License

@Override
public void recover(Parser recognizer, RecognitionException re) {
    Token token = re.getOffendingToken();
    String message;/* ww  w  .  ja v a 2s.co  m*/

    if (token == null) {
        message = "Error: no parse token found.";
    } else if (re instanceof InputMismatchException) {
        message = "Error[" + token.getLine() + ":" + token.getCharPositionInLine() + "]:"
                + " unexpected token [" + getTokenErrorDisplay(token) + "]" + " was expecting one of ["
                + re.getExpectedTokens().toString(recognizer.getVocabulary()) + "].";
    } else if (re instanceof NoViableAltException) {
        if (token.getType() == PlanAParser.EOF) {
            message = "Error: unexpected end of script.";
        } else {
            message = "Error[" + token.getLine() + ":" + token.getCharPositionInLine() + "]:"
                    + "invalid sequence of tokens near [" + getTokenErrorDisplay(token) + "].";
        }
    } else {
        message = "Error[" + token.getLine() + ":" + token.getCharPositionInLine() + "]:"
                + " unexpected token near [" + getTokenErrorDisplay(token) + "].";
    }

    ParseException parseException = new ParseException(message, token == null ? -1 : token.getStartIndex());
    parseException.initCause(re);

    throw new RuntimeException(parseException);
}

From source file:org.hawkular.inventory.rest.Traverser.java

License:Apache License

public Query navigate(String traversal) {
    HawkularInventoryGetUriLexer lexer = new HawkularInventoryGetUriLexer(new ANTLRInputStream(traversal));
    CommonTokenStream tokens = new CommonTokenStream(lexer);

    HawkularInventoryGetUriParser parser = new HawkularInventoryGetUriParser(tokens);
    parser.setErrorHandler(new BailErrorStrategy());

    ParseListener listener = new ParseListener(queryPrefix);

    try {/*  w w w .  jav  a2s. c  o  m*/
        UriContext ctx = parser.uri();

        ParseTreeWalker.DEFAULT.walk(listener, ctx);

        return listener.getParsedQuery();
    } catch (ParseCancellationException e) {
        Throwable error = e.getCause();
        if (error instanceof RecognitionException) {
            RecognitionException re = (RecognitionException) error;
            int errorIndex = indexPrefixSize + re.getOffendingToken().getTokenIndex();
            String errorToken = re.getOffendingToken().getText();
            String expectedAlternatives = re.getExpectedTokens() == null ? "none"
                    : re.getExpectedTokens().toString(HawkularInventoryGetUriLexer.VOCABULARY);

            throw new IllegalArgumentException("Illegal inventory traversal URL. Token '" + errorToken
                    + "' on index " + errorIndex + " is not legal. Expected " + expectedAlternatives);
        } else {
            throw new IllegalArgumentException(
                    "Illegal inventory traversal URL. Error message: " + e.getCause().getMessage(),
                    e.getCause());
        }
    }
}