Search in sources :

Example 91 with ParseTree

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

the class Parser method parseShortCircuit.

private ParseTree parseShortCircuit(Expression expressionIn) {
    SourcePosition start = getTreeStartLocation();
    ParseTree left = parseLogicalOR(expressionIn);
    if (peek(TokenType.QUESTION_QUESTION)) {
        if (left.type == ParseTreeType.BINARY_OPERATOR) {
            BinaryOperatorTree binaryTree = left.asBinaryOperator();
            if (binaryTree.operator.type == TokenType.AND || binaryTree.operator.type == TokenType.OR) {
                reportError("Logical OR and logical AND require parentheses when used with '??'");
            }
        }
        return parseNullishCoalesce(expressionIn, left, start);
    } else {
        return left;
    }
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) BinaryOperatorTree(com.google.javascript.jscomp.parsing.parser.trees.BinaryOperatorTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 92 with ParseTree

use of com.google.javascript.jscomp.parsing.parser.trees.ParseTree 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 93 with ParseTree

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

the class Parser method parseForStatement.

// 12.6.3 The for Statement
private ParseTree parseForStatement(SourcePosition start, ParseTree initializer) {
    if (initializer == null) {
        initializer = new NullTree(new SourceRange(getTreeEndLocation(), getTreeStartLocation()));
    }
    eat(TokenType.SEMI_COLON);
    ParseTree condition;
    if (!peek(TokenType.SEMI_COLON)) {
        condition = parseExpression();
    } else {
        condition = new NullTree(new SourceRange(getTreeEndLocation(), getTreeStartLocation()));
    }
    eat(TokenType.SEMI_COLON);
    ParseTree increment;
    if (!peek(TokenType.CLOSE_PAREN)) {
        increment = parseExpression();
    } else {
        increment = new NullTree(new SourceRange(getTreeEndLocation(), getTreeStartLocation()));
    }
    eat(TokenType.CLOSE_PAREN);
    ParseTree body = parseStatement();
    return new ForStatementTree(getTreeLocation(start), initializer, condition, increment, body);
}
Also used : ForStatementTree(com.google.javascript.jscomp.parsing.parser.trees.ForStatementTree) NullTree(com.google.javascript.jscomp.parsing.parser.trees.NullTree) SourceRange(com.google.javascript.jscomp.parsing.parser.util.SourceRange) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 94 with ParseTree

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

the class Parser method parseClass.

private ParseTree parseClass(boolean isExpression) {
    SourcePosition start = getTreeStartLocation();
    eat(TokenType.CLASS);
    IdentifierToken name = null;
    if (!isExpression || peekId()) {
        name = eatId();
    }
    ParseTree superClass = null;
    if (peek(TokenType.EXTENDS)) {
        eat(TokenType.EXTENDS);
        superClass = parseExpression();
    }
    eat(TokenType.OPEN_CURLY);
    ImmutableList<ParseTree> elements = parseClassElements();
    eat(TokenType.CLOSE_CURLY);
    return new ClassDeclarationTree(getTreeLocation(start), name, superClass, elements);
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree) ClassDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.ClassDeclarationTree)

Example 95 with ParseTree

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

the class Parser method parseForAwaitOfStatement.

private ParseTree parseForAwaitOfStatement(SourcePosition start, ParseTree initializer) {
    eatPredefinedString(PredefinedName.OF);
    ParseTree collection = parseExpression();
    eat(TokenType.CLOSE_PAREN);
    ParseTree body = parseStatement();
    return new ForAwaitOfStatementTree(getTreeLocation(start), initializer, collection, body);
}
Also used : ForAwaitOfStatementTree(com.google.javascript.jscomp.parsing.parser.trees.ForAwaitOfStatementTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Aggregations

ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)100 SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)81 ImmutableList (com.google.common.collect.ImmutableList)15 BinaryOperatorTree (com.google.javascript.jscomp.parsing.parser.trees.BinaryOperatorTree)14 FormalParameterListTree (com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree)9 FunctionDeclarationTree (com.google.javascript.jscomp.parsing.parser.trees.FunctionDeclarationTree)6 BlockTree (com.google.javascript.jscomp.parsing.parser.trees.BlockTree)5 DefaultParameterTree (com.google.javascript.jscomp.parsing.parser.trees.DefaultParameterTree)5 SourceRange (com.google.javascript.jscomp.parsing.parser.util.SourceRange)5 ArgumentListTree (com.google.javascript.jscomp.parsing.parser.trees.ArgumentListTree)4 ComputedPropertyMethodTree (com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMethodTree)4 OptionalMemberExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.OptionalMemberExpressionTree)4 OptionalMemberLookupExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.OptionalMemberLookupExpressionTree)4 ComputedPropertyMemberVariableTree (com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMemberVariableTree)3 GenericTypeListTree (com.google.javascript.jscomp.parsing.parser.trees.GenericTypeListTree)3 IdentifierExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.IdentifierExpressionTree)3 NullTree (com.google.javascript.jscomp.parsing.parser.trees.NullTree)3 OptChainCallExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.OptChainCallExpressionTree)3 CommaExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.CommaExpressionTree)2 DoWhileStatementTree (com.google.javascript.jscomp.parsing.parser.trees.DoWhileStatementTree)2