KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > util > javasource > jjt > JavaCharStream


1 /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 3.0 */
2 package de.gulden.util.javasource.jjt;
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     
12       /*--- Jens --- (MIND THE MODIFICATIONS THROUGHOUT THIS FILE!) */
13       TextImage text_image;
14     
15   public static final boolean staticFlag = false;
16   static final int hexval(char c) throws java.io.IOException JavaDoc {
17     switch(c)
18     {
19        case '0' :
20           return 0;
21        case '1' :
22           return 1;
23        case '2' :
24           return 2;
25        case '3' :
26           return 3;
27        case '4' :
28           return 4;
29        case '5' :
30           return 5;
31        case '6' :
32           return 6;
33        case '7' :
34           return 7;
35        case '8' :
36           return 8;
37        case '9' :
38           return 9;
39
40        case 'a' :
41        case 'A' :
42           return 10;
43        case 'b' :
44        case 'B' :
45           return 11;
46        case 'c' :
47        case 'C' :
48           return 12;
49        case 'd' :
50        case 'D' :
51           return 13;
52        case 'e' :
53        case 'E' :
54           return 14;
55        case 'f' :
56        case 'F' :
57           return 15;
58     }
59
60     throw new java.io.IOException JavaDoc(); // Should never come here
61
}
62
63   public int bufpos = -1;
64   int bufsize;
65   int available;
66   int tokenBegin;
67   protected int bufline[];
68   protected int bufcolumn[];
69
70   protected int column = 0;
71   protected int line = 1;
72
73   protected boolean prevCharIsCR = false;
74   protected boolean prevCharIsLF = false;
75
76   protected java.io.Reader JavaDoc inputStream;
77
78   protected char[] nextCharBuf;
79   protected char[] buffer;
80   protected int maxNextCharInd = 0;
81   protected int nextCharInd = -1;
82   protected int inBuf = 0;
83
84   protected void ExpandBuff(boolean wrapAround)
85   {
86      char[] newbuffer = new char[bufsize + 2048];
87      int newbufline[] = new int[bufsize + 2048];
88      int newbufcolumn[] = new int[bufsize + 2048];
89
90      try
91      {
92         if (wrapAround)
93         {
94            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
95            System.arraycopy(buffer, 0, newbuffer,
96                                              bufsize - tokenBegin, bufpos);
97            buffer = newbuffer;
98
99            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
100            System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
101            bufline = newbufline;
102
103            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
104            System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
105            bufcolumn = newbufcolumn;
106
107            bufpos += (bufsize - tokenBegin);
108         }
109         else
110         {
111            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
112            buffer = newbuffer;
113
114            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
115            bufline = newbufline;
116
117            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
118            bufcolumn = newbufcolumn;
119
120            bufpos -= tokenBegin;
121         }
122      }
123      catch (Throwable JavaDoc t)
124      {
125         throw new Error JavaDoc(t.getMessage());
126      }
127
128      available = (bufsize += 2048);
129      tokenBegin = 0;
130   }
131
132   protected void FillBuff() throws java.io.IOException JavaDoc
133   {
134      int i;
135      if (maxNextCharInd == 4096)
136         maxNextCharInd = nextCharInd = 0;
137
138      try {
139         if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
140                                             4096 - maxNextCharInd)) == -1)
141         {
142            inputStream.close();
143            throw new java.io.IOException JavaDoc();
144         }
145         else
146            maxNextCharInd += i;
147         return;
148      }
149      catch(java.io.IOException JavaDoc e) {
150         if (bufpos != 0)
151         {
152            --bufpos;
153            backup(0);
154         }
155         else
156         {
157            bufline[bufpos] = line;
158            bufcolumn[bufpos] = column;
159         }
160         throw e;
161      }
162   }
163
164   protected char ReadByte() throws java.io.IOException JavaDoc
165   {
166      if (++nextCharInd >= maxNextCharInd)
167         FillBuff();
168
169      return nextCharBuf[nextCharInd];
170   }
171
172   public char BeginToken() throws java.io.IOException JavaDoc
173   {
174      if (inBuf > 0)
175      {
176         --inBuf;
177
178         if (++bufpos == bufsize)
179            bufpos = 0;
180
181         tokenBegin = bufpos;
182         return buffer[bufpos];
183      }
184
185      tokenBegin = 0;
186      bufpos = -1;
187
188      return readChar();
189   }
190
191   protected void AdjustBuffSize()
192   {
193      if (available == bufsize)
194      {
195         if (tokenBegin > 2048)
196         {
197            bufpos = 0;
198            available = tokenBegin;
199         }
200         else
201            ExpandBuff(false);
202      }
203      else if (available > tokenBegin)
204         available = bufsize;
205      else if ((tokenBegin - available) < 2048)
206         ExpandBuff(true);
207      else
208         available = tokenBegin;
209   }
210
211   protected void UpdateLineColumn(char c)
212   {
213      column++;
214
215      if (prevCharIsLF)
216      {
217         prevCharIsLF = false;
218         line += (column = 1);
219         /*--- Jens ---*/
220         text_image.nextLine();
221      }
222      else if (prevCharIsCR)
223      {
224         prevCharIsCR = false;
225         if (c == '\n')
226         {
227            prevCharIsLF = true;
228         }
229         else
230            line += (column = 1);
231         /*--- Jens ---*/
232         text_image.nextLine();
233      }
234
235      switch (c)
236      {
237         case '\r' :
238            prevCharIsCR = true;
239            break;
240         case '\n' :
241            prevCharIsLF = true;
242            break;
243 /* --- removed, Jens
244         case '\t' :
245            column--;
246            column += (8 - (column & 07));
247            break;
248 */

249         default :
250            break;
251      }
252
253      bufline[bufpos] = line;
254      bufcolumn[bufpos] = column;
255
256      /*--- Jens ---*/
257      text_image.write(c);
258   }
259
260   public char readChar() throws java.io.IOException JavaDoc
261   {
262      if (inBuf > 0)
263      {
264         --inBuf;
265
266         if (++bufpos == bufsize)
267            bufpos = 0;
268
269         return buffer[bufpos];
270      }
271
272      char c;
273
274      if (++bufpos == available)
275         AdjustBuffSize();
276
277      if ((buffer[bufpos] = c = ReadByte()) == '\\')
278      {
279         UpdateLineColumn(c);
280
281         int backSlashCnt = 1;
282
283         for (;;) // Read all the backslashes
284
{
285            if (++bufpos == available)
286               AdjustBuffSize();
287
288            try
289            {
290               if ((buffer[bufpos] = c = ReadByte()) != '\\')
291               {
292                  UpdateLineColumn(c);
293                  // found a non-backslash char.
294
if ((c == 'u') && ((backSlashCnt & 1) == 1))
295                  {
296                     if (--bufpos < 0)
297                        bufpos = bufsize - 1;
298
299                     break;
300                  }
301
302                  backup(backSlashCnt);
303                  return '\\';
304               }
305            }
306            catch(java.io.IOException JavaDoc e)
307            {
308               if (backSlashCnt > 1)
309                  backup(backSlashCnt);
310
311               return '\\';
312            }
313
314            UpdateLineColumn(c);
315            backSlashCnt++;
316         }
317
318         // Here, we have seen an odd number of backslash's followed by a 'u'
319
/* --- Jens --- this disables unicode-processing - we want to preserve the original source
320         try
321         {
322            while ((c = ReadByte()) == 'u')
323               ++column;
324
325            buffer[bufpos] = c = (char)(hexval(c) << 12 |
326                                        hexval(ReadByte()) << 8 |
327                                        hexval(ReadByte()) << 4 |
328                                        hexval(ReadByte()));
329
330            column += 4;
331         }
332         catch(java.io.IOException e)
333         {
334            throw new Error("Invalid escape character at line " + line +
335                                          " column " + column + ".");
336         }
337  --- Jens --- */

338
339         if (backSlashCnt == 1)
340            return c;
341         else
342         {
343            backup(backSlashCnt - 1);
344            return '\\';
345         }
346      }
347      else
348      {
349         UpdateLineColumn(c);
350         return (c);
351      }
352   }
353
354   /**
355    * @deprecated
356    * @see #getEndColumn
357    */

358
359   public int getColumn() {
360      return bufcolumn[bufpos];
361   }
362
363   /**
364    * @deprecated
365    * @see #getEndLine
366    */

367
368   public int getLine() {
369      return bufline[bufpos];
370   }
371
372   public int getEndColumn() {
373      return bufcolumn[bufpos];
374   }
375
376   public int getEndLine() {
377      return bufline[bufpos];
378   }
379
380   public int getBeginColumn() {
381      return bufcolumn[tokenBegin];
382   }
383
384   public int getBeginLine() {
385      return bufline[tokenBegin];
386   }
387
388   public void backup(int amount) {
389
390     inBuf += amount;
391     if ((bufpos -= amount) < 0)
392        bufpos += bufsize;
393   }
394
395   public JavaCharStream(java.io.Reader JavaDoc dstream,
396                  int startline, int startcolumn, int buffersize)
397   {
398     inputStream = dstream;
399     line = startline;
400     column = startcolumn - 1;
401
402     available = bufsize = buffersize;
403     buffer = new char[buffersize];
404     bufline = new int[buffersize];
405     bufcolumn = new int[buffersize];
406     nextCharBuf = new char[4096];
407
408     /*--- Jens ---*/
409     text_image=new TextImage();
410 }
411
412   public JavaCharStream(java.io.Reader JavaDoc dstream,
413                                         int startline, int startcolumn)
414   {
415      this(dstream, startline, startcolumn, 4096);
416   }
417
418   public JavaCharStream(java.io.Reader JavaDoc dstream)
419   {
420      this(dstream, 1, 1, 4096);
421   }
422   public void ReInit(java.io.Reader JavaDoc dstream,
423                  int startline, int startcolumn, int buffersize)
424   {
425     inputStream = dstream;
426     line = startline;
427     column = startcolumn - 1;
428
429     if (buffer == null || buffersize != buffer.length)
430     {
431       available = bufsize = buffersize;
432       buffer = new char[buffersize];
433       bufline = new int[buffersize];
434       bufcolumn = new int[buffersize];
435       nextCharBuf = new char[4096];
436     }
437     prevCharIsLF = prevCharIsCR = false;
438     tokenBegin = inBuf = maxNextCharInd = 0;
439     nextCharInd = bufpos = -1;
440   }
441
442   public void ReInit(java.io.Reader JavaDoc dstream,
443                                         int startline, int startcolumn)
444   {
445      ReInit(dstream, startline, startcolumn, 4096);
446   }
447
448   public void ReInit(java.io.Reader JavaDoc dstream)
449   {
450      ReInit(dstream, 1, 1, 4096);
451   }
452   public JavaCharStream(java.io.InputStream JavaDoc dstream, int startline,
453   int startcolumn, int buffersize)
454   {
455      this(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
456   }
457
458   public JavaCharStream(java.io.InputStream JavaDoc dstream, int startline,
459                                                            int startcolumn)
460   {
461      this(dstream, startline, startcolumn, 4096);
462   }
463
464   public JavaCharStream(java.io.InputStream JavaDoc dstream)
465   {
466      this(dstream, 1, 1, 4096);
467   }
468
469   public void ReInit(java.io.InputStream JavaDoc dstream, int startline,
470   int startcolumn, int buffersize)
471   {
472      ReInit(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
473   }
474   public void ReInit(java.io.InputStream JavaDoc dstream, int startline,
475                                                            int startcolumn)
476   {
477      ReInit(dstream, startline, startcolumn, 4096);
478   }
479   public void ReInit(java.io.InputStream JavaDoc dstream)
480   {
481      ReInit(dstream, 1, 1, 4096);
482   }
483
484   public String JavaDoc GetImage()
485   {
486      if (bufpos >= tokenBegin)
487         return new String JavaDoc(buffer, tokenBegin, bufpos - tokenBegin + 1);
488      else
489         return new String JavaDoc(buffer, tokenBegin, bufsize - tokenBegin) +
490                               new String JavaDoc(buffer, 0, bufpos + 1);
491   }
492
493   public char[] GetSuffix(int len)
494   {
495      char[] ret = new char[len];
496
497      if ((bufpos + 1) >= len)
498         System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
499      else
500      {
501         System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
502                                                           len - bufpos - 1);
503         System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
504      }
505
506      return ret;
507   }
508
509   public void Done()
510   {
511      nextCharBuf = null;
512      buffer = null;
513      bufline = null;
514      bufcolumn = null;
515   }
516
517   /**
518    * Method to adjust line and column numbers for the start of a token.
519    */

520   public void adjustBeginLineColumn(int newLine, int newCol)
521   {
522      int start = tokenBegin;
523      int len;
524
525      if (bufpos >= tokenBegin)
526      {
527         len = bufpos - tokenBegin + inBuf + 1;
528      }
529      else
530      {
531         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
532      }
533
534      int i = 0, j = 0, k = 0;
535      int nextColDiff = 0, columnDiff = 0;
536
537      while (i < len &&
538             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
539      {
540         bufline[j] = newLine;
541         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
542         bufcolumn[j] = newCol + columnDiff;
543         columnDiff = nextColDiff;
544         i++;
545      }
546
547      if (i < len)
548      {
549         bufline[j] = newLine++;
550         bufcolumn[j] = newCol + columnDiff;
551
552         while (i++ < len)
553         {
554            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
555               bufline[j] = newLine++;
556            else
557               bufline[j] = newLine;
558         }
559      }
560
561      line = bufline[j];
562      column = bufcolumn[j];
563   }
564
565 }
566
Popular Tags