1 moduleantlr.v4.runtime.tree.ParseTreeWalker;
2 3 importantlr.v4.runtime.ParserRuleContext;
4 importantlr.v4.runtime.tree.ErrorNode;
5 importantlr.v4.runtime.tree.RuleNode;
6 importantlr.v4.runtime.tree.TerminalNode;
7 importantlr.v4.runtime.tree.ParseTreeWalker;
8 importantlr.v4.runtime.tree.ParseTreeListener;
9 importantlr.v4.runtime.tree.ParseTree;
10 11 classParseTreeWalker12 {
13 publicstaticimmutableParseTreeWalkerDEFAULT;
14 15 /**
16 * Performs a walk on the given parse tree starting at the root and going down recursively
17 * with depth-first search. On each node, {@link ParseTreeWalker#enterRule} is called before
18 * recursively walking down into child nodes, then
19 * {@link ParseTreeWalker#exitRule} is called after the recursive call to wind up.
20 * @param listener The listener used by the walker to process grammar rules
21 * @param t The parse tree to be walked on
22 */23 publicvoidwalk(ParseTreeListenerlistener, ParseTreet)
24 {
25 if (cast(ErrorNode) t) {
26 listener.visitErrorNode(cast(ErrorNode)t);
27 return;
28 }
29 elseif (cast(TerminalNode) t) {
30 listener.visitTerminal(cast(TerminalNode)t);
31 return;
32 }
33 RuleNoder = cast(RuleNode) t;
34 enterRule(listener, r);
35 intn = r.getChildCount();
36 for (inti = 0; i<n; i++) {
37 walk(listener, r.getChild(i));
38 }
39 exitRule(listener, r);
40 }
41 42 /**
43 * Enters a grammar rule by first triggering the generic event {@link ParseTreeListener#enterEveryRule}
44 * then by triggering the event specific to the given parse tree node
45 * @param listener The listener responding to the trigger events
46 * @param r The grammar rule containing the rule context
47 */48 protectedvoidenterRule(ParseTreeListenerlistener, RuleNoder)
49 {
50 ParserRuleContextctx = cast(ParserRuleContext) r.getRuleContext();
51 listener.enterEveryRule(ctx);
52 ctx.enterRule(listener);
53 }
54 55 /**
56 * Exits a grammar rule by first triggering the event specific to the given parse tree node
57 * then by triggering the generic event {@link ParseTreeListener#exitEveryRule}
58 * @param listener The listener responding to the trigger events
59 * @param r The grammar rule containing the rule context
60 */61 publicvoidexitRule(ParseTreeListenerlistener, RuleNoder)
62 {
63 ParserRuleContextctx = cast(ParserRuleContext) r.getRuleContext();
64 ctx.exitRule(listener);
65 listener.exitEveryRule(ctx);
66 }
67 }