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