Exemplo n.º 1
0
        /// <summary>
        /// Taken from TypeCobol CLI
        /// </summary>
        private TypeCobol.Compiler.CodeModel.SymbolTable loadCopies(List <string> copies)
        {
            var parser = new BuildParser(this, "TypeCobol.Server.loading");
            var table  = new SymbolTable(null, SymbolTable.Scope.Intrinsic);

            foreach (string path in copies)
            {
                parser.Init(path);
                parser.Parse(path);

                if (parser.Results.ProgramClassDocumentSnapshot == null)
                {
                    continue;
                }
                if (parser.Results.ProgramClassDocumentSnapshot.Program == null)
                {
                    Console.WriteLine("Error: Your Intrisic types are not included into a program.");
                    continue;
                }

                foreach (var type in parser.Results.ProgramClassDocumentSnapshot.Program.SymbolTable.CustomTypes)
                {
                    table.RegisterCustomType(type);//TODO check if already there
                }
            }
            return(table);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Taken from TypeCobol CLI
        /// </summary>
        private int 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 BuildParser(this, "TypeCobol.Server");

            parser.CustomSymbols = loadCopies(config.Copies);

            int iHasErrors = 0;

            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;
                }
                iHasErrors += 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;
                    }
                    writer.AddErrors(path, parser.Converter.GetDiagnostics(e));
                }

                if (config.Codegen)
                {
                    var codegen = new TypeCobol.Compiler.Generator.TypeCobolGenerator(parser.Source, config.Format, parser.Results.ProgramClassDocumentSnapshot);
                    if (codegen.IsValid)
                    {
                        var stream = new StreamWriter(config.OutputFiles[c]);
                        codegen.WriteCobol(stream);
                        System.Console.WriteLine("Code generated to file \"" + config.OutputFiles[c] + "\".");
                    }
                    else
                    {
                        // might be a problem regarding the input file format
                        AddError(writer, "Codegen failed for \"" + path + "\" (no Program). Check file format/encoding?", path);
                    }
                }
            }
            writer.Write();
            writer.Flush();
            return(iHasErrors);
        }