Exemplo n.º 1
0
        internal static CodeNode Parse(ParseInfo state, ref int index)
        {
            if (!Parser.Validate(state.Code, "import", ref index))
            {
                return(null);
            }

            Tools.SkipSpaces(state.Code, ref index);

            var result = new ImportStatement();

            var start = index;

            if (!Parser.ValidateString(state.Code, ref index, true))
            {
                var onlyDefault = false;
                if (Parser.ValidateName(state.Code, ref index))
                {
                    var defaultAlias = state.Code.Substring(start, index - start);
                    result._map.Add(new KeyValuePair <string, string>("", defaultAlias));

                    onlyDefault = true;
                    Tools.SkipSpaces(state.Code, ref index);
                    if (state.Code[index] == ',')
                    {
                        onlyDefault = false;
                        index++;
                        Tools.SkipSpaces(state.Code, ref index);
                    }
                }

                if (!onlyDefault)
                {
                    if (result._map.Count == 0 && state.Code[index] == '*')
                    {
                        index++;
                        Tools.SkipSpaces(state.Code, ref index);
                        var alias = parseAlias(state.Code, ref index);
                        if (alias == null)
                        {
                            ExceptionHelper.ThrowSyntaxError("Expected identifier", state.Code, index);
                        }
                        result._map.Add(new KeyValuePair <string, string>("*", alias));
                    }
                    else if (state.Code[index] == '{')
                    {
                        parseImportMap(result, state.Code, ref index);
                    }
                    else
                    {
                        ExceptionHelper.ThrowSyntaxError(Strings.UnexpectedToken, state.Code, index);
                    }
                }

                Tools.SkipSpaces(state.Code, ref index);

                if (!Parser.Validate(state.Code, "from", ref index))
                {
                    ExceptionHelper.ThrowSyntaxError("Expected 'from'", state.Code, index);
                }

                Tools.SkipSpaces(state.Code, ref index);

                start = index;

                if (!Parser.ValidateString(state.Code, ref index, true))
                {
                    ExceptionHelper.ThrowSyntaxError("Expected module name", state.Code, index);
                }
            }

            result._moduleName = Tools.Unescape(state.Code.Substring(start + 1, index - start - 2), false);

            return(result);
        }