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

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

Introduction

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

Prototype

public RuleContext getCtx() 

Source Link

Document

Gets the RuleContext at the time this exception was thrown.

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  w ww . j av a  2  s. c  om
        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:excecoes.SyntaxError.java

public SyntaxError(String message, RecognitionException e, int line, int charPositionInLine) {
    super(message, e.getRecognizer(), e.getInputStream(), (ParserRuleContext) e.getCtx());
    this.setOffendingToken(e.getOffendingToken());
    this.initCause(e);
    this.setLine(line);
    this.setCharPositionInLine(charPositionInLine);

}

From source file:no.ssb.vtl.script.support.SyntaxErrorListener.java

License:Apache License

@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int startLine, int startColumn,
        String msg, RecognitionException e) {
    VTLScriptException vtlScriptException;
    // Use the context from the RecognitionException if available.
    if (e != null && e.getCtx() != null) {
        vtlScriptException = new VTLScriptException(msg, (ParserRuleContext) e.getCtx());
    } else {/*from  w  w w .j av  a  2 s .c  o  m*/
        int stopColumn = startColumn;
        if (offendingSymbol instanceof Token) {
            Token symbol = (Token) offendingSymbol;
            int start = symbol.getStartIndex();
            int stop = symbol.getStopIndex();
            if (start >= 0 && stop >= 0) {
                stopColumn = startColumn + (stop - start) + 1;
            }
            vtlScriptException = new VTLScriptException(msg, startLine, startColumn, startLine, stopColumn);
        } else {
            vtlScriptException = new VTLScriptException(msg, startLine, startColumn);
        }
    }
    errorConsumer.accept(vtlScriptException);
}

From source file:org.kie.dmn.feel.parser.feel11.FEELParser.java

License:Apache License

private static SyntaxErrorEvent generateInvalidVariableError(Object offendingSymbol, int line,
        int charPositionInLine, RecognitionException e, CommonToken token) {
    String chars = token.getText().length() == 1 ? "character" : "sequence of characters";
    if (charPositionInLine == 0) {
        if ("in".equals(token.getText()) || REUSABLE_KEYWORDS.contains(token.getText())) {
            return new SyntaxErrorEvent(FEELEvent.Severity.ERROR,
                    Msg.createMessage(Msg.INVALID_VARIABLE_NAME_START, "keyword", token.getText()), e, line,
                    charPositionInLine, offendingSymbol);
        } else {/*  w  w  w.  j  a  v  a 2  s.c  o  m*/
            return new SyntaxErrorEvent(FEELEvent.Severity.ERROR,
                    Msg.createMessage(Msg.INVALID_VARIABLE_NAME_START, chars, token.getText()), e, line,
                    charPositionInLine, offendingSymbol);
        }
    } else if ("in".equals(token.getText())) {
        return new SyntaxErrorEvent(FEELEvent.Severity.ERROR,
                Msg.createMessage(Msg.INVALID_VARIABLE_NAME, "keyword", token.getText()), e, line,
                charPositionInLine, offendingSymbol);
    } else if ("}".equals(token.getText()) && e != null && e.getRecognizer() instanceof Parser
            && ((Parser) e.getRecognizer()).getRuleInvocationStack().contains("key")) {
        return new SyntaxErrorEvent(FEELEvent.Severity.ERROR,
                Msg.createMessage(Msg.MISSING_EXPRESSION, e.getCtx().getText()), e, line, charPositionInLine,
                offendingSymbol);
    } else {
        return new SyntaxErrorEvent(FEELEvent.Severity.ERROR,
                Msg.createMessage(Msg.INVALID_VARIABLE_NAME, chars, token.getText()), e, line,
                charPositionInLine, offendingSymbol);
    }
}