Mercury library  1.0
Translation of CFG grammars into an object model (Bachelor's thesis).
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
Edge.cs
Go to the documentation of this file.
1 using Mercury.Exceptions;
2 using Mercury.Syntax;
3 using System;
4 using System.Linq;
5 using System.Text;
6 
7 namespace Mercury.Syntax
8 {
9 //=============================================================================
10 // Edge
11 //=============================================================================
12 
37  public class Edge : IEquatable<Edge>, IComparable<Edge>
38  {
39  //--[ Constructors ]-------------------------------------------------
40 
55  public Edge(Rule rule, int dot, int left, int right)
56  {
57  if (rule == null) throw new ArgumentNullException("rule");
58 
59  Rule = rule; Dot = dot;
60  Left = left; Right = right;
61 
62  if (!IsValid()) throw new InvalidEdgeException(this);
63 
64  IsActive = !Rule.IsEpsilon && (Dot < Rule.RHS.Count);
65  IsPassive = !IsActive;
66 
67  NextSymbol = IsActive ? Rule.RHS[Dot] : null;
68  }
69 
70  //--[ Properties ]---------------------------------------------------
71 
73  public Rule Rule { get; private set; }
74 
76  public int Dot { get; private set; }
77 
79  public int Left { get; private set; }
80 
82  public int Right { get; private set; }
83 
91  public bool IsActive { get; private set; }
92 
100  public bool IsPassive { get; private set; }
101 
106  public Symbol NextSymbol { get; private set; }
107 
108  //--[ Methods ]------------------------------------------------------
109 
117  public bool IsValid()
118  { return (0 <= Dot) && (Dot <= Rule.RHS.Count) && (Left <= Right); }
119 
120  //--[ Overriden methods ]--------------------------------------------
121 
122  #region [ Object ]
123 
124  public override bool Equals(object obj)
125  {
126  Edge edge = obj as Edge;
127  return edge == null ? false : Equals(edge);
128  }
129 
130  public override int GetHashCode()
131  { return 23 * Rule.GetHashCode() + 17 * Dot + 11 * Left + 7 * Right; }
132 
133  public override string ToString()
134  {
135  StringBuilder sb = new StringBuilder(string.Format("({0}, {1}) ", Left, Right))
136  .Append(Rule.LHS).Append(" ->");
137 
138  for (int ix = 0; ix < Rule.RHS.Count; ++ix)
139  {
140  sb.Append(ix == Dot ? " . " : " ");
141  if (!Rule.RHS[ix].IsEpsilon) sb.Append(Rule.RHS[ix].ToString());
142  }
143 
144  if (Dot == Rule.RHS.Count) sb.Append(" .");
145 
146  return sb.ToString();
147  }
148 
149  #endregion
150 
151  //--[ Interface implementation ]-------------------------------------
152 
153  #region [ IEquatable ]
154 
155  public bool Equals(Edge other)
156  {
157  return (other != null) && (Dot == other.Dot)
158  && (Left == other.Left) && (Right == other.Right)
159  && (Rule.Equals(other.Rule));
160  }
161 
162  #endregion
163 
164  #region [ IComparable ]
165 
179  public int CompareTo(Edge other)
180  {
181  int temp = Left.CompareTo(other.Left);
182 
183  if (temp == 0) temp = Right.CompareTo(other.Right);
184  if (temp == 0) temp = Rule.CompareTo(other.Rule);
185  if (temp == 0) temp = Dot.CompareTo(other.Dot);
186 
187  return temp;
188  }
189 
190  #endregion
191 
192  }
193 }
IReadOnlyList< Symbol > RHS
The Right-Hand Side (RHS) of the rule.
Definition: Rule.cs:70
override bool Equals(object obj)
Definition: Edge.cs:124
Edge(Rule rule, int dot, int left, int right)
Creates a new edge. If the constructor does not throw any exception, the edge is guaranteed to be val...
Definition: Edge.cs:55
Represents an edge in the chart created by the ChartParser. This is an immutable class. The Edge contains four values:
Definition: Edge.cs:37
override int GetHashCode()
Definition: Edge.cs:130
int Left
Left node number.
Definition: Edge.cs:79
override string ToString()
Definition: Edge.cs:133
override bool Equals(object obj)
Definition: Rule.cs:83
bool Equals(Edge other)
Definition: Edge.cs:155
An exception thrown when attempting to create an invalid Mercury.Parser.Edge instance.
bool IsValid()
Determines whether this edge is valid.
Definition: Edge.cs:117
Represents a Context-Free Grammar rule. This is an immutable class.
Definition: Rule.cs:23
int CompareTo(Edge other)
Compares the edge with another edge. Defines a lexicographical order as follows:
Definition: Edge.cs:179
Represents a single symbol of the grammar. This is an immutable class. For a symbol name to be val...
Definition: Symbol.cs:51
Rule Rule
The source rule.
Definition: Edge.cs:73