forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegexCompileTime.cpp
More file actions
4807 lines (4283 loc) · 180 KB
/
RegexCompileTime.cpp
File metadata and controls
4807 lines (4283 loc) · 180 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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "ParserPch.h"
namespace UnifiedRegex
{
// ----------------------------------------------------------------------
// Compiler (inlines etc)
// ----------------------------------------------------------------------
// The VS2013 linker treats this as a redefinition of an already
// defined constant and complains. So skip the declaration if we're compiling
// with VS2013 or below.
#if !defined(_MSC_VER) || _MSC_VER >= 1900
const CharCount Compiler::initInstBufSize;
#endif
uint8* Compiler::Emit(size_t size)
{
Assert(size <= UINT32_MAX);
if (instLen - instNext < size)
{
CharCount newLen = max(instLen, initInstBufSize);
CharCount instLenPlus = (CharCount)(instLen + size - 1);
// check for overflow
if (instLenPlus < instLen || instLenPlus * 2 < instLenPlus)
{
Js::Throw::OutOfMemory();
}
while (newLen <= instLenPlus)
{
newLen *= 2;
}
instBuf = (uint8*)ctAllocator->Realloc(instBuf, instLen, newLen);
instLen = newLen;
}
uint8* inst = instBuf + instNext;
instNext += (CharCount)size;
return inst;
}
template <typename T>
T* Compiler::Emit()
{
return new(Emit(sizeof(T))) T;
}
#define EMIT(compiler, T, ...) (new (compiler.Emit(sizeof(T))) T(__VA_ARGS__))
#define L2I(O, label) LabelToInstPointer<O##Inst>(Inst::InstTag::O, label)
// Remember: The machine address of an instruction is no longer valid after a subsequent emit,
// so all label fixups must be done using Compiler::GetFixup / Compiler::DoFixup
// ----------------------------------------------------------------------
// Node
// ----------------------------------------------------------------------
void Node::AppendLiteral(CharCount& litbufNext, CharCount litbufLen, __inout_ecount(litbufLen) Char* litbuf) const
{
Assert(false);
}
CharCount Node::EmitScanFirstSet(Compiler& compiler)
{
Assert(prevConsumes.IsExact(0));
if (thisConsumes.CouldMatchEmpty())
// Can't be sure of consuming something in FIRST
return 0;
if (firstSet->Count() > maxSyncToSetSize)
// HEURISTIC: If FIRST is large we'll get too many false positives
return 0;
//
// Compilation scheme:
//
// SyncTo(Char|Char2|Set)And(Consume|Continue)
//
Char entries[CharSet<Char>::MaxCompact];
int count = firstSet->GetCompactEntries(2, entries);
if (SupportsPrefixSkipping(compiler))
{
if (count == 1)
EMIT(compiler, SyncToCharAndConsumeInst, entries[0]);
else if (count == 2)
EMIT(compiler, SyncToChar2SetAndConsumeInst, entries[0], entries[1]);
else
EMIT(compiler, SyncToSetAndConsumeInst<false>)->set.CloneFrom(compiler.rtAllocator, *firstSet);
return 1;
}
else
{
if (count == 1)
EMIT(compiler, SyncToCharAndContinueInst, entries[0]);
else if (count == 2)
EMIT(compiler, SyncToChar2SetAndContinueInst, entries[0], entries[1]);
else
EMIT(compiler, SyncToSetAndContinueInst<false>)->set.CloneFrom(compiler.rtAllocator, *firstSet);
return 0;
}
}
bool Node::IsBetterSyncronizingNode(Compiler& compiler, Node* curr, Node* proposed)
{
int proposedNumLiterals = 0;
CharCount proposedLength = proposed->MinSyncronizingLiteralLength(compiler, proposedNumLiterals);
if (proposedLength == 0 || proposedNumLiterals > maxNumSyncLiterals)
// Not a synchronizable node or too many literals.
return false;
if (curr == nullptr)
// We'll take whatever we can get
return true;
int currNumLiterals = 0;
CharCount currLength = curr->MinSyncronizingLiteralLength(compiler, currNumLiterals);
// Lexicographic ordering based on
// - whether literal length is above a threshold (above is better)
// - number of literals (smaller is better)
// - upper bound on backup (finite is better)
// - minimum literal length (longer is better)
// - actual backup upper bound (shorter is better)
if (proposedLength >= preferredMinSyncToLiteralLength
&& currLength < preferredMinSyncToLiteralLength)
{
return true;
}
if (proposedLength < preferredMinSyncToLiteralLength
&& currLength >= preferredMinSyncToLiteralLength)
{
return false;
}
if (proposedNumLiterals < currNumLiterals)
return true;
if (proposedNumLiterals > currNumLiterals)
return false;
if (!proposed->prevConsumes.IsUnbounded() && curr->prevConsumes.IsUnbounded())
return true;
if (proposed->prevConsumes.IsUnbounded() && !curr->prevConsumes.IsUnbounded())
return false;
if (proposedLength > currLength)
return true;
if (proposedLength < currLength)
return false;
return proposed->prevConsumes.upper < curr->prevConsumes.upper;
}
bool Node::IsSingleChar(Compiler& compiler, Char& outChar) const
{
if (tag != Node::MatchChar)
return false;
const MatchCharNode* node = (const MatchCharNode*)this;
if (node->isEquivClass)
return false;
outChar = node->cs[0];
return true;
}
bool Node::IsBoundedWord(Compiler& compiler) const
{
if (tag != Node::Concat)
return false;
const ConcatNode* concatNode = (const ConcatNode *)this;
if (concatNode->head->tag != Node::WordBoundary ||
concatNode->tail == 0 ||
concatNode->tail->head->tag != Node::Loop ||
concatNode->tail->tail == 0 ||
concatNode->tail->tail->head->tag != Node::WordBoundary ||
concatNode->tail->tail->tail != 0)
return false;
const WordBoundaryNode* enter = (const WordBoundaryNode*)concatNode->head;
const LoopNode* loop = (const LoopNode*)concatNode->tail->head;
const WordBoundaryNode* leave = (const WordBoundaryNode*)concatNode->tail->tail->head;
if (enter->isNegation ||
!loop->isGreedy ||
loop->repeats.lower != 1 ||
loop->repeats.upper != CharCountFlag ||
loop->body->tag != Node::MatchSet ||
leave->isNegation)
return false;
const MatchSetNode* wordSet = (const MatchSetNode*)loop->body;
if (wordSet->isNegation)
return false;
return wordSet->set.IsEqualTo(*compiler.standardChars->GetWordSet());
}
bool Node::IsBOILiteral2(Compiler& compiler) const
{
if (tag != Node::Concat)
return false;
const ConcatNode* concatNode = (const ConcatNode *)this;
if ((compiler.program->flags & (IgnoreCaseRegexFlag | MultilineRegexFlag)) != 0 ||
concatNode->head->tag != Node::BOL ||
concatNode->tail == nullptr ||
concatNode->tail->head->tag != Node::MatchLiteral ||
concatNode->tail->tail != nullptr ||
((MatchLiteralNode *)concatNode->tail->head)->isEquivClass ||
((MatchLiteralNode *)concatNode->tail->head)->length != 2)
{
return false;
}
return true;
}
bool Node::IsLeadingTrailingSpaces(Compiler& compiler, CharCount& leftMinMatch, CharCount& rightMinMatch) const
{
if (tag != Node::Alt)
return false;
if (compiler.program->flags & MultilineRegexFlag)
return false;
const AltNode* altNode = (const AltNode*)this;
if (altNode->head->tag != Node::Concat ||
altNode->tail == 0 ||
altNode->tail->head->tag != Node::Concat ||
altNode->tail->tail != 0)
return false;
const ConcatNode* left = (const ConcatNode*)altNode->head;
const ConcatNode* right = (const ConcatNode*)altNode->tail->head;
if (left->head->tag != Node::BOL ||
left->tail == 0 ||
left->tail->head->tag != Node::Loop ||
left->tail->tail != 0)
return false;
if (right->head->tag != Node::Loop ||
right->tail == 0 ||
right->tail->head->tag != Node::EOL ||
right->tail->tail != 0)
return false;
const LoopNode* leftLoop = (const LoopNode*)left->tail->head;
const LoopNode* rightLoop = (const LoopNode*)right->head;
if (!leftLoop->isGreedy ||
leftLoop->repeats.upper != CharCountFlag ||
leftLoop->body->tag != Node::MatchSet ||
!rightLoop->isGreedy ||
rightLoop->repeats.upper != CharCountFlag ||
rightLoop->body->tag != Node::MatchSet)
return false;
const MatchSetNode* leftSet = (const MatchSetNode*)leftLoop->body;
const MatchSetNode* rightSet = (const MatchSetNode*)rightLoop->body;
if (leftSet->isNegation ||
rightSet->isNegation)
return false;
leftMinMatch = leftLoop->repeats.lower;
rightMinMatch = rightLoop->repeats.lower;
return
leftSet->set.IsEqualTo(*compiler.standardChars->GetWhitespaceSet()) &&
rightSet->set.IsEqualTo(*compiler.standardChars->GetWhitespaceSet());
}
#if ENABLE_REGEX_CONFIG_OPTIONS
void Node::PrintAnnotations(DebugWriter* w) const
{
if (firstSet != 0)
{
w->PrintEOL(_u("<"));
w->Indent();
w->Print(_u("features: {"));
bool first = true;
for (uint i = Empty; i <= Assertion; i++)
{
if ((features & (1 << i)) != 0)
{
if (first)
first = false;
else
w->Print(_u(","));
switch (i)
{
case Empty: w->Print(_u("Empty")); break;
case BOL: w->Print(_u("BOL")); break;
case EOL: w->Print(_u("EOL")); break;
case WordBoundary: w->Print(_u("WordBoundary")); break;
case MatchLiteral: w->Print(_u("MatchLiteral")); break;
case MatchChar: w->Print(_u("MatchChar")); break;
case Concat: w->Print(_u("Concat")); break;
case Alt: w->Print(_u("Alt")); break;
case DefineGroup: w->Print(_u("DefineGroup")); break;
case MatchGroup: w->Print(_u("MatchGroup")); break;
case Loop: w->Print(_u("Loop")); break;
case MatchSet: w->Print(_u("MatchSet")); break;
case Assertion: w->Print(_u("Assertion")); break;
}
}
}
w->PrintEOL(_u("}"));
w->Print(_u("firstSet: "));
firstSet->Print(w);
if (isFirstExact)
w->Print(_u(" (exact)"));
w->EOL();
w->Print(_u("followSet: "));
followSet->Print(w);
w->EOL();
w->Print(_u("prevConsumes: "));
prevConsumes.Print(w);
w->EOL();
w->Print(_u("thisConsumes: "));
thisConsumes.Print(w);
w->EOL();
w->Print(_u("followConsumes: "));
followConsumes.Print(w);
w->EOL();
w->PrintEOL(_u("isThisIrrefutable: %s"), isThisIrrefutable ? _u("true") : _u("false"));
w->PrintEOL(_u("isFollowIrrefutable: %s"), isFollowIrrefutable ? _u("true") : _u("false"));
w->PrintEOL(_u("isWord: %s"), isWord ? _u("true") : _u("false"));
w->PrintEOL(_u("isThisWillNotProgress: %s"), isThisWillNotProgress ? _u("true") : _u("false"));
w->PrintEOL(_u("isThisWillNotRegress: %s"), isThisWillNotRegress ? _u("true") : _u("false"));
w->PrintEOL(_u("isPrevWillNotProgress: %s"), isPrevWillNotProgress ? _u("true") : _u("false"));
w->PrintEOL(_u("isPrevWillNotRegress: %s"), isPrevWillNotRegress ? _u("true") : _u("false"));
w->PrintEOL(_u("isDeterministic: %s"), isDeterministic ? _u("true") : _u("false"));
w->PrintEOL(_u("isNotInLoop: %s"), isNotInLoop ? _u("true") : _u("false"));
w->PrintEOL(_u("isNotNegated: %s"), isNotNegated ? _u("true") : _u("false"));
w->PrintEOL(_u("isAtLeastOnce: %s"), isAtLeastOnce ? _u("true") : _u("false"));
w->PrintEOL(_u("hasInitialHardFailBOI: %s"), hasInitialHardFailBOI ? _u("true") : _u("false"));
w->Unindent();
w->PrintEOL(_u(">"));
}
}
#endif
// ----------------------------------------------------------------------
// SimpleNode
// ----------------------------------------------------------------------
CharCount SimpleNode::LiteralLength() const
{
return 0;
}
bool SimpleNode::IsCharOrPositiveSet() const
{
return false;
}
CharCount SimpleNode::TransferPass0(Compiler& compiler, const Char* litbuf)
{
return 0;
}
void SimpleNode::TransferPass1(Compiler& compiler, const Char* litbuf)
{
}
bool SimpleNode::IsRefiningAssertion(Compiler& compiler)
{
return tag == EOL && (compiler.program->flags & MultilineRegexFlag) != 0;
}
void SimpleNode::AnnotatePass0(Compiler& compiler)
{
isWord = false;
}
void SimpleNode::AnnotatePass1(Compiler& compiler, bool parentNotInLoop, bool parentAtLeastOnce, bool parentNotSpeculative, bool parentNotNegated)
{
isFirstExact = false;
thisConsumes.Exact(0);
isThisWillNotProgress = true;
isThisWillNotRegress = true;
isNotInLoop = parentNotInLoop;
isAtLeastOnce = parentAtLeastOnce;
isNotSpeculative = parentNotSpeculative;
isNotNegated = parentNotNegated;
switch (tag)
{
case Empty:
features = HasEmpty;
firstSet = compiler.standardChars->GetEmptySet();
isThisIrrefutable = true;
break;
case BOL:
features = HasBOL;
firstSet = compiler.standardChars->GetFullSet();
isThisIrrefutable = false;
break;
case EOL:
features = HasEOL;
if ((compiler.program->flags & MultilineRegexFlag) != 0)
firstSet = compiler.standardChars->GetNewlineSet();
else
firstSet = compiler.standardChars->GetEmptySet();
isThisIrrefutable = false;
break;
default:
Assert(false);
}
}
void SimpleNode::AnnotatePass2(Compiler& compiler, CountDomain accumConsumes, bool accumPrevWillNotProgress, bool accumPrevWillNotRegress)
{
prevConsumes = accumConsumes;
isPrevWillNotProgress = accumPrevWillNotProgress;
isPrevWillNotRegress = accumPrevWillNotRegress;
}
void SimpleNode::AnnotatePass3(Compiler& compiler, CountDomain accumConsumes, CharSet<Char>* accumFollow, bool accumFollowIrrefutable, bool accumFollowEOL)
{
followConsumes = accumConsumes;
followSet = accumFollow;
isFollowIrrefutable = accumFollowIrrefutable;
isFollowEOL = accumFollowEOL;
hasInitialHardFailBOI = ((tag == BOL) &&
prevConsumes.IsExact(0) &&
(compiler.program->flags & MultilineRegexFlag) == 0 &&
isAtLeastOnce &&
isNotNegated &&
isPrevWillNotRegress);
}
void SimpleNode::AnnotatePass4(Compiler& compiler)
{
isDeterministic = true;
}
bool SimpleNode::SupportsPrefixSkipping(Compiler& compiler) const
{
return false;
}
Node* SimpleNode::HeadSyncronizingNode(Compiler& compiler)
{
return 0;
}
CharCount SimpleNode::MinSyncronizingLiteralLength(Compiler& compiler, int& numLiterals) const
{
return 0;
}
void SimpleNode::CollectSyncronizingLiterals(Compiler& compiler, ScannersMixin& scanners) const
{
Assert(false);
}
void SimpleNode::BestSyncronizingNode(Compiler& compiler, Node*& bestNode)
{
}
void SimpleNode::AccumDefineGroups(Js::ScriptContext* scriptContext, int& minGroup, int& maxGroup)
{
}
void SimpleNode::Emit(Compiler& compiler, CharCount& skipped)
{
Assert(skipped == 0);
switch (tag)
{
case Empty:
// Nothing
break;
case BOL:
{
if ((compiler.program->flags & MultilineRegexFlag) != 0)
{
//
// Compilation scheme:
//
// BOLTest
//
EMIT(compiler, BOLTestInst);
}
else
{
if (compiler.CurrentLabel() == 0)
{
// The first instruction is BOI, change the tag and only execute it once
// without looping every start position
compiler.SetBOIInstructionsProgramTag();
}
else
{
//
// Compilation scheme:
//
// BOITest
//
// Obviously starting later in the string won't help, so can hard fail if:
// - this pattern must always be matched
// - not in a negative assertion
// - backtracking could never rewind the input pointer
//
bool canHardFail = isAtLeastOnce && isNotNegated && isPrevWillNotRegress;
if (canHardFail)
{
EMIT(compiler, BOITestInst<true>);
}
else
{
EMIT(compiler, BOITestInst<false>);
}
}
}
break;
}
case EOL:
{
if ((compiler.program->flags & MultilineRegexFlag) != 0)
{
//
// Compilation scheme:
//
// EOLTest
//
EMIT(compiler, EOLTestInst);
}
else
{
//
// Compilation scheme:
//
// EOITest
//
// Can hard fail if
// - this pattern must always be matched
// - not in a negative assertion
// - backtracking could never advance the input pointer
//
bool canHardFail = isAtLeastOnce && isNotNegated && isPrevWillNotProgress;
if (canHardFail)
{
EMIT(compiler, EOITestInst<true>);
}
else
{
EMIT(compiler, EOITestInst<false>);
}
}
break;
}
default:
Assert(false);
}
}
CharCount SimpleNode::EmitScan(Compiler& compiler, bool isHeadSyncronizingNode)
{
Assert(false);
return 0;
}
bool SimpleNode::IsOctoquad(Compiler& compiler, OctoquadIdentifier* oi)
{
return false;
}
bool SimpleNode::IsCharTrieArm(Compiler& compiler, uint& accNumAlts) const
{
return tag == Empty;
}
bool SimpleNode::BuildCharTrie(Compiler& compiler, CharTrie* trie, Node* cont, bool isAcceptFirst) const
{
PROBE_STACK_NO_DISPOSE(compiler.scriptContext, Js::Constants::MinStackRegex);
Assert(tag == Empty);
if (cont == 0)
{
if (trie->Count() > 0)
// This literal is a proper prefix of an earlier literal
return false;
trie->SetAccepting();
return true;
}
return cont->BuildCharTrie(compiler, trie, 0, isAcceptFirst);
}
#if ENABLE_REGEX_CONFIG_OPTIONS
void SimpleNode::Print(DebugWriter* w, const Char* litbuf) const
{
switch (tag)
{
case Empty:
w->Print(_u("Empty")); break;
case BOL:
w->Print(_u("BOL")); break;
case EOL:
w->Print(_u("EOL")); break;
default:
Assert(false);
}
w->PrintEOL(_u("()"));
PrintAnnotations(w);
}
#endif
// ----------------------------------------------------------------------
// WordBoundaryNode
// ----------------------------------------------------------------------
CharCount WordBoundaryNode::LiteralLength() const
{
return 0;
}
bool WordBoundaryNode::IsCharOrPositiveSet() const
{
return false;
}
CharCount WordBoundaryNode::TransferPass0(Compiler& compiler, const Char* litbuf)
{
return 0;
}
void WordBoundaryNode::TransferPass1(Compiler& compiler, const Char* litbuf)
{
// WordChars and NonWordChars sets are already case invariant
}
bool WordBoundaryNode::IsRefiningAssertion(Compiler& compiler)
{
return mustIncludeEntering != mustIncludeLeaving;
}
void WordBoundaryNode::AnnotatePass0(Compiler& compiler)
{
isWord = false;
}
void WordBoundaryNode::AnnotatePass1(Compiler& compiler, bool parentNotInLoop, bool parentAtLeastOnce, bool parentNotSpeculative, bool parentNotNegated)
{
features = HasWordBoundary;
thisConsumes.Exact(0);
isFirstExact = false;
isThisIrrefutable = false;
isThisWillNotProgress = true;
isThisWillNotRegress = true;
isNotInLoop = parentNotInLoop;
isAtLeastOnce = parentAtLeastOnce;
isNotSpeculative = parentNotSpeculative;
isNotNegated = parentNotNegated;
if (isNegation)
firstSet = compiler.standardChars->GetFullSet();
else
{
if (mustIncludeEntering && !mustIncludeLeaving)
firstSet = compiler.standardChars->GetWordSet();
else if (mustIncludeLeaving && !mustIncludeEntering)
firstSet = compiler.standardChars->GetNonWordSet();
else
firstSet = compiler.standardChars->GetFullSet();
}
}
void WordBoundaryNode::AnnotatePass2(Compiler& compiler, CountDomain accumConsumes, bool accumPrevWillNotProgress, bool accumPrevWillNotRegress)
{
prevConsumes = accumConsumes;
isPrevWillNotProgress = accumPrevWillNotProgress;
isPrevWillNotRegress = accumPrevWillNotRegress;
}
void WordBoundaryNode::AnnotatePass3(Compiler& compiler, CountDomain accumConsumes, CharSet<Char>* accumFollow, bool accumFollowIrrefutable, bool accumFollowEOL)
{
followConsumes = accumConsumes;
followSet = accumFollow;
isFollowIrrefutable = accumFollowIrrefutable;
isFollowEOL = accumFollowEOL;
}
void WordBoundaryNode::AnnotatePass4(Compiler& compiler)
{
isDeterministic = true;
}
bool WordBoundaryNode::SupportsPrefixSkipping(Compiler& compiler) const
{
return false;
}
Node* WordBoundaryNode::HeadSyncronizingNode(Compiler& compiler)
{
return 0;
}
CharCount WordBoundaryNode::MinSyncronizingLiteralLength(Compiler& compiler, int& numLiterals) const
{
return 0;
}
void WordBoundaryNode::CollectSyncronizingLiterals(Compiler& compiler, ScannersMixin& scanners) const
{
Assert(false);
}
void WordBoundaryNode::BestSyncronizingNode(Compiler& compiler, Node*& bestNode)
{
}
void WordBoundaryNode::AccumDefineGroups(Js::ScriptContext* scriptContext, int& minGroup, int& maxGroup)
{
}
void WordBoundaryNode::Emit(Compiler& compiler, CharCount& skipped)
{
Assert(skipped == 0);
//
// Compilation scheme:
//
// WordBoundaryTest
//
if (isNegation)
{
EMIT(compiler, WordBoundaryTestInst<true>);
}
else
{
EMIT(compiler, WordBoundaryTestInst<false>);
}
}
CharCount WordBoundaryNode::EmitScan(Compiler& compiler, bool isHeadSyncronizingNode)
{
Assert(false);
return 0;
}
bool WordBoundaryNode::IsOctoquad(Compiler& compiler, OctoquadIdentifier* oi)
{
return false;
}
bool WordBoundaryNode::IsCharTrieArm(Compiler& compiler, uint& accNumAlts) const
{
return false;
}
bool WordBoundaryNode::BuildCharTrie(Compiler& compiler, CharTrie* trie, Node* cont, bool isAcceptFirst) const
{
Assert(false);
return false;
}
#if ENABLE_REGEX_CONFIG_OPTIONS
void WordBoundaryNode::Print(DebugWriter* w, const Char* litbuf) const
{
w->PrintEOL(_u("WordBoundary(%s, %s, %s)"), isNegation ? _u("negative") : _u("positive"), mustIncludeEntering ? _u("entering") : _u("-"), mustIncludeLeaving ? _u("leaving") : _u("-"));
PrintAnnotations(w);
}
#endif
// ----------------------------------------------------------------------
// MatchLiteralNode
// ----------------------------------------------------------------------
CharCount MatchLiteralNode::LiteralLength() const
{
return length;
}
void MatchLiteralNode::AppendLiteral(CharCount& litbufNext, CharCount litbufLen, __inout_ecount(litbufLen) Char* litbuf) const
{
// Called during parsing only, so literal always in original form
Assert(!isEquivClass);
Assert(litbufNext + length <= litbufLen && offset + length <= litbufLen);
#pragma prefast(suppress:26000, "The error said that offset + length >= litbufLen + 1, which is incorrect due to if statement below.")
if (litbufNext + length <= litbufLen && offset + length <= litbufLen) // for prefast
{
js_wmemcpy_s(litbuf + litbufNext, litbufLen - litbufNext, litbuf + offset, length);
}
litbufNext += length;
}
bool MatchLiteralNode::IsCharOrPositiveSet() const
{
return false;
}
CharCount MatchLiteralNode::TransferPass0(Compiler& compiler, const Char* litbuf)
{
Assert(length > 1);
if ((compiler.program->flags & IgnoreCaseRegexFlag) != 0
&& !compiler.standardChars->IsTrivialString(compiler.program->GetCaseMappingSource(), litbuf + offset, length))
{
// We'll need to expand each character of literal into its equivalence class
isEquivClass = true;
return UInt32Math::MulAdd<CaseInsensitive::EquivClassSize,0>(length);
}
else
return length;
}
void MatchLiteralNode::TransferPass1(Compiler& compiler, const Char* litbuf)
{
CharCount nextLit = compiler.program->rep.insts.litbufLen;
if (isEquivClass)
{
Assert((compiler.program->flags & IgnoreCaseRegexFlag) != 0);
// Expand literal according to character equivalence classes
for (CharCount i = 0; i < length; i++)
{
compiler.standardChars->ToEquivs(
compiler.program->GetCaseMappingSource(),
litbuf[offset + i],
compiler.program->rep.insts.litbuf + nextLit + i * CaseInsensitive::EquivClassSize);
}
compiler.program->rep.insts.litbufLen += length * CaseInsensitive::EquivClassSize;
}
else
{
for (CharCount i = 0; i < length; i++)
compiler.program->rep.insts.litbuf[nextLit + i] = litbuf[offset + i];
compiler.program->rep.insts.litbufLen += length;
}
offset = nextLit;
}
void MatchLiteralNode::AnnotatePass0(Compiler& compiler)
{
const Char* litbuf = compiler.program->rep.insts.litbuf;
for (CharCount i = offset; i < offset + length; i++)
{
if (!compiler.standardChars->IsWord(litbuf[i]))
{
isWord = false;
return;
}
}
isWord = true;
}
bool MatchLiteralNode::IsRefiningAssertion(Compiler& compiler)
{
return false;
}
void MatchLiteralNode::AnnotatePass1(Compiler& compiler, bool parentNotInLoop, bool parentAtLeastOnce, bool parentNotSpeculative, bool parentNotNegated)
{
features = HasMatchLiteral;
thisConsumes.Exact(length);
firstSet = Anew(compiler.ctAllocator, UnicodeCharSet);
for (int i = 0; i < (isEquivClass ? CaseInsensitive::EquivClassSize : 1); i++)
firstSet->Set(compiler.ctAllocator, compiler.program->rep.insts.litbuf[offset + i]);
isFirstExact = true;
isThisIrrefutable = false;
isThisWillNotProgress = true;
isThisWillNotRegress = true;
isNotInLoop = parentNotInLoop;
isAtLeastOnce = parentAtLeastOnce;
isNotSpeculative = parentNotSpeculative;
isNotNegated = parentNotNegated;
}
void MatchLiteralNode::AnnotatePass2(Compiler& compiler, CountDomain accumConsumes, bool accumPrevWillNotProgress, bool accumPrevWillNotRegress)
{
prevConsumes = accumConsumes;
isPrevWillNotProgress = accumPrevWillNotProgress;
isPrevWillNotRegress = accumPrevWillNotRegress;
}
void MatchLiteralNode::AnnotatePass3(Compiler& compiler, CountDomain accumConsumes, CharSet<Char>* accumFollow, bool accumFollowIrrefutable, bool accumFollowEOL)
{
followConsumes = accumConsumes;
followSet = accumFollow;
isFollowIrrefutable = accumFollowIrrefutable;
isFollowEOL = accumFollowEOL;
}
void MatchLiteralNode::AnnotatePass4(Compiler& compiler)
{
isDeterministic = true;
}
bool MatchLiteralNode::SupportsPrefixSkipping(Compiler& compiler) const
{
return true;
}
Node* MatchLiteralNode::HeadSyncronizingNode(Compiler& compiler)
{
return this;
}
CharCount MatchLiteralNode::MinSyncronizingLiteralLength(Compiler& compiler, int& numLiterals) const
{
numLiterals++;
return length;
}
void MatchLiteralNode::CollectSyncronizingLiterals(Compiler& compiler, ScannersMixin& scanners) const
{
ScannerMixin* scanner =
scanners.Add(compiler.GetScriptContext()->GetRecycler(), compiler.GetProgram(), offset, length, isEquivClass);
scanner->scanner.Setup(compiler.rtAllocator, compiler.program->rep.insts.litbuf + offset, length, isEquivClass ? CaseInsensitive::EquivClassSize : 1);
}
void MatchLiteralNode::BestSyncronizingNode(Compiler& compiler, Node*& bestNode)
{
if (IsBetterSyncronizingNode(compiler, bestNode, this))
bestNode = this;
}
void MatchLiteralNode::AccumDefineGroups(Js::ScriptContext* scriptContext, int& minGroup, int& maxGroup)
{
}
void MatchLiteralNode::Emit(Compiler& compiler, CharCount& skipped)
{
if (skipped >= length)
{
// Asking to skip entire literal
skipped -= length;
return;
}
//
// Compilation scheme:
//
// Match(Char|Char4|Literal|LiteralEquiv)Inst
//
CharCount effectiveOffset = offset + skipped * (isEquivClass ? CaseInsensitive::EquivClassSize : 1);
CharCount effectiveLength = length - skipped;
skipped -= min(skipped, length);
if (effectiveLength == 1)
{
Char* cs = compiler.program->rep.insts.litbuf + effectiveOffset;
MatchCharNode::Emit(compiler, cs, isEquivClass);
}
else
{
if (isEquivClass)
EMIT(compiler, MatchLiteralEquivInst, effectiveOffset, effectiveLength);
else
EMIT(compiler, MatchLiteralInst, effectiveOffset, effectiveLength);
}
}
CompileAssert(CaseInsensitive::EquivClassSize == 4);
CharCount MatchLiteralNode::EmitScan(Compiler& compiler, bool isHeadSyncronizingNode)
{
//
// Compilation scheme:
//
// SyncTo(Literal|LiteralEquiv|LinearLiteral)And(Continue|Consume|Backup)
//
Char * litptr = compiler.program->rep.insts.litbuf + offset;
if (isHeadSyncronizingNode)
{
// For a head literal there's no need to back up after finding the literal, so use a faster instruction
Assert(prevConsumes.IsExact(0)); // there should not be any consumes before this node
if (isEquivClass)
{
const uint lastPatCharIndex = length - 1;
if (litptr[lastPatCharIndex * CaseInsensitive::EquivClassSize] == litptr[lastPatCharIndex * CaseInsensitive::EquivClassSize + 1]
&& litptr[lastPatCharIndex * CaseInsensitive::EquivClassSize] == litptr[lastPatCharIndex * CaseInsensitive::EquivClassSize + 2]
&& litptr[lastPatCharIndex * CaseInsensitive::EquivClassSize] == litptr[lastPatCharIndex * CaseInsensitive::EquivClassSize + 3])
{
EMIT(compiler, SyncToLiteralEquivTrivialLastPatCharAndConsumeInst, offset, length)->scanner.Setup(compiler.rtAllocator, litptr, length, CaseInsensitive::EquivClassSize);