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 javancss.parser.java15;
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      /**
351       * @deprecated
352       * @see #getEndColumn
353       */
354      public int getColumn() {
355        return bufcolumn[bufpos];
356      }
357    
358      /**
359       * @deprecated
360       * @see #getEndLine
361       */
362      public int getLine() {
363        return bufline[bufpos];
364      }
365    
366    /** Get end column. */
367      public int getEndColumn() {
368        return bufcolumn[bufpos];
369      }
370    
371    /** Get end line. */
372      public int getEndLine() {
373        return bufline[bufpos];
374      }
375    
376    /** @return column of token start */
377      public int getBeginColumn() {
378        return bufcolumn[tokenBegin];
379      }
380    
381    /** @return line number of token start */
382      public int getBeginLine() {
383        return bufline[tokenBegin];
384      }
385    
386    /** Retreat. */
387      public void backup(int amount) {
388    
389        inBuf += amount;
390        if ((bufpos -= amount) < 0)
391          bufpos += bufsize;
392      }
393    
394    /** Constructor. */
395      public JavaCharStream(java.io.Reader dstream,
396                     int startline, int startcolumn, int buffersize)
397      {
398        inputStream = dstream;
399        line = startline;
400        column = startcolumn - 1;
401    
402        available = bufsize = buffersize;
403        buffer = new char[buffersize];
404        bufline = new int[buffersize];
405        bufcolumn = new int[buffersize];
406        nextCharBuf = new char[4096];
407      }
408    
409    /** Constructor. */
410      public JavaCharStream(java.io.Reader dstream,
411                                            int startline, int startcolumn)
412      {
413        this(dstream, startline, startcolumn, 4096);
414      }
415    
416    /** Constructor. */
417      public JavaCharStream(java.io.Reader dstream)
418      {
419        this(dstream, 1, 1, 4096);
420      }
421    /** Reinitialise. */
422      public void ReInit(java.io.Reader dstream,
423                     int startline, int startcolumn, int buffersize)
424      {
425        inputStream = dstream;
426        line = startline;
427        column = startcolumn - 1;
428    
429        if (buffer == null || buffersize != buffer.length)
430        {
431          available = bufsize = buffersize;
432          buffer = new char[buffersize];
433          bufline = new int[buffersize];
434          bufcolumn = new int[buffersize];
435          nextCharBuf = new char[4096];
436        }
437        prevCharIsLF = prevCharIsCR = false;
438        tokenBegin = inBuf = maxNextCharInd = 0;
439        nextCharInd = bufpos = -1;
440      }
441    
442    /** Reinitialise. */
443      public void ReInit(java.io.Reader dstream,
444                                            int startline, int startcolumn)
445      {
446        ReInit(dstream, startline, startcolumn, 4096);
447      }
448    
449    /** Reinitialise. */
450      public void ReInit(java.io.Reader dstream)
451      {
452        ReInit(dstream, 1, 1, 4096);
453      }
454    /** Constructor. */
455      public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
456      int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
457      {
458        this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
459      }
460    
461    /** Constructor. */
462      public JavaCharStream(java.io.InputStream dstream, int startline,
463      int startcolumn, int buffersize)
464      {
465        this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
466      }
467    
468    /** Constructor. */
469      public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
470                            int startcolumn) throws java.io.UnsupportedEncodingException
471      {
472        this(dstream, encoding, startline, startcolumn, 4096);
473      }
474    
475    /** Constructor. */
476      public JavaCharStream(java.io.InputStream dstream, int startline,
477                            int startcolumn)
478      {
479        this(dstream, startline, startcolumn, 4096);
480      }
481    
482    /** Constructor. */
483      public JavaCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
484      {
485        this(dstream, encoding, 1, 1, 4096);
486      }
487    
488    /** Constructor. */
489      public JavaCharStream(java.io.InputStream dstream)
490      {
491        this(dstream, 1, 1, 4096);
492      }
493    
494    /** Reinitialise. */
495      public void ReInit(java.io.InputStream dstream, String encoding, int startline,
496      int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
497      {
498        ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
499      }
500    
501    /** Reinitialise. */
502      public void ReInit(java.io.InputStream dstream, int startline,
503      int startcolumn, int buffersize)
504      {
505        ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
506      }
507    /** Reinitialise. */
508      public void ReInit(java.io.InputStream dstream, String encoding, int startline,
509                         int startcolumn) throws java.io.UnsupportedEncodingException
510      {
511        ReInit(dstream, encoding, startline, startcolumn, 4096);
512      }
513    /** Reinitialise. */
514      public void ReInit(java.io.InputStream dstream, int startline,
515                         int startcolumn)
516      {
517        ReInit(dstream, startline, startcolumn, 4096);
518      }
519    /** Reinitialise. */
520      public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
521      {
522        ReInit(dstream, encoding, 1, 1, 4096);
523      }
524    
525    /** Reinitialise. */
526      public void ReInit(java.io.InputStream dstream)
527      {
528        ReInit(dstream, 1, 1, 4096);
529      }
530    
531      /** @return token image as String */
532      public String GetImage()
533      {
534        if (bufpos >= tokenBegin)
535          return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
536        else
537          return new String(buffer, tokenBegin, bufsize - tokenBegin) +
538                                  new String(buffer, 0, bufpos + 1);
539      }
540    
541      /** @return suffix */
542      public char[] GetSuffix(int len)
543      {
544        char[] ret = new char[len];
545    
546        if ((bufpos + 1) >= len)
547          System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
548        else
549        {
550          System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
551                                                            len - bufpos - 1);
552          System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
553        }
554    
555        return ret;
556      }
557    
558      /** Set buffers back to null when finished. */
559      public void Done()
560      {
561        nextCharBuf = null;
562        buffer = null;
563        bufline = null;
564        bufcolumn = null;
565      }
566    
567      /**
568       * Method to adjust line and column numbers for the start of a token.
569       */
570      public void adjustBeginLineColumn(int newLine, int newCol)
571      {
572        int start = tokenBegin;
573        int len;
574    
575        if (bufpos >= tokenBegin)
576        {
577          len = bufpos - tokenBegin + inBuf + 1;
578        }
579        else
580        {
581          len = bufsize - tokenBegin + bufpos + 1 + inBuf;
582        }
583    
584        int i = 0, j = 0, k = 0;
585        int nextColDiff = 0, columnDiff = 0;
586    
587        while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
588        {
589          bufline[j] = newLine;
590          nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
591          bufcolumn[j] = newCol + columnDiff;
592          columnDiff = nextColDiff;
593          i++;
594        }
595    
596        if (i < len)
597        {
598          bufline[j] = newLine++;
599          bufcolumn[j] = newCol + columnDiff;
600    
601          while (i++ < len)
602          {
603            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
604              bufline[j] = newLine++;
605            else
606              bufline[j] = newLine;
607          }
608        }
609    
610        line = bufline[j];
611        column = bufcolumn[j];
612      }
613    
614    }
615    /* JavaCC - OriginalChecksum=8d07d00cccbdfa9c282f1ed3e0109a62 (do not edit this line) */