Search in sources :

Example 1 with DestructuringForm

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

AstNode (org.mozilla.javascript.ast.AstNode)1 Comment (org.mozilla.javascript.ast.Comment)1 DestructuringForm (org.mozilla.javascript.ast.DestructuringForm)1 Name (org.mozilla.javascript.ast.Name)1 VariableDeclaration (org.mozilla.javascript.ast.VariableDeclaration)1 VariableInitializer (org.mozilla.javascript.ast.VariableInitializer)1 XmlString (org.mozilla.javascript.ast.XmlString)1