Search in sources :

Example 1 with DefaultParameterTree

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

the class Parser method parsePatternAssignmentTarget.

/**
 * A PatternAssignmentTarget is the location where the assigned value gets stored, including an
 * optional default value.
 *
 * <dl>
 *   <dt>Spec AssignmentElement === PatternAssignmentTarget(PatternKind.ANY)
 *   <dd>Valid in an assignment that is not a formal parameter list or variable declaration.
 *       Sub-patterns and arbitrary left hand side expressions are allowed.
 *   <dt>Spec BindingElement === PatternAssignmentElement(PatternKind.INITIALIZER)
 *   <dd>Valid in a formal parameter list or variable declaration statement. Only sub-patterns and
 *       identifiers are allowed.
 * </dl>
 *
 * Examples:
 *
 * <pre>
 *   <code>
 *     [a, {foo: b = 'default'}] = someArray;          // valid
 *     [x.a, {foo: x.b = 'default'}] = someArray;      // valid
 *
 *     let [a, {foo: b = 'default'}] = someArray;      // valid
 *     let [x.a, {foo: x.b = 'default'}] = someArray;  // invalid
 *
 *     function f([a, {foo: b = 'default'}]) {...}     // valid
 *     function f([x.a, {foo: x.b = 'default'}]) {...} // invalid
 *   </code>
 * </pre>
 */
private ParseTree parsePatternAssignmentTarget(PatternKind patternKind) {
    SourcePosition start = getTreeStartLocation();
    ParseTree assignmentTarget;
    assignmentTarget = parsePatternAssignmentTargetNoDefault(patternKind);
    if (peek(TokenType.EQUAL)) {
        eat(TokenType.EQUAL);
        ParseTree defaultValue = parseAssignmentExpression();
        assignmentTarget = new DefaultParameterTree(getTreeLocation(start), assignmentTarget, defaultValue);
    }
    return assignmentTarget;
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) DefaultParameterTree(com.google.javascript.jscomp.parsing.parser.trees.DefaultParameterTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 2 with DefaultParameterTree

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

the class Parser method parsePropertyNameAssignment.

private ParseTree parsePropertyNameAssignment() {
    SourcePosition start = getTreeStartLocation();
    Token name = eatObjectLiteralPropertyName();
    Token colon = eatOpt(TokenType.COLON);
    if (colon == null) {
        if (name.type != TokenType.IDENTIFIER) {
            reportExpectedError(peekToken(), TokenType.COLON);
        } else if (Keywords.isKeyword(name.asIdentifier().value)) {
            reportError(name, "Cannot use keyword in short object literal");
        } else if (peek(TokenType.EQUAL)) {
            IdentifierExpressionTree idTree = new IdentifierExpressionTree(getTreeLocation(start), (IdentifierToken) name);
            eat(TokenType.EQUAL);
            ParseTree defaultValue = parseAssignmentExpression();
            return new DefaultParameterTree(getTreeLocation(start), idTree, defaultValue);
        }
    }
    ParseTree value = colon == null ? null : parseAssignmentExpression();
    return new PropertyNameAssignmentTree(getTreeLocation(start), name, value);
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) DefaultParameterTree(com.google.javascript.jscomp.parsing.parser.trees.DefaultParameterTree) IdentifierExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.IdentifierExpressionTree) PropertyNameAssignmentTree(com.google.javascript.jscomp.parsing.parser.trees.PropertyNameAssignmentTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 3 with DefaultParameterTree

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

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

the class Parser method parseParameter.

private ParseTree parseParameter() {
    SourcePosition start = getTreeStartLocation();
    ParseTree parameter = null;
    if (peek(TokenType.ELLIPSIS)) {
        parameter = parseIterRest(PatternKind.INITIALIZER);
    } else if (peekId()) {
        parameter = parseIdentifierExpression();
    } else if (peekPatternStart()) {
        parameter = parsePattern(PatternKind.INITIALIZER);
    } else {
        throw new IllegalStateException("parseParameterCalled() without confirming a parameter exists.");
    }
    if (!parameter.isRestParameter() && peek(TokenType.EQUAL)) {
        eat(TokenType.EQUAL);
        ParseTree defaultValue = parseAssignmentExpression();
        parameter = new DefaultParameterTree(getTreeLocation(start), parameter, defaultValue);
    }
    return parameter;
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) DefaultParameterTree(com.google.javascript.jscomp.parsing.parser.trees.DefaultParameterTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 5 with DefaultParameterTree

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

the class Parser method parseObjectPatternField.

private ParseTree parseObjectPatternField(PatternKind kind) {
    SourcePosition start = getTreeStartLocation();
    if (peekType() == TokenType.OPEN_SQUARE) {
        ParseTree key = parseComputedPropertyName();
        eat(TokenType.COLON);
        ParseTree value = parsePatternAssignmentTarget(kind);
        return new ComputedPropertyDefinitionTree(getTreeLocation(start), key, value);
    }
    Token name;
    if (peekIdOrKeyword()) {
        name = eatIdOrKeywordAsId();
        if (!peek(TokenType.COLON)) {
            IdentifierToken idToken = (IdentifierToken) name;
            if (Keywords.isKeyword(idToken.value)) {
                reportError("cannot use keyword '%s' here.", name);
            }
            if (peek(TokenType.EQUAL)) {
                IdentifierExpressionTree idTree = new IdentifierExpressionTree(getTreeLocation(start), idToken);
                eat(TokenType.EQUAL);
                ParseTree defaultValue = parseAssignmentExpression();
                return new DefaultParameterTree(getTreeLocation(start), idTree, defaultValue);
            }
            return new PropertyNameAssignmentTree(getTreeLocation(start), name, null);
        }
    } else {
        name = parseLiteralExpression().literalToken;
    }
    eat(TokenType.COLON);
    ParseTree value = parsePatternAssignmentTarget(kind);
    return new PropertyNameAssignmentTree(getTreeLocation(start), name, value);
}
Also used : ComputedPropertyDefinitionTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyDefinitionTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) DefaultParameterTree(com.google.javascript.jscomp.parsing.parser.trees.DefaultParameterTree) IdentifierExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.IdentifierExpressionTree) PropertyNameAssignmentTree(com.google.javascript.jscomp.parsing.parser.trees.PropertyNameAssignmentTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Aggregations

DefaultParameterTree (com.google.javascript.jscomp.parsing.parser.trees.DefaultParameterTree)5 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)5 SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)5 IdentifierExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.IdentifierExpressionTree)2 PropertyNameAssignmentTree (com.google.javascript.jscomp.parsing.parser.trees.PropertyNameAssignmentTree)2 ComputedPropertyDefinitionTree (com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyDefinitionTree)1 OptionalParameterTree (com.google.javascript.jscomp.parsing.parser.trees.OptionalParameterTree)1 TypedParameterTree (com.google.javascript.jscomp.parsing.parser.trees.TypedParameterTree)1 SourceRange (com.google.javascript.jscomp.parsing.parser.util.SourceRange)1