001: /*
002: * SimpleNode.java
003: *
004: * Copyright (c) 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.lang;
010:
011: import java.io.IOException;
012: import java.io.ObjectInputStream;
013: import java.io.Serializable;
014: import java.util.*;
015:
016: /**
017: * This class respresents a node of AST.
018: */
019: public class SimpleNode implements Serializable, Cloneable {
020:
021: static final long serialVersionUID = 2950908047102096822L;
022:
023: /**
024: * @serial
025: */
026: protected SimpleNode parent;
027:
028: /**
029: * @serial
030: */
031: protected SimpleNode[] children;
032:
033: /**
034: * @serial
035: */
036: public int id;
037:
038: int toplevel;
039:
040: public SimpleNode(int i) {
041: id = i;
042: }
043:
044: /**
045: * public void jjtOpen() { }
046: *
047: * public void jjtClose() { }
048: */
049:
050: public void jjtSetParent(SimpleNode n) {
051: parent = n;
052: }
053:
054: public SimpleNode jjtGetParent() {
055: return parent;
056: }
057:
058: public void jjtAddChild(SimpleNode n, int i) {
059: if (children == null) {
060: children = new SimpleNode[i + 1];
061: } else if (i >= children.length) {
062: SimpleNode c[] = new SimpleNode[i + 1];
063: System.arraycopy(children, 0, c, 0, children.length);
064: this .children = c;
065: }
066: children[i] = n;
067: }
068:
069: public final SimpleNode jjtGetChild(int i) {
070: if (children == null) {
071: System.out.println("children == null : " + id + ", " + str
072: + ", " + i);
073: }
074: return children[i];
075: }
076:
077: public final int jjtGetNumChildren() {
078: return (children == null) ? 0 : children.length;
079: }
080:
081: public void clearChildren() {
082: children = null;
083: }
084:
085: /**/
086: public String toString() {
087: return PnutsParserTreeConstants.jjtNodeName[id];
088: }
089:
090: public String toString(String prefix) {
091: return prefix + PnutsParserTreeConstants.jjtNodeName[id];
092: }
093:
094: public void dump(String prefix) {
095: System.out.println(toString(prefix));
096: if (children != null) {
097: for (int i = 0; i < children.length; ++i) {
098: SimpleNode n = (SimpleNode) children[i];
099: if (n != null) {
100: n.dump(prefix + " ");
101: }
102: }
103: }
104: }
105:
106: public String unparse() {
107: StringBuffer sb = new StringBuffer();
108: Visitor v = new org.pnuts.lang.UnparseVisitor(sb);
109: accept(v, null);
110: return sb.toString();
111: }
112:
113: /**/
114:
115: // pnuts specific attributes
116: /**
117: * @serial
118: */
119: public String str;
120:
121: /**
122: * @serial
123: */
124: public int beginLine;
125:
126: /**
127: * @serial
128: */
129: public int beginColumn;
130:
131: /**
132: * @serial
133: */
134: public int endLine;
135:
136: /**
137: * Value of the node. Act as cache.
138: */
139: transient Object value;
140:
141: /**
142: * @serial
143: */
144: public Object info;
145:
146: transient Map attribute;
147:
148: /**
149: * Gets an attribute value
150: *
151: * @param key the attribute name
152: * @return the attribute value
153: */
154: public Object getAttribute(String key) {
155: if (attribute == null) {
156: return null;
157: } else {
158: return attribute.get(key);
159: }
160: }
161:
162: /**
163: * Sets an attribute value for the specified key
164: *
165: * @param key the attribute name
166: * @param value the value
167: */
168: public void setAttribute(String key, Object value) {
169: if (attribute == null) {
170: attribute = new HashMap();
171: }
172: attribute.put(key, value);
173: }
174:
175: /**
176: * dispatch the instance to a Visitor
177: */
178: public Object accept(Visitor visitor, Context context) {
179: if (context != null && !context.eval) {
180: context.updateLine(this );
181: }
182:
183: switch (id) {
184: case PnutsParserTreeConstants.JJTSTART:
185: return visitor.start(this , context);
186: case PnutsParserTreeConstants.JJTSTARTSET:
187: return visitor.startSet(this , context);
188: case PnutsParserTreeConstants.JJTEXPRESSIONLIST:
189: return visitor.expressionList(this , context);
190: case PnutsParserTreeConstants.JJTIDNODE:
191: return visitor.idNode(this , context);
192: case PnutsParserTreeConstants.JJTGLOBAL:
193: return visitor.global(this , context);
194: case PnutsParserTreeConstants.JJTARRAYTYPE:
195: return visitor.arrayType(this , context);
196: case PnutsParserTreeConstants.JJTLISTELEMENTS:
197: return visitor.listElements(this , context);
198: case PnutsParserTreeConstants.JJTCASTEXPRESSION:
199: return visitor.castExpression(this , context);
200: case PnutsParserTreeConstants.JJTCLASS: // fall-thru
201: case PnutsParserTreeConstants.JJTCLASSEXPR:
202: return visitor.classNode(this , context);
203: case PnutsParserTreeConstants.JJTINDEXNODE:
204: return visitor.indexNode(this , context);
205: case PnutsParserTreeConstants.JJTRANGENODE:
206: return visitor.rangeNode(this , context);
207: case PnutsParserTreeConstants.JJTMETHODNODE:
208: return visitor.methodNode(this , context);
209: case PnutsParserTreeConstants.JJTSTATICMETHODNODE:
210: return visitor.staticMethodNode(this , context);
211: case PnutsParserTreeConstants.JJTMEMBERNODE:
212: return visitor.memberNode(this , context);
213: case PnutsParserTreeConstants.JJTSTATICMEMBERNODE:
214: return visitor.staticMemberNode(this , context);
215: case PnutsParserTreeConstants.JJTAPPLICATIONNODE:
216: return visitor.applicationNode(this , context);
217: case PnutsParserTreeConstants.JJTINTEGERNODE:
218: return visitor.integerNode(this , context);
219: case PnutsParserTreeConstants.JJTFLOATINGNODE:
220: return visitor.floatingNode(this , context);
221: case PnutsParserTreeConstants.JJTCHARACTERNODE:
222: return visitor.characterNode(this , context);
223: case PnutsParserTreeConstants.JJTSTRINGNODE:
224: return visitor.stringNode(this , context);
225: case PnutsParserTreeConstants.JJTTRUENODE:
226: return visitor.trueNode(this , context);
227: case PnutsParserTreeConstants.JJTFALSENODE:
228: return visitor.falseNode(this , context);
229: case PnutsParserTreeConstants.JJTNULLNODE:
230: return visitor.nullNode(this , context);
231: case PnutsParserTreeConstants.JJTASSIGNMENT:
232: return visitor.assignment(this , context);
233: case PnutsParserTreeConstants.JJTASSIGNMENTTA:
234: return visitor.assignmentTA(this , context);
235: case PnutsParserTreeConstants.JJTASSIGNMENTMA:
236: return visitor.assignmentMA(this , context);
237: case PnutsParserTreeConstants.JJTASSIGNMENTDA:
238: return visitor.assignmentDA(this , context);
239: case PnutsParserTreeConstants.JJTASSIGNMENTPA:
240: return visitor.assignmentPA(this , context);
241: case PnutsParserTreeConstants.JJTASSIGNMENTSA:
242: return visitor.assignmentSA(this , context);
243: case PnutsParserTreeConstants.JJTASSIGNMENTLA:
244: return visitor.assignmentLA(this , context);
245: case PnutsParserTreeConstants.JJTASSIGNMENTRA:
246: return visitor.assignmentRA(this , context);
247: case PnutsParserTreeConstants.JJTASSIGNMENTRAA:
248: return visitor.assignmentRAA(this , context);
249: case PnutsParserTreeConstants.JJTASSIGNMENTAA:
250: return visitor.assignmentAA(this , context);
251: case PnutsParserTreeConstants.JJTASSIGNMENTEA:
252: return visitor.assignmentEA(this , context);
253: case PnutsParserTreeConstants.JJTASSIGNMENTOA:
254: return visitor.assignmentOA(this , context);
255: case PnutsParserTreeConstants.JJTTERNARYNODE:
256: return visitor.ternary(this , context);
257: case PnutsParserTreeConstants.JJTORNODE:
258: return visitor.orNode(this , context);
259: case PnutsParserTreeConstants.JJTANDNODE:
260: return visitor.andNode(this , context);
261: case PnutsParserTreeConstants.JJTXORNODE:
262: return visitor.xorNode(this , context);
263: case PnutsParserTreeConstants.JJTLOGORNODE:
264: return visitor.logOrNode(this , context);
265: case PnutsParserTreeConstants.JJTLOGANDNODE:
266: return visitor.logAndNode(this , context);
267: case PnutsParserTreeConstants.JJTLOGNOTNODE:
268: return visitor.logNotNode(this , context);
269: case PnutsParserTreeConstants.JJTEQUALNODE:
270: return visitor.equalNode(this , context);
271: case PnutsParserTreeConstants.JJTNOTEQNODE:
272: return visitor.notEqNode(this , context);
273: case PnutsParserTreeConstants.JJTINSTANCEOFEXPRESSION:
274: return visitor.instanceof Expression(this , context);
275: case PnutsParserTreeConstants.JJTLTNODE:
276: return visitor.ltNode(this , context);
277: case PnutsParserTreeConstants.JJTGTNODE:
278: return visitor.gtNode(this , context);
279: case PnutsParserTreeConstants.JJTLENODE:
280: return visitor.leNode(this , context);
281: case PnutsParserTreeConstants.JJTGENODE:
282: return visitor.geNode(this , context);
283: case PnutsParserTreeConstants.JJTSHIFTLEFTNODE:
284: return visitor.shiftLeftNode(this , context);
285: case PnutsParserTreeConstants.JJTSHIFTRIGHTNODE:
286: return visitor.shiftRightNode(this , context);
287: case PnutsParserTreeConstants.JJTSHIFTARITHMETICNODE:
288: return visitor.shiftArithmeticNode(this , context);
289: case PnutsParserTreeConstants.JJTADDNODE:
290: return visitor.addNode(this , context);
291: case PnutsParserTreeConstants.JJTSUBTRACTNODE:
292: return visitor.subtractNode(this , context);
293: case PnutsParserTreeConstants.JJTMULTNODE:
294: return visitor.multNode(this , context);
295: case PnutsParserTreeConstants.JJTDIVIDENODE:
296: return visitor.divideNode(this , context);
297: case PnutsParserTreeConstants.JJTMODNODE:
298: return visitor.modNode(this , context);
299: case PnutsParserTreeConstants.JJTNEGATIVENODE:
300: return visitor.negativeNode(this , context);
301: case PnutsParserTreeConstants.JJTPREINCRNODE:
302: return visitor.preIncrNode(this , context);
303: case PnutsParserTreeConstants.JJTPREDECRNODE:
304: return visitor.preDecrNode(this , context);
305: case PnutsParserTreeConstants.JJTNOTNODE:
306: return visitor.notNode(this , context);
307: case PnutsParserTreeConstants.JJTPOSTINCRNODE:
308: return visitor.postIncrNode(this , context);
309: case PnutsParserTreeConstants.JJTPOSTDECRNODE:
310: return visitor.postDecrNode(this , context);
311: case PnutsParserTreeConstants.JJTBREAK:
312: return visitor.breakNode(this , context);
313: case PnutsParserTreeConstants.JJTCONTINUE:
314: return visitor.continueNode(this , context);
315: case PnutsParserTreeConstants.JJTRETURN:
316: return visitor.returnNode(this , context);
317: case PnutsParserTreeConstants.JJTYIELD:
318: return visitor.yieldNode(this , context);
319: case PnutsParserTreeConstants.JJTCATCHNODE:
320: return visitor.catchNode(this , context);
321: case PnutsParserTreeConstants.JJTTHROWNODE:
322: return visitor.throwNode(this , context);
323: case PnutsParserTreeConstants.JJTFINALLYNODE:
324: return visitor.finallyNode(this , context);
325: case PnutsParserTreeConstants.JJTBLOCK:
326: return visitor.blockNode(this , context);
327: case PnutsParserTreeConstants.JJTIFSTATEMENT:
328: return visitor.ifStatement(this , context);
329: case PnutsParserTreeConstants.JJTDOSTATEMENT:
330: return visitor.doStatement(this , context);
331: case PnutsParserTreeConstants.JJTWHILESTATEMENT:
332: return visitor.whileStatement(this , context);
333: case PnutsParserTreeConstants.JJTFORSTATEMENT:
334: return visitor.forStatement(this , context);
335: case PnutsParserTreeConstants.JJTFOREACHSTATEMENT:
336: return visitor.foreachStatement(this , context);
337: case PnutsParserTreeConstants.JJTSWITCHSTATEMENT:
338: return visitor.switchStatement(this , context);
339: case PnutsParserTreeConstants.JJTSWITCHBLOCK:
340: return visitor.switchBlock(this , context);
341: case PnutsParserTreeConstants.JJTFUNCTIONSTATEMENT:
342: return visitor.functionStatement(this , context);
343: case PnutsParserTreeConstants.JJTTRYSTATEMENT:
344: return visitor.tryStatement(this , context);
345: case PnutsParserTreeConstants.JJTCATCHBLOCK:
346: return visitor.catchBlock(this , context);
347: case PnutsParserTreeConstants.JJTIMPORT:
348: return visitor.importNode(this , context);
349: case PnutsParserTreeConstants.JJTPACKAGESTATEMENT:
350: return visitor.packageNode(this , context);
351: case PnutsParserTreeConstants.JJTNEW:
352: return visitor.newNode(this , context);
353: case PnutsParserTreeConstants.JJTMAPELEMENTS:
354: return visitor.mapNode(this , context);
355: case PnutsParserTreeConstants.JJTCLASSNAME:
356: return visitor.className(this , context);
357: case PnutsParserTreeConstants.JJTCLASSSCRIPT:
358: return visitor.classScript(this , context);
359: case PnutsParserTreeConstants.JJTCLASSDEF:
360: return visitor.classDef(this , context);
361: case PnutsParserTreeConstants.JJTCLASSDEFBODY:
362: return visitor.classDefBody(this , context);
363: case PnutsParserTreeConstants.JJTMETHODDEF:
364: return visitor.methodDef(this , context);
365: case PnutsParserTreeConstants.JJTBEANDEF:
366: return visitor.beanDef(this , context);
367: case PnutsParserTreeConstants.JJTPACKAGE:
368: case PnutsParserTreeConstants.JJTELSEIFNODE:
369: case PnutsParserTreeConstants.JJTELSENODE:
370: case PnutsParserTreeConstants.JJTSWITCHLABEL:
371: case PnutsParserTreeConstants.JJTPARAMLIST:
372: case PnutsParserTreeConstants.JJTPARAM:
373: default:
374: return null;
375: }
376: }
377:
378: private void readObject(ObjectInputStream s) throws IOException,
379: ClassNotFoundException {
380: s.defaultReadObject();
381: switch (id) {
382: case PnutsParserTreeConstants.JJTIDNODE:
383: case PnutsParserTreeConstants.JJTGLOBAL:
384: case PnutsParserTreeConstants.JJTMETHODNODE:
385: case PnutsParserTreeConstants.JJTMEMBERNODE:
386: case PnutsParserTreeConstants.JJTSTATICMETHODNODE:
387: case PnutsParserTreeConstants.JJTSTATICMEMBERNODE:
388: case PnutsParserTreeConstants.JJTLOCAL:
389: case PnutsParserTreeConstants.JJTPARAM:
390: case PnutsParserTreeConstants.JJTFORENUM:
391: str = str.intern();
392: break;
393: case PnutsParserTreeConstants.JJTFUNCTIONSTATEMENT:
394: if (str != null) {
395: str = str.intern();
396: }
397: case PnutsParserTreeConstants.JJTSTART:
398: break;
399: case PnutsParserTreeConstants.JJTFOREACHSTATEMENT:
400: str = str.intern();
401: case PnutsParserTreeConstants.JJTBLOCK:
402: case PnutsParserTreeConstants.JJTSWITCHBLOCK:
403: case PnutsParserTreeConstants.JJTDOSTATEMENT:
404: case PnutsParserTreeConstants.JJTWHILESTATEMENT:
405: case PnutsParserTreeConstants.JJTFORSTATEMENT:
406: case PnutsParserTreeConstants.JJTSWITCHSTATEMENT:
407: break;
408: }
409: }
410:
411: void setToken(Token t) {
412: setToken(t, t);
413: }
414:
415: private void setToken(Token t1, Token t2) {
416: beginLine = t1.beginLine;
417: beginColumn = t1.beginColumn;
418: endLine = t2.endLine;
419: }
420:
421: public Object clone() {
422: try {
423: SimpleNode c = (SimpleNode) super .clone();
424: c.children = null;
425: return c;
426: } catch (CloneNotSupportedException e) {
427: throw new InternalError();
428: }
429: }
430: }
|