Search in sources :

Example 1 with VariableInitializer

use of org.mozilla.javascript.ast.VariableInitializer in project st-js by st-js.

the class RhinoJavaScriptBuilder method variableDeclaration.

/**
 * {@inheritDoc}
 */
@Override
public AstNode variableDeclaration(boolean statement, CharSequence name, AstNode init) {
    VariableDeclaration vars = new VariableDeclaration();
    vars.setIsStatement(statement);
    VariableInitializer var = new VariableInitializer();
    var.setTarget(name(name));
    var.setInitializer(init);
    vars.addVariable(var);
    return vars;
}
Also used : VariableDeclaration(org.mozilla.javascript.ast.VariableDeclaration) VariableInitializer(org.mozilla.javascript.ast.VariableInitializer)

Example 2 with VariableInitializer

use of org.mozilla.javascript.ast.VariableInitializer in project st-js by st-js.

the class RhinoJavaScriptBuilder method variableDeclaration.

/**
 * {@inheritDoc}
 */
@Override
public AstNode variableDeclaration(boolean statement, Iterable<NameValue<AstNode>> vars) {
    VariableDeclaration varDecl = new VariableDeclaration();
    varDecl.setIsStatement(statement);
    for (NameValue<AstNode> v : vars) {
        VariableInitializer var = new VariableInitializer();
        var.setTarget(name(v.getName()));
        var.setInitializer(v.getValue());
        varDecl.addVariable(var);
    }
    return varDecl;
}
Also used : VariableDeclaration(org.mozilla.javascript.ast.VariableDeclaration) VariableInitializer(org.mozilla.javascript.ast.VariableInitializer) AstNode(org.mozilla.javascript.ast.AstNode)

Example 3 with VariableInitializer

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

the class IRFactory method transformVariableInitializers.

private Node transformVariableInitializers(VariableDeclaration node) {
    List<VariableInitializer> vars = node.getVariables();
    int size = vars.size(), i = 0;
    for (VariableInitializer var : vars) {
        AstNode target = var.getTarget();
        AstNode init = var.getInitializer();
        Node left = null;
        if (var.isDestructuring()) {
            // decompile but don't transform
            decompile(target);
            left = target;
        } else {
            left = transform(target);
        }
        Node right = null;
        if (init != null) {
            decompiler.addToken(Token.ASSIGN);
            right = transform(init);
        }
        if (var.isDestructuring()) {
            if (right == null) {
                // TODO:  should this ever happen?
                node.addChildToBack(left);
            } else {
                Node d = createDestructuringAssignment(node.getType(), left, right);
                node.addChildToBack(d);
            }
        } else {
            if (right != null) {
                left.addChildToBack(right);
            }
            node.addChildToBack(left);
        }
        if (i++ < size - 1) {
            decompiler.addToken(Token.COMMA);
        }
    }
    return node;
}
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) VariableInitializer(org.mozilla.javascript.ast.VariableInitializer) AstNode(org.mozilla.javascript.ast.AstNode)

Example 4 with VariableInitializer

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

the class Parser method variables.

/**
 * Parse a 'var' or 'const' statement, or a 'var' init list in a for
 * statement.
 * @param declType A token value: either VAR, CONST, or LET depending on
 * context.
 * @param pos the position where the node should start.  It's sometimes
 * the var/const/let keyword, and other times the beginning of the first
 * token in the first variable declaration.
 * @return the parsed variable list
 */
private VariableDeclaration variables(int declType, int pos, boolean isStatement) throws IOException {
    int end;
    VariableDeclaration pn = new VariableDeclaration(pos);
    pn.setType(declType);
    pn.setLineno(ts.lineno);
    Comment varjsdocNode = getAndResetJsDoc();
    if (varjsdocNode != null) {
        pn.setJsDocNode(varjsdocNode);
    }
    // var {b: s2, a: s1} = foo, x = 6, y, [s3, s4] = bar;
    for (; ; ) {
        AstNode destructuring = null;
        Name name = null;
        int tt = peekToken(), kidPos = ts.tokenBeg;
        end = ts.tokenEnd;
        if (tt == Token.LB || tt == Token.LC) {
            // Destructuring assignment, e.g., var [a,b] = ...
            destructuring = destructuringPrimaryExpr();
            end = getNodeEnd(destructuring);
            if (!(destructuring instanceof DestructuringForm))
                reportError("msg.bad.assign.left", kidPos, end - kidPos);
            markDestructuring(destructuring);
        } else {
            // Simple variable name
            mustMatchToken(Token.NAME, "msg.bad.var");
            name = createNameNode();
            name.setLineno(ts.getLineno());
            if (inUseStrictDirective) {
                String id = ts.getString();
                if ("eval".equals(id) || "arguments".equals(ts.getString())) {
                    reportError("msg.bad.id.strict", id);
                }
            }
            defineSymbol(declType, ts.getString(), inForInit);
        }
        int lineno = ts.lineno;
        Comment jsdocNode = getAndResetJsDoc();
        AstNode init = null;
        if (matchToken(Token.ASSIGN)) {
            init = assignExpr();
            end = getNodeEnd(init);
        }
        VariableInitializer vi = new VariableInitializer(kidPos, end - kidPos);
        if (destructuring != null) {
            if (init == null && !inForInit) {
                reportError("msg.destruct.assign.no.init");
            }
            vi.setTarget(destructuring);
        } else {
            vi.setTarget(name);
        }
        vi.setInitializer(init);
        vi.setType(declType);
        vi.setJsDocNode(jsdocNode);
        vi.setLineno(lineno);
        pn.addVariable(vi);
        if (!matchToken(Token.COMMA))
            break;
    }
    pn.setLength(end - pos);
    pn.setIsStatement(isStatement);
    return pn;
}
Also used : Comment(org.mozilla.javascript.ast.Comment) DestructuringForm(org.mozilla.javascript.ast.DestructuringForm) VariableDeclaration(org.mozilla.javascript.ast.VariableDeclaration) XmlString(org.mozilla.javascript.ast.XmlString) VariableInitializer(org.mozilla.javascript.ast.VariableInitializer) AstNode(org.mozilla.javascript.ast.AstNode) Name(org.mozilla.javascript.ast.Name)

Aggregations

VariableInitializer (org.mozilla.javascript.ast.VariableInitializer)4 AstNode (org.mozilla.javascript.ast.AstNode)3 VariableDeclaration (org.mozilla.javascript.ast.VariableDeclaration)3 Comment (org.mozilla.javascript.ast.Comment)1 DestructuringForm (org.mozilla.javascript.ast.DestructuringForm)1 FunctionNode (org.mozilla.javascript.ast.FunctionNode)1 LetNode (org.mozilla.javascript.ast.LetNode)1 Name (org.mozilla.javascript.ast.Name)1 ScriptNode (org.mozilla.javascript.ast.ScriptNode)1 XmlString (org.mozilla.javascript.ast.XmlString)1