home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 139 / dpcs0999.iso / Web / CFserver / data1.cab / Java / CFJava.cab / CFJavaRuntime.cab / netscape / util / TokenGenerator.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-10-01  |  6.9 KB  |  394 lines

  1. package netscape.util;
  2.  
  3. import java.io.FilterInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6.  
  7. class TokenGenerator extends FilterInputStream {
  8.    public static final byte NULL_TOKEN = 0;
  9.    public static final byte STRING_TOKEN = 1;
  10.    public static final byte ARRAY_BEGIN_TOKEN = 2;
  11.    public static final byte ARRAY_END_TOKEN = 3;
  12.    public static final byte VECTOR_BEGIN_TOKEN = 4;
  13.    public static final byte VECTOR_END_TOKEN = 5;
  14.    public static final byte HASHTABLE_BEGIN_TOKEN = 6;
  15.    public static final byte HASHTABLE_KEY_VALUE_SEP_TOKEN = 7;
  16.    public static final byte HASHTABLE_KEY_VALUE_END_TOKEN = 8;
  17.    public static final byte HASHTABLE_END_TOKEN = 9;
  18.    public static final byte GENERIC_SEP_TOKEN = 10;
  19.    public static final byte NULL_VALUE_TOKEN = 11;
  20.    public static final byte LAST_TOKEN_TYPE = 11;
  21.    static byte[] tokenToAscii = new byte[12];
  22.    static byte[] asciiToToken;
  23.    static final int CHARACTER_COUNT_PER_ARRAY = 128;
  24.    static final int CCPA_BIT_COUNT = 7;
  25.    static final int CCPA_MASK = 127;
  26.    static final int PARSING_NONE_STATE = 0;
  27.    static final int PARSING_STRING_STATE = 1;
  28.    static final int PARSING_QUOTED_STRING_STATE = 2;
  29.    static final int PARSING_COMMENT_STATE = 3;
  30.    static final int PARSING_C_STYLE_COMMENT_STATE = 4;
  31.    static final int PARSING_C_PLUS_PLUS_STYLE_COMMENT_STATE = 5;
  32.    private byte[][] input = new byte[1][];
  33.    private int nextAvailableByteIndex;
  34.    private int markedByteIndex;
  35.    private int nextFreeByteSlotIndex;
  36.    private byte[] bytesForCurrentToken;
  37.    private int currentToken;
  38.    private int lastToken;
  39.    private int currentLineNumber;
  40.    private boolean previousCharacterWasBackslash = false;
  41.    private boolean starFound = false;
  42.    private int parserState;
  43.  
  44.    public TokenGenerator(InputStream var1) {
  45.       super(var1);
  46.       this.input[0] = new byte[128];
  47.       this.nextAvailableByteIndex = 0;
  48.       this.nextFreeByteSlotIndex = 0;
  49.       this.currentLineNumber = 0;
  50.       this.parserState = 0;
  51.    }
  52.  
  53.    private final void markCurrentCharacter() {
  54.       this.markedByteIndex = this.nextAvailableByteIndex;
  55.    }
  56.  
  57.    private final void markPreviousCharacter() {
  58.       this.markedByteIndex = this.nextAvailableByteIndex - 1;
  59.    }
  60.  
  61.    private final void growInputBuffer() {
  62.       byte[][] var1 = new byte[this.input.length + 1][];
  63.       System.arraycopy(this.input, 0, var1, 0, this.input.length);
  64.       var1[this.input.length] = new byte[128];
  65.       this.input = var1;
  66.    }
  67.  
  68.    private final void readMoreCharacters() throws IOException {
  69.       int var2 = this.nextFreeByteSlotIndex >> 7;
  70.       if (var2 >= this.input.length) {
  71.          this.growInputBuffer();
  72.       }
  73.  
  74.       int var1 = ((FilterInputStream)this).read(this.input[var2], this.nextFreeByteSlotIndex & 127, 128 - (this.nextFreeByteSlotIndex & 127));
  75.       if (var1 != -1) {
  76.          this.nextFreeByteSlotIndex += var1;
  77.          if (((FilterInputStream)this).available() > 0 && var1 < 128) {
  78.             var2 = this.nextFreeByteSlotIndex >> 7;
  79.             if (var2 >= this.input.length) {
  80.                this.growInputBuffer();
  81.             }
  82.  
  83.             var1 = ((FilterInputStream)this).read(this.input[var2], this.nextFreeByteSlotIndex & 127, 128 - (this.nextFreeByteSlotIndex & 127));
  84.             if (var1 != -1) {
  85.                this.nextFreeByteSlotIndex += var1;
  86.             }
  87.          }
  88.  
  89.       }
  90.    }
  91.  
  92.    private final boolean hasMoreCharacters() throws IOException {
  93.       if (this.nextAvailableByteIndex < this.nextFreeByteSlotIndex) {
  94.          return true;
  95.       } else {
  96.          this.readMoreCharacters();
  97.          return this.nextAvailableByteIndex < this.nextFreeByteSlotIndex;
  98.       }
  99.    }
  100.  
  101.    private final byte peekNextCharacter() throws IOException {
  102.       byte var1 = 0;
  103.       if (this.nextAvailableByteIndex >= this.nextFreeByteSlotIndex) {
  104.          this.readMoreCharacters();
  105.       }
  106.  
  107.       if (this.nextAvailableByteIndex < this.nextFreeByteSlotIndex) {
  108.          var1 = this.input[this.nextAvailableByteIndex >> 7][this.nextAvailableByteIndex & 127];
  109.          ++this.nextAvailableByteIndex;
  110.       }
  111.  
  112.       return var1;
  113.    }
  114.  
  115.    private final void rewindToMarkedCharacter() {
  116.       this.nextAvailableByteIndex = this.markedByteIndex;
  117.    }
  118.  
  119.    private final void deletePeekedCharacters() {
  120.       for(this.markedByteIndex = -1; this.nextAvailableByteIndex >> 7 > 0; this.nextFreeByteSlotIndex -= 128) {
  121.          byte[] var1 = this.input[0];
  122.          int var2 = 0;
  123.  
  124.          for(int var3 = this.input.length - 1; var2 < var3; ++var2) {
  125.             this.input[var2] = this.input[var2 + 1];
  126.          }
  127.  
  128.          this.input[this.input.length - 1] = var1;
  129.          this.nextAvailableByteIndex -= 128;
  130.       }
  131.  
  132.    }
  133.  
  134.    private final void deletePeekedCharactersMinusOne() {
  135.       for(this.markedByteIndex = -1; this.nextAvailableByteIndex - 1 >> 7 > 0; this.nextFreeByteSlotIndex -= 128) {
  136.          byte[] var1 = this.input[0];
  137.          int var2 = 0;
  138.  
  139.          for(int var3 = this.input.length - 1; var2 < var3; ++var2) {
  140.             this.input[var2] = this.input[var2 + 1];
  141.          }
  142.  
  143.          this.input[this.input.length - 1] = var1;
  144.          this.nextAvailableByteIndex -= 128;
  145.       }
  146.  
  147.    }
  148.  
  149.    private final byte[] getAndDeletePeekedCharacters() {
  150.       int var1 = this.nextAvailableByteIndex - this.markedByteIndex;
  151.       byte[] var2 = new byte[var1];
  152.       int var3 = this.markedByteIndex;
  153.  
  154.       for(int var4 = this.markedByteIndex + var1; var3 < var4; ++var3) {
  155.          var2[var3 - this.markedByteIndex] = this.input[var3 >> 7][var3 & 127];
  156.       }
  157.  
  158.       this.deletePeekedCharacters();
  159.       this.markedByteIndex = -1;
  160.       return var2;
  161.    }
  162.  
  163.    private final byte[] getAndDeletePeekedCharactersMinusOne() {
  164.       int var1 = this.nextAvailableByteIndex - this.markedByteIndex - 1;
  165.       byte[] var2 = new byte[var1];
  166.       int var3 = this.markedByteIndex;
  167.  
  168.       for(int var4 = this.markedByteIndex + var1; var3 < var4; ++var3) {
  169.          var2[var3 - this.markedByteIndex] = this.input[var3 >> 7][var3 & 127];
  170.       }
  171.  
  172.       this.deletePeekedCharactersMinusOne();
  173.       this.markedByteIndex = -1;
  174.       return var2;
  175.    }
  176.  
  177.    private final void parseOneToken() throws DeserializationException, IOException {
  178.       while(this.currentToken == 0) {
  179.          if (this.nextAvailableByteIndex >= this.nextFreeByteSlotIndex) {
  180.             this.readMoreCharacters();
  181.             if (this.nextAvailableByteIndex >= this.nextFreeByteSlotIndex) {
  182.                break;
  183.             }
  184.          }
  185.  
  186.          if (this.markedByteIndex == -1) {
  187.             this.markedByteIndex = this.nextAvailableByteIndex;
  188.          }
  189.  
  190.          byte var1 = this.input[this.nextAvailableByteIndex >> 7][this.nextAvailableByteIndex & 127];
  191.          ++this.nextAvailableByteIndex;
  192.          if (var1 == 10) {
  193.             ++this.currentLineNumber;
  194.          }
  195.  
  196.          if (this.parserState == 2) {
  197.             if (!this.previousCharacterWasBackslash && var1 == 34) {
  198.                this.currentToken = 1;
  199.                this.bytesForCurrentToken = this.getAndDeletePeekedCharacters();
  200.                this.parserState = 0;
  201.                this.markedByteIndex = this.nextAvailableByteIndex;
  202.                this.previousCharacterWasBackslash = false;
  203.             } else if (var1 == 92) {
  204.                this.previousCharacterWasBackslash = true;
  205.             } else {
  206.                this.previousCharacterWasBackslash = false;
  207.             }
  208.          } else {
  209.             byte var2;
  210.             if (var1 >= 0 && var1 < 127) {
  211.                var2 = asciiToToken[var1];
  212.             } else {
  213.                var2 = 0;
  214.             }
  215.  
  216.             if (this.parserState == 1) {
  217.                if (var1 == 34 || var2 != 1) {
  218.                   this.currentToken = 1;
  219.                   this.bytesForCurrentToken = this.getAndDeletePeekedCharactersMinusOne();
  220.                   this.parserState = 0;
  221.                   this.markedByteIndex = this.nextAvailableByteIndex - 1;
  222.                   this.rewindToMarkedCharacter();
  223.                }
  224.             } else if (this.parserState == 3) {
  225.                if (var1 == 42) {
  226.                   this.parserState = 4;
  227.                } else {
  228.                   if (var1 != 47) {
  229.                      throw new DeserializationException("Syntax error at line " + this.lineForLastToken(), this.lineForLastToken());
  230.                   }
  231.  
  232.                   this.parserState = 5;
  233.                }
  234.             } else if (this.parserState == 4) {
  235.                if (this.starFound && var1 == 47) {
  236.                   this.starFound = false;
  237.                   this.parserState = 0;
  238.                } else if (var1 == 42) {
  239.                   this.starFound = true;
  240.                } else {
  241.                   this.starFound = false;
  242.                }
  243.             } else if (this.parserState == 5) {
  244.                if (var1 == 10) {
  245.                   this.parserState = 0;
  246.                }
  247.             } else if (var1 == 47) {
  248.                this.parserState = 3;
  249.             } else if (var2 != 0) {
  250.                if (var2 == 1) {
  251.                   if (var1 == 34) {
  252.                      this.parserState = 2;
  253.                   } else {
  254.                      this.parserState = 1;
  255.                   }
  256.  
  257.                   this.deletePeekedCharactersMinusOne();
  258.                   this.markedByteIndex = this.nextAvailableByteIndex - 1;
  259.                } else {
  260.                   this.currentToken = var2;
  261.                   this.bytesForCurrentToken = null;
  262.  
  263.                   for(this.markedByteIndex = -1; this.nextAvailableByteIndex >> 7 > 0; this.nextFreeByteSlotIndex -= 128) {
  264.                      byte[] var3 = this.input[0];
  265.                      int var4 = 0;
  266.  
  267.                      for(int var5 = this.input.length - 1; var4 < var5; ++var4) {
  268.                         this.input[var4] = this.input[var4 + 1];
  269.                      }
  270.  
  271.                      this.input[this.input.length - 1] = var3;
  272.                      this.nextAvailableByteIndex -= 128;
  273.                   }
  274.  
  275.                   this.markedByteIndex = this.nextAvailableByteIndex;
  276.                }
  277.             }
  278.          }
  279.       }
  280.  
  281.       if (this.currentToken == 0 && !this.hasMoreCharacters()) {
  282.          switch (this.parserState) {
  283.             case 0:
  284.             default:
  285.                break;
  286.             case 1:
  287.                this.currentToken = 1;
  288.                this.bytesForCurrentToken = this.getAndDeletePeekedCharacters();
  289.                this.parserState = 0;
  290.                this.previousCharacterWasBackslash = false;
  291.                return;
  292.             case 2:
  293.                this.parserState = 0;
  294.                this.previousCharacterWasBackslash = false;
  295.                throw new DeserializationException("Unterminated string at line " + this.lineForLastToken(), this.lineForLastToken());
  296.             case 3:
  297.                this.parserState = 0;
  298.                throw new DeserializationException("Syntax error at line " + this.lineForLastToken(), this.lineForLastToken());
  299.             case 4:
  300.                this.parserState = 0;
  301.                this.starFound = false;
  302.                throw new DeserializationException("Unterminated comment at line " + this.lineForLastToken(), this.lineForLastToken());
  303.             case 5:
  304.                this.parserState = 0;
  305.                return;
  306.          }
  307.       }
  308.  
  309.    }
  310.  
  311.    public final boolean hasMoreTokens() throws DeserializationException, IOException {
  312.       if (this.currentToken != 0) {
  313.          return true;
  314.       } else {
  315.          this.parseOneToken();
  316.          return this.currentToken != 0;
  317.       }
  318.    }
  319.  
  320.    public final int nextToken() throws DeserializationException, IOException {
  321.       int var1 = 0;
  322.       if (this.currentToken == 0) {
  323.          this.parseOneToken();
  324.       }
  325.  
  326.       if (this.currentToken != 0) {
  327.          var1 = this.currentToken;
  328.          this.lastToken = this.currentToken;
  329.          this.currentToken = 0;
  330.       }
  331.  
  332.       return var1;
  333.    }
  334.  
  335.    public final int peekNextToken() throws DeserializationException, IOException {
  336.       this.hasMoreTokens();
  337.       this.lastToken = this.currentToken;
  338.       return this.currentToken;
  339.    }
  340.  
  341.    public final byte[] bytesForLastToken() {
  342.       if (this.lastToken == 1) {
  343.          return this.bytesForCurrentToken;
  344.       } else {
  345.          byte[] var1 = new byte[]{tokenToAscii[this.lastToken]};
  346.          return var1;
  347.       }
  348.    }
  349.  
  350.    public byte byteForLastToken() {
  351.       return tokenToAscii[this.lastToken];
  352.    }
  353.  
  354.    public int lineForLastToken() {
  355.       return this.currentLineNumber + 1;
  356.    }
  357.  
  358.    static {
  359.       tokenToAscii[0] = 0;
  360.       tokenToAscii[1] = 0;
  361.       tokenToAscii[2] = 91;
  362.       tokenToAscii[3] = 93;
  363.       tokenToAscii[4] = 40;
  364.       tokenToAscii[5] = 41;
  365.       tokenToAscii[6] = 123;
  366.       tokenToAscii[7] = 61;
  367.       tokenToAscii[8] = 59;
  368.       tokenToAscii[9] = 125;
  369.       tokenToAscii[10] = 44;
  370.       tokenToAscii[11] = 64;
  371.       asciiToToken = new byte[127];
  372.  
  373.       for(int var0 = 0; var0 <= 32; ++var0) {
  374.          asciiToToken[var0] = 0;
  375.       }
  376.  
  377.       for(int var1 = 33; var1 < 127; ++var1) {
  378.          asciiToToken[var1] = 1;
  379.       }
  380.  
  381.       asciiToToken[91] = 2;
  382.       asciiToToken[93] = 3;
  383.       asciiToToken[40] = 4;
  384.       asciiToToken[41] = 5;
  385.       asciiToToken[123] = 6;
  386.       asciiToToken[61] = 7;
  387.       asciiToToken[59] = 8;
  388.       asciiToToken[125] = 9;
  389.       asciiToToken[44] = 10;
  390.       asciiToToken[64] = 11;
  391.       asciiToToken[47] = 0;
  392.    }
  393. }
  394.