KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > VTransformer > SimpleNode


1
2 /*
3  * Copyright © 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
4  * California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has
5  * intellectual property rights relating to technology embodied in the product
6  * that is described in this document. In particular, and without limitation,
7  * these intellectual property rights may include one or more of the U.S.
8  * patents listed at http://www.sun.com/patents and one or more additional
9  * patents or pending patent applications in the U.S. and in other countries.
10  * U.S. Government Rights - Commercial software. Government users are subject
11  * to the Sun Microsystems, Inc. standard license agreement and applicable
12  * provisions of the FAR and its supplements. Use is subject to license terms.
13  * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
14  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries. This
15  * product is covered and controlled by U.S. Export Control laws and may be
16  * subject to the export or import laws in other countries. Nuclear, missile,
17  * chemical biological weapons or nuclear maritime end uses or end users,
18  * whether direct or indirect, are strictly prohibited. Export or reexport
19  * to countries subject to U.S. embargo or to entities identified on U.S.
20  * export exclusion lists, including, but not limited to, the denied persons
21  * and specially designated nationals lists is strictly prohibited.
22  */

23
24
25 package VTransformer;
26
27 public class SimpleNode implements Node {
28   protected Node parent;
29   protected Node[] children;
30   protected int id;
31   protected JavaParser parser;
32
33   protected Token first, last;
34
35   public SimpleNode(int i) {
36     id = i;
37   }
38
39   public SimpleNode(JavaParser p, int i) {
40     this(i);
41     parser = p;
42   }
43
44   public static Node jjtCreate(JavaParser p, int id) {
45     return new SimpleNode(p, id);
46   }
47
48   public void jjtOpen() {
49     first = parser.getToken(1); // new
50
}
51
52   public void jjtClose() {
53     last = parser.getToken(0); // new
54
}
55
56   public Token getFirstToken() { return first; } // new
57
public Token getLastToken() { return last; } // new
58

59   public void jjtSetParent(Node n) { parent = n; }
60   public Node jjtGetParent() { return parent; }
61
62   public void jjtAddChild(Node n, int i) {
63     if (children == null) {
64       children = new Node[i + 1];
65     } else if (i >= children.length) {
66       Node c[] = new Node[i + 1];
67       System.arraycopy(children, 0, c, 0, children.length);
68       children = c;
69     }
70     children[i] = n;
71   }
72
73   public Node jjtGetChild(int i) {
74     return children[i];
75   }
76
77   public int jjtGetNumChildren() {
78     return (children == null) ? 0 : children.length;
79   }
80
81   /** Accept the visitor. **/
82   public Object JavaDoc jjtAccept(JavaParserVisitor visitor, Object JavaDoc data) {
83     return visitor.visit(this, data);
84   }
85
86   /** Accept the visitor. **/
87   public Object JavaDoc acceptChildren(JavaParserVisitor visitor, Object JavaDoc data) {
88     if (children != null) {
89       for (int i = 0; i < children.length; ++i) {
90         children[i].jjtAccept(visitor, data);
91       }
92     }
93     return data;
94   }
95
96   /* You can override these two methods in subclasses of SimpleNode to
97      customize the way the node appears when the tree is dumped. If
98      your output uses more than one line you should override
99      toString(String), otherwise overriding toString() is probably all
100      you need to do. */

101
102   public String JavaDoc toString() { return JavaParserTreeConstants.jjtNodeName[id]; }
103   public String JavaDoc toString(String JavaDoc prefix) { return prefix + toString(); }
104
105   /* Override this method if you want to customize how the node dumps
106      out its children. */

107
108   public void dump(String JavaDoc prefix) {
109     System.out.println(toString(prefix));
110     if (children != null) {
111       for (int i = 0; i < children.length; ++i) {
112     SimpleNode n = (SimpleNode)children[i];
113     if (n != null) {
114       n.dump(prefix + " ");
115     }
116       }
117     }
118   }
119 }
120
Popular Tags