⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 token.java

📁 java1.6众多例子参考
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        CharToken(int type, int ch) {            super(type);            this.chardata = ch;        }        int getChar() {            return this.chardata;        }        public String toString(int options) {            String ret;            switch (this.type) {              case CHAR:                switch (this.chardata) {                  case '|':  case '*':  case '+':  case '?':                  case '(':  case ')':  case '.':  case '[':                  case '{':  case '\\':                    ret = "\\"+(char)this.chardata;                    break;                  case '\f':  ret = "\\f";  break;                  case '\n':  ret = "\\n";  break;                  case '\r':  ret = "\\r";  break;                  case '\t':  ret = "\\t";  break;                  case 0x1b:  ret = "\\e";  break;                    //case 0x0b:  ret = "\\v";  break;                  default:                    if (this.chardata >= 0x10000) {                        String pre = "0"+Integer.toHexString(this.chardata);                        ret = "\\v"+pre.substring(pre.length()-6, pre.length());                    } else                        ret = ""+(char)this.chardata;                }                break;              case ANCHOR:                if (this == Token.token_linebeginning || this == Token.token_lineend)                    ret = ""+(char)this.chardata;                else                     ret = "\\"+(char)this.chardata;                break;              default:                ret = null;            }            return ret;        }        boolean match(int ch) {            if (this.type == CHAR) {                return ch == this.chardata;            } else                throw new RuntimeException("NFAArrow#match(): Internal error: "+this.type);        }    }    /**     * This class represents a node in parse tree.     */    static class ClosureToken extends Token implements java.io.Serializable {        private static final long serialVersionUID = 3545230349706932537L;                int min;        int max;        Token child;        ClosureToken(int type, Token tok) {            super(type);            this.child = tok;            this.setMin(-1);            this.setMax(-1);        }        int size() {            return 1;        }        Token getChild(int index) {            return this.child;        }        final void setMin(int min) {            this.min = min;        }        final void setMax(int max) {            this.max = max;        }        final int getMin() {            return this.min;        }        final int getMax() {            return this.max;        }        public String toString(int options) {            String ret;            if (this.type == CLOSURE) {                if (this.getMin() < 0 && this.getMax() < 0) {                    ret = this.child.toString(options)+"*";                } else if (this.getMin() == this.getMax()) {                    ret = this.child.toString(options)+"{"+this.getMin()+"}";                } else if (this.getMin() >= 0 && this.getMax() >= 0) {                    ret = this.child.toString(options)+"{"+this.getMin()+","+this.getMax()+"}";                } else if (this.getMin() >= 0 && this.getMax() < 0) {                    ret = this.child.toString(options)+"{"+this.getMin()+",}";                } else                    throw new RuntimeException("Token#toString(): CLOSURE "                                               +this.getMin()+", "+this.getMax());            } else {                if (this.getMin() < 0 && this.getMax() < 0) {                    ret = this.child.toString(options)+"*?";                } else if (this.getMin() == this.getMax()) {                    ret = this.child.toString(options)+"{"+this.getMin()+"}?";                } else if (this.getMin() >= 0 && this.getMax() >= 0) {                    ret = this.child.toString(options)+"{"+this.getMin()+","+this.getMax()+"}?";                } else if (this.getMin() >= 0 && this.getMax() < 0) {                    ret = this.child.toString(options)+"{"+this.getMin()+",}?";                } else                    throw new RuntimeException("Token#toString(): NONGREEDYCLOSURE "                                               +this.getMin()+", "+this.getMax());            }            return ret;        }    }    /**     * This class represents a node in parse tree.     */    static class ParenToken extends Token implements java.io.Serializable {        private static final long serialVersionUID = 3257572797621219636L;                Token child;        int parennumber;        ParenToken(int type, Token tok, int paren) {            super(type);            this.child = tok;            this.parennumber = paren;        }        int size() {            return 1;        }        Token getChild(int index) {            return this.child;        }        int getParenNumber() {            return this.parennumber;        }        public String toString(int options) {            String ret = null;            switch (this.type) {              case PAREN:                if (this.parennumber == 0) {                    ret = "(?:"+this.child.toString(options)+")";                } else {                    ret = "("+this.child.toString(options)+")";                }                break;              case LOOKAHEAD:                ret = "(?="+this.child.toString(options)+")";                break;              case NEGATIVELOOKAHEAD:                ret = "(?!"+this.child.toString(options)+")";                break;              case LOOKBEHIND:                ret = "(?<="+this.child.toString(options)+")";                break;              case NEGATIVELOOKBEHIND:                ret = "(?<!"+this.child.toString(options)+")";                break;              case INDEPENDENT:                ret = "(?>"+this.child.toString(options)+")";                break;            }            return ret;        }    }    /**     * (?(condition)yes-pattern|no-pattern)     */    static class ConditionToken extends Token implements java.io.Serializable {        private static final long serialVersionUID = 3761408607870399794L;                int refNumber;        Token condition;        Token yes;        Token no;        ConditionToken(int refno, Token cond, Token yespat, Token nopat) {            super(Token.CONDITION);            this.refNumber = refno;            this.condition = cond;            this.yes = yespat;            this.no = nopat;        }        int size() {            return this.no == null ? 1 : 2;        }        Token getChild(int index) {            if (index == 0)  return this.yes;            if (index == 1)  return this.no;            throw new RuntimeException("Internal Error: "+index);        }        public String toString(int options) {            String ret;            if (refNumber > 0) {                ret = "(?("+refNumber+")";            } else if (this.condition.type == Token.ANCHOR) {                ret = "(?("+this.condition+")";            } else {                ret = "(?"+this.condition;            }            if (this.no == null) {                ret += this.yes+")";            } else {                ret += this.yes+"|"+this.no+")";            }            return ret;        }    }    /**     * (ims-ims: .... )     */    static class ModifierToken extends Token implements java.io.Serializable {        private static final long serialVersionUID = 3258689892778324790L;                Token child;        int add;        int mask;        ModifierToken(Token tok, int add, int mask) {            super(Token.MODIFIERGROUP);            this.child = tok;            this.add = add;            this.mask = mask;        }        int size() {            return 1;        }        Token getChild(int index) {            return this.child;        }        int getOptions() {            return this.add;        }        int getOptionsMask() {            return this.mask;        }        public String toString(int options) {            return "(?"                +(this.add == 0 ? "" : REUtil.createOptionString(this.add))                +(this.mask == 0 ? "" : REUtil.createOptionString(this.mask))                +":"                +this.child.toString(options)                +")";        }    }    /**     * This class represents a node in parse tree.     * for UNION or CONCAT.     */    static class UnionToken extends Token implements java.io.Serializable {        private static final long serialVersionUID = 3256723987530003507L;                Vector children;        UnionToken(int type) {            super(type);        }        void addChild(Token tok) {            if (tok == null)  return;            if (this.children == null)  this.children = new Vector();            if (this.type == UNION) {                this.children.addElement(tok);                return;            }                                                // This is CONCAT, and new child is CONCAT.            if (tok.type == CONCAT) {                for (int i = 0;  i < tok.size();  i ++)                    this.addChild(tok.getChild(i)); // Recursion                return;            }            int size = this.children.size();            if (size == 0) {                this.children.addElement(tok);                return;            }            Token previous = (Token)this.children.elementAt(size-1);            if (!((previous.type == CHAR || previous.type == STRING)                  && (tok.type == CHAR || tok.type == STRING))) {                this.children.addElement(tok);                return;            }                        //System.err.println("Merge '"+previous+"' and '"+tok+"'.");            StringBuffer buffer;            int nextMaxLength = (tok.type == CHAR ? 2 : tok.getString().length());            if (previous.type == CHAR) {        // Replace previous token by STRING                buffer = new StringBuffer(2 + nextMaxLength);                int ch = previous.getChar();                if (ch >= 0x10000)                    buffer.append(REUtil.decomposeToSurrogates(ch));                else                    buffer.append((char)ch);                previous = Token.createString(null);                this.children.setElementAt(previous, size-1);            } else {                            // STRING                buffer = new StringBuffer(previous.getString().length() + nextMaxLength);                buffer.append(previous.getString());            }            if (tok.type == CHAR) {                int ch = tok.getChar();                if (ch >= 0x10000)                    buffer.append(REUtil.decomposeToSurrogates(ch));                else                    buffer.append((char)ch);            } else {                buffer.append(tok.getString());            }            ((StringToken)previous).string = new String(buffer);        }        int size() {            return this.children == null ? 0 : this.children.size();        }        Token getChild(int index) {            return (Token)this.children.elementAt(index);        }        public String toString(int options) {            String ret;            if (this.type == CONCAT) {                if (this.children.size() == 2) {                    Token ch = this.getChild(0);                    Token ch2 = this.getChild(1);                    if (ch2.type == CLOSURE && ch2.getChild(0) == ch) {                        ret = ch.toString(options)+"+";                    } else if (ch2.type == NONGREEDYCLOSURE && ch2.getChild(0) == ch) {                        ret = ch.toString(options)+"+?";                    } else                        ret = ch.toString(options)+ch2.toString(options);                } else {                    StringBuffer sb = new StringBuffer();                    for (int i = 0;  i < this.children.size();  i ++) {                        sb.append(((Token)this.children.elementAt(i)).toString(options));                    }                    ret = new String(sb);                }                return ret;            }            if (this.children.size() == 2 && this.getChild(1).type == EMPTY) {                ret = this.getChild(0).toString(options)+"?";            } else if (this.children.size() == 2                       && this.getChild(0).type == EMPTY) {                ret = this.getChild(1).toString(options)+"??";            } else {                StringBuffer sb = new StringBuffer();                sb.append(((Token)this.children.elementAt(0)).toString(options));                for (int i = 1;  i < this.children.size();  i ++) {                    sb.append((char)'|');                    sb.append(((Token)this.children.elementAt(i)).toString(options));                }                ret = new String(sb);            }            return ret;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -