Search in sources :

Example 1 with NewExpressionTree

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

the class Parser method parseLeftHandSideExpression.

/**
 * Parse LeftHandSideExpression.
 */
@SuppressWarnings("incomplete-switch")
private ParseTree parseLeftHandSideExpression() {
    SourcePosition start = getTreeStartLocation();
    // We have these possible productions.
    // LeftHandSideExpression -> NewExpression
    // -> CallExpression
    // -> MemberExpression
    // -> OptionalExpression
    // 
    // NewExpression -> new NewExpression
    // -> MemberExpression
    // 
    // CallExpression -> MemberExpression Arguments
    // -> CallExpression ... see below
    // 
    // OptionalExpression -> MemberExpression OptionalChain
    // -> CallExpression OptionalChain
    // -> OptionalExpression OptionalChain
    // 
    // We try parsing a NewExpression, here, because that will include parsing MemberExpression.
    // If what we really have is a CallExpression or OptionalExpression, then the MemberExpression
    // we get back from parseNewExpression will be the first part of it, and we'll build the
    // rest later.
    ParseTree operand = parseNewExpression();
    // this test is equivalent to is member expression
    if (!(operand instanceof NewExpressionTree) || ((NewExpressionTree) operand).arguments != null) {
        // Attempt to gather the rest of the CallExpression, if so.
        while (peekCallSuffix()) {
            switch(peekType()) {
                case OPEN_PAREN:
                    ArgumentListTree arguments = parseArguments();
                    operand = new CallExpressionTree(getTreeLocation(start), operand, arguments);
                    break;
                case OPEN_SQUARE:
                    eat(TokenType.OPEN_SQUARE);
                    ParseTree member = parseExpression();
                    eat(TokenType.CLOSE_SQUARE);
                    operand = new MemberLookupExpressionTree(getTreeLocation(start), operand, member);
                    break;
                case PERIOD:
                    eat(TokenType.PERIOD);
                    IdentifierToken id = eatIdOrKeywordAsId();
                    operand = new MemberExpressionTree(getTreeLocation(start), operand, id);
                    break;
                case NO_SUBSTITUTION_TEMPLATE:
                case TEMPLATE_HEAD:
                    operand = parseTemplateLiteral(operand);
                    break;
                default:
                    throw new AssertionError("unexpected case: " + peekType());
            }
        }
        operand = maybeParseOptionalExpression(operand);
    }
    return operand;
}
Also used : CallExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.CallExpressionTree) OptChainCallExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.OptChainCallExpressionTree) ArgumentListTree(com.google.javascript.jscomp.parsing.parser.trees.ArgumentListTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) MemberExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.MemberExpressionTree) OptionalMemberExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.OptionalMemberExpressionTree) NewExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.NewExpressionTree) MemberLookupExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.MemberLookupExpressionTree) OptionalMemberLookupExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.OptionalMemberLookupExpressionTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Aggregations

ArgumentListTree (com.google.javascript.jscomp.parsing.parser.trees.ArgumentListTree)1 CallExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.CallExpressionTree)1 MemberExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.MemberExpressionTree)1 MemberLookupExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.MemberLookupExpressionTree)1 NewExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.NewExpressionTree)1 OptChainCallExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.OptChainCallExpressionTree)1 OptionalMemberExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.OptionalMemberExpressionTree)1 OptionalMemberLookupExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.OptionalMemberLookupExpressionTree)1 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)1 SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)1