Search in sources :

Example 1 with Symbol

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

the class Parser method defineSymbol.

void defineSymbol(int declType, String name, boolean ignoreNotInBlock) {
    if (name == null) {
        if (compilerEnv.isIdeMode()) {
            // be robust in IDE-mode
            return;
        } else {
            codeBug();
        }
    }
    Scope definingScope = currentScope.getDefiningScope(name);
    Symbol symbol = definingScope != null ? definingScope.getSymbol(name) : null;
    int symDeclType = symbol != null ? symbol.getDeclType() : -1;
    if (symbol != null && (symDeclType == Token.CONST || declType == Token.CONST || (definingScope == currentScope && symDeclType == Token.LET))) {
        addError(symDeclType == Token.CONST ? "msg.const.redecl" : symDeclType == Token.LET ? "msg.let.redecl" : symDeclType == Token.VAR ? "msg.var.redecl" : symDeclType == Token.FUNCTION ? "msg.fn.redecl" : "msg.parm.redecl", name);
        return;
    }
    switch(declType) {
        case Token.LET:
            if (!ignoreNotInBlock && ((currentScope.getType() == Token.IF) || currentScope instanceof Loop)) {
                addError("msg.let.decl.not.in.block");
                return;
            }
            currentScope.putSymbol(new Symbol(declType, name));
            return;
        case Token.VAR:
        case Token.CONST:
        case Token.FUNCTION:
            if (symbol != null) {
                if (symDeclType == Token.VAR)
                    addStrictWarning("msg.var.redecl", name);
                else if (symDeclType == Token.LP) {
                    addStrictWarning("msg.var.hides.arg", name);
                }
            } else {
                currentScriptOrFn.putSymbol(new Symbol(declType, name));
            }
            return;
        case Token.LP:
            if (symbol != null) {
                // must be duplicate parameter. Second parameter hides the
                // first, so go ahead and add the second parameter
                addWarning("msg.dup.parms", name);
            }
            currentScriptOrFn.putSymbol(new Symbol(declType, name));
            return;
        default:
            throw codeBug();
    }
}
Also used : WhileLoop(org.mozilla.javascript.ast.WhileLoop) ForLoop(org.mozilla.javascript.ast.ForLoop) GeneratorExpressionLoop(org.mozilla.javascript.ast.GeneratorExpressionLoop) ArrayComprehensionLoop(org.mozilla.javascript.ast.ArrayComprehensionLoop) Loop(org.mozilla.javascript.ast.Loop) ForInLoop(org.mozilla.javascript.ast.ForInLoop) DoLoop(org.mozilla.javascript.ast.DoLoop) Scope(org.mozilla.javascript.ast.Scope) Symbol(org.mozilla.javascript.ast.Symbol)

Example 2 with Symbol

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

Aggregations

Symbol (org.mozilla.javascript.ast.Symbol)2 ArrayComprehensionLoop (org.mozilla.javascript.ast.ArrayComprehensionLoop)1 AstNode (org.mozilla.javascript.ast.AstNode)1 DoLoop (org.mozilla.javascript.ast.DoLoop)1 ForInLoop (org.mozilla.javascript.ast.ForInLoop)1 ForLoop (org.mozilla.javascript.ast.ForLoop)1 FunctionNode (org.mozilla.javascript.ast.FunctionNode)1 GeneratorExpressionLoop (org.mozilla.javascript.ast.GeneratorExpressionLoop)1 LetNode (org.mozilla.javascript.ast.LetNode)1 Loop (org.mozilla.javascript.ast.Loop)1 Name (org.mozilla.javascript.ast.Name)1 Scope (org.mozilla.javascript.ast.Scope)1 ScriptNode (org.mozilla.javascript.ast.ScriptNode)1 WhileLoop (org.mozilla.javascript.ast.WhileLoop)1