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