示例#1
0
        // ECMA-262 12.2 Primary Expressions

        public Node parsePrimaryExpression()
        {
            TokenType type;
            Token token;
            Node expr = null;
            Node node;

            if (match("("))
            {
                isBindingElement = false;
                return inheritCoverGrammar(parseGroupExpression);
            }

            if (match("["))
            {
                return inheritCoverGrammar(parseArrayInitializer);
            }

            if (match("{"))
            {
                return inheritCoverGrammar(parseObjectInitializer);
            }

            type = lookahead.type;
            node = new Node();

            if (type == TokenType.Identifier)
            {
                if (state.sourceType == "module" && lookahead.value == "await")
                {
                    tolerateUnexpectedToken(lookahead);
                }
                expr = node.finishIdentifier(lex().value);
            }
            else if (type == TokenType.StringLiteral || type == TokenType.NumericLiteral)
            {
                isAssignmentTarget = isBindingElement = false;
                if (strict && lookahead.octal)
                {
                    tolerateUnexpectedToken(lookahead, Messages.StrictOctalLiteral);
                }
                expr = node.finishLiteral(lex());
            }
            else if (type == TokenType.Keyword)
            {
                if (!strict && state.allowYield && matchKeyword("yield"))
                {
                    return parseNonComputedProperty();
                }
                isAssignmentTarget = isBindingElement = false;
                if (matchKeyword("function"))
                {
                    return parseFunctionExpression();
                }
                if (matchKeyword("this"))
                {
                    lex();
                    return node.finishThisExpression();
                }
                if (matchKeyword("class"))
                {
                    return parseClassExpression();
                }
                throwUnexpectedToken(lex());
            }
            else if (type == TokenType.BooleanLiteral)
            {
                isAssignmentTarget = isBindingElement = false;
                token = lex();
                token.value = (token.value == "true").ToString();
                expr = node.finishLiteral(token);
            }
            else if (type == TokenType.NullLiteral)
            {
                isAssignmentTarget = isBindingElement = false;
                token = lex();
                token.value = null;
                expr = node.finishLiteral(token);
            }
            else if (match("/") || match("/="))
            {
                isAssignmentTarget = isBindingElement = false;
                index = startIndex;

                if (extra.tokens != null)
                {
                    token = collectRegex();
                }
                else
                {
                    token = scanRegExp();
                }
                lex();
                expr = node.finishLiteral(token);
            }
            else if (type == TokenType.Template)
            {
                expr = parseTemplateLiteral();
            }
            else
            {
                throwUnexpectedToken(lex());
            }

            return expr;
        }