Ejemplo n.º 1
0
 // ECMA-262 15.2.3 Exports
 public Node parseExportSpecifier()
 {
     Node exported = null;
     Node local;
     Node node = new Node();
     Node def;
     if (matchKeyword("default"))
     {
         // export {default} from "something";
         def = new Node();
         lex();
         local = def.finishIdentifier("default");
     }
     else
     {
         local = parseVariableIdentifier();
     }
     if (matchContextualKeyword("as"))
     {
         lex();
         exported = parseNonComputedProperty();
     }
     return node.finishExportSpecifier(local, exported);
 }
Ejemplo n.º 2
0
        // ECMA-262 13.3.2 Variable Statement

        public Node parseVariableIdentifier(string kind = null)
        {
            Token token;
            Node node = new Node();

            token = lex();

            if (token.type == TokenType.Keyword && token.value.ToString() == "yield")
            {
                if (strict)
                {
                    tolerateUnexpectedToken(token, Messages.StrictReservedWord);
                }
                if (!state.allowYield)
                {
                    throwUnexpectedToken(token);
                }
            }
            else if (token.type != TokenType.Identifier)
            {
                if (strict && token.type == TokenType.Keyword && isStrictModeReservedWord(token.value))
                {
                    tolerateUnexpectedToken(token, Messages.StrictReservedWord);
                }
                else
                {
                    if (strict || token.value != "let" || kind != "var")
                    {
                        throwUnexpectedToken(token);
                    }
                }
            }
            else if (state.sourceType == "module" && token.type == TokenType.Identifier && token.value == "await")
            {
                tolerateUnexpectedToken(token);
            }

            return node.finishIdentifier(token.value);
        }
Ejemplo n.º 3
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;
        }
Ejemplo n.º 4
0
        public Node parseNonComputedProperty()
        {
            Token token;
            Node node = new Node();

            token = lex();

            if (!isIdentifierName(token))
            {
                throwUnexpectedToken(token);
            }

            return node.finishIdentifier(token.value);
        }
Ejemplo n.º 5
0
        public Node parseObjectPropertyKey()
        {
            Token token;
            Node node = new Node();
            Node expr;

            token = lex();

            // Note: This function is called only from parseObjectProperty(), where
            // EOF and Punctuator tokens are already filtered out.

            switch (token.type)
            {
                case TokenType.StringLiteral:
                case TokenType.NumericLiteral:
                    if (strict && token.octal)
                    {
                        tolerateUnexpectedToken(token, Messages.StrictOctalLiteral);
                    }
                    return node.finishLiteral(token);
                case TokenType.Identifier:
                case TokenType.BooleanLiteral:
                case TokenType.NullLiteral:
                case TokenType.Keyword:
                    return node.finishIdentifier(token.value);
                case TokenType.Punctuator:
                    if (token.value == "[")
                    {
                        expr = isolateCoverGrammar(parseAssignmentExpression);
                        expect("]");
                        return expr;
                    }
                    break;
            }
            throwUnexpectedToken(token);
            return null;
        }