-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrammar.cs
132 lines (111 loc) · 3.72 KB
/
Grammar.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using BFParser.DebugTools;
using BFParser.Parsers;
using BFParser.Parsers.Combinators;
using BFParser.Parsers.DebugTools;
namespace BFParser
{
public class Grammar : IDictionary<string, CoreParser>
{
private readonly Dictionary<string, CoreParser> _rules;
private readonly string _goalRuleName;
public Grammar(string goalRuleName, IDictionary<string, CoreParser> old = null)
{
_goalRuleName = goalRuleName;
_rules = old is null ? new Dictionary<string, CoreParser>() : new Dictionary<string, CoreParser>(old);
}
public IEnumerator<KeyValuePair<string, CoreParser>> GetEnumerator()
{
return _rules.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(KeyValuePair<string, CoreParser> item)
{
_rules.Add(item.Key, item.Value);
}
public void Add(string key, CoreParser value)
{
_rules.Add(key, value);
}
public void Clear()
{
_rules.Clear();
}
public bool Contains(KeyValuePair<string, CoreParser> item)
{
return _rules.Contains(item);
}
public void CopyTo(KeyValuePair<string, CoreParser>[] array, int arrayIndex)
{
var result = new KeyValuePair<string,CoreParser>[_rules.Count];
var i = 0;
foreach (var item in _rules)
{
result[i] = item;
++i;
}
}
public bool Remove(KeyValuePair<string, CoreParser> item)
{
return _rules.Remove(item.Key);
}
public int Count => _rules.Count;
public bool IsReadOnly => false;
public bool ContainsKey(string key)
{
return _rules.ContainsKey(key);
}
public bool Remove(string key)
{
return _rules.Remove(key);
}
public bool TryGetValue(string key, out CoreParser value)
{
return _rules.TryGetValue(key, out value);
}
public CoreParser this[string key]
{
get => _rules[key];
set => _rules[key] = value;
}
public ICollection<string> Keys => _rules.Keys;
public ICollection<CoreParser> Values => _rules.Values;
public CoreParser Goal => this[_goalRuleName];
public void InitGrammar()
{
foreach (var rule in _rules)
{
rule.Value.InitGrammar(this, rule.Key);
}
}
public void Visit(CoreGrammarVisitor<ConvertToDOTVisitor> visitor)
{
visitor.Visit(this);
}
public string Dot(string startRuleName = null)
{
var visitor = new CoreGrammarVisitor<ConvertToDOTVisitor>();
Visit(visitor);
return visitor.GetResult(startRuleName ?? _goalRuleName) as string;
}
public CoreParser ExpandThis(ParserCallGrammarParser basicParser, List<KeyValuePair<string, List<string>>> operations)
{
for (int i = 0; i < operations.Count; ++i)
{
var newRuleName = operations[i].Key;
var ops = operations[i].Value.Select(P.T).Aggregate((current, operation) => current | operation);
var newRule = basicParser + P.ZI(ops + basicParser);
Add(newRuleName, newRule);
basicParser = P.C(newRuleName) as ParserCallGrammarParser;
}
return basicParser;
}
}
}