Search in sources :

Example 1 with MissingPrimaryExpressionTree

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

the class Parser method parseCoverParenthesizedExpressionAndArrowParameterList.

// 12.2 Primary Expression
// CoverParenthesizedExpressionAndArrowParameterList ::=
// ( Expression )
// ( Expression, )
// ( )
// ( ... BindingIdentifier )
// ( Expression , ... BindingIdentifier )
private ParseTree parseCoverParenthesizedExpressionAndArrowParameterList() {
    if (peekType(1) == TokenType.FOR) {
        return parseGeneratorComprehension();
    }
    SourcePosition start = getTreeStartLocation();
    eat(TokenType.OPEN_PAREN);
    // Case ( )
    if (peek(TokenType.CLOSE_PAREN)) {
        eat(TokenType.CLOSE_PAREN);
        if (peek(TokenType.ARROW)) {
            return new FormalParameterListTree(getTreeLocation(start), ImmutableList.<ParseTree>of(), /* hasTrailingComma= */
            false, ImmutableList.<SourcePosition>of());
        } else {
            reportError("invalid parenthesized expression");
            return new MissingPrimaryExpressionTree(getTreeLocation(start));
        }
    }
    // Case ( ... BindingIdentifier )
    if (peek(TokenType.ELLIPSIS)) {
        ImmutableList<ParseTree> params = ImmutableList.of(parseParameter());
        eat(TokenType.CLOSE_PAREN);
        if (peek(TokenType.ARROW)) {
            return new FormalParameterListTree(getTreeLocation(start), params, /* hasTrailingComma= */
            false, ImmutableList.<SourcePosition>of());
        } else {
            reportError("invalid parenthesized expression");
            return new MissingPrimaryExpressionTree(getTreeLocation(start));
        }
    }
    // For either of the three remaining cases:
    // ( Expression )
    // ( Expression, )
    // ( Expression, ...BindingIdentifier )
    // we can parse as an expression.
    ParseTree result = parseExpression();
    // case.
    if (peek(TokenType.COMMA)) {
        if (peek(1, TokenType.CLOSE_PAREN)) {
            // Create the formal parameter list here so we can record
            // the trailing comma
            resetScanner(start);
            // parseFormalParameterList will take care of reporting errors.
            return parseFormalParameterList();
        } else {
            eat(TokenType.COMMA);
            // Since we already parsed as an expression, we will guaranteed reparse this expression
            // as an arrow function parameter list, but just leave it as a comma expression for now.
            result = new CommaExpressionTree(getTreeLocation(start), ImmutableList.of(result, parseParameter()));
        }
    }
    eat(TokenType.CLOSE_PAREN);
    return new ParenExpressionTree(getTreeLocation(start), result);
}
Also used : CommaExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.CommaExpressionTree) MissingPrimaryExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.MissingPrimaryExpressionTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParenExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.ParenExpressionTree) FormalParameterListTree(com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 2 with MissingPrimaryExpressionTree

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

the class Parser method parseMissingPrimaryExpression.

private ParseTree parseMissingPrimaryExpression() {
    SourcePosition start = getTreeStartLocation();
    nextToken();
    reportError("primary expression expected");
    return new MissingPrimaryExpressionTree(getTreeLocation(start));
}
Also used : MissingPrimaryExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.MissingPrimaryExpressionTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition)

Example 3 with MissingPrimaryExpressionTree

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

the class Parser method parseAssignment.

private ParseTree parseAssignment(Expression expressionIn) {
    if (peek(TokenType.YIELD) && inGeneratorContext()) {
        return parseYield(expressionIn);
    }
    SourcePosition start = getTreeStartLocation();
    ParseTree left = parseConditional(expressionIn);
    if (isStartOfAsyncArrowFunction(left)) {
        // re-evaluate as an async arrow function.
        resetScanner(left);
        return parseAsyncArrowFunction(expressionIn);
    }
    if (peek(TokenType.ARROW)) {
        return completeAssignmentExpressionParseAtArrow(left, expressionIn);
    }
    if (peekAssignmentOperator()) {
        left = transformLeftHandSideExpression(left);
        if (!left.isValidAssignmentTarget()) {
            reportError("invalid assignment target");
            return new MissingPrimaryExpressionTree(getTreeLocation(getTreeStartLocation()));
        }
        Token operator = nextToken();
        ParseTree right = parseAssignment(expressionIn);
        return new BinaryOperatorTree(getTreeLocation(start), left, operator, right);
    }
    return left;
}
Also used : MissingPrimaryExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.MissingPrimaryExpressionTree) 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 4 with MissingPrimaryExpressionTree

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

MissingPrimaryExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.MissingPrimaryExpressionTree)4 SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)4 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)3 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayPatternTree (com.google.javascript.jscomp.parsing.parser.trees.ArrayPatternTree)1 BinaryOperatorTree (com.google.javascript.jscomp.parsing.parser.trees.BinaryOperatorTree)1 CommaExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.CommaExpressionTree)1 FormalParameterListTree (com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree)1 NullTree (com.google.javascript.jscomp.parsing.parser.trees.NullTree)1 ParenExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.ParenExpressionTree)1