1 7 8 package java.io; 9 10 11 46 47 public class StreamTokenizer { 48 49 50 private Reader reader = null; 51 private InputStream input = null; 52 53 private char buf[] = new char[20]; 54 55 62 private int peekc = NEED_CHAR; 63 64 private static final int NEED_CHAR = Integer.MAX_VALUE; 65 private static final int SKIP_LF = Integer.MAX_VALUE - 1; 66 67 private boolean pushedBack; 68 private boolean forceLower; 69 70 private int LINENO = 1; 71 72 private boolean eolIsSignificantP = false; 73 private boolean slashSlashCommentsP = false; 74 private boolean slashStarCommentsP = false; 75 76 private byte ctype[] = new byte[256]; 77 private static final byte CT_WHITESPACE = 1; 78 private static final byte CT_DIGIT = 2; 79 private static final byte CT_ALPHA = 4; 80 private static final byte CT_QUOTE = 8; 81 private static final byte CT_COMMENT = 16; 82 83 110 public int ttype = TT_NOTHING; 111 112 115 public static final int TT_EOF = -1; 116 117 120 public static final int TT_EOL = '\n'; 121 122 125 public static final int TT_NUMBER = -2; 126 127 130 public static final int TT_WORD = -3; 131 132 136 private static final int TT_NOTHING = -4; 137 138 155 public String sval; 156 157 167 public double nval; 168 169 170 private StreamTokenizer() { 171 wordChars('a', 'z'); 172 wordChars('A', 'Z'); 173 wordChars(128 + 32, 255); 174 whitespaceChars(0, ' '); 175 commentChar('/'); 176 quoteChar('"'); 177 quoteChar('\''); 178 parseNumbers(); 179 } 180 181 212 @Deprecated 213 public StreamTokenizer(InputStream is) { 214 this(); 215 if (is == null) { 216 throw new NullPointerException (); 217 } 218 input = is; 219 } 220 221 227 public StreamTokenizer(Reader r) { 228 this(); 229 if (r == null) { 230 throw new NullPointerException (); 231 } 232 reader = r; 233 } 234 235 242 public void resetSyntax() { 243 for (int i = ctype.length; --i >= 0;) 244 ctype[i] = 0; 245 } 246 247 256 public void wordChars(int low, int hi) { 257 if (low < 0) 258 low = 0; 259 if (hi >= ctype.length) 260 hi = ctype.length - 1; 261 while (low <= hi) 262 ctype[low++] |= CT_ALPHA; 263 } 264 265 277 public void whitespaceChars(int low, int hi) { 278 if (low < 0) 279 low = 0; 280 if (hi >= ctype.length) 281 hi = ctype.length - 1; 282 while (low <= hi) 283 ctype[low++] = CT_WHITESPACE; 284 } 285 286 297 public void ordinaryChars(int low, int hi) { 298 if (low < 0) 299 low = 0; 300 if (hi >= ctype.length) 301 hi = ctype.length - 1; 302 while (low <= hi) 303 ctype[low++] = 0; 304 } 305 306 323 public void ordinaryChar(int ch) { 324 if (ch >= 0 && ch < ctype.length) 325 ctype[ch] = 0; 326 } 327 328 337 public void commentChar(int ch) { 338 if (ch >= 0 && ch < ctype.length) 339 ctype[ch] = CT_COMMENT; 340 } 341 342 366 public void quoteChar(int ch) { 367 if (ch >= 0 && ch < ctype.length) 368 ctype[ch] = CT_QUOTE; 369 } 370 371 391 public void parseNumbers() { 392 for (int i = '0'; i <= '9'; i++) 393 ctype[i] |= CT_DIGIT; 394 ctype['.'] |= CT_DIGIT; 395 ctype['-'] |= CT_DIGIT; 396 } 397 398 421 public void eolIsSignificant(boolean flag) { 422 eolIsSignificantP = flag; 423 } 424 425 437 public void slashStarComments(boolean flag) { 438 slashStarCommentsP = flag; 439 } 440 441 454 public void slashSlashComments(boolean flag) { 455 slashSlashCommentsP = flag; 456 } 457 458 475 public void lowerCaseMode(boolean fl) { 476 forceLower = fl; 477 } 478 479 480 private int read() throws IOException { 481 if (reader != null) 482 return reader.read(); 483 else if (input != null) 484 return input.read(); 485 else 486 throw new IllegalStateException (); 487 } 488 489 507 public int nextToken() throws IOException { 508 if (pushedBack) { 509 pushedBack = false; 510 return ttype; 511 } 512 byte ct[] = ctype; 513 sval = null; 514 515 int c = peekc; 516 if (c < 0) 517 c = NEED_CHAR; 518 if (c == SKIP_LF) { 519 c = read(); 520 if (c < 0) 521 return ttype = TT_EOF; 522 if (c == '\n') 523 c = NEED_CHAR; 524 } 525 if (c == NEED_CHAR) { 526 c = read(); 527 if (c < 0) 528 return ttype = TT_EOF; 529 } 530 ttype = c; 531 532 535 peekc = NEED_CHAR; 536 537 int ctype = c < 256 ? ct[c] : CT_ALPHA; 538 while ((ctype & CT_WHITESPACE) != 0) { 539 if (c == '\r') { 540 LINENO++; 541 if (eolIsSignificantP) { 542 peekc = SKIP_LF; 543 return ttype = TT_EOL; 544 } 545 c = read(); 546 if (c == '\n') 547 c = read(); 548 } else { 549 if (c == '\n') { 550 LINENO++; 551 if (eolIsSignificantP) { 552 return ttype = TT_EOL; 553 } 554 } 555 c = read(); 556 } 557 if (c < 0) 558 return ttype = TT_EOF; 559 ctype = c < 256 ? ct[c] : CT_ALPHA; 560 } 561 562 if ((ctype & CT_DIGIT) != 0) { 563 boolean neg = false; 564 if (c == '-') { 565 c = read(); 566 if (c != '.' && (c < '0' || c > '9')) { 567 peekc = c; 568 return ttype = '-'; 569 } 570 neg = true; 571 } 572 double v = 0; 573 int decexp = 0; 574 int seendot = 0; 575 while (true) { 576 if (c == '.' && seendot == 0) 577 seendot = 1; 578 else if ('0' <= c && c <= '9') { 579 v = v * 10 + (c - '0'); 580 decexp += seendot; 581 } else 582 break; 583 c = read(); 584 } 585 peekc = c; 586 if (decexp != 0) { 587 double denom = 10; 588 decexp--; 589 while (decexp > 0) { 590 denom *= 10; 591 decexp--; 592 } 593 594 v = v / denom; 595 } 596 nval = neg ? -v : v; 597 return ttype = TT_NUMBER; 598 } 599 600 if ((ctype & CT_ALPHA) != 0) { 601 int i = 0; 602 do { 603 if (i >= buf.length) { 604 char nb[] = new char[buf.length * 2]; 605 System.arraycopy(buf, 0, nb, 0, buf.length); 606 buf = nb; 607 } 608 buf[i++] = (char) c; 609 c = read(); 610 ctype = c < 0 ? CT_WHITESPACE : c < 256 ? ct[c] : CT_ALPHA; 611 } while ((ctype & (CT_ALPHA | CT_DIGIT)) != 0); 612 peekc = c; 613 sval = String.copyValueOf(buf, 0, i); 614 if (forceLower) 615 sval = sval.toLowerCase(); 616 return ttype = TT_WORD; 617 } 618 619 if ((ctype & CT_QUOTE) != 0) { 620 ttype = c; 621 int i = 0; 622 626 int d = read(); 627 while (d >= 0 && d != ttype && d != '\n' && d != '\r') { 628 if (d == '\\') { 629 c = read(); 630 int first = c; 631 if (c >= '0' && c <= '7') { 632 c = c - '0'; 633 int c2 = read(); 634 if ('0' <= c2 && c2 <= '7') { 635 c = (c << 3) + (c2 - '0'); 636 c2 = read(); 637 if ('0' <= c2 && c2 <= '7' && first <= '3') { 638 c = (c << 3) + (c2 - '0'); 639 d = read(); 640 } else 641 d = c2; 642 } else 643 d = c2; 644 } else { 645 switch (c) { 646 case 'a': 647 c = 0x7; 648 break; 649 case 'b': 650 c = '\b'; 651 break; 652 case 'f': 653 c = 0xC; 654 break; 655 case 'n': 656 c = '\n'; 657 break; 658 case 'r': 659 c = '\r'; 660 break; 661 case 't': 662 c = '\t'; 663 break; 664 case 'v': 665 c = 0xB; 666 break; 667 } 668 d = read(); 669 } 670 } else { 671 c = d; 672 d = read(); 673 } 674 if (i >= buf.length) { 675 char nb[] = new char[buf.length * 2]; 676 System.arraycopy(buf, 0, nb, 0, buf.length); 677 buf = nb; 678 } 679 buf[i++] = (char)c; 680 } 681 682 686 peekc = (d == ttype) ? NEED_CHAR : d; 687 688 sval = String.copyValueOf(buf, 0, i); 689 return ttype; 690 } 691 692 if (c == '/' && (slashSlashCommentsP || slashStarCommentsP)) { 693 c = read(); 694 if (c == '*' && slashStarCommentsP) { 695 int prevc = 0; 696 while ((c = read()) != '/' || prevc != '*') { 697 if (c == '\r') { 698 LINENO++; 699 c = read(); 700 if (c == '\n') { 701 c = read(); 702 } 703 } else { 704 if (c == '\n') { 705 LINENO++; 706 c = read(); 707 } 708 } 709 if (c < 0) 710 return ttype = TT_EOF; 711 prevc = c; 712 } 713 return nextToken(); 714 } else if (c == '/' && slashSlashCommentsP) { 715 while ((c = read()) != '\n' && c != '\r' && c >= 0); 716 peekc = c; 717 return nextToken(); 718 } else { 719 720 if ((ct['/'] & CT_COMMENT) != 0) { 721 while ((c = read()) != '\n' && c != '\r' && c >= 0); 722 peekc = c; 723 return nextToken(); 724 } else { 725 peekc = c; 726 return ttype = '/'; 727 } 728 } 729 } 730 731 if ((ctype & CT_COMMENT) != 0) { 732 while ((c = read()) != '\n' && c != '\r' && c >= 0); 733 peekc = c; 734 return nextToken(); 735 } 736 737 return ttype = c; 738 } 739 740 751 public void pushBack() { 752 if (ttype != TT_NOTHING) 753 pushedBack = true; 754 } 755 756 761 public int lineno() { 762 return LINENO; 763 } 764 765 779 public String toString() { 780 String ret; 781 switch (ttype) { 782 case TT_EOF: 783 ret = "EOF"; 784 break; 785 case TT_EOL: 786 ret = "EOL"; 787 break; 788 case TT_WORD: 789 ret = sval; 790 break; 791 case TT_NUMBER: 792 ret = "n=" + nval; 793 break; 794 case TT_NOTHING: 795 ret = "NOTHING"; 796 break; 797 default: { 798 804 if (ttype < 256 && 805 ((ctype[ttype] & CT_QUOTE) != 0)) { 806 ret = sval; 807 break; 808 } 809 810 char s[] = new char[3]; 811 s[0] = s[2] = '\''; 812 s[1] = (char) ttype; 813 ret = new String (s); 814 break; 815 } 816 } 817 return "Token[" + ret + "], line " + LINENO; 818 } 819 820 } 821 | Popular Tags |