KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > exp > parser > JavaCharStream


1 /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 4.0 */
2 /*****************************************************************
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements. See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership. The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied. See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  ****************************************************************/

20
21
22 package org.apache.cayenne.exp.parser;
23
24 /**
25  * An implementation of interface CharStream, where the stream is assumed to
26  * contain only ASCII characters (with java-like unicode escape processing).
27  */

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

367
368   public int getColumn() {
369      return bufcolumn[bufpos];
370   }
371
372   /**
373    * @deprecated
374    * @see #getEndLine
375    */

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

559   public void adjustBeginLineColumn(int newLine, int newCol)
560   {
561      int start = tokenBegin;
562      int len;
563
564      if (bufpos >= tokenBegin)
565      {
566         len = bufpos - tokenBegin + inBuf + 1;
567      }
568      else
569      {
570         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
571      }
572
573      int i = 0, j = 0, k = 0;
574      int nextColDiff = 0, columnDiff = 0;
575
576      while (i < len &&
577             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
578      {
579         bufline[j] = newLine;
580         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
581         bufcolumn[j] = newCol + columnDiff;
582         columnDiff = nextColDiff;
583         i++;
584      }
585
586      if (i < len)
587      {
588         bufline[j] = newLine++;
589         bufcolumn[j] = newCol + columnDiff;
590
591         while (i++ < len)
592         {
593            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
594               bufline[j] = newLine++;
595            else
596               bufline[j] = newLine;
597         }
598      }
599
600      line = bufline[j];
601      column = bufcolumn[j];
602   }
603
604 }
605
Popular Tags