Debian Codegraph @

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

raw
1 //------------------------------------------------------------------------------
2 // <copyright file="RegexInterpreter.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6
7 // This RegexInterpreter class is internal to the RegularExpression package.
8 // It executes a block of regular expression codes while consuming
9 // input.
10
11
12 namespace System.Text.RegularExpressions
13 {
14
15 using System.Collections;
16 using System.Diagnostics;
17 using System.Globalization;
18
19 internal sealed class RegexInterpreter : RegexRunner {
20 internal int runoperator;
21 internal int [] runcodes;
22 internal int runcodepos;
23 internal String [] runstrings;
24 internal RegexCode runcode;
25 internal RegexPrefix runfcPrefix;
26 internal RegexBoyerMoore runbmPrefix;
27 internal int runanchors;
28 internal bool runrtl;
29 internal bool runci;
30 internal CultureInfo runculture;
31
32 internal RegexInterpreter(RegexCode code, CultureInfo culture) {
33 runcode = code;
34 runcodes = code._codes;
35 runstrings = code._strings;
36 runfcPrefix = code._fcPrefix;
37 runbmPrefix = code._bmPrefix;
38 runanchors = code._anchors;
39 runculture = culture;
40 }
41
42 protected override void InitTrackCount() {
43 runtrackcount = runcode._trackcount;
44 }
45
46 private void Advance() {
47 Advance(0);
48 }
49
50 private void Advance(int i) {
51 runcodepos += (i + 1);
52 SetOperator(runcodes[runcodepos]);
53 }
54
55 private void Goto(int newpos) {
56 // when branching backward, ensure storage
57 if (newpos < runcodepos)
58 EnsureStorage();
59
60 SetOperator(runcodes[newpos]);
61 runcodepos = newpos;
62 }
63
64 private void Textto(int newpos) {
65 runtextpos = newpos;
66 }
67
68 private void Trackto(int newpos) {
69 runtrackpos = runtrack.Length - newpos;
70 }
71
72 private int Textstart() {
73 return runtextstart;
74 }
75
76 private int Textpos() {
77 return runtextpos;
78 }
79
80 // push onto the backtracking stack
81 private int Trackpos() {
82 return runtrack.Length - runtrackpos;
83 }
84
85 private void TrackPush() {
86 runtrack[--runtrackpos] = runcodepos;
87 }
88
89 private void TrackPush(int I1) {
90 runtrack[--runtrackpos] = I1;
91 runtrack[--runtrackpos] = runcodepos;
92 }
93
94 private void TrackPush(int I1, int I2) {
95 runtrack[--runtrackpos] = I1;
96 runtrack[--runtrackpos] = I2;
97 runtrack[--runtrackpos] = runcodepos;
98 }
99
100 private void TrackPush(int I1, int I2, int I3) {
101 runtrack[--runtrackpos] = I1;
102 runtrack[--runtrackpos] = I2;
103 runtrack[--runtrackpos] = I3;
104 runtrack[--runtrackpos] = runcodepos;
105 }
106
107 private void TrackPush2(int I1) {
108 runtrack[--runtrackpos] = I1;
109 runtrack[--runtrackpos] = -runcodepos;
110 }
111
112 private void TrackPush2(int I1, int I2) {
113 runtrack[--runtrackpos] = I1;
114 runtrack[--runtrackpos] = I2;
115 runtrack[--runtrackpos] = -runcodepos;
116 }
117
118 private void Backtrack() {
119 int newpos = runtrack[runtrackpos++];
120 #if DBG
121 if (runmatch.Debug) {
122 if (newpos < 0)
123 Debug.WriteLine(" Backtracking (back2) to code position " + (-newpos));
124 else
125 Debug.WriteLine(" Backtracking to code position " + newpos);
126 }
127 #endif
128
129 if (newpos < 0) {
130 newpos = -newpos;
131 SetOperator(runcodes[newpos] | RegexCode.Back2);
132 }
133 else {
134 SetOperator(runcodes[newpos] | RegexCode.Back);
135 }
136
137 // When branching backward, ensure storage
138 if (newpos < runcodepos)
139 EnsureStorage();
140
141 runcodepos = newpos;
142 }
143
144 private void SetOperator(int op) {
145 runci = (0 != (op & RegexCode.Ci));
146 runrtl = (0 != (op & RegexCode.Rtl));
147 runoperator = op & ~(RegexCode.Rtl | RegexCode.Ci);
148 }
149
150 private void TrackPop() {
151 runtrackpos++;
152 }
153
154 // pop framesize items from the backtracking stack
155 private void TrackPop(int framesize) {
156 runtrackpos += framesize;
157 }
158
159 // Technically we are actually peeking at items already popped. So if you want to
160 // get and pop the top item from the stack, you do
161 // TrackPop();
162 // TrackPeek();
163 private int TrackPeek() {
164 return runtrack[runtrackpos - 1];
165 }
166
167 // get the ith element down on the backtracking stack
168 private int TrackPeek(int i) {
169 return runtrack[runtrackpos - i - 1];
170 }
171
172 // Push onto the grouping stack
173 private void StackPush(int I1) {
174 runstack[--runstackpos] = I1;
175 }
176
177 private void StackPush(int I1, int I2) {
178 runstack[--runstackpos] = I1;
179 runstack[--runstackpos] = I2;
180 }
181
182 private void StackPop() {
183 runstackpos++;
184 }
185
186 // pop framesize items from the grouping stack
187 private void StackPop(int framesize) {
188 runstackpos += framesize;
189 }
190
191 // Technically we are actually peeking at items already popped. So if you want to
192 // get and pop the top item from the stack, you do
193 // StackPop();
194 // StackPeek();
195 private int StackPeek() {
196 return runstack[runstackpos - 1];
197 }
198
199 // get the ith element down on the grouping stack
200 private int StackPeek(int i) {
201 return runstack[runstackpos - i - 1];
202 }
203
204 private int Operator() {
205 return runoperator;
206 }
207
208 private int Operand(int i) {
209 return runcodes[runcodepos + i + 1];
210 }
211
212 private int Leftchars() {
213 return runtextpos - runtextbeg;
214 }
215
216 private int Rightchars() {
217 return runtextend - runtextpos;
218 }
219
220 private int Bump() {
221 return runrtl ? -1 : 1;
222 }
223
224 private int Forwardchars() {
225 return runrtl ? runtextpos - runtextbeg : runtextend - runtextpos;
226 }
227
228 private char Forwardcharnext() {
229 char ch = (runrtl ? runtext[--runtextpos] : runtext[runtextpos++]);
230
231 return(runci ? Char.ToLower(ch, runculture) : ch);
232 }
233
234 private bool Stringmatch(String str) {
235 int c;
236 int pos;
237
238 if (!runrtl) {
239 if (runtextend - runtextpos < (c = str.Length))
240 return false;
241
242 pos = runtextpos + c;
243 }
244 else {
245 if (runtextpos - runtextbeg < (c = str.Length))
246 return false;
247
248 pos = runtextpos;
249 }
250
251 if (!runci) {
252 while (c != 0)
253 if (str[--c] != runtext[--pos])
254 return false;
255 }
256 else {
257 while (c != 0)
258 if (str[--c] != Char.ToLower(runtext[--pos], runculture))
259 return false;
260 }
261
262 if (!runrtl) {
263 pos += str.Length;
264 }
265
266 runtextpos = pos;
267
268 return true;
269 }
270
271 private bool Refmatch(int index, int len) {
272 int c;
273 int pos;
274 int cmpos;
275
276 if (!runrtl) {
277 if (runtextend - runtextpos < len)
278 return false;
279
280 pos = runtextpos + len;
281 }
282 else {
283 if (runtextpos - runtextbeg < len)
284 return false;
285
286 pos = runtextpos;
287 }
288 cmpos = index + len;
289
290 c = len;
291
292 if (!runci) {
293 while (c-- != 0)
294 if (runtext[--cmpos] != runtext[--pos])
295 return false;
296 }
297 else {
298 while (c-- != 0)
299 if (Char.ToLower(runtext[--cmpos], runculture) != Char.ToLower(runtext[--pos], runculture))
300 return false;
301 }
302
303 if (!runrtl) {
304 pos += len;
305 }
306
307 runtextpos = pos;
308
309 return true;
310 }
311
312 private void Backwardnext() {
313 runtextpos += runrtl ? 1 : -1;
314 }
315
316 private char CharAt(int j) {
317 return runtext[j];
318 }
319
320 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
321 // !!!! This function must be kept synchronized with GenerateFindFirstChar !!!!
322 // !!!! in RegexCompiler.cs !!!!
323 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
324 protected override bool FindFirstChar() {
325 int i;
326 String set;
327
328 if (0 != (runanchors & (RegexFCD.Beginning | RegexFCD.Start | RegexFCD.EndZ | RegexFCD.End))) {
329 if (!runcode._rightToLeft) {
330 if ((0 != (runanchors & RegexFCD.Beginning) && runtextpos > runtextbeg) ||
331 (0 != (runanchors & RegexFCD.Start) && runtextpos > runtextstart)) {
332 runtextpos = runtextend;
333 return false;
334 }
335 if (0 != (runanchors & RegexFCD.EndZ) && runtextpos < runtextend - 1) {
336 runtextpos = runtextend - 1;
337 }
338 else if (0 != (runanchors & RegexFCD.End) && runtextpos < runtextend) {
339 runtextpos = runtextend;
340 }
341 }
342 else {
343 if ((0 != (runanchors & RegexFCD.End) && runtextpos < runtextend) ||
344 (0 != (runanchors & RegexFCD.EndZ) && (runtextpos < runtextend - 1 ||
345 (runtextpos == runtextend - 1 && CharAt(runtextpos) != '\n'))) ||
346 (0 != (runanchors & RegexFCD.Start) && runtextpos < runtextstart)) {
347 runtextpos = runtextbeg;
348 return false;
349 }
350 if (0 != (runanchors & RegexFCD.Beginning) && runtextpos > runtextbeg) {
351 runtextpos = runtextbeg;
352 }
353 }
354
355 if (runbmPrefix != null) {
356 return runbmPrefix.IsMatch(runtext, runtextpos, runtextbeg, runtextend);
357 }
358
359 return true; // found a valid start or end anchor
360 }
361 else if (runbmPrefix != null) {
362 runtextpos = runbmPrefix.Scan(runtext, runtextpos, runtextbeg, runtextend);
363
364 if (runtextpos == -1) {
365 runtextpos = (runcode._rightToLeft ? runtextbeg : runtextend);
366 return false;
367 }
368
369 return true;
370 }
371 else if (runfcPrefix == null) {
372 return true;
373 }
374
375 runrtl = runcode._rightToLeft;
376 runci = runfcPrefix.CaseInsensitive;
377 set = runfcPrefix.Prefix;
378
379 if (RegexCharClass.IsSingleton(set)) {
380 char ch = RegexCharClass.SingletonChar(set);
381
382 for (i = Forwardchars(); i > 0; i--) {
383 if (ch == Forwardcharnext()) {
384 Backwardnext();
385 return true;
386 }
387 }
388 }
389 else {
390 for (i = Forwardchars(); i > 0; i--) {
391 if (RegexCharClass.CharInClass(Forwardcharnext(), set)) {
392 Backwardnext();
393 return true;
394 }
395 }
396 }
397 return false;
398 }
399
400 protected override void Go() {
401 Goto(0);
402
403 for (;;) {
404 #if DBG
405 if (runmatch.Debug) {
406 DumpState();
407 }
408 #endif
409
410 CheckTimeout();
411
412 switch (Operator()) {
413 case RegexCode.Stop:
414 return;
415
416 case RegexCode.Nothing:
417 break;
418
419 case RegexCode.Goto:
420 Goto(Operand(0));
421 continue;
422
423 case RegexCode.Testref:
424 if (!IsMatched(Operand(0)))
425 break;
426 Advance(1);
427 continue;
428
429 case RegexCode.Lazybranch:
430 TrackPush(Textpos());
431 Advance(1);
432 continue;
433
434 case RegexCode.Lazybranch | RegexCode.Back:
435 TrackPop();
436 Textto(TrackPeek());
437 Goto(Operand(0));
438 continue;
439
440 case RegexCode.Setmark:
441 StackPush(Textpos());
442 TrackPush();
443 Advance();
444 continue;
445
446 case RegexCode.Nullmark:
447 StackPush(-1);
448 TrackPush();
449 Advance();
450 continue;
451
452 case RegexCode.Setmark | RegexCode.Back:
453 case RegexCode.Nullmark | RegexCode.Back:
454 StackPop();
455 break;
456
457 case RegexCode.Getmark:
458 StackPop();
459 TrackPush(StackPeek());
460 Textto(StackPeek());
461 Advance();
462 continue;
463
464 case RegexCode.Getmark | RegexCode.Back:
465 TrackPop();
466 StackPush(TrackPeek());
467 break;
468
469 case RegexCode.Capturemark:
470 if (Operand(1) != -1 && !IsMatched(Operand(1)))
471 break;
472 StackPop();
473 if (Operand(1) != -1)
474 TransferCapture(Operand(0), Operand(1), StackPeek(), Textpos());
475 else
476 Capture(Operand(0), StackPeek(), Textpos());
477 TrackPush(StackPeek());
478
479 Advance(2);
480
481 continue;
482
483 case RegexCode.Capturemark | RegexCode.Back:
484 TrackPop();
485 StackPush(TrackPeek());
486 Uncapture();
487 if (Operand(0) != -1 && Operand(1) != -1)
488 Uncapture();
489
490 break;
491
492 case RegexCode.Branchmark:
493 {
494 int matched;
495 StackPop();
496
497 matched = Textpos() - StackPeek();
498
499 if (matched != 0) { // Nonempty match -> loop now
500 TrackPush(StackPeek(), Textpos()); // Save old mark, textpos
501 StackPush(Textpos()); // Make new mark
502 Goto(Operand(0)); // Loop
503 }
504 else { // Empty match -> straight now
505 TrackPush2(StackPeek()); // Save old mark
506 Advance(1); // Straight
507 }
508 continue;
509 }
510
511 case RegexCode.Branchmark | RegexCode.Back:
512 TrackPop(2);
513 StackPop();
514 Textto(TrackPeek(1)); // Recall position
515 TrackPush2(TrackPeek()); // Save old mark
516 Advance(1); // Straight
517 continue;
518
519 case RegexCode.Branchmark | RegexCode.Back2:
520 TrackPop();
521 StackPush(TrackPeek()); // Recall old mark
522 break; // Backtrack
523
524 case RegexCode.Lazybranchmark:
525 {
526 // We hit this the first time through a lazy loop and after each
527 // successful match of the inner expression. It simply continues
528 // on and doesn't loop.
529 StackPop();
530
531 int oldMarkPos = StackPeek();
532
533 if (Textpos() != oldMarkPos) { // Nonempty match -> try to loop again by going to 'back' state
534 if (oldMarkPos != -1)
535 TrackPush(oldMarkPos, Textpos()); // Save old mark, textpos
536 else
537 TrackPush(Textpos(), Textpos());
538 }
539 else {
540 // The inner expression found an empty match, so we'll go directly to 'back2' if we
541 // backtrack. In this case, we need to push something on the stack, since back2 pops.
542 // However, in the case of ()+? or similar, this empty match may be legitimate, so push the text
543 // position associated with that empty match.
544 StackPush(oldMarkPos);
545
546 TrackPush2(StackPeek()); // Save old mark
547 }
548 Advance(1);
549 continue;
550 }
551
552 case RegexCode.Lazybranchmark | RegexCode.Back:
553 {
554 // After the first time, Lazybranchmark | RegexCode.Back occurs
555 // with each iteration of the loop, and therefore with every attempted
556 // match of the inner expression. We'll try to match the inner expression,
557 // then go back to Lazybranchmark if successful. If the inner expression
558 // failes, we go to Lazybranchmark | RegexCode.Back2
559 int pos;
560
561 TrackPop(2);
562 pos = TrackPeek(1);
563 TrackPush2(TrackPeek()); // Save old mark
564 StackPush(pos); // Make new mark
565 Textto(pos); // Recall position
566 Goto(Operand(0)); // Loop
567 continue;
568 }
569
570 case RegexCode.Lazybranchmark | RegexCode.Back2:
571 // The lazy loop has failed. We'll do a true backtrack and
572 // start over before the lazy loop.
573 StackPop();
574 TrackPop();
575 StackPush(TrackPeek()); // Recall old mark
576 break;
577
578 case RegexCode.Setcount:
579 StackPush(Textpos(), Operand(0));
580 TrackPush();
581 Advance(1);
582 continue;
583
584 case RegexCode.Nullcount:
585 StackPush(-1, Operand(0));
586 TrackPush();
587 Advance(1);
588 continue;
589
590 case RegexCode.Setcount | RegexCode.Back:
591 StackPop(2);
592 break;
593
594 case RegexCode.Nullcount | RegexCode.Back:
595 StackPop(2);
596 break;
597
598 case RegexCode.Branchcount:
599 // StackPush:
600 // 0: Mark
601 // 1: Count
602 {
603 StackPop(2);
604 int mark = StackPeek();
605 int count = StackPeek(1);
606 int matched = Textpos() - mark;
607
608 if (count >= Operand(1) || (matched == 0 && count >= 0)) { // Max loops or empty match -> straight now
609 TrackPush2(mark, count); // Save old mark, count
610 Advance(2); // Straight
611 }
612 else { // Nonempty match -> count+loop now
613 TrackPush(mark); // remember mark
614 StackPush(Textpos(), count + 1); // Make new mark, incr count
615 Goto(Operand(0)); // Loop
616 }
617 continue;
618 }
619
620 case RegexCode.Branchcount | RegexCode.Back:
621 // TrackPush:
622 // 0: Previous mark
623 // StackPush:
624 // 0: Mark (= current pos, discarded)
625 // 1: Count
626 TrackPop();
627 StackPop(2);
628 if (StackPeek(1) > 0) { // Positive -> can go straight
629 Textto(StackPeek()); // Zap to mark
630 TrackPush2(TrackPeek(), StackPeek(1) - 1); // Save old mark, old count
631 Advance(2); // Straight
632 continue;
633 }
634 StackPush(TrackPeek(), StackPeek(1) - 1); // recall old mark, old count
635 break;
636
637 case RegexCode.Branchcount | RegexCode.Back2:
638 // TrackPush:
639 // 0: Previous mark
640 // 1: Previous count
641 TrackPop(2);
642 StackPush(TrackPeek(), TrackPeek(1)); // Recall old mark, old count
643 break; // Backtrack
644
645
646 case RegexCode.Lazybranchcount:
647 // StackPush:
648 // 0: Mark
649 // 1: Count
650 {
651 StackPop(2);
652 int mark = StackPeek();
653 int count = StackPeek(1);
654
655 if (count < 0) { // Negative count -> loop now
656 TrackPush2(mark); // Save old mark
657 StackPush(Textpos(), count + 1); // Make new mark, incr count
658 Goto(Operand(0)); // Loop
659 }
660 else { // Nonneg count -> straight now
661 TrackPush(mark, count, Textpos()); // Save mark, count, position
662 Advance(2); // Straight
663 }
664 continue;
665 }
666
667 case RegexCode.Lazybranchcount | RegexCode.Back:
668 // TrackPush:
669 // 0: Mark
670 // 1: Count
671 // 2: Textpos
672 {
673 TrackPop(3);
674 int mark = TrackPeek();
675 int textpos = TrackPeek(2);
676
677 if (TrackPeek(1) < Operand(1) && textpos != mark) { // Under limit and not empty match -> loop
678 Textto(textpos); // Recall position
679 StackPush(textpos, TrackPeek(1) + 1); // Make new mark, incr count
680 TrackPush2(mark); // Save old mark
681 Goto(Operand(0)); // Loop
682 continue;
683 }
684 else { // Max loops or empty match -> backtrack
685 StackPush(TrackPeek(), TrackPeek(1)); // Recall old mark, count
686 break; // backtrack
687 }
688 }
689
690 case RegexCode.Lazybranchcount | RegexCode.Back2:
691 // TrackPush:
692 // 0: Previous mark
693 // StackPush:
694 // 0: Mark (== current pos, discarded)
695 // 1: Count
696 TrackPop();
697 StackPop(2);
698 StackPush(TrackPeek(), StackPeek(1) - 1); // Recall old mark, count
699 break; // Backtrack
700
701 case RegexCode.Setjump:
702 StackPush(Trackpos(), Crawlpos());
703 TrackPush();
704 Advance();
705 continue;
706
707 case RegexCode.Setjump | RegexCode.Back:
708 StackPop(2);
709 break;
710
711 case RegexCode.Backjump:
712 // StackPush:
713 // 0: Saved trackpos
714 // 1: Crawlpos
715 StackPop(2);
716 Trackto(StackPeek());
717
718 while (Crawlpos() != StackPeek(1))
719 Uncapture();
720
721 break;
722
723 case RegexCode.Forejump:
724 // StackPush:
725 // 0: Saved trackpos
726 // 1: Crawlpos
727 StackPop(2);
728 Trackto(StackPeek());
729 TrackPush(StackPeek(1));
730 Advance();
731 continue;
732
733 case RegexCode.Forejump | RegexCode.Back:
734 // TrackPush:
735 // 0: Crawlpos
736 TrackPop();
737
738 while (Crawlpos() != TrackPeek())
739 Uncapture();
740
741 break;
742
743 case RegexCode.Bol:
744 if (Leftchars() > 0 && CharAt(Textpos() - 1) != '\n')
745 break;
746 Advance();
747 continue;
748
749 case RegexCode.Eol:
750 if (Rightchars() > 0 && CharAt(Textpos()) != '\n')
751 break;
752 Advance();
753 continue;
754
755 case RegexCode.Boundary:
756 if (!IsBoundary(Textpos(), runtextbeg, runtextend))
757 break;
758 Advance();
759 continue;
760
761 case RegexCode.Nonboundary:
762 if (IsBoundary(Textpos(), runtextbeg, runtextend))
763 break;
764 Advance();
765 continue;
766
767 case RegexCode.ECMABoundary:
768 if (!IsECMABoundary(Textpos(), runtextbeg, runtextend))
769 break;
770 Advance();
771 continue;
772
773 case RegexCode.NonECMABoundary:
774 if (IsECMABoundary(Textpos(), runtextbeg, runtextend))
775 break;
776 Advance();
777 continue;
778
779 case RegexCode.Beginning:
780 if (Leftchars() > 0)
781 break;
782 Advance();
783 continue;
784
785 case RegexCode.Start:
786 if (Textpos() != Textstart())
787 break;
788 Advance();
789 continue;
790
791 case RegexCode.EndZ:
792 if (Rightchars() > 1 || Rightchars() == 1 && CharAt(Textpos()) != '\n')
793 break;
794 Advance();
795 continue;
796
797 case RegexCode.End:
798 if (Rightchars() > 0)
799 break;
800 Advance();
801 continue;
802
803 case RegexCode.One:
804 if (Forwardchars() < 1 || Forwardcharnext() != (char)Operand(0))
805 break;
806
807 Advance(1);
808 continue;
809
810 case RegexCode.Notone:
811 if (Forwardchars() < 1 || Forwardcharnext() == (char)Operand(0))
812 break;
813
814 Advance(1);
815 continue;
816
817 case RegexCode.Set:
818 if (Forwardchars() < 1 || !RegexCharClass.CharInClass(Forwardcharnext(), runstrings[Operand(0)]))
819 break;
820
821 Advance(1);
822 continue;
823
824 case RegexCode.Multi:
825 {
826 if (!Stringmatch(runstrings[Operand(0)]))
827 break;
828
829 Advance(1);
830 continue;
831 }
832
833 case RegexCode.Ref:
834 {
835 int capnum = Operand(0);
836
837 if (IsMatched(capnum)) {
838 if (!Refmatch(MatchIndex(capnum), MatchLength(capnum)))
839 break;
840 } else {
841 if ((runregex.roptions & RegexOptions.ECMAScript) == 0)
842 break;
843 }
844
845 Advance(1);
846 continue;
847 }
848
849 case RegexCode.Onerep:
850 {
851 int c = Operand(1);
852
853 if (Forwardchars() < c)
854 break;
855
856 char ch = (char)Operand(0);
857
858 while (c-- > 0)
859 if (Forwardcharnext() != ch)
860 goto BreakBackward;
861
862 Advance(2);
863 continue;
864 }
865
866 case RegexCode.Notonerep:
867 {
868 int c = Operand(1);
869
870 if (Forwardchars() < c)
871 break;
872
873 char ch = (char)Operand(0);
874
875 while (c-- > 0)
876 if (Forwardcharnext() == ch)
877 goto BreakBackward;
878
879 Advance(2);
880 continue;
881 }
882
883 case RegexCode.Setrep:
884 {
885 int c = Operand(1);
886
887 if (Forwardchars() < c)
888 break;
889
890 String set = runstrings[Operand(0)];
891
892 while (c-- > 0)
893 if (!RegexCharClass.CharInClass(Forwardcharnext(), set))
894 goto BreakBackward;
895
896 Advance(2);
897 continue;
898 }
899
900 case RegexCode.Oneloop:
901 {
902 int c = Operand(1);
903
904 if (c > Forwardchars())
905 c = Forwardchars();
906
907 char ch = (char)Operand(0);
908 int i;
909
910 for (i = c; i > 0; i--) {
911 if (Forwardcharnext() != ch) {
912 Backwardnext();
913 break;
914 }
915 }
916
917 if (c > i)
918 TrackPush(c - i - 1, Textpos() - Bump());
919
920 Advance(2);
921 continue;
922 }
923
924 case RegexCode.Notoneloop:
925 {
926 int c = Operand(1);
927
928 if (c > Forwardchars())
929 c = Forwardchars();
930
931 char ch = (char)Operand(0);
932 int i;
933
934 for (i = c; i > 0; i--) {
935 if (Forwardcharnext() == ch) {
936 Backwardnext();
937 break;
938 }
939 }
940
941 if (c > i)
942 TrackPush(c - i - 1, Textpos() - Bump());
943
944 Advance(2);
945 continue;
946 }
947
948 case RegexCode.Setloop:
949 {
950 int c = Operand(1);
951
952 if (c > Forwardchars())
953 c = Forwardchars();
954
955 String set = runstrings[Operand(0)];
956 int i;
957
958 for (i = c; i > 0; i--) {
959 if (!RegexCharClass.CharInClass(Forwardcharnext(), set)) {
960 Backwardnext();
961 break;
962 }
963 }
964
965 if (c > i)
966 TrackPush(c - i - 1, Textpos() - Bump());
967
968 Advance(2);
969 continue;
970 }
971
972 case RegexCode.Oneloop | RegexCode.Back:
973 case RegexCode.Notoneloop | RegexCode.Back:
974 {
975 TrackPop(2);
976 int i = TrackPeek();
977 int pos = TrackPeek(1);
978
979 Textto(pos);
980
981 if (i > 0)
982 TrackPush(i - 1, pos - Bump());
983
984 Advance(2);
985 continue;
986 }
987
988 case RegexCode.Setloop | RegexCode.Back:
989 {
990 TrackPop(2);
991 int i = TrackPeek();
992 int pos = TrackPeek(1);
993
994 Textto(pos);
995
996 if (i > 0)
997 TrackPush(i - 1, pos - Bump());
998
999 Advance(2);
1000 continue;
1001 }
1002
1003 case RegexCode.Onelazy:
1004 case RegexCode.Notonelazy:
1005 {
1006 int c = Operand(1);
1007
1008 if (c > Forwardchars())
1009 c = Forwardchars();
1010
1011 if (c > 0)
1012 TrackPush(c - 1, Textpos());
1013
1014 Advance(2);
1015 continue;
1016 }
1017
1018 case RegexCode.Setlazy:
1019 {
1020 int c = Operand(1);
1021
1022 if (c > Forwardchars())
1023 c = Forwardchars();
1024
1025 if (c > 0)
1026 TrackPush(c - 1, Textpos());
1027
1028 Advance(2);
1029 continue;
1030 }
1031
1032 case RegexCode.Onelazy | RegexCode.Back:
1033 {
1034 TrackPop(2);
1035 int pos = TrackPeek(1);
1036 Textto(pos);
1037
1038 if (Forwardcharnext() != (char)Operand(0))
1039 break;
1040
1041 int i = TrackPeek();
1042
1043 if (i > 0)
1044 TrackPush(i - 1, pos + Bump());
1045
1046 Advance(2);
1047 continue;
1048 }
1049
1050 case RegexCode.Notonelazy | RegexCode.Back:
1051 {
1052 TrackPop(2);
1053 int pos = TrackPeek(1);
1054 Textto(pos);
1055
1056 if (Forwardcharnext() == (char)Operand(0))
1057 break;
1058
1059 int i = TrackPeek();
1060
1061 if (i > 0)
1062 TrackPush(i - 1, pos + Bump());
1063
1064 Advance(2);
1065 continue;
1066 }
1067
1068 case RegexCode.Setlazy | RegexCode.Back:
1069 {
1070 TrackPop(2);
1071 int pos = TrackPeek(1);
1072 Textto(pos);
1073
1074 if (!RegexCharClass.CharInClass(Forwardcharnext(), runstrings[Operand(0)]))
1075 break;
1076
1077 int i = TrackPeek();
1078
1079 if (i > 0)
1080 TrackPush(i - 1, pos + Bump());
1081
1082 Advance(2);
1083 continue;
1084 }
1085
1086 default:
1087 throw new NotImplementedException(SR.GetString(SR.UnimplementedState));
1088 }
1089
1090 BreakBackward:
1091 ;
1092
1093 // "break Backward" comes here:
1094 Backtrack();
1095 }
1096
1097 }
1098
1099 #if DBG
1100 internal override void DumpState() {
1101 base.DumpState();
1102 Debug.WriteLine(" " + runcode.OpcodeDescription(runcodepos) +
1103 ((runoperator & RegexCode.Back) != 0 ? " Back" : "") +
1104 ((runoperator & RegexCode.Back2) != 0 ? " Back2" : ""));
1105 Debug.WriteLine("");
1106 }
1107 #endif
1108
1109 }
1110
1111
1112
1113 }