Mercury library  1.0
Translation of CFG grammars into an object model (Bachelor's thesis).
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
EdgeDerivation.cs
Go to the documentation of this file.
1 using System;
2 
3 namespace Mercury.Syntax
4 {
5 //=============================================================================
6 // EdgeDerivation
7 //=============================================================================
8 
10  internal enum DerivationType
11  {
13  Initial = 0,
15  Fundamental = 1,
17  Scanning = 2,
19  Prediction = 3,
20  }
21 
23  internal class EdgeDerivation : IEquatable<EdgeDerivation>, IComparable<EdgeDerivation>
24  {
25  //--[ Private fieds ]------------------------------------------------
26 
27  int? memHash = null; // memoized hash
28 
29  //--[ Constructors ]-------------------------------------------------
30 
31  private EdgeDerivation()
32  {
33  Source = null;
34  Additional = null;
35  }
36 
37  //--[ Static methods ]-----------------------------------------------
38 
39  public static EdgeDerivation Initial()
40  { return new EdgeDerivation() { Derivation = DerivationType.Initial }; }
41 
42  public static EdgeDerivation Prediction()
43  { return new EdgeDerivation() { Derivation = DerivationType.Prediction }; }
44 
45  public static EdgeDerivation Scanning(Edge source)
46  {
47  return new EdgeDerivation() { Derivation = DerivationType.Scanning, Source = source };
48  }
49 
50  public static EdgeDerivation Fundamental(Edge source, Edge additional)
51  {
52  return new EdgeDerivation()
53  { Derivation = DerivationType.Fundamental, Source = source, Additional = additional };
54  }
55 
56  //--[ Properties ]---------------------------------------------------
57 
60  public DerivationType Derivation { get; private set; }
61 
63  public Edge Source { get; private set; }
64 
66  public Edge Additional { get; private set; }
67 
68  //--[ Overriden methods ]--------------------------------------------
69 
70  #region [ Object ]
71 
72  public override bool Equals(object obj)
73  {
74  EdgeDerivation ed = obj as EdgeDerivation;
75  return ed == null ? false : Equals(ed);
76  }
77 
78  public override int GetHashCode()
79  {
80  if (!memHash.HasValue)
81  {
82  memHash = 3 * (int) Derivation;
83  memHash += 7 * (Source == null ? 0 : Source.GetHashCode());
84  memHash += 23 * (Additional == null ? 0 : Additional.GetHashCode());
85  }
86 
87  return memHash.Value;
88  }
89 
90  #endregion
91 
92  //--[ Interface implementation ]-------------------------------------
93 
94  #region [ IEquatable ]
95 
96  public bool Equals(EdgeDerivation other)
97  {
98  return (other != null)
99  && (Derivation == other.Derivation) && (GetHashCode() == other.GetHashCode())
100  && (Source == null ? other.Source == null : Source.Equals(other.Source))
101  && (Additional == null ? other.Additional == null : Additional.Equals(other.Additional));
102  }
103 
104  #endregion
105 
106  #region [ IComparable ]
107 
108  public int CompareTo(EdgeDerivation other)
109  {
110  int temp = Derivation.CompareTo(other.Derivation);
111 
112  if ((temp == 0) && (Source != null)) temp = Source.CompareTo(other.Source);
113  if ((temp == 0) && (Additional != null)) temp = Additional.CompareTo(other.Additional);
114 
115  return temp;
116  }
117 
118  #endregion
119 
120  }
121 }
Edge created by input scanning
Edge created by fundamental A or B rule
Edge created by prediction