public IUavcanTypeResolver Create(string testScheme) { _sourceText = testScheme; var grammar = new TestSchemeFileGrammar(); var parser = new Parser(grammar); var tree = parser.Parse(testScheme); if (tree.Status != ParseTreeStatus.Parsed) { var sb = new StringBuilder(); sb.AppendLine($"Cannot parse test scheme file."); foreach (var msg in tree.ParserMessages) { sb.AppendLine($"{msg} at {msg.Location}."); } throw new InvalidOperationException(sb.ToString()); } CollectTestTypes(tree.Root); return(new StringUavcanTypeResolver(_types.Select(x => (x.Meta, x.Body)))); }
static void Main(string[] args) { const string pyuavcanPath = @"C:\Sources\pyuavcan"; var pyVersion = GetPythonVersion(pyuavcanPath); if (pyVersion == null) { throw new InvalidOperationException("Cannot execute Python."); } Console.WriteLine($"Python {pyVersion}"); var dsdlRoot = Path.Combine(pyuavcanPath, @"uavcan\dsdl_files"); var dsdlRootCopy = IOUtils.GetTemporaryDirectory(); IOUtils.DirectoryCopy(dsdlRoot, dsdlRootCopy, true); try { var root = Assembly.GetExecutingAssembly().Location; while (Path.GetFileName(root) != "SerializationTestGenerator") { root = Path.GetDirectoryName(root); } var testsTextPath = Path.Combine(root, "Tests.txt"); var testsText = File.ReadAllText(testsTextPath); var grammar = new TestSchemeFileGrammar(); var parser = new Parser(grammar); var tree = parser.Parse(testsText); if (tree.Status != ParseTreeStatus.Parsed) { Console.WriteLine($"Cannot parse source file {testsTextPath}."); foreach (var msg in tree.ParserMessages) { Console.WriteLine($"{msg} at {msg.Location}."); } return; } Directory.CreateDirectory(dsdlRoot); var schemePath = Path.Combine(root, @"..\Uavcan.NET.Dsdl.Testing\Serialization\AutogeneratedTests\Scheme.txt"); var typesPath = Path.Combine(root, @"..\Uavcan.NET.Dsdl.Testing\Serialization\AutogeneratedTests\Types.cs"); var testsCsPath = Path.Combine(root, @"..\Uavcan.NET.Dsdl.Testing\Serialization\AutogeneratedTests\Tests.cs"); var prog = new Program { SourceText = testsText, PyUavcanDirectory = pyuavcanPath, }; prog.CollectTestTypes(tree.Root); prog.CompileDsdlTypes(); prog.BuildCompoundTypesLookup(); prog.CreateCSharpNames(); prog.BuildScheme(schemePath); prog.BuildDsdlDefinitions(dsdlRoot); prog.BuildTypes(typesPath); prog.BuildTestMethods(testsCsPath); } finally { IOUtils.DirectoryDelete(dsdlRoot); IOUtils.DirectoryCopy(dsdlRootCopy, dsdlRoot, true); IOUtils.DirectoryDelete(dsdlRootCopy); } }