Search in sources :

Example 6 with Scope

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

the class IRFactory method transformForLoop.

private Node transformForLoop(ForLoop loop) {
    decompiler.addToken(Token.FOR);
    decompiler.addToken(Token.LP);
    loop.setType(Token.LOOP);
    // XXX: Can't use pushScope/popScope here since 'createFor' may split
    // the scope
    Scope savedScope = currentScope;
    currentScope = loop;
    try {
        Node init = transform(loop.getInitializer());
        decompiler.addToken(Token.SEMI);
        Node test = transform(loop.getCondition());
        decompiler.addToken(Token.SEMI);
        Node incr = transform(loop.getIncrement());
        decompiler.addToken(Token.RP);
        decompiler.addEOL(Token.LC);
        Node body = transform(loop.getBody());
        decompiler.addEOL(Token.RC);
        return createFor(loop, init, test, incr, body);
    } finally {
        currentScope = savedScope;
    }
}
Also used : Scope(org.mozilla.javascript.ast.Scope) ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode)

Example 7 with Scope

use of org.mozilla.javascript.ast.Scope 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 8 with Scope

use of org.mozilla.javascript.ast.Scope 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 9 with Scope

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

the class Parser method pushScope.

void pushScope(Scope scope) {
    Scope parent = scope.getParentScope();
    // in which case we just need to set currentScope variable.
    if (parent != null) {
        if (parent != currentScope)
            codeBug();
    } else {
        currentScope.addChildScope(scope);
    }
    currentScope = scope;
}
Also used : Scope(org.mozilla.javascript.ast.Scope)

Example 10 with Scope

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

the class Parser method createScopeNode.

/**
 * Create a node that can be used to hold lexically scoped variable
 * definitions (via let declarations).
 *
 * @param token the token of the node to create
 * @param lineno line number of source
 * @return the created node
 */
protected Scope createScopeNode(int token, int lineno) {
    Scope scope = new Scope();
    scope.setType(token);
    scope.setLineno(lineno);
    return scope;
}
Also used : Scope(org.mozilla.javascript.ast.Scope)

Aggregations

Scope (org.mozilla.javascript.ast.Scope)18 FunctionNode (org.mozilla.javascript.ast.FunctionNode)13 ScriptNode (org.mozilla.javascript.ast.ScriptNode)13 AstNode (org.mozilla.javascript.ast.AstNode)8 LetNode (org.mozilla.javascript.ast.LetNode)7 ArrayList (java.util.ArrayList)4 Jump (org.mozilla.javascript.ast.Jump)4 XmlString (org.mozilla.javascript.ast.XmlString)4 ArrayComprehensionLoop (org.mozilla.javascript.ast.ArrayComprehensionLoop)3 GeneratorExpressionLoop (org.mozilla.javascript.ast.GeneratorExpressionLoop)3 Iterator (java.util.Iterator)2 List (java.util.List)2 DoLoop (org.mozilla.javascript.ast.DoLoop)2 ForInLoop (org.mozilla.javascript.ast.ForInLoop)2 ForLoop (org.mozilla.javascript.ast.ForLoop)2 Loop (org.mozilla.javascript.ast.Loop)2 Name (org.mozilla.javascript.ast.Name)2 WhileLoop (org.mozilla.javascript.ast.WhileLoop)2 ArrayLiteral (org.mozilla.javascript.ast.ArrayLiteral)1 EmptyExpression (org.mozilla.javascript.ast.EmptyExpression)1