KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > expression > parser > JavaCharStream


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

343
344   static public int getColumn() {
345      return bufcolumn[bufpos];
346   }
347
348   /**
349    * @deprecated
350    * @see #getEndLine
351    */

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

506   static public void adjustBeginLineColumn(int newLine, int newCol)
507   {
508      int start = tokenBegin;
509      int len;
510
511      if (bufpos >= tokenBegin)
512      {
513         len = bufpos - tokenBegin + inBuf + 1;
514      }
515      else
516      {
517         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
518      }
519
520      int i = 0, j = 0, k = 0;
521      int nextColDiff = 0, columnDiff = 0;
522
523      while (i < len &&
524             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
525      {
526         bufline[j] = newLine;
527         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
528         bufcolumn[j] = newCol + columnDiff;
529         columnDiff = nextColDiff;
530         i++;
531      }
532
533      if (i < len)
534      {
535         bufline[j] = newLine++;
536         bufcolumn[j] = newCol + columnDiff;
537
538         while (i++ < len)
539         {
540            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
541               bufline[j] = newLine++;
542            else
543               bufline[j] = newLine;
544         }
545      }
546
547      line = bufline[j];
548      column = bufcolumn[j];
549   }
550
551 }
552
Popular Tags