001: // Copyright (c) Corporation for National Research Initiatives
002: package org.python.parser;
003:
004: import org.python.parser.ast.*;
005: import java.io.DataOutputStream;
006: import java.io.IOException;
007:
008: public class SimpleNode implements Node {
009: public int beginLine, beginColumn;
010:
011: public boolean from_future_checked = false; // from __future__ support
012:
013: public SimpleNode() {
014: }
015:
016: public static Node jjtCreate(PythonGrammar p, int id) {
017: return p.jjtree.openNode(id);
018: }
019:
020: public int getId() {
021: return -1;
022: }
023:
024: public Object getImage() {
025: return null;
026: }
027:
028: public void setImage(Object image) {
029: }
030:
031: /* You can override these two methods in subclasses of SimpleNode to
032: customize the way the node appears when the tree is dumped. If
033: your output uses more than one line you should override
034: toString(String), otherwise overriding toString() is probably all
035: you need to do. */
036:
037: public String toString() {
038: return super .toString() + " at line " + beginLine;
039: }
040:
041: public String toString(String prefix) {
042: return prefix + toString();
043: }
044:
045: public Object accept(VisitorIF visitor) throws Exception {
046: throw new ParseException("Unexpected node: " + this );
047: }
048:
049: public void traverse(VisitorIF visitor) throws Exception {
050: throw new ParseException("Unexpected node: " + this );
051: }
052:
053: /* Override this method if you want to customize how the node dumps
054: ut its children. */
055:
056: protected String dumpThis(String s) {
057: return s;
058: }
059:
060: protected String dumpThis(Object o) {
061: return String.valueOf(o);
062: }
063:
064: protected String dumpThis(Object[] s) {
065: StringBuffer sb = new StringBuffer();
066: if (s == null) {
067: sb.append("null");
068: } else {
069: sb.append("[");
070: for (int i = 0; i < s.length; i++) {
071: if (i > 0)
072: sb.append(", ");
073: sb.append(String.valueOf(s[i]));
074: }
075: sb.append("]");
076: }
077:
078: return sb.toString();
079: }
080:
081: protected String dumpThis(int i) {
082: return Integer.toString(i);
083: }
084:
085: protected String dumpThis(int i, String[] names) {
086: // XXX Verify bounds.
087: return names[i];
088: }
089:
090: protected String dumpThis(int[] arr, String[] names) {
091: StringBuffer sb = new StringBuffer();
092: if (arr == null) {
093: sb.append("null");
094: } else {
095: sb.append("[");
096: for (int i = 0; i < arr.length; i++) {
097: if (i > 0)
098: sb.append(", ");
099: // XXX Verify bounds.
100: sb.append(names[arr[i]]);
101: }
102: sb.append("]");
103: }
104: return sb.toString();
105: }
106:
107: protected String dumpThis(boolean b) {
108: return String.valueOf(b);
109: }
110:
111: public void pickle(DataOutputStream ostream) throws IOException {
112: throw new IOException("Pickling not implemented");
113: }
114:
115: protected void pickleThis(String s, DataOutputStream ostream)
116: throws IOException {
117: if (s == null) {
118: ostream.writeInt(-1);
119: } else {
120: ostream.writeInt(s.length());
121: ostream.writeBytes(s);
122: }
123: }
124:
125: protected void pickleThis(String[] s, DataOutputStream ostream)
126: throws IOException {
127: if (s == null) {
128: ostream.writeInt(-1);
129: } else {
130: ostream.writeInt(s.length);
131: for (int i = 0; i < s.length; i++) {
132: pickleThis(s[i], ostream);
133: }
134: }
135: }
136:
137: protected void pickleThis(SimpleNode o, DataOutputStream ostream)
138: throws IOException {
139: if (o == null) {
140: ostream.writeInt(-1);
141: } else {
142: o.pickle(ostream);
143: }
144: }
145:
146: protected void pickleThis(SimpleNode[] s, DataOutputStream ostream)
147: throws IOException {
148: if (s == null) {
149: ostream.writeInt(-1);
150: } else {
151: ostream.writeInt(s.length);
152: for (int i = 0; i < s.length; i++) {
153: pickleThis(s[i], ostream);
154: }
155: }
156: }
157:
158: protected void pickleThis(int i, DataOutputStream ostream)
159: throws IOException {
160: ostream.writeInt(i);
161: }
162:
163: protected void pickleThis(int[] arr, DataOutputStream ostream)
164: throws IOException {
165: if (arr == null) {
166: ostream.writeInt(-1);
167: } else {
168: ostream.writeInt(arr.length);
169: for (int i = 0; i < arr.length; i++) {
170: ostream.writeInt(arr[i]);
171: }
172: }
173: }
174:
175: protected void pickleThis(boolean b, DataOutputStream ostream)
176: throws IOException {
177: ostream.writeBoolean(b);
178: }
179:
180: protected void pickleThis(Object n, DataOutputStream ostream)
181: throws IOException {
182: String s = n.toString();
183: ostream.writeInt(s.length());
184: ostream.writeBytes(s);
185: }
186: }
|