Search in sources :

Example 21 with ParseTree

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

the class Parser method parseGetAccessor.

private ParseTree parseGetAccessor(PartialClassElement partial) {
    eatPredefinedString(PredefinedName.GET);
    if (peekPropertyName(0)) {
        Token propertyName = eatObjectLiteralPropertyName();
        eat(TokenType.OPEN_PAREN);
        eat(TokenType.CLOSE_PAREN);
        BlockTree body = parseFunctionBody();
        recordFeatureUsed(Feature.GETTER);
        return new GetAccessorTree(getTreeLocation(partial.start), propertyName, partial.isStatic, body);
    } else {
        ParseTree property = parseComputedPropertyName();
        eat(TokenType.OPEN_PAREN);
        eat(TokenType.CLOSE_PAREN);
        BlockTree body = parseFunctionBody();
        recordFeatureUsed(Feature.GETTER);
        return new ComputedPropertyGetterTree(getTreeLocation(partial.start), property, partial.isStatic, body);
    }
}
Also used : GetAccessorTree(com.google.javascript.jscomp.parsing.parser.trees.GetAccessorTree) ComputedPropertyGetterTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyGetterTree) BlockTree(com.google.javascript.jscomp.parsing.parser.trees.BlockTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 22 with ParseTree

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

the class Parser method maybeParseOptionalExpression.

/**
 * Tries to parse the expression as an optional expression.
 *
 * <p>`operand?.identifier` or `operand?.[expression]` or `operand?.(arg1, arg2)`
 *
 * <p>returns parse tree after trying to parse it as an optional expression
 */
private ParseTree maybeParseOptionalExpression(ParseTree operand) {
    // The optional chain's source info should cover the lhs operand also
    SourcePosition start = operand.location.start;
    while (peek(TokenType.QUESTION_DOT)) {
        eat(TokenType.QUESTION_DOT);
        switch(peekType()) {
            case OPEN_PAREN:
                ArgumentListTree arguments = parseArguments();
                operand = new OptChainCallExpressionTree(getTreeLocation(start), operand, arguments, /* isStartOfOptionalChain = */
                true, arguments.hasTrailingComma);
                break;
            case OPEN_SQUARE:
                eat(TokenType.OPEN_SQUARE);
                ParseTree member = parseExpression();
                eat(TokenType.CLOSE_SQUARE);
                operand = new OptionalMemberLookupExpressionTree(getTreeLocation(start), operand, member, /* isStartOfOptionalChain = */
                true);
                break;
            case NO_SUBSTITUTION_TEMPLATE:
            case TEMPLATE_HEAD:
                reportError("template literal cannot be used within optional chaining");
                break;
            default:
                if (peekIdOrKeyword()) {
                    IdentifierToken id = eatIdOrKeywordAsId();
                    operand = new OptionalMemberExpressionTree(getTreeLocation(start), operand, id, /* isStartOfOptionalChain = */
                    true);
                } else {
                    reportError("syntax error: %s not allowed in optional chain", peekType());
                }
        }
        operand = parseRemainingOptionalChainSegment(operand);
    }
    return operand;
}
Also used : ArgumentListTree(com.google.javascript.jscomp.parsing.parser.trees.ArgumentListTree) OptionalMemberLookupExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.OptionalMemberLookupExpressionTree) OptChainCallExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.OptChainCallExpressionTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) OptionalMemberExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.OptionalMemberExpressionTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 23 with ParseTree

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

the class Parser method parseNullishCoalesce.

private ParseTree parseNullishCoalesce(Expression expressionIn, ParseTree left, SourcePosition start) {
    while (peek(TokenType.QUESTION_QUESTION)) {
        Token operator = eat(TokenType.QUESTION_QUESTION);
        ParseTree right = parseBitwiseOR(expressionIn);
        left = new BinaryOperatorTree(getTreeLocation(start), left, operator, right);
    }
    if (peek(TokenType.AND) || peek(TokenType.OR)) {
        reportError("Logical OR and logical AND require parentheses when used with '??'");
    }
    return left;
}
Also used : BinaryOperatorTree(com.google.javascript.jscomp.parsing.parser.trees.BinaryOperatorTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 24 with ParseTree

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

the class Parser method parseIterRest.

private ParseTree parseIterRest(PatternKind patternKind) {
    SourcePosition start = getTreeStartLocation();
    eat(TokenType.ELLIPSIS);
    ParseTree patternAssignmentTarget = parseRestAssignmentTarget(patternKind);
    return new IterRestTree(getTreeLocation(start), patternAssignmentTarget);
}
Also used : IterRestTree(com.google.javascript.jscomp.parsing.parser.trees.IterRestTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 25 with ParseTree

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

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