diff --git a/Compiler.Tests/ParsingTests.cs b/Compiler.Tests/ParsingTests.cs index 6b80b9b..fa49ff8 100644 --- a/Compiler.Tests/ParsingTests.cs +++ b/Compiler.Tests/ParsingTests.cs @@ -168,6 +168,11 @@ public void GroupingTest() foreach (var statement in compilationUnit.Unwrap()) { + if(statement.IsUnderGrouping()) continue; + if (statement is Uses uses) + { + output.WriteLine(uses.Parent!.ToString()); + } Assert.IsNotType(statement); } diff --git a/YangParser/SemanticModel/Grouping.cs b/YangParser/SemanticModel/Grouping.cs index 824eae3..2f790dd 100644 --- a/YangParser/SemanticModel/Grouping.cs +++ b/YangParser/SemanticModel/Grouping.cs @@ -40,7 +40,10 @@ public override string ToCode() public IStatement[] WithUse(Uses use) { - foreach (var child in this.Unwrap()) + var copy = StatementFactory.Create(Source); + Parent.Insert([copy]); + copy.Parent = Parent; + foreach (var child in copy.Unwrap()) { if (child is Uses inner) { @@ -58,7 +61,7 @@ public IStatement[] WithUse(Uses use) continue; } - baseType.Argument = this.GetInheritedPrefix() + ":" + baseType.Argument; + baseType.Argument = copy.GetInheritedPrefix() + ":" + baseType.Argument; } continue; @@ -74,13 +77,13 @@ public IStatement[] WithUse(Uses use) continue; } - type.Argument = this.GetInheritedPrefix() + ":" + type.Argument; + type.Argument = copy.GetInheritedPrefix() + ":" + type.Argument; } //Propogate usings upwards if (use.GetModule() is Module target) { - if (this.GetModule() is Module source) + if (copy.GetModule() is Module source) { if (source != target) { @@ -91,6 +94,7 @@ public IStatement[] WithUse(Uses use) target.Usings[pair.Key] = pair.Value; } } + foreach (var pair in source.ImportedModules) { if (!target.ImportedModules.ContainsKey(pair.Key)) @@ -102,7 +106,7 @@ public IStatement[] WithUse(Uses use) } } - var containingModule = this.GetModule(); + var containingModule = copy.GetModule(); if (containingModule is null) { Log.Write($"Error, could not find containing module for grouping '{Argument}'"); @@ -112,7 +116,8 @@ public IStatement[] WithUse(Uses use) containingModule.Expand(); } + Parent.Replace(copy, []); - return Children; + return copy.Children; } } \ No newline at end of file