-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatcher.cpp
More file actions
1609 lines (1480 loc) · 62.3 KB
/
matcher.cpp
File metadata and controls
1609 lines (1480 loc) · 62.3 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
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
/**
* License:
* This Source Code Form is subject to the terms of
* the Mozilla Public License, v. 2.0. If a copy of
* the MPL was not distributed with this file, You
* can obtain one at http://mozilla.org/MPL/2.0/.
*
* Authors:
* David Ellsworth <davide.by.zero@gmail.com>
*/
#include "regex.h"
#include "matcher.h"
#include "matcher-optimization.h"
RegexPattern *nullAlternative = NULL;
RegexSymbol *nullSymbol = NULL;
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::nonMatch(NonMatchType type)
{
if (debugTrace)
{
switch (type)
{
case NonMatch_Default:
fputs(": non-match", stderr);
break;
case NonMatch_NegativeLookahead:
fputs("Match found inside negative lookahead, resulting in a non-match outside it\n\n", stderr);
break;
case NonMatch_EmptyOptional:
fputs("Empty match found in group with maximum quantifer unsatisfied; treating this as a non-match\n\n", stderr);
break;
case NonMatch_CountingPossibleMatches:
fputs("Turning successful full match into a non-match to count possible matches\n\n", stderr);
break;
default:
UNREACHABLE_CODE;
}
}
position = groupStackTop->position;
// if any changes are made here, they may need to be duplicated in Backtrack_Commit<USE_STRINGS>::popTo()
for (;;)
{
if (verb != RegexVerb_None && verb != RegexVerb_Then)
{
if ((groupStackTop->group->isNegativeLookaround()) && stack->okayToTryAlternatives(*this))
verb = RegexVerb_None;
}
else
if (*alternative && (stack.empty() || stack->okayToTryAlternatives(*this)) && !inrange(groupStackTop->group->type, RegexGroup_Conditional, RegexGroup_LookaroundConditional))
{
alternative++;
if (*alternative)
{
verb = RegexVerb_None;
if (groupStackTop->group->type==RegexGroup_Lookinto || groupStackTop->group->type==RegexGroup_LookintoMolecular || groupStackTop->group->type==RegexGroup_NegativeLookinto)
position = 0;
else
position = groupStackTop->position;
symbol = (*alternative)->symbols;
currentMatch = ULLONG_MAX;
return;
}
}
if (stack.empty())
{
match = verb == RegexVerb_Commit ? -2 : -1;
return;
}
BacktrackNode<USE_STRINGS> &formerTop = *stack;
stack.pop(*this, true);
bool stopHere = formerTop.popTo(*this);
stack.deletePendingChunk();
if (stopHere && verb == RegexVerb_None)
break;
}
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::pushStack()
{
if ((*symbol)->possessive)
return;
Backtrack_TryMatch<USE_STRINGS> *pushStack = stack.template push< Backtrack_TryMatch<USE_STRINGS> >();
pushStack->position = position;
pushStack->currentMatch = currentMatch;
pushStack->symbol = *symbol;
}
template <bool USE_STRINGS>
bool RegexMatcher<USE_STRINGS>::getLookintoEntrace(Uint backrefIndex, Uint64 &inputLookintoSize, const char *&inputLookintoPtr)
{
if (backrefIndex == UINT_MAX)
inputLookintoSize = input0;
else
if (backrefIndex == 0)
inputLookintoSize = position - startPosition;
else
{
readCapture(backrefIndex - 1, inputLookintoSize, inputLookintoPtr);
if (inputLookintoSize == NON_PARTICIPATING_CAPTURE_GROUP)
{
if (!emulate_ECMA_NPCGs)
{
nonMatch();
return false;
}
inputLookintoSize = 0;
}
}
return true;
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::enterGroup(RegexGroup *group)
{
bool enteringLookinto = false;
Uint64 inputLookintoSize;
const char *inputLookintoPtr = NULL;
{
Uint64 newPosition = position;
switch (group->type)
{
case RegexGroup_Lookinto:
case RegexGroup_LookintoMolecular:
case RegexGroup_NegativeLookinto:
enteringLookinto = true;
if (!getLookintoEntrace(((RegexGroupLookinto*)group)->backrefIndex, inputLookintoSize, inputLookintoPtr))
return;
break;
}
}
RegexPattern **alternativeTmp = group->alternatives;
if (group->type == RegexGroup_Conditional)
{
Uint64 multiple;
const char *pBackref;
readCapture(((RegexConditional*)group)->backrefIndex, multiple, pBackref);
if (multiple == NON_PARTICIPATING_CAPTURE_GROUP)
{
alternativeTmp++;
if (*alternativeTmp == NULL)
{
symbol++;
return;
}
}
}
alternative = alternativeTmp;
symbol = (*alternative)->symbols;
groupStackTop++;
groupStackTop->position = position;
groupStackTop->loopCount = 1;
groupStackTop->group = group;
groupStackTop->numCaptured = 0;
if (group->possessive)
stack.template push< Backtrack_BeginAtomicGroup<USE_STRINGS> >();
if (enteringLookinto)
{
Backtrack_EnterGroupLookinto<USE_STRINGS> *pushLookinto = stack.template push< Backtrack_EnterGroupLookinto<USE_STRINGS> >();
pushLookinto->pushInput(*this, inputLookintoSize, inputLookintoPtr);
position = 0;
}
else
stack.template push< Backtrack_EnterGroup<USE_STRINGS> >();
if (group->type == RegexGroup_Atomic)
stack.template push< Backtrack_BeginAtomicGroup<USE_STRINGS> >();
else
if (group->type == RegexGroup_LookaroundConditional)
enterGroup(((RegexLookaroundConditional*)group)->lookaround);
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::leaveGroup(Backtrack_LeaveGroup<USE_STRINGS> *pushStack, Uint64 pushPosition)
{
RegexGroup *const group = groupStackTop->group;
pushStack->position = pushPosition;
pushStack->loopCount = groupStackTop->loopCount;
pushStack->group = group;
pushStack->numCaptured = groupStackTop->numCaptured;
pushStack->alternative = (Uint)(alternative - group->alternatives);
if (group->type == RegexGroup_Capturing)
{
Uint backrefIndex = ((RegexGroupCapturing*)group)->backrefIndex;
Uint64 prevValue = captures[backrefIndex];
writeCaptureRelative(backrefIndex, groupStackTop->position, position);
if (!enable_persistent_backrefs || prevValue == NON_PARTICIPATING_CAPTURE_GROUP)
{
*captureStackTop++ = backrefIndex;
groupStackTop->numCaptured++;
}
}
alternative = group->parentAlternative;
symbol = group->self + 1;
groupStackTop[-1].numCaptured += groupStackTop->numCaptured;
groupStackTop--;
currentMatch = ULLONG_MAX;
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::leaveLazyGroup()
{
Backtrack_LeaveGroupLazily<USE_STRINGS> *pushStack;
if (enable_persistent_backrefs && groupStackTop->group->type == RegexGroup_Capturing)
{
Backtrack_LeaveCaptureGroupLazily<USE_STRINGS> *pushStackCapture = stack.template push< Backtrack_LeaveCaptureGroupLazily<USE_STRINGS> >();
pushStackCapture->setCapture(*this);
pushStack = pushStackCapture;
}
else
pushStack = stack.template push< Backtrack_LeaveGroupLazily<USE_STRINGS> >();
pushStack->positionDiff = position - groupStackTop->position;
leaveGroup(pushStack, position);
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::leaveMaxedOutGroup()
{
Backtrack_LeaveGroup<USE_STRINGS> *pushStack;
RegexGroup *const group = groupStackTop->group;
bool possessive = group->possessive;
if (enable_persistent_backrefs && group->type == RegexGroup_Capturing)
{
Backtrack_LeaveCaptureGroup<USE_STRINGS> *pushStackCapture = stack.template push< Backtrack_LeaveCaptureGroup<USE_STRINGS> >();
pushStackCapture->setCapture(*this);
pushStack = pushStackCapture;
}
else
pushStack = stack.template push< Backtrack_LeaveGroup<USE_STRINGS> >();
leaveGroup(pushStack, groupStackTop->position);
if (possessive)
popAtomicGroup(group);
}
template <bool USE_STRINGS>
Backtrack_LoopGroup<USE_STRINGS> *RegexMatcher<USE_STRINGS>::pushStack_LoopGroup()
{
Uint64 size;
int set_numCaptured = -1;
if (!enable_persistent_backrefs)
size = Backtrack_LoopGroup<USE_STRINGS>::get_size(groupStackTop->numCaptured);
else
{
Uint backrefIndex = ((RegexGroupCapturing*)groupStackTop->group)->backrefIndex;
set_numCaptured = (groupStackTop->group->type == RegexGroup_Capturing) ? 1 : 0;
size = Backtrack_LoopGroup<USE_STRINGS>::get_size(set_numCaptured);
}
Backtrack_LoopGroup<USE_STRINGS> *pushStack = stack.template push< Backtrack_LoopGroup<USE_STRINGS> >(size);
if (set_numCaptured >= 0)
pushStack->numCaptured = set_numCaptured;
return pushStack;
}
template <bool USE_STRINGS>
void *RegexMatcher<USE_STRINGS>::loopGroup(Backtrack_LoopGroup<USE_STRINGS> *pushLoop, Uint64 pushPosition, Uint64 oldPosition, Uint alternativeNum)
{
groupStackTop->loopCount++;
pushLoop->position = pushPosition;
const RegexGroup *group = groupStackTop->group;
if (!enable_persistent_backrefs)
{
const Uint numCaptured = groupStackTop->numCaptured;
pushLoop->numCaptured = numCaptured;
groupStackTop->numCaptured = 0;
const char *&dummy = (const char *&)pushLoop->buffer;
Uint64 *values = (Uint64*)pushLoop->buffer;
const char **offsets;
Uint *indexes;
if (!USE_STRINGS)
indexes = (Uint*)(values + numCaptured);
else
{
offsets = (const char **)(values + numCaptured);
indexes = (Uint*)(offsets + numCaptured);
}
for (Uint i=0; i<numCaptured; i++)
{
indexes[i] = captureStackTop[(int)i - (int)numCaptured];
readCapture(indexes[i], values[i], USE_STRINGS ? offsets[i] : dummy);
if (!enable_persistent_backrefs)
captures[indexes[i]] = NON_PARTICIPATING_CAPTURE_GROUP;
}
captureStackTop -= numCaptured;
}
else
if (group->type == RegexGroup_Capturing)
{
Uint backrefIndex = ((RegexGroupCapturing*)group)->backrefIndex;
pushLoop->numCaptured = 1;
const char *&dummy = (const char *&)pushLoop->buffer;
if (!USE_STRINGS)
readCapture(backrefIndex, *(Uint64*)(pushLoop->buffer ), dummy);
else
readCapture(backrefIndex, *(Uint64*)(pushLoop->buffer + sizeof(const char*)), *(const char**)pushLoop->buffer);
bool firstCapture = captures[backrefIndex] == NON_PARTICIPATING_CAPTURE_GROUP;
if (writeCaptureRelative(backrefIndex, oldPosition, pushPosition) && firstCapture)
{
*captureStackTop++ = backrefIndex;
groupStackTop->numCaptured++;
}
}
alternative = group->alternatives;
symbol = group->alternatives[0]->symbols;
groupStackTop->position = position;
currentMatch = ULLONG_MAX;
pushLoop->oldPosition = oldPosition;
pushLoop->alternative = alternativeNum;
if (group->type == RegexGroup_Atomic)
stack.template push< Backtrack_BeginAtomicGroup<USE_STRINGS> >();
else
if (group->type == RegexGroup_LookaroundConditional)
enterGroup(((RegexLookaroundConditional*)group)->lookaround);
return (void*)pushLoop->buffer;
}
bool matchWordCharacter(Uchar ch);
template<> void RegexMatcher<false>::initInput(Uint64 _input, Uint numCaptureGroups, Uint maxLookintoDepth)
{
input = input0 = _input;
basicCharIsWordCharacter = matchWordCharacter(basicChar);
}
template<> void RegexMatcher<true>::initInput(Uint64 _input, Uint numCaptureGroups, Uint maxLookintoDepth)
{
delete [] stringLookintoBase;
stringLookintoBase = new const char * [maxLookintoDepth];
stringLookintoTop = stringLookintoBase;
stringToMatchAgainst = stringToMatchAgainst0 = (const char *)_input;
input = input0 = strlen(stringToMatchAgainst);
delete [] captureOffsets;
captureOffsets = new const char * [numCaptureGroups];
for (Uint i=0; i<numCaptureGroups; i++)
captureOffsets[i] = stringToMatchAgainst;
if (enable_persistent_backrefs)
{
delete [] captureOffsetsAtomicTmp;
captureOffsetsAtomicTmp = new const char * [numCaptureGroups];
}
}
template<> bool RegexMatcher<false>::doesRepetendMatch(const char *pBackref, Uint64 multiple, Uint64 count)
{
return true;
}
template<> bool RegexMatcher<false>::doesRepetendMatch(bool (*matchFunction)(Uchar ch), Uint64 multiple, Uint64 count)
{
return true;
}
template<> bool RegexMatcher<false>::doesRepetendMatch(RegexCharacterClass *charClass, Uint64 multiple, Uint64 count)
{
return true;
}
template<> bool RegexMatcher<true>::doesRepetendMatch(const char *pBackref, Uint64 multiple, Uint64 count)
{
if (pBackref)
{
const char *s = stringToMatchAgainst + position;
const char *upperBound = stringToMatchAgainst + input - multiple;
for (Uint64 i=0; i < count && s <= upperBound; i++, s+=multiple)
if (memcmp(s, pBackref, (size_t)multiple)!=0)
return false;
}
return true;
}
template<> bool RegexMatcher<true>::doesRepetendMatch(bool (*matchFunction)(Uchar ch), Uint64 multiple, Uint64 count)
{
const char *s = stringToMatchAgainst + position;
const char *upperBound = stringToMatchAgainst + input - 1;
for (Uint64 i=0; i < count && s <= upperBound; i++, s+=1)
if (!matchFunction(*s))
return false;
return true;
}
template<> bool RegexMatcher<true>::doesRepetendMatch(RegexCharacterClass *charClass, Uint64 multiple, Uint64 count)
{
const char *s = stringToMatchAgainst + position;
const char *upperBound = stringToMatchAgainst + input - 1;
for (Uint64 i=0; i < count && s <= upperBound; i++, s+=1)
if (!charClass->isInClass(*s))
return false;
return true;
}
template<> bool RegexMatcher<false>::doesRepetendMatchOnce(const char *pBackref, Uint64 multiple, Uint64 count)
{
return true;
}
template<> bool RegexMatcher<false>::doesRepetendMatchOnce(bool (*matchFunction)(Uchar ch), Uint64 multiple, Uint64 count)
{
return true;
}
template<> bool RegexMatcher<false>::doesRepetendMatchOnce(RegexCharacterClass *charClass, Uint64 multiple, Uint64 count)
{
return true;
}
template<> bool RegexMatcher<true>::doesRepetendMatchOnce(const char *pBackref, Uint64 multiple, Uint64 count)
{
if (pBackref)
{
const char *s = stringToMatchAgainst + position + count * multiple;
return memcmp(s, pBackref, (size_t)multiple)==0;
}
return true;
}
template<> bool RegexMatcher<true>::doesRepetendMatchOnce(bool (*matchFunction)(Uchar ch), Uint64 multiple, Uint64 count)
{
return matchFunction(stringToMatchAgainst[position + count]);
}
template<> bool RegexMatcher<true>::doesRepetendMatchOnce(RegexCharacterClass *charClass, Uint64 multiple, Uint64 count)
{
return !!charClass->isInClass(stringToMatchAgainst[position + count]); // todo: give these functions bool8 return values so that the overhead of the conversion from bool8 to bool can be eliminated
}
template<> void RegexMatcher<false>::countRepetendMatches(const char *pBackref, Uint64 multiple)
{
}
template<> void RegexMatcher<false>::countRepetendMatches(bool (*matchFunction)(Uchar ch), Uint64 multiple)
{
}
template<> void RegexMatcher<false>::countRepetendMatches(RegexCharacterClass *charClass, Uint64 multiple)
{
}
template<> void RegexMatcher<true>::countRepetendMatches(const char *pBackref, Uint64 multiple)
{
const char *s = stringToMatchAgainst + position;
Uint64 count;
for (count = 0; count < currentMatch; count++, s+=multiple)
if (memcmp(s, pBackref, (size_t)multiple)!=0)
break;
currentMatch = count;
}
template<> void RegexMatcher<true>::countRepetendMatches(bool (*matchFunction)(Uchar ch), Uint64 multiple)
{
const char *s = stringToMatchAgainst + position;
Uint64 count;
for (count = 0; count < currentMatch; count++, s+=1)
if (!matchFunction(*s))
break;
currentMatch = count;
}
template<> void RegexMatcher<true>::countRepetendMatches(RegexCharacterClass *charClass, Uint64 multiple)
{
const char *s = stringToMatchAgainst + position;
Uint64 count;
for (count = 0; count < currentMatch; count++, s+=1)
if (!charClass->isInClass(*s))
break;
currentMatch = count;
}
template<> inline bool RegexMatcher<false>::doesStringMatch(RegexSymbol *stringSymbol)
{
// it can't be a match in the case of !USE_STRINGS, because RegexSymbol_String is only created by the parser if there's more than one kind of character in it
return false;
}
template<> inline bool RegexMatcher<true>::doesStringMatch(RegexSymbol *stringSymbol)
{
return position + stringSymbol->strLength <= input && memcmp(stringToMatchAgainst + position, stringSymbol->string, stringSymbol->strLength)==0;
}
template<> bool RegexMatcher<false>::matchWordBoundary()
{
return basicCharIsWordCharacter && (position==0 || position==input) && input!=0;
}
template<> bool RegexMatcher<true>::matchWordBoundary()
{
bool lfWord = position==0 ? false : matchWordCharacter(stringToMatchAgainst[position-1]);
bool rhWord = position==input ? false : matchWordCharacter(stringToMatchAgainst[position ]);
return lfWord != rhWord;
}
template<> void (RegexMatcher<false>::*&RegexMatcher<false>::matchFunction(RegexSymbol *thisSymbol))(RegexSymbol *thisSymbol)
{
return thisSymbol->numberMatchFunction;
}
template<> void (RegexMatcher<true >::*&RegexMatcher<true >::matchFunction(RegexSymbol *thisSymbol))(RegexSymbol *thisSymbol)
{
return thisSymbol->stringMatchFunction;
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_AlwaysMatch(RegexSymbol *thisSymbol)
{
symbol++;
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_NeverMatch(RegexSymbol *thisSymbol)
{
nonMatch();
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_String(RegexSymbol *thisSymbol)
{
if (!doesStringMatch(thisSymbol))
{
nonMatch();
return;
}
position += thisSymbol->strLength;
symbol++;
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_Character(RegexSymbol *thisSymbol)
{
if (USE_STRINGS)
matchSymbol_Character_or_Backref(thisSymbol, 1, thisSymbol->characterAny ? (const char *)NULL : &thisSymbol->character);
else
matchSymbol_Character_or_Backref(thisSymbol, 1, (const char *)NULL);
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_CharacterClass(RegexSymbol *thisSymbol)
{
if (USE_STRINGS)
matchSymbol_Character_or_Backref(thisSymbol, 1, (RegexCharacterClass*)thisSymbol);
else
matchSymbol_Character_or_Backref(thisSymbol, 1, (const char *)NULL);
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_Backref(RegexSymbol *thisSymbol)
{
Uint64 multiple;
const char *pBackref;
readCapture(((RegexBackref*)thisSymbol)->index, multiple, pBackref);
if (multiple == NON_PARTICIPATING_CAPTURE_GROUP)
{
if (!emulate_ECMA_NPCGs && thisSymbol->minCount != 0)
{
nonMatch();
return;
}
multiple = 0;
}
if (multiple == 0) // don't backtrack when it will make no difference to do so
{
symbol++;
return;
}
matchSymbol_Character_or_Backref(thisSymbol, multiple, pBackref);
}
template <bool USE_STRINGS>
template <typename MATCH_TYPE>
bool RegexMatcher<USE_STRINGS>::matchSymbol_Character_or_Backref(RegexSymbol *thisSymbol, Uint64 multiple, MATCH_TYPE repetend)
// return true if repetend was matched at least once
{
if (currentMatch == ULLONG_MAX)
{
if (char optimized = runtimeOptimize_matchSymbol_Character_or_Backref(thisSymbol, multiple, repetend))
return optimized > 0;
if (thisSymbol->lazy)
{
currentMatch = thisSymbol->minCount;
if (!doesRepetendMatch(repetend, multiple, currentMatch))
{
nonMatch();
return false;
}
}
else
{
if (thisSymbol->maxCount == UINT_MAX)
{
Uint64 spaceLeft = input - position;
currentMatch = spaceLeft / multiple;
if (currentMatch < thisSymbol->minCount)
{
nonMatch();
return false;
}
if (USE_STRINGS && repetend)
{
countRepetendMatches(repetend, multiple);
if (currentMatch < thisSymbol->minCount)
{
nonMatch();
return false;
}
}
bool matched = currentMatch != 0;
if (currentMatch > thisSymbol->minCount)
pushStack();
if (USE_STRINGS)
position += currentMatch * multiple;
else
position = input - spaceLeft % multiple;
currentMatch = ULLONG_MAX;
symbol++;
return matched;
}
else
{
currentMatch = thisSymbol->maxCount;
if (USE_STRINGS && repetend)
{
countRepetendMatches(repetend, multiple);
if (currentMatch < thisSymbol->minCount)
{
nonMatch();
return false;
}
}
}
}
}
else
goto try_next_match;
for (;;)
{
{
Uint64 neededMatch = position + currentMatch * multiple;
if (input >= neededMatch)
{
if (USE_STRINGS && thisSymbol->lazy && currentMatch && !doesRepetendMatchOnce(repetend, multiple, currentMatch-1))
{
nonMatch();
return false;
}
bool matched = currentMatch != 0;
if (currentMatch != (thisSymbol->lazy ? MAX_EXTEND(thisSymbol->maxCount) : thisSymbol->minCount))
pushStack();
position = neededMatch;
currentMatch = ULLONG_MAX;
symbol++;
return matched;
}
}
if (thisSymbol->lazy)
{
nonMatch();
return false;
}
try_next_match:
if (thisSymbol->lazy)
{
if (currentMatch == MAX_EXTEND(thisSymbol->maxCount))
{
nonMatch();
return false;
}
currentMatch++;
}
else
{
if (currentMatch == thisSymbol->minCount)
{
nonMatch();
return false;
}
currentMatch--;
}
}
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_Group(RegexSymbol *thisSymbol)
{
RegexGroup *group = (RegexGroup*)thisSymbol;
if (group->maxCount == 0)
{
symbol++;
return;
}
if (group->lazy && group->minCount == 0)
{
Backtrack_SkipGroup<USE_STRINGS> *pushStack = stack.template push< Backtrack_SkipGroup<USE_STRINGS> >();
pushStack->position = position;
pushStack->group = group;
symbol++;
return;
}
enterGroup(group);
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_Verb_Accept(RegexSymbol *thisSymbol)
{
symbol++;
verb = RegexVerb_Accept;
symbol = &nullSymbol;
}
const char Backtrack_VerbName_Commit[] = "Backtrack_Commit";
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_Verb_Commit(RegexSymbol *thisSymbol)
{
stack.template push< Backtrack_Commit<USE_STRINGS> >();
symbol++;
}
const char Backtrack_VerbName_Prune[] = "Backtrack_Prune";
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_Verb_Prune(RegexSymbol *thisSymbol)
{
stack.template push< Backtrack_Prune<USE_STRINGS> >();
symbol++;
}
const char Backtrack_VerbName_Skip[] = ""; // dummy string, not actually used; just here to make g++ happy
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_Verb_Skip(RegexSymbol *thisSymbol)
{
Backtrack_Skip<USE_STRINGS> *pushStack = stack.template push< Backtrack_Skip<USE_STRINGS> >();
pushStack->skipPosition = position;
symbol++;
}
const char Backtrack_VerbName_Then[] = "Backtrack_Then";
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_Verb_Then(RegexSymbol *thisSymbol)
{
stack.template push< Backtrack_Then<USE_STRINGS> >();
symbol++;
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_ResetStart(RegexSymbol *thisSymbol)
{
if (startPosition < position)
{
Backtrack_ResetStart<USE_STRINGS> *pushStack = stack.template push< Backtrack_ResetStart<USE_STRINGS> >();
pushStack->startPosition = startPosition;
startPosition = position;
}
symbol++;
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_AnchorStart(RegexSymbol *thisSymbol)
{
if (position == 0)
{
symbol++;
return;
}
nonMatch();
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_AnchorEnd(RegexSymbol *thisSymbol)
{
if (position == input)
{
symbol++;
return;
}
nonMatch();
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_WordBoundaryNot(RegexSymbol *thisSymbol)
{
if (!matchWordBoundary())
{
symbol++;
return;
}
nonMatch();
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_WordBoundary(RegexSymbol *thisSymbol)
{
if (matchWordBoundary())
{
symbol++;
return;
}
nonMatch();
}
bool matchDigitNot (Uchar ch) {return !(inrange(ch,'0','9') );}
bool matchDigit (Uchar ch) {return (inrange(ch,'0','9') );}
bool matchSpaceNot (Uchar ch) {return !(inrange(ch,0x9,0xD) || ch==' ' || ch==(Uchar)0xA0 );} // non-breaking space; WARNING: may not be portable
bool matchSpace (Uchar ch) {return (inrange(ch,0x9,0xD) || ch==' ' || ch==(Uchar)0xA0 );} // non-breaking space; WARNING: may not be portable
bool matchWordCharacterNot(Uchar ch) {return !(inrange(ch,'0','9') || inrange(ch,'A','Z') || inrange(ch,'a','z') || ch=='_');}
bool matchWordCharacter (Uchar ch) {return (inrange(ch,'0','9') || inrange(ch,'A','Z') || inrange(ch,'a','z') || ch=='_');}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_DigitNot(RegexSymbol *thisSymbol)
{
if (USE_STRINGS)
matchSymbol_Character_or_Backref(thisSymbol, 1, &matchDigitNot);
else
matchSymbol_Character_or_Backref(thisSymbol, 1, (const char *)NULL);
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_Digit(RegexSymbol *thisSymbol)
{
if (USE_STRINGS)
matchSymbol_Character_or_Backref(thisSymbol, 1, &matchDigit);
else
matchSymbol_Character_or_Backref(thisSymbol, 1, (const char *)NULL);
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_SpaceNot(RegexSymbol *thisSymbol)
{
if (USE_STRINGS)
matchSymbol_Character_or_Backref(thisSymbol, 1, &matchSpaceNot);
else
matchSymbol_Character_or_Backref(thisSymbol, 1, (const char *)NULL);
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_Space(RegexSymbol *thisSymbol)
{
if (USE_STRINGS)
matchSymbol_Character_or_Backref(thisSymbol, 1, &matchSpace);
else
matchSymbol_Character_or_Backref(thisSymbol, 1, (const char *)NULL);
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_WordCharacterNot(RegexSymbol *thisSymbol)
{
if (USE_STRINGS)
matchSymbol_Character_or_Backref(thisSymbol, 1, &matchWordCharacterNot);
else
matchSymbol_Character_or_Backref(thisSymbol, 1, (const char *)NULL);
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::matchSymbol_WordCharacter(RegexSymbol *thisSymbol)
{
if (USE_STRINGS)
matchSymbol_Character_or_Backref(thisSymbol, 1, &matchWordCharacter);
else
matchSymbol_Character_or_Backref(thisSymbol, 1, (const char *)NULL);
}
template<> bool RegexMatcher<false>::characterCanMatch(RegexSymbol *thisSymbol)
{
if (thisSymbol->characterAny)
return true;
return thisSymbol->character == basicChar;
}
template<> bool RegexMatcher<true>::characterCanMatch(RegexSymbol *thisSymbol)
{
return true;
}
template<> bool8 RegexMatcher<false>::characterClassCanMatch(RegexCharacterClass *thisSymbol)
{
return thisSymbol->isInClass(basicChar);
}
template<> bool8 RegexMatcher<true>::characterClassCanMatch(RegexCharacterClass *thisSymbol)
{
return true;
}
template<> void (RegexMatcher<false>::*RegexMatcher<false>::chooseBuiltinCharacterClassFunction(bool (*characterMatchFunction)(Uchar ch), void (RegexMatcher<false>::*matchFunction)(RegexSymbol *thisSymbol)))(RegexSymbol *thisSymbol)
{
if (characterMatchFunction(basicChar))
return &RegexMatcher<false>::matchSymbol_Character;
else
return &RegexMatcher<false>::matchSymbol_NeverMatch;
}
template<> void (RegexMatcher<true>::*RegexMatcher<true>::chooseBuiltinCharacterClassFunction(bool (*characterMatchFunction)(Uchar ch), void (RegexMatcher<true>::*matchFunction)(RegexSymbol *thisSymbol)))(RegexSymbol *thisSymbol)
{
return matchFunction;
}
template <bool USE_STRINGS>
void RegexMatcher<USE_STRINGS>::virtualizeSymbols(RegexGroup *rootGroup)
{
// Note that this group iterator code is redundant with that at the end of RegexParser::RegexParser(); todo: Factor it out into a separate function
RegexPattern **thisAlternative;
RegexSymbol **thisSymbol = &(RegexSymbol*&)rootGroup;
for (;;)
{
if (*thisSymbol)
{
switch ((*thisSymbol)->type)
{
case RegexSymbol_NoOp:
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_AlwaysMatch;
break;
case RegexSymbol_Character:
if (characterCanMatch(*thisSymbol))
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_Character;
else
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_NeverMatch;
break;
case RegexSymbol_CharacterClass:
if (characterClassCanMatch((RegexCharacterClass*)*thisSymbol))
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_CharacterClass;
else
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_NeverMatch;
break;
case RegexSymbol_String:
if (USE_STRINGS)
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_String;
else
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_NeverMatch;
break;
case RegexSymbol_Backref:
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_Backref;
break;
case RegexSymbol_ResetStart:
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_ResetStart;
break;
case RegexSymbol_AnchorStart:
if ((*thisSymbol)->minCount == 0)
{
(*thisSymbol)->type = RegexSymbol_NoOp;
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_AlwaysMatch;
}
else
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_AnchorStart;
break;
case RegexSymbol_AnchorEnd:
if ((*thisSymbol)->minCount == 0)
{
(*thisSymbol)->type = RegexSymbol_NoOp;
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_AlwaysMatch;
}
else
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_AnchorEnd;
break;
case RegexSymbol_WordBoundaryNot:
if ((*thisSymbol)->minCount == 0)
{
(*thisSymbol)->type = RegexSymbol_NoOp;
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_AlwaysMatch;
}
else
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_WordBoundaryNot;
break;
case RegexSymbol_WordBoundary:
if ((*thisSymbol)->minCount == 0)
{
(*thisSymbol)->type = RegexSymbol_NoOp;
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_AlwaysMatch;
}
else
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_WordBoundary;
break;
case RegexSymbol_DigitNot:
matchFunction(*thisSymbol++) = chooseBuiltinCharacterClassFunction(matchDigitNot, &RegexMatcher<USE_STRINGS>::matchSymbol_DigitNot);
break;
case RegexSymbol_Digit:
matchFunction(*thisSymbol++) = chooseBuiltinCharacterClassFunction(matchDigit, &RegexMatcher<USE_STRINGS>::matchSymbol_Digit);
break;
case RegexSymbol_SpaceNot:
matchFunction(*thisSymbol++) = chooseBuiltinCharacterClassFunction(matchSpaceNot, &RegexMatcher<USE_STRINGS>::matchSymbol_SpaceNot);
break;
case RegexSymbol_Space:
matchFunction(*thisSymbol++) = chooseBuiltinCharacterClassFunction(matchSpace, &RegexMatcher<USE_STRINGS>::matchSymbol_Space);
break;
case RegexSymbol_WordCharacterNot:
matchFunction(*thisSymbol++) = chooseBuiltinCharacterClassFunction(matchWordCharacterNot, &RegexMatcher<USE_STRINGS>::matchSymbol_WordCharacterNot);
break;
case RegexSymbol_WordCharacter:
matchFunction(*thisSymbol++) = chooseBuiltinCharacterClassFunction(matchWordCharacter, &RegexMatcher<USE_STRINGS>::matchSymbol_WordCharacter);
break;
case RegexSymbol_ConstGroupNonCapturing:
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_ConstGroupNonCapturing;
break;
case RegexSymbol_ConstGroupCapturing:
matchFunction(*thisSymbol++) = &RegexMatcher<USE_STRINGS>::matchSymbol_ConstGroupCapturing;
break;
case RegexSymbol_IsPrime:
case RegexSymbol_IsPowerOf2:
if (USE_STRINGS)
{
RegexSymbol *originalSymbol = (*thisSymbol)->originalSymbol;
delete *thisSymbol;
*thisSymbol = originalSymbol;
}