001: package net.sourceforge.jrefactory.io;
002:
003: import java.io.InputStreamReader;
004:
005: /**
006: * Basic character stream. The javacc tool creates one of four different types
007: * of character streams. To be able to switch between different types, I've
008: * created this parent class. The parent class invokes the appropriate child
009: * class that was created by javacc. <P>
010: *
011: *
012: *@author Mike Atkinson
013: *@author <a href="mailto:JRefactory@ladyshot.demon.co.uk">Mike Atkinson</a>
014: *@version $Id: CharStream.java,v 1.3 2003/11/11 18:48:20 mikeatkinson Exp $
015: */
016: public abstract class CharStream {
017: /**
018: * Are there more characters available
019: */
020: protected int available;
021: /**
022: * The buffer column
023: */
024: protected int bufcolumn[];
025:
026: /**
027: * The buffer
028: */
029: protected char[] buffer;
030: /**
031: * The buffer line
032: */
033: protected int bufline[];
034: /**
035: * The buffer location
036: */
037: public int bufpos = -1;
038: /**
039: * The buffer size
040: */
041: protected int bufsize;
042:
043: /**
044: * The column index
045: */
046: protected int column = 0;
047: /**
048: * Index into the buffer
049: */
050: protected int inBuf = 0;
051:
052: /**
053: * The input
054: */
055: protected java.io.Reader inputStream;
056: /**
057: * The line index
058: */
059: protected int line = 1;
060: /**
061: * The maximum next character index
062: */
063: protected int maxNextCharInd = 0;
064:
065: /**
066: * Was the previous character a CR?
067: */
068: protected boolean prevCharIsCR = false;
069: /**
070: * Was the previous character a LF?
071: */
072: protected boolean prevCharIsLF = false;
073: /**
074: * Index of the current token's starting point
075: */
076: protected int tokenBegin;
077: /**
078: * Use the ascii character stream
079: */
080: public final static int ASCII = 1;
081: /**
082: * Use the unicode character stream
083: */
084: public final static int FULL_CHAR = 3;
085: /**
086: * Use the Java character stream
087: */
088: public final static int JAVA_LIKE = 4;
089: /**
090: * Use the unicode character stream
091: */
092: public final static int UNICODE = 2;
093:
094: /**
095: * This field determines which type of character stream to use
096: */
097: private static int charStreamType = -1;
098:
099: /**
100: * Is this a parser
101: */
102: public final static boolean staticFlag = false;
103:
104: /**
105: * Description of the Method
106: *
107: *@return Description of the Returned Value
108: */
109: public abstract String GetImage();
110:
111: /**
112: * Description of the Method
113: *
114: *@param len Description of Parameter
115: *@return Description of the Returned Value
116: */
117: public abstract char[] GetSuffix(int len);
118:
119: /**
120: * Gets the BeginColumn attribute of the CharStream class
121: *
122: *@return The BeginColumn value
123: */
124: public int getBeginColumn() {
125: return bufcolumn[tokenBegin];
126: }
127:
128: /**
129: * Gets the BeginLine attribute of the CharStream class
130: *
131: *@return The BeginLine value
132: */
133: public int getBeginLine() {
134: return bufline[tokenBegin];
135: }
136:
137: /**
138: * Gets the Column attribute of the CharStream class
139: *
140: *@return The Column value
141: * @deprecated
142: * @see #getEndColumn
143: */
144: public int getColumn() {
145: return bufcolumn[bufpos];
146: }
147:
148: /**
149: * Gets the EndColumn attribute of the CharStream class
150: *
151: *@return The EndColumn value
152: */
153: public int getEndColumn() {
154: return bufcolumn[bufpos];
155: }
156:
157: /**
158: * Gets the EndLine attribute of the CharStream class
159: *
160: *@return The EndLine value
161: */
162: public int getEndLine() {
163: return bufline[bufpos];
164: }
165:
166: /**
167: * Gets the Line attribute of the CharStream class
168: *
169: *@return The Line value
170: * @deprecated
171: * @see #getEndLine
172: */
173: public int getLine() {
174: return bufline[bufpos];
175: }
176:
177: /**
178: * Description of the Method
179: *
180: *@return Description of the Returned Value
181: *@exception java.io.IOException Description of Exception
182: */
183: public char BeginToken() throws java.io.IOException {
184: tokenBegin = -1;
185: char c = readChar();
186: tokenBegin = bufpos;
187:
188: return c;
189: }
190:
191: /**
192: * Description of the Method
193: *
194: *@param dstream Description of Parameter
195: *@param startline Description of Parameter
196: *@param startcolumn Description of Parameter
197: *@param buffersize Description of Parameter
198: */
199: public abstract void ReInit(java.io.Reader dstream, int startline,
200: int startcolumn, int buffersize);
201:
202: /**
203: * Description of the Method
204: *
205: *@param dstream Description of Parameter
206: *@param startline Description of Parameter
207: *@param startcolumn Description of Parameter
208: */
209: public void ReInit(java.io.Reader dstream, int startline,
210: int startcolumn) {
211: ReInit(dstream, startline, startcolumn, 4096);
212: }
213:
214: /**
215: * Description of the Method
216: *
217: *@param dstream Description of Parameter
218: *@param startline Description of Parameter
219: *@param startcolumn Description of Parameter
220: *@param buffersize Description of Parameter
221: */
222: public void ReInit(java.io.InputStream dstream, int startline,
223: int startcolumn, int buffersize) {
224: ReInit(dstream, startline, startcolumn, buffersize);
225: }
226:
227: /**
228: * Description of the Method
229: *
230: *@param dstream Description of Parameter
231: *@param startline Description of Parameter
232: *@param startcolumn Description of Parameter
233: */
234: public void ReInit(java.io.InputStream dstream, int startline,
235: int startcolumn) {
236: ReInit(new java.io.InputStreamReader(dstream), startline,
237: startcolumn, 4096);
238: }
239:
240: /**
241: * Description of the Method
242: *
243: *@param newLine Description of Parameter
244: *@param newCol Description of Parameter
245: */
246: public abstract void adjustBeginLineColumn(int newLine, int newCol);
247:
248: /**
249: * Description of the Method
250: *
251: *@param amount Description of Parameter
252: */
253: public abstract void backup(int amount);
254:
255: /**
256: * Description of the Method
257: *
258: *@return Description of the Returned Value
259: *@exception java.io.IOException Description of Exception
260: */
261: public abstract char readChar() throws java.io.IOException;
262:
263: public static void setCharStreamType(int type) {
264: charStreamType = type;
265: }
266:
267: /**
268: * Constructor for the CharStream object
269: *
270: *@param dstream Description of Parameter
271: *@param startline Description of Parameter
272: *@param startcolumn Description of Parameter
273: *@param buffersize Description of Parameter
274: *@return Description of the Returned Value
275: */
276: public static CharStream make(java.io.Reader dstream,
277: int startline, int startcolumn, int buffersize) {
278: if (charStreamType == JAVA_LIKE) {
279: return new JavaCharStream(dstream, startline, startcolumn,
280: buffersize);
281: } else if (charStreamType != UNICODE) {
282: return new ASCII_CharStream(dstream, startline,
283: startcolumn, buffersize,
284: charStreamType == FULL_CHAR);
285: } else {
286: return new SimpleCharStream(dstream, startline,
287: startcolumn, buffersize);
288: }
289: }
290:
291: /**
292: * Constructor for the CharStream object
293: *
294: *@param dstream Description of Parameter
295: *@param startline Description of Parameter
296: *@param startcolumn Description of Parameter
297: *@return Description of the Returned Value
298: */
299: public static CharStream make(java.io.Reader dstream,
300: int startline, int startcolumn) {
301: return make(dstream, startline, startcolumn, 4096);
302: }
303:
304: /**
305: * Constructor for the CharStream object
306: *
307: *@param dstream Description of Parameter
308: *@param startline Description of Parameter
309: *@param startcolumn Description of Parameter
310: *@param buffersize Description of Parameter
311: *@return Description of the Returned Value
312: */
313: public static CharStream make(java.io.InputStream dstream,
314: int startline, int startcolumn, int buffersize) {
315: return make(new InputStreamReader(dstream), startline,
316: startcolumn, buffersize);
317: }
318:
319: /**
320: * Constructor for the CharStream object
321: *
322: *@param dstream Description of Parameter
323: *@param startline Description of Parameter
324: *@param startcolumn Description of Parameter
325: *@return Description of the Returned Value
326: */
327: public static CharStream make(java.io.InputStream dstream,
328: int startline, int startcolumn) {
329: return make(new InputStreamReader(dstream), startline,
330: startcolumn, 4096);
331: }
332: }
|