forked from dlclark/regexp2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.go
1613 lines (1298 loc) · 34 KB
/
runner.go
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package regexp2
import (
"bytes"
"errors"
"fmt"
"math"
"strconv"
"strings"
"time"
"unicode"
"github.com/dlclark/regexp2/syntax"
)
type runner struct {
re *Regexp
code *syntax.Code
runtextstart int // starting point for search
runtext []rune // text to search
runtextpos int // current position in text
runtextend int
// The backtracking stack. Opcodes use this to store data regarding
// what they have matched and where to backtrack to. Each "frame" on
// the stack takes the form of [CodePosition Data1 Data2...], where
// CodePosition is the position of the current opcode and
// the data values are all optional. The CodePosition can be negative, and
// these values (also called "back2") are used by the BranchMark family of opcodes
// to indicate whether they are backtracking after a successful or failed
// match.
// When we backtrack, we pop the CodePosition off the stack, set the current
// instruction pointer to that code position, and mark the opcode
// with a backtracking flag ("Back"). Each opcode then knows how to
// handle its own data.
runtrack []int
runtrackpos int
// This stack is used to track text positions across different opcodes.
// For example, in /(a*b)+/, the parentheses result in a SetMark/CaptureMark
// pair. SetMark records the text position before we match a*b. Then
// CaptureMark uses that position to figure out where the capture starts.
// Opcodes which push onto this stack are always paired with other opcodes
// which will pop the value from it later. A successful match should mean
// that this stack is empty.
runstack []int
runstackpos int
// The crawl stack is used to keep track of captures. Every time a group
// has a capture, we push its group number onto the runcrawl stack. In
// the case of a balanced match, we push BOTH groups onto the stack.
runcrawl []int
runcrawlpos int
runtrackcount int // count of states that may do backtracking
runmatch *Match // result object
ignoreTimeout bool
timeout time.Duration // timeout in milliseconds (needed for actual)
deadline fasttime
operator syntax.InstOp
codepos int
rightToLeft bool
caseInsensitive bool
}
// run searches for matches and can continue from the previous match
//
// quick is usually false, but can be true to not return matches, just put it in caches
// textstart is -1 to start at the "beginning" (depending on Right-To-Left), otherwise an index in input
// input is the string to search for our regex pattern
func (re *Regexp) run(quick bool, textstart int, input []rune) (*Match, error) {
// get a cached runner
runner := re.getRunner()
defer re.putRunner(runner)
if textstart < 0 {
if re.RightToLeft() {
textstart = len(input)
} else {
textstart = 0
}
}
return runner.scan(input, textstart, quick, re.MatchTimeout)
}
// Scans the string to find the first match. Uses the Match object
// both to feed text in and as a place to store matches that come out.
//
// All the action is in the Go() method. Our
// responsibility is to load up the class members before
// calling Go.
//
// The optimizer can compute a set of candidate starting characters,
// and we could use a separate method Skip() that will quickly scan past
// any characters that we know can't match.
func (r *runner) scan(rt []rune, textstart int, quick bool, timeout time.Duration) (*Match, error) {
r.timeout = timeout
r.ignoreTimeout = (time.Duration(math.MaxInt64) == timeout)
r.runtextstart = textstart
r.runtext = rt
r.runtextend = len(rt)
stoppos := r.runtextend
bump := 1
if r.re.RightToLeft() {
bump = -1
stoppos = 0
}
r.runtextpos = textstart
initted := false
r.startTimeoutWatch()
for {
if r.re.Debug() {
//fmt.Printf("\nSearch content: %v\n", string(r.runtext))
fmt.Printf("\nSearch range: from 0 to %v\n", r.runtextend)
fmt.Printf("Firstchar search starting at %v stopping at %v\n", r.runtextpos, stoppos)
}
if r.findFirstChar() {
if err := r.checkTimeout(); err != nil {
return nil, err
}
if !initted {
r.initMatch()
initted = true
}
if r.re.Debug() {
fmt.Printf("Executing engine starting at %v\n\n", r.runtextpos)
}
if err := r.execute(); err != nil {
return nil, err
}
if r.runmatch.matchcount[0] > 0 {
// We'll return a match even if it touches a previous empty match
return r.tidyMatch(quick), nil
}
// reset state for another go
r.runtrackpos = len(r.runtrack)
r.runstackpos = len(r.runstack)
r.runcrawlpos = len(r.runcrawl)
}
// failure!
if r.runtextpos == stoppos {
r.tidyMatch(true)
return nil, nil
}
// Recognize leading []* and various anchors, and bump on failure accordingly
// r.bump by one and start again
r.runtextpos += bump
}
// We never get here
}
func (r *runner) execute() error {
r.goTo(0)
for {
if r.re.Debug() {
r.dumpState()
}
if err := r.checkTimeout(); err != nil {
return err
}
switch r.operator {
case syntax.Stop:
return nil
case syntax.Nothing:
break
case syntax.Goto:
r.goTo(r.operand(0))
continue
case syntax.Testref:
if !r.runmatch.isMatched(r.operand(0)) {
break
}
r.advance(1)
continue
case syntax.Lazybranch:
r.trackPush1(r.textPos())
r.advance(1)
continue
case syntax.Lazybranch | syntax.Back:
r.trackPop()
r.textto(r.trackPeek())
r.goTo(r.operand(0))
continue
case syntax.Setmark:
r.stackPush(r.textPos())
r.trackPush()
r.advance(0)
continue
case syntax.Nullmark:
r.stackPush(-1)
r.trackPush()
r.advance(0)
continue
case syntax.Setmark | syntax.Back, syntax.Nullmark | syntax.Back:
r.stackPop()
break
case syntax.Getmark:
r.stackPop()
r.trackPush1(r.stackPeek())
r.textto(r.stackPeek())
r.advance(0)
continue
case syntax.Getmark | syntax.Back:
r.trackPop()
r.stackPush(r.trackPeek())
break
case syntax.Capturemark:
if r.operand(1) != -1 && !r.runmatch.isMatched(r.operand(1)) {
break
}
r.stackPop()
if r.operand(1) != -1 {
r.transferCapture(r.operand(0), r.operand(1), r.stackPeek(), r.textPos())
} else {
r.capture(r.operand(0), r.stackPeek(), r.textPos())
}
r.trackPush1(r.stackPeek())
r.advance(2)
continue
case syntax.Capturemark | syntax.Back:
r.trackPop()
r.stackPush(r.trackPeek())
r.uncapture()
if r.operand(0) != -1 && r.operand(1) != -1 {
r.uncapture()
}
break
case syntax.Branchmark:
r.stackPop()
matched := r.textPos() - r.stackPeek()
if matched != 0 { // Nonempty match -> loop now
r.trackPush2(r.stackPeek(), r.textPos()) // Save old mark, textpos
r.stackPush(r.textPos()) // Make new mark
r.goTo(r.operand(0)) // Loop
} else { // Empty match -> straight now
r.trackPushNeg1(r.stackPeek()) // Save old mark
r.advance(1) // Straight
}
continue
case syntax.Branchmark | syntax.Back:
r.trackPopN(2)
r.stackPop()
r.textto(r.trackPeekN(1)) // Recall position
r.trackPushNeg1(r.trackPeek()) // Save old mark
r.advance(1) // Straight
continue
case syntax.Branchmark | syntax.Back2:
r.trackPop()
r.stackPush(r.trackPeek()) // Recall old mark
break // Backtrack
case syntax.Lazybranchmark:
{
// We hit this the first time through a lazy loop and after each
// successful match of the inner expression. It simply continues
// on and doesn't loop.
r.stackPop()
oldMarkPos := r.stackPeek()
if r.textPos() != oldMarkPos { // Nonempty match -> try to loop again by going to 'back' state
if oldMarkPos != -1 {
r.trackPush2(oldMarkPos, r.textPos()) // Save old mark, textpos
} else {
r.trackPush2(r.textPos(), r.textPos())
}
} else {
// The inner expression found an empty match, so we'll go directly to 'back2' if we
// backtrack. In this case, we need to push something on the stack, since back2 pops.
// However, in the case of ()+? or similar, this empty match may be legitimate, so push the text
// position associated with that empty match.
r.stackPush(oldMarkPos)
r.trackPushNeg1(r.stackPeek()) // Save old mark
}
r.advance(1)
continue
}
case syntax.Lazybranchmark | syntax.Back:
// After the first time, Lazybranchmark | syntax.Back occurs
// with each iteration of the loop, and therefore with every attempted
// match of the inner expression. We'll try to match the inner expression,
// then go back to Lazybranchmark if successful. If the inner expression
// fails, we go to Lazybranchmark | syntax.Back2
r.trackPopN(2)
pos := r.trackPeekN(1)
r.trackPushNeg1(r.trackPeek()) // Save old mark
r.stackPush(pos) // Make new mark
r.textto(pos) // Recall position
r.goTo(r.operand(0)) // Loop
continue
case syntax.Lazybranchmark | syntax.Back2:
// The lazy loop has failed. We'll do a true backtrack and
// start over before the lazy loop.
r.stackPop()
r.trackPop()
r.stackPush(r.trackPeek()) // Recall old mark
break
case syntax.Setcount:
r.stackPush2(r.textPos(), r.operand(0))
r.trackPush()
r.advance(1)
continue
case syntax.Nullcount:
r.stackPush2(-1, r.operand(0))
r.trackPush()
r.advance(1)
continue
case syntax.Setcount | syntax.Back:
r.stackPopN(2)
break
case syntax.Nullcount | syntax.Back:
r.stackPopN(2)
break
case syntax.Branchcount:
// r.stackPush:
// 0: Mark
// 1: Count
r.stackPopN(2)
mark := r.stackPeek()
count := r.stackPeekN(1)
matched := r.textPos() - mark
if count >= r.operand(1) || (matched == 0 && count >= 0) { // Max loops or empty match -> straight now
r.trackPushNeg2(mark, count) // Save old mark, count
r.advance(2) // Straight
} else { // Nonempty match -> count+loop now
r.trackPush1(mark) // remember mark
r.stackPush2(r.textPos(), count+1) // Make new mark, incr count
r.goTo(r.operand(0)) // Loop
}
continue
case syntax.Branchcount | syntax.Back:
// r.trackPush:
// 0: Previous mark
// r.stackPush:
// 0: Mark (= current pos, discarded)
// 1: Count
r.trackPop()
r.stackPopN(2)
if r.stackPeekN(1) > 0 { // Positive -> can go straight
r.textto(r.stackPeek()) // Zap to mark
r.trackPushNeg2(r.trackPeek(), r.stackPeekN(1)-1) // Save old mark, old count
r.advance(2) // Straight
continue
}
r.stackPush2(r.trackPeek(), r.stackPeekN(1)-1) // recall old mark, old count
break
case syntax.Branchcount | syntax.Back2:
// r.trackPush:
// 0: Previous mark
// 1: Previous count
r.trackPopN(2)
r.stackPush2(r.trackPeek(), r.trackPeekN(1)) // Recall old mark, old count
break // Backtrack
case syntax.Lazybranchcount:
// r.stackPush:
// 0: Mark
// 1: Count
r.stackPopN(2)
mark := r.stackPeek()
count := r.stackPeekN(1)
if count < 0 { // Negative count -> loop now
r.trackPushNeg1(mark) // Save old mark
r.stackPush2(r.textPos(), count+1) // Make new mark, incr count
r.goTo(r.operand(0)) // Loop
} else { // Nonneg count -> straight now
r.trackPush3(mark, count, r.textPos()) // Save mark, count, position
r.advance(2) // Straight
}
continue
case syntax.Lazybranchcount | syntax.Back:
// r.trackPush:
// 0: Mark
// 1: Count
// 2: r.textPos
r.trackPopN(3)
mark := r.trackPeek()
textpos := r.trackPeekN(2)
if r.trackPeekN(1) < r.operand(1) && textpos != mark { // Under limit and not empty match -> loop
r.textto(textpos) // Recall position
r.stackPush2(textpos, r.trackPeekN(1)+1) // Make new mark, incr count
r.trackPushNeg1(mark) // Save old mark
r.goTo(r.operand(0)) // Loop
continue
} else { // Max loops or empty match -> backtrack
r.stackPush2(r.trackPeek(), r.trackPeekN(1)) // Recall old mark, count
break // backtrack
}
case syntax.Lazybranchcount | syntax.Back2:
// r.trackPush:
// 0: Previous mark
// r.stackPush:
// 0: Mark (== current pos, discarded)
// 1: Count
r.trackPop()
r.stackPopN(2)
r.stackPush2(r.trackPeek(), r.stackPeekN(1)-1) // Recall old mark, count
break // Backtrack
case syntax.Setjump:
r.stackPush2(r.trackpos(), r.crawlpos())
r.trackPush()
r.advance(0)
continue
case syntax.Setjump | syntax.Back:
r.stackPopN(2)
break
case syntax.Backjump:
// r.stackPush:
// 0: Saved trackpos
// 1: r.crawlpos
r.stackPopN(2)
r.trackto(r.stackPeek())
for r.crawlpos() != r.stackPeekN(1) {
r.uncapture()
}
break
case syntax.Forejump:
// r.stackPush:
// 0: Saved trackpos
// 1: r.crawlpos
r.stackPopN(2)
r.trackto(r.stackPeek())
r.trackPush1(r.stackPeekN(1))
r.advance(0)
continue
case syntax.Forejump | syntax.Back:
// r.trackPush:
// 0: r.crawlpos
r.trackPop()
for r.crawlpos() != r.trackPeek() {
r.uncapture()
}
break
case syntax.Bol:
if r.leftchars() > 0 && r.charAt(r.textPos()-1) != '\n' {
break
}
r.advance(0)
continue
case syntax.Eol:
if r.rightchars() > 0 && r.charAt(r.textPos()) != '\n' {
break
}
r.advance(0)
continue
case syntax.Boundary:
if !r.isBoundary(r.textPos(), 0, r.runtextend) {
break
}
r.advance(0)
continue
case syntax.Nonboundary:
if r.isBoundary(r.textPos(), 0, r.runtextend) {
break
}
r.advance(0)
continue
case syntax.ECMABoundary:
if !r.isECMABoundary(r.textPos(), 0, r.runtextend) {
break
}
r.advance(0)
continue
case syntax.NonECMABoundary:
if r.isECMABoundary(r.textPos(), 0, r.runtextend) {
break
}
r.advance(0)
continue
case syntax.Beginning:
if r.leftchars() > 0 {
break
}
r.advance(0)
continue
case syntax.Start:
if r.textPos() != r.textstart() {
break
}
r.advance(0)
continue
case syntax.EndZ:
rchars := r.rightchars()
if rchars > 1 {
break
}
// RE2 and EcmaScript define $ as "asserts position at the end of the string"
// PCRE/.NET adds "or before the line terminator right at the end of the string (if any)"
if (r.re.options & (RE2 | ECMAScript)) != 0 {
// RE2/Ecmascript mode
if rchars > 0 {
break
}
} else if rchars == 1 && r.charAt(r.textPos()) != '\n' {
// "regular" mode
break
}
r.advance(0)
continue
case syntax.End:
if r.rightchars() > 0 {
break
}
r.advance(0)
continue
case syntax.One:
if r.forwardchars() < 1 || r.forwardcharnext() != rune(r.operand(0)) {
break
}
r.advance(1)
continue
case syntax.Notone:
if r.forwardchars() < 1 || r.forwardcharnext() == rune(r.operand(0)) {
break
}
r.advance(1)
continue
case syntax.Set:
if r.forwardchars() < 1 || !r.code.Sets[r.operand(0)].CharIn(r.forwardcharnext()) {
break
}
r.advance(1)
continue
case syntax.Multi:
if !r.runematch(r.code.Strings[r.operand(0)]) {
break
}
r.advance(1)
continue
case syntax.Ref:
capnum := r.operand(0)
if r.runmatch.isMatched(capnum) {
if !r.refmatch(r.runmatch.matchIndex(capnum), r.runmatch.matchLength(capnum)) {
break
}
} else {
if (r.re.options & ECMAScript) == 0 {
break
}
}
r.advance(1)
continue
case syntax.Onerep:
c := r.operand(1)
if r.forwardchars() < c {
break
}
ch := rune(r.operand(0))
for c > 0 {
if r.forwardcharnext() != ch {
goto BreakBackward
}
c--
}
r.advance(2)
continue
case syntax.Notonerep:
c := r.operand(1)
if r.forwardchars() < c {
break
}
ch := rune(r.operand(0))
for c > 0 {
if r.forwardcharnext() == ch {
goto BreakBackward
}
c--
}
r.advance(2)
continue
case syntax.Setrep:
c := r.operand(1)
if r.forwardchars() < c {
break
}
set := r.code.Sets[r.operand(0)]
for c > 0 {
if !set.CharIn(r.forwardcharnext()) {
goto BreakBackward
}
c--
}
r.advance(2)
continue
case syntax.Oneloop:
c := r.operand(1)
if c > r.forwardchars() {
c = r.forwardchars()
}
ch := rune(r.operand(0))
i := c
for ; i > 0; i-- {
if r.forwardcharnext() != ch {
r.backwardnext()
break
}
}
if c > i {
r.trackPush2(c-i-1, r.textPos()-r.bump())
}
r.advance(2)
continue
case syntax.Notoneloop:
c := r.operand(1)
if c > r.forwardchars() {
c = r.forwardchars()
}
ch := rune(r.operand(0))
i := c
for ; i > 0; i-- {
if r.forwardcharnext() == ch {
r.backwardnext()
break
}
}
if c > i {
r.trackPush2(c-i-1, r.textPos()-r.bump())
}
r.advance(2)
continue
case syntax.Setloop:
c := r.operand(1)
if c > r.forwardchars() {
c = r.forwardchars()
}
set := r.code.Sets[r.operand(0)]
i := c
for ; i > 0; i-- {
if !set.CharIn(r.forwardcharnext()) {
r.backwardnext()
break
}
}
if c > i {
r.trackPush2(c-i-1, r.textPos()-r.bump())
}
r.advance(2)
continue
case syntax.Oneloop | syntax.Back, syntax.Notoneloop | syntax.Back:
r.trackPopN(2)
i := r.trackPeek()
pos := r.trackPeekN(1)
r.textto(pos)
if i > 0 {
r.trackPush2(i-1, pos-r.bump())
}
r.advance(2)
continue
case syntax.Setloop | syntax.Back:
r.trackPopN(2)
i := r.trackPeek()
pos := r.trackPeekN(1)
r.textto(pos)
if i > 0 {
r.trackPush2(i-1, pos-r.bump())
}
r.advance(2)
continue
case syntax.Onelazy, syntax.Notonelazy:
c := r.operand(1)
if c > r.forwardchars() {
c = r.forwardchars()
}
if c > 0 {
r.trackPush2(c-1, r.textPos())
}
r.advance(2)
continue
case syntax.Setlazy:
c := r.operand(1)
if c > r.forwardchars() {
c = r.forwardchars()
}
if c > 0 {
r.trackPush2(c-1, r.textPos())
}
r.advance(2)
continue
case syntax.Onelazy | syntax.Back:
r.trackPopN(2)
pos := r.trackPeekN(1)
r.textto(pos)
if r.forwardcharnext() != rune(r.operand(0)) {
break
}
i := r.trackPeek()
if i > 0 {
r.trackPush2(i-1, pos+r.bump())
}
r.advance(2)
continue
case syntax.Notonelazy | syntax.Back:
r.trackPopN(2)
pos := r.trackPeekN(1)
r.textto(pos)
if r.forwardcharnext() == rune(r.operand(0)) {
break
}
i := r.trackPeek()
if i > 0 {
r.trackPush2(i-1, pos+r.bump())
}
r.advance(2)
continue
case syntax.Setlazy | syntax.Back:
r.trackPopN(2)
pos := r.trackPeekN(1)
r.textto(pos)
if !r.code.Sets[r.operand(0)].CharIn(r.forwardcharnext()) {
break
}
i := r.trackPeek()
if i > 0 {
r.trackPush2(i-1, pos+r.bump())
}
r.advance(2)
continue
default:
return errors.New("unknown state in regex runner")
}
BreakBackward:
;
// "break Backward" comes here:
r.backtrack()
}
}
// increase the size of stack and track storage
func (r *runner) ensureStorage() {
if r.runstackpos < r.runtrackcount*4 {
doubleIntSlice(&r.runstack, &r.runstackpos)
}
if r.runtrackpos < r.runtrackcount*4 {
doubleIntSlice(&r.runtrack, &r.runtrackpos)
}
}
func doubleIntSlice(s *[]int, pos *int) {
oldLen := len(*s)
newS := make([]int, oldLen*2)
copy(newS[oldLen:], *s)
*pos += oldLen
*s = newS
}
// Save a number on the longjump unrolling stack
func (r *runner) crawl(i int) {
if r.runcrawlpos == 0 {
doubleIntSlice(&r.runcrawl, &r.runcrawlpos)
}
r.runcrawlpos--
r.runcrawl[r.runcrawlpos] = i
}
// Remove a number from the longjump unrolling stack
func (r *runner) popcrawl() int {
val := r.runcrawl[r.runcrawlpos]
r.runcrawlpos++
return val
}
// Get the height of the stack
func (r *runner) crawlpos() int {
return len(r.runcrawl) - r.runcrawlpos
}
func (r *runner) advance(i int) {
r.codepos += (i + 1)
r.setOperator(r.code.Codes[r.codepos])
}
func (r *runner) goTo(newpos int) {
// when branching backward or in place, ensure storage
if newpos <= r.codepos {
r.ensureStorage()
}
r.setOperator(r.code.Codes[newpos])
r.codepos = newpos
}
func (r *runner) textto(newpos int) {
r.runtextpos = newpos
}
func (r *runner) trackto(newpos int) {
r.runtrackpos = len(r.runtrack) - newpos
}
func (r *runner) textstart() int {
return r.runtextstart
}
func (r *runner) textPos() int {
return r.runtextpos
}
// push onto the backtracking stack
func (r *runner) trackpos() int {
return len(r.runtrack) - r.runtrackpos
}
func (r *runner) trackPush() {
r.runtrackpos--
r.runtrack[r.runtrackpos] = r.codepos
}
func (r *runner) trackPush1(I1 int) {
r.runtrackpos--
r.runtrack[r.runtrackpos] = I1
r.runtrackpos--
r.runtrack[r.runtrackpos] = r.codepos
}
func (r *runner) trackPush2(I1, I2 int) {
r.runtrackpos--
r.runtrack[r.runtrackpos] = I1
r.runtrackpos--
r.runtrack[r.runtrackpos] = I2
r.runtrackpos--