From 87ace51a9ea513102f7ab8140ba6c87378c3f8cb Mon Sep 17 00:00:00 2001 From: caran Date: Tue, 28 May 2024 15:30:03 +0200 Subject: [PATCH] Test fixes --- Compiler.Tests/ParsingTests.cs | 47 ++++--------------- YangParser/Generator/Log.cs | 21 +++++++-- YangParser/SemanticModel/Augment.cs | 13 ----- YangParser/SemanticModel/DefaultValue.cs | 2 +- YangParser/SemanticModel/Grouping.cs | 24 ++++++---- YangParser/SemanticModel/Statement.cs | 2 +- .../SemanticModel/StatementExtensions.cs | 38 +++++++++++---- 7 files changed, 70 insertions(+), 77 deletions(-) diff --git a/Compiler.Tests/ParsingTests.cs b/Compiler.Tests/ParsingTests.cs index d57169e..8244ef6 100644 --- a/Compiler.Tests/ParsingTests.cs +++ b/Compiler.Tests/ParsingTests.cs @@ -1,6 +1,7 @@ using System.Text; using Xunit.Abstractions; using YangParser; +using YangParser.Generator; using YangParser.Parser; using YangParser.SemanticModel; @@ -19,7 +20,7 @@ public void IetfYangLibrary() public const string ModuleOne = """ module one { yang-version 1.1; - namespace "urn:one"; + namespace "urn:ns:one"; prefix one; import two { prefix b; @@ -46,7 +47,7 @@ grouping A { public const string ModuleTwo = """ module two { yang-version 1.1; - namespace "urn:two"; + namespace "urn:ns:two"; prefix two; import three { prefix b; @@ -78,7 +79,7 @@ leaf module-set-idu { public const string ModuleThree = """ module three { yang-version 1.1; - namespace "urn:three"; + namespace "urn:NS:three"; prefix three; typedef operator { type bits { @@ -128,27 +129,18 @@ public void GroupingTest() } CompilationUnit compilationUnit = new CompilationUnit(modules.OfType().ToArray()); + IncludeSubmodules(compilationUnit.Children.OfType().ToDictionary(x => x.Argument), + compilationUnit.Children.ToDictionary(x => x.Argument)); foreach (var module in compilationUnit.Children.OfType()) { - var usings = module.Unwrap().OfType().ToArray(); - foreach (var use in usings) + foreach (var use in module.Uses) { if (use.IsUnderGrouping()) { continue; } - if (use.Parent == null) continue; - - var grouping = use.GetGrouping(); - var parent = use.Parent; - parent!.Replace(use, grouping.WithUse(use)); - if (parent.Children.Contains(use)) - { - throw new SemanticError( - $"'Failed to replace '{use.Argument}' in '{parent.GetType().Name} {parent.Argument}'", - module.Source, usings.Select(u => u.Source).ToArray()); - } + use.Expand(); } } @@ -156,11 +148,8 @@ public void GroupingTest() { Assert.IsNotType(statement); } - - IncludeSubmodules(compilationUnit.Children.OfType().ToDictionary(x => x.Argument), - compilationUnit.Children.ToDictionary(x => x.Argument)); - UnwrapUses(compilationUnit); - + Log.Clear(); + output.WriteLine(Log.Content); output.WriteLine(Clean(compilationUnit.ToCode())); } @@ -228,22 +217,6 @@ private void Print(IStatement statement, int indent = 0) output.WriteLine($"{tabs}}}"); } - private static void UnwrapUses(IStatement compilation) - { - foreach (var module in compilation.Children.OfType()) - { - var usings = module.Unwrap().OfType().ToArray(); - foreach (var use in usings) - { - if (use.IsUnderGrouping()) - { - continue; - } - - use.Expand(); - } - } - } private static void IncludeSubmodules(Dictionary modules, Dictionary topLevels) diff --git a/YangParser/Generator/Log.cs b/YangParser/Generator/Log.cs index ee984bc..eb7c30a 100644 --- a/YangParser/Generator/Log.cs +++ b/YangParser/Generator/Log.cs @@ -9,24 +9,35 @@ public static class Log { private static DateTime Start; private static FileStream? m_stream; + private static FileStream Stream { get { if (m_stream is null) Start = DateTime.Now; - return m_stream ??= new FileStream(@"C:\tmp\YangGenerator\log", FileMode.Create, FileAccess.Write, FileShare.ReadWrite); + return m_stream ??= new FileStream(@"C:\tmp\YangGenerator\log", FileMode.Create, FileAccess.Write, + FileShare.ReadWrite); } } public static void Clear() { - m_writer?.Flush(); + Write("END OF LOG\n*/"); // m_stream?.Dispose(); // m_writer?.Dispose(); // m_stream = null; // m_writer = null; } - private static StreamWriter? m_writer; - private static StreamWriter writer => m_writer ??= new StreamWriter(Stream); - public static void Write(string message) => writer.WriteLine($"{(DateTime.Now - Start).TotalSeconds:F2}: " + message); + + private static StringBuilder? m_writer; + private static StringBuilder writer => m_writer ??= new StringBuilder("/*\n"); + + public static void Write(string message) + { + writer.AppendLine($"{(DateTime.Now - Start).TotalSeconds:F2}: " + message); + } + + public static string Content => writer.ToString(); + +// } } \ No newline at end of file diff --git a/YangParser/SemanticModel/Augment.cs b/YangParser/SemanticModel/Augment.cs index c4fab7b..0dd0e7e 100644 --- a/YangParser/SemanticModel/Augment.cs +++ b/YangParser/SemanticModel/Augment.cs @@ -42,19 +42,6 @@ public Augment(YangStatement statement) : base(statement) new ChildRule(Uses.Keyword, Cardinality.ZeroOrMore), new ChildRule(Keyword, Cardinality.ZeroOrMore) ]; - - public override string ToCode() - { - var nodes = Children.Select(child => child.ToCode()).ToArray(); - var name = MakeName(Argument.Split('/', ':').Last()) + "Augmentation"; - Attributes.Add($"Target(\"{Argument.Replace("\n", "")}\")"); - //TODO: Add Augmentation logic - return $$""" - /* - {{this}} - */ - """; - } protected override void ValidateParent() { this.GetModule()?.Augments.Add(this); diff --git a/YangParser/SemanticModel/DefaultValue.cs b/YangParser/SemanticModel/DefaultValue.cs index f9beb23..e4bfdfb 100644 --- a/YangParser/SemanticModel/DefaultValue.cs +++ b/YangParser/SemanticModel/DefaultValue.cs @@ -67,7 +67,7 @@ private string GetTypeSpecification(string prefix, string value, Type type) { return aPrefix + type.Name + "." + bit.Ancestor()!.Name + "." + MakeName(Argument); } - return $"new(\"{Argument}\")/*Union*/"; + return $"new(\"{Argument}\")"; default: var source = this.FindReference(type.Argument); if (source is not null) diff --git a/YangParser/SemanticModel/Grouping.cs b/YangParser/SemanticModel/Grouping.cs index ee16495..38b83f6 100644 --- a/YangParser/SemanticModel/Grouping.cs +++ b/YangParser/SemanticModel/Grouping.cs @@ -42,6 +42,11 @@ public IStatement[] WithUse(Uses use) { foreach (var child in this.Unwrap()) { + if (child is Uses inner) + { + inner.Expand(); + } + if (child is not Type type) continue; if (type.Argument.Contains("identityref")) { @@ -72,27 +77,25 @@ public IStatement[] WithUse(Uses use) type.Argument = this.GetInheritedPrefix() + ":" + type.Argument; } - foreach (var inner in this.Unwrap().OfType().ToArray()) - { - inner.Expand(); - } - //Propogate usings upwards if (use.GetModule() is Module target) { if (this.GetModule() is Module source) { - if (source == target) return Children; - foreach (var pair in source.Usings) + if (source != target) { - if (!target.Usings.ContainsKey(pair.Key)) + foreach (var pair in source.Usings) { - // Log.Write($"Adding prefix {pair.Key} to '{target.Argument}' from '{source.Argument}'"); - target.Usings[pair.Key] = pair.Value; + if (!target.Usings.ContainsKey(pair.Key)) + { + // Log.Write($"Adding prefix {pair.Key} to '{target.Argument}' from '{source.Argument}'"); + target.Usings[pair.Key] = pair.Value; + } } } } } + var containingModule = this.GetModule(); if (containingModule is null) { @@ -106,6 +109,7 @@ public IStatement[] WithUse(Uses use) return Children; } + protected override void ValidateParent() { this.GetModule()?.Groupings.Add(this); diff --git a/YangParser/SemanticModel/Statement.cs b/YangParser/SemanticModel/Statement.cs index 6f7c5f7..f582609 100644 --- a/YangParser/SemanticModel/Statement.cs +++ b/YangParser/SemanticModel/Statement.cs @@ -258,7 +258,7 @@ protected virtual void ValidateParent() public virtual string ToCode() { - return $"#error ToCode() call on non-overriden type {GetType()}"; + return $"#warning ToCode() call on non-overriden type {GetType()}:\n/*{this}\n*/"; } public static string InterfaceName(Base b) diff --git a/YangParser/SemanticModel/StatementExtensions.cs b/YangParser/SemanticModel/StatementExtensions.cs index d8a598a..94f5abf 100644 --- a/YangParser/SemanticModel/StatementExtensions.cs +++ b/YangParser/SemanticModel/StatementExtensions.cs @@ -80,8 +80,8 @@ public static IEnumerable Unwrap(this IStatement source) public static IStatement? FindSourceFor(this IStatement source, string prefix) { - var module = source.Root(); - var imports = module.Children.OfType().SelectMany(module => module.Imports); + var module = source.GetModule(); + var imports = module.Imports; return imports?.FirstOrDefault(import => import.GetChild().Argument == prefix); } @@ -145,13 +145,18 @@ public static Grouping GetGrouping(this Uses use) use.Source); } - var source = use.Root().Children.OfType().FirstOrDefault(m => m.Argument == import.Argument) ?? throw new SemanticError($"Could not find a module with the key {import.Argument}", import.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}', import was {Statement.SingleLine(import.ToString())}", use.Source); + var source = use.Root().Children.OfType().FirstOrDefault(m => m.Argument == import.Argument) ?? + throw new SemanticError($"Could not find a module with the key {import.Argument}", + import.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}', import was {Statement.SingleLine(import.ToString())}", + use.Source); return grouping; } else { - 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 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); } } @@ -160,7 +165,7 @@ public static Grouping GetGrouping(this Uses use) if (findGrouping is null) { throw new SemanticError( - $"Could not find a grouping statement to use for 'uses {use.Argument}' in module '{module.Argument}'", + $"Could not find a grouping statement to use for 'uses {use.Argument}' [{use.Parent}] in module '{module.Argument}'", use.Source); } @@ -170,10 +175,12 @@ public static Grouping GetGrouping(this Uses use) public static void Expand(this Uses use) { + if (use.Parent?.Children.Contains(use) != true) return; var grouping = use.GetGrouping(); var parent = use.Parent; parent!.Replace(use, grouping.WithUse(use)); } + public static string Prefix(this string argument, out string name) { if (argument.Contains(":")) @@ -191,15 +198,17 @@ public static string Prefix(this string argument, out string name) builder.Append(components[i]); builder.Append("."); } + name = components.Last(); return builder.ToString(); - } + name = argument; return string.Empty; - } + static Dictionary<(System.Type, string), IStatement?> _cache = []; + public static T? FindReference(this IStatement source, string reference) where T : IStatement { var key = (typeof(T), reference); @@ -207,6 +216,7 @@ public static string Prefix(this string argument, out string name) { return (T?)cached; } + var prefix = reference.Prefix(out var name); if (string.IsNullOrEmpty(prefix)) { @@ -222,7 +232,7 @@ public static string Prefix(this string argument, out string name) _cache[key] = value; return value; } - else if(!prefix.Contains('.')) + else if (!prefix.Contains('.')) { IStatement? module; var import = source.FindSourceFor(prefix); @@ -241,6 +251,7 @@ public static string Prefix(this string argument, out string name) $"Failed to find module for '{moduleName}'"); return default; } + var value = module.SearchDownwards(name) ?? module.SearchUpwards(name); _cache[key] = value; return value; @@ -254,12 +265,14 @@ public static string Prefix(this string argument, out string name) $"Failed to find module for '{prefix}'"); return default; } + var value = module.SearchDownwards(name) ?? module.SearchUpwards(name); _cache[key] = value; return value; } } } + public static T? Ancestor(this IStatement source) where T : IStatement { while (source.Parent is not null) @@ -270,9 +283,12 @@ public static string Prefix(this string argument, out string name) return t; } } + return default; } - public static T? SearchDownwards(this IStatement source, string argument, params IStatement[] except) where T : IStatement + + public static T? SearchDownwards(this IStatement source, string argument, params IStatement[] except) + where T : IStatement { if (source.Argument == argument && source is T t && source is not DefaultValue) { @@ -290,12 +306,14 @@ public static string Prefix(this string argument, out string name) return default; } + public static T? SearchUpwards(this IStatement source, string argument) where T : IStatement { if (source.Argument == argument && source is T t && source is not DefaultValue) { return t; } + if (source.Parent is null) return default; var result = SearchDownwards(source.Parent, argument, source); if (result is not null) return result;