View Javadoc

    1   /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.1 */
    2   /* JavaCCOptions:STATIC=false */
    3   package net.sourceforge.pmd.lang.ast;
    4
    5   /**
    6    * An implementation of interface CharStream, where the stream is assumed to
    7    * contain only ASCII characters (without unicode processing).
    8    */
    9
    10  public class SimpleCharStream implements CharStream
    11  {
    12  /** Whether parser is static. */
    13    public static final boolean staticFlag = false;
    14    int bufsize;
    15    int available;
    16    int tokenBegin;
    17  /** Position in buffer. */
    18    public int bufpos = -1;
    19    protected int bufline[];
    20    protected int bufcolumn[];
    21
    22    protected int column = 0;
    23    protected int line = 1;
    24
    25    protected boolean prevCharIsCR = false;
    26    protected boolean prevCharIsLF = false;
    27
    28    protected java.io.Reader inputStream;
    29
    30    protected char[] buffer;
    31    protected int maxNextCharInd = 0;
    32    protected int inBuf = 0;
    33    protected int tabSize = 8;
    34
    35    protected void setTabSize(int i) { tabSize = i; }
    36    protected int getTabSize(int i) { return tabSize; }
    37
    38
    39    protected void ExpandBuff(boolean wrapAround)
    40    {
    41       char[] newbuffer = new char[bufsize + 2048];
    42       int newbufline[] = new int[bufsize + 2048];
    43       int newbufcolumn[] = new int[bufsize + 2048];
    44
    45       try
    46       {
    47          if (wrapAround)
    48          {
    49             System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
    50             System.arraycopy(buffer, 0, newbuffer,
    51                                               bufsize - tokenBegin, bufpos);
    52             buffer = newbuffer;
    53
    54             System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
    55             System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
    56             bufline = newbufline;
    57
    58             System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
    59             System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
    60             bufcolumn = newbufcolumn;
    61
    62             maxNextCharInd = (bufpos += (bufsize - tokenBegin));
    63          }
    64          else
    65          {
    66             System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
    67             buffer = newbuffer;
    68
    69             System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
    70             bufline = newbufline;
    71
    72             System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
    73             bufcolumn = newbufcolumn;
    74
    75             maxNextCharInd = (bufpos -= tokenBegin);
    76          }
    77       }
    78       catch (Throwable t)
    79       {
    80          throw new Error(t.getMessage());
    81       }
    82
    83
    84       bufsize += 2048;
    85       available = bufsize;
    86       tokenBegin = 0;
    87    }
    88
    89    protected void FillBuff() throws java.io.IOException
    90    {
    91       if (maxNextCharInd == available)
    92       {
    93          if (available == bufsize)
    94          {
    95             if (tokenBegin > 2048)
    96             {
    97                bufpos = maxNextCharInd = 0;
    98                available = tokenBegin;
    99             }
    100            else if (tokenBegin < 0)
    101               bufpos = maxNextCharInd = 0;
    102            else
    103               ExpandBuff(false);
    104         }
    105         else if (available > tokenBegin)
    106            available = bufsize;
    107         else if ((tokenBegin - available) < 2048)
    108            ExpandBuff(true);
    109         else
    110            available = tokenBegin;
    111      }
    112
    113      int i;
    114      try {
    115         if ((i = inputStream.read(buffer, maxNextCharInd,
    116                                     available - maxNextCharInd)) == -1)
    117         {
    118            inputStream.close();
    119            throw new java.io.IOException();
    120         }
    121         else
    122            maxNextCharInd += i;
    123         return;
    124      }
    125      catch(java.io.IOException e) {
    126         --bufpos;
    127         backup(0);
    128         if (tokenBegin == -1)
    129            tokenBegin = bufpos;
    130         throw e;
    131      }
    132   }
    133
    134 /** Start. */
    135   public char BeginToken() throws java.io.IOException
    136   {
    137      tokenBegin = -1;
    138      char c = readChar();
    139      tokenBegin = bufpos;
    140
    141      return c;
    142   }
    143
    144   protected void UpdateLineColumn(char c)
    145   {
    146      column++;
    147
    148      if (prevCharIsLF)
    149      {
    150         prevCharIsLF = false;
    151         line += (column = 1);
    152      }
    153      else if (prevCharIsCR)
    154      {
    155         prevCharIsCR = false;
    156         if (c == '\n')
    157         {
    158            prevCharIsLF = true;
    159         }
    160         else
    161            line += (column = 1);
    162      }
    163
    164      switch (c)
    165      {
    166         case '\r' :
    167            prevCharIsCR = true;
    168            break;
    169         case '\n' :
    170            prevCharIsLF = true;
    171            break;
    172         case '\t' :
    173            column--;
    174            column += (tabSize - (column % tabSize));
    175            break;
    176         default :
    177            break;
    178      }
    179
    180      bufline[bufpos] = line;
    181      bufcolumn[bufpos] = column;
    182   }
    183
    184 /** Read a character. */
    185   public char readChar() throws java.io.IOException
    186   {
    187      if (inBuf > 0)
    188      {
    189         --inBuf;
    190
    191         if (++bufpos == bufsize)
    192            bufpos = 0;
    193
    194         return buffer[bufpos];
    195      }
    196
    197      if (++bufpos >= maxNextCharInd)
    198         FillBuff();
    199
    200      char c = buffer[bufpos];
    201
    202      UpdateLineColumn(c);
    203      return c;
    204   }
    205
    206   /**
    207    * @deprecated
    208    * @see #getEndColumn
    209    */
    210
    211   public int getColumn() {
    212      return bufcolumn[bufpos];
    213   }
    214
    215   /**
    216    * @deprecated
    217    * @see #getEndLine
    218    */
    219
    220   public int getLine() {
    221      return bufline[bufpos];
    222   }
    223
    224   /** Get token end column number. */
    225   public int getEndColumn() {
    226      return bufcolumn[bufpos];
    227   }
    228
    229   /** Get token end line number. */
    230   public int getEndLine() {
    231      return bufline[bufpos];
    232   }
    233
    234   /** Get token beginning column number. */
    235   public int getBeginColumn() {
    236      return bufcolumn[tokenBegin];
    237   }
    238
    239   /** Get token beginning line number. */
    240   public int getBeginLine() {
    241      return bufline[tokenBegin];
    242   }
    243
    244 /** Backup a number of characters. */
    245   public void backup(int amount) {
    246
    247     inBuf += amount;
    248     if ((bufpos -= amount) < 0)
    249        bufpos += bufsize;
    250   }
    251
    252   /** Constructor. */
    253   public SimpleCharStream(java.io.Reader dstream, int startline,
    254   int startcolumn, int buffersize)
    255   {
    256     inputStream = dstream;
    257     line = startline;
    258     column = startcolumn - 1;
    259
    260     available = bufsize = buffersize;
    261     buffer = new char[buffersize];
    262     bufline = new int[buffersize];
    263     bufcolumn = new int[buffersize];
    264   }
    265
    266   /** Constructor. */
    267   public SimpleCharStream(java.io.Reader dstream, int startline,
    268                           int startcolumn)
    269   {
    270      this(dstream, startline, startcolumn, 4096);
    271   }
    272
    273   /** Constructor. */
    274   public SimpleCharStream(java.io.Reader dstream)
    275   {
    276      this(dstream, 1, 1, 4096);
    277   }
    278
    279   /** Reinitialise. */
    280   public void ReInit(java.io.Reader dstream, int startline,
    281   int startcolumn, int buffersize)
    282   {
    283     inputStream = dstream;
    284     line = startline;
    285     column = startcolumn - 1;
    286
    287     if (buffer == null || buffersize != buffer.length)
    288     {
    289       available = bufsize = buffersize;
    290       buffer = new char[buffersize];
    291       bufline = new int[buffersize];
    292       bufcolumn = new int[buffersize];
    293     }
    294     prevCharIsLF = prevCharIsCR = false;
    295     tokenBegin = inBuf = maxNextCharInd = 0;
    296     bufpos = -1;
    297   }
    298
    299   /** Reinitialise. */
    300   public void ReInit(java.io.Reader dstream, int startline,
    301                      int startcolumn)
    302   {
    303      ReInit(dstream, startline, startcolumn, 4096);
    304   }
    305
    306   /** Reinitialise. */
    307   public void ReInit(java.io.Reader dstream)
    308   {
    309      ReInit(dstream, 1, 1, 4096);
    310   }
    311   /** Constructor. */
    312   public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
    313   int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
    314   {
    315      this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
    316   }
    317
    318   /** Constructor. */
    319   public SimpleCharStream(java.io.InputStream dstream, int startline,
    320   int startcolumn, int buffersize)
    321   {
    322      this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
    323   }
    324
    325   /** Constructor. */
    326   public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
    327                           int startcolumn) throws java.io.UnsupportedEncodingException
    328   {
    329      this(dstream, encoding, startline, startcolumn, 4096);
    330   }
    331
    332   /** Constructor. */
    333   public SimpleCharStream(java.io.InputStream dstream, int startline,
    334                           int startcolumn)
    335   {
    336      this(dstream, startline, startcolumn, 4096);
    337   }
    338
    339   /** Constructor. */
    340   public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
    341   {
    342      this(dstream, encoding, 1, 1, 4096);
    343   }
    344
    345   /** Constructor. */
    346   public SimpleCharStream(java.io.InputStream dstream)
    347   {
    348      this(dstream, 1, 1, 4096);
    349   }
    350
    351   /** Reinitialise. */
    352   public void ReInit(java.io.InputStream dstream, String encoding, int startline,
    353                           int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
    354   {
    355      ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
    356   }
    357
    358   /** Reinitialise. */
    359   public void ReInit(java.io.InputStream dstream, int startline,
    360                           int startcolumn, int buffersize)
    361   {
    362      ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
    363   }
    364
    365   /** Reinitialise. */
    366   public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
    367   {
    368      ReInit(dstream, encoding, 1, 1, 4096);
    369   }
    370
    371   /** Reinitialise. */
    372   public void ReInit(java.io.InputStream dstream)
    373   {
    374      ReInit(dstream, 1, 1, 4096);
    375   }
    376   /** Reinitialise. */
    377   public void ReInit(java.io.InputStream dstream, String encoding, int startline,
    378                      int startcolumn) throws java.io.UnsupportedEncodingException
    379   {
    380      ReInit(dstream, encoding, startline, startcolumn, 4096);
    381   }
    382   /** Reinitialise. */
    383   public void ReInit(java.io.InputStream dstream, int startline,
    384                      int startcolumn)
    385   {
    386      ReInit(dstream, startline, startcolumn, 4096);
    387   }
    388   /** Get token literal value. */
    389   public String GetImage()
    390   {
    391      if (bufpos >= tokenBegin)
    392         return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
    393      else
    394         return new String(buffer, tokenBegin, bufsize - tokenBegin) +
    395                               new String(buffer, 0, bufpos + 1);
    396   }
    397
    398   /** Get the suffix. */
    399   public char[] GetSuffix(int len)
    400   {
    401      char[] ret = new char[len];
    402
    403      if ((bufpos + 1) >= len)
    404         System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
    405      else
    406      {
    407         System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
    408                                                           len - bufpos - 1);
    409         System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
    410      }
    411
    412      return ret;
    413   }
    414
    415   /** Reset buffer when finished. */
    416   public void Done()
    417   {
    418      buffer = null;
    419      bufline = null;
    420      bufcolumn = null;
    421   }
    422
    423   /**
    424    * Method to adjust line and column numbers for the start of a token.
    425    */
    426   public void adjustBeginLineColumn(int newLine, int newCol)
    427   {
    428      int start = tokenBegin;
    429      int len;
    430
    431      if (bufpos >= tokenBegin)
    432      {
    433         len = bufpos - tokenBegin + inBuf + 1;
    434      }
    435      else
    436      {
    437         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
    438      }
    439
    440      int i = 0, j = 0, k = 0;
    441      int nextColDiff = 0, columnDiff = 0;
    442
    443      while (i < len &&
    444             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
    445      {
    446         bufline[j] = newLine;
    447         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
    448         bufcolumn[j] = newCol + columnDiff;
    449         columnDiff = nextColDiff;
    450         i++;
    451      }
    452
    453      if (i < len)
    454      {
    455         bufline[j] = newLine++;
    456         bufcolumn[j] = newCol + columnDiff;
    457
    458         while (i++ < len)
    459         {
    460            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
    461               bufline[j] = newLine++;
    462            else
    463               bufline[j] = newLine;
    464         }
    465      }
    466
    467      line = bufline[j];
    468      column = bufcolumn[j];
    469   }
    470
    471 }
    472 /* JavaCC - OriginalChecksum=81d99296909abd2c9b4b1e2d27786d9e (do not edit this line) */