Skip to content

Commit

Permalink
Test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-andersson-at-westermo committed May 28, 2024
1 parent 83e98ca commit 87ace51
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 77 deletions.
47 changes: 10 additions & 37 deletions Compiler.Tests/ParsingTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text;
using Xunit.Abstractions;
using YangParser;
using YangParser.Generator;
using YangParser.Parser;
using YangParser.SemanticModel;

Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -128,39 +129,27 @@ public void GroupingTest()
}

CompilationUnit compilationUnit = new CompilationUnit(modules.OfType<Module>().ToArray());
IncludeSubmodules(compilationUnit.Children.OfType<Module>().ToDictionary(x => x.Argument),
compilationUnit.Children.ToDictionary(x => x.Argument));
foreach (var module in compilationUnit.Children.OfType<Module>())
{
var usings = module.Unwrap().OfType<Uses>().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();
}
}

foreach (var statement in compilationUnit.Unwrap())
{
Assert.IsNotType<Uses>(statement);
}

IncludeSubmodules(compilationUnit.Children.OfType<Module>().ToDictionary(x => x.Argument),
compilationUnit.Children.ToDictionary(x => x.Argument));
UnwrapUses(compilationUnit);

Log.Clear();
output.WriteLine(Log.Content);
output.WriteLine(Clean(compilationUnit.ToCode()));
}

Expand Down Expand Up @@ -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<Module>())
{
var usings = module.Unwrap().OfType<Uses>().ToArray();
foreach (var use in usings)
{
if (use.IsUnderGrouping())
{
continue;
}

use.Expand();
}
}
}

private static void IncludeSubmodules(Dictionary<string, Module> modules,
Dictionary<string, IStatement> topLevels)
Expand Down
21 changes: 16 additions & 5 deletions YangParser/Generator/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

// }
}
13 changes: 0 additions & 13 deletions YangParser/SemanticModel/Augment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/DefaultValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private string GetTypeSpecification(string prefix, string value, Type type)
{
return aPrefix + type.Name + "." + bit.Ancestor<Type>()!.Name + "." + MakeName(Argument);
}
return $"new(\"{Argument}\")/*Union*/";
return $"new(\"{Argument}\")";
default:
var source = this.FindReference<TypeDefinition>(type.Argument);
if (source is not null)
Expand Down
24 changes: 14 additions & 10 deletions YangParser/SemanticModel/Grouping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand Down Expand Up @@ -72,27 +77,25 @@ public IStatement[] WithUse(Uses use)
type.Argument = this.GetInheritedPrefix() + ":" + type.Argument;
}

foreach (var inner in this.Unwrap().OfType<Uses>().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)
{
Expand All @@ -106,6 +109,7 @@ public IStatement[] WithUse(Uses use)

return Children;
}

protected override void ValidateParent()
{
this.GetModule()?.Groupings.Add(this);
Expand Down
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Statement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 28 additions & 10 deletions YangParser/SemanticModel/StatementExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public static IEnumerable<IStatement> Unwrap(this IStatement source)

public static IStatement? FindSourceFor(this IStatement source, string prefix)
{
var module = source.Root();
var imports = module.Children.OfType<Module>().SelectMany(module => module.Imports);
var module = source.GetModule();
var imports = module.Imports;
return imports?.FirstOrDefault(import => import.GetChild<Prefix>().Argument == prefix);
}

Expand Down Expand Up @@ -145,13 +145,18 @@ public static Grouping GetGrouping(this Uses use)
use.Source);
}

var source = use.Root().Children.OfType<Module>().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<Module>().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<Module>().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<Module>().FirstOrDefault(m => m.Namespace == prefix) ??
throw new SemanticError($"Could not find a module with the key {prefix}", use.Source);
var grouping = use.FindGrouping(source);
}
}
Expand All @@ -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);
}

Expand All @@ -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(":"))
Expand All @@ -191,22 +198,25 @@ 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<T>(this IStatement source, string reference) where T : IStatement
{
var key = (typeof(T), reference);
if (_cache.TryGetValue(key, out var cached))
{
return (T?)cached;
}

var prefix = reference.Prefix(out var name);
if (string.IsNullOrEmpty(prefix))
{
Expand All @@ -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);
Expand All @@ -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<T>(name) ?? module.SearchUpwards<T>(name);
_cache[key] = value;
return value;
Expand All @@ -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<T>(name) ?? module.SearchUpwards<T>(name);
_cache[key] = value;
return value;
}
}
}

public static T? Ancestor<T>(this IStatement source) where T : IStatement
{
while (source.Parent is not null)
Expand All @@ -270,9 +283,12 @@ public static string Prefix(this string argument, out string name)
return t;
}
}

return default;
}
public static T? SearchDownwards<T>(this IStatement source, string argument, params IStatement[] except) where T : IStatement

public static T? SearchDownwards<T>(this IStatement source, string argument, params IStatement[] except)
where T : IStatement
{
if (source.Argument == argument && source is T t && source is not DefaultValue)
{
Expand All @@ -290,12 +306,14 @@ public static string Prefix(this string argument, out string name)

return default;
}

public static T? SearchUpwards<T>(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<T>(source.Parent, argument, source);
if (result is not null) return result;
Expand Down

0 comments on commit 87ace51

Please sign in to comment.