KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdql > parser > JavaCharStream


1 /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 2.1 */
2 /*
3  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
4  * All rights reserved.
5  */

6
7 package com.hp.hpl.jena.rdql.parser;
8
9 /**
10  * An implementation of interface CharStream, where the stream is assumed to
11  * contain only ASCII characters (with java-like unicode escape processing).
12  */

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

348
349   public final int getColumn() {
350      return bufcolumn[bufpos];
351   }
352
353   /**
354    * @deprecated
355    * @see #getEndLine
356    */

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

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