| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | |
| | | 4 | | namespace NanoCLang.Entities { |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides a class for location entities. |
| | | 7 | | /// </summary> |
| | | 8 | | public class Location : Base, IEquatable<Location?>, ISubstitutable<Location>, ICloneable { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Creates a new location instance with the given <paramref name="name"/>. <paramref name="isAbstract"/> specif |
| | | 11 | | /// </summary> |
| | | 12 | | /// <param name="name">Name of the location.</param> |
| | | 13 | | /// <param name="isAbstract"><c>true</c> iff the location is abstract, else concrete.</param> |
| | 1208 | 14 | | public Location(string name, bool isAbstract) { |
| | 604 | 15 | | Name = name; |
| | 604 | 16 | | Abstract = isAbstract; |
| | 604 | 17 | | } |
| | | 18 | | /// <summary> |
| | | 19 | | /// Name of the location. |
| | | 20 | | /// </summary> |
| | 4416 | 21 | | public string Name { get; } |
| | | 22 | | /// <summary> |
| | | 23 | | /// <c>true</c> iff the location is abstract, else <see langword="false"/>. |
| | | 24 | | /// </summary> |
| | 4193 | 25 | | public bool Abstract { get; } |
| | | 26 | | /// <summary> |
| | | 27 | | /// <c>true</c> iff the location is concrete, else <see langword="false"/>. |
| | | 28 | | /// </summary> |
| | 9 | 29 | | public bool Concrete => !Abstract; |
| | | 30 | | /// <inheritdoc/> |
| | 158 | 31 | | public object Clone() => new Location(Name, Abstract); |
| | | 32 | | /// <inheritdoc/> |
| | | 33 | | public Location Replace(Substitutor rep) |
| | 171 | 34 | | => rep.Locations.TryGetValue(Name, out var subs) |
| | 171 | 35 | | ? new Location(subs, Abstract) |
| | 171 | 36 | | : (Location)Clone(); |
| | | 37 | | #region Equality checks |
| | | 38 | | /// <inheritdoc/> |
| | 0 | 39 | | public override bool Equals(object? obj) => Equals(obj as Location); |
| | | 40 | | /// <inheritdoc/> |
| | 895 | 41 | | public bool Equals(Location? other) => !(other is null) && Name == other.Name && Abstract == other.Abstract; |
| | | 42 | | /// <inheritdoc/> |
| | 1663 | 43 | | public override int GetHashCode() => HashCode.Combine(Name, Abstract); |
| | | 44 | | /// <inheritdoc/> |
| | 52 | 45 | | public static bool operator ==(Location? left, Location? right) => EqualityComparer<Location?>.Default.Equals(le |
| | | 46 | | /// <inheritdoc/> |
| | 40 | 47 | | public static bool operator !=(Location? left, Location? right) => !(left == right); |
| | | 48 | | #endregion |
| | | 49 | | /// <inheritdoc/> |
| | 389 | 50 | | public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) { |
| | 684 | 51 | | if (Abstract) yield return "~"; |
| | 389 | 52 | | yield return Name; |
| | 389 | 53 | | } |
| | | 54 | | } |
| | | 55 | | } |