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

📄 node.java

📁 java中比较著名的js引擎当属mozilla开源的rhino
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public void removeChild(Node child) {        Node prev = getChildBefore(child);        if (prev == null)            first = first.next;        else            prev.next = child.next;        if (child == last) last = prev;        child.next = null;    }    public void replaceChild(Node child, Node newChild) {        newChild.next = child.next;        if (child == first) {            first = newChild;        } else {            Node prev = getChildBefore(child);            prev.next = newChild;        }        if (child == last)            last = newChild;        child.next = null;    }    public void replaceChildAfter(Node prevChild, Node newChild) {        Node child = prevChild.next;        newChild.next = child.next;        prevChild.next = newChild;        if (child == last)            last = newChild;        child.next = null;    }    private static final String propToString(int propType)    {        if (Token.printTrees) {            // If Context.printTrees is false, the compiler            // can remove all these strings.            switch (propType) {                case FUNCTION_PROP:      return "function";                case LOCAL_PROP:         return "local";                case LOCAL_BLOCK_PROP:   return "local_block";                case REGEXP_PROP:        return "regexp";                case CASEARRAY_PROP:     return "casearray";                case TARGETBLOCK_PROP:   return "targetblock";                case VARIABLE_PROP:      return "variable";                case ISNUMBER_PROP:      return "isnumber";                case DIRECTCALL_PROP:    return "directcall";                case SPECIALCALL_PROP:   return "specialcall";                case SKIP_INDEXES_PROP:  return "skip_indexes";                case OBJECT_IDS_PROP:    return "object_ids_prop";                case INCRDECR_PROP:      return "incrdecr_prop";                case CATCH_SCOPE_PROP:   return "catch_scope_prop";                case LABEL_ID_PROP:      return "label_id_prop";                case MEMBER_TYPE_PROP:   return "member_type_prop";                case NAME_PROP:          return "name_prop";                default: Kit.codeBug();            }        }        return null;    }    private PropListItem lookupProperty(int propType)    {        PropListItem x = propListHead;        while (x != null && propType != x.type) {            x = x.next;        }        return x;    }    private PropListItem ensureProperty(int propType)    {        PropListItem item = lookupProperty(propType);        if (item == null) {            item = new PropListItem();            item.type = propType;            item.next = propListHead;            propListHead = item;        }        return item;    }    public void removeProp(int propType)    {        PropListItem x = propListHead;        if (x != null) {            PropListItem prev = null;            while (x.type != propType) {                prev = x;                x = x.next;                if (x == null) { return; }            }            if (prev == null) {                propListHead = x.next;            } else {                prev.next = x.next;            }        }    }    public Object getProp(int propType)    {        PropListItem item = lookupProperty(propType);        if (item == null) { return null; }        return item.objectValue;    }    public int getIntProp(int propType, int defaultValue)    {        PropListItem item = lookupProperty(propType);        if (item == null) { return defaultValue; }        return item.intValue;    }    public int getExistingIntProp(int propType)    {        PropListItem item = lookupProperty(propType);        if (item == null) { Kit.codeBug(); }        return item.intValue;    }    public void putProp(int propType, Object prop)    {        if (prop == null) {            removeProp(propType);        } else {            PropListItem item = ensureProperty(propType);            item.objectValue = prop;        }    }    public void putIntProp(int propType, int prop)    {        PropListItem item = ensureProperty(propType);        item.intValue = prop;    }    public int getLineno() {        return lineno;    }    /** Can only be called when <tt>getType() == Token.NUMBER</tt> */    public final double getDouble() {        return ((NumberNode)this).number;    }    public final void setDouble(double number) {        ((NumberNode)this).number = number;    }    /** Can only be called when node has String context. */    public final String getString() {        return ((StringNode)this).str;    }    /** Can only be called when node has String context. */    public final void setString(String s) {        if (s == null) Kit.codeBug();        ((StringNode)this).str = s;    }    public static Node newTarget()    {        return new Node(Token.TARGET);    }    public final int labelId()    {        if (type != Token.TARGET) Kit.codeBug();        return getIntProp(LABEL_ID_PROP, -1);    }    public void labelId(int labelId)    {        if (type != Token.TARGET) Kit.codeBug();        putIntProp(LABEL_ID_PROP, labelId);    }    public String toString()    {        if (Token.printTrees) {            StringBuffer sb = new StringBuffer();            toString(new ObjToIntMap(), sb);            return sb.toString();        }        return String.valueOf(type);    }    private void toString(ObjToIntMap printIds, StringBuffer sb)    {        if (Token.printTrees) {            sb.append(Token.name(type));            if (this instanceof StringNode) {                sb.append(' ');                sb.append(getString());            } else if (this instanceof ScriptOrFnNode) {                ScriptOrFnNode sof = (ScriptOrFnNode)this;                if (this instanceof FunctionNode) {                    FunctionNode fn = (FunctionNode)this;                    sb.append(' ');                    sb.append(fn.getFunctionName());                }                sb.append(" [source name: ");                sb.append(sof.getSourceName());                sb.append("] [encoded source length: ");                sb.append(sof.getEncodedSourceEnd()                          - sof.getEncodedSourceStart());                sb.append("] [base line: ");                sb.append(sof.getBaseLineno());                sb.append("] [end line: ");                sb.append(sof.getEndLineno());                sb.append(']');            } else if (this instanceof Jump) {                Jump jump = (Jump)this;                if (type == Token.BREAK || type == Token.CONTINUE) {                    sb.append(" [label: ");                    appendPrintId(jump.getJumpStatement(), printIds, sb);                    sb.append(']');                } else if (type == Token.TRY) {                    Node catchNode = jump.target;                    Node finallyTarget = jump.getFinally();                    if (catchNode != null) {                        sb.append(" [catch: ");                        appendPrintId(catchNode, printIds, sb);                        sb.append(']');                    }                    if (finallyTarget != null) {                        sb.append(" [finally: ");                        appendPrintId(finallyTarget, printIds, sb);                        sb.append(']');                    }                } else if (type == Token.LABEL || type == Token.LOOP                           || type == Token.SWITCH)                {                    sb.append(" [break: ");                    appendPrintId(jump.target, printIds, sb);                    sb.append(']');                    if (type == Token.LOOP) {                        sb.append(" [continue: ");                        appendPrintId(jump.getContinue(), printIds, sb);                        sb.append(']');                    }                } else {                    sb.append(" [target: ");                    appendPrintId(jump.target, printIds, sb);                    sb.append(']');                }            } else if (type == Token.NUMBER) {                sb.append(' ');                sb.append(getDouble());            } else if (type == Token.TARGET) {                sb.append(' ');                appendPrintId(this, printIds, sb);            }            if (lineno != -1) {                sb.append(' ');                sb.append(lineno);            }            for (PropListItem x = propListHead; x != null; x = x.next) {                int type = x.type;                sb.append(" [");                sb.append(propToString(type));                sb.append(": ");                String value;                switch (type) {                  case TARGETBLOCK_PROP : // can't add this as it recurses                    value = "target block property";                    break;                  case LOCAL_BLOCK_PROP :     // can't add this as it is dull                    value = "last local block";                    break;                  case ISNUMBER_PROP:                    switch (x.intValue) {                      case BOTH:                        value = "both";                        break;                      case RIGHT:                        value = "right";                        break;                      case LEFT:                        value = "left";                        break;                      default:                        throw Kit.codeBug();                    }                    break;                  case SPECIALCALL_PROP:                    switch (x.intValue) {                      case SPECIALCALL_EVAL:                        value = "eval";                        break;                      case SPECIALCALL_WITH:                        value = "with";                        break;                      default:                        // NON_SPECIALCALL should not be stored                        throw Kit.codeBug();                    }                    break;                  default :                    Object obj = x.objectValue;                    if (obj != null) {                        value = obj.toString();                    } else {                        value = String.valueOf(x.intValue);                    }                    break;                }                sb.append(value);                sb.append(']');            }        }    }    public String toStringTree(ScriptOrFnNode treeTop) {        if (Token.printTrees) {            StringBuffer sb = new StringBuffer();            toStringTreeHelper(treeTop, this, null, 0, sb);            return sb.toString();        }        return null;    }    private static void toStringTreeHelper(ScriptOrFnNode treeTop, Node n,                                           ObjToIntMap printIds,                                           int level, StringBuffer sb)    {        if (Token.printTrees) {            if (printIds == null) {                printIds = new ObjToIntMap();                generatePrintIds(treeTop, printIds);            }            for (int i = 0; i != level; ++i) {                sb.append("    ");            }            n.toString(printIds, sb);            sb.append('\n');            for (Node cursor = n.getFirstChild(); cursor != null;                 cursor = cursor.getNext())            {                if (cursor.getType() == Token.FUNCTION) {                    int fnIndex = cursor.getExistingIntProp(Node.FUNCTION_PROP);                    FunctionNode fn = treeTop.getFunctionNode(fnIndex);                    toStringTreeHelper(fn, fn, null, level + 1, sb);                } else {                    toStringTreeHelper(treeTop, cursor, printIds, level + 1, sb);                }            }        }    }    private static void generatePrintIds(Node n, ObjToIntMap map)    {        if (Token.printTrees) {            map.put(n, map.size());            for (Node cursor = n.getFirstChild(); cursor != null;                 cursor = cursor.getNext())            {                generatePrintIds(cursor, map);            }        }    }    private static void appendPrintId(Node n, ObjToIntMap printIds,                                      StringBuffer sb)    {        if (Token.printTrees) {            if (n != null) {                int id = printIds.get(n, -1);                sb.append('#');                if (id != -1) {                    sb.append(id + 1);                } else {                    sb.append("<not_available>");                }            }        }    }    int type;              // type of the node; Token.NAME for example    Node next;             // next sibling    private Node first;    // first element of a linked list of children    private Node last;     // last element of a linked list of children    private int lineno = -1;    // encapsulated int data; depends on type    /**     * Linked list of properties. Since vast majority of nodes would have     * no more then 2 properties, linked list saves memory and provides     * fast lookup. If this does not holds, propListHead can be replaced     * by UintMap.     */    private PropListItem propListHead;}

⌨️ 快捷键说明

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