KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > ext > parsers > java_1_2 > JavaCharStream


1 /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 2.1 */
2 package org.ejen.ext.parsers.java_1_2;
3
4 /**
5  * An implementation of interface CharStream, where the stream is assumed to
6  * contain only ASCII characters (with java-like unicode escape processing).
7  */

8 public final class JavaCharStream {
9     public static final boolean staticFlag = true;
10     static final int hexval(char c) throws java.io.IOException JavaDoc {
11         switch (c) {
12         case '0':
13             return 0;
14
15         case '1':
16             return 1;
17
18         case '2':
19             return 2;
20
21         case '3':
22             return 3;
23
24         case '4':
25             return 4;
26
27         case '5':
28             return 5;
29
30         case '6':
31             return 6;
32
33         case '7':
34             return 7;
35
36         case '8':
37             return 8;
38
39         case '9':
40             return 9;
41
42         case 'a':
43         case 'A':
44             return 10;
45
46         case 'b':
47         case 'B':
48             return 11;
49
50         case 'c':
51         case 'C':
52             return 12;
53
54         case 'd':
55         case 'D':
56             return 13;
57
58         case 'e':
59         case 'E':
60             return 14;
61
62         case 'f':
63         case 'F':
64             return 15;
65         }
66         throw new java.io.IOException JavaDoc(); // Should never come here
67
}
68     static public int bufpos = -1;
69     static int bufsize;
70     static int available;
71     static int tokenBegin;
72     static private int bufline[];
73     static private int bufcolumn[];
74     static private int column = 0;
75     static private int line = 1;
76     static private boolean prevCharIsCR = false;
77     static private boolean prevCharIsLF = false;
78     static private java.io.Reader JavaDoc inputStream;
79     static private char[] nextCharBuf;
80     static private char[] buffer;
81     static private int maxNextCharInd = 0;
82     static private int nextCharInd = -1;
83     static private int inBuf = 0;
84     static private final void ExpandBuff(boolean wrapAround) {
85         char[] newbuffer = new char[bufsize + 2048];
86         int newbufline[] = new int[bufsize + 2048];
87         int newbufcolumn[] = new int[bufsize + 2048];
88
89         try {
90             if (wrapAround) {
91                 System.arraycopy(buffer, tokenBegin, newbuffer, 0,
92                         bufsize - tokenBegin);
93                 System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin,
94                         bufpos);
95                 buffer = newbuffer;
96                 System.arraycopy(bufline, tokenBegin, newbufline, 0,
97                         bufsize - tokenBegin);
98                 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin,
99                         bufpos);
100                 bufline = newbufline;
101                 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0,
102                         bufsize - tokenBegin);
103                 System.arraycopy(bufcolumn, 0, newbufcolumn,
104                         bufsize - tokenBegin, bufpos);
105                 bufcolumn = newbufcolumn;
106                 bufpos += (bufsize - tokenBegin);
107             } else {
108                 System.arraycopy(buffer, tokenBegin, newbuffer, 0,
109                         bufsize - tokenBegin);
110                 buffer = newbuffer;
111                 System.arraycopy(bufline, tokenBegin, newbufline, 0,
112                         bufsize - tokenBegin);
113                 bufline = newbufline;
114                 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0,
115                         bufsize - tokenBegin);
116                 bufcolumn = newbufcolumn;
117                 bufpos -= tokenBegin;
118             }
119         } catch (Throwable JavaDoc t) {
120             throw new Error JavaDoc(t.getMessage());
121         }
122         available = (bufsize += 2048);
123         tokenBegin = 0;
124     }
125
126     static private final void FillBuff() throws java.io.IOException JavaDoc {
127         int i;
128
129         if (maxNextCharInd == 4096) {
130             maxNextCharInd = nextCharInd = 0;
131         }
132         try {
133             if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
134                             4096 - maxNextCharInd))
135                     == -1) {
136                 inputStream.close();
137                 throw new java.io.IOException JavaDoc();
138             } else {
139                 maxNextCharInd += i;
140             }
141             return;
142         } catch (java.io.IOException JavaDoc e) {
143             if (bufpos != 0) {
144                 --bufpos;
145                 backup(0);
146             } else {
147                 bufline[bufpos] = line;
148                 bufcolumn[bufpos] = column;
149             }
150             throw e;
151         }
152     }
153
154     static private final char ReadByte() throws java.io.IOException JavaDoc {
155         if (++nextCharInd >= maxNextCharInd) {
156             FillBuff();
157         }
158         return nextCharBuf[nextCharInd];
159     }
160
161     static public final char BeginToken() throws java.io.IOException JavaDoc {
162         if (inBuf > 0) {
163             --inBuf;
164             if (++bufpos == bufsize) {
165                 bufpos = 0;
166             }
167             tokenBegin = bufpos;
168             return buffer[bufpos];
169         }
170         tokenBegin = 0;
171         bufpos = -1;
172         return readChar();
173     }
174
175     static private final void AdjustBuffSize() {
176         if (available == bufsize) {
177             if (tokenBegin > 2048) {
178                 bufpos = 0;
179                 available = tokenBegin;
180             } else {
181                 ExpandBuff(false);
182             }
183         } else if (available > tokenBegin) {
184             available = bufsize;
185         } else if ((tokenBegin - available) < 2048) {
186             ExpandBuff(true);
187         } else {
188             available = tokenBegin;
189         }
190     }
191
192     static private final void UpdateLineColumn(char c) {
193         column++;
194         if (prevCharIsLF) {
195             prevCharIsLF = false;
196             line += (column = 1);
197         } else if (prevCharIsCR) {
198             prevCharIsCR = false;
199             if (c == '\n') {
200                 prevCharIsLF = true;
201             } else {
202                 line += (column = 1);
203             }
204         }
205         switch (c) {
206         case '\r':
207             prevCharIsCR = true;
208             break;
209
210         case '\n':
211             prevCharIsLF = true;
212             break;
213
214         case '\t':
215             column--;
216             column += (8 - (column & 07));
217             break;
218
219         default:
220             break;
221         }
222         bufline[bufpos] = line;
223         bufcolumn[bufpos] = column;
224     }
225
226     static public final char readChar() throws java.io.IOException JavaDoc {
227         if (inBuf > 0) {
228             --inBuf;
229             if (++bufpos == bufsize) {
230                 bufpos = 0;
231             }
232             return buffer[bufpos];
233         }
234         char c;
235
236         if (++bufpos == available) {
237             AdjustBuffSize();
238         }
239         if ((buffer[bufpos] = c = ReadByte()) == '\\') {
240             UpdateLineColumn(c);
241             int backSlashCnt = 1;
242
243             for (;;) // Read all the backslashes
244
{
245                 if (++bufpos == available) {
246                     AdjustBuffSize();
247                 }
248                 try {
249                     if ((buffer[bufpos] = c = ReadByte()) != '\\') {
250                         UpdateLineColumn(c);
251                         // found a non-backslash char.
252
if ((c == 'u') && ((backSlashCnt & 1) == 1)) {
253                             if (--bufpos < 0) {
254                                 bufpos = bufsize - 1;
255                             }
256                             break;
257                         }
258                         backup(backSlashCnt);
259                         return '\\';
260                     }
261                 } catch (java.io.IOException JavaDoc e) {
262                     if (backSlashCnt > 1) {
263                         backup(backSlashCnt);
264                     }
265                     return '\\';
266                 }
267                 UpdateLineColumn(c);
268                 backSlashCnt++;
269             }
270             // Here, we have seen an odd number of backslash's followed by a 'u'
271
try {
272                 while ((c = ReadByte()) == 'u') {
273                     ++column;
274                 }
275                 buffer[bufpos] = c = (char) (hexval(c) << 12
276                         | hexval(ReadByte()) << 8 | hexval(ReadByte()) << 4
277                         | hexval(ReadByte()));
278                 column += 4;
279             } catch (java.io.IOException JavaDoc e) {
280                 throw new Error JavaDoc("Invalid escape character at line " + line
281                         + " column " + column + ".");
282             }
283             if (backSlashCnt == 1) {
284                 return c;
285             } else {
286                 backup(backSlashCnt - 1);
287                 return '\\';
288             }
289         } else {
290             UpdateLineColumn(c);
291             return (c);
292         }
293     }
294
295     /**
296      * @deprecated
297      * @see #getEndColumn
298      */

299     static public final int getColumn() {
300         return bufcolumn[bufpos];
301     }
302
303     /**
304      * @deprecated
305      * @see #getEndLine
306      */

307     static public final int getLine() {
308         return bufline[bufpos];
309     }
310
311     static public final int getEndColumn() {
312         return bufcolumn[bufpos];
313     }
314
315     static public final int getEndLine() {
316         return bufline[bufpos];
317     }
318
319     static public final int getBeginColumn() {
320         return bufcolumn[tokenBegin];
321     }
322
323     static public final int getBeginLine() {
324         return bufline[tokenBegin];
325     }
326
327     static public final void backup(int amount) {
328         inBuf += amount;
329         if ((bufpos -= amount) < 0) {
330             bufpos += bufsize;
331         }
332     }
333
334     public JavaCharStream(java.io.Reader JavaDoc dstream,
335             int startline, int startcolumn, int buffersize) {
336         if (inputStream != null) {
337             throw new Error JavaDoc("\n ERROR: Second call to the constructor of a static JavaCharStream. You must\n"
338                     + " either use ReInit() or set the JavaCC option STATIC to false\n"
339                     + " during the generation of this class.");
340         }
341         inputStream = dstream;
342         line = startline;
343         column = startcolumn - 1;
344         available = bufsize = buffersize;
345         buffer = new char[buffersize];
346         bufline = new int[buffersize];
347         bufcolumn = new int[buffersize];
348         nextCharBuf = new char[4096];
349     }
350
351     public JavaCharStream(java.io.Reader JavaDoc dstream,
352             int startline, int startcolumn) {
353         this(dstream, startline, startcolumn, 4096);
354     }
355
356     public JavaCharStream(java.io.Reader JavaDoc dstream) {
357         this(dstream, 1, 1, 4096);
358     }
359
360     public void ReInit(java.io.Reader JavaDoc dstream,
361             int startline, int startcolumn, int buffersize) {
362         inputStream = dstream;
363         line = startline;
364         column = startcolumn - 1;
365         if (buffer == null || buffersize != buffer.length) {
366             available = bufsize = buffersize;
367             buffer = new char[buffersize];
368             bufline = new int[buffersize];
369             bufcolumn = new int[buffersize];
370             nextCharBuf = new char[4096];
371         }
372         prevCharIsLF = prevCharIsCR = false;
373         tokenBegin = inBuf = maxNextCharInd = 0;
374         nextCharInd = bufpos = -1;
375     }
376
377     public void ReInit(java.io.Reader JavaDoc dstream,
378             int startline, int startcolumn) {
379         ReInit(dstream, startline, startcolumn, 4096);
380     }
381
382     public void ReInit(java.io.Reader JavaDoc dstream) {
383         ReInit(dstream, 1, 1, 4096);
384     }
385
386     public JavaCharStream(java.io.InputStream JavaDoc dstream, int startline,
387             int startcolumn, int buffersize) {
388         this(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn,
389                 4096);
390     }
391
392     public JavaCharStream(java.io.InputStream JavaDoc dstream, int startline,
393             int startcolumn) {
394         this(dstream, startline, startcolumn, 4096);
395     }
396
397     public JavaCharStream(java.io.InputStream JavaDoc dstream) {
398         this(dstream, 1, 1, 4096);
399     }
400
401     public void ReInit(java.io.InputStream JavaDoc dstream, int startline,
402             int startcolumn, int buffersize) {
403         ReInit(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn,
404                 4096);
405     }
406
407     public void ReInit(java.io.InputStream JavaDoc dstream, int startline,
408             int startcolumn) {
409         ReInit(dstream, startline, startcolumn, 4096);
410     }
411
412     public void ReInit(java.io.InputStream JavaDoc dstream) {
413         ReInit(dstream, 1, 1, 4096);
414     }
415
416     static public final String JavaDoc GetImage() {
417         if (bufpos >= tokenBegin) {
418             return new String JavaDoc(buffer, tokenBegin, bufpos - tokenBegin + 1);
419         } else {
420             return new String JavaDoc(buffer, tokenBegin, bufsize - tokenBegin)
421                     + new String JavaDoc(buffer, 0, bufpos + 1);
422         }
423     }
424
425     static public final char[] GetSuffix(int len) {
426         char[] ret = new char[len];
427
428         if ((bufpos + 1) >= len) {
429             System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
430         } else {
431             System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
432                     len - bufpos - 1);
433             System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
434         }
435         return ret;
436     }
437
438     static public void Done() {
439         nextCharBuf = null;
440         buffer = null;
441         bufline = null;
442         bufcolumn = null;
443     }
444
445     /**
446      * Method to adjust line and column numbers for the start of a token.<BR>
447      */

448     static public void adjustBeginLineColumn(int newLine, int newCol) {
449         int start = tokenBegin;
450         int len;
451
452         if (bufpos >= tokenBegin) {
453             len = bufpos - tokenBegin + inBuf + 1;
454         } else {
455             len = bufsize - tokenBegin + bufpos + 1 + inBuf;
456         }
457         int i = 0, j = 0, k = 0;
458         int nextColDiff = 0, columnDiff = 0;
459
460         while (i < len
461                 && bufline[j = start % bufsize]
462                         == bufline[k = ++start % bufsize]) {
463             bufline[j] = newLine;
464             nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
465             bufcolumn[j] = newCol + columnDiff;
466             columnDiff = nextColDiff;
467             i++;
468         }
469         if (i < len) {
470             bufline[j] = newLine++;
471             bufcolumn[j] = newCol + columnDiff;
472             while (i++ < len) {
473                 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) {
474                     bufline[j] = newLine++;
475                 } else {
476                     bufline[j] = newLine;
477                 }
478             }
479         }
480         line = bufline[j];
481         column = bufcolumn[j];
482     }
483 }
484
Popular Tags