KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > expression > ast > SimpleCharStream


1 /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 3.0 */
2 /**************************************************************************************
3  * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
4  * http://aspectwerkz.codehaus.org *
5  * ---------------------------------------------------------------------------------- *
6  * The software in this package is published under the terms of the LGPL license *
7  * a copy of which has been included with this distribution in the license.txt file. *
8  **************************************************************************************/

9 package org.codehaus.aspectwerkz.expression.ast;
10
11 /**
12  * An implementation of interface CharStream, where the stream is assumed to contain only ASCII characters (without
13  * unicode processing).
14  */

15
16 public class SimpleCharStream {
17     public static final boolean staticFlag = true;
18
19     static int bufsize;
20
21     static int available;
22
23     static int tokenBegin;
24
25     static public int bufpos = -1;
26
27     static protected int bufline[];
28
29     static protected int bufcolumn[];
30
31     static protected int column = 0;
32
33     static protected int line = 1;
34
35     static protected boolean prevCharIsCR = false;
36
37     static protected boolean prevCharIsLF = false;
38
39     static protected java.io.Reader JavaDoc inputStream;
40
41     static protected char[] buffer;
42
43     static protected int maxNextCharInd = 0;
44
45     static protected int inBuf = 0;
46
47     static protected void ExpandBuff(boolean wrapAround) {
48         char[] newbuffer = new char[bufsize + 2048];
49         int newbufline[] = new int[bufsize + 2048];
50         int newbufcolumn[] = new int[bufsize + 2048];
51
52         try {
53             if (wrapAround) {
54                 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
55                 System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
56                 buffer = newbuffer;
57
58                 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
59                 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
60                 bufline = newbufline;
61
62                 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
63                 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
64                 bufcolumn = newbufcolumn;
65
66                 maxNextCharInd = (bufpos += (bufsize - tokenBegin));
67             } else {
68                 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
69                 buffer = newbuffer;
70
71                 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
72                 bufline = newbufline;
73
74                 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
75                 bufcolumn = newbufcolumn;
76
77                 maxNextCharInd = (bufpos -= tokenBegin);
78             }
79         } catch (Throwable JavaDoc t) {
80             throw new Error JavaDoc(t.getMessage());
81         }
82
83         bufsize += 2048;
84         available = bufsize;
85         tokenBegin = 0;
86     }
87
88     static protected void FillBuff() throws java.io.IOException JavaDoc {
89         if (maxNextCharInd == available) {
90             if (available == bufsize) {
91                 if (tokenBegin > 2048) {
92                     bufpos = maxNextCharInd = 0;
93                     available = tokenBegin;
94                 } else if (tokenBegin < 0) {
95                     bufpos = maxNextCharInd = 0;
96                 } else {
97                     ExpandBuff(false);
98                 }
99             } else if (available > tokenBegin) {
100                 available = bufsize;
101             } else if ((tokenBegin - available) < 2048) {
102                 ExpandBuff(true);
103             } else {
104                 available = tokenBegin;
105             }
106         }
107
108         int i;
109         try {
110             if ((i = inputStream.read(buffer, maxNextCharInd, available - maxNextCharInd)) == -1) {
111                 inputStream.close();
112                 throw new java.io.IOException JavaDoc();
113             } else {
114                 maxNextCharInd += i;
115             }
116             return;
117         } catch (java.io.IOException JavaDoc e) {
118             --bufpos;
119             backup(0);
120             if (tokenBegin == -1) {
121                 tokenBegin = bufpos;
122             }
123             throw e;
124         }
125     }
126
127     static public char BeginToken() throws java.io.IOException JavaDoc {
128         tokenBegin = -1;
129         char c = readChar();
130         tokenBegin = bufpos;
131
132         return c;
133     }
134
135     static protected void UpdateLineColumn(char c) {
136         column++;
137
138         if (prevCharIsLF) {
139             prevCharIsLF = false;
140             line += (column = 1);
141         } else if (prevCharIsCR) {
142             prevCharIsCR = false;
143             if (c == '\n') {
144                 prevCharIsLF = true;
145             } else {
146                 line += (column = 1);
147             }
148         }
149
150         switch (c) {
151             case '\r':
152                 prevCharIsCR = true;
153                 break;
154             case '\n':
155                 prevCharIsLF = true;
156                 break;
157             case '\t':
158                 column--;
159                 column += (8 - (column & 07));
160                 break;
161             default:
162                 break;
163         }
164
165         bufline[bufpos] = line;
166         bufcolumn[bufpos] = column;
167     }
168
169     static public char readChar() throws java.io.IOException JavaDoc {
170         if (inBuf > 0) {
171             --inBuf;
172
173             if (++bufpos == bufsize) {
174                 bufpos = 0;
175             }
176
177             return buffer[bufpos];
178         }
179
180         if (++bufpos >= maxNextCharInd) {
181             FillBuff();
182         }
183
184         char c = buffer[bufpos];
185
186         UpdateLineColumn(c);
187         return (c);
188     }
189
190     /**
191      * @deprecated @see #getEndColumn
192      */

193
194     static public int getColumn() {
195         return bufcolumn[bufpos];
196     }
197
198     /**
199      * @deprecated @see #getEndLine
200      */

201
202     static public int getLine() {
203         return bufline[bufpos];
204     }
205
206     static public int getEndColumn() {
207         return bufcolumn[bufpos];
208     }
209
210     static public int getEndLine() {
211         return bufline[bufpos];
212     }
213
214     static public int getBeginColumn() {
215         return bufcolumn[tokenBegin];
216     }
217
218     static public int getBeginLine() {
219         return bufline[tokenBegin];
220     }
221
222     static public void backup(int amount) {
223
224         inBuf += amount;
225         if ((bufpos -= amount) < 0) {
226             bufpos += bufsize;
227         }
228     }
229
230     public SimpleCharStream(java.io.Reader JavaDoc dstream, int startline, int startcolumn, int buffersize) {
231         if (inputStream != null) {
232             throw new Error JavaDoc(
233                     "\n ERROR: Second call to the constructor of a static SimpleCharStream. You must\n"
234                     + " either use ReInit() or set the JavaCC option STATIC to false\n"
235                     + " during the generation of this class."
236             );
237         }
238         inputStream = dstream;
239         line = startline;
240         column = startcolumn - 1;
241
242         available = bufsize = buffersize;
243         buffer = new char[buffersize];
244         bufline = new int[buffersize];
245         bufcolumn = new int[buffersize];
246     }
247
248     public SimpleCharStream(java.io.Reader JavaDoc dstream, int startline, int startcolumn) {
249         this(dstream, startline, startcolumn, 4096);
250     }
251
252     public SimpleCharStream(java.io.Reader JavaDoc dstream) {
253         this(dstream, 1, 1, 4096);
254     }
255
256     public void ReInit(java.io.Reader JavaDoc dstream, int startline, int startcolumn, int buffersize) {
257         inputStream = dstream;
258         line = startline;
259         column = startcolumn - 1;
260
261         if (buffer == null || buffersize != buffer.length) {
262             available = bufsize = buffersize;
263             buffer = new char[buffersize];
264             bufline = new int[buffersize];
265             bufcolumn = new int[buffersize];
266         }
267         prevCharIsLF = prevCharIsCR = false;
268         tokenBegin = inBuf = maxNextCharInd = 0;
269         bufpos = -1;
270     }
271
272     public void ReInit(java.io.Reader JavaDoc dstream, int startline, int startcolumn) {
273         ReInit(dstream, startline, startcolumn, 4096);
274     }
275
276     public void ReInit(java.io.Reader JavaDoc dstream) {
277         ReInit(dstream, 1, 1, 4096);
278     }
279
280     public SimpleCharStream(java.io.InputStream JavaDoc dstream, int startline, int startcolumn, int buffersize) {
281         this(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
282     }
283
284     public SimpleCharStream(java.io.InputStream JavaDoc dstream, int startline, int startcolumn) {
285         this(dstream, startline, startcolumn, 4096);
286     }
287
288     public SimpleCharStream(java.io.InputStream JavaDoc dstream) {
289         this(dstream, 1, 1, 4096);
290     }
291
292     public void ReInit(java.io.InputStream JavaDoc dstream, int startline, int startcolumn, int buffersize) {
293         ReInit(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
294     }
295
296     public void ReInit(java.io.InputStream JavaDoc dstream) {
297         ReInit(dstream, 1, 1, 4096);
298     }
299
300     public void ReInit(java.io.InputStream JavaDoc dstream, int startline, int startcolumn) {
301         ReInit(dstream, startline, startcolumn, 4096);
302     }
303
304     static public String JavaDoc GetImage() {
305         if (bufpos >= tokenBegin) {
306             return new String JavaDoc(buffer, tokenBegin, bufpos - tokenBegin + 1);
307         } else {
308             return new String JavaDoc(buffer, tokenBegin, bufsize - tokenBegin) + new String JavaDoc(buffer, 0, bufpos + 1);
309         }
310     }
311
312     static public char[] GetSuffix(int len) {
313         char[] ret = new char[len];
314
315         if ((bufpos + 1) >= len) {
316             System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
317         } else {
318             System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, len - bufpos - 1);
319             System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
320         }
321
322         return ret;
323     }
324
325     static public void Done() {
326         buffer = null;
327         bufline = null;
328         bufcolumn = null;
329     }
330
331     /**
332      * Method to adjust line and column numbers for the start of a token.
333      */

334     static public void adjustBeginLineColumn(int newLine, int newCol) {
335         int start = tokenBegin;
336         int len;
337
338         if (bufpos >= tokenBegin) {
339             len = bufpos - tokenBegin + inBuf + 1;
340         } else {
341             len = bufsize - tokenBegin + bufpos + 1 + inBuf;
342         }
343
344         int i = 0, j = 0, k = 0;
345         int nextColDiff = 0, columnDiff = 0;
346
347         while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) {
348             bufline[j] = newLine;
349             nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
350             bufcolumn[j] = newCol + columnDiff;
351             columnDiff = nextColDiff;
352             i++;
353         }
354
355         if (i < len) {
356             bufline[j] = newLine++;
357             bufcolumn[j] = newCol + columnDiff;
358
359             while (i++ < len) {
360                 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) {
361                     bufline[j] = newLine++;
362                 } else {
363                     bufline[j] = newLine;
364                 }
365             }
366         }
367
368         line = bufline[j];
369         column = bufcolumn[j];
370     }
371
372 }
Popular Tags