Example #1
0
        private static void parseExportMap(ExportStatement export, ParseInfo state, ref int index)
        {
            index++;
            Tools.SkipSpaces(state.Code, ref index);

            if (state.Code[index] == '}')
            {
                ExceptionHelper.ThrowSyntaxError("Empty export map", state.Code, index);
            }

            while (state.Code[index] != '}')
            {
                var start = index;
                if (!Parser.ValidateName(state.Code, ref index))
                {
                    ExceptionHelper.ThrowSyntaxError("Invalid export name", state.Code, index);
                }
                var name  = state.Code.Substring(start, index - start);
                var alias = name;

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

                alias = parseAlias(state.Code, ref index) ?? name;

                for (var i = 0; i < export._map.Count; i++)
                {
                    if (export._map[i].Key == name)
                    {
                        ExceptionHelper.ThrowSyntaxError("Duplicate import", state.Code, index);
                    }
                }

                export._map.Add(
                    new KeyValuePair <string, Expression>(
                        alias,
                        new Variable(name, state.lexicalScopeLevel)
                {
                    Position = start,
                    Length   = name.Length
                }));

                if (Parser.Validate(state.Code, ",", ref index))
                {
                    Tools.SkipSpaces(state.Code, ref index);
                }
            }

            index++;
        }
Example #2
0
        internal static CodeNode Parse(ParseInfo state, ref int index)
        {
            if (!Parser.Validate(state.Code, "export", ref index))
            {
                return(null);
            }

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

            var result   = new ExportStatement();
            var reexport = 0;

            if (Parser.Validate(state.Code, "*", ref index))
            {
                reexport = 1;
            }
            else if (Parser.Validate(state.Code, "default", ref index))
            {
                reexport = -1;
                Tools.SkipSpaces(state.Code, ref index);

                using (state.WithCodeContext(CodeContext.InExport))
                {
                    var variables = VariableDefinition.Parse(state, ref index);

                    if (variables != null)
                    {
                        result._internalDefinition = variables;
                    }
                    else
                    {
                        var expression = ClassDefinition.Parse(state, ref index)
                                         ?? FunctionDefinition.Parse(state, ref index, BaseLibrary.FunctionKind.Function)
                                         ?? ExpressionTree.Parse(state, ref index);

                        result._map.Add(new KeyValuePair <string, Expression>("", (Expression)expression));
                    }
                }
            }
            else if (state.Code[index] == '{')
            {
                parseExportMap(result, state, ref index);
            }
            else
            {
                using (state.WithCodeContext(CodeContext.InExport))
                {
                    reexport = -1;
                    var definition =
                        VariableDefinition.Parse(state, ref index)
                        ?? ClassDefinition.Parse(state, ref index)
                        ?? FunctionDefinition.Parse(state, ref index, BaseLibrary.FunctionKind.Function)
                        ?? FunctionDefinition.Parse(state, ref index, BaseLibrary.FunctionKind.AsyncFunction);

                    if (definition == null)
                    {
                        ExceptionHelper.ThrowSyntaxError(Strings.UnexpectedToken, state.Code, index);
                    }

                    result._internalDefinition = definition;
                }
            }

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

            if (Parser.Validate(state.Code, "from", ref index))
            {
                if (reexport == -1)
                {
                    ExceptionHelper.ThrowSyntaxError("Reexport is not allowed with this syntax", state.Code, index - 4);
                }

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

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

                result._reexportSourceModuleName = Tools.Unescape(state.Code.Substring(start + 1, index - start - 2), false);
            }
            else if (reexport == 1)
            {
                ExceptionHelper.ThrowSyntaxError("Expected 'from'", state.Code, index);
            }

            return(result);
        }