Debian Codegraph @

mcs/class/referencesource/System/regex/system/text/regularexpressions/RegexFCD.cs

raw
1 //------------------------------------------------------------------------------
2 // <copyright file="RegexFCD.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6
7 // This RegexFCD class is internal to the Regex package.
8 // It builds a bunch of FC information (RegexFC) about
9 // the regex for optimization purposes.
10
11 // Implementation notes:
12 //
13 // This step is as simple as walking the tree and emitting
14 // sequences of codes.
15
16 namespace System.Text.RegularExpressions {
17
18 using System.Collections;
19 using System.Globalization;
20
21 internal sealed class RegexFCD {
22 private int[] _intStack;
23 private int _intDepth;
24 private RegexFC[] _fcStack;
25 private int _fcDepth;
26 private bool _skipAllChildren; // don't process any more children at the current level
27 private bool _skipchild; // don't process the current child.
28 private bool _failed = false;
29
30 private const int BeforeChild = 64;
31 private const int AfterChild = 128;
32
33 // where the regex can be pegged
34
35 internal const int Beginning = 0x0001;
36 internal const int Bol = 0x0002;
37 internal const int Start = 0x0004;
38 internal const int Eol = 0x0008;
39 internal const int EndZ = 0x0010;
40 internal const int End = 0x0020;
41 internal const int Boundary = 0x0040;
42 internal const int ECMABoundary = 0x0080;
43
44 /*
45 * This is the one of the only two functions that should be called from outside.
46 * It takes a RegexTree and computes the set of chars that can start it.
47 */
48 internal static RegexPrefix FirstChars(RegexTree t) {
49 RegexFCD s = new RegexFCD();
50 RegexFC fc = s.RegexFCFromRegexTree(t);
51
52 if (fc == null || fc._nullable)
53 return null;
54
55 CultureInfo culture = ((t._options & RegexOptions.CultureInvariant) != 0) ? CultureInfo.InvariantCulture : CultureInfo.CurrentCulture;
56 return new RegexPrefix(fc.GetFirstChars(culture), fc.IsCaseInsensitive());
57 }
58
59 /*
60 * This is a related computation: it takes a RegexTree and computes the
61 * leading substring if it see one. It's quite trivial and gives up easily.
62 */
63 internal static RegexPrefix Prefix(RegexTree tree) {
64 RegexNode curNode;
65 RegexNode concatNode = null;
66 int nextChild = 0;
67
68 curNode = tree._root;
69
70 for (;;) {
71 switch (curNode._type) {
72 case RegexNode.Concatenate:
73 if (curNode.ChildCount() > 0) {
74 concatNode = curNode;
75 nextChild = 0;
76 }
77 break;
78
79 case RegexNode.Greedy:
80 case RegexNode.Capture:
81 curNode = curNode.Child(0);
82 concatNode = null;
83 continue;
84
85 case RegexNode.Oneloop:
86 case RegexNode.Onelazy:
87 if (curNode._m > 0) {
88 string pref = String.Empty.PadRight(curNode._m, curNode._ch);
89 return new RegexPrefix(pref, 0 != (curNode._options & RegexOptions.IgnoreCase));
90 }
91 else
92 return RegexPrefix.Empty;
93
94 case RegexNode.One:
95 return new RegexPrefix(curNode._ch.ToString(CultureInfo.InvariantCulture), 0 != (curNode._options & RegexOptions.IgnoreCase));
96
97 case RegexNode.Multi:
98 return new RegexPrefix(curNode._str, 0 != (curNode._options & RegexOptions.IgnoreCase));
99
100 case RegexNode.Bol:
101 case RegexNode.Eol:
102 case RegexNode.Boundary:
103 case RegexNode.ECMABoundary:
104 case RegexNode.Beginning:
105 case RegexNode.Start:
106 case RegexNode.EndZ:
107 case RegexNode.End:
108 case RegexNode.Empty:
109 case RegexNode.Require:
110 case RegexNode.Prevent:
111 break;
112
113 default:
114 return RegexPrefix.Empty;
115 }
116
117 if (concatNode == null || nextChild >= concatNode.ChildCount())
118 return RegexPrefix.Empty;
119
120 curNode = concatNode.Child(nextChild++);
121 }
122 }
123
124 /*
125 * Yet another related computation: it takes a RegexTree and computes the
126 * leading anchors that it encounters.
127 */
128 internal static int Anchors(RegexTree tree) {
129 RegexNode curNode;
130 RegexNode concatNode = null;
131 int nextChild = 0;
132 int result = 0;
133
134 curNode = tree._root;
135
136 for (;;) {
137 switch (curNode._type) {
138 case RegexNode.Concatenate:
139 if (curNode.ChildCount() > 0) {
140 concatNode = curNode;
141 nextChild = 0;
142 }
143 break;
144
145 case RegexNode.Greedy:
146 case RegexNode.Capture:
147 curNode = curNode.Child(0);
148 concatNode = null;
149 continue;
150
151 case RegexNode.Bol:
152 case RegexNode.Eol:
153 case RegexNode.Boundary:
154 case RegexNode.ECMABoundary:
155 case RegexNode.Beginning:
156 case RegexNode.Start:
157 case RegexNode.EndZ:
158 case RegexNode.End:
159 return result | AnchorFromType(curNode._type);
160
161 case RegexNode.Empty:
162 case RegexNode.Require:
163 case RegexNode.Prevent:
164 break;
165
166 default:
167 return result;
168 }
169
170 if (concatNode == null || nextChild >= concatNode.ChildCount())
171 return result;
172
173 curNode = concatNode.Child(nextChild++);
174 }
175 }
176
177 /*
178 * Convert anchor type to anchor bit.
179 */
180 private static int AnchorFromType(int type) {
181 switch (type) {
182 case RegexNode.Bol: return Bol;
183 case RegexNode.Eol: return Eol;
184 case RegexNode.Boundary: return Boundary;
185 case RegexNode.ECMABoundary: return ECMABoundary;
186 case RegexNode.Beginning: return Beginning;
187 case RegexNode.Start: return Start;
188 case RegexNode.EndZ: return EndZ;
189 case RegexNode.End: return End;
190 default: return 0;
191 }
192 }
193
194 #if DBG
195 internal static String AnchorDescription(int anchors) {
196 StringBuilder sb = new StringBuilder();
197
198 if (0 != (anchors & Beginning)) sb.Append(", Beginning");
199 if (0 != (anchors & Start)) sb.Append(", Start");
200 if (0 != (anchors & Bol)) sb.Append(", Bol");
201 if (0 != (anchors & Boundary)) sb.Append(", Boundary");
202 if (0 != (anchors & ECMABoundary)) sb.Append(", ECMABoundary");
203 if (0 != (anchors & Eol)) sb.Append(", Eol");
204 if (0 != (anchors & End)) sb.Append(", End");
205 if (0 != (anchors & EndZ)) sb.Append(", EndZ");
206
207 if (sb.Length >= 2)
208 return(sb.ToString(2, sb.Length - 2));
209
210 return "None";
211 }
212 #endif
213
214 /*
215 * private constructor; can't be created outside
216 */
217 private RegexFCD() {
218 _fcStack = new RegexFC[32];
219 _intStack = new int[32];
220 }
221
222 /*
223 * To avoid recursion, we use a simple integer stack.
224 * This is the push.
225 */
226 private void PushInt(int I) {
227 if (_intDepth >= _intStack.Length) {
228 int [] expanded = new int[_intDepth * 2];
229
230 System.Array.Copy(_intStack, 0, expanded, 0, _intDepth);
231
232 _intStack = expanded;
233 }
234
235 _intStack[_intDepth++] = I;
236 }
237
238 /*
239 * True if the stack is empty.
240 */
241 private bool IntIsEmpty() {
242 return _intDepth == 0;
243 }
244
245 /*
246 * This is the pop.
247 */
248 private int PopInt() {
249 return _intStack[--_intDepth];
250 }
251
252 /*
253 * We also use a stack of RegexFC objects.
254 * This is the push.
255 */
256 private void PushFC(RegexFC fc) {
257 if (_fcDepth >= _fcStack.Length) {
258 RegexFC[] expanded = new RegexFC[_fcDepth * 2];
259
260 System.Array.Copy(_fcStack, 0, expanded, 0, _fcDepth);
261 _fcStack = expanded;
262 }
263
264 _fcStack[_fcDepth++] = fc;
265 }
266
267 /*
268 * True if the stack is empty.
269 */
270 private bool FCIsEmpty() {
271 return _fcDepth == 0;
272 }
273
274 /*
275 * This is the pop.
276 */
277 private RegexFC PopFC() {
278 return _fcStack[--_fcDepth];
279 }
280
281 /*
282 * This is the top.
283 */
284 private RegexFC TopFC() {
285 return _fcStack[_fcDepth - 1];
286 }
287
288 /*
289 * The main FC computation. It does a shortcutted depth-first walk
290 * through the tree and calls CalculateFC to emits code before
291 * and after each child of an interior node, and at each leaf.
292 */
293 private RegexFC RegexFCFromRegexTree(RegexTree tree) {
294 RegexNode curNode;
295 int curChild;
296
297 curNode = tree._root;
298 curChild = 0;
299
300 for (;;) {
301 if (curNode._children == null) {
302 // This is a leaf node
303 CalculateFC(curNode._type, curNode, 0);
304 }
305 else if (curChild < curNode._children.Count && !_skipAllChildren) {
306 // This is an interior node, and we have more children to analyze
307 CalculateFC(curNode._type | BeforeChild, curNode, curChild);
308
309 if (!_skipchild) {
310 curNode = (RegexNode)curNode._children[curChild];
311 // this stack is how we get a depth first walk of the tree.
312 PushInt(curChild);
313 curChild = 0;
314 }
315 else {
316 curChild++;
317 _skipchild = false;
318 }
319 continue;
320 }
321
322 // This is an interior node where we've finished analyzing all the children, or
323 // the end of a leaf node.
324 _skipAllChildren = false;
325
326 if (IntIsEmpty())
327 break;
328
329 curChild = PopInt();
330 curNode = curNode._next;
331
332 CalculateFC(curNode._type | AfterChild, curNode, curChild);
333 if (_failed)
334 return null;
335
336 curChild++;
337 }
338
339 if (FCIsEmpty())
340 return null;
341
342 return PopFC();
343 }
344
345 /*
346 * Called in Beforechild to prevent further processing of the current child
347 */
348 private void SkipChild() {
349 _skipchild = true;
350 }
351
352 /*
353 * FC computation and shortcut cases for each node type
354 */
355 private void CalculateFC(int NodeType, RegexNode node, int CurIndex) {
356 bool ci = false;
357 bool rtl = false;
358
359 if (NodeType <= RegexNode.Ref) {
360 if ((node._options & RegexOptions.IgnoreCase) != 0)
361 ci = true;
362 if ((node._options & RegexOptions.RightToLeft) != 0)
363 rtl = true;
364 }
365
366 switch (NodeType) {
367 case RegexNode.Concatenate | BeforeChild:
368 case RegexNode.Alternate | BeforeChild:
369 case RegexNode.Testref | BeforeChild:
370 case RegexNode.Loop | BeforeChild:
371 case RegexNode.Lazyloop | BeforeChild:
372 break;
373
374 case RegexNode.Testgroup | BeforeChild:
375 if (CurIndex == 0)
376 SkipChild();
377 break;
378
379 case RegexNode.Empty:
380 PushFC(new RegexFC(true));
381 break;
382
383 case RegexNode.Concatenate | AfterChild:
384 if (CurIndex != 0) {
385 RegexFC child = PopFC();
386 RegexFC cumul = TopFC();
387
388 _failed = !cumul.AddFC(child, true);
389 }
390
391 if (!TopFC()._nullable)
392 _skipAllChildren = true;
393 break;
394
395 case RegexNode.Testgroup | AfterChild:
396 if (CurIndex > 1) {
397 RegexFC child = PopFC();
398 RegexFC cumul = TopFC();
399
400 _failed = !cumul.AddFC(child, false);
401 }
402 break;
403
404 case RegexNode.Alternate | AfterChild:
405 case RegexNode.Testref | AfterChild:
406 if (CurIndex != 0) {
407 RegexFC child = PopFC();
408 RegexFC cumul = TopFC();
409
410 _failed = !cumul.AddFC(child, false);
411 }
412 break;
413
414 case RegexNode.Loop | AfterChild:
415 case RegexNode.Lazyloop | AfterChild:
416 if (node._m == 0)
417 TopFC()._nullable = true;
418 break;
419
420 case RegexNode.Group | BeforeChild:
421 case RegexNode.Group | AfterChild:
422 case RegexNode.Capture | BeforeChild:
423 case RegexNode.Capture | AfterChild:
424 case RegexNode.Greedy | BeforeChild:
425 case RegexNode.Greedy | AfterChild:
426 break;
427
428 case RegexNode.Require | BeforeChild:
429 case RegexNode.Prevent | BeforeChild:
430 SkipChild();
431 PushFC(new RegexFC(true));
432 break;
433
434 case RegexNode.Require | AfterChild:
435 case RegexNode.Prevent | AfterChild:
436 break;
437
438 case RegexNode.One:
439 case RegexNode.Notone:
440 PushFC(new RegexFC(node._ch, NodeType == RegexNode.Notone, false, ci));
441 break;
442
443 case RegexNode.Oneloop:
444 case RegexNode.Onelazy:
445 PushFC(new RegexFC(node._ch, false, node._m == 0, ci));
446 break;
447
448 case RegexNode.Notoneloop:
449 case RegexNode.Notonelazy:
450 PushFC(new RegexFC(node._ch, true, node._m == 0, ci));
451 break;
452
453 case RegexNode.Multi:
454 if (node._str.Length == 0)
455 PushFC(new RegexFC(true));
456 else if (!rtl)
457 PushFC(new RegexFC(node._str[0], false, false, ci));
458 else
459 PushFC(new RegexFC(node._str[node._str.Length - 1], false, false, ci));
460 break;
461
462 case RegexNode.Set:
463 PushFC(new RegexFC(node._str, false, ci));
464 break;
465
466 case RegexNode.Setloop:
467 case RegexNode.Setlazy:
468 PushFC(new RegexFC(node._str, node._m == 0, ci));
469 break;
470
471 case RegexNode.Ref:
472 PushFC(new RegexFC(RegexCharClass.AnyClass, true, false));
473 break;
474
475 case RegexNode.Nothing:
476 case RegexNode.Bol:
477 case RegexNode.Eol:
478 case RegexNode.Boundary:
479 case RegexNode.Nonboundary:
480 case RegexNode.ECMABoundary:
481 case RegexNode.NonECMABoundary:
482 case RegexNode.Beginning:
483 case RegexNode.Start:
484 case RegexNode.EndZ:
485 case RegexNode.End:
486 PushFC(new RegexFC(true));
487 break;
488
489 default:
490 throw new ArgumentException(SR.GetString(SR.UnexpectedOpcode, NodeType.ToString(CultureInfo.CurrentCulture)));
491 }
492 }
493 }
494
495 internal sealed class RegexFC {
496 internal RegexCharClass _cc;
497 internal bool _nullable;
498 internal bool _caseInsensitive;
499
500 internal RegexFC(bool nullable) {
501 _cc = new RegexCharClass();
502 _nullable = nullable;
503 }
504
505 internal RegexFC(char ch, bool not, bool nullable, bool caseInsensitive) {
506 _cc = new RegexCharClass();
507
508 if (not) {
509 if (ch > 0)
510 _cc.AddRange('\0', (char)(ch - 1));
511 if (ch < 0xFFFF)
512 _cc.AddRange((char)(ch + 1), '\uFFFF');
513 }
514 else {
515 _cc.AddRange(ch, ch);
516 }
517
518 _caseInsensitive = caseInsensitive;
519 _nullable = nullable;
520 }
521
522 internal RegexFC(String charClass, bool nullable, bool caseInsensitive) {
523 _cc = RegexCharClass.Parse(charClass);
524
525 _nullable = nullable;
526 _caseInsensitive = caseInsensitive;
527 }
528
529 internal bool AddFC(RegexFC fc, bool concatenate) {
530 if (!_cc.CanMerge || !fc._cc.CanMerge) {
531 return false;
532 }
533
534 if (concatenate) {
535 if (!_nullable)
536 return true;
537
538 if (!fc._nullable)
539 _nullable = false;
540 }
541 else {
542 if (fc._nullable)
543 _nullable = true;
544 }
545
546 _caseInsensitive |= fc._caseInsensitive;
547 _cc.AddCharClass(fc._cc);
548 return true;
549 }
550
551 internal String GetFirstChars(CultureInfo culture) {
552 if (_caseInsensitive)
553 _cc.AddLowercase(culture);
554
555 return _cc.ToStringClass();
556 }
557
558 internal bool IsCaseInsensitive() {
559 return _caseInsensitive;
560 }
561 }
562
563 internal sealed class RegexPrefix {
564 internal String _prefix;
565 internal bool _caseInsensitive;
566
567 internal static RegexPrefix _empty = new RegexPrefix(String.Empty, false);
568
569 internal RegexPrefix(String prefix, bool ci) {
570 _prefix = prefix;
571 _caseInsensitive = ci;
572 }
573
574 internal String Prefix {
575 get {
576 return _prefix;
577 }
578 }
579
580 internal bool CaseInsensitive {
581 get {
582 return _caseInsensitive;
583 }
584 }
585 internal static RegexPrefix Empty {
586 get {
587 return _empty;
588 }
589 }
590 }
591 }