-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatcher.h
More file actions
1706 lines (1586 loc) · 61.6 KB
/
matcher.h
File metadata and controls
1706 lines (1586 loc) · 61.6 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>
*/
#define NON_PARTICIPATING_CAPTURE_GROUP ULLONG_MAX
#pragma warning(push)
#pragma warning(disable : 4355)
#pragma pack( push, 1 )
extern RegexPattern *nullAlternative;
extern RegexSymbol *nullSymbol;
template <bool> class BacktrackNode;
template <bool> class RegexMatcher;
template <bool> class Backtrack_EnterGroupLookinto;
class GroupStackNode;
template <bool USE_STRINGS>
class Backtrack
{
#ifdef _DEBUG
Backtrack<USE_STRINGS> &stack;
Uint64 stackDepth;
#endif
// todo: fix the problem that a constant chunk size limits the maximum number of capture groups (to something very very large, but still)
enum { CHUNK_SIZE = 256*1024 };
Uint8 *firstChunk;
Uint8 *chunkBase;
Uint8 *pendingChunkDeletion;
BacktrackNode<USE_STRINGS> *nextToBePopped;
struct ChunkInfo
{
Uint8 *baseOfPreviousChunk;
BacktrackNode<USE_STRINGS> *previousNode;
};
public:
Backtrack()
: pendingChunkDeletion(NULL)
#ifdef _DEBUG
, stack(*this), stackDepth(0)
#endif
{
firstChunk = (Uint8*)malloc(CHUNK_SIZE);
chunkBase = firstChunk;
nextToBePopped = (BacktrackNode<USE_STRINGS>*)(chunkBase + CHUNK_SIZE);
}
~Backtrack()
{
free(chunkBase); // assume that flush() has already been called
}
void flush();
bool empty()
{
return nextToBePopped == (BacktrackNode<USE_STRINGS>*)(chunkBase + CHUNK_SIZE);
}
template <class NODE_TYPE> NODE_TYPE *push(size_t size);
template <class NODE_TYPE> NODE_TYPE *push() { return push<NODE_TYPE>(sizeof(NODE_TYPE)); }
void pop(RegexMatcher<USE_STRINGS> &matcher, bool delayChunkDeletion = false);
void fprint(RegexMatcher<USE_STRINGS> &matcher, FILE *f);
void deletePendingChunk()
{
if (pendingChunkDeletion)
{
free(pendingChunkDeletion);
pendingChunkDeletion = NULL;
}
}
BacktrackNode<USE_STRINGS> &operator*()
{
return *nextToBePopped;
}
BacktrackNode<USE_STRINGS> *operator->()
{
return nextToBePopped;
}
#ifdef _DEBUG
Uint64 getStackDepth()
{
return stackDepth;
}
#endif
};
extern const char Backtrack_VerbName_Commit[];
extern const char Backtrack_VerbName_Prune [];
extern const char Backtrack_VerbName_Skip [];
extern const char Backtrack_VerbName_Then [];
struct captureTuple
{
Uint64 length;
const char *offset;
Uint index;
captureTuple(Uint64 length, const char *offset, Uint index) : length(length), offset(offset), index(index) {}
};
template <bool>
struct RegexMatcherBase
{
};
template <>
struct RegexMatcherBase<false>
{
friend class Regex;
protected:
char basicChar;
bool basicCharIsWordCharacter;
};
template <>
struct RegexMatcherBase<true>
{
const char *stringToMatchAgainst0;
const char *stringToMatchAgainst;
const char **captureOffsets;
const char **captureOffsetsAtomicTmp; // only used with enable_persistent_backrefs
const char **stringLookintoBase;
const char **stringLookintoTop;
};
template <bool USE_STRINGS>
class RegexMatcher : public RegexMatcherBase<USE_STRINGS>
{
friend class Backtrack<USE_STRINGS>;
friend class BacktrackNode<USE_STRINGS>;
friend class Backtrack_Verb<USE_STRINGS, RegexVerb_Commit, Backtrack_VerbName_Commit>;
friend class Backtrack_Verb<USE_STRINGS, RegexVerb_Prune , Backtrack_VerbName_Prune >;
friend class Backtrack_Verb<USE_STRINGS, RegexVerb_Skip , Backtrack_VerbName_Skip >;
friend class Backtrack_Verb<USE_STRINGS, RegexVerb_Then , Backtrack_VerbName_Then >;
friend class Backtrack_Skip<USE_STRINGS>;
friend class Backtrack_AtomicCapture<USE_STRINGS>;
friend class Backtrack_SkipGroup<USE_STRINGS>;
friend class Backtrack_EnterGroup<USE_STRINGS>;
friend class Backtrack_EnterGroupLookinto<USE_STRINGS>;
friend class Backtrack_LeaveConstGroupCapturing<USE_STRINGS>;
friend class Backtrack_LeaveGroup<USE_STRINGS>;
friend class Backtrack_LeaveGroupLazily<USE_STRINGS>;
friend class Backtrack_LeaveCaptureGroup_Base<false, USE_STRINGS>;
friend class Backtrack_LeaveCaptureGroup_Base<true , USE_STRINGS>;
friend class Backtrack_LeaveMolecularLookahead<USE_STRINGS>;
friend class Backtrack_LoopGroup<USE_STRINGS>;
friend class Backtrack_TryMatch<USE_STRINGS>;
friend class Backtrack_ResetStart<USE_STRINGS>;
#ifdef _DEBUG
RegexMatcher<USE_STRINGS> &matcher;
#endif
Uint64 input0;
Uint64 input;
Uint64 *captures;
Uint captureIndexNumUsedAtomicTmp; // only used with enable_persistent_backrefs
bool *captureIndexUsedAtomicTmp; // only used with enable_persistent_backrefs
Uint *captureIndexesAtomicTmp; // only used with enable_persistent_backrefs
Uint64 *capturesAtomicTmp; // only used with enable_persistent_backrefs
Uint64 *inputLookintoBase;
Uint64 *inputLookintoTop;
RegexVerb verb; // can only be RegexVerb_None, RegexVerb_Commit, RegexVerb_Prune, RegexVerb_Skip, or RegexVerb_Then
Uint64 skipPosition; // for RegexVerb_Skip
Backtrack<USE_STRINGS> stack;
Uint *captureStackBase;
Uint *captureStackTop;
GroupStackNode *groupStackBase;
GroupStackNode *groupStackTop;
Uint64 position, startPosition;
Uint64 currentMatch; // ULLONG_MAX means no match has been tried yet
RegexPattern **alternative;
RegexSymbol **symbol;
Uint64 numSteps;
char match; // zero = looking for match, negative = match failed, positive = match found
enum NonMatchType
{
NonMatch_Default,
NonMatch_NegativeLookahead,
NonMatch_EmptyOptional,
NonMatch_CountingPossibleMatches,
};
void nonMatch(NonMatchType type = NonMatch_Default);
void yesMatch(Uint64 newPosition, bool haveChoice);
void pushStack();
bool getLookintoEntrace(Uint backrefIndex, Uint64 &inputLookintoSize, const char *&inputLookintoPtr);
void enterGroup(RegexGroup *group);
void leaveGroup(Backtrack_LeaveGroup<USE_STRINGS> *pushStack, Uint64 pushPosition);
void leaveLazyGroup();
void leaveMaxedOutGroup();
Backtrack_LoopGroup<USE_STRINGS> *pushStack_LoopGroup();
void *loopGroup(Backtrack_LoopGroup<USE_STRINGS> *pushLoop, Uint64 pushPosition, Uint64 oldPosition, Uint alternativeNum);
void popAtomicGroup(RegexGroup *const group);
inline void pushLookintoInput(Uint64 newInput, const char *newStringToMatchAgainst);
inline void popLookintoInput();
inline void initInput(Uint64 _input, Uint numCaptureGroups, Uint maxLookintoDepth);
inline void readCapture(Uint index, Uint64 &multiple, const char *&pBackref);
inline void writeCapture(Uint index, Uint64 multiple, const char * pBackref);
inline bool writeCaptureRelative(Uint index, Uint64 start, Uint64 end); // returns true iff this changes the capture's value
void writeCaptureAtomicTmp(captureTuple capture);
void readCaptureAtomicTmp(Uint i, Uint &index, Uint64 &length, const char *&offset);
inline bool doesRepetendMatchOnce(const char *pBackref, Uint64 multiple, Uint64 count);
inline bool doesRepetendMatchOnce(bool (*matchFunction)(Uchar ch), Uint64 multiple, Uint64 count);
inline bool doesRepetendMatchOnce(RegexCharacterClass *charClass, Uint64 multiple, Uint64 count);
inline bool doesRepetendMatch(const char *pBackref, Uint64 multiple, Uint64 count);
inline bool doesRepetendMatch(bool (*matchFunction)(Uchar ch), Uint64 multiple, Uint64 count);
inline bool doesRepetendMatch(RegexCharacterClass *charClass, Uint64 multiple, Uint64 count);
inline void countRepetendMatches(const char *pBackref, Uint64 multiple);
inline void countRepetendMatches(bool (*matchFunction)(Uchar ch), Uint64 multiple);
inline void countRepetendMatches(RegexCharacterClass *charClass, Uint64 multiple);
inline bool doesStringMatch(RegexSymbol *stringSymbol);
inline bool matchWordBoundary();
void matchSymbol_AlwaysMatch (RegexSymbol *thisSymbol);
void matchSymbol_NeverMatch (RegexSymbol *thisSymbol);
void matchSymbol_String (RegexSymbol *thisSymbol);
void matchSymbol_Character (RegexSymbol *thisSymbol);
void matchSymbol_CharacterClass (RegexSymbol *thisSymbol);
void matchSymbol_Backref (RegexSymbol *thisSymbol);
void matchSymbol_Group (RegexSymbol *thisSymbol);
void matchSymbol_Verb_Accept (RegexSymbol *thisSymbol);
void matchSymbol_Verb_Commit (RegexSymbol *thisSymbol);
void matchSymbol_Verb_Prune (RegexSymbol *thisSymbol);
void matchSymbol_Verb_Skip (RegexSymbol *thisSymbol);
void matchSymbol_Verb_Then (RegexSymbol *thisSymbol);
void matchSymbol_ResetStart (RegexSymbol *thisSymbol);
void matchSymbol_AnchorStart (RegexSymbol *thisSymbol);
void matchSymbol_AnchorEnd (RegexSymbol *thisSymbol);
void matchSymbol_WordBoundaryNot (RegexSymbol *thisSymbol);
void matchSymbol_WordBoundary (RegexSymbol *thisSymbol);
void matchSymbol_DigitNot (RegexSymbol *thisSymbol);
void matchSymbol_Digit (RegexSymbol *thisSymbol);
void matchSymbol_SpaceNot (RegexSymbol *thisSymbol);
void matchSymbol_Space (RegexSymbol *thisSymbol);
void matchSymbol_WordCharacterNot (RegexSymbol *thisSymbol);
void matchSymbol_WordCharacter (RegexSymbol *thisSymbol);
void matchSymbol_ConstGroupNonCapturing (RegexSymbol *thisSymbol);
void matchSymbol_ConstGroupCapturing (RegexSymbol *thisSymbol);
void matchSymbol_IsPrime (RegexSymbol *thisSymbol);
void matchSymbol_IsPowerOf2 (RegexSymbol *thisSymbol);
Uint64 matchSymbol_ConstGroup(RegexSymbol *thisSymbol, bool capturing);
template <typename MATCH_TYPE>
inline int8 runtimeOptimize_matchSymbol_Character_or_Backref(RegexSymbol *const thisSymbol, Uint64 const multiple, MATCH_TYPE const repetend);
template <typename MATCH_TYPE>
bool matchSymbol_Character_or_Backref (RegexSymbol *thisSymbol, Uint64 multiple, MATCH_TYPE pBackref);
inline void (RegexMatcher<USE_STRINGS>::*&matchFunction(RegexSymbol *thisSymbol))(RegexSymbol *thisSymbol);
inline bool characterCanMatch(RegexSymbol *thisSymbol);
inline bool8 characterClassCanMatch(RegexCharacterClass *thisSymbol);
inline void (RegexMatcher<USE_STRINGS>::*chooseBuiltinCharacterClassFunction(bool (*characterMatchFunction)(Uchar ch), void (RegexMatcher<USE_STRINGS>::*matchFunction)(RegexSymbol *thisSymbol)))(RegexSymbol *thisSymbol);
inline bool staticallyOptimizeGroup(RegexSymbol **thisSymbol);
inline void virtualizeSymbols(RegexGroup *rootGroup);
inline void fprintCapture(FILE *f, Uint i);
inline void fprintCapture(FILE *f, Uint64 length, const char *offset);
public:
inline RegexMatcher();
inline ~RegexMatcher();
bool Match(RegexGroupRoot ®ex, Uint numCaptureGroups, Uint maxGroupDepth, Uint maxLookintoDepth, Uint64 _input, Uint returnMatch_backrefIndex, Uint64 &returnMatchOffset, Uint64 &returnMatchLength, Uint64 *possibleMatchesCount_ptr);
};
template <> void RegexMatcher<false>::pushLookintoInput(Uint64 newInput, const char *newStringToMatchAgainst);
template <> void RegexMatcher<false>::popLookintoInput();
template <> void RegexMatcher<true>::pushLookintoInput(Uint64 newInput, const char *newStringToMatchAgainst);
template <> void RegexMatcher<true>::popLookintoInput();
template <> void RegexMatcher<false>::fprintCapture(FILE *f, Uint64 length, const char *offset);
template <> void RegexMatcher<false>::fprintCapture(FILE *f, Uint i);
template <> void RegexMatcher<true>::fprintCapture(FILE *f, Uint64 length, const char *offset);
template <> void RegexMatcher<true>::fprintCapture(FILE *f, Uint i);
template<> inline void RegexMatcher<false>::readCapture(Uint index, Uint64 &multiple, const char *&pBackref)
{
multiple = captures[index];
}
template<> inline void RegexMatcher<true>::readCapture(Uint index, Uint64 &multiple, const char *&pBackref)
{
multiple = captures [index];
pBackref = captureOffsets[index];
}
template<> inline void RegexMatcher<false>::writeCapture(Uint index, Uint64 multiple, const char *pBackref)
{
captures[index] = multiple;
}
template<> inline void RegexMatcher<true>::writeCapture(Uint index, Uint64 multiple, const char *pBackref)
{
captures [index] = multiple;
captureOffsets[index] = pBackref;
}
template<> inline bool RegexMatcher<false>::writeCaptureRelative(Uint index, Uint64 start, Uint64 end)
{
Uint64 prevLength = captures[index];
captures[index] = end - start;
return captures[index] != prevLength;
}
template<> inline bool RegexMatcher<true>::writeCaptureRelative(Uint index, Uint64 start, Uint64 end)
{
Uint64 prevLength = captures [index];
const char *prevOffset = captureOffsets[index];
captures [index] = end - start;
captureOffsets[index] = stringToMatchAgainst + start;
return captures[index] != prevLength || captureOffsets[index] != prevOffset;
}
class GroupStackNode
{
friend class RegexMatcher<false>;
friend class RegexMatcher<true>;
friend class BacktrackNode<false>;
friend class BacktrackNode<true>;
friend class Backtrack_AtomicCapture<false>;
friend class Backtrack_AtomicCapture<true>;
friend class Backtrack_SkipGroup<false>;
friend class Backtrack_SkipGroup<true>;
friend class Backtrack_EnterGroup<false>;
friend class Backtrack_EnterGroup<true>;
friend class Backtrack_LeaveConstGroupCapturing<false>;
friend class Backtrack_LeaveConstGroupCapturing<true>;
friend class Backtrack_LeaveGroup<false>;
friend class Backtrack_LeaveGroup<true>;
friend class Backtrack_LeaveGroupLazily<false>;
friend class Backtrack_LeaveGroupLazily<true>;
friend class Backtrack_LeaveCaptureGroup_Base<true, false>;
friend class Backtrack_LeaveCaptureGroup_Base<true, true >;
friend class Backtrack_LeaveMolecularLookahead<false>;
friend class Backtrack_LeaveMolecularLookahead<true>;
friend class Backtrack_LoopGroup<false>;
friend class Backtrack_LoopGroup<true>;
friend class Backtrack_TryMatch<false>;
friend class Backtrack_TryMatch<true>;
Uint64 position;
Uint64 loopCount; // how many times "group" has been matched
RegexGroup *group;
Uint numCaptured; // how many capture groups inside this group (including nested groups) have been pushed onto the capture stack
};
template <bool USE_STRINGS>
class BacktrackNode
{
friend class RegexMatcher<USE_STRINGS>;
friend class Backtrack<USE_STRINGS>;
virtual size_t getSize(RegexMatcher<USE_STRINGS> &matcher)=0;
virtual bool popTo(RegexMatcher<USE_STRINGS> &matcher)=0; // returns true if the popping can finish with this one
virtual void popForNegativeLookahead(RegexMatcher<USE_STRINGS> &matcher)=0;
virtual int popForAtomicCapture(RegexMatcher<USE_STRINGS> &matcher)=0; // returns the numCaptured delta
virtual captureTuple popForAtomicForwardCapture(RegexMatcher<USE_STRINGS> &matcher, Uint captureNum)=0;
virtual bool okayToTryAlternatives(RegexMatcher<USE_STRINGS> &matcher)=0;
virtual bool isAtomicGroup()
{
return false;
}
virtual void fprintDebug(RegexMatcher<USE_STRINGS> &matcher, FILE *f)=0;
};
template <bool USE_STRINGS>
void Backtrack<USE_STRINGS>::flush()
{
while (chunkBase != firstChunk)
{
ChunkInfo *node = (ChunkInfo*)(chunkBase + CHUNK_SIZE - sizeof(ChunkInfo));
Uint8 *oldChunk = chunkBase;
chunkBase = node->baseOfPreviousChunk;
free(oldChunk);
}
nextToBePopped = (BacktrackNode<USE_STRINGS>*)(chunkBase + CHUNK_SIZE);
}
template <bool USE_STRINGS>
template <class NODE_TYPE> NODE_TYPE *Backtrack<USE_STRINGS>::push(size_t size)
{
#ifdef _DEBUG
stackDepth++;
#endif
Uint8 *newNode = (Uint8*)nextToBePopped - size;
if (newNode < chunkBase)
{
Uint8 *newChunk = (Uint8*)malloc(CHUNK_SIZE);
ChunkInfo *node = (ChunkInfo*)(newChunk + CHUNK_SIZE - sizeof(ChunkInfo));
node->baseOfPreviousChunk = chunkBase;
node->previousNode = nextToBePopped;
chunkBase = newChunk;
newNode = (Uint8*)node - size;
}
return new(nextToBePopped = (BacktrackNode<USE_STRINGS>*)newNode) NODE_TYPE();
}
template <bool USE_STRINGS>
void Backtrack<USE_STRINGS>::pop(RegexMatcher<USE_STRINGS> &matcher, bool delayChunkDeletion/* = false*/)
{
#ifdef _DEBUG
stackDepth--;
#endif
Uint8 *next = (Uint8*)nextToBePopped + nextToBePopped->getSize(matcher);
if (next == chunkBase + CHUNK_SIZE - sizeof(ChunkInfo) && chunkBase != firstChunk)
{
ChunkInfo *node = (ChunkInfo*)next;
Uint8 *oldChunk = chunkBase;
chunkBase = node->baseOfPreviousChunk;
nextToBePopped = node->previousNode;
if (delayChunkDeletion)
pendingChunkDeletion = oldChunk;
else
free(oldChunk);
return;
}
nextToBePopped = (BacktrackNode<USE_STRINGS>*)next;
}
template <bool USE_STRINGS>
void Backtrack<USE_STRINGS>::fprint(RegexMatcher<USE_STRINGS> &matcher, FILE *f)
{
BacktrackNode<USE_STRINGS> *nextPop = nextToBePopped;
Uint8 *base = chunkBase;
while (nextPop != (BacktrackNode<USE_STRINGS>*)(chunkBase + CHUNK_SIZE))
{
nextPop->fprintDebug(matcher, f);
Uint8 *next = (Uint8*)nextPop + nextPop->getSize(matcher);
if (next == base + CHUNK_SIZE - sizeof(ChunkInfo) && base != firstChunk)
{
ChunkInfo *node = (ChunkInfo*)next;
Uint8 *oldChunk = base;
base = node->baseOfPreviousChunk;
nextPop = node->previousNode;
}
else
nextPop = (BacktrackNode<USE_STRINGS>*)next;
}
}
template <bool USE_STRINGS, RegexVerb verb, const char *name>
class Backtrack_Verb : public BacktrackNode<USE_STRINGS>
{
protected:
virtual size_t getSize(RegexMatcher<USE_STRINGS> &matcher)
{
return sizeof(*this);
}
virtual bool popTo(RegexMatcher<USE_STRINGS> &matcher)
{
if (matcher.verb == RegexVerb_None)
matcher.verb = verb;
return false;
}
virtual void popForNegativeLookahead(RegexMatcher<USE_STRINGS> &matcher)
{
}
virtual int popForAtomicCapture(RegexMatcher<USE_STRINGS> &matcher)
{
return 0;
}
virtual captureTuple popForAtomicForwardCapture(RegexMatcher<USE_STRINGS> &matcher, Uint captureNum)
{
UNREACHABLE_CODE;
}
virtual bool okayToTryAlternatives(RegexMatcher<USE_STRINGS> &matcher)
{
return false;
}
virtual void fprintDebug(RegexMatcher<USE_STRINGS> &matcher, FILE *f)
{
fputs(name, f);
fputc('\n', f);
}
};
template <bool USE_STRINGS>
class Backtrack_Commit : public Backtrack_Verb<USE_STRINGS, RegexVerb_Commit, Backtrack_VerbName_Commit> {};
template <bool USE_STRINGS>
class Backtrack_Prune : public Backtrack_Verb<USE_STRINGS, RegexVerb_Prune, Backtrack_VerbName_Prune> {};
template <bool USE_STRINGS>
class Backtrack_Skip : public Backtrack_Verb<USE_STRINGS, RegexVerb_Skip, Backtrack_VerbName_Skip>
{
friend class RegexMatcher<USE_STRINGS>;
Uint64 skipPosition;
virtual size_t getSize(RegexMatcher<USE_STRINGS> &matcher)
{
return sizeof(*this);
}
virtual bool popTo(RegexMatcher<USE_STRINGS> &matcher)
{
if (matcher.verb == RegexVerb_None)
{
matcher.verb = RegexVerb_Skip;
matcher.skipPosition = skipPosition;
}
return false;
}
virtual void fprintDebug(RegexMatcher<USE_STRINGS> &matcher, FILE *f)
{
fprintf(f, "Backtrack_Skip: position=%llu\n", skipPosition);
}
};
template <bool USE_STRINGS>
class Backtrack_Then : public Backtrack_Verb<USE_STRINGS, RegexVerb_Then, Backtrack_VerbName_Then> {};
#pragma warning(push)
#pragma warning(disable : 4700) // for passing "offsets" to fprintCapture() with USE_STRINGS=false
template <bool USE_STRINGS>
class Backtrack_AtomicCapture : public BacktrackNode<USE_STRINGS>
{
friend class RegexMatcher<USE_STRINGS>;
RegexPattern **parentAlternative;
Uint numCaptured;
Uint8 buffer[FLEXIBLE_SIZE_ARRAY];
static size_t get_size(Uint numCaptured)
{
return (size_t)&((Backtrack_AtomicCapture*)0)->buffer + (enable_persistent_backrefs ? (sizeof(Uint64) + (USE_STRINGS ? sizeof(const char*) : 0) + sizeof(Uint))*numCaptured : 0);
}
virtual size_t getSize(RegexMatcher<USE_STRINGS> &matcher)
{
return get_size(numCaptured);
}
virtual bool popTo(RegexMatcher<USE_STRINGS> &matcher)
{
if (enable_persistent_backrefs)
{
const Uint64 *values = (Uint64*)buffer;
const char **offsets;
const 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++)
{
matcher.writeCapture(indexes[i], values[i], USE_STRINGS ? offsets[i] : NULL);
if (values[i] == NON_PARTICIPATING_CAPTURE_GROUP)
{
#ifdef _DEBUG
if (matcher.captureStackTop[-1] != indexes[i])
THROW_ENGINEBUG;
#endif
matcher.captureStackTop--;
matcher.groupStackTop->numCaptured--;
}
}
}
else
{
for (Uint i=0; i<numCaptured; i++)
matcher.captures[*--matcher.captureStackTop] = NON_PARTICIPATING_CAPTURE_GROUP;
matcher.groupStackTop->numCaptured -= numCaptured;
}
matcher.alternative = parentAlternative;
return false;
}
virtual void popForNegativeLookahead(RegexMatcher<USE_STRINGS> &matcher)
{
if (enable_persistent_backrefs)
{
const Uint64 *values = (Uint64*)buffer;
const char **offsets;
const 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++)
{
if (values[i] == NON_PARTICIPATING_CAPTURE_GROUP)
{
#ifdef _DEBUG
if (matcher.captureStackTop[-1] != indexes[i])
THROW_ENGINEBUG;
#endif
matcher.captureStackTop--;
matcher.groupStackTop->numCaptured--;
}
}
}
else
{
for (Uint i=0; i<numCaptured; i++)
matcher.captures[*--matcher.captureStackTop] = NON_PARTICIPATING_CAPTURE_GROUP;
}
}
virtual int popForAtomicCapture(RegexMatcher<USE_STRINGS> &matcher)
{
return (int)numCaptured;
}
virtual captureTuple popForAtomicForwardCapture(RegexMatcher<USE_STRINGS> &matcher, Uint captureNum)
{
const Uint64 *values = (Uint64*)buffer;
const char **offsets;
const Uint *indexes;
if (!USE_STRINGS)
indexes = (Uint*)(values + numCaptured);
else
{
offsets = (const char **)(values + numCaptured);
indexes = (Uint*)(offsets + numCaptured);
}
return USE_STRINGS ? captureTuple(values[captureNum], offsets[captureNum], indexes[captureNum])
: captureTuple(values[captureNum], NULL , indexes[captureNum]);
}
virtual bool okayToTryAlternatives(RegexMatcher<USE_STRINGS> &matcher)
{
return false;
}
void transfer(RegexMatcher<USE_STRINGS> &matcher)
{
if (!enable_persistent_backrefs)
return;
numCaptured = matcher.captureIndexNumUsedAtomicTmp;
const char *&dummy = (const char *&)buffer;
Uint64 *values = (Uint64*)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<matcher.captureIndexNumUsedAtomicTmp; i++)
matcher.readCaptureAtomicTmp(i, indexes[i], values[i], USE_STRINGS ? offsets[i] : dummy);
matcher.captureIndexNumUsedAtomicTmp = 0;
}
virtual void fprintDebug(RegexMatcher<USE_STRINGS> &matcher, FILE *f)
{
fputs("Backtrack_AtomicCapture: ", f);
if (!enable_persistent_backrefs)
fprintf(f, "numCaptured=%u\n", numCaptured);
else
{
const Uint64 *values = (Uint64*)buffer;
const char **offsets;
const 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++)
{
if (i != 0)
fputs(", ", f);
fprintf(f, "\\%u=", indexes[i]+1);
matcher.fprintCapture(f, values[i], USE_STRINGS ? offsets[i] : NULL);
}
fputc('\n', f);
}
}
};
#pragma warning(pop)
template <bool USE_STRINGS>
class Backtrack_SkipGroup : public BacktrackNode<USE_STRINGS>
{
friend class RegexMatcher<USE_STRINGS>;
Uint64 position;
RegexGroup *group;
virtual size_t getSize(RegexMatcher<USE_STRINGS> &matcher)
{
return sizeof(*this);
}
virtual bool popTo(RegexMatcher<USE_STRINGS> &matcher)
{
matcher.position = position;
matcher.enterGroup(group);
matcher.currentMatch = ULLONG_MAX;
return true;
}
virtual void popForNegativeLookahead(RegexMatcher<USE_STRINGS> &matcher)
{
}
virtual int popForAtomicCapture(RegexMatcher<USE_STRINGS> &matcher)
{
return 0;
}
virtual captureTuple popForAtomicForwardCapture(RegexMatcher<USE_STRINGS> &matcher, Uint captureNum)
{
UNREACHABLE_CODE;
}
virtual bool okayToTryAlternatives(RegexMatcher<USE_STRINGS> &matcher)
{
return false;
}
virtual void fprintDebug(RegexMatcher<USE_STRINGS> &matcher, FILE *f)
{
fprintf(f, "Backtrack_SkipGroup: position=%llu\n", position);
}
};
template <bool USE_STRINGS>
class Backtrack_EnterGroup : public BacktrackNode<USE_STRINGS>
{
friend class Backtrack_EnterGroupLookinto<USE_STRINGS>;
virtual size_t getSize(RegexMatcher<USE_STRINGS> &matcher)
{
return sizeof(*this);
}
virtual bool popTo(RegexMatcher<USE_STRINGS> &matcher)
{
RegexGroup *const group = matcher.groupStackTop->group;
#ifdef _DEBUG
if (enable_persistent_backrefs ? matcher.groupStackTop->numCaptured != (matcher.groupStackTop->loopCount > 1) : matcher.groupStackTop->numCaptured)
THROW_ENGINEBUG;
#endif
matcher.groupStackTop--;
matcher.alternative = group->parentAlternative;
matcher.position = matcher.groupStackTop[+1].position;
if (group->isNegativeLookaround())
{
// if we've reached here, it means no match was found inside the negative lookahead, which makes it a match outside
if (!group->self) // group->self will be NULL if this is the lookaround in a conditional
{
if (debugTrace)
fputs("\n\n""Non-match found inside negative lookahead conditional, resulting in a match outside it; jumping to \"yes\" alternative", stderr);
matcher.symbol = (*matcher.alternative)->symbols;
}
else
{
if (debugTrace)
fputs("\n\n""Non-match found inside negative lookahead, resulting in a match outside it", stderr);
matcher.symbol = group->self + 1;
}
matcher.currentMatch = ULLONG_MAX;
return true;
}
if (matcher.groupStackTop->group->type == RegexGroup_LookaroundConditional && group == ((RegexLookaroundConditional*)matcher.groupStackTop->group)->lookaround)
{
if (debugTrace)
fputs("\n\n""Non-match found inside lookaround conditional; jumping to \"no\" alternative", stderr);
matcher.alternative++;
matcher.symbol = *matcher.alternative ? (*matcher.alternative)->symbols : &nullSymbol;
matcher.currentMatch = ULLONG_MAX;
return true;
}
if (matcher.groupStackTop[+1].loopCount > group->minCount && group->minCount != group->maxCount)
{
matcher.symbol = group->self + 1;
matcher.currentMatch = ULLONG_MAX;
return true;
}
return false;
}
virtual void popForNegativeLookahead(RegexMatcher<USE_STRINGS> &matcher)
{
matcher.groupStackTop--;
}
virtual int popForAtomicCapture(RegexMatcher<USE_STRINGS> &matcher)
{
matcher.groupStackTop--;
return 0;
}
virtual captureTuple popForAtomicForwardCapture(RegexMatcher<USE_STRINGS> &matcher, Uint captureNum)
{
UNREACHABLE_CODE;
}
virtual bool okayToTryAlternatives(RegexMatcher<USE_STRINGS> &matcher)
{
return true;
}
virtual void fprintDebug(RegexMatcher<USE_STRINGS> &matcher, FILE *f)
{
fprintf(f, "Backtrack_EnterGroup\n");
}
};
template <bool USE_STRINGS> class Backtrack_EnterGroupLookinto;
template <> class Backtrack_EnterGroupLookinto<false> : public Backtrack_EnterGroup<false>
{
friend class RegexMatcher<false>;
void pushInput(RegexMatcher<false> &matcher, Uint64 newInput, const char *newStringToMatchAgainst)
{
matcher.pushLookintoInput(newInput, newStringToMatchAgainst);
}
virtual size_t getSize(RegexMatcher<false> &matcher)
{
return sizeof(*this);
}
virtual bool popTo(RegexMatcher<false> &matcher)
{
matcher.popLookintoInput();
return Backtrack_EnterGroup<false>::popTo(matcher);
}
virtual void popForNegativeLookahead(RegexMatcher<false> &matcher)
{
matcher.popLookintoInput();
return Backtrack_EnterGroup<false>::popForNegativeLookahead(matcher);
}
virtual int popForAtomicCapture(RegexMatcher<false> &matcher)
{
matcher.popLookintoInput();
return Backtrack_EnterGroup<false>::popForAtomicCapture(matcher);
}
virtual void fprintDebug(RegexMatcher<false> &matcher, FILE *f)
{
fprintf(f, "Backtrack_EnterGroupLookinto\n");
}
};
template <> class Backtrack_EnterGroupLookinto<true> : public Backtrack_EnterGroup<true>
{
friend class RegexMatcher<true>;
void pushInput(RegexMatcher<true> &matcher, Uint64 newInput, const char *newStringToMatchAgainst)
{
matcher.pushLookintoInput(newInput, newStringToMatchAgainst);
}
virtual size_t getSize(RegexMatcher<true> &matcher)
{
return sizeof(*this);
}
virtual bool popTo(RegexMatcher<true> &matcher)
{
matcher.popLookintoInput();
return Backtrack_EnterGroup<true>::popTo(matcher);
}
virtual void popForNegativeLookahead(RegexMatcher<true> &matcher)
{
matcher.popLookintoInput();
return Backtrack_EnterGroup<true>::popForNegativeLookahead(matcher);
}
virtual int popForAtomicCapture(RegexMatcher<true> &matcher)
{
matcher.popLookintoInput();
return Backtrack_EnterGroup<true>::popForAtomicCapture(matcher);
}
virtual void fprintDebug(RegexMatcher<true> &matcher, FILE *f)
{
fprintf(f, "Backtrack_EnterGroupLookinto\n");
}
};
template <bool USE_STRINGS>
class Backtrack_BeginAtomicGroup : public BacktrackNode<USE_STRINGS>
{
virtual size_t getSize(RegexMatcher<USE_STRINGS> &matcher)
{
return sizeof(*this);
}
virtual bool popTo(RegexMatcher<USE_STRINGS> &matcher)
{
return false;
}
virtual void popForNegativeLookahead(RegexMatcher<USE_STRINGS> &matcher)
{
}
virtual int popForAtomicCapture(RegexMatcher<USE_STRINGS> &matcher)
{
return 0;
}
virtual captureTuple popForAtomicForwardCapture(RegexMatcher<USE_STRINGS> &matcher, Uint captureNum)
{
UNREACHABLE_CODE;
}
virtual bool okayToTryAlternatives(RegexMatcher<USE_STRINGS> &matcher)
{
return true;
}
virtual bool isAtomicGroup()
{
return true;
}
virtual void fprintDebug(RegexMatcher<USE_STRINGS> &matcher, FILE *f)
{
fprintf(f, "Backtrack_BeginAtomicGroup\n");
}
};
template <bool USE_STRINGS>
class Backtrack_LeaveMolecularLookahead : public BacktrackNode<USE_STRINGS>
{
friend class RegexMatcher<USE_STRINGS>;
Uint64 position;
Uint numCaptured;
Uint alternative;
RegexGroup *group;
virtual size_t getSize(RegexMatcher<USE_STRINGS> &matcher)
{
return sizeof(*this);
}
virtual bool popTo(RegexMatcher<USE_STRINGS> &matcher)
{
matcher.groupStackTop++;
matcher.groupStackTop->position = position;
matcher.groupStackTop->group = group;
matcher.groupStackTop->numCaptured = numCaptured;
matcher.groupStackTop[-1].numCaptured -= numCaptured;
matcher.alternative = group->alternatives + alternative;
Uint64 inputLookintoSize;
const char *inputLookintoPtr = NULL;
if (!matcher.getLookintoEntrace(((RegexGroupLookinto*)group)->backrefIndex, inputLookintoSize, inputLookintoPtr))
return false;
matcher.pushLookintoInput(inputLookintoSize, inputLookintoPtr);
return false;
}
virtual void popForNegativeLookahead(RegexMatcher<USE_STRINGS> &matcher)
{
matcher.groupStackTop++;
matcher.groupStackTop->group = group;
}
virtual int popForAtomicCapture(RegexMatcher<USE_STRINGS> &matcher)
{
matcher.groupStackTop++;
matcher.groupStackTop->group = group;
return 0;
}
virtual captureTuple popForAtomicForwardCapture(RegexMatcher<USE_STRINGS> &matcher, Uint captureNum)
{
UNREACHABLE_CODE;
}
virtual bool okayToTryAlternatives(RegexMatcher<USE_STRINGS> &matcher)
{
return false;
}
virtual void fprintDebug(RegexMatcher<USE_STRINGS> &matcher, FILE *f)
{
fprintf(f, "Backtrack_LeaveMolecularLookahead: position=%llu, numCaptured=%u, alternative=%u\n", position, numCaptured, alternative);
}
};
template <bool USE_STRINGS>
class Backtrack_LeaveConstGroupCapturing : public BacktrackNode<USE_STRINGS>
{
friend class RegexMatcher<USE_STRINGS>;
Uint backrefIndex;
Uint8 buffer[FLEXIBLE_SIZE_ARRAY];
void popCaptureGroup(RegexMatcher<USE_STRINGS> &matcher)
{
matcher.captures[backrefIndex] = NON_PARTICIPATING_CAPTURE_GROUP;
matcher.captureStackTop--;
matcher.groupStackTop->numCaptured--;
#ifdef _DEBUG
if (*matcher.captureStackTop != backrefIndex)
THROW_ENGINEBUG;
#endif
}
protected:
void popCapture(RegexMatcher<USE_STRINGS> &matcher)
{
const char *&dummy = (const char *&)buffer;
if (!USE_STRINGS)
matcher.writeCapture(backrefIndex, *(Uint64*)(buffer ), dummy);
else
matcher.writeCapture(backrefIndex, *(Uint64*)(buffer + sizeof(const char*)), *(const char**)buffer);
if (matcher.captures[backrefIndex] == NON_PARTICIPATING_CAPTURE_GROUP)
{
matcher.captureStackTop--;
matcher.groupStackTop->numCaptured--;
#ifdef _DEBUG