Search in sources :

Example 1 with ArrayLiteral

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

the class Parser method arrayLiteral.

/**
 * May return an {@link ArrayLiteral} or {@link ArrayComprehension}.
 */
private AstNode arrayLiteral() throws IOException {
    if (currentToken != Token.LB)
        codeBug();
    int pos = ts.tokenBeg, end = ts.tokenEnd;
    List<AstNode> elements = new ArrayList<AstNode>();
    ArrayLiteral pn = new ArrayLiteral(pos);
    boolean after_lb_or_comma = true;
    int afterComma = -1;
    int skipCount = 0;
    for (; ; ) {
        int tt = peekToken();
        if (tt == Token.COMMA) {
            consumeToken();
            afterComma = ts.tokenEnd;
            if (!after_lb_or_comma) {
                after_lb_or_comma = true;
            } else {
                elements.add(new EmptyExpression(ts.tokenBeg, 1));
                skipCount++;
            }
        } else if (tt == Token.RB) {
            consumeToken();
            // for ([a,] in obj) is legal, but for ([a] in obj) is
            // not since we have both key and value supplied. The
            // trick is that [a,] and [a] are equivalent in other
            // array literal contexts. So we calculate a special
            // length value just for destructuring assignment.
            end = ts.tokenEnd;
            pn.setDestructuringLength(elements.size() + (after_lb_or_comma ? 1 : 0));
            pn.setSkipCount(skipCount);
            if (afterComma != -1)
                warnTrailingComma(pos, elements, afterComma);
            break;
        } else if (tt == Token.FOR && !after_lb_or_comma && elements.size() == 1) {
            return arrayComprehension(elements.get(0), pos);
        } else if (tt == Token.EOF) {
            reportError("msg.no.bracket.arg");
            break;
        } else {
            if (!after_lb_or_comma) {
                reportError("msg.no.bracket.arg");
            }
            elements.add(assignExpr());
            after_lb_or_comma = false;
            afterComma = -1;
        }
    }
    for (AstNode e : elements) {
        pn.addElement(e);
    }
    pn.setLength(end - pos);
    return pn;
}
Also used : ArrayList(java.util.ArrayList) ArrayLiteral(org.mozilla.javascript.ast.ArrayLiteral) AstNode(org.mozilla.javascript.ast.AstNode) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression)

Example 2 with ArrayLiteral

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

the class Parser method destructuringAssignmentHelper.

Node destructuringAssignmentHelper(int variableType, Node left, Node right, String tempName) {
    Scope result = createScopeNode(Token.LETEXPR, left.getLineno());
    result.addChildToFront(new Node(Token.LET, createName(Token.NAME, tempName, right)));
    try {
        pushScope(result);
        defineSymbol(Token.LET, tempName, true);
    } finally {
        popScope();
    }
    Node comma = new Node(Token.COMMA);
    result.addChildToBack(comma);
    List<String> destructuringNames = new ArrayList<String>();
    boolean empty = true;
    switch(left.getType()) {
        case Token.ARRAYLIT:
            empty = destructuringArray((ArrayLiteral) left, variableType, tempName, comma, destructuringNames);
            break;
        case Token.OBJECTLIT:
            empty = destructuringObject((ObjectLiteral) left, variableType, tempName, comma, destructuringNames);
            break;
        case Token.GETPROP:
        case Token.GETELEM:
            switch(variableType) {
                case Token.CONST:
                case Token.LET:
                case Token.VAR:
                    reportError("msg.bad.assign.left");
            }
            comma.addChildToBack(simpleAssignment(left, createName(tempName)));
            break;
        default:
            reportError("msg.bad.assign.left");
    }
    if (empty) {
        // Don't want a COMMA node with no children. Just add a zero.
        comma.addChildToBack(createNumber(0));
    }
    result.putProp(Node.DESTRUCTURING_NAMES, destructuringNames);
    return result;
}
Also used : ObjectLiteral(org.mozilla.javascript.ast.ObjectLiteral) Scope(org.mozilla.javascript.ast.Scope) 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) ArrayList(java.util.ArrayList) XmlString(org.mozilla.javascript.ast.XmlString) ArrayLiteral(org.mozilla.javascript.ast.ArrayLiteral)

Example 3 with ArrayLiteral

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

the class IRFactory method createForIn.

/**
 * Generate IR for a for..in loop.
 */
private Node createForIn(int declType, Node loop, Node lhs, Node obj, Node body, boolean isForEach, boolean isForOf) {
    int destructuring = -1;
    int destructuringLen = 0;
    Node lvalue;
    int type = lhs.getType();
    if (type == Token.VAR || type == Token.LET) {
        Node kid = lhs.getLastChild();
        int kidType = kid.getType();
        if (kidType == Token.ARRAYLIT || kidType == Token.OBJECTLIT) {
            type = destructuring = kidType;
            lvalue = kid;
            destructuringLen = 0;
            if (kid instanceof ArrayLiteral)
                destructuringLen = ((ArrayLiteral) kid).getDestructuringLength();
        } else if (kidType == Token.NAME) {
            lvalue = Node.newString(Token.NAME, kid.getString());
        } else {
            reportError("msg.bad.for.in.lhs");
            return null;
        }
    } else if (type == Token.ARRAYLIT || type == Token.OBJECTLIT) {
        destructuring = type;
        lvalue = lhs;
        destructuringLen = 0;
        if (lhs instanceof ArrayLiteral)
            destructuringLen = ((ArrayLiteral) lhs).getDestructuringLength();
    } else {
        lvalue = makeReference(lhs);
        if (lvalue == null) {
            reportError("msg.bad.for.in.lhs");
            return null;
        }
    }
    Node localBlock = new Node(Token.LOCAL_BLOCK);
    int initType = isForEach ? Token.ENUM_INIT_VALUES : isForOf ? Token.ENUM_INIT_VALUES_IN_ORDER : (destructuring != -1 ? Token.ENUM_INIT_ARRAY : Token.ENUM_INIT_KEYS);
    Node init = new Node(initType, obj);
    init.putProp(Node.LOCAL_BLOCK_PROP, localBlock);
    Node cond = new Node(Token.ENUM_NEXT);
    cond.putProp(Node.LOCAL_BLOCK_PROP, localBlock);
    Node id = new Node(Token.ENUM_ID);
    id.putProp(Node.LOCAL_BLOCK_PROP, localBlock);
    Node newBody = new Node(Token.BLOCK);
    Node assign;
    if (destructuring != -1) {
        assign = createDestructuringAssignment(declType, lvalue, id);
        if (!isForEach && !isForOf && (destructuring == Token.OBJECTLIT || destructuringLen != 2)) {
            // destructuring assignment is only allowed in for..each or
            // with an array type of length 2 (to hold key and value)
            reportError("msg.bad.for.in.destruct");
        }
    } else {
        assign = simpleAssignment(lvalue, id);
    }
    newBody.addChildToBack(new Node(Token.EXPR_VOID, assign));
    newBody.addChildToBack(body);
    loop = createLoop((Jump) loop, LOOP_WHILE, newBody, cond, null, null);
    loop.addChildToFront(init);
    if (type == Token.VAR || type == Token.LET)
        loop.addChildToFront(lhs);
    localBlock.addChildToBack(loop);
    return localBlock;
}
Also used : ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) ArrayLiteral(org.mozilla.javascript.ast.ArrayLiteral) Jump(org.mozilla.javascript.ast.Jump)

Example 4 with ArrayLiteral

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

the class Parser method arrowFunctionParams.

private void arrowFunctionParams(FunctionNode fnNode, AstNode params, Map<String, Node> destructuring, Set<String> paramNames) {
    if (params instanceof ArrayLiteral || params instanceof ObjectLiteral) {
        markDestructuring(params);
        fnNode.addParam(params);
        String pname = currentScriptOrFn.getNextTempName();
        defineSymbol(Token.LP, pname, false);
        destructuring.put(pname, params);
    } else if (params instanceof InfixExpression && params.getType() == Token.COMMA) {
        arrowFunctionParams(fnNode, ((InfixExpression) params).getLeft(), destructuring, paramNames);
        arrowFunctionParams(fnNode, ((InfixExpression) params).getRight(), destructuring, paramNames);
    } else if (params instanceof Name) {
        fnNode.addParam(params);
        String paramName = ((Name) params).getIdentifier();
        defineSymbol(Token.LP, paramName);
        if (this.inUseStrictDirective) {
            if ("eval".equals(paramName) || "arguments".equals(paramName)) {
                reportError("msg.bad.id.strict", paramName);
            }
            if (paramNames.contains(paramName))
                addError("msg.dup.param.strict", paramName);
            paramNames.add(paramName);
        }
    } else {
        reportError("msg.no.parm", params.getPosition(), params.getLength());
        fnNode.addParam(makeErrorNode());
    }
}
Also used : ObjectLiteral(org.mozilla.javascript.ast.ObjectLiteral) InfixExpression(org.mozilla.javascript.ast.InfixExpression) XmlString(org.mozilla.javascript.ast.XmlString) ArrayLiteral(org.mozilla.javascript.ast.ArrayLiteral) Name(org.mozilla.javascript.ast.Name)

Aggregations

ArrayLiteral (org.mozilla.javascript.ast.ArrayLiteral)4 AstNode (org.mozilla.javascript.ast.AstNode)3 ArrayList (java.util.ArrayList)2 FunctionNode (org.mozilla.javascript.ast.FunctionNode)2 LetNode (org.mozilla.javascript.ast.LetNode)2 ObjectLiteral (org.mozilla.javascript.ast.ObjectLiteral)2 ScriptNode (org.mozilla.javascript.ast.ScriptNode)2 XmlString (org.mozilla.javascript.ast.XmlString)2 EmptyExpression (org.mozilla.javascript.ast.EmptyExpression)1 ErrorNode (org.mozilla.javascript.ast.ErrorNode)1 InfixExpression (org.mozilla.javascript.ast.InfixExpression)1 Jump (org.mozilla.javascript.ast.Jump)1 Name (org.mozilla.javascript.ast.Name)1 Scope (org.mozilla.javascript.ast.Scope)1