-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFsa.Builder.cs
211 lines (178 loc) · 6.47 KB
/
Fsa.Builder.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
namespace SourceGenerator;
public static partial class FsaExtensions
{
public static List<Fsa> MergeFrontier(this List<Fsa> frontier)
{
var merge = new Fsa();
foreach (var state in frontier)
{
state.Epsilon.Add(merge);
}
return [merge];
}
}
public partial class Fsa
{
/// <summary>
/// Creates new nodes in the FSA to match the provided word. The resulting
/// machine is likely nondeterministic, depending on which regular expression
/// is provided and any logical "ORs" or ambiguous tokens.
///
/// Paths are not reused nor optimized at this stage. If a letter is already
/// in the "next" list of a state, it is added as an epsilon transition.
/// </summary>
public void Build(string word, int accept, out List<Fsa> frontier)
{
_ParseOR(word, 0, out var end, out frontier);
if (end < word.Length)
{
throw new ApplicationException($"Unexpected '{word[end]}' at offset {end}");
}
if (accept > 0)
{
foreach (var state in frontier)
{
// If there are already elements, tokens may be ambiguous
state.Accepts.Add(accept);
}
}
}
/// <summary>
/// Creates new nodes in the FSA to match the provided word. The resulting
/// machine is likely nondeterministic, depending on which regular expression
/// is provided and any logical "ORs" or ambiguous tokens.
///
/// Paths are not reused nor optimized at this stage. If a letter is already
/// in the "next" list of a state, it is added as an epsilon transition.
/// </summary>
public void Build(string word, int accept)
{
Build(word, accept, out var _);
}
protected void _AddTransition(char on, Fsa to)
{
if (Next.ContainsKey(on))
{
// Become nondeterministic on second occurrence of letter
var epsState = new Fsa();
epsState.Next[on] = to;
Epsilon.Add(epsState);
} else
{
Next[on] = to;
}
}
protected void _ParsePARENS(string word, int start, out int end, out List<Fsa> frontier)
{
// Revert to top of parsing hierarchy
_ParseOR(word, start + 1, out end, out frontier);
// Combine possible set of states down to one with epsilon
frontier = frontier.MergeFrontier();
if (end >= word.Length || word[end] != ')')
{
throw new ApplicationException($"Expected ')' at offset {end}");
}
end++;
}
protected void _ParseOR(string word, int start, out int end, out List<Fsa> frontier)
{
// "- 1" to counteract the initial "++end"
end = start - 1;
frontier = [];
do
{
_ParseSERIES(word, ++end, out end, out var _frontier);
frontier.AddRange(_frontier);
} while (end < word.Length && word[end] == '|');
}
protected void _ParseSERIES(string word, int start, out int end, out List<Fsa> frontier, bool escaped = false)
{
if (start >= word.Length
|| (!escaped && (word[start] == ')' || word[start] == '|')))
{
end = start;
frontier = [this];
return;
}
_ParsePLUS(word, start, out end, out frontier, escaped: escaped);
frontier.Single()._ParseSERIES(word, end, out end, out frontier);
}
protected void _ParsePLUS(string word, int start, out int end, out List<Fsa> frontier, bool escaped = false)
{
if (!escaped && (word[start] == '+' || word[start] == '{' || word[start] == '*'))
{
end = start;
goto infinite_loop_detected;
}
if (!escaped && word[start] == '?')
{
end = start;
throw new ApplicationException($"Expected optional expression before offset {end}");
}
var epsState = new Fsa();
epsState._ParseLETTER(word, start, out end, out frontier, escaped: escaped);
if (end < word.Length
&& (word[end] == '+' || word[end] == '{' || word[end] == '?' || word[end] == '*'))
{
if (word[end] != '?'
// Detect any paths from this state directly to the frontier--
// creating a cycle here would cause infinite loops
&& (frontier.Contains(epsState) || frontier.Intersect(epsState.EpsilonClosure()).Any()))
{
goto infinite_loop_detected;
}
switch (word[end])
{
case '+':
end++;
break;
case '{':
_EXT_ParsePLUS_Bounded(word, ref start, ref end, ref frontier, escaped: escaped);
return;
case '?':
_EXT_ParsePLUS_Optional(word, ref start, ref end, ref frontier, escaped: escaped);
return;
case '*':
_EXT_ParsePLUS_Star(word, ref start, ref end, ref frontier, escaped: escaped);
return;
}
// Keep loop as sub-state to avoid unintended transitions
Epsilon.Add(epsState);
foreach (var state in frontier)
{
state.Epsilon.Add(epsState);
}
} else
{
// No looping to consider; merge in sub-state's transitions
Epsilon.AddRange(epsState.Epsilon);
foreach (var (k, v) in epsState.Next)
{
_AddTransition(k, v);
}
}
return;
infinite_loop_detected:
throw new ApplicationException($"Cannot use '+', '{{}}', or '*' (at offset {end}) on the empty string");
}
protected void _ParseLETTER(string word, int start, out int end, out List<Fsa> frontier, bool escaped = false)
{
var letter = word[start];
switch (letter)
{
case '\\' when !escaped:
_ParseSERIES(word, start + 1, out end, out frontier, escaped: true);
return;
case '(' when !escaped:
_ParsePARENS(word, start, out end, out frontier);
return;
case '[' when !escaped:
_EXT_ParseRANGE(word, start, out end, out frontier);
return;
}
var newState = new Fsa(letter);
_AddTransition(letter, newState);
end = start + 1;
frontier = [newState];
}
}