home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / io / StreamTokenizer.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  5.9 KB  |  382 lines

  1. package java.io;
  2.  
  3. public class StreamTokenizer {
  4.    private InputStream input;
  5.    private char[] buf;
  6.    private int peekc = 32;
  7.    private boolean pushedBack;
  8.    private boolean forceLower;
  9.    private int LINENO = 1;
  10.    private boolean eolIsSignificantP = false;
  11.    private boolean slashSlashCommentsP = false;
  12.    private boolean slashStarCommentsP = false;
  13.    private byte[] ctype = new byte[256];
  14.    private static final byte CT_WHITESPACE = 1;
  15.    private static final byte CT_DIGIT = 2;
  16.    private static final byte CT_ALPHA = 4;
  17.    private static final byte CT_QUOTE = 8;
  18.    private static final byte CT_COMMENT = 16;
  19.    public int ttype;
  20.    public static final int TT_EOF = -1;
  21.    public static final int TT_EOL = 10;
  22.    public static final int TT_NUMBER = -2;
  23.    public static final int TT_WORD = -3;
  24.    public String sval;
  25.    public double nval;
  26.  
  27.    public StreamTokenizer(InputStream I) {
  28.       this.input = I;
  29.       this.buf = new char[20];
  30.       this.wordChars(97, 122);
  31.       this.wordChars(65, 90);
  32.       this.wordChars(160, 255);
  33.       this.whitespaceChars(0, 32);
  34.       this.commentChar(47);
  35.       this.quoteChar(34);
  36.       this.quoteChar(39);
  37.       this.parseNumbers();
  38.    }
  39.  
  40.    public void resetSyntax() {
  41.       int i = this.ctype.length;
  42.  
  43.       while(true) {
  44.          --i;
  45.          if (i < 0) {
  46.             return;
  47.          }
  48.  
  49.          this.ctype[i] = 0;
  50.       }
  51.    }
  52.  
  53.    public void wordChars(int low, int hi) {
  54.       if (low < 0) {
  55.          low = 0;
  56.       }
  57.  
  58.       if (hi > this.ctype.length) {
  59.          hi = this.ctype.length;
  60.       }
  61.  
  62.       while(low <= hi) {
  63.          byte[] var10000 = this.ctype;
  64.          int var10001 = low++;
  65.          var10000[var10001] = (byte)(var10000[var10001] | 4);
  66.       }
  67.  
  68.    }
  69.  
  70.    public void whitespaceChars(int low, int hi) {
  71.       if (low < 0) {
  72.          low = 0;
  73.       }
  74.  
  75.       if (hi > this.ctype.length) {
  76.          hi = this.ctype.length;
  77.       }
  78.  
  79.       while(low <= hi) {
  80.          this.ctype[low++] = 1;
  81.       }
  82.  
  83.    }
  84.  
  85.    public void ordinaryChars(int low, int hi) {
  86.       if (low < 0) {
  87.          low = 0;
  88.       }
  89.  
  90.       if (hi > this.ctype.length) {
  91.          hi = this.ctype.length;
  92.       }
  93.  
  94.       while(low <= hi) {
  95.          this.ctype[low++] = 0;
  96.       }
  97.  
  98.    }
  99.  
  100.    public void ordinaryChar(int ch) {
  101.       this.ctype[ch] = 0;
  102.    }
  103.  
  104.    public void commentChar(int ch) {
  105.       this.ctype[ch] = 16;
  106.    }
  107.  
  108.    public void quoteChar(int ch) {
  109.       this.ctype[ch] = 8;
  110.    }
  111.  
  112.    public void parseNumbers() {
  113.       for(int i = 48; i <= 57; ++i) {
  114.          byte[] var10000 = this.ctype;
  115.          var10000[i] = (byte)(var10000[i] | 2);
  116.       }
  117.  
  118.       byte[] var2 = this.ctype;
  119.       var2[46] = (byte)(var2[46] | 2);
  120.       var2 = this.ctype;
  121.       var2[45] = (byte)(var2[45] | 2);
  122.    }
  123.  
  124.    public void eolIsSignificant(boolean flag) {
  125.       this.eolIsSignificantP = flag;
  126.    }
  127.  
  128.    public void slashStarComments(boolean flag) {
  129.       this.slashStarCommentsP = flag;
  130.    }
  131.  
  132.    public void slashSlashComments(boolean flag) {
  133.       this.slashSlashCommentsP = flag;
  134.    }
  135.  
  136.    public void lowerCaseMode(boolean fl) {
  137.       this.forceLower = fl;
  138.    }
  139.  
  140.    public int nextToken() throws IOException {
  141.       if (this.pushedBack) {
  142.          this.pushedBack = false;
  143.          return this.ttype;
  144.       } else {
  145.          InputStream is = this.input;
  146.          byte[] ct = this.ctype;
  147.          int c = this.peekc;
  148.          this.sval = null;
  149.          if (c < 0) {
  150.             return this.ttype = -1;
  151.          } else {
  152.             int ctype;
  153.             for(ctype = c < 256 ? ct[c] : 4; (ctype & 1) != 0; ctype = c < 256 ? ct[c] : 4) {
  154.                if (c == 13) {
  155.                   ++this.LINENO;
  156.                   c = is.read();
  157.                   if (c == 10) {
  158.                      c = is.read();
  159.                   }
  160.  
  161.                   if (this.eolIsSignificantP) {
  162.                      this.peekc = c;
  163.                      return this.ttype = 10;
  164.                   }
  165.                } else {
  166.                   if (c == 10) {
  167.                      ++this.LINENO;
  168.                      if (this.eolIsSignificantP) {
  169.                         this.peekc = 32;
  170.                         return this.ttype = 10;
  171.                      }
  172.                   }
  173.  
  174.                   c = is.read();
  175.                }
  176.  
  177.                if (c < 0) {
  178.                   return this.ttype = -1;
  179.                }
  180.             }
  181.  
  182.             if ((ctype & 2) != 0) {
  183.                boolean neg = false;
  184.                if (c == 45) {
  185.                   c = is.read();
  186.                   if (c != 46 && (c < 48 || c > 57)) {
  187.                      this.peekc = c;
  188.                      return this.ttype = 45;
  189.                   }
  190.  
  191.                   neg = true;
  192.                }
  193.  
  194.                double v = (double)0.0F;
  195.                int decexp = 0;
  196.                int seendot = 0;
  197.  
  198.                while(true) {
  199.                   if (c == 46 && seendot == 0) {
  200.                      seendot = 1;
  201.                   } else {
  202.                      if (c < 48 || c > 57) {
  203.                         this.peekc = c;
  204.                         if (decexp != 0) {
  205.                            double denom = (double)10.0F;
  206.                            --decexp;
  207.  
  208.                            while(decexp > 0) {
  209.                               denom *= (double)10.0F;
  210.                               --decexp;
  211.                            }
  212.  
  213.                            v /= denom;
  214.                         }
  215.  
  216.                         this.nval = neg ? -v : v;
  217.                         return this.ttype = -2;
  218.                      }
  219.  
  220.                      v = v * (double)10.0F + (double)(c - 48);
  221.                      decexp += seendot;
  222.                   }
  223.  
  224.                   c = is.read();
  225.                }
  226.             } else if ((ctype & 4) != 0) {
  227.                int i = 0;
  228.  
  229.                do {
  230.                   if (i >= this.buf.length) {
  231.                      char[] nb = new char[this.buf.length * 2];
  232.                      System.arraycopy(this.buf, 0, nb, 0, this.buf.length);
  233.                      this.buf = nb;
  234.                   }
  235.  
  236.                   this.buf[i++] = (char)c;
  237.                   c = is.read();
  238.                   ctype = c < 0 ? 1 : (c < 256 ? ct[c] : 4);
  239.                } while((ctype & 6) != 0);
  240.  
  241.                this.peekc = c;
  242.                this.sval = String.copyValueOf(this.buf, 0, i);
  243.                if (this.forceLower) {
  244.                   this.sval = this.sval.toLowerCase();
  245.                }
  246.  
  247.                return this.ttype = -3;
  248.             } else if ((ctype & 16) != 0) {
  249.                while((c = is.read()) != 10 && c != 13 && c >= 0) {
  250.                }
  251.  
  252.                this.peekc = c;
  253.                return this.nextToken();
  254.             } else if ((ctype & 8) != 0) {
  255.                this.ttype = c;
  256.  
  257.                int i;
  258.                for(i = 0; (c = is.read()) >= 0 && c != this.ttype && c != 10 && c != 13; this.buf[i++] = (char)c) {
  259.                   if (c == 92) {
  260.                      switch (c = is.read()) {
  261.                         case 98:
  262.                            c = 8;
  263.                            break;
  264.                         case 97:
  265.                            c = 7;
  266.                            break;
  267.                         case 55:
  268.                         case 54:
  269.                         case 53:
  270.                         case 52:
  271.                         case 51:
  272.                         case 50:
  273.                         case 49:
  274.                         case 48:
  275.                            c -= 48;
  276.                            int c2 = is.read();
  277.                            if (c2 >= 48 && c2 <= 55) {
  278.                               c = (c << 3) + (c2 - 48);
  279.                               c2 = is.read();
  280.                               if (c2 >= 48 && c2 <= 55) {
  281.                                  c = (c << 3) + (c2 - 48);
  282.                               } else {
  283.                                  this.peekc = c;
  284.                               }
  285.                            } else {
  286.                               this.peekc = c;
  287.                            }
  288.                            break;
  289.                         case 118:
  290.                            c = 11;
  291.                            break;
  292.                         case 116:
  293.                            c = 9;
  294.                            break;
  295.                         case 114:
  296.                            c = 13;
  297.                            break;
  298.                         case 110:
  299.                            c = 10;
  300.                            break;
  301.                         case 102:
  302.                            c = 12;
  303.                      }
  304.                   }
  305.  
  306.                   if (i >= this.buf.length) {
  307.                      char[] nb = new char[this.buf.length * 2];
  308.                      System.arraycopy(this.buf, 0, nb, 0, this.buf.length);
  309.                      this.buf = nb;
  310.                   }
  311.                }
  312.  
  313.                this.peekc = 32;
  314.                this.sval = String.copyValueOf(this.buf, 0, i);
  315.                return this.ttype;
  316.             } else if (c == 47 && (this.slashSlashCommentsP || this.slashStarCommentsP)) {
  317.                c = is.read();
  318.                if (c == 42 && this.slashStarCommentsP) {
  319.                   for(int prevc = 0; (c = is.read()) != 47 || prevc != 42; prevc = c) {
  320.                      if (c == 10) {
  321.                         ++this.LINENO;
  322.                      }
  323.  
  324.                      if (c < 0) {
  325.                         return this.ttype = -1;
  326.                      }
  327.                   }
  328.  
  329.                   this.peekc = 32;
  330.                   return this.nextToken();
  331.                } else if (c == 47 && this.slashSlashCommentsP) {
  332.                   while((c = is.read()) != 10 && c != 13 && c >= 0) {
  333.                   }
  334.  
  335.                   this.peekc = c;
  336.                   return this.nextToken();
  337.                } else {
  338.                   this.peekc = c;
  339.                   return this.ttype = 47;
  340.                }
  341.             } else {
  342.                this.peekc = 32;
  343.                return this.ttype = c;
  344.             }
  345.          }
  346.       }
  347.    }
  348.  
  349.    public void pushBack() {
  350.       this.pushedBack = true;
  351.    }
  352.  
  353.    public int lineno() {
  354.       return this.LINENO;
  355.    }
  356.  
  357.    public String toString() {
  358.       String ret;
  359.       switch (this.ttype) {
  360.          case -1:
  361.             ret = "EOF";
  362.             break;
  363.          case -2:
  364.             ret = "n=" + this.nval;
  365.             break;
  366.          case -3:
  367.             ret = this.sval;
  368.             break;
  369.          case 10:
  370.             ret = "EOL";
  371.             break;
  372.          default:
  373.             char[] s = new char[3];
  374.             s[0] = s[2] = '\'';
  375.             s[1] = (char)this.ttype;
  376.             ret = new String(s);
  377.       }
  378.  
  379.       return "Token[" + ret + "], line " + this.LINENO;
  380.    }
  381. }
  382.