Example usage for org.antlr.v4.runtime ParserRuleContext toInfoString

List of usage examples for org.antlr.v4.runtime ParserRuleContext toInfoString

Introduction

In this page you can find the example usage for org.antlr.v4.runtime ParserRuleContext toInfoString.

Prototype

public String toInfoString(Parser recognizer) 

Source Link

Document

Used for rule context info debugging during parse-time, not so much for ATN debugging

Usage

From source file:android.databinding.tool.ExpressionParser.java

License:Apache License

public Expr parse(String input, @Nullable Location locationInFile) {
    ANTLRInputStream inputStream = new ANTLRInputStream(input);
    BindingExpressionLexer lexer = new BindingExpressionLexer(inputStream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    final BindingExpressionParser parser = new BindingExpressionParser(tokenStream);
    BindingExpressionParser.BindingSyntaxContext root = parser.bindingSyntax();
    try {/*from  www .j  a v a 2 s.co m*/
        mModel.setCurrentLocationInFile(locationInFile);
        visitor.setParseTreeListener(new ParseTreeListener() {
            List<ParserRuleContext> mStack = new ArrayList<ParserRuleContext>();

            @Override
            public void visitTerminal(TerminalNode node) {
            }

            @Override
            public void visitErrorNode(ErrorNode node) {
            }

            @Override
            public void enterEveryRule(ParserRuleContext ctx) {
                mStack.add(ctx);
                mModel.setCurrentParserContext(ctx);
            }

            @Override
            public void exitEveryRule(ParserRuleContext ctx) {
                Preconditions.check(ctx == mStack.get(mStack.size() - 1),
                        "Inconsistent exit from context. Received %s, expecting %s", ctx.toInfoString(parser),
                        mStack.get(mStack.size() - 1).toInfoString(parser));
                mStack.remove(mStack.size() - 1);
                if (mStack.size() > 0) {
                    mModel.setCurrentParserContext(mStack.get(mStack.size() - 1));
                } else {
                    mModel.setCurrentParserContext(null);
                }
            }
        });
        return root.accept(visitor);
    } finally {
        mModel.setCurrentLocationInFile(null);
    }
}