-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternElement.cs
More file actions
322 lines (265 loc) · 11.7 KB
/
PatternElement.cs
File metadata and controls
322 lines (265 loc) · 11.7 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
using System.Collections.Generic;
namespace TokenDiscovery {
public enum Look {
Here,
Behind,
Ahead,
}
/// <summary>
/// The Pattern class represents its hierarchic patterns as a tree of PatternElement nodes
/// </summary>
/// <remarks>
///
/// The general structure of a simple sequence of patterns like "A B C" is a list in the
/// .Alternatives property with one sub-list containing a single sequence of 3 elements. Each
/// having its .Pattern property set to point to one named property (eg the "A" pattern).
///
/// A nested pattern like "A (B C) D" would be similar, but the second element would not have its
/// .Pattern property set. Instead its .Alternatives collection property would contain a single
/// sequence for "B C". This is a trivial example of nesting that behaves the same as if
/// it were "A B C D", thanks to the associative property of these patterns. But it does mean
/// that the "(B C)" sub-tree can have separate quantifiers. Eg "A (B C)+ D", which would
/// violate associativity.
///
/// Taking this one step further, "A (B | C D) E" would have a similar structure to the above.
/// However, its second node would take more advantage of the pattern.Root.Alternatives
/// collection. Its first sub-list would contain the "B" sequence and its second would contain
/// the "C D" sequence.
///
/// Note that Parser.NewPattern() also does some optimization to eliminate some unnecessary
/// nesting. For example "A (B C) D" would be reduced to its equivalent "A B C D". However
/// "A (B C)+ D" would not be reduced because of the "+" quantifier. One upshot of this reduction
/// process is that two patterns can effectively be compared for functional equivalence.
///
/// </remarks>
public class PatternElement {
#region Public properties
public Parser Parser;
public Pattern Pattern;
/// <summary>
/// A list of alternative sequences of child elements
/// </summary>
public List<List<PatternElement>> Alternatives;
public int MinQuantity = 1;
public int MaxQuantity = 1; // -1 = unlimited
public Look Look = Look.Here;
#endregion
public PatternElement(Parser parser) {
Parser = parser;
}
#region Render a plain-text representation of a pattern
public override string ToString() {
return ToString(false, false, true);
}
public string ToString(bool useIdsForNameless, bool fullDepth, bool topLevel) {
string text = "";
if (Pattern != null) {
text += Pattern.ToString(useIdsForNameless, fullDepth, false);
if (!topLevel && (text.Contains(' ') || text.Contains('|'))) {
text = "(" + text + ")";
}
} else { // Alternatives
for (int i = 0; i < Alternatives.Count; i++) {
if (i > 0) text += " | ";
var alt = Alternatives[i];
for (int j = 0; j < alt.Count; j++) {
var subText = alt[j].ToString(useIdsForNameless, fullDepth, false);
if (j > 0) text += " ";
text += subText;
}
}
if (
(
Alternatives.Count > 1 &&
!topLevel
) || (
(
Alternatives[0].Count > 1
) && (
MinQuantity != 1 ||
MaxQuantity != 1 ||
Look != Look.Here
)
)
) {
text = "(" + text + ")";
}
}
if (Look == Look.Behind) text = "<" + text;
if (Look == Look.Ahead) text = ">" + text;
if (MinQuantity == 0 && MaxQuantity == 0) {
text += "!";
} else if (MinQuantity == 0 && MaxQuantity == 1) {
text += "?";
} else if (MinQuantity == 0 && MaxQuantity == -1) {
text += "*";
} else if (MinQuantity == 1 && MaxQuantity == -1) {
text += "+";
} else if (MinQuantity != 1 && MaxQuantity == -1) {
text += "{" + MinQuantity + "+}";
} else if (MinQuantity != 1 && MaxQuantity == MinQuantity) {
text += "{" + MinQuantity + "}";
} else if (MinQuantity != 1 || MaxQuantity != 1) {
text += "{" + MinQuantity + "-" + MaxQuantity + "}";
}
return text;
}
public string ToDebugString(string indent) {
string text = "{\n";
if (MinQuantity != 1) text += indent + " MinQuantity: " + MinQuantity + "\n";
if (MaxQuantity != 1) text += indent + " MaxQuantity: " + (MaxQuantity == -1 ? "Unlimited" : MaxQuantity) + "\n";
if (Look != Look.Here) text += indent + " Look: " + Look + "\n";
if (Pattern != null) {
text += indent + " Pattern: " + Pattern.Identity;
if (Pattern.Name == null) {
text += " ( " + Pattern.Describe() + " )";
}
text += "\n";
} else {
text += indent + " Alternatives: [\n";
for (int i = 0; i < Alternatives.Count; i++) {
if (i > 0) text += " ----------\n";
foreach (var subPattern in Alternatives[i]) {
text += indent + " " + subPattern.ToDebugString(indent + " ");
}
}
text += indent + " ]\n";
}
text += indent + "}\n";
return text;
}
public bool DependsOn(Pattern otherPattern) {
if (Pattern == otherPattern) return true;
foreach (var sequence in Alternatives) {
foreach (var elem in sequence) {
if (elem.Pattern == otherPattern) return true;
}
}
return false;
}
public int CalculatePenalty() {
int penalty = 1;
if (Look != Look.Here) penalty += 2;
if (Pattern != null) {
penalty += Pattern.Penalty;
} else {
//if (Alternatives.Count > 1) penalty++;
int maxPenalty = 0;
foreach (var sequence in Alternatives) {
int subPenalty = 0;
foreach (var elem in sequence) {
subPenalty += elem.CalculatePenalty();
}
if (subPenalty > maxPenalty) maxPenalty = subPenalty;
}
penalty += maxPenalty;
}
return penalty;
}
#endregion
#region Parse plain-text representation of a pattern to construct the hierarchic definition
public void Clear() {
Pattern = null;
Alternatives = null;
MinQuantity = 1;
MaxQuantity = 1;
}
public void Import(PatternElement from) {
Pattern = from.Pattern;
Alternatives = from.Alternatives;
Look = from.Look;
MinQuantity = from.MinQuantity;
MaxQuantity = from.MaxQuantity;
}
#endregion
#region Parsing text
public bool Match(TokenChain chain, Token parentToken, int startAt, out int endAt) {
int origStartAt = startAt;
endAt = startAt;
if (Look == Look.Behind) {
if (Pattern != null) {
int seqEndAt = startAt - 1;
if (seqEndAt < 0) {
if (MaxQuantity == 0) return true; // We didn't want to find this
return false; // We did want to find this
}
if (chain.Tails[seqEndAt].TryGetValue(Pattern.Id, out Token _)) {
if (MaxQuantity == 0) return false; // We didn't want to find this
return true; // We did want to find this
}
if (MaxQuantity == 0) return true; // We didn't want to find this
return false; // We did want to find this
} else {
foreach (var sequence in Alternatives) {
int seqEndAt = startAt - 1;
if (seqEndAt < 0) {
if (MaxQuantity == 0) return true; // We didn't want to find this
return false; // We did want to find this
}
bool allMatched = true;
sequence.Reverse(); // We must reverse the sequence order because we're looking backward
foreach (var elem in sequence) {
if (seqEndAt < 0) {
allMatched = false;
break; // Failed to match this alternative sequence
}
if (!chain.Tails[seqEndAt].TryGetValue(elem.Pattern.Id, out Token token)) {
allMatched = false;
break; // Failed to match this alternative sequence
}
seqEndAt = token.StartAt - 1;
}
if (allMatched) {
if (MaxQuantity == 0) return false; // We didn't want to find this
return true; // We did want to find this
}
}
if (MaxQuantity == 0) return true; // We didn't want to find this
return false; // We did want to find this
}
}
int matchCount = 0;
while (
MaxQuantity == -1 ||
(MaxQuantity == 0 && matchCount < 1) ||
matchCount < MaxQuantity
) {
int innerEndAt;
if (Pattern != null) {
var token = Pattern.Match(chain, parentToken, startAt);
if (token == null) break;
endAt = startAt + token.Length;
startAt = endAt;
matchCount++;
} else {
bool allMatched = true;
foreach (var sequence in Alternatives) {
allMatched = true;
int seqStartAt = startAt;
foreach (var elem in sequence) {
if (!elem.Match(chain, parentToken, seqStartAt, out innerEndAt)) {
allMatched = false;
break; // Failed to match this alternative sequence
}
seqStartAt = innerEndAt;
}
if (allMatched) {
matchCount++;
startAt = seqStartAt;
endAt = startAt;
break;
}
}
if (!allMatched) break;
}
}
if (MaxQuantity == 0 && matchCount > 0) return false;
if (matchCount >= MinQuantity) {
if (Look == Look.Ahead) endAt = origStartAt;
return true;
}
return false;
}
#endregion
}
}