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