KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javacc > jjtree > SimpleNode


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

22
23 package org.javacc.jjtree;
24
25 public class SimpleNode implements Node {
26   private Node parent;
27   private Node[] children;
28   private int id;
29   private int myOrdinal;
30   
31   public SimpleNode(int i) {
32     id = i;
33   }
34
35   public SimpleNode(JJTreeParser p, int i) {
36     this(i);
37   }
38
39   public void jjtOpen() {}
40   public void jjtClose() {}
41   
42   public void jjtSetParent(Node n) { parent = n; }
43   public Node jjtGetParent() { return parent; }
44
45   public void jjtAddChild(Node n, int i) {
46     if (children == null) {
47       children = new Node[i + 1];
48     } else if (i >= children.length) {
49       Node c[] = new Node[i + 1];
50       System.arraycopy(children, 0, c, 0, children.length);
51       children = c;
52     }
53     children[i] = n;
54     ((SimpleNode)n).setOrdinal(i);
55   }
56
57   public Node jjtGetChild(int i) {
58     return children[i];
59   }
60
61   public int jjtGetNumChildren() {
62     return (children == null) ? 0 : children.length;
63   }
64
65   /* You can override these two methods in subclasses of SimpleNode to
66      customize the way the node appears when the tree is dumped. If
67      your output uses more than one line you should override
68      toString(String), otherwise overriding toString() is probably all
69      you need to do. */

70
71   public String JavaDoc toString() { return JJTreeParserTreeConstants.jjtNodeName[id]; }
72   public String JavaDoc toString(String JavaDoc prefix) { return prefix + toString(); }
73
74   /* Override this method if you want to customize how the node dumps
75      out its children. */

76
77   public void dump(String JavaDoc prefix) {
78     System.out.println(toString(prefix));
79     if (children != null) {
80       for (int i = 0; i < children.length; ++i) {
81     SimpleNode n = (SimpleNode)children[i];
82     if (n != null) {
83       n.dump(prefix + " ");
84     }
85       }
86     }
87   }
88
89   public int getOrdinal()
90   {
91     return myOrdinal;
92   }
93
94   public void setOrdinal(int o)
95   {
96     myOrdinal = o;
97   }
98
99
100   /*****************************************************************
101    *
102    * The following is added manually to enhance all tree nodes with
103    * attributes that store the first and last tokens corresponding to
104    * each node, as well as to print the tokens back to the specified
105    * output stream.
106    *
107    *****************************************************************/

108   
109   private Token first, last;
110   
111   public Token getFirstToken() { return first; }
112   public void setFirstToken(Token t) { first = t; }
113   public Token getLastToken() { return last; }
114   public void setLastToken(Token t) { last = t; }
115   
116   /* This method prints the tokens corresponding to this node
117      recursively calling the print methods of its children.
118      Overriding this print method in appropriate nodes gives the
119      output the added stuff not in the input. */

120
121   public void print(IO io) {
122     /* Some productions do not consume any tokens. In that case their
123        first and last tokens are a bit strange. */

124     if (getLastToken().next == getFirstToken()) {
125       return;
126     }
127
128     Token t1 = getFirstToken();
129     Token t = new Token();
130     t.next = t1;
131     SimpleNode n;
132     for (int ord = 0; ord < jjtGetNumChildren(); ord++) {
133       n = (SimpleNode)jjtGetChild(ord);
134       while (true) {
135     t = t.next;
136     if (t == n.getFirstToken()) break;
137     print(t, io);
138       }
139       n.print(io);
140       t = n.getLastToken();
141     }
142     while (t != getLastToken()) {
143       t = t.next;
144       print(t, io);
145     }
146   }
147   
148   
149   String JavaDoc translateImage(Token t)
150   {
151     return t.image;
152   }
153   
154   String JavaDoc whiteOut(Token t)
155   {
156     String JavaDoc s = "";
157
158     for (int i = 0; i < t.image.length(); ++i) {
159       s += " ";
160     }
161     return s;
162   }
163
164
165
166   /* Indicates whether the token should be replaced by white space or
167      replaced with the actual node variable. */

168   private boolean whitingOut = false;
169
170   protected void print(Token t, IO io) {
171     Token tt = t.specialToken;
172     if (tt != null) {
173       while (tt.specialToken != null) tt = tt.specialToken;
174       while (tt != null) {
175     io.print(addUnicodeEscapes(translateImage(tt)));
176     tt = tt.next;
177       }
178     }
179
180     /* If we're within a node scope we modify the source in the
181        following ways:
182
183        1) we rename all references to `jjtThis' to be references to
184        the actual node variable.
185
186        2) we replace all calls to `jjtree.currentNode()' with
187        references to the node variable. */

188
189     NodeScope s = NodeScope.getEnclosingNodeScope(this);
190     if (s == null) {
191       /* Not within a node scope so we don't need to modify the
192          source. */

193       io.print(addUnicodeEscapes(translateImage(t)));
194       return;
195     }
196
197     if (t.image.equals("jjtThis")) {
198       io.print(s.getNodeVariable());
199       return;
200     } else if (t.image.equals("jjtree")) {
201       if (t.next.image.equals(".")) {
202     if (t.next.next.image.equals("currentNode")) {
203       if (t.next.next.next.image.equals("(")) {
204         if (t.next.next.next.next.image.equals(")")) {
205           /* Found `jjtree.currentNode()' so go into white out
206                  mode. We'll stay in this mode until we find the
207                  closing parenthesis. */

208           whitingOut = true;
209         }
210       }
211     }
212       }
213     }
214     if (whitingOut) {
215       if (t.image.equals("jjtree")) {
216     io.print(s.getNodeVariable());
217     io.print(" ");
218       } else if (t.image.equals(")")) {
219     io.print(" ");
220     whitingOut = false;
221       } else {
222     for (int i = 0; i < t.image.length(); ++i) {
223       io.print(" ");
224     }
225       }
226       return;
227     }
228       
229     io.print(addUnicodeEscapes(translateImage(t)));
230   }
231
232
233   protected String JavaDoc addUnicodeEscapes(String JavaDoc str) {
234     String JavaDoc retval = "";
235     char ch;
236     for (int i = 0; i < str.length(); i++) {
237       ch = str.charAt(i);
238       if ((ch < 0x20 || ch > 0x7e) && ch != '\t' && ch != '\n' && ch != '\r' && ch != '\f') {
239     String JavaDoc s = "0000" + Integer.toString(ch, 16);
240     retval += "\\u" + s.substring(s.length() - 4, s.length());
241       } else {
242     retval += ch;
243       }
244     }
245     return retval;
246   }
247
248
249   static void openJJTreeComment(IO io, String JavaDoc arg)
250   {
251     if (arg != null) {
252       io.print("/*@bgen(jjtree) " + arg + " */");
253     } else {
254       io.print("/*@bgen(jjtree)*/");
255     }
256   }
257
258
259   static void closeJJTreeComment(IO io)
260   {
261     io.print("/*@egen*/");
262   }
263
264
265   String JavaDoc getIndentation(SimpleNode n)
266   {
267     return getIndentation(n, 0);
268   }
269
270
271   String JavaDoc getIndentation(SimpleNode n, int offset)
272   {
273     String JavaDoc s = "";
274     for (int i = offset + 1; i < n.getFirstToken().beginColumn; ++i) {
275       s += " ";
276     }
277     return s;
278   }
279
280 }
281
282 /*end*/
283
Popular Tags