Search in sources :

Example 11 with ParseTree

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

the class Parser method parseParameter.

private ParseTree parseParameter(ParamContext context) {
    SourcePosition start = getTreeStartLocation();
    ParseTree parameter = null;
    if (peek(TokenType.SPREAD)) {
        parameter = parseRestParameter();
    } else if (peekId()) {
        parameter = parseIdentifierExpression();
        if (peek(TokenType.QUESTION)) {
            eat(TokenType.QUESTION);
            parameter = new OptionalParameterTree(getTreeLocation(start), parameter);
        }
    } else if (context != ParamContext.TYPE_EXPRESSION && peekPatternStart()) {
        parameter = parsePattern(PatternKind.INITIALIZER);
    } else {
        throw new IllegalStateException("parseParameterCalled() without confirming a parameter exists.");
    }
    ParseTree typeAnnotation = null;
    SourceRange typeLocation = null;
    if (peek(TokenType.COLON)) {
        if (peek(1, TokenType.STRING)) {
            eat(TokenType.COLON);
            // Specialized Signature
            typeAnnotation = parseLiteralExpression();
        } else {
            typeAnnotation = parseTypeAnnotation();
        }
        typeLocation = getTreeLocation(getTreeStartLocation());
    }
    if (context == ParamContext.IMPLEMENTATION && !parameter.isRestParameter() && peek(TokenType.EQUAL)) {
        eat(TokenType.EQUAL);
        ParseTree defaultValue = parseAssignmentExpression();
        parameter = new DefaultParameterTree(getTreeLocation(start), parameter, defaultValue);
    }
    if (typeAnnotation != null) {
        // Must be a direct child of the parameter list.
        parameter = new TypedParameterTree(typeLocation, parameter, typeAnnotation);
    }
    return parameter;
}
Also used : OptionalParameterTree(com.google.javascript.jscomp.parsing.parser.trees.OptionalParameterTree) TypedParameterTree(com.google.javascript.jscomp.parsing.parser.trees.TypedParameterTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) DefaultParameterTree(com.google.javascript.jscomp.parsing.parser.trees.DefaultParameterTree) SourceRange(com.google.javascript.jscomp.parsing.parser.util.SourceRange) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 12 with ParseTree

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

the class Parser method parseTypeArgumentList.

private ParseTree parseTypeArgumentList(SourcePosition start, TypeNameTree typeName) {
    // < TypeArgumentList >
    // TypeArgumentList , TypeArgument
    eat(TokenType.OPEN_ANGLE);
    scanner.incTypeParameterLevel();
    ImmutableList.Builder<ParseTree> typeArguments = ImmutableList.builder();
    ParseTree type = parseType();
    typeArguments.add(type);
    while (peek(TokenType.COMMA)) {
        eat(TokenType.COMMA);
        type = parseType();
        if (type != null) {
            typeArguments.add(type);
        }
    }
    eat(TokenType.CLOSE_ANGLE);
    scanner.decTypeParameterLevel();
    return new ParameterizedTypeTree(getTreeLocation(start), typeName, typeArguments.build());
}
Also used : ParameterizedTypeTree(com.google.javascript.jscomp.parsing.parser.trees.ParameterizedTypeTree) ImmutableList(com.google.common.collect.ImmutableList) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 13 with ParseTree

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

the class Parser method parseArguments.

private ArgumentListTree parseArguments() {
    // ArgumentList :
    // AssignmentOrSpreadExpression
    // ArgumentList , AssignmentOrSpreadExpression
    // 
    // AssignmentOrSpreadExpression :
    // ... AssignmentExpression
    // AssignmentExpression
    SourcePosition start = getTreeStartLocation();
    ImmutableList.Builder<ParseTree> arguments = ImmutableList.builder();
    boolean trailingComma = false;
    ImmutableList.Builder<SourcePosition> commaPositions = ImmutableList.builder();
    eat(TokenType.OPEN_PAREN);
    while (peekAssignmentOrSpread()) {
        arguments.add(parseAssignmentOrSpread());
        if (!peek(TokenType.CLOSE_PAREN)) {
            Token comma = eat(TokenType.COMMA);
            if (comma != null) {
                commaPositions.add(comma.getStart());
            }
            if (peek(TokenType.CLOSE_PAREN)) {
                recordFeatureUsed(Feature.TRAILING_COMMA_IN_PARAM_LIST);
                if (!config.atLeast8) {
                    reportError(comma, "Invalid trailing comma in arguments list");
                }
                trailingComma = true;
            }
        }
    }
    eat(TokenType.CLOSE_PAREN);
    return new ArgumentListTree(getTreeLocation(start), arguments.build(), trailingComma, commaPositions.build());
}
Also used : ArgumentListTree(com.google.javascript.jscomp.parsing.parser.trees.ArgumentListTree) ImmutableList(com.google.common.collect.ImmutableList) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 14 with ParseTree

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

the class Parser method parseAwaitExpression.

private ParseTree parseAwaitExpression() {
    SourcePosition start = getTreeStartLocation();
    if (functionContextStack.isEmpty() || !functionContextStack.peekLast().isAsynchronous) {
        reportError("'await' used in a non-async function context");
    }
    eatPredefinedString(AWAIT);
    ParseTree expression = parseUnaryExpression();
    return new AwaitExpressionTree(getTreeLocation(start), expression);
}
Also used : AwaitExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.AwaitExpressionTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 15 with ParseTree

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

the class Parser method parseLogicalAND.

// 11.11 Logical AND
private ParseTree parseLogicalAND(Expression expressionIn) {
    SourcePosition start = getTreeStartLocation();
    ParseTree left = parseBitwiseOR(expressionIn);
    while (peek(TokenType.AND)) {
        Token operator = eat(TokenType.AND);
        ParseTree right = parseBitwiseOR(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)

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