-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathRegexRuntime.h
1967 lines (1547 loc) · 63.7 KB
/
RegexRuntime.h
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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
//
// Regex programs and their execution context
//
#pragma once
namespace UnifiedRegex
{
typedef CharCount Label;
// FORWARD
struct ScannerInfo;
class ContStack;
class AssertionStack;
class OctoquadMatcher;
enum class ChompMode : uint8
{
Star, // min = 0, max = infinite
Plus // min = 1, max = infinite
};
// ----------------------------------------------------------------------
// Programs
// ----------------------------------------------------------------------
struct Program : private Chars<char16>
{
friend class Lowerer;
friend class Compiler;
friend struct MatchLiteralNode;
friend struct AltNode;
friend class Matcher;
friend struct LoopInfo;
template <typename ScannerT>
friend struct SyncToLiteralAndConsumeInstT;
template <typename ScannerT>
friend struct SyncToLiteralAndContinueInstT;
template <typename ScannerT>
friend struct SyncToLiteralAndBackupInstT;
template <typename ScannerT>
friend struct ScannerMixinT;
template <uint lastPatCharEquivClassSize>
friend struct EquivScannerMixinT;
#define M(TagName) friend struct TagName##Inst;
#define MTemplate(TagName, TemplateDeclaration, GenericClassName, ...) TemplateDeclaration friend struct GenericClassName;
#include "RegexOpCodes.h"
#undef M
#undef MTemplate
public:
// Copy of original text of regex (without delimiting '/'s or trailing flags), null terminated.
// In run-time allocator, owned by program
Field(Char*) source;
Field(CharCount) sourceLen; // length in char16's, NOT including terminating null
// Number of capturing groups (including implicit overall group at index 0)
Field(uint16) numGroups;
Field(int) numLoops;
Field(RegexFlags) flags;
private:
enum class ProgramTag : uint8
{
InstructionsTag,
BOIInstructionsTag,
BOIInstructionsForStickyFlagTag,
SingleCharTag,
BoundedWordTag,
LeadingTrailingSpacesTag,
OctoquadTag,
BOILiteral2Tag
};
Field(ProgramTag) tag;
struct Instructions
{
// Instruction array, in run-time allocator, owned by program, never null
Field(uint8*) insts;
Field(CharCount) instsLen; // in bytes
// Literals
// In run-time allocator, owned by program, may be 0
Field(CharCount) litbufLen; // length of litbuf in char16's, no terminating null
Field(Char*) litbuf;
// These scanner infos are used by ScannersMixin, which is used by only SyncToLiteralsAndBackupInst. There will only
// ever be only one of those instructions per program. Since scanners are large (> 1 KB), for that instruction they
// are allocated on the recycler with pointers stored here to reference them.
Field(Field(ScannerInfo *)*) scannersForSyncToLiterals;
};
struct SingleChar
{
Field(Char) c;
Field(uint8) padding[sizeof(Instructions) - sizeof(Char)];
};
struct Octoquad
{
Field(OctoquadMatcher*) matcher;
Field(uint8) padding[sizeof(Instructions) - sizeof(void*)];
};
struct BOILiteral2
{
Field(DWORD) literal;
Field(uint8) padding[sizeof(Instructions) - sizeof(DWORD)];
};
struct LeadingTrailingSpaces
{
Field(CharCount) beginMinMatch;
Field(CharCount) endMinMatch;
Field(uint8) padding[sizeof(Instructions) - (sizeof(CharCount) * 2)];
};
struct Other
{
Field(uint8) padding[sizeof(Instructions)];
};
union RepType
{
Field(Instructions) insts;
Field(SingleChar) singleChar;
Field(Octoquad) octoquad;
Field(BOILiteral2) boiLiteral2;
Field(LeadingTrailingSpaces) leadingTrailingSpaces;
Field(Other) other;
RepType() {}
};
Field(RepType) rep;
public:
Program(RegexFlags flags);
static Program *New(Recycler *recycler, RegexFlags flags);
static size_t GetOffsetOfTag() { return offsetof(Program, tag); }
static size_t GetOffsetOfRep() { return offsetof(Program, rep); }
static size_t GetOffsetOfBOILiteral2Literal() { return offsetof(BOILiteral2, literal); }
static ProgramTag GetBOILiteral2Tag() { return ProgramTag::BOILiteral2Tag; }
Field(ScannerInfo *)*CreateScannerArrayForSyncToLiterals(Recycler *const recycler);
ScannerInfo *AddScannerForSyncToLiterals(
Recycler *const recycler,
const int scannerIndex,
const CharCount offset,
const CharCount length,
const bool isEquivClass);
void FreeBody(ArenaAllocator* rtAllocator);
inline CaseInsensitive::MappingSource GetCaseMappingSource() const
{
return (flags & UnicodeRegexFlag) != 0
? CaseInsensitive::MappingSource::CaseFolding
: CaseInsensitive::MappingSource::UnicodeData;
}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w);
#endif
};
class Matcher;
// ----------------------------------------------------------------------
// CountDomain
// ----------------------------------------------------------------------
struct CountDomain : private Chars<char16>
{
CharCount lower;
CharCountOrFlag upper; // CharCountFlag => unbounded
inline CountDomain() : lower(0), upper(CharCountFlag) {}
inline CountDomain(CharCount exact) : lower(exact), upper(exact) {}
inline CountDomain(CharCount lower, CharCountOrFlag upper) : lower(lower), upper(upper) {}
inline void Exact(CharCount n)
{
lower = upper = n;
}
inline void Unknown()
{
lower = 0;
upper = CharCountFlag;
}
inline void Lub(const CountDomain& other)
{
lower = min(lower, other.lower);
upper = upper == CharCountFlag || other.upper == CharCountFlag ? CharCountFlag : max(upper, other.upper);
}
inline void Add(const CountDomain& other)
{
lower = lower + other.lower;
upper = upper == CharCountFlag || other.upper == CharCountFlag ? CharCountFlag : upper + other.upper;
}
inline void Sub(const CountDomain& other)
{
lower = other.upper == CharCountFlag || other.upper > lower ? 0 : lower - other.upper;
upper = upper == CharCountFlag ? CharCountFlag : (other.lower > upper ? 0 : upper - other.lower);
}
inline void Mult(const CountDomain& other)
{
if (lower != 0)
{
CharCount maxOther = MaxCharCount / lower;
if (other.lower > maxOther)
// Clip to maximum
lower = MaxCharCount;
else
lower *= other.lower;
}
if (upper != 0 && upper != CharCountFlag)
{
if (other.upper == CharCountFlag)
upper = CharCountFlag;
else
{
CharCount maxOther = MaxCharCount / upper;
if (other.upper > maxOther)
// Clip to 'unbounded'
upper = CharCountFlag;
else
upper *= other.upper;
}
}
}
inline bool CouldMatchEmpty() const
{
return lower == 0;
}
inline bool IsUnbounded() const
{
return upper == CharCountFlag;
}
inline bool IsFixed() const
{
return lower == upper;
}
inline bool IsExact(CharCount n) const
{
return lower == n && upper == n;
}
inline bool IsGreaterThan(const CountDomain& other) const
{
return other.upper != CharCountFlag && lower > other.upper;
}
inline bool IsLessThan(const CountDomain& other) const
{
return upper != CharCountFlag && upper < other.lower;
}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w) const;
#endif
};
// ----------------------------------------------------------------------
// Mix-in types
// ----------------------------------------------------------------------
#pragma pack(push, 1)
// Contains information about how much to back up after syncing to a literal (for the SyncTo... instructions)
struct BackupMixin
{
const CountDomain backup; // range of characters to backup, if upper is CharCountFlag then backup to existing matchStart
inline BackupMixin(const CountDomain& backup) : backup(backup) {}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct CharMixin
{
char16 c;
inline CharMixin(char16 c) : c(c) {}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct Char2Mixin
{
char16 cs[2];
inline Char2Mixin(char16 c0, char16 c1) { cs[0] = c0; cs[1] = c1; }
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct Char3Mixin
{
char16 cs[3];
inline Char3Mixin(char16 c0, char16 c1, char16 c2) { cs[0] = c0; cs[1] = c1; cs[2] = c2; }
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct Char4Mixin
{
char16 cs[4];
inline Char4Mixin(char16 c0, char16 c1, char16 c2, char16 c3) { cs[0] = c0; cs[1] = c1; cs[2] = c2; cs[3] = c3; }
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct LiteralMixin
{
Field(CharCount) offset; // into program's literal buffer
Field(CharCount) length; // in char16's
inline LiteralMixin(CharCount offset, CharCount length) : offset(offset), length(length) {}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf, bool isEquivClass) const;
#endif
};
template<bool IsNegation>
struct SetMixin
{
RuntimeCharSet<char16> set; // contents always lives in run-time allocator
// set must always be cloned from source
void FreeBody(ArenaAllocator* rtAllocator);
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct TrieMixin
{
RuntimeCharTrie trie;
// Trie must always be cloned
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct Char2LiteralScannerMixin : Char2Mixin
{
// scanner must be setup
Char2LiteralScannerMixin(CharCount offset, CharCount length) : Char2Mixin(0, 0) { Assert(length == 2); }
void Setup(char16 c0, char16 c1) { cs[0] = c0; cs[1] = c1; }
CharCount GetLiteralLength() const { return 2; }
bool Match(Matcher& matcher, const char16* const input, const CharCount inputLength, CharCount& inputOffset) const;
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
template <typename ScannerT>
struct ScannerMixinT : LiteralMixin
{
Field(ScannerT) scanner;
// scanner must be setup
ScannerMixinT(CharCount offset, CharCount length) : LiteralMixin(offset, length) {}
CharCount GetLiteralLength() const { return length; }
bool Match(Matcher& matcher, const char16* const input, const CharCount inputLength, CharCount& inputOffset) const;
void FreeBody(ArenaAllocator* rtAllocator);
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf, bool isEquivClass = false) const;
#endif
};
typedef ScannerMixinT<TextbookBoyerMoore<char16>> ScannerMixin;
typedef ScannerMixinT<TextbookBoyerMooreWithLinearMap<char16>> ScannerMixin_WithLinearCharMap;
template <uint lastPatCharEquivCLassSize>
struct EquivScannerMixinT : ScannerMixin
{
// scanner must be setup
EquivScannerMixinT(CharCount offset, CharCount length) : ScannerMixin(offset, length) {}
bool Match(Matcher& matcher, const char16* const input, const CharCount inputLength, CharCount& inputOffset) const;
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
typedef EquivScannerMixinT<CaseInsensitive::EquivClassSize> EquivScannerMixin;
typedef EquivScannerMixinT<1> EquivTrivialLastPatCharScannerMixin;
struct ScannerInfo : ScannerMixin
{
Field(bool) isEquivClass;
// scanner must be setup
inline ScannerInfo(CharCount offset, CharCount length, bool isEquivClass) : ScannerMixin(offset, length), isEquivClass(isEquivClass) {}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct ScannersMixin
{
static const int MaxNumSyncLiterals = 4;
int numLiterals;
Field(ScannerInfo*)* infos;
// scanner mixins must be added
inline ScannersMixin(Recycler *const recycler, Program *const program)
: numLiterals(0), infos(program->CreateScannerArrayForSyncToLiterals(recycler))
{
}
// Only used at compile time
ScannerInfo* Add(Recycler *recycler, Program *program, CharCount offset, CharCount length, bool isEquivClass);
void FreeBody(ArenaAllocator* rtAllocator);
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct GroupMixin
{
const int groupId;
inline GroupMixin(int groupId) : groupId(groupId) {}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct ChompBoundedMixin
{
const CountDomain repeats; // if upper is CharCountFlag, consume as many characters as possible
inline ChompBoundedMixin(const CountDomain& repeats) : repeats(repeats) {}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct JumpMixin
{
Label targetLabel;
// targetLabel must always be fixed up
inline JumpMixin()
{
#if DBG
targetLabel = (Label)-1;
#endif
}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct BodyGroupsMixin
{
int minBodyGroupId;
int maxBodyGroupId;
inline BodyGroupsMixin(int minBodyGroupId, int maxBodyGroupId) : minBodyGroupId(minBodyGroupId), maxBodyGroupId(maxBodyGroupId) {}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct BeginLoopBasicsMixin
{
int loopId;
const CountDomain repeats;
bool hasOuterLoops;
inline BeginLoopBasicsMixin(int loopId, const CountDomain& repeats, bool hasOuterLoops)
: loopId(loopId), repeats(repeats), hasOuterLoops(hasOuterLoops)
{}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct BeginLoopMixin : BeginLoopBasicsMixin
{
bool hasInnerNondet;
Label exitLabel;
// exitLabel must always be fixed up
inline BeginLoopMixin(int loopId, const CountDomain& repeats, bool hasOuterLoops, bool hasInnerNondet)
: BeginLoopBasicsMixin(loopId, repeats, hasOuterLoops), hasInnerNondet(hasInnerNondet)
{
#if DBG
exitLabel = (Label)-1;
#endif
}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct GreedyMixin
{
bool isGreedy;
inline GreedyMixin(bool isGreedy) : isGreedy(isGreedy) {}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct RepeatLoopMixin
{
Label beginLabel; // label of the BeginLoopX instruction
inline RepeatLoopMixin(Label beginLabel) : beginLabel(beginLabel) {}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct GreedyLoopNoBacktrackMixin
{
int loopId;
Label exitLabel;
// exitLabel must always be fixed up
inline GreedyLoopNoBacktrackMixin(int loopId) : loopId(loopId) {}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct TryMixin
{
Label failLabel;
// failLabel must always be fixed up
inline TryMixin()
{
#if DBG
failLabel = (Label)-1;
#endif
}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct NegationMixin
{
bool isNegation;
inline NegationMixin(bool isNegation) : isNegation(isNegation) {}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct NextLabelMixin
{
Label nextLabel;
// nextLabel must always be fixed up
inline NextLabelMixin()
{
#if DBG
nextLabel = (Label)-1;
#endif
}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct FixedLengthMixin
{
CharCount length;
inline FixedLengthMixin(CharCount length) : length(length) {}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct FollowFirstMixin : private Chars<char16>
{
Char followFirst;
inline FollowFirstMixin(Char followFirst) : followFirst(followFirst) {}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct NoNeedToSaveMixin
{
bool noNeedToSave;
inline NoNeedToSaveMixin(bool noNeedToSave) : noNeedToSave(noNeedToSave) {}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
struct SwitchCase
{
char16 c;
Label targetLabel;
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w) const;
#endif
};
template <uint8 n>
struct SwitchMixin
{
static constexpr uint8 MaxCases = n;
uint8 numCases;
// numCases cases, in increasing character order
SwitchCase cases[MaxCases];
// Cases must always be added
inline SwitchMixin() : numCases(0)
{
#if DBG
for (uint8 i = 0; i < MaxCases; i++)
{
cases[i].c = (char16)-1;
cases[i].targetLabel = (Label)-1;
}
#endif
}
// Only used at compile time
void AddCase(char16 c, Label targetLabel);
#if ENABLE_REGEX_CONFIG_OPTIONS
void Print(DebugWriter* w, const char16* litbuf) const;
#endif
};
// ----------------------------------------------------------------------
// Instructions
// ----------------------------------------------------------------------
// NOTE: #pragma pack(1) applies to all Inst structs as well as all Mixin structs (see above).
struct Inst : protected Chars<char16>
{
enum class InstTag : uint8
{
#define M(TagName) TagName,
#define MTemplate(TagName, ...) M(TagName)
#include "RegexOpCodes.h"
#undef M
#undef MTemplate
};
Field(InstTag) tag;
inline Inst(InstTag tag) : tag(tag) {}
void FreeBody(ArenaAllocator* rtAllocator) {}
#if ENABLE_REGEX_CONFIG_OPTIONS
static bool IsBaselineMode();
static Label GetPrintLabel(Label label);
template <typename T>
void PrintBytes(DebugWriter *w, Inst *inst, T *that, const char16 *annotation) const;
#endif
};
#define INST_BODY_FREE(T) \
void FreeBody(ArenaAllocator* rtAllocator) \
{ \
T::FreeBody(rtAllocator); \
Inst::FreeBody(rtAllocator); \
}
#if ENABLE_REGEX_CONFIG_OPTIONS
#define INST_BODY_PRINT int Print(DebugWriter*w, Label label, const Char* litbuf) const;
#else
#define INST_BODY_PRINT
#endif
#define REGEX_INST_EXEC_PARAMETERS Matcher& matcher, const Char* const input, const CharCount inputLength, CharCount &matchStart, CharCount& inputOffset, CharCount &nextSyncInputOffset, const uint8*& instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks, bool firstIteration
#define INST_BODY bool Exec(REGEX_INST_EXEC_PARAMETERS) const; \
INST_BODY_PRINT
//
// Control flow
//
struct NopInst : Inst
{
inline NopInst() : Inst(InstTag::Nop) {}
INST_BODY
};
struct FailInst : Inst
{
inline FailInst() : Inst(InstTag::Fail) {}
INST_BODY
};
struct SuccInst : Inst
{
inline SuccInst() : Inst(InstTag::Succ) {}
INST_BODY
};
struct JumpInst : Inst, JumpMixin
{
// targetLabel must always be fixed up
inline JumpInst() : Inst(InstTag::Jump), JumpMixin() {}
INST_BODY
};
struct JumpIfNotCharInst : Inst, CharMixin, JumpMixin
{
// targetLabel must always be fixed up
inline JumpIfNotCharInst(Char c) : Inst(InstTag::JumpIfNotChar), CharMixin(c), JumpMixin() {}
INST_BODY
};
struct MatchCharOrJumpInst : Inst, CharMixin, JumpMixin
{
// targetLabel must always be fixed up
inline MatchCharOrJumpInst(Char c) : Inst(InstTag::MatchCharOrJump), CharMixin(c), JumpMixin() {}
INST_BODY
};
struct JumpIfNotSetInst : Inst, SetMixin<false>, JumpMixin
{
// set must always be cloned from source
// targetLabel must always be fixed up
inline JumpIfNotSetInst() : Inst(InstTag::JumpIfNotSet), JumpMixin() {}
INST_BODY
INST_BODY_FREE(SetMixin<false>)
};
struct MatchSetOrJumpInst : Inst, SetMixin<false>, JumpMixin
{
// set must always be cloned from source
// targetLabel must always be fixed up
inline MatchSetOrJumpInst() : Inst(InstTag::MatchSetOrJump), JumpMixin() {}
INST_BODY
INST_BODY_FREE(SetMixin<false>)
};
#define SwitchInstActual(n) \
struct Switch##n##Inst : Inst, SwitchMixin<n> \
{ \
inline Switch##n##Inst() : Inst(InstTag::Switch##n), SwitchMixin() {} \
INST_BODY \
};
SwitchInstActual(2);
SwitchInstActual(4);
SwitchInstActual(8);
SwitchInstActual(16);
SwitchInstActual(24);
#undef SwitchInstActual
#define SwitchAndConsumeInstActual(n) \
struct SwitchAndConsume##n##Inst : Inst, SwitchMixin<n> \
{ \
inline SwitchAndConsume##n##Inst() : Inst(InstTag::SwitchAndConsume##n), SwitchMixin() {} \
INST_BODY \
};
SwitchAndConsumeInstActual(2);
SwitchAndConsumeInstActual(4);
SwitchAndConsumeInstActual(8);
SwitchAndConsumeInstActual(16);
SwitchAndConsumeInstActual(24);
#undef SwitchAndConsumeInstActual
//
// Built-in assertions
//
// BOI = Beginning of Input
template <bool canHardFail>
struct BOITestInst : Inst
{
BOITestInst();
INST_BODY
};
// EOI = End of Input
template <bool canHardFail>
struct EOITestInst : Inst
{
EOITestInst();
INST_BODY
};
// BOL = Beginning of Line (/^.../)
struct BOLTestInst : Inst
{
inline BOLTestInst() : Inst(InstTag::BOLTest) {}
INST_BODY
};
// EOL = End of Line (/...$/)
struct EOLTestInst : Inst
{
inline EOLTestInst() : Inst(InstTag::EOLTest) {}
INST_BODY
};
template <bool isNegation>
struct WordBoundaryTestInst : Inst
{
WordBoundaryTestInst();
INST_BODY
};
//
// Matching
//
struct MatchCharInst : Inst, CharMixin
{
inline MatchCharInst(Char c) : Inst(InstTag::MatchChar), CharMixin(c) {}
INST_BODY
};
struct MatchChar2Inst : Inst, Char2Mixin
{
inline MatchChar2Inst(Char c0, Char c1) : Inst(InstTag::MatchChar2), Char2Mixin(c0, c1) {}
INST_BODY
};
struct MatchChar3Inst : Inst, Char3Mixin
{
inline MatchChar3Inst(Char c0, Char c1, Char c2) : Inst(InstTag::MatchChar3), Char3Mixin(c0, c1, c2) {}
INST_BODY
};
struct MatchChar4Inst : Inst, Char4Mixin
{
inline MatchChar4Inst(Char c0, Char c1, Char c2, Char c3) : Inst(InstTag::MatchChar4), Char4Mixin(c0, c1, c2, c3) {}
INST_BODY
};
template<bool IsNegation>
struct MatchSetInst : Inst, SetMixin<IsNegation>
{
// set must always be cloned from source
inline MatchSetInst() : Inst(IsNegation ? InstTag::MatchNegatedSet : InstTag::MatchSet) {}
INST_BODY
INST_BODY_FREE(SetMixin<IsNegation>)
};
struct MatchLiteralInst : Inst, LiteralMixin
{
inline MatchLiteralInst(CharCount offset, CharCount length) : Inst(InstTag::MatchLiteral), LiteralMixin(offset, length) {}
INST_BODY
};
struct MatchLiteralEquivInst : Inst, LiteralMixin
{
inline MatchLiteralEquivInst(CharCount offset, CharCount length) : Inst(InstTag::MatchLiteralEquiv), LiteralMixin(offset, length) {}
INST_BODY
};
struct MatchTrieInst : Inst, TrieMixin
{
// Trie must always be cloned
inline MatchTrieInst() : Inst(InstTag::MatchTrie) {}
void FreeBody(ArenaAllocator* rtAllocator);
INST_BODY
};
struct OptMatchCharInst : Inst, CharMixin
{
inline OptMatchCharInst(Char c) : Inst(InstTag::OptMatchChar), CharMixin(c) {}
INST_BODY
};
struct OptMatchSetInst : Inst, SetMixin<false>
{
// set must always be cloned from source
inline OptMatchSetInst() : Inst(InstTag::OptMatchSet) {}
INST_BODY
INST_BODY_FREE(SetMixin<false>)
};
//
// Synchronization:
// SyncTo(Char|Char2Set|Set|Char2Literal|Literal|LiteralEquiv|Literals)And(Consume|Continue|Backup)
//
struct SyncToCharAndContinueInst : Inst, CharMixin
{
inline SyncToCharAndContinueInst(Char c) : Inst(InstTag::SyncToCharAndContinue), CharMixin(c) {}
INST_BODY
};
struct SyncToChar2SetAndContinueInst : Inst, Char2Mixin
{
inline SyncToChar2SetAndContinueInst(Char c0, Char c1) : Inst(InstTag::SyncToChar2SetAndContinue), Char2Mixin(c0, c1) {}
INST_BODY
};
template<bool IsNegation>
struct SyncToSetAndContinueInst : Inst, SetMixin<IsNegation>
{
// set must always be cloned from source
inline SyncToSetAndContinueInst() : Inst(IsNegation ? InstTag::SyncToNegatedSetAndContinue : InstTag::SyncToSetAndContinue) {}
INST_BODY
INST_BODY_FREE(SetMixin<IsNegation>)
};
template <typename ScannerT>
struct SyncToLiteralAndContinueInstT : Inst, ScannerT
{
SyncToLiteralAndContinueInstT(InstTag tag, CharCount offset, CharCount length) : Inst(tag), ScannerT(offset, length) {}
INST_BODY
};