CanonSharp/CanonSharp.Common/LexicalAnalyzer/EmptyChar.cs
2024-07-27 14:56:28 +08:00

50 lines
924 B
C#

namespace CanonSharp.Common.LexicalAnalyzer;
public class EmptyChar : IEquatable<EmptyChar>
{
public bool IsEmpty { get; }
public char Char { get; }
public static EmptyChar Empty => new();
private EmptyChar()
{
IsEmpty = true;
Char = char.MaxValue;
}
public EmptyChar(char c)
{
IsEmpty = false;
Char = c;
}
public bool Equals(EmptyChar? other)
{
if (other is null)
{
return false;
}
if (IsEmpty)
{
return other.IsEmpty;
}
return Char == other.Char;
}
public override bool Equals(object? obj) => obj is EmptyChar other && Equals(other);
public override int GetHashCode()
{
return IsEmpty.GetHashCode() ^ Char.GetHashCode();
}
public override string ToString()
{
return IsEmpty ? "ε" : Char.ToString();
}
}