Search in sources :

Example 11 with Name

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

the class IRFactory method initFunction.

private Node initFunction(FunctionNode fnNode, int functionIndex, Node statements, int functionType) {
    fnNode.setFunctionType(functionType);
    fnNode.addChildToBack(statements);
    int functionCount = fnNode.getFunctionCount();
    if (functionCount != 0) {
        // Functions containing other functions require activation objects
        fnNode.setRequiresActivation();
    }
    if (functionType == FunctionNode.FUNCTION_EXPRESSION) {
        Name name = fnNode.getFunctionName();
        if (name != null && name.length() != 0 && fnNode.getSymbol(name.getIdentifier()) == null) {
            // A function expression needs to have its name as a
            // variable (if it isn't already allocated as a variable).
            // See ECMA Ch. 13.  We add code to the beginning of the
            // function to initialize a local variable of the
            // function's name to the function value, but only if the
            // function doesn't already define a formal parameter, var,
            // or nested function with the same name.
            fnNode.putSymbol(new Symbol(Token.FUNCTION, name.getIdentifier()));
            Node setFn = new Node(Token.EXPR_VOID, new Node(Token.SETNAME, Node.newString(Token.BINDNAME, name.getIdentifier()), new Node(Token.THISFN)));
            statements.addChildrenToFront(setFn);
        }
    }
    // Add return to end if needed.
    Node lastStmt = statements.getLastChild();
    if (lastStmt == null || lastStmt.getType() != Token.RETURN) {
        statements.addChildToBack(new Node(Token.RETURN));
    }
    Node result = Node.newString(Token.FUNCTION, fnNode.getName());
    result.putIntProp(Node.FUNCTION_PROP, functionIndex);
    return result;
}
Also used : Symbol(org.mozilla.javascript.ast.Symbol) ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) Name(org.mozilla.javascript.ast.Name)

Example 12 with Name

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

the class Parser method function.

private FunctionNode function(int type) throws IOException {
    int syntheticType = type;
    // line number where source starts
    int baseLineno = ts.lineno;
    // start of "function" kwd
    int functionSourceStart = ts.tokenBeg;
    Name name = null;
    AstNode memberExprNode = null;
    if (matchToken(Token.NAME)) {
        name = createNameNode(true, Token.NAME);
        if (inUseStrictDirective) {
            String id = name.getIdentifier();
            if ("eval".equals(id) || "arguments".equals(id)) {
                reportError("msg.bad.id.strict", id);
            }
        }
        if (!matchToken(Token.LP)) {
            if (compilerEnv.isAllowMemberExprAsFunctionName()) {
                AstNode memberExprHead = name;
                name = null;
                memberExprNode = memberExprTail(false, memberExprHead);
            }
            mustMatchToken(Token.LP, "msg.no.paren.parms");
        }
    } else if (matchToken(Token.LP)) {
    // Anonymous function:  leave name as null
    } else {
        if (compilerEnv.isAllowMemberExprAsFunctionName()) {
            // Note that memberExpr can not start with '(' like
            // in function (1+2).toString(), because 'function (' already
            // processed as anonymous function
            memberExprNode = memberExpr(false);
        }
        mustMatchToken(Token.LP, "msg.no.paren.parms");
    }
    int lpPos = currentToken == Token.LP ? ts.tokenBeg : -1;
    if (memberExprNode != null) {
        syntheticType = FunctionNode.FUNCTION_EXPRESSION;
    }
    if (syntheticType != FunctionNode.FUNCTION_EXPRESSION && name != null && name.length() > 0) {
        // Function statements define a symbol in the enclosing scope
        defineSymbol(Token.FUNCTION, name.getIdentifier());
    }
    FunctionNode fnNode = new FunctionNode(functionSourceStart, name);
    fnNode.setFunctionType(type);
    if (lpPos != -1)
        fnNode.setLp(lpPos - functionSourceStart);
    fnNode.setJsDocNode(getAndResetJsDoc());
    PerFunctionVariables savedVars = new PerFunctionVariables(fnNode);
    try {
        parseFunctionParams(fnNode);
        fnNode.setBody(parseFunctionBody(type, fnNode));
        fnNode.setEncodedSourceBounds(functionSourceStart, ts.tokenEnd);
        fnNode.setLength(ts.tokenEnd - functionSourceStart);
        if (compilerEnv.isStrictMode() && !fnNode.getBody().hasConsistentReturnUsage()) {
            String msg = (name != null && name.length() > 0) ? "msg.no.return.value" : "msg.anon.no.return.value";
            addStrictWarning(msg, name == null ? "" : name.getIdentifier());
        }
    } finally {
        savedVars.restore();
    }
    if (memberExprNode != null) {
        // TODO(stevey): fix missing functionality
        Kit.codeBug();
        // rewrite later
        fnNode.setMemberExprNode(memberExprNode);
    /* old code:
            if (memberExprNode != null) {
                pn = nf.createAssignment(Token.ASSIGN, memberExprNode, pn);
                if (functionType != FunctionNode.FUNCTION_EXPRESSION) {
                    // XXX check JScript behavior: should it be createExprStatement?
                    pn = nf.createExprStatementNoReturn(pn, baseLineno);
                }
            }
            */
    }
    fnNode.setSourceName(sourceURI);
    fnNode.setBaseLineno(baseLineno);
    fnNode.setEndLineno(ts.lineno);
    // at the function boundary when checking for redeclarations.
    if (compilerEnv.isIdeMode()) {
        fnNode.setParentScope(currentScope);
    }
    return fnNode;
}
Also used : FunctionNode(org.mozilla.javascript.ast.FunctionNode) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode) Name(org.mozilla.javascript.ast.Name)

Example 13 with Name

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

the class Parser method propertyName.

/**
 * Check if :: follows name in which case it becomes a qualified name.
 *
 * @param atPos a natural number if we just read an '@' token, else -1
 *
 * @param s the name or string that was matched (an identifier, "throw" or
 * "*").
 *
 * @param memberTypeFlags flags tracking whether we're a '.' or '..' child
 *
 * @return an XmlRef node if it's an attribute access, a child of a
 * '..' operator, or the name is followed by ::.  For a plain name,
 * returns a Name node.  Returns an ErrorNode for malformed XML
 * expressions.  (For now - might change to return a partial XmlRef.)
 */
private AstNode propertyName(int atPos, String s, int memberTypeFlags) throws IOException {
    int pos = atPos != -1 ? atPos : ts.tokenBeg, lineno = ts.lineno;
    int colonPos = -1;
    Name name = createNameNode(true, currentToken);
    Name ns = null;
    if (matchToken(Token.COLONCOLON)) {
        ns = name;
        colonPos = ts.tokenBeg;
        switch(nextToken()) {
            // handles name::name
            case Token.NAME:
                name = createNameNode();
                break;
            // handles name::*
            case Token.MUL:
                saveNameTokenData(ts.tokenBeg, "*", ts.lineno);
                name = createNameNode(false, -1);
                break;
            // handles name::[expr] or *::[expr]
            case Token.LB:
                return xmlElemRef(atPos, ns, colonPos);
            default:
                reportError("msg.no.name.after.coloncolon");
                return makeErrorNode();
        }
    }
    if (ns == null && memberTypeFlags == 0 && atPos == -1) {
        return name;
    }
    XmlPropRef ref = new XmlPropRef(pos, getNodeEnd(name) - pos);
    ref.setAtPos(atPos);
    ref.setNamespace(ns);
    ref.setColonPos(colonPos);
    ref.setPropName(name);
    ref.setLineno(lineno);
    return ref;
}
Also used : XmlPropRef(org.mozilla.javascript.ast.XmlPropRef) Name(org.mozilla.javascript.ast.Name)

Example 14 with Name

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

the class Parser method plainProperty.

private ObjectProperty plainProperty(AstNode property, int ptt) throws IOException {
    // Support, e.g., |var {x, y} = o| as destructuring shorthand
    // for |var {x: x, y: y} = o|, as implemented in spidermonkey JS 1.8.
    int tt = peekToken();
    if ((tt == Token.COMMA || tt == Token.RC) && ptt == Token.NAME && compilerEnv.getLanguageVersion() >= Context.VERSION_1_8) {
        if (!inDestructuringAssignment) {
            reportError("msg.bad.object.init");
        }
        AstNode nn = new Name(property.getPosition(), property.getString());
        ObjectProperty pn = new ObjectProperty();
        pn.putProp(Node.DESTRUCTURING_SHORTHAND, Boolean.TRUE);
        pn.setLeftAndRight(property, nn);
        return pn;
    }
    mustMatchToken(Token.COLON, "msg.no.colon.prop");
    ObjectProperty pn = new ObjectProperty();
    pn.setOperatorPosition(ts.tokenBeg);
    pn.setLeftAndRight(property, assignExpr());
    return pn;
}
Also used : ObjectProperty(org.mozilla.javascript.ast.ObjectProperty) AstNode(org.mozilla.javascript.ast.AstNode) Name(org.mozilla.javascript.ast.Name)

Example 15 with Name

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

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