KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > sqlparser > core > ASCII_UCodeESC_CharStream


1 /* Generated By:JavaCC: Do not edit this line. ASCII_UCodeESC_CharStream.java Version 0.7pre6 */
2 package com.genimen.djeneric.repository.sqlparser.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 final class ASCII_UCodeESC_CharStream
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   private int bufline[];
66   private int bufcolumn[];
67
68   private int column = 0;
69   private int line = 1;
70
71   private java.io.Reader JavaDoc inputStream;
72
73   private boolean prevCharIsCR = false;
74   private boolean prevCharIsLF = false;
75
76   private char[] nextCharBuf;
77   private char[] buffer;
78   private int maxNextCharInd = 0;
79   private int nextCharInd = -1;
80   private int inBuf = 0;
81
82   private final 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   private final 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   private final char ReadByte() throws java.io.IOException JavaDoc
161   {
162     if (++nextCharInd >= maxNextCharInd) FillBuff();
163
164     return nextCharBuf[nextCharInd];
165   }
166
167   public final char BeginToken() throws java.io.IOException JavaDoc
168   {
169     if (inBuf > 0)
170     {
171       --inBuf;
172       return buffer[tokenBegin = (bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos];
173     }
174
175     tokenBegin = 0;
176     bufpos = -1;
177
178     return readChar();
179   }
180
181   private final void AdjustBuffSize()
182   {
183     if (available == bufsize)
184     {
185       if (tokenBegin > 2048)
186       {
187         bufpos = 0;
188         available = tokenBegin;
189       }
190       else ExpandBuff(false);
191     }
192     else if (available > tokenBegin) available = bufsize;
193     else if ((tokenBegin - available) < 2048) ExpandBuff(true);
194     else available = tokenBegin;
195   }
196
197   private final void UpdateLineColumn(char c)
198   {
199     column++;
200
201     if (prevCharIsLF)
202     {
203       prevCharIsLF = false;
204       line += (column = 1);
205     }
206     else if (prevCharIsCR)
207     {
208       prevCharIsCR = false;
209       if (c == '\n')
210       {
211         prevCharIsLF = true;
212       }
213       else line += (column = 1);
214     }
215
216     switch (c)
217     {
218       case '\r' :
219         prevCharIsCR = true;
220         break;
221       case '\n' :
222         prevCharIsLF = true;
223         break;
224       case '\t' :
225         column--;
226         column += (8 - (column & 07));
227         break;
228       default :
229         break;
230     }
231
232     bufline[bufpos] = line;
233     bufcolumn[bufpos] = column;
234   }
235
236   public final char readChar() throws java.io.IOException JavaDoc
237   {
238     if (inBuf > 0)
239     {
240       --inBuf;
241       return buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos];
242     }
243
244     char c;
245
246     if (++bufpos == available) AdjustBuffSize();
247
248     if (((buffer[bufpos] = c = (char) ((char) 0xff & ReadByte())) == '\\'))
249     {
250       UpdateLineColumn(c);
251
252       int backSlashCnt = 1;
253
254       for (;;) // Read all the backslashes
255
{
256         if (++bufpos == available) AdjustBuffSize();
257
258         try
259         {
260           if ((buffer[bufpos] = c = (char) ((char) 0xff & ReadByte())) != '\\')
261           {
262             UpdateLineColumn(c);
263             // found a non-backslash char.
264
if ((c == 'u') && ((backSlashCnt & 1) == 1))
265             {
266               if (--bufpos < 0) bufpos = bufsize - 1;
267
268               break;
269             }
270
271             backup(backSlashCnt);
272             return '\\';
273           }
274         }
275         catch (java.io.IOException JavaDoc e)
276         {
277           if (backSlashCnt > 1) backup(backSlashCnt);
278
279           return '\\';
280         }
281
282         UpdateLineColumn(c);
283         backSlashCnt++;
284       }
285
286       // Here, we have seen an odd number of backslash's followed by a 'u'
287
try
288       {
289         while ((c = (char) ((char) 0xff & ReadByte())) == 'u')
290           ++column;
291
292         buffer[bufpos] = c = (char) (hexval(c) << 12 | hexval((char) ((char) 0xff & ReadByte())) << 8
293                                      | hexval((char) ((char) 0xff & ReadByte())) << 4 | hexval((char) ((char) 0xff & ReadByte())));
294
295         column += 4;
296       }
297       catch (java.io.IOException JavaDoc e)
298       {
299         throw new Error JavaDoc("Invalid escape character at line " + line + " column " + column + ".");
300       }
301
302       if (backSlashCnt == 1) return c;
303       else
304       {
305         backup(backSlashCnt - 1);
306         return '\\';
307       }
308     }
309     else
310     {
311       UpdateLineColumn(c);
312       return (c);
313     }
314   }
315
316   /**
317    * @deprecated
318    * @see #getEndColumn
319    */

320
321   public final int getColumn()
322   {
323     return bufcolumn[bufpos];
324   }
325
326   /**
327    * @deprecated
328    * @see #getEndLine
329    */

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

456   public void adjustBeginLineColumn(int newLine, int newCol)
457   {
458     int start = tokenBegin;
459     int len;
460
461     if (bufpos >= tokenBegin)
462     {
463       len = bufpos - tokenBegin + inBuf + 1;
464     }
465     else
466     {
467       len = bufsize - tokenBegin + bufpos + 1 + inBuf;
468     }
469
470     int i = 0, j = 0, k = 0;
471     int nextColDiff = 0, columnDiff = 0;
472
473     while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
474     {
475       bufline[j] = newLine;
476       nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
477       bufcolumn[j] = newCol + columnDiff;
478       columnDiff = nextColDiff;
479       i++;
480     }
481
482     if (i < len)
483     {
484       bufline[j] = newLine++;
485       bufcolumn[j] = newCol + columnDiff;
486
487       while (i++ < len)
488       {
489         if (bufline[j = start % bufsize] != bufline[++start % bufsize]) bufline[j] = newLine++;
490         else bufline[j] = newLine;
491       }
492     }
493
494     line = bufline[j];
495     column = bufcolumn[j];
496   }
497
498 }
Popular Tags