aboutsummaryrefslogtreecommitdiff
path: root/src/monkey/seaspider/org/gnunet/seaspider/parser/visitors/TreeDumper.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/monkey/seaspider/org/gnunet/seaspider/parser/visitors/TreeDumper.java')
-rw-r--r--src/monkey/seaspider/org/gnunet/seaspider/parser/visitors/TreeDumper.java132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/monkey/seaspider/org/gnunet/seaspider/parser/visitors/TreeDumper.java b/src/monkey/seaspider/org/gnunet/seaspider/parser/visitors/TreeDumper.java
new file mode 100644
index 000000000..c2e7c1558
--- /dev/null
+++ b/src/monkey/seaspider/org/gnunet/seaspider/parser/visitors/TreeDumper.java
@@ -0,0 +1,132 @@
1//
2// Generated by JTB 1.3.2
3//
4package org.gnunet.seaspider.parser.visitors;
5
6import org.gnunet.seaspider.parser.nodes.*;
7
8import java.util.*;
9import java.io.*;
10
11/**
12 * Dumps the syntax tree to a Writer using the location information in
13 * each NodeToken.
14 */
15public class TreeDumper extends DepthFirstVisitor {
16 protected PrintWriter out;
17 private int curLine = 1;
18 private int curColumn = 1;
19 private boolean startAtNextToken = false;
20 private boolean printSpecials = true;
21
22 /**
23 * The default constructor uses System.out as its output location.
24 * You may specify your own Writer or OutputStream using one of the
25 * other constructors.
26 */
27 public TreeDumper() { out = new PrintWriter(System.out, true); }
28 public TreeDumper(Writer o) { out = new PrintWriter(o, true); }
29 public TreeDumper(OutputStream o) { out = new PrintWriter(o, true); }
30
31 /**
32 * Flushes the OutputStream or Writer that this TreeDumper is using.
33 */
34 public void flushWriter() { out.flush(); }
35
36 /**
37 * Allows you to specify whether or not to print special tokens.
38 */
39 public void printSpecials(boolean b) { printSpecials = b; }
40
41 /**
42 * Starts the tree dumper on the line containing the next token
43 * visited. For example, if the next token begins on line 50 and the
44 * dumper is currently on line 1 of the file, it will set its current
45 * line to 50 and continue printing from there, as opposed to
46 * printing 49 blank lines and then printing the token.
47 */
48 public void startAtNextToken() { startAtNextToken = true; }
49
50 /**
51 * Resets the position of the output "cursor" to the first line and
52 * column. When using a dumper on a syntax tree more than once, you
53 * either need to call this method or startAtNextToken() between each
54 * dump.
55 */
56 public void resetPosition() { curLine = curColumn = 1; }
57
58 /**
59 * Dumps the current NodeToken to the output stream being used.
60 *
61 * @throws IllegalStateException if the token position is invalid
62 * relative to the current position, i.e. its location places it
63 * before the previous token.
64 */
65 public void visit(NodeToken n) {
66 if ( n.beginLine == -1 || n.beginColumn == -1 ) {
67 printToken(n.tokenImage);
68 return;
69 }
70
71 //
72 // Handle special tokens
73 //
74 if ( printSpecials && n.numSpecials() > 0 )
75 for ( Enumeration<NodeToken> e = n.specialTokens.elements(); e.hasMoreElements(); )
76 visit(e.nextElement());
77
78 //
79 // Handle startAtNextToken option
80 //
81 if ( startAtNextToken ) {
82 curLine = n.beginLine;
83 curColumn = 1;
84 startAtNextToken = false;
85
86 if ( n.beginColumn < curColumn )
87 out.println();
88 }
89
90 //
91 // Check for invalid token position relative to current position.
92 //
93 if ( n.beginLine < curLine )
94 throw new IllegalStateException("at token \"" + n.tokenImage +
95 "\", n.beginLine = " + Integer.toString(n.beginLine) +
96 ", curLine = " + Integer.toString(curLine));
97 else if ( n.beginLine == curLine && n.beginColumn < curColumn )
98 throw new IllegalStateException("at token \"" + n.tokenImage +
99 "\", n.beginColumn = " +
100 Integer.toString(n.beginColumn) + ", curColumn = " +
101 Integer.toString(curColumn));
102
103 //
104 // Move output "cursor" to proper location, then print the token
105 //
106 if ( curLine < n.beginLine ) {
107 curColumn = 1;
108 for ( ; curLine < n.beginLine; ++curLine )
109 out.println();
110 }
111
112 for ( ; curColumn < n.beginColumn; ++curColumn )
113 out.print(" ");
114
115 printToken(n.tokenImage);
116 }
117
118 private void printToken(String s) {
119 for ( int i = 0; i < s.length(); ++i ) {
120 if ( s.charAt(i) == '\n' ) {
121 ++curLine;
122 curColumn = 1;
123 }
124 else
125 curColumn++;
126
127 out.print(s.charAt(i));
128 }
129
130 out.flush();
131 }
132}