001    /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 5.0 */
002    /* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
003    package org.apache.commons.ognl;
004    
005    /**
006     * An implementation of interface CharStream, where the stream is assumed to
007     * contain only ASCII characters (with java-like unicode escape processing).
008     */
009    
010    public
011    class JavaCharStream
012    {
013      /** Whether parser is static. */
014      public static final boolean staticFlag = false;
015    
016      static final int hexval(char c) throws java.io.IOException {
017        switch(c)
018        {
019           case '0' :
020              return 0;
021           case '1' :
022              return 1;
023           case '2' :
024              return 2;
025           case '3' :
026              return 3;
027           case '4' :
028              return 4;
029           case '5' :
030              return 5;
031           case '6' :
032              return 6;
033           case '7' :
034              return 7;
035           case '8' :
036              return 8;
037           case '9' :
038              return 9;
039    
040           case 'a' :
041           case 'A' :
042              return 10;
043           case 'b' :
044           case 'B' :
045              return 11;
046           case 'c' :
047           case 'C' :
048              return 12;
049           case 'd' :
050           case 'D' :
051              return 13;
052           case 'e' :
053           case 'E' :
054              return 14;
055           case 'f' :
056           case 'F' :
057              return 15;
058        }
059    
060        throw new java.io.IOException(); // Should never come here
061      }
062    
063    /** Position in buffer. */
064      public int bufpos = -1;
065      int bufsize;
066      int available;
067      int tokenBegin;
068      protected int bufline[];
069      protected int bufcolumn[];
070    
071      protected int column = 0;
072      protected int line = 1;
073    
074      protected boolean prevCharIsCR = false;
075      protected boolean prevCharIsLF = false;
076    
077      protected java.io.Reader inputStream;
078    
079      protected char[] nextCharBuf;
080      protected char[] buffer;
081      protected int maxNextCharInd = 0;
082      protected int nextCharInd = -1;
083      protected int inBuf = 0;
084      protected int tabSize = 8;
085    
086      protected void setTabSize(int i) { tabSize = i; }
087      protected int getTabSize(int i) { return tabSize; }
088    
089      protected void ExpandBuff(boolean wrapAround)
090      {
091        char[] newbuffer = new char[bufsize + 2048];
092        int newbufline[] = new int[bufsize + 2048];
093        int newbufcolumn[] = new int[bufsize + 2048];
094    
095        try
096        {
097          if (wrapAround)
098          {
099            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
100            System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
101            buffer = newbuffer;
102    
103            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
104            System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
105            bufline = newbufline;
106    
107            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
108            System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
109            bufcolumn = newbufcolumn;
110    
111            bufpos += (bufsize - tokenBegin);
112        }
113        else
114        {
115            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
116            buffer = newbuffer;
117    
118            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
119            bufline = newbufline;
120    
121            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
122            bufcolumn = newbufcolumn;
123    
124            bufpos -= tokenBegin;
125          }
126        }
127        catch (Throwable t)
128        {
129          throw new Error(t.getMessage());
130        }
131    
132        available = (bufsize += 2048);
133        tokenBegin = 0;
134      }
135    
136      protected void FillBuff() throws java.io.IOException
137      {
138        int i;
139        if (maxNextCharInd == 4096)
140          maxNextCharInd = nextCharInd = 0;
141    
142        try {
143          if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
144                                              4096 - maxNextCharInd)) == -1)
145          {
146            inputStream.close();
147            throw new java.io.IOException();
148          }
149          else
150             maxNextCharInd += i;
151          return;
152        }
153        catch(java.io.IOException e) {
154          if (bufpos != 0)
155          {
156            --bufpos;
157            backup(0);
158          }
159          else
160          {
161            bufline[bufpos] = line;
162            bufcolumn[bufpos] = column;
163          }
164          throw e;
165        }
166      }
167    
168      protected char ReadByte() throws java.io.IOException
169      {
170        if (++nextCharInd >= maxNextCharInd)
171          FillBuff();
172    
173        return nextCharBuf[nextCharInd];
174      }
175    
176    /** @return starting character for token. */
177      public char BeginToken() throws java.io.IOException
178      {
179        if (inBuf > 0)
180        {
181          --inBuf;
182    
183          if (++bufpos == bufsize)
184            bufpos = 0;
185    
186          tokenBegin = bufpos;
187          return buffer[bufpos];
188        }
189    
190        tokenBegin = 0;
191        bufpos = -1;
192    
193        return readChar();
194      }
195    
196      protected void AdjustBuffSize()
197      {
198        if (available == bufsize)
199        {
200          if (tokenBegin > 2048)
201          {
202            bufpos = 0;
203            available = tokenBegin;
204          }
205          else
206            ExpandBuff(false);
207        }
208        else if (available > tokenBegin)
209          available = bufsize;
210        else if ((tokenBegin - available) < 2048)
211          ExpandBuff(true);
212        else
213          available = tokenBegin;
214      }
215    
216      protected void UpdateLineColumn(char c)
217      {
218        column++;
219    
220        if (prevCharIsLF)
221        {
222          prevCharIsLF = false;
223          line += (column = 1);
224        }
225        else if (prevCharIsCR)
226        {
227          prevCharIsCR = false;
228          if (c == '\n')
229          {
230            prevCharIsLF = true;
231          }
232          else
233            line += (column = 1);
234        }
235    
236        switch (c)
237        {
238          case '\r' :
239            prevCharIsCR = true;
240            break;
241          case '\n' :
242            prevCharIsLF = true;
243            break;
244          case '\t' :
245            column--;
246            column += (tabSize - (column % tabSize));
247            break;
248          default :
249            break;
250        }
251    
252        bufline[bufpos] = line;
253        bufcolumn[bufpos] = column;
254      }
255    
256    /** Read a character. */
257      public char readChar() throws java.io.IOException
258      {
259        if (inBuf > 0)
260        {
261          --inBuf;
262    
263          if (++bufpos == bufsize)
264            bufpos = 0;
265    
266          return buffer[bufpos];
267        }
268    
269        char c;
270    
271        if (++bufpos == available)
272          AdjustBuffSize();
273    
274        if ((buffer[bufpos] = c = ReadByte()) == '\\')
275        {
276          UpdateLineColumn(c);
277    
278          int backSlashCnt = 1;
279    
280          for (;;) // Read all the backslashes
281          {
282            if (++bufpos == available)
283              AdjustBuffSize();
284    
285            try
286            {
287              if ((buffer[bufpos] = c = ReadByte()) != '\\')
288              {
289                UpdateLineColumn(c);
290                // found a non-backslash char.
291                if ((c == 'u') && ((backSlashCnt & 1) == 1))
292                {
293                  if (--bufpos < 0)
294                    bufpos = bufsize - 1;
295    
296                  break;
297                }
298    
299                backup(backSlashCnt);
300                return '\\';
301              }
302            }
303            catch(java.io.IOException e)
304            {
305              // We are returning one backslash so we should only backup (count-1)
306              if (backSlashCnt > 1)
307                backup(backSlashCnt-1);
308    
309              return '\\';
310            }
311    
312            UpdateLineColumn(c);
313            backSlashCnt++;
314          }
315    
316          // Here, we have seen an odd number of backslash's followed by a 'u'
317          try
318          {
319            while ((c = ReadByte()) == 'u')
320              ++column;
321    
322            buffer[bufpos] = c = (char)(hexval(c) << 12 |
323                                        hexval(ReadByte()) << 8 |
324                                        hexval(ReadByte()) << 4 |
325                                        hexval(ReadByte()));
326    
327            column += 4;
328          }
329          catch(java.io.IOException e)
330          {
331            throw new Error("Invalid escape character at line " + line +
332                                             " column " + column + ".");
333          }
334    
335          if (backSlashCnt == 1)
336            return c;
337          else
338          {
339            backup(backSlashCnt - 1);
340            return '\\';
341          }
342        }
343        else
344        {
345          UpdateLineColumn(c);
346          return c;
347        }
348      }
349    
350      @Deprecated
351      /**
352       * @deprecated
353       * @see #getEndColumn
354       */
355      public int getColumn() {
356        return bufcolumn[bufpos];
357      }
358    
359      @Deprecated
360      /**
361       * @deprecated
362       * @see #getEndLine
363       */
364      public int getLine() {
365        return bufline[bufpos];
366      }
367    
368    /** Get end column. */
369      public int getEndColumn() {
370        return bufcolumn[bufpos];
371      }
372    
373    /** Get end line. */
374      public int getEndLine() {
375        return bufline[bufpos];
376      }
377    
378    /** @return column of token start */
379      public int getBeginColumn() {
380        return bufcolumn[tokenBegin];
381      }
382    
383    /** @return line number of token start */
384      public int getBeginLine() {
385        return bufline[tokenBegin];
386      }
387    
388    /** Retreat. */
389      public void backup(int amount) {
390    
391        inBuf += amount;
392        if ((bufpos -= amount) < 0)
393          bufpos += bufsize;
394      }
395    
396    /** Constructor. */
397      public JavaCharStream(java.io.Reader dstream,
398                     int startline, int startcolumn, int buffersize)
399      {
400        inputStream = dstream;
401        line = startline;
402        column = startcolumn - 1;
403    
404        available = bufsize = buffersize;
405        buffer = new char[buffersize];
406        bufline = new int[buffersize];
407        bufcolumn = new int[buffersize];
408        nextCharBuf = new char[4096];
409      }
410    
411    /** Constructor. */
412      public JavaCharStream(java.io.Reader dstream,
413                                            int startline, int startcolumn)
414      {
415        this(dstream, startline, startcolumn, 4096);
416      }
417    
418    /** Constructor. */
419      public JavaCharStream(java.io.Reader dstream)
420      {
421        this(dstream, 1, 1, 4096);
422      }
423    /** Reinitialise. */
424      public void ReInit(java.io.Reader dstream,
425                     int startline, int startcolumn, int buffersize)
426      {
427        inputStream = dstream;
428        line = startline;
429        column = startcolumn - 1;
430    
431        if (buffer == null || buffersize != buffer.length)
432        {
433          available = bufsize = buffersize;
434          buffer = new char[buffersize];
435          bufline = new int[buffersize];
436          bufcolumn = new int[buffersize];
437          nextCharBuf = new char[4096];
438        }
439        prevCharIsLF = prevCharIsCR = false;
440        tokenBegin = inBuf = maxNextCharInd = 0;
441        nextCharInd = bufpos = -1;
442      }
443    
444    /** Reinitialise. */
445      public void ReInit(java.io.Reader dstream,
446                                            int startline, int startcolumn)
447      {
448        ReInit(dstream, startline, startcolumn, 4096);
449      }
450    
451    /** Reinitialise. */
452      public void ReInit(java.io.Reader dstream)
453      {
454        ReInit(dstream, 1, 1, 4096);
455      }
456    /** Constructor. */
457      public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
458      int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
459      {
460        this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
461      }
462    
463    /** Constructor. */
464      public JavaCharStream(java.io.InputStream dstream, int startline,
465      int startcolumn, int buffersize)
466      {
467        this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
468      }
469    
470    /** Constructor. */
471      public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
472                            int startcolumn) throws java.io.UnsupportedEncodingException
473      {
474        this(dstream, encoding, startline, startcolumn, 4096);
475      }
476    
477    /** Constructor. */
478      public JavaCharStream(java.io.InputStream dstream, int startline,
479                            int startcolumn)
480      {
481        this(dstream, startline, startcolumn, 4096);
482      }
483    
484    /** Constructor. */
485      public JavaCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
486      {
487        this(dstream, encoding, 1, 1, 4096);
488      }
489    
490    /** Constructor. */
491      public JavaCharStream(java.io.InputStream dstream)
492      {
493        this(dstream, 1, 1, 4096);
494      }
495    
496    /** Reinitialise. */
497      public void ReInit(java.io.InputStream dstream, String encoding, int startline,
498      int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
499      {
500        ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
501      }
502    
503    /** Reinitialise. */
504      public void ReInit(java.io.InputStream dstream, int startline,
505      int startcolumn, int buffersize)
506      {
507        ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
508      }
509    /** Reinitialise. */
510      public void ReInit(java.io.InputStream dstream, String encoding, int startline,
511                         int startcolumn) throws java.io.UnsupportedEncodingException
512      {
513        ReInit(dstream, encoding, startline, startcolumn, 4096);
514      }
515    /** Reinitialise. */
516      public void ReInit(java.io.InputStream dstream, int startline,
517                         int startcolumn)
518      {
519        ReInit(dstream, startline, startcolumn, 4096);
520      }
521    /** Reinitialise. */
522      public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
523      {
524        ReInit(dstream, encoding, 1, 1, 4096);
525      }
526    
527    /** Reinitialise. */
528      public void ReInit(java.io.InputStream dstream)
529      {
530        ReInit(dstream, 1, 1, 4096);
531      }
532    
533      /** @return token image as String */
534      public String GetImage()
535      {
536        if (bufpos >= tokenBegin)
537          return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
538        else
539          return new String(buffer, tokenBegin, bufsize - tokenBegin) +
540                                  new String(buffer, 0, bufpos + 1);
541      }
542    
543      /** @return suffix */
544      public char[] GetSuffix(int len)
545      {
546        char[] ret = new char[len];
547    
548        if ((bufpos + 1) >= len)
549          System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
550        else
551        {
552          System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
553                                                            len - bufpos - 1);
554          System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
555        }
556    
557        return ret;
558      }
559    
560      /** Set buffers back to null when finished. */
561      public void Done()
562      {
563        nextCharBuf = null;
564        buffer = null;
565        bufline = null;
566        bufcolumn = null;
567      }
568    
569      /**
570       * Method to adjust line and column numbers for the start of a token.
571       */
572      public void adjustBeginLineColumn(int newLine, int newCol)
573      {
574        int start = tokenBegin;
575        int len;
576    
577        if (bufpos >= tokenBegin)
578        {
579          len = bufpos - tokenBegin + inBuf + 1;
580        }
581        else
582        {
583          len = bufsize - tokenBegin + bufpos + 1 + inBuf;
584        }
585    
586        int i = 0, j = 0, k = 0;
587        int nextColDiff = 0, columnDiff = 0;
588    
589        while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
590        {
591          bufline[j] = newLine;
592          nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
593          bufcolumn[j] = newCol + columnDiff;
594          columnDiff = nextColDiff;
595          i++;
596        }
597    
598        if (i < len)
599        {
600          bufline[j] = newLine++;
601          bufcolumn[j] = newCol + columnDiff;
602    
603          while (i++ < len)
604          {
605            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
606              bufline[j] = newLine++;
607            else
608              bufline[j] = newLine;
609          }
610        }
611    
612        line = bufline[j];
613        column = bufcolumn[j];
614      }
615    
616    }
617    /* JavaCC - OriginalChecksum=7fed424457d5835b27d8aba99b626820 (do not edit this line) */