Search in sources :

Example 1 with ArrayPatternTree

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

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ArrayPatternTree (com.google.javascript.jscomp.parsing.parser.trees.ArrayPatternTree)1 MissingPrimaryExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.MissingPrimaryExpressionTree)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