Beispiel #1
0
        private static int WriteCSharpSourceFiles(
            string inputFile,
            bool writeSource,
            bool writeTests,
            bool writeSignatures,
            string outputFile
            )
        {
            var tree = ReadTree(inputFile);

            // The syntax.xml doc contains some nodes that are useful for other tools, but which are
            // not needed by this syntax generator.  Specifically, we have `<Choice>` and
            // `<Sequence>` nodes in the xml file to help others tools understand the relationship
            // between some fields (i.e. 'only one of these children can be non-null').  To make our
            // life easier, we just flatten all those nodes, grabbing all the nested `<Field>` nodes
            // and placing into a single linear list that we can then process.
            TreeFlattening.FlattenChildren(tree);

            if (writeSignatures)
            {
                SignatureWriter.Write(Console.Out, tree);
            }
            else
            {
                if (writeSource)
                {
                    var outputPath         = outputFile.Trim('"');
                    var prefix             = Path.GetFileName(inputFile);
                    var outputMainFile     = Path.Combine(outputPath, $"{prefix}.Main.Generated.cs");
                    var outputInternalFile = Path.Combine(
                        outputPath,
                        $"{prefix}.Internal.Generated.cs"
                        );
                    var outputSyntaxFile = Path.Combine(
                        outputPath,
                        $"{prefix}.Syntax.Generated.cs"
                        );

                    WriteToFile(writer => SourceWriter.WriteMain(writer, tree), outputMainFile);
                    WriteToFile(
                        writer => SourceWriter.WriteInternal(writer, tree),
                        outputInternalFile
                        );
                    WriteToFile(writer => SourceWriter.WriteSyntax(writer, tree), outputSyntaxFile);
                }
                if (writeTests)
                {
                    WriteToFile(writer => TestWriter.Write(writer, tree), outputFile);
                }
            }

            return(0);
        }
Beispiel #2
0
        public static int Main(string[] args)
        {
            if (args.Length < 2 || args.Length > 3)
            {
                WriteUsage();
                return(1);
            }

            string inputFile = args[0];

            if (!File.Exists(inputFile))
            {
                Console.WriteLine(inputFile + " not found.");
                return(1);
            }

            bool   writeSource     = true;
            bool   writeTests      = false;
            bool   writeSignatures = false;
            string outputFile      = null;

            if (args.Length == 3)
            {
                outputFile = args[1];

                if (args[2] == "/test")
                {
                    writeTests  = true;
                    writeSource = false;
                }
                else
                {
                    WriteUsage();
                    return(1);
                }
            }
            else if (args.Length == 2)
            {
                if (args[1] == "/sig")
                {
                    writeSignatures = true;
                }
                else
                {
                    outputFile = args[1];
                }
            }

            var reader = XmlReader.Create(inputFile, new XmlReaderSettings {
                DtdProcessing = DtdProcessing.Prohibit
            });
            var  serializer = new XmlSerializer(typeof(Tree));
            Tree tree       = (Tree)serializer.Deserialize(reader);

            if (writeSignatures)
            {
                SignatureWriter.Write(Console.Out, tree);
            }
            else
            {
                if (writeSource)
                {
                    var outputPath         = outputFile.Trim('"');
                    var prefix             = Path.GetFileName(inputFile);
                    var outputMainFile     = Path.Combine(outputPath, $"{prefix}.Main.Generated.cs");
                    var outputInternalFile = Path.Combine(outputPath, $"{prefix}.Internal.Generated.cs");
                    var outputSyntaxFile   = Path.Combine(outputPath, $"{prefix}.Syntax.Generated.cs");

                    WriteToFile(tree, SourceWriter.WriteMain, outputMainFile);
                    WriteToFile(tree, SourceWriter.WriteInternal, outputInternalFile);
                    WriteToFile(tree, SourceWriter.WriteSyntax, outputSyntaxFile);
                }
                if (writeTests)
                {
                    WriteToFile(tree, TestWriter.Write, outputFile);
                }
            }

            return(0);
        }
Beispiel #3
0
        public static void Main(string[] args)
        {
            if (args.Length < 2 || args.Length > 3)
            {
                WriteUsage();
                return;
            }

            string inputFile = args[0];

            if (!File.Exists(inputFile))
            {
                Console.WriteLine(inputFile + " not found.");
                return;
            }

            bool   writeSource     = true;
            bool   writeTests      = false;
            bool   writeSignatures = false;
            string outputFile      = null;

            if (args.Length == 3)
            {
                outputFile = args[1];

                if (args[2] == "/test")
                {
                    writeTests  = true;
                    writeSource = false;
                }
                else
                {
                    WriteUsage();
                    return;
                }
            }
            else if (args.Length == 2)
            {
                if (args[1] == "/sig")
                {
                    writeSignatures = true;
                }
                else
                {
                    outputFile = args[1];
                }
            }

            var reader = XmlReader.Create(inputFile, new XmlReaderSettings {
                DtdProcessing = DtdProcessing.Prohibit
            });
            var  serializer = new XmlSerializer(typeof(Tree));
            Tree tree       = (Tree)serializer.Deserialize(reader);

            if (writeSignatures)
            {
                SignatureWriter.Write(Console.Out, tree);
            }
            else
            {
                if (writeSource)
                {
                    WriteToFile(tree, SourceWriter.Write, outputFile);
                }
                if (writeTests)
                {
                    WriteToFile(tree, TestWriter.Write, outputFile);
                }
            }
        }