Search in sources :

Example 1 with ParseTreeListener

use of org.antlr.v4.runtime.tree.ParseTreeListener in project antlr4 by antlr.

the class IterativeParseTreeWalker method walk.

@Override
public void walk(ParseTreeListener listener, ParseTree t) {
    final Deque<ParseTree> nodeStack = new ArrayDeque<ParseTree>();
    final IntegerStack indexStack = new IntegerStack();
    ParseTree currentNode = t;
    int currentIndex = 0;
    while (currentNode != null) {
        // pre-order visit
        if (currentNode instanceof ErrorNode) {
            listener.visitErrorNode((ErrorNode) currentNode);
        } else if (currentNode instanceof TerminalNode) {
            listener.visitTerminal((TerminalNode) currentNode);
        } else {
            final RuleNode r = (RuleNode) currentNode;
            enterRule(listener, r);
        }
        // Move down to first child, if exists
        if (currentNode.getChildCount() > 0) {
            nodeStack.push(currentNode);
            indexStack.push(currentIndex);
            currentIndex = 0;
            currentNode = currentNode.getChild(0);
            continue;
        }
        // No child nodes, so walk tree
        do {
            // post-order visit
            if (currentNode instanceof RuleNode) {
                exitRule(listener, (RuleNode) currentNode);
            }
            // No parent, so no siblings
            if (nodeStack.isEmpty()) {
                currentNode = null;
                currentIndex = 0;
                break;
            }
            // Move to next sibling if possible
            currentNode = nodeStack.peek().getChild(++currentIndex);
            if (currentNode != null) {
                break;
            }
            // No next, sibling, so move up
            currentNode = nodeStack.pop();
            currentIndex = indexStack.pop();
        } while (currentNode != null);
    }
}
Also used : IntegerStack(org.antlr.v4.runtime.misc.IntegerStack) ArrayDeque(java.util.ArrayDeque)

Example 2 with ParseTreeListener

use of org.antlr.v4.runtime.tree.ParseTreeListener in project antlr4 by antlr.

the class ParseTreeWalker method exitRule.

protected void exitRule(ParseTreeListener listener, RuleNode r) {
    ParserRuleContext ctx = (ParserRuleContext) r.getRuleContext();
    ctx.exitRule(listener);
    listener.exitEveryRule(ctx);
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext)

Example 3 with ParseTreeListener

use of org.antlr.v4.runtime.tree.ParseTreeListener in project antlr4 by antlr.

the class BaseCppTest method writeParserTestFile.

protected void writeParserTestFile(String parserName, String lexerName, String listenerName, String visitorName, String parserStartRuleName, boolean debug, boolean trace) {
    if (!parserStartRuleName.endsWith(")"))
        parserStartRuleName += "()";
    ST outputFileST = new ST("#include \\<iostream>\n" + "\n" + "#include \"antlr4-runtime.h\"\n" + "#include \"<lexerName>.h\"\n" + "#include \"<parserName>.h\"\n" + "\n" + "using namespace antlr4;\n" + "\n" + "class TreeShapeListener : public tree::ParseTreeListener {\n" + "public:\n" + "  void visitTerminal(tree::TerminalNode *) override {}\n" + "  void visitErrorNode(tree::ErrorNode *) override {}\n" + "  void exitEveryRule(ParserRuleContext *) override {}\n" + "  void enterEveryRule(ParserRuleContext *ctx) override {\n" + "    for (auto child : ctx->children) {\n" + "      tree::ParseTree *parent = child->parent;\n" + "      ParserRuleContext *rule = dynamic_cast\\<ParserRuleContext *>(parent);\n" + "      if (rule != ctx) {\n" + "        throw \"Invalid parse tree shape detected.\";\n" + "      }\n" + "    }\n" + "  }\n" + "};\n" + "\n" + "\n" + "int main(int argc, const char* argv[]) {\n" + "  ANTLRFileStream input(argv[1]);\n" + "  <lexerName> lexer(&input);\n" + "  CommonTokenStream tokens(&lexer);\n" + "<createParser>" + "\n" + "  tree::ParseTree *tree = parser.<parserStartRuleName>;\n" + "  TreeShapeListener listener;\n" + "  tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);\n" + "\n" + "  return 0;\n" + "}\n");
    String stSource = "  <parserName> parser(&tokens);\n";
    if (debug) {
        stSource += "  DiagnosticErrorListener errorListener;\n";
        stSource += "  parser.addErrorListener(&errorListener);\n";
    }
    if (trace)
        stSource += "  parser.setTrace(true);\n";
    ST createParserST = new ST(stSource);
    outputFileST.add("createParser", createParserST);
    outputFileST.add("parserName", parserName);
    outputFileST.add("lexerName", lexerName);
    outputFileST.add("listenerName", listenerName);
    outputFileST.add("visitorName", visitorName);
    outputFileST.add("parserStartRuleName", parserStartRuleName);
    writeFile(tmpdir, "Test.cpp", outputFileST.render());
}
Also used : ST(org.stringtemplate.v4.ST) STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)

Example 4 with ParseTreeListener

use of org.antlr.v4.runtime.tree.ParseTreeListener in project antlr4 by antlr.

the class Parser method triggerExitRuleEvent.

/**
	 * Notify any parse listeners of an exit rule event.
	 *
	 * @see #addParseListener
	 */
protected void triggerExitRuleEvent() {
    // reverse order walk of listeners
    for (int i = _parseListeners.size() - 1; i >= 0; i--) {
        ParseTreeListener listener = _parseListeners.get(i);
        _ctx.exitRule(listener);
        listener.exitEveryRule(_ctx);
    }
}
Also used : ParseTreeListener(org.antlr.v4.runtime.tree.ParseTreeListener)

Example 5 with ParseTreeListener

use of org.antlr.v4.runtime.tree.ParseTreeListener in project antlr4 by antlr.

the class Parser method triggerEnterRuleEvent.

/**
	 * Notify any parse listeners of an enter rule event.
	 *
	 * @see #addParseListener
	 */
protected void triggerEnterRuleEvent() {
    for (ParseTreeListener listener : _parseListeners) {
        listener.enterEveryRule(_ctx);
        _ctx.enterRule(listener);
    }
}
Also used : ParseTreeListener(org.antlr.v4.runtime.tree.ParseTreeListener)

Aggregations

ParseTreeListener (org.antlr.v4.runtime.tree.ParseTreeListener)4 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2 File (java.io.File)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 URLClassLoader (java.net.URLClassLoader)1 ArrayDeque (java.util.ArrayDeque)1 ExecutionException (java.util.concurrent.ExecutionException)1 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)1 CharStream (org.antlr.v4.runtime.CharStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 Lexer (org.antlr.v4.runtime.Lexer)1 Parser (org.antlr.v4.runtime.Parser)1 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)1 RecognitionException (org.antlr.v4.runtime.RecognitionException)1 Token (org.antlr.v4.runtime.Token)1 TokenSource (org.antlr.v4.runtime.TokenSource)1 ATN (org.antlr.v4.runtime.atn.ATN)1