Search in sources :

Example 1 with ObjectLiteral

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

the class Parser method objectLiteral.

private ObjectLiteral objectLiteral() throws IOException {
    int pos = ts.tokenBeg, lineno = ts.lineno;
    int afterComma = -1;
    List<ObjectProperty> elems = new ArrayList<ObjectProperty>();
    Set<String> getterNames = null;
    Set<String> setterNames = null;
    if (this.inUseStrictDirective) {
        getterNames = new HashSet<String>();
        setterNames = new HashSet<String>();
    }
    Comment objJsdocNode = getAndResetJsDoc();
    commaLoop: for (; ; ) {
        String propertyName = null;
        int entryKind = PROP_ENTRY;
        int tt = peekToken();
        Comment jsdocNode = getAndResetJsDoc();
        if (tt == Token.RC) {
            if (afterComma != -1)
                warnTrailingComma(pos, elems, afterComma);
            break commaLoop;
        } else {
            AstNode pname = objliteralProperty();
            if (pname == null) {
                propertyName = null;
                reportError("msg.bad.prop");
            } else {
                propertyName = ts.getString();
                int ppos = ts.tokenBeg;
                consumeToken();
                // This code path needs to handle both destructuring object
                // literals like:
                // var {get, b} = {get: 1, b: 2};
                // and getters like:
                // var x = {get 1() { return 2; };
                // So we check a whitelist of tokens to check if we're at the
                // first case. (Because of keywords, the second case may be
                // many tokens.)
                int peeked = peekToken();
                if (peeked != Token.COMMA && peeked != Token.COLON && peeked != Token.RC) {
                    if (peeked == Token.LP) {
                        entryKind = METHOD_ENTRY;
                    } else if (pname.getType() == Token.NAME) {
                        if ("get".equals(propertyName)) {
                            entryKind = GET_ENTRY;
                        } else if ("set".equals(propertyName)) {
                            entryKind = SET_ENTRY;
                        }
                    }
                    if (entryKind == GET_ENTRY || entryKind == SET_ENTRY) {
                        pname = objliteralProperty();
                        if (pname == null) {
                            reportError("msg.bad.prop");
                        }
                        consumeToken();
                    }
                    if (pname == null) {
                        propertyName = null;
                    } else {
                        propertyName = ts.getString();
                        ObjectProperty objectProp = methodDefinition(ppos, pname, entryKind);
                        pname.setJsDocNode(jsdocNode);
                        elems.add(objectProp);
                    }
                } else {
                    pname.setJsDocNode(jsdocNode);
                    elems.add(plainProperty(pname, tt));
                }
            }
        }
        if (this.inUseStrictDirective && propertyName != null) {
            switch(entryKind) {
                case PROP_ENTRY:
                case METHOD_ENTRY:
                    if (getterNames.contains(propertyName) || setterNames.contains(propertyName)) {
                        addError("msg.dup.obj.lit.prop.strict", propertyName);
                    }
                    getterNames.add(propertyName);
                    setterNames.add(propertyName);
                    break;
                case GET_ENTRY:
                    if (getterNames.contains(propertyName)) {
                        addError("msg.dup.obj.lit.prop.strict", propertyName);
                    }
                    getterNames.add(propertyName);
                    break;
                case SET_ENTRY:
                    if (setterNames.contains(propertyName)) {
                        addError("msg.dup.obj.lit.prop.strict", propertyName);
                    }
                    setterNames.add(propertyName);
                    break;
            }
        }
        // Eat any dangling jsdoc in the property.
        getAndResetJsDoc();
        if (matchToken(Token.COMMA)) {
            afterComma = ts.tokenEnd;
        } else {
            break commaLoop;
        }
    }
    mustMatchToken(Token.RC, "msg.no.brace.prop");
    ObjectLiteral pn = new ObjectLiteral(pos, ts.tokenEnd - pos);
    if (objJsdocNode != null) {
        pn.setJsDocNode(objJsdocNode);
    }
    pn.setElements(elems);
    pn.setLineno(lineno);
    return pn;
}
Also used : ObjectProperty(org.mozilla.javascript.ast.ObjectProperty) Comment(org.mozilla.javascript.ast.Comment) ObjectLiteral(org.mozilla.javascript.ast.ObjectLiteral) ArrayList(java.util.ArrayList) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode)

Example 2 with ObjectLiteral

use of org.mozilla.javascript.ast.ObjectLiteral 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 ObjectLiteral

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

the class Parser method memberExpr.

/**
 * Parse a new-expression, or if next token isn't {@link Token#NEW},
 * a primary expression.
 * @param allowCallSyntax passed down to {@link #memberExprTail}
 */
private AstNode memberExpr(boolean allowCallSyntax) throws IOException {
    int tt = peekToken(), lineno = ts.lineno;
    AstNode pn;
    if (tt != Token.NEW) {
        pn = primaryExpr();
    } else {
        consumeToken();
        int pos = ts.tokenBeg;
        NewExpression nx = new NewExpression(pos);
        AstNode target = memberExpr(false);
        int end = getNodeEnd(target);
        nx.setTarget(target);
        int lp = -1;
        if (matchToken(Token.LP)) {
            lp = ts.tokenBeg;
            List<AstNode> args = argumentList();
            if (args != null && args.size() > ARGC_LIMIT)
                reportError("msg.too.many.constructor.args");
            int rp = ts.tokenBeg;
            end = ts.tokenEnd;
            if (args != null)
                nx.setArguments(args);
            nx.setParens(lp - pos, rp - pos);
        }
        // additional argument to the constructor.
        if (matchToken(Token.LC)) {
            ObjectLiteral initializer = objectLiteral();
            end = getNodeEnd(initializer);
            nx.setInitializer(initializer);
        }
        nx.setLength(end - pos);
        pn = nx;
    }
    pn.setLineno(lineno);
    AstNode tail = memberExprTail(allowCallSyntax, pn);
    return tail;
}
Also used : ObjectLiteral(org.mozilla.javascript.ast.ObjectLiteral) NewExpression(org.mozilla.javascript.ast.NewExpression) AstNode(org.mozilla.javascript.ast.AstNode)

Example 4 with ObjectLiteral

use of org.mozilla.javascript.ast.ObjectLiteral 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

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