5 Purpose: ParseNode is used to represent a parse production
9 package iotpolicy.tree;
13 public class ParseNode implements Walkable {
16 private ParseNode parent;
17 private ParseNodeVector children;
19 private Object literal;
21 //private SymbolTable st;
23 public ParseNode(String label) {
28 children = new ParseNodeVector();
31 public ParseNode (String label, int line) {
36 children = new ParseNodeVector();
39 public void setLabel(String label) {
43 public String getLabel() {
47 public void setLiteral(Object o) {
51 public Object getLiteral() {
56 public void setSymbolTable(SymbolTable st) {
58 throw new IRException("symboltable is null!");
63 public SymbolTable getSymbolTable() {
66 return parent.getSymbolTable();
76 public int getLine() {
81 return parent.getLine();
88 public void setParent(ParseNode parent) {
92 public ParseNode getParent() {
96 public ParseNode insertChild(ParseNode child) {
98 throw new NullPointerException("Can't add null node to parse tree");
101 children.insertElementAt(child, 0);
102 child.setParent(this);
106 public ParseNode insertChild(String newlabel) {
107 ParseNode child = new ParseNode(newlabel, -1);
108 return insertChild(child);
111 public ParseNode addChild(ParseNode child) {
114 throw new NullPointerException("Can't add null node to parse tree: "+getLabel());
117 children.addElement(child);
118 child.setParent(this);
122 public ParseNode addChild(String newlabel) {
124 ParseNode child = new ParseNode(newlabel, -1);
125 children.addElement(child);
126 child.setParent(this);
130 public ParseNode addChild(String newlabel, int line) {
131 ParseNode child = new ParseNode(newlabel, line);
132 children.addElement(child);
133 child.setParent(this);
137 public ParseNodeVector getChildren() {
141 public ParseNode getChild(String label) {
145 for (i = 0; i < children.size(); i++) {
146 p = children.elementAt(i);
147 if (p.getLabel().equals(label)) {
155 public ParseNode getRoot() {
156 return (parent == null)?this:parent.getRoot();
159 public String getTerminal() {
160 ParseNode pn = children.elementAt(0);
164 return pn.getLabel();
168 public ParseNode getFirstChild() {
169 return children.elementAt(0);
172 public ParseNodeVector getChildren(String label) {
174 ParseNodeVector v = new ParseNodeVector();
176 for (i = 0; i < children.size(); i++) {
177 ParseNode pn = children.elementAt(i);
178 if (pn.getLabel().equals(label))
185 public String getNodeName() {
186 return label + " - " + getLine();
189 public int getNeighborCount() {
190 return children.size();
193 public Object getNeighbor(int index) {
194 return children.elementAt(index);
197 public String doIndent(int indent) {
199 String output = new String();
200 for(int i=0; i<indent; i++) output += " ";
204 public String PPrint(int indent, boolean recursive) {
206 String output = new String();
208 if (children.size()==0) {
209 output += doIndent(indent) + "<" + label + "/>\n";
211 output += doIndent(indent) + "<" + label + ">\n";
215 for (int i = 0; i < children.size(); i++) {
216 Walkable w = (Walkable)children.elementAt(i);
217 output += w.PPrint(indent, true);
220 for (int i = 0; i < children.size(); i++) {
221 Walkable w = (Walkable)children.elementAt(i);
222 output += doIndent(indent) + "<" + w.getNodeName() + "/>\n";
227 output += doIndent(indent) + "</" + label + ">\n";