Example #1
0
        public CompilationException Add(SyntaxTree node)
        {
            CompilationException exception = new CompilationException();

            var(tree, formartter, errors) = node;


            exception.Formatter = formartter;
            exception.Diagnostics.AddRange(errors);


            if (exception.Diagnostics.Count != 0)
            {
                exception.ErrorFlag = CompileError.Syntax;
                exception.Message   = "语法错误,请仔细检查脚本代码!";
                NErrorLog log = new NErrorLog();
                log.Handler(exception.Diagnostics);
                if (NErrorLog.Enabled)
                {
                    log.Write();
                }
                exception.Log = log.Buffer.ToString();
            }
            else
            {
                TreeCodeMapping[tree]  = tree.ToString();
                TreeUsingMapping[tree] = default;
            }
            SyntaxExceptions.Add(exception);
            return(exception);
        }
Example #2
0
 public static void ErrorRecoder(CSharpCompilation compilation, List <CompilationException> exceptions)
 {
     if (NErrorLog.Enabled)
     {
         NErrorLog log = new NErrorLog();
         foreach (var item in exceptions)
         {
             log.Handler(compilation, item.Diagnostics);
         }
         log.Write();
     }
 }
Example #3
0
 public static void ErrorRecoder(params CompilationException[] exceptions)
 {
     if (NErrorLog.Enabled)
     {
         NErrorLog log = new NErrorLog();
         foreach (var item in exceptions)
         {
             log.Handler(item.Diagnostics);
         }
         log.Write();
     }
 }
Example #4
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);
        }