KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Hashtable JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 public class ASTNodeDescriptor extends SimpleNode {
29   ASTNodeDescriptor(int id) {
30     super(id);
31   }
32
33   private boolean faked = false;
34
35   static ASTNodeDescriptor indefinite(String JavaDoc s)
36   {
37     ASTNodeDescriptor nd = new ASTNodeDescriptor(JJTreeParserTreeConstants.JJTNODEDESCRIPTOR);
38     nd.name = s;
39     nd.setNodeIdValue();
40     nd.faked = true;
41     return nd;
42   }
43
44
45   static Vector JavaDoc nodeIds = new Vector JavaDoc();
46   static Vector JavaDoc nodeNames = new Vector JavaDoc();
47   static Hashtable JavaDoc nodeSeen = new Hashtable JavaDoc();
48
49   static Vector JavaDoc getNodeIds()
50   {
51     return nodeIds;
52   }
53
54   static Vector JavaDoc getNodeNames()
55   {
56     return nodeNames;
57   }
58
59   void setNodeIdValue()
60   {
61     String JavaDoc k = getNodeId();
62     if (!nodeSeen.containsKey(k)) {
63       nodeSeen.put(k, k);
64       nodeNames.addElement(name);
65       nodeIds.addElement(k);
66     }
67   }
68
69   String JavaDoc getNodeId()
70   {
71     return "JJT" + name.toUpperCase().replace('.', '_');
72   }
73
74
75   String JavaDoc name;
76   boolean isGT;
77   ASTNodeDescriptorExpression expression;
78
79
80   boolean isVoid()
81   {
82     return name.equals("void");
83   }
84
85   public String JavaDoc toString()
86   {
87     if (faked) {
88       return "(faked) " + name;
89     } else {
90     return super.toString() + ": " + name;
91     }
92   }
93
94
95   String JavaDoc getDescriptor()
96   {
97     if (expression == null) {
98       return name;
99     } else {
100       return "#" + name + "(" + (isGT ? ">" : "") + expression_text() + ")";
101     }
102   }
103
104
105   String JavaDoc getNodeType()
106   {
107     if (JJTreeOptions.getMulti()) {
108       return JJTreeOptions.getNodePrefix() + name;
109     } else {
110       return "SimpleNode";
111     }
112   }
113
114
115   String JavaDoc getNodeName()
116   {
117     return name;
118   }
119
120
121   String JavaDoc openNode(String JavaDoc nodeVar)
122   {
123     return "jjtree.openNodeScope(" + nodeVar + ");";
124   }
125
126
127   private String JavaDoc expression_text()
128   {
129     if (expression.getFirstToken().image.equals(")") &&
130     expression.getLastToken().image.equals("(")) {
131       return "true";
132     }
133
134     String JavaDoc s = "";
135     Token t = expression.getFirstToken();
136     while (true) {
137        s += " " + t.image;
138        if (t == expression.getLastToken()) {
139      break;
140        }
141        t = t.next;
142     }
143     return s;
144   }
145
146
147   String JavaDoc closeNode(String JavaDoc nodeVar)
148   {
149     if (expression == null) {
150       return "jjtree.closeNodeScope(" + nodeVar + ", true);";
151     } else if (isGT) {
152       return "jjtree.closeNodeScope(" + nodeVar + ", jjtree.nodeArity() >" +
153     expression_text() + ");";
154     } else {
155       return "jjtree.closeNodeScope(" + nodeVar + ", " +
156     expression_text() + ");";
157     }
158   }
159
160
161   String JavaDoc translateImage(Token t)
162   {
163     return whiteOut(t);
164   }
165
166 }
167
168 /*end*/
169
Popular Tags