Search in sources :

Example 1 with VariableDeclarationListTree

use of com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationListTree in project closure-compiler by google.

the class Parser method parseVariableStatement.

// 12.2 Variable Statement
private VariableStatementTree parseVariableStatement() {
    SourcePosition start = getTreeStartLocation();
    VariableDeclarationListTree declarations = parseVariableDeclarationList();
    eatPossiblyImplicitSemiColon();
    return new VariableStatementTree(getTreeLocation(start), declarations);
}
Also used : VariableDeclarationListTree(com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationListTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) VariableStatementTree(com.google.javascript.jscomp.parsing.parser.trees.VariableStatementTree)

Example 2 with VariableDeclarationListTree

use of com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationListTree in project closure-compiler by google.

the class Parser method parseForStatement.

// 12.6.3 The for Statement
// 12.6.4 The for-in Statement
// The for-of Statement
// The for-await-of Statement
private ParseTree parseForStatement() {
    SourcePosition start = getTreeStartLocation();
    eat(TokenType.FOR);
    boolean awaited = peekPredefinedString(AWAIT);
    if (awaited) {
        eatPredefinedString(AWAIT);
    }
    eat(TokenType.OPEN_PAREN);
    if (peekVariableDeclarationList()) {
        VariableDeclarationListTree variables = parseVariableDeclarationListNoIn();
        if (peek(TokenType.IN)) {
            if (awaited) {
                reportError("for-await-of is the only allowed asynchronous iteration");
            }
            // for-in: only one declaration allowed
            if (variables.declarations.size() > 1) {
                reportError("for-in statement may not have more than one variable declaration");
            }
            VariableDeclarationTree declaration = variables.declarations.get(0);
            if (declaration.initializer != null) {
                // http://esdiscuss.org/topic/initializer-expression-on-for-in-syntax-subject
                if (config.atLeast6) {
                    reportError("for-in statement may not have initializer");
                } else {
                    errorReporter.reportWarning(declaration.location.start, "for-in statement should not have initializer");
                }
            }
            return parseForInStatement(start, variables);
        } else if (peekPredefinedString(PredefinedName.OF)) {
            // for-of: only one declaration allowed
            if (variables.declarations.size() > 1) {
                if (awaited) {
                    reportError("for-await-of statement may not have more than one variable declaration");
                } else {
                    reportError("for-of statement may not have more than one variable declaration");
                }
            }
            // for-of: initializer is illegal
            VariableDeclarationTree declaration = variables.declarations.get(0);
            if (declaration.initializer != null) {
                if (awaited) {
                    reportError("for-await-of statement may not have initializer");
                } else {
                    reportError("for-of statement may not have initializer");
                }
            }
            if (awaited) {
                return parseForAwaitOfStatement(start, variables);
            } else {
                return parseForOfStatement(start, variables);
            }
        } else {
            // "Vanilla" for statement: const/destructuring must have initializer
            checkVanillaForInitializers(variables);
            return parseForStatement(start, variables);
        }
    }
    if (peek(TokenType.SEMI_COLON)) {
        return parseForStatement(start, null);
    }
    ParseTree initializer = parseExpressionNoIn();
    if (peek(TokenType.IN) || peek(TokenType.EQUAL) || peekPredefinedString(PredefinedName.OF)) {
        initializer = transformLeftHandSideExpression(initializer);
        if (!initializer.isValidAssignmentTarget()) {
            reportError("invalid assignment target");
        }
    }
    if (peek(TokenType.IN) || peekPredefinedString(PredefinedName.OF)) {
        if (initializer.type != ParseTreeType.BINARY_OPERATOR && initializer.type != ParseTreeType.COMMA_EXPRESSION) {
            if (peek(TokenType.IN)) {
                return parseForInStatement(start, initializer);
            } else {
                // for {await}? ( _ of _ )
                if (awaited) {
                    return parseForAwaitOfStatement(start, initializer);
                } else {
                    return parseForOfStatement(start, initializer);
                }
            }
        }
    }
    return parseForStatement(start, initializer);
}
Also used : VariableDeclarationListTree(com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationListTree) VariableDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 3 with VariableDeclarationListTree

use of com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationListTree in project closure-compiler by google.

the class Parser method parseVariableDeclarationList.

private VariableDeclarationListTree parseVariableDeclarationList(Expression expressionIn) {
    SourcePosition start = getTreeStartLocation();
    TokenType token = peekType();
    switch(token) {
        case CONST:
        case LET:
        case VAR:
            eat(token);
            break;
        default:
            reportError(peekToken(), "expected declaration");
            return null;
    }
    ImmutableList.Builder<VariableDeclarationTree> declarations = ImmutableList.builder();
    declarations.add(parseVariableDeclaration(token, expressionIn));
    while (peek(TokenType.COMMA)) {
        eat(TokenType.COMMA);
        declarations.add(parseVariableDeclaration(token, expressionIn));
    }
    return new VariableDeclarationListTree(getTreeLocation(start), token, declarations.build());
}
Also used : VariableDeclarationListTree(com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationListTree) VariableDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationTree) ImmutableList(com.google.common.collect.ImmutableList) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition)

Aggregations

VariableDeclarationListTree (com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationListTree)3 SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)3 VariableDeclarationTree (com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationTree)2 ImmutableList (com.google.common.collect.ImmutableList)1 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)1 VariableStatementTree (com.google.javascript.jscomp.parsing.parser.trees.VariableStatementTree)1