Skip to content

Commit

Permalink
Fix equality of Pattern/Build.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ygg01 committed Jan 16, 2024
1 parent b6e412b commit 4415ce3
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions Linguini.Syntax/Ast/Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Linguini.Syntax.Ast
{

public class Attribute
public class Attribute : IEquatable<Attribute>
{
public readonly Identifier Id;
public readonly Pattern Value;
Expand All @@ -35,9 +35,29 @@ public static Attribute From(string id, PatternBuilder patternBuilder)
{
return new Attribute(new Identifier(id), patternBuilder.Build());
}

public bool Equals(Attribute? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Id.Equals(other.Id) && Value.Equals(other.Value);
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Attribute)obj);
}

public override int GetHashCode()
{
return HashCode.Combine(Id, Value);
}
}

public class Pattern
public class Pattern : IEquatable<Pattern>
{
public readonly List<IPatternElement> Elements;

Expand All @@ -50,6 +70,38 @@ public Pattern()
{
Elements = new List<IPatternElement>();
}

public bool Equals(Pattern? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
if (Elements.Count != other.Elements.Count)
{
return false;
}

for (var index = 0; index < Elements.Count; index++)
{
var patternElement = Elements[index];
var otherPatternElement = other.Elements[index];
if (!patternElement.Equals(otherPatternElement)) return false;
}

return true;
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Pattern)obj);
}

public override int GetHashCode()
{
return Elements.GetHashCode();
}
}

public class PatternBuilder
Expand Down

0 comments on commit 4415ce3

Please sign in to comment.