checking in changes
[IRC.git] / Robust / src / t.test
1 public class ParseNode {
2
3     private String label;
4     private ParseNode parent;
5     private ParseNodeVector children;
6     private int line=-1;
7     private Object literal;
8
9     //private SymbolTable st;
10
11     public ParseNode(String label) {
12         this.label = label;
13         this.line = -1;
14         this.parent = null;
15         this.literal=null;
16         children = new ParseNodeVector();
17     }
18
19     public ParseNode ( String label, int line ) {
20         this.label = label;
21         this.line = line;
22         this.parent = null;
23         this.literal=null;
24         children = new ParseNodeVector();
25     }
26
27     public void setLabel( String label ) {
28         this.label = label;
29     }
30
31     public String getLabel() {
32         return label;
33     }
34
35     public void setLiteral(Object o) {
36         literal=o;
37     }
38
39     public Object getLiteral(Object o) {
40         return literal;
41     }
42
43     /*
44     public void setSymbolTable(SymbolTable st) {
45         if (st == null) {
46             throw new IRException("symboltable is null!");
47         }
48         this.st = st;
49     }
50
51     public SymbolTable getSymbolTable() {
52         if (st == null) {
53             if (parent != null) {
54                 return parent.getSymbolTable();
55             } else {
56                 return null;
57             }
58         } else {
59             return st;
60         }
61     }
62     */
63
64     public int getLine() {
65         if (line >= 0) {
66             return line;
67         } else {
68             if (parent != null) {
69                 return parent.getLine();
70             } else {
71                 return 0;
72             }
73         }
74     }
75
76     public void setParent( ParseNode parent ) {
77         this.parent = parent;
78     }
79
80     public ParseNode getParent() {
81         return parent;
82     }
83
84     public ParseNode insertChild(ParseNode child) {
85         if (child == null) {
86         }
87
88         children.insertElementAt(child, 0);
89         child.setParent(this);
90         return child;
91     }
92
93     public ParseNode insertChild(String newlabel) {
94         ParseNode child = new ParseNode(newlabel, -1);
95         return insertChild(child);
96     }
97
98     public ParseNode addChild( ParseNode child ) {
99
100         if (child == null) {
101         }
102
103         children.addElement (child);
104         child.setParent(this);
105         return child;
106     }
107
108     public ParseNode addChild( String newlabel ) {
109         
110         ParseNode child = new ParseNode(newlabel, -1);
111         children.addElement(child);
112         child.setParent(this);
113         return child;
114     }
115
116     public ParseNode addChild (String newlabel, int line) {
117         ParseNode child = new ParseNode(newlabel, line);
118         children.addElement(child);
119         child.setParent(this);
120         return child;
121     }
122
123     public ParseNodeVector getChildren() {
124         return children;
125     }
126
127     public ParseNode getChild (String label) {
128         int i;
129         ParseNode p;
130
131         for (i = 0; i < children.size(); i++) {
132             p = children.elementAt(i);
133             if (p.getLabel().equals(label)) {
134                 return p;
135             }
136         }
137
138         return null;
139     }
140
141     public ParseNode getRoot() {
142         if (parent==null)
143         return  this; else return parent.getRoot();
144     }
145
146     public String getTerminal () {
147         ParseNode pn = children.elementAt(0);
148         if (pn == null) {
149             return null;
150         } else {
151             return pn.getLabel();
152         }
153     }
154
155
156     public ParseNodeVector getChildren(String label) {
157         int i;
158         ParseNodeVector v = new ParseNodeVector();
159
160         for (i = 0; i < children.size(); i++) {
161             ParseNode pn = children.elementAt(i);
162             if (pn.getLabel().equals(label))
163                 v.addElement(pn);
164         }
165
166         return v;
167     }
168
169     public String getNodeName() {
170         return label + " - " + getLine();
171     }
172
173     public int getNeighborCount() {
174         return children.size();
175     }
176
177     public Object getNeighbor(int index) {
178         return children.elementAt(index);
179     }
180
181     public String doIndent(int indent) {
182
183         String output = new String();
184         for(int i=0;i<indent;i++) output += " ";
185         return output;
186     }
187
188     public String PPrint(int indent, boolean recursive) {
189
190         String output = new String();
191
192         if (children.size()==0) {
193             output += doIndent(indent) + "<" + label + "/>\n";
194         } else {
195             output += doIndent(indent) + "<" + label + ">\n";
196             indent += 2;
197             
198             if (recursive) {
199                 for (int i = 0; i < children.size(); i++) {
200                     Walkable w = (Walkable)children.elementAt(i);
201                     output += w.PPrint(indent, true);
202                 }
203             } else {
204                 for (int i = 0; i < children.size(); i++) {
205                     Walkable w = (Walkable)children.elementAt(i);
206                     output += doIndent(indent) + "<" + w.getNodeName() + "/>\n";
207                 }          
208             }
209             
210             indent -= 2;
211             output += doIndent(indent) + "</" + label + ">\n";
212         }
213
214         return output;  
215     }
216
217 }
218
219 public class String {}
220
221 public class ParseNodeVector {}
222
223 public class Object {}