Search in sources :

Example 36 with ParseTree

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

the class Parser method parseCaseClauses.

private ImmutableList<ParseTree> parseCaseClauses() {
    boolean foundDefaultClause = false;
    ImmutableList.Builder<ParseTree> result = ImmutableList.builder();
    while (true) {
        SourcePosition start = getTreeStartLocation();
        switch(peekType()) {
            case CASE:
                eat(TokenType.CASE);
                ParseTree expression = parseExpression();
                eat(TokenType.COLON);
                ImmutableList<ParseTree> statements = parseCaseStatementsOpt();
                result.add(new CaseClauseTree(getTreeLocation(start), expression, statements));
                break;
            case DEFAULT:
                if (foundDefaultClause) {
                    reportError("Switch statements may have at most one default clause");
                } else {
                    foundDefaultClause = true;
                }
                eat(TokenType.DEFAULT);
                eat(TokenType.COLON);
                result.add(new DefaultClauseTree(getTreeLocation(start), parseCaseStatementsOpt()));
                break;
            default:
                return result.build();
        }
    }
}
Also used : DefaultClauseTree(com.google.javascript.jscomp.parsing.parser.trees.DefaultClauseTree) ImmutableList(com.google.common.collect.ImmutableList) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) CaseClauseTree(com.google.javascript.jscomp.parsing.parser.trees.CaseClauseTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 37 with ParseTree

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

the class Parser method parseAsyncMethod.

private ParseTree parseAsyncMethod(PartialClassElement partial) {
    eatPredefinedString(ASYNC);
    boolean generator = peek(TokenType.STAR);
    if (generator) {
        eat(TokenType.STAR);
    }
    if (peekPropertyName(0)) {
        if (peekIdOrKeyword()) {
            IdentifierToken name = eatIdOrKeywordAsId();
            FunctionDeclarationTree.Builder builder = FunctionDeclarationTree.builder(FunctionDeclarationTree.Kind.MEMBER).setAsync(true).setGenerator(generator).setStatic(partial.isStatic).setName(name);
            parseFunctionTail(builder, generator ? FunctionFlavor.ASYNCHRONOUS_GENERATOR : FunctionFlavor.ASYNCHRONOUS);
            return builder.build(getTreeLocation(name.getStart()));
        } else {
            // { 'str'() {} }
            // { 123() {} }
            // Treat these as if they were computed properties.
            ParseTree nameExpr = parseLiteralExpression();
            FunctionDeclarationTree.Builder builder = FunctionDeclarationTree.builder(FunctionDeclarationTree.Kind.EXPRESSION).setAsync(true).setGenerator(generator).setStatic(partial.isStatic);
            parseFunctionTail(builder, generator ? FunctionFlavor.ASYNCHRONOUS_GENERATOR : FunctionFlavor.ASYNCHRONOUS);
            ParseTree function = builder.build(getTreeLocation(nameExpr.getStart()));
            return new ComputedPropertyMethodTree(getTreeLocation(nameExpr.getStart()), nameExpr, function);
        }
    } else {
        // expect '[' to start computed property name
        ParseTree nameExpr = parseComputedPropertyName();
        FunctionDeclarationTree.Builder builder = FunctionDeclarationTree.builder(FunctionDeclarationTree.Kind.EXPRESSION).setAsync(true).setGenerator(generator).setStatic(partial.isStatic);
        parseFunctionTail(builder, generator ? FunctionFlavor.ASYNCHRONOUS_GENERATOR : FunctionFlavor.ASYNCHRONOUS);
        ParseTree function = builder.build(getTreeLocation(nameExpr.getStart()));
        return new ComputedPropertyMethodTree(getTreeLocation(nameExpr.getStart()), nameExpr, function);
    }
}
Also used : FunctionDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.FunctionDeclarationTree) ComputedPropertyMethodTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMethodTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 38 with ParseTree

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

the class Parser method parseEquality.

// 11.9 Equality Expression
private ParseTree parseEquality(Expression expressionIn) {
    SourcePosition start = getTreeStartLocation();
    ParseTree left = parseRelational(expressionIn);
    while (peekEqualityOperator()) {
        Token operator = nextToken();
        ParseTree right = parseRelational(expressionIn);
        left = new BinaryOperatorTree(getTreeLocation(start), left, operator, right);
    }
    return left;
}
Also used : 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 39 with ParseTree

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

the class Parser method parseUpdateExpression.

private ParseTree parseUpdateExpression() {
    SourcePosition start = getTreeStartLocation();
    if (peekUpdateOperator()) {
        Token operator = nextToken();
        ParseTree operand = parseUnaryExpression();
        return UpdateExpressionTree.prefix(getTreeLocation(start), operator, operand);
    } else {
        ParseTree lhs = parseLeftHandSideExpression();
        if (peekUpdateOperator() && !peekImplicitSemiColon()) {
            // newline not allowed before an update operator.
            Token operator = nextToken();
            return UpdateExpressionTree.postfix(getTreeLocation(start), operator, lhs);
        } else {
            return lhs;
        }
    }
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 40 with ParseTree

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

the class Parser method parseMemberExpressionNoNew.

// 11.2 Member Expression without the new production
private ParseTree parseMemberExpressionNoNew() {
    SourcePosition start = getTreeStartLocation();
    ParseTree operand;
    if (peekImportDot()) {
        operand = parseImportDotMeta();
    } else if (peekAsyncFunctionStart()) {
        operand = parseAsyncFunctionExpression();
    } else if (peekFunction()) {
        operand = parseFunctionExpression();
    } else {
        operand = parsePrimaryExpression();
    }
    while (peekMemberExpressionSuffix()) {
        switch(peekType()) {
            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 RuntimeException("unreachable");
        }
    }
    return operand;
}
Also used : 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) 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

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