public static void Check_DocumentFormatExceptions() { bool exceptionOK = false; try { DocumentFormat docFormat = new DocumentFormat(Encoding.Default, EndOfLineDelimiter.FixedLengthLines, 0, ColumnsLayout.FreeTextFormat); } catch (Exception e) { if (e is ArgumentException) exceptionOK = true; } if (!exceptionOK) { throw new Exception("Free text format with fixed length lines should not be allowed"); } exceptionOK = false; try { DocumentFormat docFormat = new DocumentFormat(Encoding.Default, EndOfLineDelimiter.FixedLengthLines, 71, ColumnsLayout.CobolReferenceFormat); } catch (Exception e) { if (e is ArgumentException) exceptionOK = true; } if (!exceptionOK) { throw new Exception("Cobol reference text format with fixed length lines < 72 should not be allowed"); } // But Cobol reference text format with fixed length lines == 72 should be allowed DocumentFormat docFormat2 = new DocumentFormat(Encoding.Default, EndOfLineDelimiter.FixedLengthLines, 72, ColumnsLayout.CobolReferenceFormat); }
public static void ParseGenerateCompare(string path, List<Skeleton> skeletons, DocumentFormat format) { var document = Parser.Parse(Path.Combine(ROOT, INPUT, path), format); var columns = document.Results.ProgramClassDocumentSnapshot.TextSourceInfo.ColumnsLayout; var writer = new StringWriter(); // write parsing errors WriteErrors(writer, document.Errors[0], "CodeElements", columns); WriteErrors(writer, document.Errors[1], "ProgramClass", columns); // write generated code var codegen = new Generator(writer, document.Results.TokensLines, skeletons); var program = document.Results.ProgramClassDocumentSnapshot.Program; codegen.Generate(program.SyntaxTree.Root, program.SymbolTable, columns); // flush writer.Close(); // compare with expected result string expected = File.ReadAllText(Path.Combine(ROOT, OUTPUT, path), format.Encoding); TypeCobol.Test.TestUtils.compareLines(path, writer.ToString(), expected); }
private static SymbolTable LoadCopies(AbstractErrorWriter writer, List<string> paths, DocumentFormat copyDocumentFormat) { var parser = new Parser(); var table = new SymbolTable(null, SymbolTable.Scope.Intrinsic); var copies = new List<string>(); foreach(string path in paths) copies.AddRange(Tools.FileSystem.GetFiles(path, parser.Extensions, false)); foreach(string path in copies) { try { parser.Init(path, copyDocumentFormat); parser.Parse(path); foreach (var diagnostic in parser.Results.CodeElementsDocumentSnapshot.ParserDiagnostics) { AddError(writer, "Syntax error during parsing of " + path + ": " + diagnostic, path, "intrinsicLoading"); } if (parser.Results.ProgramClassDocumentSnapshot == null) continue; foreach (var diagnostic in parser.Results.ProgramClassDocumentSnapshot.Diagnostics) { AddError(writer, "Semantic error during parsing of " + path + ": " + diagnostic, path, "intrinsicLoading"); } if (parser.Results.ProgramClassDocumentSnapshot.Program == null) { AddError(writer, "Error: Your Intrisic types/functions are not included into a program.", path, "intrinsicLoading"); continue; } var symbols = parser.Results.ProgramClassDocumentSnapshot.Program.SymbolTable; foreach (var types in symbols.Types) foreach (var type in types.Value) table.AddType((Compiler.Nodes.TypeDefinition) type); foreach (var functions in symbols.Functions) foreach (var function in functions.Value) table.AddFunction((Compiler.Nodes.FunctionDeclaration) function); //TODO check if types or functions are already there } catch (Exception e) { AddError(writer, e.Message, path, "intrinsicLoading"); } } return table; }
// public static void Main(string[] args) public static void main(string[] args) { // Directory where the source programs and COPY files are stored string sourcePath = @"D:\Users\Laurent\OneDrive\Dev\Visual Studio 2012\Projects\TypeCobol\TypeCobol.Test\Samples\EI Cobol samples\EI-Production"; string[] programExtensions = { "*.PGM" }; string[] copyExtensions = { "*.CPY" }; // List of all sample programs used to compute the statistics IList<string> textNames = new List<string>(); foreach (string programExtension in programExtensions) { foreach (string filePath in Directory.EnumerateFiles(sourcePath, programExtension)) { string textName = Path.GetFileNameWithoutExtension(filePath); textNames.Add(textName); } } // Source file format for the samples DocumentFormat docFormat = new DocumentFormat(Encoding.GetEncoding("iso8859-1"), EndOfLineDelimiter.CrLfCharacters, 80, ColumnsLayout.CobolReferenceFormat); // Initialize a compilation project TypeCobolOptions compilerOptions = new TypeCobolOptions(); CompilationProject project = new CompilationProject("samples", sourcePath, programExtensions.Concat(copyExtensions).ToArray(), docFormat.Encoding, docFormat.EndOfLineDelimiter, docFormat.FixedLineLength, docFormat.ColumnsLayout, compilerOptions); // Output files used to store the results string resultFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string countersFile = Path.Combine(resultFilePath, "SyntaxCounters.txt"); string languageModelForProgramFile = Path.Combine(resultFilePath, "LanguageModel.Program.txt"); string languageModelForCopyFile = Path.Combine(resultFilePath, "LanguageModel.Copy.txt"); // Compute statistics Stopwatch chrono = new Stopwatch(); chrono.Start(); StatsGenerator.GenerateStatisticsForPrograms(project, textNames, Console.Out, countersFile, languageModelForProgramFile, languageModelForCopyFile); chrono.Stop(); Console.WriteLine(""); Console.WriteLine("Programs analyzed in " + Math.Round(chrono.ElapsedMilliseconds / (double)1000, 3) + " sec"); }
public static void Main(string[] args) { // Basic test program, useful to debug : compiles all sample programs located under TypeCobol.Test\Samples\EI Cobol samples\EI-Production" string currentDirectory = Directory.GetCurrentDirectory(); string projectRootPath = currentDirectory.Substring(0, currentDirectory.IndexOf(@"\TypeCobol\") + 11); string sourcePath = projectRootPath + @"TypeCobol.Test\Samples\EI Cobol samples\EI-Production"; string[] programExtensions = { "*.PGM" }; string[] copyExtensions = { "*.CPY" }; DocumentFormat docFormat = new DocumentFormat(Encoding.GetEncoding("iso8859-1"), EndOfLineDelimiter.CrLfCharacters, 80, ColumnsLayout.CobolReferenceFormat); TypeCobolOptions compilerOptions = new TypeCobolOptions(); CompilationProject project = new CompilationProject("samples", sourcePath, programExtensions.Concat(copyExtensions).ToArray(), docFormat.Encoding, docFormat.EndOfLineDelimiter, docFormat.FixedLineLength, docFormat.ColumnsLayout, compilerOptions); // Iterate over all programs in the source directory foreach (string programExtension in programExtensions) { foreach (string filePath in Directory.EnumerateFiles(sourcePath, programExtension)) { // Compile program string textName = Path.GetFileNameWithoutExtension(filePath); Console.Write(textName + " ... "); try { FileCompiler fileCompiler = new FileCompiler(null, textName, project.SourceFileProvider, project, ColumnsLayout.CobolReferenceFormat, compilerOptions.Clone(), null, false); fileCompiler.CompileOnce(); Console.WriteLine(" OK"); } catch(Exception e) { Console.WriteLine("error :"); Console.WriteLine(e.Message); } } } /* // TO DO : read compiler options on the command line // Start with the default compiler options TypeCobolOptions compilerOptions = new TypeCobolOptions(); // Simple test version // - all referenced files should be located under the current directory CompilationProject project = new CompilationProject("project", ".", new string[] { "*.cbl", "*.cpy" }, IBMCodePages.GetDotNetEncodingFromIBMCCSID(1147), EndOfLineDelimiter.FixedLengthLines, 80, ColumnsLayout.CobolReferenceFormat, compilerOptions); // - gets one file name as argument and compiles it if (args.Length == 1) { string textName = args[0]; FileCompiler fileCompiler = new FileCompiler(null, textName, project.SourceFileProvider, project, ColumnsLayout.CobolReferenceFormat, compilerOptions.Clone(), false); fileCompiler.CompileOnce(); } // - gets an optional "-continuous" flag as the first argument to activate source file monitoring and automatic recompilation else if (args.Length == 2) { if (String.Equals(args[0], "-continuous", StringComparison.InvariantCultureIgnoreCase)) { string textName = args[1]; FileCompiler fileCompiler = new FileCompiler(null, textName, project.SourceFileProvider, project, ColumnsLayout.CobolReferenceFormat, compilerOptions.Clone(), false); fileCompiler.StartContinuousBackgroundCompilation(400, 400, 900, 2000); Console.WriteLine("Processing, press enter to stop ..."); fileCompiler.StartContinuousFileProcessing(); Console.ReadLine(); } else { Console.WriteLine("ERROR : Invalid option"); } } else { Console.WriteLine("ERROR : Invalid number of arguments"); } */ }
public static void Check_UTF8File() { DocumentFormat docFormat = new DocumentFormat(Encoding.UTF8, EndOfLineDelimiter.CrLfCharacters, 0, ColumnsLayout.FreeTextFormat); SourceFileProvider fileProvider = new SourceFileProvider(); fileProvider.AddLocalDirectoryLibrary( PlatformUtils.GetPathForProjectFile(@"Compiler\File\Samples"), false, null, docFormat.Encoding, docFormat.EndOfLineDelimiter, docFormat.FixedLineLength); DummyTextSourceListener textSourceListener = new DummyTextSourceListener(); string filename = "UTF8Format.txt"; CobolFile cobolFile; if (fileProvider.TryGetFile(filename, out cobolFile)) { // Load the CobolFile in a TextDocument ReadOnlyTextDocument textDocument = new ReadOnlyTextDocument(filename, docFormat.Encoding, docFormat.ColumnsLayout, cobolFile.ReadChars()); // Send all text lines in one batch to the test observer textDocument.TextChanged += textSourceListener.OnTextChanged; textDocument.StartSendingChangeEvents(); } TextChangeMap tce; tce = new TextChangeMap(textSourceListener.LastTextChangedEvent.TextChanges.First<TextChange>(), docFormat.ColumnsLayout); CheckLine(filename, tce, 0, "english: hello, world"); tce = new TextChangeMap(textSourceListener.LastTextChangedEvent.TextChanges[1], docFormat.ColumnsLayout); bool okay = false; try { CheckLine(filename, tce, 1, "français: salut, tout le monde"); } catch (Exception) { okay = true; } if (!okay) throw new Exception("Exception should have been thrown!"); CheckLine(filename, tce, 1, "arabic: مرحبا بالعالم"); tce = new TextChangeMap(textSourceListener.LastTextChangedEvent.TextChanges[2], docFormat.ColumnsLayout); CheckLine(filename, tce, 2, "japanese: こんにちは世界"); }