Skip to content

Commit

Permalink
Grouping is pretend immutable.
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-andersson-at-westermo committed May 29, 2024
1 parent 8f5bb2d commit 99db906
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
5 changes: 5 additions & 0 deletions Compiler.Tests/ParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uses>(statement);
}

Expand Down
17 changes: 11 additions & 6 deletions YangParser/SemanticModel/Grouping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Check warning on line 44 in YangParser/SemanticModel/Grouping.cs

View workflow job for this annotation

GitHub Actions / Performance regression check

Dereference of a possibly null reference.

Check warning on line 44 in YangParser/SemanticModel/Grouping.cs

View workflow job for this annotation

GitHub Actions / Performance regression check

Dereference of a possibly null reference.

Check warning on line 44 in YangParser/SemanticModel/Grouping.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
copy.Parent = Parent;
foreach (var child in copy.Unwrap())
{
if (child is Uses inner)
{
Expand All @@ -58,7 +61,7 @@ public IStatement[] WithUse(Uses use)
continue;
}

baseType.Argument = this.GetInheritedPrefix() + ":" + baseType.Argument;
baseType.Argument = copy.GetInheritedPrefix() + ":" + baseType.Argument;
}

continue;
Expand All @@ -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)
{
Expand All @@ -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))
Expand All @@ -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}'");
Expand All @@ -112,7 +116,8 @@ public IStatement[] WithUse(Uses use)
containingModule.Expand();
}

Parent.Replace(copy, []);

return Children;
return copy.Children;
}
}

0 comments on commit 99db906

Please sign in to comment.