Source for org.jfree.formula.parser.JavaCharStream

   1: /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 4.0 */
   2: package org.jfree.formula.parser;
   3: 
   4: /**
   5:  * An implementation of interface CharStream, where the stream is assumed to
   6:  * contain only ASCII characters (with java-like unicode escape processing).
   7:  */
   8: 
   9: public class JavaCharStream
  10: {
  11:   public static final boolean staticFlag = false;
  12:   static final int hexval(char c) throws java.io.IOException {
  13:     switch(c)
  14:     {
  15:        case '0' :
  16:           return 0;
  17:        case '1' :
  18:           return 1;
  19:        case '2' :
  20:           return 2;
  21:        case '3' :
  22:           return 3;
  23:        case '4' :
  24:           return 4;
  25:        case '5' :
  26:           return 5;
  27:        case '6' :
  28:           return 6;
  29:        case '7' :
  30:           return 7;
  31:        case '8' :
  32:           return 8;
  33:        case '9' :
  34:           return 9;
  35: 
  36:        case 'a' :
  37:        case 'A' :
  38:           return 10;
  39:        case 'b' :
  40:        case 'B' :
  41:           return 11;
  42:        case 'c' :
  43:        case 'C' :
  44:           return 12;
  45:        case 'd' :
  46:        case 'D' :
  47:           return 13;
  48:        case 'e' :
  49:        case 'E' :
  50:           return 14;
  51:        case 'f' :
  52:        case 'F' :
  53:           return 15;
  54:     }
  55: 
  56:     throw new java.io.IOException(); // Should never come here
  57:   }
  58: 
  59:   public int bufpos = -1;
  60:   int bufsize;
  61:   int available;
  62:   int tokenBegin;
  63:   protected int bufline[];
  64:   protected int bufcolumn[];
  65: 
  66:   protected int column = 0;
  67:   protected int line = 1;
  68: 
  69:   protected boolean prevCharIsCR = false;
  70:   protected boolean prevCharIsLF = false;
  71: 
  72:   protected java.io.Reader inputStream;
  73: 
  74:   protected char[] nextCharBuf;
  75:   protected char[] buffer;
  76:   protected int maxNextCharInd = 0;
  77:   protected int nextCharInd = -1;
  78:   protected int inBuf = 0;
  79:   protected int tabSize = 8;
  80: 
  81:   protected void setTabSize(int i) { tabSize = i; }
  82:   protected int getTabSize(int i) { return tabSize; }
  83: 
  84:   protected void ExpandBuff(boolean wrapAround)
  85:   {
  86:      char[] newbuffer = new char[bufsize + 2048];
  87:      int newbufline[] = new int[bufsize + 2048];
  88:      int newbufcolumn[] = new int[bufsize + 2048];
  89: 
  90:      try
  91:      {
  92:         if (wrapAround)
  93:         {
  94:            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  95:            System.arraycopy(buffer, 0, newbuffer,
  96:                                              bufsize - tokenBegin, bufpos);
  97:            buffer = newbuffer;
  98: 
  99:            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
 100:            System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
 101:            bufline = newbufline;
 102: 
 103:            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
 104:            System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
 105:            bufcolumn = newbufcolumn;
 106: 
 107:            bufpos += (bufsize - tokenBegin);
 108:         }
 109:         else
 110:         {
 111:            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
 112:            buffer = newbuffer;
 113: 
 114:            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
 115:            bufline = newbufline;
 116: 
 117:            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
 118:            bufcolumn = newbufcolumn;
 119: 
 120:            bufpos -= tokenBegin;
 121:         }
 122:      }
 123:      catch (Throwable t)
 124:      {
 125:         throw new Error(t.getMessage());
 126:      }
 127: 
 128:      available = (bufsize += 2048);
 129:      tokenBegin = 0;
 130:   }
 131: 
 132:   protected void FillBuff() throws java.io.IOException
 133:   {
 134:      int i;
 135:      if (maxNextCharInd == 4096)
 136:         maxNextCharInd = nextCharInd = 0;
 137: 
 138:      try {
 139:         if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
 140:                                             4096 - maxNextCharInd)) == -1)
 141:         {
 142:            inputStream.close();
 143:            throw new java.io.IOException();
 144:         }
 145:         else
 146:            maxNextCharInd += i;
 147:         return;
 148:      }
 149:      catch(java.io.IOException e) {
 150:         if (bufpos != 0)
 151:         {
 152:            --bufpos;
 153:            backup(0);
 154:         }
 155:         else
 156:         {
 157:            bufline[bufpos] = line;
 158:            bufcolumn[bufpos] = column;
 159:         }
 160:         throw e;
 161:      }
 162:   }
 163: 
 164:   protected char ReadByte() throws java.io.IOException
 165:   {
 166:      if (++nextCharInd >= maxNextCharInd)
 167:         FillBuff();
 168: 
 169:      return nextCharBuf[nextCharInd];
 170:   }
 171: 
 172:   public char BeginToken() throws java.io.IOException
 173:   {     
 174:      if (inBuf > 0)
 175:      {
 176:         --inBuf;
 177: 
 178:         if (++bufpos == bufsize)
 179:            bufpos = 0;
 180: 
 181:         tokenBegin = bufpos;
 182:         return buffer[bufpos];
 183:      }
 184: 
 185:      tokenBegin = 0;
 186:      bufpos = -1;
 187: 
 188:      return readChar();
 189:   }     
 190: 
 191:   protected void AdjustBuffSize()
 192:   {
 193:      if (available == bufsize)
 194:      {
 195:         if (tokenBegin > 2048)
 196:         {
 197:            bufpos = 0;
 198:            available = tokenBegin;
 199:         }
 200:         else
 201:            ExpandBuff(false);
 202:      }
 203:      else if (available > tokenBegin)
 204:         available = bufsize;
 205:      else if ((tokenBegin - available) < 2048)
 206:         ExpandBuff(true);
 207:      else
 208:         available = tokenBegin;
 209:   }
 210: 
 211:   protected void UpdateLineColumn(char c)
 212:   {
 213:      column++;
 214: 
 215:      if (prevCharIsLF)
 216:      {
 217:         prevCharIsLF = false;
 218:         line += (column = 1);
 219:      }
 220:      else if (prevCharIsCR)
 221:      {
 222:         prevCharIsCR = false;
 223:         if (c == '\n')
 224:         {
 225:            prevCharIsLF = true;
 226:         }
 227:         else
 228:            line += (column = 1);
 229:      }
 230: 
 231:      switch (c)
 232:      {
 233:         case '\r' :
 234:            prevCharIsCR = true;
 235:            break;
 236:         case '\n' :
 237:            prevCharIsLF = true;
 238:            break;
 239:         case '\t' :
 240:            column--;
 241:            column += (tabSize - (column % tabSize));
 242:            break;
 243:         default :
 244:            break;
 245:      }
 246: 
 247:      bufline[bufpos] = line;
 248:      bufcolumn[bufpos] = column;
 249:   }
 250: 
 251:   public char readChar() throws java.io.IOException
 252:   {
 253:      if (inBuf > 0)
 254:      {
 255:         --inBuf;
 256: 
 257:         if (++bufpos == bufsize)
 258:            bufpos = 0;
 259: 
 260:         return buffer[bufpos];
 261:      }
 262: 
 263:      char c;
 264: 
 265:      if (++bufpos == available)
 266:         AdjustBuffSize();
 267: 
 268:      if ((buffer[bufpos] = c = ReadByte()) == '\\')
 269:      {
 270:         UpdateLineColumn(c);
 271: 
 272:         int backSlashCnt = 1;
 273: 
 274:         for (;;) // Read all the backslashes
 275:         {
 276:            if (++bufpos == available)
 277:               AdjustBuffSize();
 278: 
 279:            try
 280:            {
 281:               if ((buffer[bufpos] = c = ReadByte()) != '\\')
 282:               {
 283:                  UpdateLineColumn(c);
 284:                  // found a non-backslash char.
 285:                  if ((c == 'u') && ((backSlashCnt & 1) == 1))
 286:                  {
 287:                     if (--bufpos < 0)
 288:                        bufpos = bufsize - 1;
 289: 
 290:                     break;
 291:                  }
 292: 
 293:                  backup(backSlashCnt);
 294:                  return '\\';
 295:               }
 296:            }
 297:            catch(java.io.IOException e)
 298:            {
 299:               if (backSlashCnt > 1)
 300:                  backup(backSlashCnt);
 301: 
 302:               return '\\';
 303:            }
 304: 
 305:            UpdateLineColumn(c);
 306:            backSlashCnt++;
 307:         }
 308: 
 309:         // Here, we have seen an odd number of backslash's followed by a 'u'
 310:         try
 311:         {
 312:            while ((c = ReadByte()) == 'u')
 313:               ++column;
 314: 
 315:            buffer[bufpos] = c = (char)(hexval(c) << 12 |
 316:                                        hexval(ReadByte()) << 8 |
 317:                                        hexval(ReadByte()) << 4 |
 318:                                        hexval(ReadByte()));
 319: 
 320:            column += 4;
 321:         }
 322:         catch(java.io.IOException e)
 323:         {
 324:            throw new Error("Invalid escape character at line " + line +
 325:                                          " column " + column + ".");
 326:         }
 327: 
 328:         if (backSlashCnt == 1)
 329:            return c;
 330:         else
 331:         {
 332:            backup(backSlashCnt - 1);
 333:            return '\\';
 334:         }
 335:      }
 336:      else
 337:      {
 338:         UpdateLineColumn(c);
 339:         return c;
 340:      }
 341:   }
 342: 
 343:   /**
 344:    * @deprecated 
 345:    * @see #getEndColumn
 346:    */
 347: 
 348:   public int getColumn() {
 349:      return bufcolumn[bufpos];
 350:   }
 351: 
 352:   /**
 353:    * @deprecated 
 354:    * @see #getEndLine
 355:    */
 356: 
 357:   public int getLine() {
 358:      return bufline[bufpos];
 359:   }
 360: 
 361:   public int getEndColumn() {
 362:      return bufcolumn[bufpos];
 363:   }
 364: 
 365:   public int getEndLine() {
 366:      return bufline[bufpos];
 367:   }
 368: 
 369:   public int getBeginColumn() {
 370:      return bufcolumn[tokenBegin];
 371:   }
 372: 
 373:   public int getBeginLine() {
 374:      return bufline[tokenBegin];
 375:   }
 376: 
 377:   public void backup(int amount) {
 378: 
 379:     inBuf += amount;
 380:     if ((bufpos -= amount) < 0)
 381:        bufpos += bufsize;
 382:   }
 383: 
 384:   public JavaCharStream(java.io.Reader dstream,
 385:                  int startline, int startcolumn, int buffersize)
 386:   {
 387:     inputStream = dstream;
 388:     line = startline;
 389:     column = startcolumn - 1;
 390: 
 391:     available = bufsize = buffersize;
 392:     buffer = new char[buffersize];
 393:     bufline = new int[buffersize];
 394:     bufcolumn = new int[buffersize];
 395:     nextCharBuf = new char[4096];
 396:   }
 397: 
 398:   public JavaCharStream(java.io.Reader dstream,
 399:                                         int startline, int startcolumn)
 400:   {
 401:      this(dstream, startline, startcolumn, 4096);
 402:   }
 403: 
 404:   public JavaCharStream(java.io.Reader dstream)
 405:   {
 406:      this(dstream, 1, 1, 4096);
 407:   }
 408:   public void ReInit(java.io.Reader dstream,
 409:                  int startline, int startcolumn, int buffersize)
 410:   {
 411:     inputStream = dstream;
 412:     line = startline;
 413:     column = startcolumn - 1;
 414: 
 415:     if (buffer == null || buffersize != buffer.length)
 416:     {
 417:       available = bufsize = buffersize;
 418:       buffer = new char[buffersize];
 419:       bufline = new int[buffersize];
 420:       bufcolumn = new int[buffersize];
 421:       nextCharBuf = new char[4096];
 422:     }
 423:     prevCharIsLF = prevCharIsCR = false;
 424:     tokenBegin = inBuf = maxNextCharInd = 0;
 425:     nextCharInd = bufpos = -1;
 426:   }
 427: 
 428:   public void ReInit(java.io.Reader dstream,
 429:                                         int startline, int startcolumn)
 430:   {
 431:      ReInit(dstream, startline, startcolumn, 4096);
 432:   }
 433: 
 434:   public void ReInit(java.io.Reader dstream)
 435:   {
 436:      ReInit(dstream, 1, 1, 4096);
 437:   }
 438:   public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
 439:   int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
 440:   {
 441:      this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
 442:   }
 443: 
 444:   public JavaCharStream(java.io.InputStream dstream, int startline,
 445:   int startcolumn, int buffersize)
 446:   {
 447:      this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
 448:   }
 449: 
 450:   public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
 451:                         int startcolumn) throws java.io.UnsupportedEncodingException
 452:   {
 453:      this(dstream, encoding, startline, startcolumn, 4096);
 454:   }
 455: 
 456:   public JavaCharStream(java.io.InputStream dstream, int startline,
 457:                         int startcolumn)
 458:   {
 459:      this(dstream, startline, startcolumn, 4096);
 460:   }
 461: 
 462:   public JavaCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
 463:   {
 464:      this(dstream, encoding, 1, 1, 4096);
 465:   }
 466: 
 467:   public JavaCharStream(java.io.InputStream dstream)
 468:   {
 469:      this(dstream, 1, 1, 4096);
 470:   }
 471: 
 472:   public void ReInit(java.io.InputStream dstream, String encoding, int startline,
 473:   int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
 474:   {
 475:      ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
 476:   }
 477: 
 478:   public void ReInit(java.io.InputStream dstream, int startline,
 479:   int startcolumn, int buffersize)
 480:   {
 481:      ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
 482:   }
 483:   public void ReInit(java.io.InputStream dstream, String encoding, int startline,
 484:                      int startcolumn) throws java.io.UnsupportedEncodingException
 485:   {
 486:      ReInit(dstream, encoding, startline, startcolumn, 4096);
 487:   }
 488:   public void ReInit(java.io.InputStream dstream, int startline,
 489:                      int startcolumn)
 490:   {
 491:      ReInit(dstream, startline, startcolumn, 4096);
 492:   }
 493:   public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
 494:   {
 495:      ReInit(dstream, encoding, 1, 1, 4096);
 496:   }
 497: 
 498:   public void ReInit(java.io.InputStream dstream)
 499:   {
 500:      ReInit(dstream, 1, 1, 4096);
 501:   }
 502: 
 503:   public String GetImage()
 504:   {
 505:      if (bufpos >= tokenBegin)
 506:         return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
 507:      else
 508:         return new String(buffer, tokenBegin, bufsize - tokenBegin) +
 509:                               new String(buffer, 0, bufpos + 1);
 510:   }
 511: 
 512:   public char[] GetSuffix(int len)
 513:   {
 514:      char[] ret = new char[len];
 515: 
 516:      if ((bufpos + 1) >= len)
 517:         System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
 518:      else
 519:      {
 520:         System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
 521:                                                           len - bufpos - 1);
 522:         System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
 523:      }
 524: 
 525:      return ret;
 526:   }
 527: 
 528:   public void Done()
 529:   {
 530:      nextCharBuf = null;
 531:      buffer = null;
 532:      bufline = null;
 533:      bufcolumn = null;
 534:   }
 535: 
 536:   /**
 537:    * Method to adjust line and column numbers for the start of a token.
 538:    */
 539:   public void adjustBeginLineColumn(int newLine, int newCol)
 540:   {
 541:      int start = tokenBegin;
 542:      int len;
 543: 
 544:      if (bufpos >= tokenBegin)
 545:      {
 546:         len = bufpos - tokenBegin + inBuf + 1;
 547:      }
 548:      else
 549:      {
 550:         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
 551:      }
 552: 
 553:      int i = 0, j = 0, k = 0;
 554:      int nextColDiff = 0, columnDiff = 0;
 555: 
 556:      while (i < len &&
 557:             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
 558:      {
 559:         bufline[j] = newLine;
 560:         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
 561:         bufcolumn[j] = newCol + columnDiff;
 562:         columnDiff = nextColDiff;
 563:         i++;
 564:      } 
 565: 
 566:      if (i < len)
 567:      {
 568:         bufline[j] = newLine++;
 569:         bufcolumn[j] = newCol + columnDiff;
 570: 
 571:         while (i++ < len)
 572:         {
 573:            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
 574:               bufline[j] = newLine++;
 575:            else
 576:               bufline[j] = newLine;
 577:         }
 578:      }
 579: 
 580:      line = bufline[j];
 581:      column = bufcolumn[j];
 582:   }
 583: 
 584: }