Search in sources :

Example 1 with NullTree

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

the class Parser method parseArrayLiteral.

// 11.1.4 Array Literal Expression
private ParseTree parseArrayLiteral() {
    // ArrayLiteral :
    // [ Elisionopt ]
    // [ ElementList ]
    // [ ElementList , Elisionopt ]
    // 
    // ElementList :
    // Elisionopt AssignmentOrSpreadExpression
    // ElementList , Elisionopt AssignmentOrSpreadExpression
    // 
    // Elision :
    // ,
    // Elision ,
    SourcePosition start = getTreeStartLocation();
    ImmutableList.Builder<ParseTree> elements = ImmutableList.builder();
    eat(TokenType.OPEN_SQUARE);
    Token trailingCommaToken = null;
    while (peek(TokenType.COMMA) || peek(TokenType.ELLIPSIS) || peekAssignmentExpression()) {
        trailingCommaToken = null;
        if (peek(TokenType.COMMA)) {
            SourcePosition commaStart = getTreeStartLocation();
            trailingCommaToken = eat(TokenType.COMMA);
            // Consider the empty element to start & end immediately before the comma token.
            elements.add(new NullTree(new SourceRange(commaStart, commaStart)));
        } else {
            if (peek(TokenType.ELLIPSIS)) {
                recordFeatureUsed(Feature.SPREAD_EXPRESSIONS);
                elements.add(parseIterSpread());
            } else {
                elements.add(parseAssignmentExpression());
            }
            if (!peek(TokenType.CLOSE_SQUARE)) {
                trailingCommaToken = eat(TokenType.COMMA);
            }
        }
    }
    eat(TokenType.CLOSE_SQUARE);
    maybeReportTrailingComma(trailingCommaToken);
    return new ArrayLiteralExpressionTree(getTreeLocation(start), elements.build(), trailingCommaToken != null);
}
Also used : ArrayLiteralExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.ArrayLiteralExpressionTree) ImmutableList(com.google.common.collect.ImmutableList) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) 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 2 with NullTree

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

the class Parser method parseArrayPattern.

// Pattern ::= ... | "[" Element? ("," Element?)* "]"
private ParseTree parseArrayPattern(PatternKind kind) {
    SourcePosition start = getTreeStartLocation();
    ImmutableList.Builder<ParseTree> elements = ImmutableList.builder();
    eat(TokenType.OPEN_SQUARE);
    while (peek(TokenType.COMMA) || peekArrayPatternElement()) {
        if (peek(TokenType.COMMA)) {
            SourcePosition nullStart = getTreeStartLocation();
            eat(TokenType.COMMA);
            elements.add(new NullTree(getTreeLocation(nullStart)));
        } else {
            elements.add(parsePatternAssignmentTarget(kind));
            if (peek(TokenType.COMMA)) {
                // Consume the comma separator
                eat(TokenType.COMMA);
            } else {
                // Otherwise we must be done
                break;
            }
        }
    }
    if (peek(TokenType.ELLIPSIS)) {
        recordFeatureUsed(Feature.ARRAY_PATTERN_REST);
        elements.add(parseIterRest(kind));
    }
    if (eat(TokenType.CLOSE_SQUARE) == null) {
        // source has syntactic errors.
        return new MissingPrimaryExpressionTree(getTreeLocation(getTreeStartLocation()));
    }
    return new ArrayPatternTree(getTreeLocation(start), elements.build());
}
Also used : MissingPrimaryExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.MissingPrimaryExpressionTree) ImmutableList(com.google.common.collect.ImmutableList) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) NullTree(com.google.javascript.jscomp.parsing.parser.trees.NullTree) ArrayPatternTree(com.google.javascript.jscomp.parsing.parser.trees.ArrayPatternTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 3 with NullTree

use of com.google.javascript.jscomp.parsing.parser.trees.NullTree 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)

Aggregations

NullTree (com.google.javascript.jscomp.parsing.parser.trees.NullTree)3 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)3 ImmutableList (com.google.common.collect.ImmutableList)2 SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)2 SourceRange (com.google.javascript.jscomp.parsing.parser.util.SourceRange)2 ArrayLiteralExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.ArrayLiteralExpressionTree)1 ArrayPatternTree (com.google.javascript.jscomp.parsing.parser.trees.ArrayPatternTree)1 ForStatementTree (com.google.javascript.jscomp.parsing.parser.trees.ForStatementTree)1 MissingPrimaryExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.MissingPrimaryExpressionTree)1