Search in sources :

Example 1 with OptionalParameterTree

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

Aggregations

DefaultParameterTree (com.google.javascript.jscomp.parsing.parser.trees.DefaultParameterTree)1 OptionalParameterTree (com.google.javascript.jscomp.parsing.parser.trees.OptionalParameterTree)1 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)1 TypedParameterTree (com.google.javascript.jscomp.parsing.parser.trees.TypedParameterTree)1 SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)1 SourceRange (com.google.javascript.jscomp.parsing.parser.util.SourceRange)1