Search in sources :

Example 21 with Name

use of org.mozilla.javascript.ast.Name in project HL4A by HL4A.

the class Parser method destructuringObject.

boolean destructuringObject(ObjectLiteral node, int variableType, String tempName, Node parent, List<String> destructuringNames) {
    boolean empty = true;
    int setOp = variableType == Token.CONST ? Token.SETCONST : Token.SETNAME;
    for (ObjectProperty prop : node.getElements()) {
        int lineno = 0;
        // tokenStream isn't set.  Deal with it.
        if (ts != null) {
            lineno = ts.lineno;
        }
        AstNode id = prop.getLeft();
        Node rightElem = null;
        if (id instanceof Name) {
            Node s = Node.newString(((Name) id).getIdentifier());
            rightElem = new Node(Token.GETPROP, createName(tempName), s);
        } else if (id instanceof StringLiteral) {
            Node s = Node.newString(((StringLiteral) id).getValue());
            rightElem = new Node(Token.GETPROP, createName(tempName), s);
        } else if (id instanceof NumberLiteral) {
            Node s = createNumber((int) ((NumberLiteral) id).getNumber());
            rightElem = new Node(Token.GETELEM, createName(tempName), s);
        } else {
            throw codeBug();
        }
        rightElem.setLineno(lineno);
        AstNode value = prop.getRight();
        if (value.getType() == Token.NAME) {
            String name = ((Name) value).getIdentifier();
            parent.addChildToBack(new Node(setOp, createName(Token.BINDNAME, name, null), rightElem));
            if (variableType != -1) {
                defineSymbol(variableType, name, true);
                destructuringNames.add(name);
            }
        } else {
            parent.addChildToBack(destructuringAssignmentHelper(variableType, value, rightElem, currentScriptOrFn.getNextTempName()));
        }
        empty = false;
    }
    return empty;
}
Also used : ObjectProperty(org.mozilla.javascript.ast.ObjectProperty) StringLiteral(org.mozilla.javascript.ast.StringLiteral) ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) ErrorNode(org.mozilla.javascript.ast.ErrorNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode) NumberLiteral(org.mozilla.javascript.ast.NumberLiteral) Name(org.mozilla.javascript.ast.Name)

Example 22 with Name

use of org.mozilla.javascript.ast.Name in project HL4A by HL4A.

the class Node method toString.

private void toString(ObjToIntMap printIds, StringBuilder sb) {
    if (Token.printTrees) {
        sb.append(Token.name(type));
        if (this instanceof Name) {
            sb.append(' ');
            sb.append(getString());
            Scope scope = getScope();
            if (scope != null) {
                sb.append("[scope: ");
                appendPrintId(scope, printIds, sb);
                sb.append("]");
            }
        } else if (this instanceof Scope) {
            if (this instanceof ScriptNode) {
                ScriptNode sof = (ScriptNode) this;
                if (this instanceof FunctionNode) {
                    FunctionNode fn = (FunctionNode) this;
                    sb.append(' ');
                    sb.append(fn.getName());
                }
                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(']');
            }
            if (((Scope) this).getSymbolTable() != null) {
                sb.append(" [scope ");
                appendPrintId(this, printIds, sb);
                sb.append(": ");
                Iterator<String> iter = ((Scope) this).getSymbolTable().keySet().iterator();
                while (iter.hasNext()) {
                    sb.append(iter.next());
                    sb.append(" ");
                }
                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 // can't add this as it recurses
                TARGETBLOCK_PROP:
                    value = "target block property";
                    break;
                case // can't add this as it is dull
                LOCAL_BLOCK_PROP:
                    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;
                case OBJECT_IDS_PROP:
                    {
                        Object[] a = (Object[]) x.objectValue;
                        value = "[";
                        for (int i = 0; i < a.length; i++) {
                            value += a[i].toString();
                            if (i + 1 < a.length)
                                value += ", ";
                        }
                        value += "]";
                        break;
                    }
                default:
                    Object obj = x.objectValue;
                    if (obj != null) {
                        value = obj.toString();
                    } else {
                        value = String.valueOf(x.intValue);
                    }
                    break;
            }
            sb.append(value);
            sb.append(']');
        }
    }
}
Also used : Scope(org.mozilla.javascript.ast.Scope) ScriptNode(org.mozilla.javascript.ast.ScriptNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) Iterator(java.util.Iterator) ScriptNode(org.mozilla.javascript.ast.ScriptNode) Jump(org.mozilla.javascript.ast.Jump) Name(org.mozilla.javascript.ast.Name)

Example 23 with Name

use of org.mozilla.javascript.ast.Name in project HL4A by HL4A.

the class Node method newString.

public static Node newString(int type, String str) {
    Name name = new Name();
    name.setIdentifier(str);
    name.setType(type);
    return name;
}
Also used : Name(org.mozilla.javascript.ast.Name)

Aggregations

Name (org.mozilla.javascript.ast.Name)23 AstNode (org.mozilla.javascript.ast.AstNode)11 XmlString (org.mozilla.javascript.ast.XmlString)11 FunctionNode (org.mozilla.javascript.ast.FunctionNode)10 ScriptNode (org.mozilla.javascript.ast.ScriptNode)7 LetNode (org.mozilla.javascript.ast.LetNode)5 Comment (org.mozilla.javascript.ast.Comment)3 ErrorNode (org.mozilla.javascript.ast.ErrorNode)3 Jump (org.mozilla.javascript.ast.Jump)3 ObjectProperty (org.mozilla.javascript.ast.ObjectProperty)3 Iterator (java.util.Iterator)2 InfixExpression (org.mozilla.javascript.ast.InfixExpression)2 LabeledStatement (org.mozilla.javascript.ast.LabeledStatement)2 NumberLiteral (org.mozilla.javascript.ast.NumberLiteral)2 PropertyGet (org.mozilla.javascript.ast.PropertyGet)2 Scope (org.mozilla.javascript.ast.Scope)2 StringLiteral (org.mozilla.javascript.ast.StringLiteral)2 XmlPropRef (org.mozilla.javascript.ast.XmlPropRef)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1