Example usage for org.antlr.v4.runtime Parser getRuleInvocationStack

List of usage examples for org.antlr.v4.runtime Parser getRuleInvocationStack

Introduction

In this page you can find the example usage for org.antlr.v4.runtime Parser getRuleInvocationStack.

Prototype

public List<String> getRuleInvocationStack() 

Source Link

Document

Return List<String> of the rule names in your parser instance leading up to a call to the current rule.

Usage

From source file:net.certiv.json.parser.JsonErrorListener.java

License:Open Source License

@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
        String msg, RecognitionException e) {

    Parser parser = (Parser) recognizer;
    String name = parser.getSourceName();
    TokenStream tokens = parser.getInputStream();

    Token offSymbol = (Token) offendingSymbol;
    int thisError = offSymbol.getTokenIndex();
    if (offSymbol.getType() == -1 && thisError == tokens.size() - 1) {
        Log.debug(this, name + ": Incorrect error: " + msg);
        return;//from w ww.j a  v  a  2s  . co m
    }
    String offSymName = JsonLexer.VOCABULARY.getSymbolicName(offSymbol.getType());
    if (thisError > lastError + 10) {
        lastError = thisError - 10;
    }
    for (int idx = lastError + 1; idx <= thisError; idx++) {
        Token token = tokens.get(idx);
        if (token.getChannel() != Token.HIDDEN_CHANNEL)
            Log.error(this, name + ":" + token.toString());
    }
    lastError = thisError;

    List<String> stack = parser.getRuleInvocationStack();
    Collections.reverse(stack);

    Log.error(this, name + " rule stack: " + stack);
    Log.error(this, name + " line " + line + ":" + charPositionInLine + " at " + offSymName + ": " + msg);
}