Example #1
0
        public Node parseFunctionDeclaration(Node node, bool identifierIsOptional = true)
        {
            Node id = null;
            List<Node> @params = new List<Node>();
            List<Node> defaults = new List<Node>();
            List<Node> body;
            Token token;
            Token stricted;
            Options tmp;
            Token firstRestricted = null;
            string message = "";
            bool previousStrict;
            bool isGenerator;
            bool previousAllowYield;

            previousAllowYield = state.allowYield;

            expectKeyword("function");

            isGenerator = match("*");
            if (isGenerator)
            {
                lex();
            }

            if (!identifierIsOptional || !match("("))
            {
                token = lookahead;
                id = parseVariableIdentifier();
                if (strict)
                {
                    if (isRestrictedWord(token.value))
                    {
                        tolerateUnexpectedToken(token, Messages.StrictFunctionName);
                    }
                }
                else
                {
                    if (isRestrictedWord(token.value))
                    {
                        firstRestricted = token;
                        message = Messages.StrictFunctionName;
                    }
                    else if (isStrictModeReservedWord(token.value))
                    {
                        firstRestricted = token;
                        message = Messages.StrictReservedWord;
                    }
                }
            }

            state.allowYield = !isGenerator;
            tmp = parseParams(firstRestricted);
            @params = tmp.@params;
            defaults = tmp.defaults;
            stricted = tmp.stricted;
            firstRestricted = tmp.firstRestricted;
            if (tmp.message != null)
            {
                message = tmp.message;
            }


            previousStrict = strict;
            body = new List<Node>() { parseFunctionSourceElements() };
            if (strict && firstRestricted != null)
            {
                throwUnexpectedToken(firstRestricted, message);
            }
            if (strict && stricted != null)
            {
                tolerateUnexpectedToken(stricted, message);
            }

            strict = previousStrict;
            state.allowYield = previousAllowYield;

            return node.finishFunctionDeclaration(id, @params, defaults, body, isGenerator);
        }