private static void runOnce2(Config config) { TextWriter w; if (config.ErrorFile == null) { w = Console.Error; } else { w = File.CreateText(config.ErrorFile); } AbstractErrorWriter errorWriter; if (config.IsErrorXML) { errorWriter = new XMLWriter(w); } else { errorWriter = new ConsoleWriter(w); } errorWriter.Outputs = config.OutputFiles; var parser = new Parser(); parser.CustomSymbols = LoadCopies(errorWriter, config.Copies, config.Format); //Load of the intrinsics parser.CustomSymbols = LoadDependencies(errorWriter, config.Dependencies, config.Format, parser.CustomSymbols); //Load of the dependency files for (int c = 0; c < config.InputFiles.Count; c++) { string path = config.InputFiles[c]; try { var typeCobolOptions = new TypeCobolOptions { HaltOnMissingCopy = config.HaltOnMissingCopyFilePath != null, ExecToStep = config.ExecToStep, }; #if EUROINFO_RULES typeCobolOptions.AutoRemarksEnable = config.AutoRemarks; #endif parser.Init(path, typeCobolOptions, config.Format, config.CopyFolders); } catch (Exception ex) { Server.AddError(errorWriter, MessageCode.ParserInit, ex.Message, path); continue; } parser.Parse(path); if (!string.IsNullOrEmpty(config.HaltOnMissingCopyFilePath)) { if (parser.MissingCopys.Count > 0) { //Write in the specified file all the absent copys detected File.WriteAllLines(config.HaltOnMissingCopyFilePath, parser.MissingCopys); } else { //Delete the file File.Delete(config.HaltOnMissingCopyFilePath); } } if (parser.Results.CodeElementsDocumentSnapshot == null && config.ExecToStep > ExecutionStep.Preprocessor) { Server.AddError(errorWriter, MessageCode.SyntaxErrorInParser, "File \"" + path + "\" has syntactic error(s) preventing codegen (CodeElements).", path); continue; } else if (parser.Results.ProgramClassDocumentSnapshot == null && config.ExecToStep > ExecutionStep.SyntaxCheck) { Server.AddError(errorWriter, MessageCode.SyntaxErrorInParser, "File \"" + path + "\" has semantic error(s) preventing codegen (ProgramClass).", path); continue; } var allDiags = parser.Results.AllDiagnostics(); int errors = allDiags.Count; errorWriter.AddErrors(path, allDiags); if (config.ExecToStep >= ExecutionStep.Generate && errors == 0) { var skeletons = TypeCobol.Codegen.Config.Config.Parse(config.skeletonPath); var codegen = new TypeCobol.Codegen.Generators.DefaultGenerator(parser.Results, new StreamWriter(config.OutputFiles[c]), skeletons); var program = parser.Results.ProgramClassDocumentSnapshot.Program; codegen.Generate(program.SyntaxTree.Root, program.SymbolTable, ColumnsLayout.CobolReferenceFormat); } } errorWriter.Write(); errorWriter.FlushAndClose(); errorWriter = null; //as w can be a Text file created, we need to close it w.Close(); }
private static void runOnce(Config config) { TextWriter w; if (config.ErrorFile == null) w = System.Console.Error; else w = File.CreateText(config.ErrorFile); AbstractErrorWriter writer; if (config.IsErrorXML) writer = new XMLWriter(w); else writer = new ConsoleWriter(w); writer.Outputs = config.OutputFiles; var parser = new Parser(); parser.CustomSymbols = loadCopies(config.Copies); for(int c=0; c<config.InputFiles.Count; c++) { string path = config.InputFiles[c]; try { parser.Init(path, config.Format); } catch(System.IO.IOException ex) { AddError(writer, ex.Message, path); continue; } parser.Parse(path); if (parser.Results.CodeElementsDocumentSnapshot == null) { AddError(writer, "File \""+path+"\" has syntactic error(s) preventing codegen (CodeElements).", path); continue; } writer.AddErrors(path, parser.Converter.AsDiagnostics(parser.Results.CodeElementsDocumentSnapshot.ParserDiagnostics)); // no need to add errors from parser.CodeElementsSnapshot.CodeElements // as they are on parser.CodeElementsSnapshot.CodeElements which are added below if (parser.Results.ProgramClassDocumentSnapshot == null) { AddError(writer, "File \""+path+"\" has semantic error(s) preventing codegen (ProgramClass).", path); continue; } int errors = 0; errors += new List<Compiler.Diagnostics.Diagnostic>(parser.Results.CodeElementsDocumentSnapshot.ParserDiagnostics).Count; errors += parser.Results.ProgramClassDocumentSnapshot.Diagnostics.Count; writer.AddErrors(path, parser.Converter.AsDiagnostics(parser.Results.ProgramClassDocumentSnapshot.Diagnostics)); foreach(var e in parser.Results.CodeElementsDocumentSnapshot.CodeElements) { if (e.Diagnostics.Count < 1) continue; errors += e.Diagnostics.Count; writer.AddErrors(path, parser.Converter.GetDiagnostics(e)); } if (config.Codegen && (errors == 0 || !config.Safe)) { var skeletons = TypeCobol.Codegen.Config.Config.Parse(config.skeletonPath); var codegen = new TypeCobol.Codegen.Generator(new StreamWriter(config.OutputFiles[c]), parser.Results.TokensLines, skeletons); var program = parser.Results.ProgramClassDocumentSnapshot.Program; codegen.Generate(program.SyntaxTree.Root, program.SymbolTable, ColumnsLayout.CobolReferenceFormat); } } writer.Write(); writer.Flush(); }
/// <summary> /// runOnce method to parse the input file(s). /// </summary> /// <param name="config">Config</param> internal static ReturnCode runOnce(TypeCobolConfiguration config) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); string debugLine = DateTime.Now + " start parsing of "; if (config.InputFiles.Count > 0) { debugLine += Path.GetFileName(config.InputFiles[0]); } debugLine += "\n"; File.AppendAllText("TypeCobol.CLI.log", debugLine); Console.WriteLine(debugLine); TextWriter textWriter; if (config.ErrorFile == null) { textWriter = Console.Error; } else { textWriter = File.CreateText(config.ErrorFile); } AbstractErrorWriter errorWriter; if (config.IsErrorXML) { errorWriter = new XMLWriter(textWriter); } else { errorWriter = new ConsoleWriter(textWriter); } errorWriter.Outputs = config.OutputFiles; //Call the runOnce2() Methode and manage all the different kinds of exception. ReturnCode returnCode; try { returnCode = runOnce2(config, errorWriter); } catch (TypeCobolException typeCobolException)//Catch managed exceptions { AnalyticsWrapper.Telemetry.TrackException(typeCobolException); if (typeCobolException.NeedMail) { AnalyticsWrapper.Telemetry.SendMail(typeCobolException, config.InputFiles, config.CopyFolders, config.CommandLine); } if (typeCobolException.Logged) { Server.AddError(errorWriter, typeCobolException.MessageCode, typeCobolException.ColumnStartIndex, typeCobolException.ColumnEndIndex, typeCobolException.LineNumber, typeCobolException.Message + "\n" + typeCobolException.StackTrace, typeCobolException.Path); } if (typeCobolException is PresenceOfDiagnostics) { returnCode = ReturnCode.ParsingDiagnostics; } else if (typeCobolException is ParsingException) { returnCode = ReturnCode.FatalError; } else if (typeCobolException is GenerationException) { returnCode = ReturnCode.GenerationError; } else if (typeCobolException is MissingCopyException) { returnCode = ReturnCode.MissingCopy; } else { returnCode = ReturnCode.FatalError; //Just in case.. } } catch (Exception e)//Catch any other exception { AnalyticsWrapper.Telemetry.TrackException(e); AnalyticsWrapper.Telemetry.SendMail(e, config.InputFiles, config.CopyFolders, config.CommandLine); Server.AddError(errorWriter, MessageCode.SyntaxErrorInParser, e.Message + e.StackTrace, string.Empty); returnCode = ReturnCode.FatalError; } errorWriter.Write(returnCode); errorWriter.FlushAndClose(); errorWriter = null; //as textWriter can be a Text file created, we need to close it textWriter.Close(); stopWatch.Stop(); debugLine = " parsed in " + stopWatch.Elapsed + " ms\n"; File.AppendAllText("TypeCobol.CLI.log", debugLine); Console.WriteLine(debugLine); AnalyticsWrapper.Telemetry.TrackEvent("[Duration] Execution Time", EventType.Genration, new Dictionary <string, string> { { "Duration", "Duration" } }, //Custom properties for metrics new Dictionary <string, double> { { "ExecutionTime", stopWatch.Elapsed.Milliseconds } }); //Metrics fo duration return(returnCode); }