KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > parser > Node


1 /*
2  * Node.java
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2.1
7  * of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307, USA.
18  *
19  * Copyright (c) 2003-2005 Per Cederberg. All rights reserved.
20  */

21
22 package net.percederberg.grammatica.parser;
23
24 import java.io.PrintStream JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 /**
30  * An abstract parse tree node. This class is inherited by all nodes
31  * in the parse tree, i.e. by the token and production classes.
32  *
33  * @author Per Cederberg, <per at percederberg dot net>
34  * @version 1.2
35  */

36 public abstract class Node {
37
38     /**
39      * The parent node.
40      */

41     private Node parent = null;
42
43     /**
44      * The computed node values.
45      */

46     private ArrayList JavaDoc values = null;
47
48     /**
49      * Checks if this node is hidden, i.e. if it should not be visible
50      * outside the parser.
51      *
52      * @return true if the node should be hidden, or
53      * false otherwise
54      */

55     boolean isHidden() {
56         return false;
57     }
58
59     /**
60      * Returns the node type id. This value is set as a unique
61      * identifier for each type of node, in order to simplify later
62      * identification.
63      *
64      * @return the node type id
65      */

66     public abstract int getId();
67
68     /**
69      * Returns the node name.
70      *
71      * @return the node name
72      */

73     public abstract String JavaDoc getName();
74
75     /**
76      * The line number of the first character in this node. If the
77      * node has child elements, this value will be fetched from the
78      * first child.
79      *
80      * @return the line number of the first character, or
81      * -1 if not applicable
82      */

83     public int getStartLine() {
84         int line;
85
86         for (int i = 0; i < getChildCount(); i++) {
87             line = getChildAt(i).getStartLine();
88             if (line >= 0) {
89                 return line;
90             }
91         }
92         return -1;
93     }
94
95     /**
96      * The column number of the first character in this node. If the
97      * node has child elements, this value will be fetched from the
98      * first child.
99      *
100      * @return the column number of the first token character, or
101      * -1 if not applicable
102      */

103     public int getStartColumn() {
104         int col;
105
106         for (int i = 0; i < getChildCount(); i++) {
107             col = getChildAt(i).getStartColumn();
108             if (col >= 0) {
109                 return col;
110             }
111         }
112         return -1;
113     }
114
115     /**
116      * The line number of the last character in this node. If the node
117      * has child elements, this value will be fetched from the last
118      * child.
119      *
120      * @return the line number of the last token character, or
121      * -1 if not applicable
122      */

123     public int getEndLine() {
124         int line;
125
126         for (int i = getChildCount() - 1; i >= 0; i--) {
127             line = getChildAt(i).getEndLine();
128             if (line >= 0) {
129                 return line;
130             }
131         }
132         return -1;
133     }
134
135     /**
136      * The column number of the last character in this node. If the
137      * node has child elements, this value will be fetched from the
138      * last child.
139      *
140      * @return the column number of the last token character, or
141      * -1 if not applicable
142      */

143     public int getEndColumn() {
144         int col;
145
146         for (int i = getChildCount() - 1; i >= 0; i--) {
147             col = getChildAt(i).getEndColumn();
148             if (col >= 0) {
149                 return col;
150             }
151         }
152         return -1;
153     }
154
155     /**
156      * Returns the parent node.
157      *
158      * @return the parent parse tree node
159      */

160     public Node getParent() {
161         return parent;
162     }
163
164     /**
165      * Sets the parent node.
166      *
167      * @param parent the new parent node
168      */

169     void setParent(Node parent) {
170         this.parent = parent;
171     }
172
173     /**
174      * Returns the number of child nodes.
175      *
176      * @return the number of child nodes
177      */

178     public int getChildCount() {
179         return 0;
180     }
181
182     /**
183      * Returns the child node with the specified index.
184      *
185      * @param index the child index, 0 <= index < count
186      *
187      * @return the child node found, or
188      * null if index out of bounds
189      */

190     public Node getChildAt(int index) {
191         return null;
192     }
193
194     /**
195      * Returns the number of descendant nodes.
196      *
197      * @return the number of descendant nodes
198      *
199      * @since 1.2
200      */

201     public int getDescendantCount() {
202         int count = 0;
203
204         for (int i = 0; i < getChildCount(); i++) {
205             count += 1 + getChildAt(i).getDescendantCount();
206         }
207         return count;
208     }
209
210     /**
211      * Returns the number of computed values associated with this
212      * node. Any number of values can be associated with a node
213      * through calls to addValue().
214      *
215      * @return the number of values associated with this node
216      */

217     public int getValueCount() {
218         if (values == null) {
219             return 0;
220         } else {
221             return values.size();
222         }
223     }
224
225     /**
226      * Returns a computed value of this node, if previously set. A
227      * value may be used for storing intermediate results in the parse
228      * tree during analysis.
229      *
230      * @param pos the value position, 0 <= pos < count
231      *
232      * @return the computed node value, or
233      * null if not set
234      */

235     public Object JavaDoc getValue(int pos) {
236         if (values == null || pos < 0 || pos >= values.size()) {
237             return null;
238         } else {
239             return values.get(pos);
240         }
241     }
242
243     /**
244      * Returns the vector with all the computed values for this node.
245      * Note that the vector is not a copy, so changes will affect the
246      * values in this node (as it is the same object).
247      *
248      * @return a vector with all values, or
249      * null if no values have been set
250      *
251      * @since 1.2
252      */

253     public ArrayList JavaDoc getAllValues() {
254         return values;
255     }
256
257     /**
258      * Adds a computed value to this node. The computed value may be
259      * used for storing intermediate results in the parse tree during
260      * analysis.
261      *
262      * @param value the node value
263      */

264     public void addValue(Object JavaDoc value) {
265         if (value != null) {
266             if (this.values == null) {
267                 this.values = new ArrayList JavaDoc();
268             }
269             values.add(value);
270         }
271     }
272
273     /**
274      * Adds a set of computed values to this node.
275      *
276      * @param values the list with node values
277      */

278     public void addValues(Vector JavaDoc values) {
279         if (values != null) {
280             for (int i = 0; i < values.size(); i++) {
281                 addValue(values.get(i));
282             }
283         }
284     }
285
286     /**
287      * Adds a set of computed values to this node.
288      *
289      * @param values the list with node values
290      *
291      * @since 1.2
292      */

293     public void addValues(ArrayList JavaDoc values) {
294         if (values != null) {
295             for (int i = 0; i < values.size(); i++) {
296                 addValue(values.get(i));
297             }
298         }
299     }
300
301     /**
302      * Removes all computed values stored in this node.
303      */

304     public void removeAllValues() {
305         values = null;
306     }
307
308     /**
309      * Prints this node and all subnodes to the specified output
310      * stream.
311      *
312      * @param output the output stream to use
313      */

314     public void printTo(PrintStream JavaDoc output) {
315         printTo(new PrintWriter JavaDoc(output));
316     }
317
318     /**
319      * Prints this node and all subnodes to the specified output
320      * stream.
321      *
322      * @param output the output stream to use
323      */

324     public void printTo(PrintWriter JavaDoc output) {
325         printTo(output, "");
326         output.flush();
327     }
328
329     /**
330      * Prints this node and all subnodes to the specified output
331      * stream.
332      *
333      * @param output the output stream to use
334      * @param indent the indentation string
335      */

336     private void printTo(PrintWriter JavaDoc output, String JavaDoc indent) {
337         output.println(indent + toString());
338         indent = indent + " ";
339         for (int i = 0; i < getChildCount(); i++) {
340             getChildAt(i).printTo(output, indent);
341         }
342     }
343 }
344
Popular Tags