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

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

Introduction

In this page you can find the example usage for org.antlr.v4.runtime RecognitionException getOffendingState.

Prototype

public int getOffendingState() 

Source Link

Document

Get the ATN state number the parser was in at the time the error occurred.

Usage

From source file:com.facebook.presto.sql.parser.ErrorHandler.java

License:Apache License

@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
        String message, RecognitionException e) {
    try {//from www.  j a v  a2s .co m
        Parser parser = (Parser) recognizer;

        ATN atn = parser.getATN();

        ATNState currentState;
        Token currentToken;
        RuleContext context;

        if (e != null) {
            currentState = atn.states.get(e.getOffendingState());
            currentToken = e.getOffendingToken();
            context = e.getCtx();

            if (e instanceof NoViableAltException) {
                currentToken = ((NoViableAltException) e).getStartToken();
            }
        } else {
            currentState = atn.states.get(parser.getState());
            currentToken = parser.getCurrentToken();
            context = parser.getContext();
        }

        Analyzer analyzer = new Analyzer(atn, parser.getVocabulary(), specialRules, specialTokens, ignoredRules,
                parser.getTokenStream());
        Multimap<Integer, String> candidates = analyzer.process(currentState, currentToken.getTokenIndex(),
                context);

        // pick the candidate tokens associated largest token index processed (i.e., the path that consumed the most input)
        String expected = candidates.asMap().entrySet().stream().max(Comparator.comparing(Map.Entry::getKey))
                .get().getValue().stream().sorted().collect(Collectors.joining(", "));

        message = String.format("mismatched input '%s'. Expecting: %s", ((Token) offendingSymbol).getText(),
                expected);
    } catch (Exception exception) {
        LOG.error(exception,
                "Unexpected failure when handling parsing error. This is likely a bug in the implementation");
    }

    throw new ParsingException(message, e, line, charPositionInLine);
}

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  w w  .  ja v  a2  s . c o 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();
}