1 | using System; |
---|
2 | |
---|
3 | using AST = antlr.collections.AST; |
---|
4 | |
---|
5 | namespace antlr |
---|
6 | { |
---|
7 | /* ANTLR Translator Generator |
---|
8 | * Project led by Terence Parr at http://www.jGuru.com |
---|
9 | * Software rights: http://www.antlr.org/license.html |
---|
10 | * |
---|
11 | * $Id:$ |
---|
12 | */ |
---|
13 | |
---|
14 | // |
---|
15 | // ANTLR C# Code Generator by Micheal Jordan |
---|
16 | // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com |
---|
17 | // Anthony Oguntimehin |
---|
18 | // |
---|
19 | // With many thanks to Eric V. Smith from the ANTLR list. |
---|
20 | // |
---|
21 | |
---|
22 | /// <summary> |
---|
23 | /// Summary description for DumpASTVisitor. |
---|
24 | /// </summary> |
---|
25 | /** Simple class to dump the contents of an AST to the output */ |
---|
26 | public class DumpASTVisitor : ASTVisitor |
---|
27 | { |
---|
28 | protected int level = 0; |
---|
29 | |
---|
30 | |
---|
31 | private void tabs() |
---|
32 | { |
---|
33 | for (int i = 0; i < level; i++) |
---|
34 | { |
---|
35 | Console.Out.Write(" "); |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | public void visit(AST node) |
---|
40 | { |
---|
41 | // Flatten this level of the tree if it has no children |
---|
42 | bool flatten = /*true*/ false; |
---|
43 | AST node2; |
---|
44 | for (node2 = node; node2 != null; node2 = node2.getNextSibling()) |
---|
45 | { |
---|
46 | if (node2.getFirstChild() != null) |
---|
47 | { |
---|
48 | flatten = false; |
---|
49 | break; |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | for (node2 = node; node2 != null; node2 = node2.getNextSibling()) |
---|
54 | { |
---|
55 | if (!flatten || node2 == node) |
---|
56 | { |
---|
57 | tabs(); |
---|
58 | } |
---|
59 | if (node2.getText() == null) |
---|
60 | { |
---|
61 | Console.Out.Write("nil"); |
---|
62 | } |
---|
63 | else |
---|
64 | { |
---|
65 | Console.Out.Write(node2.getText()); |
---|
66 | } |
---|
67 | |
---|
68 | Console.Out.Write(" [" + node2.Type + "] "); |
---|
69 | |
---|
70 | if (flatten) |
---|
71 | { |
---|
72 | Console.Out.Write(" "); |
---|
73 | } |
---|
74 | else |
---|
75 | { |
---|
76 | Console.Out.WriteLine(""); |
---|
77 | } |
---|
78 | |
---|
79 | if (node2.getFirstChild() != null) |
---|
80 | { |
---|
81 | level++; |
---|
82 | visit(node2.getFirstChild()); |
---|
83 | level--; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | if (flatten) |
---|
88 | { |
---|
89 | Console.Out.WriteLine(""); |
---|
90 | } |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | |
---|