Mercury library 1.0 Translation of CFG grammars into an object model (Bachelor's thesis).

- Main Page
- Packages
- Classes
- Files

- File List

All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
RewriteRuleCollection.cs
Go to the documentation of this file.
1 using Mercury.Nucleus.Collections;
2 using Mercury.Syntax;
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6
7 namespace Mercury.Interpreting
8 {
9 //=============================================================================
10 // RewriteRuleCollection
11 //=============================================================================
12
14 public class RewriteRuleCollection<T> : IReadOnlyCollection<RewriteRule<T>>
15 where T : class
16 {
17 //--[ Private fields ]-----------------------------------------------
18
19 private IReadOnlyList<RewriteRule<T>> rules;
20 private IReadOnlyMultiDictionary<Symbol, RewriteRule<T>> rulemap;
21 private IReadOnlyList<RewriteRule<T>> wildrules;
22
23 // This constructor should be called only by RewriteRuleCollectionBuilder
24 internal RewriteRuleCollection(IEnumerable<RewriteRule<T>> rules,
25 IReadOnlyMultiDictionary<Symbol, RewriteRule<T>> rulemap)
26 {
27 if (rules == null) throw new ArgumentNullException("rules");
28 if (rulemap == null) throw new ArgumentNullException("rulemap");
29
30 this.rules = rules.ToList();
31 this.rulemap = rulemap;
32
33 if (!rulemap.TryGetValue(new Symbol(), out wildrules))
34 wildrules = new RewriteRule<T>[0];
35 }
36
37 //--[ Methods ]------------------------------------------------------
38
44 public IEnumerable<RewriteRule<T>> RulesFor(Symbol head)
45 {
46 IReadOnlyList<RewriteRule<T>> tmp;
47
48 if (!rulemap.TryGetValue(head, out tmp))
49 tmp = new RewriteRule<T>[0];
50
51 return tmp.Concat(wildrules);
52 }
53
54 //--[ Interface implementation ]-------------------------------------
55
56 #region [ IReadOnlyCollection ]
57
58 public int Count { get { return rules.Count; } }
59
60 public IEnumerator<RewriteRule<T>> GetEnumerator()
61 { return rules.GetEnumerator(); }
62
63 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
64 { return rules.GetEnumerator(); }
65
66 #endregion
67
68 }
69
70 //=============================================================================
71 // RewriteRuleCollectionBuilder
72 //=============================================================================
73
78 public class RewriteRuleCollectionBuilder<T>
79 where T : class
80 {
81 //--[ Private fields ]-----------------------------------------------
82
83 private List<RewriteRule<T>> rules;
84 private UniqueListMultiDictionary<Symbol, RewriteRule<T>> rulemap;
85
86 //--[ Constructors ]-------------------------------------------------
87
91 public RewriteRuleCollectionBuilder()
92 {
93 rules = new List<RewriteRule<T>>();
94 rulemap = new UniqueListMultiDictionary<Symbol, RewriteRule<T>>();
95 }
96
97 //--[ Methods ]------------------------------------------------------
98
102 public RewriteRuleCollectionBuilder<T> AddRewriteRule(RewriteRule<T> rule)
103 {
104 var alt = (Alternative) rule.Head;
105
106 bool newrule = true;
107 if (alt.Symbols.Count == 0)
108 newrule = rulemap.Add(new Symbol(), rule);
109 else for (int ix = 0; ix < alt.Symbols.Count; ++ix)
110 newrule &= rulemap.Add(alt.Symbols[ix], rule);
111
112 if (newrule) rules.Add(rule);
113
114 return this;
115 }
116
120 public RewriteRuleCollectionBuilder<T> AddRules(RewriteRuleCollection<T> rules)
121 { foreach (var rule in rules) AddRewriteRule(rule); return this; }
122
124 public RewriteRuleCollection<T> GetCollection()
125 { return new RewriteRuleCollection<T>(rules, rulemap); }
126 }
127
128 }
Mercury.Interpreting.RewriteRule< T >
Interpreter rewrite rule
Definition: RewriteRule.cs:145
Mercury.Interpreting.Alternative
Represents an alternative of elements. It may match specified symbols or symboltypes (Nonterminals...
Definition: Element.cs:48
Mercury.Interpreting.RewriteRuleCollection< T >.GetEnumerator
IEnumerator< RewriteRule< T > > GetEnumerator()
Definition: RewriteRuleCollection.cs:60
Mercury.Interpreting.RewriteRuleCollectionBuilder< T >
Can be used to construct Mercury.Interpreting.RewriteRuleCollection{T}
Definition: RewriteRuleCollection.cs:78
Mercury.Interpreting.RewriteRuleCollection< T >
A read-only collection of rewrite rules
Definition: RewriteRuleCollection.cs:14
Mercury.Interpreting.RewriteRuleCollectionBuilder< T >.AddRules
RewriteRuleCollectionBuilder< T > AddRules(RewriteRuleCollection< T > rules)
Adds the collection of rules.
Definition: RewriteRuleCollection.cs:120
Mercury.Interpreting.RewriteRuleCollectionBuilder< T >.GetCollection
RewriteRuleCollection< T > GetCollection()
Creates a RewriteRuleCollection with rules from this builder.
Definition: RewriteRuleCollection.cs:124
Mercury.Interpreting.RewriteRuleCollection< T >.RulesFor
IEnumerable< RewriteRule< T > > RulesFor(Symbol head)
Returns a list of rules whose LHS Structure's head matches the given head .
Definition: RewriteRuleCollection.cs:44
Mercury.Interpreting.RewriteRule< T >.Head
IElement Head
Value of LHS's root node.
Definition: RewriteRule.cs:177
Mercury.Interpreting.RewriteRuleCollectionBuilder< T >.RewriteRuleCollectionBuilder
RewriteRuleCollectionBuilder()
Initializes a new instance of the RewriteRuleCollectionBuilder{T} class.
Definition: RewriteRuleCollection.cs:91
Mercury.Interpreting.RewriteRuleCollectionBuilder< T >.AddRewriteRule
RewriteRuleCollectionBuilder< T > AddRewriteRule(RewriteRule< T > rule)
Adds a RewriteRule to the collection
Definition: RewriteRuleCollection.cs:102
Mercury.Syntax.Symbol
Represents a single symbol of the grammar. This is an immutable class. For a symbol name to be val...
Definition: Symbol.cs:51