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