home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML Construction Kit / Dynamic HTML Construction Kit.iso / earthlink / nscomm / java40.jar / java / io / StreamTokenizer.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-11-03  |  4.4 KB  |  444 lines

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