KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > scriptengine > core > JavaCharStream


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

326
327   public int getColumn()
328   {
329     return bufcolumn[bufpos];
330   }
331
332   /**
333    * @deprecated
334    * @see #getEndLine
335    */

336
337   public int getLine()
338   {
339     return bufline[bufpos];
340   }
341
342   public int getEndColumn()
343   {
344     return bufcolumn[bufpos];
345   }
346
347   public int getEndLine()
348   {
349     return bufline[bufpos];
350   }
351
352   public int getBeginColumn()
353   {
354     return bufcolumn[tokenBegin];
355   }
356
357   public int getBeginLine()
358   {
359     return bufline[tokenBegin];
360   }
361
362   public void backup(int amount)
363   {
364
365     inBuf += amount;
366     if ((bufpos -= amount) < 0) bufpos += bufsize;
367   }
368
369   public JavaCharStream(java.io.Reader JavaDoc dstream, int startline, int startcolumn, int buffersize)
370   {
371     inputStream = dstream;
372     line = startline;
373     column = startcolumn - 1;
374
375     available = bufsize = buffersize;
376     buffer = new char[buffersize];
377     bufline = new int[buffersize];
378     bufcolumn = new int[buffersize];
379     nextCharBuf = new char[4096];
380   }
381
382   public JavaCharStream(java.io.Reader JavaDoc dstream, int startline, int startcolumn)
383   {
384     this(dstream, startline, startcolumn, 4096);
385   }
386
387   public JavaCharStream(java.io.Reader JavaDoc dstream)
388   {
389     this(dstream, 1, 1, 4096);
390   }
391
392   public void ReInit(java.io.Reader JavaDoc dstream, int startline, int startcolumn, int buffersize)
393   {
394     inputStream = dstream;
395     line = startline;
396     column = startcolumn - 1;
397
398     if (buffer == null || buffersize != buffer.length)
399     {
400       available = bufsize = buffersize;
401       buffer = new char[buffersize];
402       bufline = new int[buffersize];
403       bufcolumn = new int[buffersize];
404       nextCharBuf = new char[4096];
405     }
406     prevCharIsLF = prevCharIsCR = false;
407     tokenBegin = inBuf = maxNextCharInd = 0;
408     nextCharInd = bufpos = -1;
409   }
410
411   public void ReInit(java.io.Reader JavaDoc dstream, int startline, int startcolumn)
412   {
413     ReInit(dstream, startline, startcolumn, 4096);
414   }
415
416   public void ReInit(java.io.Reader JavaDoc dstream)
417   {
418     ReInit(dstream, 1, 1, 4096);
419   }
420
421   public JavaCharStream(java.io.InputStream JavaDoc dstream, int startline, int startcolumn, int buffersize)
422   {
423     this(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
424   }
425
426   public JavaCharStream(java.io.InputStream JavaDoc dstream, int startline, int startcolumn)
427   {
428     this(dstream, startline, startcolumn, 4096);
429   }
430
431   public JavaCharStream(java.io.InputStream JavaDoc dstream)
432   {
433     this(dstream, 1, 1, 4096);
434   }
435
436   public void ReInit(java.io.InputStream JavaDoc dstream, int startline, int startcolumn, int buffersize)
437   {
438     ReInit(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
439   }
440
441   public void ReInit(java.io.InputStream JavaDoc dstream, int startline, int startcolumn)
442   {
443     ReInit(dstream, startline, startcolumn, 4096);
444   }
445
446   public void ReInit(java.io.InputStream JavaDoc dstream)
447   {
448     ReInit(dstream, 1, 1, 4096);
449   }
450
451   public String JavaDoc GetImage()
452   {
453     if (bufpos >= tokenBegin) return new String JavaDoc(buffer, tokenBegin, bufpos - tokenBegin + 1);
454     else return new String JavaDoc(buffer, tokenBegin, bufsize - tokenBegin) + new String JavaDoc(buffer, 0, bufpos + 1);
455   }
456
457   public char[] GetSuffix(int len)
458   {
459     char[] ret = new char[len];
460
461     if ((bufpos + 1) >= len) System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
462     else
463     {
464       System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, len - bufpos - 1);
465       System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
466     }
467
468     return ret;
469   }
470
471   public void Done()
472   {
473     nextCharBuf = null;
474     buffer = null;
475     bufline = null;
476     bufcolumn = null;
477   }
478
479   /**
480    * Method to adjust line and column numbers for the start of a token.<BR>
481    */

482   public void adjustBeginLineColumn(int newLine, int newCol)
483   {
484     int start = tokenBegin;
485     int len;
486
487     if (bufpos >= tokenBegin)
488     {
489       len = bufpos - tokenBegin + inBuf + 1;
490     }
491     else
492     {
493       len = bufsize - tokenBegin + bufpos + 1 + inBuf;
494     }
495
496     int i = 0, j = 0, k = 0;
497     int nextColDiff = 0, columnDiff = 0;
498
499     while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
500     {
501       bufline[j] = newLine;
502       nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
503       bufcolumn[j] = newCol + columnDiff;
504       columnDiff = nextColDiff;
505       i++;
506     }
507
508     if (i < len)
509     {
510       bufline[j] = newLine++;
511       bufcolumn[j] = newCol + columnDiff;
512
513       while (i++ < len)
514       {
515         if (bufline[j = start % bufsize] != bufline[++start % bufsize]) bufline[j] = newLine++;
516         else bufline[j] = newLine;
517       }
518     }
519
520     line = bufline[j];
521     column = bufcolumn[j];
522   }
523
524 }
Popular Tags