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);
}
}
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);
}
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());
}
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);
}
}
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);
}
}
Aggregations