KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > DumpASTVisitor


1 package persistence.antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/license.html
6  *
7  */

8
9 import java.io.*;
10
11 import persistence.antlr.collections.AST;
12
13 /** Simple class to dump the contents of an AST to the output */
14 public class DumpASTVisitor implements ASTVisitor {
15     protected int level = 0;
16
17
18     private void tabs() {
19         for (int i = 0; i < level; i++) {
20             System.out.print(" ");
21         }
22     }
23
24     public void visit(AST node) {
25         // Flatten this level of the tree if it has no children
26
boolean flatten = /*true*/ false;
27         AST node2;
28         for (node2 = node; node2 != null; node2 = node2.getNextSibling()) {
29             if (node2.getFirstChild() != null) {
30                 flatten = false;
31                 break;
32             }
33         }
34
35         for (node2 = node; node2 != null; node2 = node2.getNextSibling()) {
36             if (!flatten || node2 == node) {
37                 tabs();
38             }
39             if (node2.getText() == null) {
40                 System.out.print("nil");
41             }
42             else {
43                 System.out.print(node2.getText());
44             }
45
46             System.out.print(" [" + node2.getType() + "] ");
47
48             if (flatten) {
49                 System.out.print(" ");
50             }
51             else {
52                 System.out.println("");
53             }
54
55             if (node2.getFirstChild() != null) {
56                 level++;
57                 visit(node2.getFirstChild());
58                 level--;
59             }
60         }
61
62         if (flatten) {
63             System.out.println("");
64         }
65     }
66 }
67
Popular Tags