Mercury library  1.0
Translation of CFG grammars into an object model (Bachelor's thesis).
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
Entries.cs
Go to the documentation of this file.
1 using Mercury.Nucleus.Trees;
2 using Mercury.Syntax;
3 using System.Collections.Generic;
4 using System.IO;
5 using System.Linq;
6 using System.Text;
7 
8 namespace Mercury.Interpreting
9 {
10 //=============================================================================
11 // << Evaluation Entries >>
12 //=============================================================================
13 
14 //==<< DataEntry >>==========================================================
15 
17  public interface DataEntry
18  { }
19 
20 //==<< TextEntry >>==========================================================
21 
25  public sealed class TextEntry : DataEntry
26  {
27  //--[ Internal ]-----------------------------------------------------
28 
29  internal string msg;
30  internal object[] args;
31  internal string mem = null;
32 
33  internal TextEntry(string actname, string msg, params object[] args)
34  {
35  ActionName = actname;
36  this.msg = msg;
37  this.args = args;
38  }
39 
40  //--[ Properties ]---------------------------------------------------
41 
43  public string ActionName { get; internal set; }
44 
46  public string Text { get { return mem == null ? mem = string.Format(msg, args) : mem; } }
47  }
48 
49 //==<< TreeEntry >>==========================================================
50 
52  public sealed class TreeEntry<T> : DataEntry
53  where T : class
54  {
55  //--[ Internal ]-----------------------------------------------------
56 
57  internal List<DataEntry> Rules { get; set; }
58 
59  internal TreeEntry(Tree<Symbol> t)
60  {
61  Tree = t;
62  Succeeded = false;
63  Rules = new List<DataEntry>();
64  }
65 
66  //--[ Properties ]---------------------------------------------------
67 
69  public Tree<Symbol> Tree { get; private set; }
70 
72  public bool Succeeded { get; internal set; }
73 
75  public IReadOnlyList<DataEntry> RuleEntries { get { return Rules; } }
76  }
77 
78 //==<< MemoEntry >>==========================================================
79 
82  public sealed class MemoEntry<T> : DataEntry
83  {
85  public T MemoizedValue { get; internal set; }
86  }
87 
88 //==<< RuleEntry >>==========================================================
89 
92  public sealed class RuleEntry<T> : DataEntry
93  where T : class
94  {
95  //--[ Internal ]-----------------------------------------------------
96 
97  internal List<DataEntry> Entries { get; set; }
98 
99  internal RuleEntry(RewriteRule<T> rwr)
100  {
101  RewriteRule = rwr;
102  Match = false;
103  Value = null;
104  Error = true;
105  Instances = null;
106  Entries = new List<DataEntry>();
107  }
108 
109  //--[ Properties ]---------------------------------------------------
110 
112  public RewriteRule<T> RewriteRule { get; private set; }
113 
115  public bool Match { get; internal set; }
116 
118  public T Value { get; internal set; }
119 
124  public bool Error { get; internal set; }
125 
127  public InstanceList Instances { get; internal set; }
128 
133  public IReadOnlyList<DataEntry> SubEntries { get { return Entries; } }
134 
135  }
136 
137 //=============================================================================
138 // EntryWriter
139 //=============================================================================
140 
146  public static class EntryWriter<T>
147  where T : class
148  {
149  //--[ Private methods ]---------------------------------------------
150 
151  #region [ DEntry ]
152 
153  private static void WriteDEntry(int lvl, DataEntry dentry, TextWriter o)
154  {
155  TreeEntry<T> tentry = dentry as TreeEntry<T>;
156  if (tentry != null) WriteDEntry(lvl, tentry, o);
157  else WriteDEntry(lvl, dentry as TextEntry, o);
158  }
159 
160  private static void WriteDEntry(int lvl, TreeEntry<T> tentry, TextWriter o)
161  {
162  var pad = new string('\t', lvl);
163  o.WriteLine("{0}>> {1}", pad, tentry.Tree);
164  foreach (var rentry in tentry.RuleEntries)
165  WriteREntry(lvl + 1, rentry, o);
166 
167  o.WriteLine("{0}<< {{{1}}}", pad, tentry.Succeeded ? "SUCC" : "FAIL");
168  o.WriteLine();
169  }
170 
171  private static void WriteDEntry(int lvl, TextEntry tentry, TextWriter o)
172  {
173  var pad = new string('\t', lvl);
174  o.WriteLine("{0}:: {1}: {2}", pad, tentry.ActionName, tentry.Text);
175  }
176 
177  #endregion
178 
179  #region [ REntry ]
180 
181  private static void WriteREntry(int lvl, DataEntry dentry, TextWriter o)
182  {
183  var rentry = dentry as RuleEntry<T>;
184  if (rentry != null) WriteREntry(lvl, rentry, o);
185  else WriteREntry(lvl, dentry as MemoEntry<T>, o);
186  }
187 
188  private static void WriteREntry(int lvl, MemoEntry<T> mentry, TextWriter o)
189  {
190  var pad = new string('\t', lvl);
191  o.WriteLine("{0}~~ {{MEMOIZED}} <{1}>", pad,
192  mentry.MemoizedValue == null ? "(null)" : mentry.MemoizedValue.ToString());
193  }
194 
195  private static void WriteREntry(int lvl, RuleEntry<T> rentry, TextWriter o)
196  {
197  var pad = new string('\t', lvl);
198  o.WriteLine("{0}-> {1}", pad, rentry.RewriteRule);
199  if (rentry.Error)
200  o.WriteLine("{0} {{<EXCEPTION>}}", pad);
201  else
202  o.Write("{0} {{{1}}} ", pad, rentry.Match ? "MATCH" : "no match");
203 
204  if (rentry.Match && (rentry.Instances.Count > 0))
205  {
206  int max = rentry.Instances.Select(x => x.Var.Name.Length).Max();
207  var str = "{0,-" + max + "}";
208 
209  int ix = 0;
210  foreach (var instance in rentry.Instances)
211  {
212  o.Write(ix++ == 0 ? "{1} " : "{0} ~~> ", pad, "~~>");
213  var obj = (instance.Var is Wildcard)
214  ? "{" + string.Join("; ", (instance.Value as Tree<Symbol>[]).Select(x => x.ToString(2))) + "}"
215  : (instance.Value as Tree<Symbol>).ToString(2);
216  o.WriteLine(str + " = {1}", instance.Var.Name, obj);
217  }
218  }
219  else
220  o.WriteLine();
221 
222  foreach (var tentry in rentry.SubEntries)
223  WriteDEntry(lvl + 1, tentry, o);
224 
225  if (rentry.Match)
226  o.WriteLine("{0}<- ret <{1}>", pad, rentry.Value == null ? "(null)" : rentry.Value.ToString());
227  }
228 
229  #endregion
230 
231  //--[ Methods ]------------------------------------------------------
232 
236  public static void WriteEntries(TreeEntry<T> root, TextWriter o)
237  { WriteDEntry(0, root, o); }
238 
239  }
240 
241 }
Entry base (dummy) interface.
Definition: Entries.cs:17
Describes a rule that has been used to interpret a tree.
Definition: Entries.cs:92
Represents an entry for tree interpretation.
Definition: Entries.cs:52
Represents memoized value
Definition: Entries.cs:82
Stores informations logged by actions or internal Evaluator.
Definition: Entries.cs:25
bool Match
Indicates whether the LHS of rule matched the tree.
Definition: Entries.cs:115
InstanceList Instances
List of instances or null if rule did not match.
Definition: Entries.cs:127
IReadOnlyList< DataEntry > SubEntries
List of TextEntries or TreeEntries that occured while evaluating action of this rule.
Definition: Entries.cs:133
IReadOnlyList< DataEntry > RuleEntries
List of rule entries that were tried.
Definition: Entries.cs:75
static void WriteEntries(TreeEntry< T > root, TextWriter o)
Writes the entries into the stream.
Definition: Entries.cs:236
int Count
Number of elements in the list
Definition: Instance.cs:112
An element that matches zero or more values. Can be used as a variable, but the actioncall must be of...
Definition: Element.cs:370
Represents the list of instances
Definition: Instance.cs:94
bool Error
Indicates whether an exception has been thrown when evaluating the rule action.
Definition: Entries.cs:124
A derivation tree