01: package antlr;
02:
03: /* ANTLR Translator Generator
04: * Project led by Terence Parr at http://www.cs.usfca.edu
05: * Software rights: http://www.antlr.org/license.html
06: */
07:
08: import java.io.*;
09:
10: import antlr.collections.AST;
11:
12: /** Simple class to dump the contents of an AST to the output */
13: public class DumpASTVisitor implements ASTVisitor {
14: protected int level = 0;
15:
16: private void tabs() {
17: for (int i = 0; i < level; i++) {
18: System.out.print(" ");
19: }
20: }
21:
22: public void visit(AST node) {
23: // Flatten this level of the tree if it has no children
24: boolean flatten = /*true*/false;
25: AST node2;
26: for (node2 = node; node2 != null; node2 = node2
27: .getNextSibling()) {
28: if (node2.getFirstChild() != null) {
29: flatten = false;
30: break;
31: }
32: }
33:
34: for (node2 = node; node2 != null; node2 = node2
35: .getNextSibling()) {
36: if (!flatten || node2 == node) {
37: tabs();
38: }
39: if (node2.getText() == null) {
40: System.out.print("nil");
41: } else {
42: System.out.print(node2.getText());
43: }
44:
45: System.out.print(" [" + node2.getType() + "] ");
46:
47: if (flatten) {
48: System.out.print(" ");
49: } else {
50: System.out.println("");
51: }
52:
53: if (node2.getFirstChild() != null) {
54: level++;
55: visit(node2.getFirstChild());
56: level--;
57: }
58: }
59:
60: if (flatten) {
61: System.out.println("");
62: }
63: }
64: }
|