Search in sources :

Example 1 with FormalParameterListTree

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

the class Parser method transformToArrowFormalParameters.

private FormalParameterListTree transformToArrowFormalParameters(ParseTree leftOfArrow) {
    FormalParameterListTree arrowParameterList;
    switch(leftOfArrow.type) {
        case FORMAL_PARAMETER_LIST:
            arrowParameterList = leftOfArrow.asFormalParameterList();
            break;
        case IDENTIFIER_EXPRESSION:
            // e.g. x => x + 1
            arrowParameterList = new FormalParameterListTree(leftOfArrow.location, ImmutableList.<ParseTree>of(leftOfArrow), /* hasTrailingComma= */
            false, ImmutableList.<SourcePosition>of());
            break;
        case ARGUMENT_LIST:
        case PAREN_EXPRESSION:
            // e.g. (x) => x + 1
            resetScanner(leftOfArrow);
            // If we fail to parse as an ArrowFunction parameter list then
            // parseFormalParameterList will take care of reporting errors.
            arrowParameterList = parseFormalParameterList();
            break;
        default:
            reportError(leftOfArrow, "invalid arrow function parameters");
            arrowParameterList = newEmptyFormalParameterList(leftOfArrow.location);
    }
    return arrowParameterList;
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) FormalParameterListTree(com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 2 with FormalParameterListTree

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

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

the class Parser method parseFunctionTypeExpression.

private ParseTree parseFunctionTypeExpression() {
    SourcePosition start = getTreeStartLocation();
    ParseTree typeExpression = null;
    if (peekFunctionTypeExpression()) {
        FormalParameterListTree formalParameterList;
        formalParameterList = parseFormalParameterList(ParamContext.IMPLEMENTATION);
        eat(TokenType.ARROW);
        ParseTree returnType = parseType();
        typeExpression = new FunctionTypeTree(getTreeLocation(start), formalParameterList, returnType);
    } else {
        typeExpression = parseArrayTypeExpression();
    }
    return typeExpression;
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) FunctionTypeTree(com.google.javascript.jscomp.parsing.parser.trees.FunctionTypeTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree) FormalParameterListTree(com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree)

Example 4 with FormalParameterListTree

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

the class Parser method parseCallSignature.

private CallSignatureTree parseCallSignature(boolean isNew) {
    SourcePosition start = getTreeStartLocation();
    if (isNew) {
        eat(TokenType.NEW);
    }
    GenericTypeListTree generics = maybeParseGenericTypes();
    FormalParameterListTree params = parseFormalParameterList(ParamContext.SIGNATURE);
    ParseTree returnType = maybeParseColonType();
    return new CallSignatureTree(getTreeLocation(start), isNew, generics, params, returnType);
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) GenericTypeListTree(com.google.javascript.jscomp.parsing.parser.trees.GenericTypeListTree) CallSignatureTree(com.google.javascript.jscomp.parsing.parser.trees.CallSignatureTree) FormalParameterListTree(com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 5 with FormalParameterListTree

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

the class Parser method parseFormalParameterList.

private FormalParameterListTree parseFormalParameterList(ParamContext context) {
    SourcePosition listStart = getTreeStartLocation();
    eat(TokenType.OPEN_PAREN);
    ImmutableList.Builder<ParseTree> result = ImmutableList.builder();
    while (peekParameter(context)) {
        result.add(parseParameter(context));
        if (!peek(TokenType.CLOSE_PAREN)) {
            Token comma = eat(TokenType.COMMA);
            if (peek(TokenType.CLOSE_PAREN)) {
                features = features.with(Feature.TRAILING_COMMA_IN_PARAM_LIST);
                if (!config.atLeast8) {
                    reportError(comma, "Invalid trailing comma in formal parameter list");
                }
            }
        }
    }
    eat(TokenType.CLOSE_PAREN);
    return new FormalParameterListTree(getTreeLocation(listStart), result.build());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree) FormalParameterListTree(com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree)

Aggregations

FormalParameterListTree (com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree)9 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)9 SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)7 ImmutableList (com.google.common.collect.ImmutableList)2 FunctionDeclarationTree (com.google.javascript.jscomp.parsing.parser.trees.FunctionDeclarationTree)2 BlockTree (com.google.javascript.jscomp.parsing.parser.trees.BlockTree)1 CallSignatureTree (com.google.javascript.jscomp.parsing.parser.trees.CallSignatureTree)1 CommaExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.CommaExpressionTree)1 ComputedPropertySetterTree (com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertySetterTree)1 FunctionTypeTree (com.google.javascript.jscomp.parsing.parser.trees.FunctionTypeTree)1 GenericTypeListTree (com.google.javascript.jscomp.parsing.parser.trees.GenericTypeListTree)1 IdentifierExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.IdentifierExpressionTree)1 MissingPrimaryExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.MissingPrimaryExpressionTree)1 ParenExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.ParenExpressionTree)1 SetAccessorTree (com.google.javascript.jscomp.parsing.parser.trees.SetAccessorTree)1