01: package persistence.antlr;
02:
03: /* ANTLR Translator Generator
04: * Project led by Terence Parr at http://www.jGuru.com
05: * Software rights: http://www.antlr.org/license.html
06: *
07: */
08:
09: 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: private void tabs() {
18: for (int i = 0; i < level; i++) {
19: System.out.print(" ");
20: }
21: }
22:
23: public void visit(AST node) {
24: // Flatten this level of the tree if it has no children
25: boolean flatten = /*true*/false;
26: AST node2;
27: for (node2 = node; node2 != null; node2 = node2
28: .getNextSibling()) {
29: if (node2.getFirstChild() != null) {
30: flatten = false;
31: break;
32: }
33: }
34:
35: for (node2 = node; node2 != null; node2 = node2
36: .getNextSibling()) {
37: if (!flatten || node2 == node) {
38: tabs();
39: }
40: if (node2.getText() == null) {
41: System.out.print("nil");
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: } else {
51: System.out.println("");
52: }
53:
54: if (node2.getFirstChild() != null) {
55: level++;
56: visit(node2.getFirstChild());
57: level--;
58: }
59: }
60:
61: if (flatten) {
62: System.out.println("");
63: }
64: }
65: }
|