1 module antlr.v4.runtime.tree.ParseTreeWalker;
2 
3 import antlr.v4.runtime.ParserRuleContext;
4 import antlr.v4.runtime.tree.ErrorNode;
5 import antlr.v4.runtime.tree.RuleNode;
6 import antlr.v4.runtime.tree.TerminalNode;
7 import antlr.v4.runtime.tree.ParseTreeWalker;
8 import antlr.v4.runtime.tree.ParseTreeListener;
9 import antlr.v4.runtime.tree.ParseTree;
10 
11 /**
12  * TODO add class description
13  */
14 class ParseTreeWalker
15 {
16 
17     public static immutable ParseTreeWalker DEFAULT;
18 
19     public static this()
20     {
21     }
22 
23     public void walk(ParseTreeListener listener, ParseTree t)
24     {
25         if (cast(ErrorNode)t) {
26             listener.visitErrorNode(cast(ErrorNode)t);
27             return;
28         }
29         else if (cast(TerminalNode)t) {
30             listener.visitTerminal(cast(TerminalNode)t);
31             return;
32         }
33         RuleNode r = cast(RuleNode)t;
34         enterRule(listener, r);
35         int n = r.getChildCount();
36         for (int i = 0; i<n; i++) {
37             walk(listener, r.getChild(i));
38         }
39         exitRule(listener, r);
40     }
41 
42     protected void enterRule(ParseTreeListener listener, RuleNode r)
43     {
44         ParserRuleContext ctx = cast(ParserRuleContext)r.getRuleContext();
45         listener.enterEveryRule(ctx);
46         ctx.enterRule(listener);
47     }
48 
49     public void exitRule(ParseTreeListener listener, RuleNode r)
50     {
51         ParserRuleContext ctx = cast(ParserRuleContext)r.getRuleContext();
52         ctx.exitRule(listener);
53         listener.exitEveryRule(ctx);
54     }
55 
56 }