示例#1
0
        public Node parseImportDeclaration()
        {
            List<Node> specifiers = new List<Node>();
            Node src;
            Node node = new Node();

            if (state.inFunctionBody)
            {
                throwError(Messages.IllegalImportDeclaration);
            }

            expectKeyword("import");

            if (lookahead.type == TokenType.StringLiteral)
            {
                // import "foo";
                src = parseModuleSpecifier();
            }
            else
            {

                if (match("{"))
                {
                    // import {bar}
                    specifiers = specifiers.Concat(parseNamedImports()).ToList();
                }
                else if (match("*"))
                {
                    // import * as foo
                    specifiers.Add(parseImportNamespaceSpecifier());
                }
                else if (isIdentifierName(lookahead) && !matchKeyword("default"))
                {
                    // import foo
                    specifiers.Add(parseImportDefaultSpecifier());
                    if (match(","))
                    {
                        lex();
                        if (match("*"))
                        {
                            // import foo, * as foo
                            specifiers.Add(parseImportNamespaceSpecifier());
                        }
                        else if (match("{"))
                        {
                            // import foo, {bar}
                            specifiers = specifiers.Concat(parseNamedImports()).ToList();
                        }
                        else
                        {
                            throwUnexpectedToken(lookahead);
                        }
                    }
                }
                else
                {
                    throwUnexpectedToken(lex());
                }

                if (!matchContextualKeyword("from"))
                {
                    throwError(
                        string.IsNullOrEmpty(lookahead.value) ? Messages.UnexpectedToken : Messages.MissingFromClause,
                        lookahead.value);
                }
                lex();
                src = parseModuleSpecifier();
            }

            consumeSemicolon();
            return node.finishImportDeclaration(specifiers, src.ToString());
        }