示例#1
0
        // This function is to try to parse a MethodDefinition as defined in 14.3. But in the case of object literals,
        // it might be called at a position where there is in fact a short hand identifier pattern or a data property.
        // This can only be determined after we consumed up to the left parentheses.
        //
        // In order to avoid back tracking, it returns `null` if the position is not a MethodDefinition and the caller
        // is responsible to visit other options.
        public Node tryParseMethodDefinition(Token token, Node key, bool computed, Node node)
        {
            Node value;
            Options options;
            Node methodNode;
            Options @params;
            bool previousAllowYield = state.allowYield;

            if (token.type == TokenType.Identifier)
            {
                // check for `get` and `set`;

                if (token.value == "get" && lookaheadPropertyName())
                {
                    computed = match("[");
                    key = parseObjectPropertyKey();
                    methodNode = new Node();
                    expect("(");
                    expect(")");

                    state.allowYield = false;
                    value = parsePropertyFunction(methodNode, new Options()
                    {
                        @params = new List<Node>(),
                        defaults = new List<Node>(),
                        stricted = null,
                        firstRestricted = null,
                        message = null
                    }, false);
                    state.allowYield = previousAllowYield;

                    return node.finishProperty("get", key, computed, value, false, false);
                }
                else if (token.value == "set" && lookaheadPropertyName())
                {
                    computed = match("[");
                    key = parseObjectPropertyKey();
                    methodNode = new Node();
                    expect("(");

                    options = new Options()
                    {
                        @params = new List<Node>(),
                        defaultCount = 0,
                        defaults = new List<Node>(),
                        firstRestricted = null,
                        // paramSet = {}
                    };
                    if (match(")"))
                    {
                        tolerateUnexpectedToken(lookahead);
                    }
                    else
                    {
                        state.allowYield = false;
                        parseParam(options);
                        state.allowYield = previousAllowYield;
                        if (options.defaultCount == 0)
                        {
                            options.defaults = new List<Node>();
                        }
                    }
                    expect(")");

                    state.allowYield = false;
                    value = parsePropertyFunction(methodNode, options, false);
                    state.allowYield = previousAllowYield;

                    return node.finishProperty("set", key, computed, value, false, false);
                }
            }
            else if (token.type == TokenType.Punctuator && token.value == "*" && lookaheadPropertyName())
            {
                computed = match("[");
                key = parseObjectPropertyKey();
                methodNode = new Node();

                state.allowYield = true;
                @params = parseParams();
                state.allowYield = previousAllowYield;

                state.allowYield = false;
                value = parsePropertyFunction(methodNode, @params, true);
                state.allowYield = previousAllowYield;

                return node.finishProperty("init", key, computed, value, true, false);
            }

            if (key != null && match("("))
            {
                value = parsePropertyMethodFunction();
                return node.finishProperty("init", key, computed, value, true, false);
            }

            // Not a MethodDefinition.
            return null;
        }
示例#2
0
        public Node parseObjectProperty( bool hasProto)
        {
            var token = lookahead;
            Node node = new Node();
            bool computed;
            Node key = null;
            Node maybeMethod;
            bool proto;
            Node value;

            computed = match("[");
            if (match("*")) {
                lex();
            } else {
                key = parseObjectPropertyKey();
            }
            maybeMethod = tryParseMethodDefinition(token, key, computed, node);
            if (maybeMethod!= null) {
                return maybeMethod;
            }

            if (key== null) {
                throwUnexpectedToken(lookahead);
            }

            // Check for duplicated __proto__
            if (!computed) {
                proto = (key.type == Syntax.Identifier && key.name == "__proto__") ||
                    (key.type == Syntax.Literal && key.name== "__proto__");
                if (hasProto && proto) {
                    tolerateError(Messages.DuplicateProtoProperty);
                }
                hasProto |= proto;
            }

            if (match(":")) {
                lex();
                value = inheritCoverGrammar(parseAssignmentExpression);
                return node.finishProperty("init", key, computed, value, false, false);
            }

            if (token.type == TokenType.Identifier) {
                if (match("=")) {
                    firstCoverInitializedNameError = lookahead;
                    lex();
                    value = isolateCoverGrammar(parseAssignmentExpression);
                    return node.finishProperty("init", key, computed,
                        new Node(token).finishAssignmentPattern(key, value), false, true);
                }
                return node.finishProperty("init", key, computed, key, false, true);
            }

            throwUnexpectedToken(lookahead);
            return null;
        }
示例#3
0
        public Node parsePropertyPattern(List<Token> @params, string kind)
        {
            var node = new Node();
            Node key;
            Token keyToken;
            bool computed = match("[");
            Node init;
            if (lookahead.type == TokenType.Identifier)
            {
                keyToken = lookahead;
                key = parseVariableIdentifier();
                if (match("="))
                {
                    @params.Add(keyToken);
                    lex();
                    init = parseAssignmentExpression();

                    return node.finishProperty("init", key, false,
                        new Node(keyToken).finishAssignmentPattern(key, init), false, false);
                }
                else if (!match(":"))
                {
                    @params.Add(keyToken);
                    return node.finishProperty("init", key, false, key, false, true);
                }
            }
            else
            {
                key = new Node();// parseObjectPropertyKey(@params, kind);
            }
            expect(":");
            init = parsePatternWithDefault(@params, kind);
            return node.finishProperty("init", key, computed, init, false, false);
        }