static NatashaCSharpEngine()
        {
            ErrorHandlers           = new ConcurrentDictionary <string, Func <Diagnostic, SyntaxBase, string, string> >();
            ErrorHandlers["CS0104"] = (diagnostic, syntax, sourceCode) =>
            {
                var(str1, str2) = CS0104Helper.Handler(diagnostic);
                var sets = syntax.ReferenceCache[diagnostic.Location.SourceTree.ToString()];
                if (sets.Contains(str1))
                {
                    if (sets.Contains(str2))
                    {
                        if (str2 == "System")
                        {
                            return(sourceCode.Replace($"using {str2};", ""));
                        }
                        else
                        {
                            return(sourceCode.Replace($"using {str1};", ""));
                        }
                    }
                    else
                    {
                        return(sourceCode.Replace($"using {str2};", ""));
                    }
                }
                else
                {
                    return(sourceCode.Replace($"using {str1};", ""));
                }
            };


            ErrorHandlers["CS0234"] = (diagnostic, syntax, sourceCode) =>
            {
                var tempResult = CS0234Helper.Handler(diagnostic);
                GlobalUsing.Remove(tempResult);
                return(Regex.Replace(sourceCode, $"using {tempResult}(.*?);", ""));
            };


            ErrorHandlers["CS0246"] = (diagnostic, syntax, sourceCode) =>
            {
                CS0246Helper.Handler(diagnostic);
                foreach (var @using in CS0246Helper.GetUsings(diagnostic, sourceCode))
                {
                    GlobalUsing.Remove(@using);
                    sourceCode = sourceCode.Replace($"using {@using};", "");
                }
                return(sourceCode);
            };
        }
        static NatashaCSharpEngine()
        {
            ErrorHandlers           = new ConcurrentDictionary <string, Action <CompilationException, Diagnostic, NatashaCSharpSyntax, Dictionary <string, string> > >();
            ErrorHandlers["CS0104"] = (exception, diagnostic, syntax, dict) =>
            {
                var(str1, str2) = CS0104Helper.Handler(diagnostic);
                var sets = syntax.UsingCache[exception.Formatter];
                if (sets.Contains(str1))
                {
                    if (sets.Contains(str2))
                    {
                        if (str2 == "System")
                        {
                            dict[exception.Formatter] = dict[exception.Formatter].Replace($"using {str2};", "");
                        }
                        else
                        {
                            dict[exception.Formatter] = dict[exception.Formatter].Replace($"using {str1};", "");
                        }
                    }
                    else
                    {
                        dict[exception.Formatter] = dict[exception.Formatter].Replace($"using {str2};", "");
                    }
                }
                else
                {
                    dict[exception.Formatter] = dict[exception.Formatter].Replace($"using {str1};", "");
                }
            };


            ErrorHandlers["CS0234"] = (exception, diagnostic, syntax, dict) =>
            {
                var tempResult = CS0234Helper.Handler(diagnostic);
                GlobalUsing.Remove(tempResult);
                dict[exception.Formatter] = Regex.Replace(dict[exception.Formatter], $"using {tempResult}(.*?);", "");
            };


            ErrorHandlers["CS0246"] = (exception, diagnostic, syntax, dict) =>
            {
                CS0246Helper.Handler(diagnostic);
                foreach (var @using in CS0246Helper.GetUsings(diagnostic, dict[exception.Formatter]))
                {
                    GlobalUsing.Remove(@using);
                    dict[exception.Formatter] = dict[exception.Formatter].Replace($"using {@using};", "");
                }
            };
        }
Beispiel #3
0
        /// <summary>
        /// 获取编译后的程序集
        /// </summary>
        /// <returns></returns>
        public Assembly GetAssembly()
        {
            if (!CheckSyntax())
            {
                return(null);
            }


            if (AssemblyName == default)
            {
                AssemblyName = Guid.NewGuid().ToString("N");
            }


            var      result   = StreamCompiler();
            Assembly assembly = result.Assembly;

            if (result.Compilation != null)
            {
                if (assembly == default || assembly == null)
                {
                    bool CS0104SHUT = false;
                    bool CS0234SHUT = false;
                    bool CSO246SHUT = false;


                    var          tempCache = SyntaxInfos.TreeCodeMapping;
                    SyntaxOption option    = new SyntaxOption();


                    foreach (var item in result.Errors)
                    {
                        if (item.Id == "CS0104")
                        {
                            WriteWarningLog(item);


                            CS0104SHUT = true;
                            var tempTree = item.Location.SourceTree;
                            var tempCode = tempCache[tempTree];
                            var(str1, str2) = CS0104Helper.Handler(item.Descriptor.MessageFormat.ToString(), item.GetMessage());
                            var sets = SyntaxInfos.TreeUsingMapping[tempTree];
                            if (sets.Contains(str1))
                            {
                                if (sets.Contains(str2))
                                {
                                    if (str2 == "System")
                                    {
                                        tempCache[tempTree] = tempCode.Replace($"using {str2};", "");
                                    }
                                    else
                                    {
                                        tempCache[tempTree] = tempCode.Replace($"using {str1};", "");
                                    }
                                }
                                else
                                {
                                    tempCache[tempTree] = tempCode.Replace($"using {str2};", "");
                                }
                            }
                            else
                            {
                                tempCache[tempTree] = tempCode.Replace($"using {str1};", "");
                            }
                        }
                        else if (item.Id == "CS0234")
                        {
                            WriteWarningLog(item);

                            CS0234SHUT = true;
                            var tempResult = CS0234Helper.Handler(item.Descriptor.MessageFormat.ToString(), item.GetMessage());
                            GlobalUsing.Remove(tempResult);
                            var tempTree = item.Location.SourceTree;
                            var tempCode = tempCache[tempTree];
                            tempCache[tempTree] = Regex.Replace(tempCode, $"using {tempResult}(.*?);", "");
                        }
                        else if (item.Id == "CS0246")
                        {
                            WriteWarningLog(item);


                            CSO246SHUT = true;
                            var tempTree = item.Location.SourceTree;
                            var tempCode = tempCache[tempTree];
                            var formart  = item.Descriptor.MessageFormat.ToString();
                            CS0246Helper.Handler(item.Descriptor.MessageFormat.ToString(), item.GetMessage());
                            foreach (var @using in CS0246Helper.GetUsings(formart, tempCode))
                            {
                                GlobalUsing.Remove(@using);
                                tempCache[tempTree] = tempCode.Replace($"using {@using};", "");
                            }
                        }
                    }


                    if (CS0104SHUT || CS0234SHUT || CSO246SHUT)
                    {
                        ErrorRetryCount += 1;
                        if (ErrorRetryCount < 2)
                        {
                            foreach (var item in tempCache)
                            {
                                option.Add(tempCache[item.Key], SyntaxInfos.TreeUsingMapping[item.Key]);
                            }

                            SyntaxInfos = option;
                            return(GetAssembly());
                        }
                    }


                    CompileException.Diagnostics.AddRange(result.Errors);
                    CompileException.ErrorFlag = CompileError.Compile;
                    CompileException.Message   = "发生错误,无法生成程序集!";


                    NErrorLog logError = null;
                    if (UseDetailLog)
                    {
                        logError = new NErrorLog();
                        logError.Handler(result.Compilation, CompileException.Diagnostics);
                        CompileException.Log = logError.Buffer.ToString();
                    }


                    if (NErrorLog.Enabled)
                    {
                        if (logError == default)
                        {
                            logError = new NErrorLog();
                            logError.Handler(result.Compilation, CompileException.Diagnostics);
                        }

                        logError.Write();
                    }


                    if (ErrorActionType == CompilerResultError.ThrowException)
                    {
                        throw new Exception(CompileException.Log);
                    }
                }
                else
                {
                    CompileException.ErrorFlag = CompileError.None;


                    NSucceedLog logSucceed = null;
                    if (UseDetailLog)
                    {
                        logSucceed = new NSucceedLog();
                        logSucceed.Handler(result.Compilation);
                        CompileException.Log = logSucceed.Buffer.ToString();
                    }


                    if (NSucceedLog.Enabled)
                    {
                        if (logSucceed == default)
                        {
                            logSucceed = new NSucceedLog();
                            logSucceed.Handler(result.Compilation);
                        }

                        logSucceed.Write();
                    }
                }
            }
            return(assembly);
        }
Beispiel #4
0
        /// <summary>
        /// 获取编译后的程序集
        /// </summary>
        /// <returns></returns>
        public Assembly GetAssembly()
        {
            if (SyntaxInfos.Trees.Count == 0)
            {
                return(null);
            }


            if (AssemblyName == default)
            {
                AssemblyName = Guid.NewGuid().ToString("N");
            }


            var      result   = StreamComplier();
            Assembly assembly = result.Assembly;

            if (result.Compilation != null)
            {
                if (assembly == default || assembly == null)
                {
                    bool         CS0104SHUT = false;
                    bool         CS0234SHUT = false;
                    bool         CSO246SHUT = false;
                    var          tempCache  = SyntaxInfos.TreeCodeMapping;
                    SyntaxOption option     = new SyntaxOption();


                    foreach (var item in result.Errors)
                    {
                        if (item.Id == "CS0104")
                        {
                            CS0104SHUT = true;
                            var tempTree = item.Location.SourceTree;
                            var tempCode = tempCache[tempTree];
                            var(str1, str2) = CS0104Helper.Handler(item.Descriptor.MessageFormat.ToString(), item.GetMessage());
                            var sets = SyntaxInfos.TreeUsingMapping[tempTree];
                            if (sets.Contains(str2))
                            {
                                tempCache[tempTree] = tempCode.Replace($"using {str1};", "");
                            }
                            else if (sets.Contains(str1))
                            {
                                tempCache[tempTree] = tempCode.Replace($"using {str2};", "");
                            }
                            else
                            {
                                tempCache[tempTree] = tempCode.Replace($"using {str1};", "");
                            }
                        }
                        else if (item.Id == "CS0234")
                        {
                            CS0234SHUT = true;
                            var tempResult = CS0234Helper.Handler(item.Descriptor.MessageFormat.ToString(), item.GetMessage());
                            UsingDefaultCache.Remove(tempResult);
                            var tempTree = item.Location.SourceTree;
                            var tempCode = tempCache[tempTree];
                            tempCache[tempTree] = tempCode.Replace($"using {tempResult};", "");
                        }
                        else if (item.Id == "CS0246")
                        {
                            CSO246SHUT = true;
                            var tempTree = item.Location.SourceTree;
                            var tempCode = tempCache[tempTree];
                            var formart  = item.Descriptor.MessageFormat.ToString();
                            CS0246Helper.Handler(item.Descriptor.MessageFormat.ToString(), item.GetMessage());
                            foreach (var @using in CS0246Helper.GetUsings(formart, tempCode))
                            {
                                UsingDefaultCache.Remove(@using);
                                tempCache[tempTree] = tempCode.Replace($"using {@using};", "");
                            }
                        }
                    }


                    if (CS0104SHUT || CS0234SHUT || CSO246SHUT)
                    {
                        ErrorRetryCount += 1;
                        if (ErrorRetryCount < 2)
                        {
                            foreach (var item in tempCache)
                            {
                                option.Add(tempCache[item.Key], SyntaxInfos.TreeUsingMapping[item.Key]);
                            }

                            SyntaxInfos = option;
                            return(GetAssembly());
                        }
                    }


                    ComplieException.Diagnostics.AddRange(result.Errors);
                    ComplieException.ErrorFlag = ComplieError.Complie;
                    ComplieException.Message   = "发生错误,无法生成程序集!";


                    NError logError = new NError();
                    logError.Handler(result.Compilation, ComplieException.Diagnostics);
                    ComplieException.Log = logError.Buffer.ToString();
                    if (NError.Enabled)
                    {
                        logError.Write();
                    }
                }
                else
                {
                    if (_domain != DomainManagment.Default)
                    {
                        DomainCache.Add(result.Assembly, _domain);
                    }

                    NSucceed logSucceed = new NSucceed();
                    logSucceed.Handler(result.Compilation);
                    ComplieException.ErrorFlag = ComplieError.None;
                    ComplieException.Log       = logSucceed.Buffer.ToString();
                    if (NSucceed.Enabled)
                    {
                        logSucceed.Write();
                    }
                }
            }
            return(assembly);
        }