public static void WriteTo(StreamWriter stream) { using (var output = new IndentedWriter(stream)) { output.Line( $@"// Copyright (c) {DateTime.Today.Year}, David Aramant // Distributed under the 3-clause BSD license. For full terms see the file LICENSE. using System.CodeDom.Compiler; using UdmfParsing.Udmf.Parsing.SuperpowerVersion.AbstractSyntaxTree; namespace UdmfParsing.Udmf.Parsing.SuperpowerVersion").OpenParen() .Line($"[GeneratedCode(\"{CurrentLibraryInfo.Name}\", \"{CurrentLibraryInfo.Version}\")]") .Line("public static partial class UdmfSemanticAnalyzer").OpenParen(); WriteGlobalFieldParsing(output); output.Line(); WriteBlockParsing(output); output.Line(); foreach (var block in UdmfDefinitions.Blocks.Where(b => b.IsSubBlock)) { WriteBlockParser(block, output); } output.CloseParen(); output.CloseParen(); } }
public static void WriteToPath(string basePath) { if (!Directory.Exists(basePath)) { Directory.CreateDirectory(basePath); } using var stream = File.CreateText(Path.Combine(basePath, "UdmfSemanticAnalyzer.Generated.cs")); using var output = new IndentedWriter(stream); var includes = new[] { "System.CodeDom.Compiler", "System.Collections.Generic", "System.Collections.Immutable", "Tiledriver.Core.FormatModels.Common", "Tiledriver.Core.FormatModels.Common.Reading", "Tiledriver.Core.FormatModels.Common.Reading.AbstractSyntaxTree", }; output .WriteHeader("Tiledriver.Core.FormatModels.Udmf.Reading", includes) .Line($"[GeneratedCode(\"{CurrentLibraryInfo.Name}\", \"{CurrentLibraryInfo.Version}\")]") .Line($"public static partial class UdmfSemanticAnalyzer") .OpenParen(); foreach (var block in UdmfDefinitions.Blocks.Where(b => b.Serialization == SerializationType.Normal)) { CreateBlockReader(output, block); } CreateGlobalBlockReader(output, UdmfDefinitions.Blocks.Single(b => b.Serialization == SerializationType.TopLevel)); output.CloseParen(); }
public static void WriteToPath(string basePath) { if (!Directory.Exists(basePath)) { Directory.CreateDirectory(basePath); } using var stream = File.CreateText(Path.Combine(basePath, "UdmfWriter.Generated.cs")); using var output = new IndentedWriter(stream); output .WriteHeader("Tiledriver.Core.FormatModels.Udmf.Writing", new[] { "System.CodeDom.Compiler", "System.IO", "Tiledriver.Core.FormatModels.Common" }) .Line($"[GeneratedCode(\"{CurrentLibraryInfo.Name}\", \"{CurrentLibraryInfo.Version}\")]") .Line($"public static partial class UdmfWriter") .OpenParen(); foreach (var block in UdmfDefinitions.Blocks.Where(b => b.Serialization != SerializationType.Custom)) { CreateBlockWriter(output, block); } output.CloseParen(); }
public static void WriteToPath(string basePath) { if (!Directory.Exists(basePath)) { Directory.CreateDirectory(basePath); } using var stream = File.CreateText(Path.Combine(basePath, "MapDeclarationParser.Generated.cs")); using var output = new IndentedWriter(stream); var includes = new[] { "System.CodeDom.Compiler", "System.Linq", "Tiledriver.Core.FormatModels.Common", "Tiledriver.Core.FormatModels.MapInfo.Reading.AbstractSyntaxTree", }; output .Line("#nullable enable") .WriteHeader("Tiledriver.Core.FormatModels.MapInfo.Reading", includes) .Line($"[GeneratedCode(\"{CurrentLibraryInfo.Name}\", \"{CurrentLibraryInfo.Version}\")]") .Line($"public static partial class MapDeclarationParser") .OpenParen(); WriteParser(output, MapInfoDefinitions.Blocks.Single(b => b.ClassName == "DefaultMap")); output.Line(); WriteParser(output, MapInfoDefinitions.Blocks.Single(b => b.ClassName == "AddDefaultMap")); output.Line(); WriteMapParser(output, MapInfoDefinitions.Blocks.Single(b => b.ClassName == "Map")); output.Line(); WriteDefaultMapUpdater(output, MapInfoDefinitions.Blocks.Single(b => b.ClassName == "DefaultMap")); output.CloseParen(); }
public static void WriteToPath(string basePath) { foreach (var block in UdmfDefinitions.Blocks) { using (var blockStream = File.CreateText(Path.Combine(basePath, block.CodeName.ToPascalCase() + ".Generated.cs"))) using (var output = new IndentedWriter(blockStream)) { output.Line( $@"// Copyright (c) {DateTime.Today.Year}, David Aramant // Distributed under the 3-clause BSD license. For full terms see the file LICENSE. using System.CodeDom.Compiler; using System.Collections.Generic; using System.IO; using System.Linq; using UdmfParsing.Udmf.WritingExtensions; namespace UdmfParsing.Udmf"); output.OpenParen(); output.Line( $"[GeneratedCode(\"{CurrentLibraryInfo.Name}\", \"{CurrentLibraryInfo.Version}\")]"); output.Line( $"public sealed partial class {block.CodeName}"); output.OpenParen(); WriteProperties(block, output); output.Line(); WriteConstructors(output, block); output.Line(); WriteWriteToMethod(block, output); output.Line(); WriteSemanticValidityMethods(output, block); WriteCloneMethod(output, block); output.CloseParen(); output.Line(); output.CloseParen(); // End namespace } } }
private static void WriteGlobalFieldParsing(IndentedWriter output) { output. Line("static partial void ProcessGlobalAssignment(MapData map, Assignment assignment)"). OpenParen(); var block = UdmfDefinitions.Blocks.Single(b => b.CodeName.ToPascalCase() == "MapData"); WriteFieldSwitch(output, block, "map"); output. CloseParen(); }
static void WriteActorFile(string basePath, IReadOnlyList <Actor> actors) { using var blockStream = File.CreateText(Path.Combine(basePath, "Actor.Generated.cs")); using var output = new IndentedWriter(blockStream); output .WriteHeader("Tiledriver.Core.GameInfo.Doom", new[] { "System.Collections.Generic", "System.CodeDom.Compiler" }) .Line($"[GeneratedCode(\"{CurrentLibraryInfo.Name}\", \"{CurrentLibraryInfo.Version}\")]") .Line($"public sealed partial record Actor") .OpenParen(); foreach (var actor in actors) { output .Line($"/// <summary>{actor.Description}</summary>") .Line($"public static readonly Actor {actor.SafeName} = new(") .IncreaseIndent() .JoinLines(",", new[] { $"Id: {actor.Id}", $"Description: \"{actor.Description}\"", $"Width: {actor.Width}", $"Height: {actor.Height}" }) .DecreaseIndent() .Line(");") .Line(); } foreach (var category in actors.GroupBy(c => c.CategoryName)) { output .Line($"public static class {category.Key}") .OpenParen() .Lines(category.Select(actor => $"public static Actor {actor.SafeName} => Actor.{actor.SafeName};")) .Line("public static IEnumerable<Actor> GetAll()") .OpenParen() .Lines(category.Select(actor => $"yield return {actor.SafeName};")) .CloseParen() .CloseParen(); } output .CloseParen(); }
private static void WriteBlockParser(Block block, IndentedWriter output) { var variable = block.CodeName.ToCamelCase(); output. Line($"static {block.CodeName} Process{block.CodeName}(Block block)"). OpenParen(). Line($"var {variable} = new {block.CodeName}();"). Line("foreach (var assignment in block.Fields)"). OpenParen(); WriteFieldSwitch(output, block, variable); output. CloseParen(). Line($"return {variable};"). CloseParen(). Line(); }
private static void CreateBlockWriter(IndentedWriter output, Block block) { output .Line($"private static void Write(StreamWriter writer, {block.ClassName} {block.Name})") .OpenParen(); if (block.Serialization == SerializationType.Normal) { output .Line($"writer.WriteLine(\"{block.Name}\");") .Line("writer.WriteLine(\"{\");"); } foreach (var p in block.Properties) { if (p is ScalarProperty sp) { var defaultValue = sp.DefaultString; string optionalDefault = defaultValue != null ? ", " + defaultValue : string.Empty; string indent = block.Serialization == SerializationType.Normal ? string.Empty : ", indent:false"; output.Line($"WriteProperty(writer, \"{sp.FormatName}\", {block.Name}.{sp.PropertyName}{optionalDefault}{indent});"); } else if (p is CollectionProperty cp) { output.Line($"foreach(var block in {block.Name}.{cp.PropertyName})") .OpenParen() .Line($"Write(writer, block);") .CloseParen(); } } if (block.Serialization == SerializationType.Normal) { output.Line("writer.WriteLine(\"}\");"); } output.CloseParen(); }
private static void WriteConstructors(IndentedWriter sb, Block block) { sb.Line($"public {block.CodeName}() {{ }}"); sb.Line($"public {block.CodeName}("); sb.IncreaseIndent(); foreach (var indexed in block.OrderedProperties().Select((param, index) => new { param, index })) { var property = indexed.param; var argument = $"{property.ConstructorArgumentType} {property.ConstructorArgumentName}" + (property.IsRequired ? string.Empty : $" = {property.DefaultValue}"); sb.Line(argument + (indexed.index == block.Properties.Count() - 1 ? ")" : ",")); } sb.DecreaseIndent(); sb.OpenParen(); foreach (var property in block.OrderedProperties()) { switch (property) { case Field field: sb.Line($"{field.PropertyName} = {field.ConstructorArgumentName};"); break; case BlockList requiredBlockList when requiredBlockList.IsRequired: sb.Line($"{requiredBlockList.PropertyName}.AddRange({requiredBlockList.ConstructorArgumentName});"); break; case BlockList blockList: sb.Line($"{blockList.PropertyName}.AddRange({blockList.ConstructorArgumentName} ?? Enumerable.Empty<{blockList.SingularName}>());"); break; } } sb.Line(@"AdditionalSemanticChecks();"); sb.CloseParen(); }