Beispiel #1
0
        private static SyntaxTree MakeProgram(string sourceFilePath, IOKind ioKind, string methodName, string className, string namespaceName)
        {
            ParsingResult result = Parsing.ParseFromFile(sourceFilePath);

            if (!result.Success)
            {
                throw new Exception("Syntax error in source file.");
            }
            else
            {
                if (!result.ParsedAll)
                {
                    Console.WriteLine("Could not parse the whole source file, output might not work as intended.");
                }

                MethodDeclarationSyntax method = Exprs.Method(methodName, ioKind, result.SyntaxGenerators);

                if (ioKind == IOKind.Argument)
                {
                    method = method.WithParameterList(SyntaxFactory.ParseParameterList("(IEnumerable<byte> input)"));
                }

                ClassDeclarationSyntax @class = Exprs.Class(className);
                @class = @class.WithMembers(SyntaxFactory.SingletonList <MemberDeclarationSyntax>(method));

                NamespaceDeclarationSyntax @namespace = Exprs.Namespace(namespaceName);
                @namespace = @namespace.WithMembers(SyntaxFactory.SingletonList <MemberDeclarationSyntax>(@class));

                CompilationUnitSyntax cu = SyntaxFactory.CompilationUnit().AddMembers(@namespace);

                return(SyntaxFactory.SyntaxTree(cu, path: sourceFilePath));
            }
        }