Search in sources :

Example 1 with VariableDeclaration

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

the class Parser method let.

// have to pass in 'let' kwd position to compute kid offsets properly
private AstNode let(boolean isStatement, int pos) throws IOException {
    LetNode pn = new LetNode(pos);
    pn.setLineno(ts.lineno);
    if (mustMatchToken(Token.LP, "msg.no.paren.after.let"))
        pn.setLp(ts.tokenBeg - pos);
    pushScope(pn);
    try {
        VariableDeclaration vars = variables(Token.LET, ts.tokenBeg, isStatement);
        pn.setVariables(vars);
        if (mustMatchToken(Token.RP, "msg.no.paren.let")) {
            pn.setRp(ts.tokenBeg - pos);
        }
        if (isStatement && peekToken() == Token.LC) {
            // let statement
            consumeToken();
            // position stmt at LC
            int beg = ts.tokenBeg;
            AstNode stmt = statements();
            mustMatchToken(Token.RC, "msg.no.curly.let");
            stmt.setLength(ts.tokenEnd - beg);
            pn.setLength(ts.tokenEnd - pos);
            pn.setBody(stmt);
            pn.setType(Token.LET);
        } else {
            // let expression
            AstNode expr = expr();
            pn.setLength(getNodeEnd(expr) - pos);
            pn.setBody(expr);
            if (isStatement) {
                // let expression in statement context
                ExpressionStatement es = new ExpressionStatement(pn, !insideFunction());
                es.setLineno(pn.getLineno());
                return es;
            }
        }
    } finally {
        popScope();
    }
    return pn;
}
Also used : LetNode(org.mozilla.javascript.ast.LetNode) ExpressionStatement(org.mozilla.javascript.ast.ExpressionStatement) VariableDeclaration(org.mozilla.javascript.ast.VariableDeclaration) AstNode(org.mozilla.javascript.ast.AstNode)

Example 2 with VariableDeclaration

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

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

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

the class IRFactory method transformForInLoop.

private Node transformForInLoop(ForInLoop loop) {
    decompiler.addToken(Token.FOR);
    if (loop.isForEach())
        decompiler.addName("each ");
    decompiler.addToken(Token.LP);
    loop.setType(Token.LOOP);
    pushScope(loop);
    try {
        int declType = -1;
        AstNode iter = loop.getIterator();
        if (iter instanceof VariableDeclaration) {
            declType = ((VariableDeclaration) iter).getType();
        }
        Node lhs = transform(iter);
        if (loop.isForOf()) {
            decompiler.addName("of ");
        } else {
            decompiler.addToken(Token.IN);
        }
        Node obj = transform(loop.getIteratedObject());
        decompiler.addToken(Token.RP);
        decompiler.addEOL(Token.LC);
        Node body = transform(loop.getBody());
        decompiler.addEOL(Token.RC);
        return createForIn(declType, loop, lhs, obj, body, loop.isForEach(), loop.isForOf());
    } finally {
        popScope();
    }
}
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) VariableDeclaration(org.mozilla.javascript.ast.VariableDeclaration) AstNode(org.mozilla.javascript.ast.AstNode)

Example 5 with VariableDeclaration

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

VariableDeclaration (org.mozilla.javascript.ast.VariableDeclaration)7 AstNode (org.mozilla.javascript.ast.AstNode)6 VariableInitializer (org.mozilla.javascript.ast.VariableInitializer)3 ExpressionStatement (org.mozilla.javascript.ast.ExpressionStatement)2 LetNode (org.mozilla.javascript.ast.LetNode)2 ArrayComprehensionLoop (org.mozilla.javascript.ast.ArrayComprehensionLoop)1 Comment (org.mozilla.javascript.ast.Comment)1 DestructuringForm (org.mozilla.javascript.ast.DestructuringForm)1 DoLoop (org.mozilla.javascript.ast.DoLoop)1 EmptyExpression (org.mozilla.javascript.ast.EmptyExpression)1 EmptyStatement (org.mozilla.javascript.ast.EmptyStatement)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 KeywordLiteral (org.mozilla.javascript.ast.KeywordLiteral)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