1 /* Generated By:JavaCC: Do not edit this line. ASCII_UCodeESC_CharStream.java Version 0.7pre6 */
2 /* ====================================================================
3  *
4  * Skin Look And Feel 6.7 License.
5  *
6  * Copyright (c) 2000-2006 L2FProd.com.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. The end-user documentation included with the redistribution, if
21  *    any, must include the following acknowlegement:
22  *       "This product includes software developed by L2FProd.com
23  *        (http://www.L2FProd.com/)."
24  *    Alternately, this acknowlegement may appear in the software itself,
25  *    if and wherever such third-party acknowlegements normally appear.
26  *
27  * 4. The names "Skin Look And Feel", "SkinLF" and "L2FProd.com" must not
28  *    be used to endorse or promote products derived from this software
29  *    without prior written permission. For written permission, please
30  *    contact info@L2FProd.com.
31  *
32  * 5. Products derived from this software may not be called "SkinLF"
33  *    nor may "SkinLF" appear in their names without prior written
34  *    permission of L2FProd.com.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED.  IN NO EVENT SHALL L2FPROD.COM OR ITS CONTRIBUTORS BE
40  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  */
49 package com.l2fprod.gui.plaf.skin.impl.gtk.parser;
50 
51 /**
52  * An implementation of interface CharStream, where the stream is assumed to
53  * contain only ASCII characters (with java-like unicode escape processing).
54  */
55 
56 public final class ASCII_UCodeESC_CharStream
57 {
58   public static final boolean staticFlag = false;
hexval(char c)59   static final int hexval(char c) throws java.io.IOException {
60     switch(c)
61     {
62        case '0' :
63           return 0;
64        case '1' :
65           return 1;
66        case '2' :
67           return 2;
68        case '3' :
69           return 3;
70        case '4' :
71           return 4;
72        case '5' :
73           return 5;
74        case '6' :
75           return 6;
76        case '7' :
77           return 7;
78        case '8' :
79           return 8;
80        case '9' :
81           return 9;
82 
83        case 'a' :
84        case 'A' :
85           return 10;
86        case 'b' :
87        case 'B' :
88           return 11;
89        case 'c' :
90        case 'C' :
91           return 12;
92        case 'd' :
93        case 'D' :
94           return 13;
95        case 'e' :
96        case 'E' :
97           return 14;
98        case 'f' :
99        case 'F' :
100           return 15;
101     }
102 
103     throw new java.io.IOException(); // Should never come here
104   }
105 
106   public int bufpos = -1;
107   int bufsize;
108   int available;
109   int tokenBegin;
110   private int bufline[];
111   private int bufcolumn[];
112 
113   private int column = 0;
114   private int line = 1;
115 
116   private java.io.Reader inputStream;
117 
118   private boolean prevCharIsCR = false;
119   private boolean prevCharIsLF = false;
120 
121   private char[] nextCharBuf;
122   private char[] buffer;
123   private int maxNextCharInd = 0;
124   private int nextCharInd = -1;
125   private int inBuf = 0;
126 
ExpandBuff(boolean wrapAround)127   private final void ExpandBuff(boolean wrapAround)
128   {
129      char[] newbuffer = new char[bufsize + 2048];
130      int newbufline[] = new int[bufsize + 2048];
131      int newbufcolumn[] = new int[bufsize + 2048];
132 
133      try
134      {
135         if (wrapAround)
136         {
137            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
138            System.arraycopy(buffer, 0, newbuffer,
139                                              bufsize - tokenBegin, bufpos);
140            buffer = newbuffer;
141 
142            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
143            System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
144            bufline = newbufline;
145 
146            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
147            System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
148            bufcolumn = newbufcolumn;
149 
150            bufpos += (bufsize - tokenBegin);
151         }
152         else
153         {
154            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
155            buffer = newbuffer;
156 
157            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
158            bufline = newbufline;
159 
160            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
161            bufcolumn = newbufcolumn;
162 
163            bufpos -= tokenBegin;
164         }
165      }
166      catch (Throwable t)
167      {
168         throw new Error(t.getMessage());
169      }
170 
171      available = (bufsize += 2048);
172      tokenBegin = 0;
173   }
174 
FillBuff()175   private final void FillBuff() throws java.io.IOException
176   {
177      int i;
178      if (maxNextCharInd == 4096)
179         maxNextCharInd = nextCharInd = 0;
180 
181      try {
182         if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
183                                             4096 - maxNextCharInd)) == -1)
184         {
185            inputStream.close();
186            throw new java.io.IOException();
187         }
188         else
189            maxNextCharInd += i;
190         return;
191      }
192      catch(java.io.IOException e) {
193         if (bufpos != 0)
194         {
195            --bufpos;
196            backup(0);
197         }
198         else
199         {
200            bufline[bufpos] = line;
201            bufcolumn[bufpos] = column;
202         }
203         throw e;
204      }
205   }
206 
ReadByte()207   private final char ReadByte() throws java.io.IOException
208   {
209      if (++nextCharInd >= maxNextCharInd)
210         FillBuff();
211 
212      return nextCharBuf[nextCharInd];
213   }
214 
BeginToken()215   public final char BeginToken() throws java.io.IOException
216   {
217      if (inBuf > 0)
218      {
219         --inBuf;
220         return buffer[tokenBegin = (bufpos == bufsize - 1) ? (bufpos = 0)
221                                                            : ++bufpos];
222      }
223 
224      tokenBegin = 0;
225      bufpos = -1;
226 
227      return readChar();
228   }
229 
AdjustBuffSize()230   private final void AdjustBuffSize()
231   {
232      if (available == bufsize)
233      {
234         if (tokenBegin > 2048)
235         {
236            bufpos = 0;
237            available = tokenBegin;
238         }
239         else
240            ExpandBuff(false);
241      }
242      else if (available > tokenBegin)
243         available = bufsize;
244      else if ((tokenBegin - available) < 2048)
245         ExpandBuff(true);
246      else
247         available = tokenBegin;
248   }
249 
UpdateLineColumn(char c)250   private final void UpdateLineColumn(char c)
251   {
252      column++;
253 
254      if (prevCharIsLF)
255      {
256         prevCharIsLF = false;
257         line += (column = 1);
258      }
259      else if (prevCharIsCR)
260      {
261         prevCharIsCR = false;
262         if (c == '\n')
263         {
264            prevCharIsLF = true;
265         }
266         else
267            line += (column = 1);
268      }
269 
270      switch (c)
271      {
272         case '\r' :
273            prevCharIsCR = true;
274            break;
275         case '\n' :
276            prevCharIsLF = true;
277            break;
278         case '\t' :
279            column--;
280            column += (8 - (column & 07));
281            break;
282         default :
283            break;
284      }
285 
286      bufline[bufpos] = line;
287      bufcolumn[bufpos] = column;
288   }
289 
readChar()290   public final char readChar() throws java.io.IOException
291   {
292      if (inBuf > 0)
293      {
294         --inBuf;
295         return buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos];
296      }
297 
298      char c;
299 
300      if (++bufpos == available)
301         AdjustBuffSize();
302 
303      if (((buffer[bufpos] = c = (char)((char)0xff & ReadByte())) == '\\'))
304      {
305         UpdateLineColumn(c);
306 
307         int backSlashCnt = 1;
308 
309         for (;;) // Read all the backslashes
310         {
311            if (++bufpos == available)
312               AdjustBuffSize();
313 
314            try
315            {
316               if ((buffer[bufpos] = c = (char)((char)0xff & ReadByte())) != '\\')
317               {
318                  UpdateLineColumn(c);
319                  // found a non-backslash char.
320                  if ((c == 'u') && ((backSlashCnt & 1) == 1))
321                  {
322                     if (--bufpos < 0)
323                        bufpos = bufsize - 1;
324 
325                     break;
326                  }
327 
328                  backup(backSlashCnt);
329                  return '\\';
330               }
331            }
332            catch(java.io.IOException e)
333            {
334               if (backSlashCnt > 1)
335                  backup(backSlashCnt);
336 
337               return '\\';
338            }
339 
340            UpdateLineColumn(c);
341            backSlashCnt++;
342         }
343 
344         // Here, we have seen an odd number of backslash's followed by a 'u'
345         try
346         {
347            while ((c = (char)((char)0xff & ReadByte())) == 'u')
348               ++column;
349 
350            buffer[bufpos] = c = (char)(hexval(c) << 12 |
351                                        hexval((char)((char)0xff & ReadByte())) << 8 |
352                                        hexval((char)((char)0xff & ReadByte())) << 4 |
353                                        hexval((char)((char)0xff & ReadByte())));
354 
355            column += 4;
356         }
357         catch(java.io.IOException e)
358         {
359            throw new Error("Invalid escape character at line " + line +
360                                          " column " + column + ".");
361         }
362 
363         if (backSlashCnt == 1)
364            return c;
365         else
366         {
367            backup(backSlashCnt - 1);
368            return '\\';
369         }
370      }
371      else
372      {
373         UpdateLineColumn(c);
374         return (c);
375      }
376   }
377 
378   /**
379    * @deprecated
380    * @see #getEndColumn
381    */
382 
getColumn()383   public final int getColumn() {
384      return bufcolumn[bufpos];
385   }
386 
387   /**
388    * @deprecated
389    * @see #getEndLine
390    */
391 
getLine()392   public final int getLine() {
393      return bufline[bufpos];
394   }
395 
getEndColumn()396   public final int getEndColumn() {
397      return bufcolumn[bufpos];
398   }
399 
getEndLine()400   public final int getEndLine() {
401      return bufline[bufpos];
402   }
403 
getBeginColumn()404   public final int getBeginColumn() {
405      return bufcolumn[tokenBegin];
406   }
407 
getBeginLine()408   public final int getBeginLine() {
409      return bufline[tokenBegin];
410   }
411 
backup(int amount)412   public final void backup(int amount) {
413 
414     inBuf += amount;
415     if ((bufpos -= amount) < 0)
416        bufpos += bufsize;
417   }
418 
ASCII_UCodeESC_CharStream(java.io.Reader dstream, int startline, int startcolumn, int buffersize)419   public ASCII_UCodeESC_CharStream(java.io.Reader dstream,
420                  int startline, int startcolumn, int buffersize)
421   {
422     inputStream = dstream;
423     line = startline;
424     column = startcolumn - 1;
425 
426     available = bufsize = buffersize;
427     buffer = new char[buffersize];
428     bufline = new int[buffersize];
429     bufcolumn = new int[buffersize];
430     nextCharBuf = new char[4096];
431   }
432 
ASCII_UCodeESC_CharStream(java.io.Reader dstream, int startline, int startcolumn)433   public ASCII_UCodeESC_CharStream(java.io.Reader dstream,
434                                         int startline, int startcolumn)
435   {
436      this(dstream, startline, startcolumn, 4096);
437   }
ReInit(java.io.Reader dstream, int startline, int startcolumn, int buffersize)438   public void ReInit(java.io.Reader dstream,
439                  int startline, int startcolumn, int buffersize)
440   {
441     inputStream = dstream;
442     line = startline;
443     column = startcolumn - 1;
444 
445     if (buffer == null || buffersize != buffer.length)
446     {
447       available = bufsize = buffersize;
448       buffer = new char[buffersize];
449       bufline = new int[buffersize];
450       bufcolumn = new int[buffersize];
451       nextCharBuf = new char[4096];
452     }
453     prevCharIsLF = prevCharIsCR = false;
454     tokenBegin = inBuf = maxNextCharInd = 0;
455     nextCharInd = bufpos = -1;
456   }
457 
ReInit(java.io.Reader dstream, int startline, int startcolumn)458   public void ReInit(java.io.Reader dstream,
459                                         int startline, int startcolumn)
460   {
461      ReInit(dstream, startline, startcolumn, 4096);
462   }
ASCII_UCodeESC_CharStream(java.io.InputStream dstream, int startline, int startcolumn, int buffersize)463   public ASCII_UCodeESC_CharStream(java.io.InputStream dstream, int startline,
464   int startcolumn, int buffersize)
465   {
466      this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
467   }
468 
ASCII_UCodeESC_CharStream(java.io.InputStream dstream, int startline, int startcolumn)469   public ASCII_UCodeESC_CharStream(java.io.InputStream dstream, int startline,
470                                                            int startcolumn)
471   {
472      this(dstream, startline, startcolumn, 4096);
473   }
474 
ReInit(java.io.InputStream dstream, int startline, int startcolumn, int buffersize)475   public void ReInit(java.io.InputStream dstream, int startline,
476   int startcolumn, int buffersize)
477   {
478      ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
479   }
ReInit(java.io.InputStream dstream, int startline, int startcolumn)480   public void ReInit(java.io.InputStream dstream, int startline,
481                                                            int startcolumn)
482   {
483      ReInit(dstream, startline, startcolumn, 4096);
484   }
485 
GetImage()486   public final String GetImage()
487   {
488      if (bufpos >= tokenBegin)
489         return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
490      else
491         return new String(buffer, tokenBegin, bufsize - tokenBegin) +
492                               new String(buffer, 0, bufpos + 1);
493   }
494 
GetSuffix(int len)495   public final char[] GetSuffix(int len)
496   {
497      char[] ret = new char[len];
498 
499      if ((bufpos + 1) >= len)
500         System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
501      else
502      {
503         System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
504                                                           len - bufpos - 1);
505         System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
506      }
507 
508      return ret;
509   }
510 
Done()511   public void Done()
512   {
513      nextCharBuf = null;
514      buffer = null;
515      bufline = null;
516      bufcolumn = null;
517   }
518 
519   /**
520    * Method to adjust line and column numbers for the start of a token.<BR>
521    */
adjustBeginLineColumn(int newLine, int newCol)522   public void adjustBeginLineColumn(int newLine, int newCol)
523   {
524      int start = tokenBegin;
525      int len;
526 
527      if (bufpos >= tokenBegin)
528      {
529         len = bufpos - tokenBegin + inBuf + 1;
530      }
531      else
532      {
533         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
534      }
535 
536      int i = 0, j = 0, k = 0;
537      int nextColDiff = 0, columnDiff = 0;
538 
539      while (i < len &&
540             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
541      {
542         bufline[j] = newLine;
543         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
544         bufcolumn[j] = newCol + columnDiff;
545         columnDiff = nextColDiff;
546         i++;
547      }
548 
549      if (i < len)
550      {
551         bufline[j] = newLine++;
552         bufcolumn[j] = newCol + columnDiff;
553 
554         while (i++ < len)
555         {
556            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
557               bufline[j] = newLine++;
558            else
559               bufline[j] = newLine;
560         }
561      }
562 
563      line = bufline[j];
564      column = bufcolumn[j];
565   }
566 
567 }
568