From 8c5259ddce0e8cc00241cc0ffcea637fc49bd973 Mon Sep 17 00:00:00 2001 From: caran Date: Tue, 28 May 2024 15:39:47 +0200 Subject: [PATCH] Builds, with warnings.. many warnings --- YangParser/Generator/Log.cs | 7 +- YangParser/Generator/YangGenerator.cs | 218 +++++++++--------- .../SemanticModel/StatementExtensions.cs | 14 +- YangParser/SemanticModel/Type.cs | 3 +- 4 files changed, 120 insertions(+), 122 deletions(-) diff --git a/YangParser/Generator/Log.cs b/YangParser/Generator/Log.cs index eb7c30a..35f0f99 100644 --- a/YangParser/Generator/Log.cs +++ b/YangParser/Generator/Log.cs @@ -7,7 +7,7 @@ namespace YangParser.Generator; public static class Log { - private static DateTime Start; + private static DateTime Start = DateTime.Now; private static FileStream? m_stream; private static FileStream Stream @@ -22,7 +22,6 @@ private static FileStream Stream public static void Clear() { - Write("END OF LOG\n*/"); // m_stream?.Dispose(); // m_writer?.Dispose(); // m_stream = null; @@ -30,11 +29,11 @@ public static void Clear() } private static StringBuilder? m_writer; - private static StringBuilder writer => m_writer ??= new StringBuilder("/*\n"); + private static StringBuilder writer => m_writer ??= new StringBuilder(); public static void Write(string message) { - writer.AppendLine($"{(DateTime.Now - Start).TotalSeconds:F2}: " + message); + writer.AppendLine($"/*\n{(DateTime.Now - Start).TotalSeconds:F2}: " + message + "\n*/"); } public static string Content => writer.ToString(); diff --git a/YangParser/Generator/YangGenerator.cs b/YangParser/Generator/YangGenerator.cs index 88b28bc..6ef5586 100644 --- a/YangParser/Generator/YangGenerator.cs +++ b/YangParser/Generator/YangGenerator.cs @@ -96,26 +96,29 @@ private void MakeClasses(SourceProductionContext context, ImmutableArray MakeSemanticModel(ResultOrException statement) @@ -305,99 +301,99 @@ private void AddAttributesClass(IncrementalGeneratorPostInitializationContext co { var fileName = "YangModules/Attributes/Yang.Attributes.cs"; var contents = """ - using System; - namespace Yang.Attributes; - [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] - public class RevisionAttribute(string date) : Attribute - { - public string Date { get; } = date; - } + using System; + namespace Yang.Attributes; + [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] + public class RevisionAttribute(string date) : Attribute + { + public string Date { get; } = date; + } - [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] - public class PresenceAttribute(string meaning) : Attribute - { - public string Meaning { get; } = meaning; - } - [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] - public class ProvidesFeatureAttribute(string flag) : Attribute - { - public string FeatureFlag { get; } = flag; - } - [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] - public class IfFeatureAttribute(string flag) : Attribute - { - public string FeatureFlag { get; } = flag; - } - [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] - public class ReferenceAttribute(string reference) : Attribute - { - public string Reference { get; } = reference; - } - [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] - public class WhenAttribute(string xPath) : Attribute - { - public string XPath { get; } = xPath; - } - [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] - public class TargetAttribute(string xPath) : Attribute - { - public string XPath { get; } = xPath; - } - [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] - public class KeyAttribute(params string[] value) : Attribute - { - public string[] Value { get; } = value; - } - [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] - public class MinElementsAttribute(int value) : Attribute - { - public int Value { get; } = value; - } - [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] - public class MaxElementsAttribute(int value) : Attribute - { - public int Value { get; } = value; - } - [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] - public class InheritsAttribute(string baseName) : Attribute - { - public string BaseName { get; } = baseName; - } - [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] - public class OrderedByAttribute(string value) : Attribute - { - public string Value { get; } = value; - } - [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] - public class NotConfigurationData : Attribute; + [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] + public class PresenceAttribute(string meaning) : Attribute + { + public string Meaning { get; } = meaning; + } + [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] + public class ProvidesFeatureAttribute(string flag) : Attribute + { + public string FeatureFlag { get; } = flag; + } + [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] + public class IfFeatureAttribute(string flag) : Attribute + { + public string FeatureFlag { get; } = flag; + } + [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] + public class ReferenceAttribute(string reference) : Attribute + { + public string Reference { get; } = reference; + } + [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] + public class WhenAttribute(string xPath) : Attribute + { + public string XPath { get; } = xPath; + } + [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] + public class TargetAttribute(string xPath) : Attribute + { + public string XPath { get; } = xPath; + } + [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] + public class KeyAttribute(params string[] value) : Attribute + { + public string[] Value { get; } = value; + } + [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] + public class MinElementsAttribute(int value) : Attribute + { + public int Value { get; } = value; + } + [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] + public class MaxElementsAttribute(int value) : Attribute + { + public int Value { get; } = value; + } + [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] + public class InheritsAttribute(string baseName) : Attribute + { + public string BaseName { get; } = baseName; + } + [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] + public class OrderedByAttribute(string value) : Attribute + { + public string Value { get; } = value; + } + [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] + public class NotConfigurationData : Attribute; - public class InstanceIdentifier(string path) - { - public string Path { get; } = path; - } - public interface IChannel - { - string Send(string xml); - } - public interface IXMLSource - { - string ToXML(); - } - """; + public class InstanceIdentifier(string path) + { + public string Path { get; } = path; + } + public interface IChannel + { + string Send(string xml); + } + public interface IXMLSource + { + string ToXML(); + } + """; context.AddSource(fileName, contents); Log.Write($"Writing file {fileName}"); - var file = "C:/tmp/YangGenerator/" + fileName; - var dir = System.IO.Path.GetDirectoryName(file); -#pragma warning disable RS1035 // Do not use APIs banned for analyzers - if (Directory.Exists(dir) == false) - { - Directory.CreateDirectory(dir); - } -#pragma warning restore RS1035 // Do not use APIs banned for analyzers - using var fs = new FileStream(file, FileMode.Create); - using var writer = new StreamWriter(fs); - writer.Write(contents); - Log.Clear(); +// var file = "C:/tmp/YangGenerator/" + fileName; +// var dir = System.IO.Path.GetDirectoryName(file); +// #pragma warning disable RS1035 // Do not use APIs banned for analyzers +// if (Directory.Exists(dir) == false) +// { +// Directory.CreateDirectory(dir); +// } +// #pragma warning restore RS1035 // Do not use APIs banned for analyzers +// using var fs = new FileStream(file, FileMode.Create); +// using var writer = new StreamWriter(fs); +// writer.Write(contents); +// Log.Clear(); } } \ No newline at end of file diff --git a/YangParser/SemanticModel/StatementExtensions.cs b/YangParser/SemanticModel/StatementExtensions.cs index 94f5abf..c79aff7 100644 --- a/YangParser/SemanticModel/StatementExtensions.cs +++ b/YangParser/SemanticModel/StatementExtensions.cs @@ -40,7 +40,7 @@ public static T GetChild(this IStatement statement) where T : class, IStateme public static bool TryGetChild(this IStatement statement, out T? child) where T : class, IStatement { child = null; - if (statement?.Children.FirstOrDefault(c => c is T) is T chosen) + if (statement.Children.FirstOrDefault(c => c is T) is T chosen) { child = chosen; return true; @@ -81,7 +81,7 @@ public static IEnumerable Unwrap(this IStatement source) public static IStatement? FindSourceFor(this IStatement source, string prefix) { var module = source.GetModule(); - var imports = module.Imports; + var imports = module?.Imports; return imports?.FirstOrDefault(import => import.GetChild().Argument == prefix); } @@ -120,7 +120,7 @@ public static Grouping GetGrouping(this Uses use) use.Source); } - var prefix = use.Argument.Prefix(out var name); + var prefix = use.Argument.Prefix(out _); if (!string.IsNullOrEmpty(prefix)) //Is 'outside-of-tree' { if (prefix == use.GetInheritedPrefix()) @@ -135,7 +135,8 @@ public static Grouping GetGrouping(this Uses use) return grouping; } - else if (!prefix.Contains(".")) + + if (!prefix.Contains(".")) { var import = use.FindSourceFor(prefix); if (import is null) @@ -157,7 +158,10 @@ public static Grouping GetGrouping(this Uses use) { var source = use.Root().Children.OfType().FirstOrDefault(m => m.Namespace == prefix) ?? throw new SemanticError($"Could not find a module with the key {prefix}", use.Source); - var grouping = use.FindGrouping(source); + var grouping = use.FindGrouping(source) ?? throw new SemanticError( + $"Could not find a grouping statement to use for 'uses {use.Argument}' in module '{source.Argument}' from prefix '{prefix}'", + use.Source); + return grouping; } } diff --git a/YangParser/SemanticModel/Type.cs b/YangParser/SemanticModel/Type.cs index bf04d93..5a153cc 100644 --- a/YangParser/SemanticModel/Type.cs +++ b/YangParser/SemanticModel/Type.cs @@ -1,4 +1,3 @@ -using System; using System.Linq; using YangParser.Parser; using YangParser.SemanticModel.Builtins; @@ -40,7 +39,7 @@ public string? Definition if (!BuiltinTypeReference.IsBuiltin(this, out var typeName, out var definition)) { if (this.FindReference(Argument) != null) return null; - m_definition = BuiltinTypeReference.DefaultPattern(this, [], [], TypeName(this), m_name); + m_definition = BuiltinTypeReference.DefaultPattern(this, [], [], TypeName(this), Name!); return m_definition; } m_name = typeName;