Search in sources :

Example 1 with ArrayLiteralExpressionTree

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

Aggregations

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