1 using Mercury.Exceptions;
7 namespace Mercury.Syntax
37 public class Edge : IEquatable<Edge>, IComparable<Edge>
55 public Edge(
Rule rule,
int dot,
int left,
int right)
57 if (rule == null)
throw new ArgumentNullException(
"rule");
59 Rule = rule; Dot = dot;
60 Left = left; Right = right;
64 IsActive = !Rule.IsEpsilon && (Dot < Rule.RHS.Count);
65 IsPassive = !IsActive;
67 NextSymbol = IsActive ? Rule.RHS[Dot] : null;
76 public int Dot {
get;
private set; }
79 public int Left {
get;
private set; }
82 public int Right {
get;
private set; }
91 public bool IsActive {
get;
private set; }
100 public bool IsPassive {
get;
private set; }
106 public Symbol NextSymbol {
get;
private set; }
118 {
return (0 <= Dot) && (Dot <= Rule.RHS.Count) && (Left <= Right); }
127 return edge == null ?
false : Equals(edge);
131 {
return 23 * Rule.GetHashCode() + 17 * Dot + 11 * Left + 7 * Right; }
135 StringBuilder sb =
new StringBuilder(
string.Format(
"({0}, {1}) ", Left, Right))
136 .Append(Rule.LHS).Append(
" ->");
138 for (
int ix = 0; ix < Rule.RHS.Count; ++ix)
140 sb.Append(ix == Dot ?
" . " :
" ");
141 if (!
Rule.
RHS[ix].IsEpsilon) sb.Append(Rule.RHS[ix].ToString());
144 if (Dot ==
Rule.
RHS.Count) sb.Append(
" .");
146 return sb.ToString();
153 #region [ IEquatable ]
157 return (other != null) && (Dot == other.Dot)
158 && (Left == other.
Left) && (Right == other.Right)
164 #region [ IComparable ]
181 int temp = Left.CompareTo(other.Left);
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);
IReadOnlyList< Symbol > RHS
The Right-Hand Side (RHS) of the rule.
override bool Equals(object obj)
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...
Represents an edge in the chart created by the ChartParser. This is an immutable class. The Edge contains four values:
override int GetHashCode()
int Left
Left node number.
override string ToString()
override bool Equals(object obj)
An exception thrown when attempting to create an invalid Mercury.Parser.Edge instance.
bool IsValid()
Determines whether this edge is valid.
Represents a Context-Free Grammar rule. This is an immutable class.
int CompareTo(Edge other)
Compares the edge with another edge. Defines a lexicographical order as follows:
Represents a single symbol of the grammar. This is an immutable class. For a symbol name to be val...
Rule Rule
The source rule.