-
Notifications
You must be signed in to change notification settings - Fork 424
/
dfa.rs
1407 lines (1327 loc) · 53.8 KB
/
dfa.rs
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 2014-2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/*!
The DFA matching engine.
A DFA provides faster matching because the engine is in exactly one state at
any point in time. In the NFA, there may be multiple active states, and
considerable CPU cycles are spent shuffling them around. In finite automata
speak, the DFA follows epsilon transitions in the regex far less than the NFA.
A DFA is a classic trade off between time and space. The NFA is slower, but
its memory requirements are typically small and predictable. The DFA is faster,
but given the right regex and the right input, the number of states in the
DFA can grow exponentially. To mitigate this space problem, we do two things:
1. We implement an *online* DFA. That is, the DFA is constructed from the NFA
during a search. When a new state is computed, it is stored in a cache so
that it may be reused. An important consequence of this implementation
is that states that are never reached for a particular input are never
computed. (This is impossible in an "offline" DFA which needs to compute
all possible states up front.)
2. If the cache gets too big, we wipe it and continue matching.
In pathological cases, a new state can be created for every byte of input.
(e.g., The regex `(a|b)*a(a|b){20}` on a long sequence of a's and b's.)
In this case, performance regresses to slightly slower than the full NFA
simulation, in large part because the cache becomes useless. (We could
investigate quitting the DFA and falling back to the NFA.)
N.B. While this implementation is heavily commented, Russ Cox's series of
articles on regexes is strongly recommended: https://swtch.com/~rsc/regexp/
(As is the DFA implementation in RE2, which heavily influenced this
implementation.)
*/
use std::collections::HashMap;
use std::fmt;
use std::mem;
use exec::ProgramCache;
use prog::{Inst, Program};
use sparse::SparseSet;
/// The cache limit specifies approximately how much space we're willing to
/// give to the state cache. Once the state cache exceeds the size, it is wiped
/// and all states must be re-computed.
///
/// Note that this value does not impact correctness. It can be set to 0 and
/// the DFA will run just fine. (It will only ever store exactly one state
/// in the cache, and will likely run very slowly, but it will work.)
///
/// Also note that this limit is *per thread of execution*. That is, if the
/// same regex is used to search text across multiple threads simultaneously,
/// then the DFA cache is not shared. Instead, copies are made.
///
/// TODO(burntsushi): This feels like a knob that a caller ought to be able to
/// configure.
const CACHE_LIMIT: usize = 2 * (1<<20);
/// Return true if and only if the given program can be executed by a DFA.
///
/// Generally, a DFA is possible only when there are no word boundary
/// assertions. This is due to the difficulty (but likely not impossibility)
/// of tracking multi-byte assertions in the DFA.
pub fn can_exec(insts: &Program) -> bool {
use prog::Inst::*;
use prog::EmptyLook::*;
// If for some reason we manage to allocate a regex program with more
// than 2^32-1 instructions, then we can't execute the DFA because we
// use 32 bit pointers.
if insts.len() > ::std::u32::MAX as usize {
return false;
}
for inst in insts {
match *inst {
Char(_) | Ranges(_) => return false,
EmptyLook(ref inst) => {
match inst.look {
WordBoundary | NotWordBoundary => return false,
WordBoundaryAscii | NotWordBoundaryAscii => {}
StartLine | EndLine | StartText | EndText => {}
}
}
Match(_) | Save(_) | Split(_) | Bytes(_) => {}
}
}
true
}
/// A reusable cache of DFA states.
///
/// This cache is reused between multiple invocations of the same regex
/// program. (It is not shared simultaneously between threads. If there is
/// contention, then new caches are created.)
#[derive(Clone, Debug)]
pub struct Cache {
/// Group persistent DFA related cache state together. The sparse sets
/// listed below are used as scratch space while computing uncached states.
inner: CacheInner,
/// qcur and qnext are ordered sets with constant time
/// addition/membership/clearing-whole-set and linear time iteration. They
/// are used to manage the sets of NFA states in DFA states when computing
/// cached DFA states. In particular, the order of the NFA states matters
/// for leftmost-first style matching. Namely, when computing a cached
/// state, the set of NFA states stops growing as soon as the first Match
/// instruction is observed.
qcur: SparseSet,
qnext: SparseSet,
}
#[derive(Clone, Debug)]
/// CacheInner is logically just a part of Cache, but groups together fields
/// that aren't passed as function parameters throughout search. (This split
/// is mostly an artifact of the borrow checker. It is happily paid.)
struct CacheInner {
/// A cache of pre-compiled DFA states, keyed by the set of NFA states
/// and the set of empty-width flags set at the byte in the input when the
/// state was observed.
///
/// A StatePtr is effectively a `*State`, but to avoid reference counting,
/// we just pass indexes around manually. The performance impact of this
/// isn't particularly clear, but this does enable us to use 32 bit
/// indexes, which can significantly impact the number of states we can
/// cram into our memory bounds.
compiled: HashMap<StateKey, StatePtr>,
/// Our heap of states. Both `CacheInner.compiled` and `State.next` point
/// into this vec.
states: Vec<State>,
/// A set of cached start states, which are limited to the number of
/// permutations of flags set just before the initial byte of input. (The
/// index into this vec is a `EmptyFlags`.)
///
/// N.B. A start state can be "dead" (i.e., no possible match), so we
/// represent it with a StatePtr.
start_states: Vec<StatePtr>,
/// Stack scratch space used to follow epsilon transitions in the NFA.
/// (This permits us to avoid recursion.)
///
/// The maximum stack size is the number of NFA states.
stack: Vec<InstPtr>,
/// The total number of times this cache has been flushed by the DFA
/// because of space constraints.
flush_count: u64,
}
/// Fsm encapsulates the actual execution of the DFA.
///
/// Most of the fields in this struct are mutable pointers to locations in the
/// DFA cache.
///
/// Why don't we just store the Cache directly in this struct? Well, it
/// turns out that we often want to "iterate over {qcur,qnext} and perform
/// some possibly mutable operation." The borrow checker forbids this, even
/// if the callee doesn't mutate {qcur,qnext}. Therefore, we stick most of
/// Cache into Fsm directly as pointers and pass {qcur,qnext} around as
/// parameters.
///
/// N.B. We only use a single lifetime here since all pointers are taken
/// from the same cache.
#[derive(Debug)]
pub struct Fsm<'a> {
/// prog contains the NFA instruction opcodes. DFA execution uses either
/// the `dfa` instructions or the `dfa_reverse` instructions from
/// `exec::Executor`. (It never uses `Executor.prog`, which may have
/// Unicode opcodes that cannot be executed by this DFA.)
prog: &'a Program,
/// The start state. We record it here because the pointer may change
/// when the cache is wiped.
start: StatePtr,
/// The current position in the input.
at: usize,
/// Should we quit after seeing the first match?
quit_after_match: bool,
/// The last state that matched.
///
/// When no match has occurred, this is set to STATE_UNKNOWN.
last_match_si: StatePtr,
/// The input position of the last cache flush. We use this to determine
/// if we're thrashing in the cache too often. If so, the DFA quits so
/// that we can fall back to the NFA algorithm.
last_cache_flush: usize,
/// All cached DFA information that is persisted between searches.
cache: &'a mut CacheInner,
}
/// The result of running the DFA.
///
/// Generally, the result is either a match or not a match, but sometimes the
/// DFA runs too slow because the cache size is too small. In that case, it
/// gives up with the intent of falling back to the NFA algorithm.
#[derive(Clone, Debug)]
pub enum Result<T> {
Match(T),
NoMatch,
Quit,
}
impl<T> Result<T> {
pub fn is_match(&self) -> bool {
match *self {
Result::Match(_) => true,
Result::NoMatch | Result::Quit => false,
}
}
}
/// State is a DFA state. It contains transitions to next states (given an
/// input byte), an ordered set of NFA states (not necessarily complete) and
/// the set of empty-width flags set in the input when this state was created.
/// (It also contains the set of empty-width flags in all of its constituent
/// NFA states, which is useful for determining where and when to try and
/// follow epsilon transitions.)
#[derive(Clone)]
struct State {
/// The set of transitions out of this state to other states.
///
/// This is tricky and is NOT a simple vec with length 256. A vec with
/// length 256 would work fine if not for the following:
///
/// 1. Empty assertions can lead to matches "at the boundary" of the input,
/// so it is useful to have one extra transition that corresponds to
/// EOF. So why doesn't a vec with length 257 work?
/// 2. If the vec has length 257 and each StatePtr is 4 bytes (even on 64
/// bit), then every state occupies at least 1KB on the heap. That's
/// ridiculous. As an optimization, we compute the set of all
/// equivalence classes of bytes in the regex. Each equivalence class
/// is defined to be the set of bytes that are indistinguishable when
/// searching for a match. For example, in the regex `[a-z]`, the byte
/// ranges `0..ord(a)-1`, `ord(a)-ord(z)` and `ord(z)+1..257` all
/// correspond to distinct classes. Therefore, we only need a vec of
/// length *3* for that particular regex, which is quite a bit better.
/// (Equivalence classes are computed during compilation.)
next: Box<[StatePtr]>,
/// The set of NFA states in this DFA state, which are computed by
/// following epsilon transitions from `insts[0]`. Note that not all
/// epsilon transitions are necessarily followed! Namely, epsilon
/// transitions that correspond to empty assertions are only followed if
/// the flags set at the current byte satisfy the assertion.
insts: Box<[InstPtr]>,
/// Flags for this state, which indicate whether it is a match state,
/// observed an ASCII word byte and whether any instruction in `insts`
/// is a zero-width assertion.
flags: StateFlags,
}
/// A state's key for identifying it in the cache. In particular, if two
/// state's cache keys are equivalent, then they cannot be discriminatory in
/// a match.
///
/// We capture two bits of information: an ordered set of NFA states for the
/// DFA state and whether it corresponds to a match state.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
struct StateKey {
/// An ordered set of NFA states.
insts: Box<[InstPtr]>,
/// The state's flags.
flags: StateFlags,
}
/// InstPtr is a 32 bit pointer into a sequence of opcodes (i.e., it indexes
/// an NFA state).
///
/// Throughout this library, this is usually set to `usize`, but we force a
/// `u32` here to save on space.
type InstPtr = u32;
/// StatePtr is a 32 bit pointer into a sequence of states.
///
/// It has two special values: STATE_UNKNOWN and STATE_DEAD. All other values
/// are valid indexes into a cache's state sequence.
///
/// (We use sentinel values here to save on space. A more sensible
/// representation is a sum type.)
type StatePtr = u32;
/// An unknown state means that the state has not been computed yet, and that
/// the only way to progress is to compute it.
const STATE_UNKNOWN: StatePtr = 0;
/// A dead state means that the state has been computed and it is known that
/// once it is entered, no match can ever occur.
const STATE_DEAD: StatePtr = 1;
/// Byte is a u8 in spirit, but a u16 in practice so that we can represent the
/// special EOF sentinel value.
#[derive(Copy, Clone, Debug)]
struct Byte(u16);
/// A set of flags for zero-width assertions.
#[derive(Clone, Copy, Eq, Debug, Default, Hash, PartialEq)]
struct EmptyFlags {
start: bool,
end: bool,
start_line: bool,
end_line: bool,
word_boundary: bool,
not_word_boundary: bool,
}
/// A set of flags describing various configurations of a DFA state. This is
/// represented by a `u8` so that it is compact.
#[derive(Clone, Copy, Eq, Default, Hash, PartialEq)]
struct StateFlags(u8);
impl Cache {
/// Create new empty cache for the DFA engine.
pub fn new(prog: &Program) -> Self {
Cache {
inner: CacheInner {
compiled: HashMap::new(),
states: vec![State::invalid(), State::invalid()],
start_states: vec![STATE_UNKNOWN; 256],
stack: vec![],
flush_count: 0,
},
qcur: SparseSet::new(prog.insts.len()),
qnext: SparseSet::new(prog.insts.len()),
}
}
}
impl<'a> Fsm<'a> {
#[inline(always)] // reduces constant overhead
pub fn forward(
prog: &'a Program,
cache: &ProgramCache,
quit_after_match: bool,
text: &[u8],
at: usize,
) -> Result<usize> {
let mut cache = cache.borrow_mut();
let mut cache = &mut cache.dfa;
let mut dfa = Fsm {
prog: prog,
start: 0, // filled in below
at: at,
quit_after_match: quit_after_match,
last_match_si: STATE_UNKNOWN,
last_cache_flush: at,
cache: &mut cache.inner,
};
let (empty_flags, state_flags) = dfa.start_flags(text, at);
dfa.start = match dfa.start_state(
&mut cache.qcur,
empty_flags,
state_flags,
) {
None => return Result::Quit,
Some(STATE_DEAD) => return Result::NoMatch,
Some(si) => si,
};
debug_assert!(dfa.start != STATE_UNKNOWN);
dfa.exec_at(&mut cache.qcur, &mut cache.qnext, text)
}
#[inline(always)] // reduces constant overhead
pub fn reverse(
prog: &'a Program,
cache: &ProgramCache,
quit_after_match: bool,
text: &[u8],
at: usize,
) -> Result<usize> {
let mut cache = cache.borrow_mut();
let mut cache = &mut cache.dfa_reverse;
let mut dfa = Fsm {
prog: prog,
start: 0, // filled in below
at: at,
quit_after_match: quit_after_match,
last_match_si: STATE_UNKNOWN,
last_cache_flush: at,
cache: &mut cache.inner,
};
let (empty_flags, state_flags) = dfa.start_flags_reverse(text, at);
dfa.start = match dfa.start_state(
&mut cache.qcur,
empty_flags,
state_flags,
) {
None => return Result::Quit,
Some(STATE_DEAD) => return Result::NoMatch,
Some(si) => si,
};
debug_assert!(dfa.start != STATE_UNKNOWN);
dfa.exec_at_reverse(&mut cache.qcur, &mut cache.qnext, text)
}
#[inline(always)] // reduces constant overhead
pub fn forward_many(
prog: &'a Program,
cache: &ProgramCache,
matches: &mut [bool],
text: &[u8],
at: usize,
) -> Result<usize> {
debug_assert!(matches.len() == prog.matches.len());
let mut cache = cache.borrow_mut();
let mut cache = &mut cache.dfa;
let mut dfa = Fsm {
prog: prog,
start: 0, // filled in below
at: at,
quit_after_match: false,
last_match_si: STATE_UNKNOWN,
last_cache_flush: at,
cache: &mut cache.inner,
};
let (empty_flags, state_flags) = dfa.start_flags(text, at);
dfa.start = match dfa.start_state(
&mut cache.qcur,
empty_flags,
state_flags,
) {
None => return Result::Quit,
Some(STATE_DEAD) => return Result::NoMatch,
Some(si) => si,
};
debug_assert!(dfa.start != STATE_UNKNOWN);
let result = dfa.exec_at(&mut cache.qcur, &mut cache.qnext, text);
if result.is_match() {
if matches.len() == 1 {
matches[0] = true;
} else {
debug_assert!(dfa.last_match_si != STATE_UNKNOWN);
debug_assert!(dfa.last_match_si != STATE_DEAD);
for &ip in dfa.cache.state(dfa.last_match_si).insts.iter() {
if let Inst::Match(slot) = dfa.prog[ip as usize] {
matches[slot] = true;
}
}
}
}
result
}
/// Executes the DFA on a forward NFA.
///
/// {qcur,qnext} are scratch ordered sets which may be non-empty.
#[inline(always)] // reduces constant overhead
fn exec_at(
&mut self,
qcur: &mut SparseSet,
qnext: &mut SparseSet,
text: &[u8],
) -> Result<usize> {
// For the most part, the DFA is basically:
//
// last_match = null
// while current_byte != EOF:
// si = current_state.next[current_byte]
// if si is match
// last_match = si
// return last_match
//
// However, we need to deal with a few things:
//
// 1. This is an *online* DFA, so the current state's next list
// may not point to anywhere yet, so we must go out and compute
// them. (They are then cached into the current state's next list
// to avoid re-computation.)
// 2. If we come across a state that is known to be dead (i.e., never
// leads to a match), then we can quit early.
// 3. If the caller just wants to know if a match occurs, then we
// can quit as soon as we know we have a match. (Full leftmost
// first semantics require continuing on.)
// 4. If we're in the start state, then we can use a pre-computed set
// of prefix literals to skip quickly along the input.
// 5. After the input is exhausted, we run the DFA on one symbol
// that stands for EOF. This is useful for handling empty width
// assertions.
// 6. We can't actually do state.next[byte]. Instead, we have to do
// state.next[byte_classes[byte]], which permits us to keep the
// 'next' list very small.
debug_assert!(!self.prog.is_reverse);
// The last match is the currently known ending match position. It is
// reported as an index to the most recent byte that resulted in a
// transition to a match state and is always stored in capture slot `1`
// when searching forwards. Its maximum value is `text.len()`,
// which can only happen after the special EOF sentinel value is fed
// to the DFA.
let mut result = Result::NoMatch;
let mut si = self.start;
let mut next_si;
let mut b: u8;
while self.at < text.len() {
// Our set of literal prefixes can itself be a DFA, but it is
// offline and can generally be quite a bit faster. (For instance,
// memchr is used if possible.)
if !self.prog.prefixes.is_empty()
&& si == self.start
&& !self.prog.is_anchored_start {
self.at = match self.prefix_at(text, self.at) {
None => return Result::NoMatch,
Some(i) => i,
};
}
b = text[self.at];
next_si = {
let s = self.cache.state(si);
if s.flags.is_match() {
result = Result::Match(self.at - 1);
if self.quit_after_match {
return result;
}
self.last_match_si = si;
}
s.next[self.u8_class(b)]
};
if next_si <= STATE_DEAD {
// The next state may not have been cached, so re-compute it
// (i.e., follow epsilon transitions).
let byte = Byte::byte(b);
next_si = match self.next_state(qcur, qnext, si, byte) {
None => return Result::Quit,
Some(STATE_DEAD) => return result,
Some(si) => si,
};
debug_assert!(next_si != STATE_UNKNOWN);
}
si = next_si;
self.at += 1;
}
if self.cache.state(si).flags.is_match() {
result = Result::Match(self.at - 1);
if self.quit_after_match {
return result;
}
self.last_match_si = si;
}
// Run the DFA once more on the special EOF senitnel value.
si = match self.next_state(qcur, qnext, si, Byte::eof()) {
None => return Result::Quit,
Some(STATE_DEAD) => return result,
Some(si) => si,
};
debug_assert!(si != STATE_UNKNOWN);
if self.cache.state(si).flags.is_match() {
self.last_match_si = si;
result = Result::Match(text.len());
}
result
}
/// Executes the DFA on a reverse NFA.
#[inline(always)] // reduces constant overhead
fn exec_at_reverse(
&mut self,
qcur: &mut SparseSet,
qnext: &mut SparseSet,
text: &[u8],
) -> Result<usize> {
// The comments in `exec_at` above mostly apply here too. The main
// difference is that we move backwards over the input and we look for
// the longest possible match instead of the leftmost-first match.
//
// N.B. The code duplication here is regrettable. Efforts to improve
// it without sacrificing performance are welcome. ---AG
debug_assert!(self.prog.is_reverse);
let mut result = Result::NoMatch;
let mut si = self.start;
let mut next_si;
let mut b: u8;
while self.at > 0 {
self.at -= 1;
b = text[self.at];
next_si = {
let s = self.cache.state(si);
if s.flags.is_match() {
result = Result::Match(self.at + 2);
if self.quit_after_match {
return result;
}
self.last_match_si = si;
}
s.next[self.u8_class(b)]
};
if next_si <= STATE_DEAD {
// The next state may not have been cached, so re-compute it
// (i.e., follow epsilon transitions).
let byte = Byte::byte(b);
next_si = match self.next_state(qcur, qnext, si, byte) {
None => return Result::Quit,
Some(STATE_DEAD) => return result,
Some(si) => si,
};
debug_assert!(next_si != STATE_UNKNOWN);
}
si = next_si;
}
if self.cache.state(si).flags.is_match() {
result = Result::Match(self.at + 1);
if self.quit_after_match {
return result;
}
self.last_match_si = si;
}
// Run the DFA once more on the special EOF senitnel value.
si = match self.next_state(qcur, qnext, si, Byte::eof()) {
None => return Result::Quit,
Some(STATE_DEAD) => return result,
Some(si) => si,
};
debug_assert!(si != STATE_UNKNOWN);
if self.cache.state(si).flags.is_match() {
self.last_match_si = si;
result = Result::Match(0);
}
result
}
/// Computes the next state given the current state and the current input
/// byte (which may be EOF).
///
/// If STATE_DEAD is returned, then there is no valid state transition.
/// This implies that no permutation of future input can lead to a match
/// state.
///
/// STATE_UNKNOWN can never be returned.
fn exec_byte(
&mut self,
qcur: &mut SparseSet,
qnext: &mut SparseSet,
mut si: StatePtr,
b: Byte,
) -> Option<StatePtr> {
use prog::Inst::*;
// Initialize a queue with the current DFA state's NFA states.
qcur.clear();
for &ip in self.cache.state(si).insts.iter() {
qcur.add(ip as usize);
}
// Before inspecting the current byte, we may need to also inspect
// whether the position immediately preceding the current byte
// satisfies the empty assertions found in the current state.
//
// We only need to do this step if there are any empty assertions in
// the current state.
let is_word_last = self.cache.state(si).flags.is_word();
let is_word = b.is_ascii_word();
if self.cache.state(si).flags.has_empty() {
// Compute the flags immediately preceding the current byte.
// This means we only care about the "end" or "end line" flags.
// (The "start" flags are computed immediately proceding the
// current byte and is handled below.)
let mut flags = EmptyFlags::default();
if b.is_eof() {
flags.end = true;
flags.end_line = true;
} else if b.as_byte().map_or(false, |b| b == b'\n') {
flags.end_line = true;
}
if is_word_last == is_word {
flags.not_word_boundary = true;
} else {
flags.word_boundary = true;
}
// Now follow epsilon transitions from every NFA state, but make
// sure we only follow transitions that satisfy our flags.
qnext.clear();
for &ip in &*qcur {
self.follow_epsilons(usize_to_u32(ip), qnext, flags);
}
mem::swap(qcur, qnext);
}
// Now we set flags for immediately after the current byte. Since start
// states are processed separately, and are the only states that can
// have the StartText flag set, we therefore only need to worry about
// the StartLine flag here.
//
// We do also keep track of whether this DFA state contains a NFA state
// that is a matching state. This is precisely how we delay the DFA
// matching by one byte in order to process the special EOF sentinel
// byte. Namely, if this DFA state containing a matching NFA state,
// then it is the *next* DFA state that is marked as a match.
let mut empty_flags = EmptyFlags::default();
let mut state_flags = StateFlags::default();
empty_flags.start_line = b.as_byte().map_or(false, |b| b == b'\n');
if b.is_ascii_word() {
state_flags.set_word();
}
// Now follow all epsilon transitions again, but only after consuming
// the current byte.
qnext.clear();
for &ip in &*qcur {
match self.prog[ip as usize] {
// These states never happen in a byte-based program.
Char(_) | Ranges(_) => unreachable!(),
// These states are handled when following epsilon transitions.
Save(_) | Split(_) | EmptyLook(_) => {}
Match(_) => {
state_flags.set_match();
if !self.continue_past_first_match() {
break;
} else if self.prog.matches.len() > 1
&& !qnext.contains_ip(ip as usize) {
// If we are continuing on to find other matches,
// then keep a record of the match states we've seen.
qnext.add(ip);
}
}
Bytes(ref inst) => {
if b.as_byte().map_or(false, |b| inst.matches(b)) {
self.follow_epsilons(
inst.goto as InstPtr, qnext, empty_flags);
}
}
}
}
let mut cache = true;
if b.is_eof() && self.prog.matches.len() > 1 {
// If we're processing the last byte of the input and we're
// matching a regex set, then make the next state contain the
// previous states transitions. We do this so that the main
// matching loop can extract all of the match instructions.
mem::swap(qcur, qnext);
// And don't cache this state because it's totally bunk.
cache = false;
}
// We've now built up the set of NFA states that ought to comprise the
// next DFA state, so try to find it in the cache, and if it doesn't
// exist, cache it.
//
// N.B. We pass `&mut si` here because the cache may clear itself if
// it has gotten too full. When that happens, the location of the
// current state may change.
let next = match self.cached_state(qnext, state_flags, Some(&mut si)) {
None => return None,
Some(next) => next,
};
debug_assert!(next != STATE_UNKNOWN);
// And now store our state in the current state's next list.
let cls = self.byte_class(b);
if cache {
self.cache.state_mut(si).next[cls] = next;
}
Some(next)
}
/// Follows the epsilon transitions starting at (and including) `ip`. The
/// resulting states are inserted into the ordered set `q`.
///
/// Conditional epsilon transitions (i.e., empty width assertions) are only
/// followed if they are satisfied by the given flags, which should
/// represent the flags set at the current location in the input.
///
/// If the current location corresponds to the empty string, then only the
/// end line and/or end text flags may be set. If the current location
/// corresponds to a real byte in the input, then only the start line
/// and/or start text flags may be set.
///
/// As an exception to the above, when finding the initial state, any of
/// the above flags may be set:
///
/// If matching starts at the beginning of the input, then start text and
/// start line should be set. If the input is empty, then end text and end
/// line should also be set.
///
/// If matching starts after the beginning of the input, then only start
/// line should be set if the preceding byte is `\n`. End line should never
/// be set in this case. (Even if the proceding byte is a `\n`, it will
/// be handled in a subsequent DFA state.)
fn follow_epsilons(
&mut self,
ip: InstPtr,
q: &mut SparseSet,
flags: EmptyFlags,
) {
use prog::Inst::*;
use prog::EmptyLook::*;
// We need to traverse the NFA to follow epsilon transitions, so avoid
// recursion with an explicit stack.
self.cache.stack.push(ip);
while let Some(ip) = self.cache.stack.pop() {
// Don't visit states we've already added.
if q.contains_ip(ip as usize) {
continue;
}
q.add(ip as usize);
match self.prog[ip as usize] {
Char(_) | Ranges(_) => unreachable!(),
Match(_) | Bytes(_) => {}
EmptyLook(ref inst) => {
// Only follow empty assertion states if our flags satisfy
// the assertion.
match inst.look {
StartLine if flags.start_line => {
self.cache.stack.push(inst.goto as InstPtr);
}
EndLine if flags.end_line => {
self.cache.stack.push(inst.goto as InstPtr);
}
StartText if flags.start => {
self.cache.stack.push(inst.goto as InstPtr);
}
EndText if flags.end => {
self.cache.stack.push(inst.goto as InstPtr);
}
WordBoundaryAscii if flags.word_boundary => {
self.cache.stack.push(inst.goto as InstPtr);
}
NotWordBoundaryAscii if flags.not_word_boundary => {
self.cache.stack.push(inst.goto as InstPtr);
}
StartLine | EndLine | StartText | EndText => {}
WordBoundaryAscii | NotWordBoundaryAscii => {}
// The DFA doesn't support Unicode word boundaries. :-(
WordBoundary | NotWordBoundary => unreachable!(),
}
}
Save(ref inst) => self.cache.stack.push(inst.goto as InstPtr),
Split(ref inst) => {
self.cache.stack.push(inst.goto2 as InstPtr);
self.cache.stack.push(inst.goto1 as InstPtr);
}
}
}
}
/// Find a previously computed state matching the given set of instructions
/// and is_match bool.
///
/// The given set of instructions should represent a single state in the
/// NFA along with all states reachable without consuming any input.
///
/// The is_match bool should be true if and only if the preceding DFA state
/// contains an NFA matching state. The cached state produced here will
/// then signify a match. (This enables us to delay a match by one byte,
/// in order to account for the EOF sentinel byte.)
///
/// If the cache is full, then it is wiped before caching a new state.
///
/// The current state should be specified if it exists, since it will need
/// to be preserved if the cache clears itself. (Start states are
/// always saved, so they should not be passed here.) It takes a mutable
/// pointer to the index because if the cache is cleared, the state's
/// location may change.
fn cached_state(
&mut self,
q: &SparseSet,
mut state_flags: StateFlags,
current_state: Option<&mut StatePtr>,
) -> Option<StatePtr> {
// If we couldn't come up with a non-empty key to represent this state,
// then it is dead and can never lead to a match.
//
// Note that inst_flags represent the set of empty width assertions
// in q. We use this as an optimization in exec_byte to determine when
// we should follow epsilon transitions at the empty string preceding
// the current byte.
let key = match self.cached_state_key(q, &mut state_flags) {
None => return Some(STATE_DEAD),
Some(v) => v,
};
// In the cache? Cool. Done.
if let Some(&si) = self.cache.compiled.get(&key) {
return Some(si);
}
// If the cache has gotten too big, wipe it.
if self.approximate_size() > CACHE_LIMIT {
if !self.clear_cache_and_save(current_state) {
// Ooops. DFA is giving up.
return None;
}
}
// OK, now there's enough room to push our new state.
// We do this even if the cache size is set to 0!
let next = vec![STATE_UNKNOWN; self.num_byte_classes()];
self.cache.states.push(State {
next: next.into_boxed_slice(),
insts: key.insts.clone(),
flags: state_flags,
});
let si = usize_to_u32(self.cache.states.len().checked_sub(1).unwrap());
self.cache.compiled.insert(key, si);
Some(si)
}
/// Produces a key suitable for describing a state in the DFA cache.
///
/// The key invariant here is that equivalent keys are produced for any two
/// sets of ordered NFA states (and toggling of whether the previous NFA
/// states contain a match state) that do not discriminate a match for any
/// input.
///
/// Specifically, q should be an ordered set of NFA states and is_match
/// should be true if and only if the previous NFA states contained a match
/// state.
fn cached_state_key(
&mut self,
q: &SparseSet,
state_flags: &mut StateFlags,
) -> Option<StateKey> {
use prog::Inst::*;
use prog::EmptyLook::*;
// We need to build up enough information to recognize pre-built states
// in the DFA. Generally speaking, this includes every instruction
// except for those which are purely epsilon transitions, e.g., the
// Save and Split instructions.
//
// Empty width assertions are also epsilon transitions, but since they
// are conditional, we need to make them part of a state's key in the
// cache.
let mut insts = vec![];
for &ip in q {
let ip = usize_to_u32(ip);
match self.prog[ip as usize] {
Char(_) | Ranges(_) => unreachable!(),
Save(_) => {}
Split(_) => {}
Bytes(_) => insts.push(ip),
EmptyLook(ref inst) => {
match inst.look {
StartLine => {
state_flags.set_empty();
insts.push(ip);
}
EndLine => {
state_flags.set_empty();
insts.push(ip);
}
StartText => {
state_flags.set_empty();
insts.push(ip);
}
EndText => {
state_flags.set_empty();
insts.push(ip);
}
WordBoundaryAscii => {
state_flags.set_empty();
insts.push(ip);
}
NotWordBoundaryAscii => {
state_flags.set_empty();
insts.push(ip);
}
WordBoundary | NotWordBoundary => unreachable!(),
}
}
Match(_) => {
insts.push(ip);
if !self.continue_past_first_match() {
break;
}
}
}
}
// If we couldn't transition to any other instructions and we didn't
// see a match when expanding NFA states previously, then this is a
// dead state and no amount of additional input can transition out
// of this state.
if insts.len() == 0 && !state_flags.is_match() {
None
} else {
Some(StateKey {
insts: insts.into_boxed_slice(),
flags: *state_flags,
})
}
}
/// Clears the cache, but saves and restores current_state if it is not
/// none.
///
/// The current state must be provided here in case its location in the
/// cache changes.
///
/// This returns false if the cache is not cleared and the DFA should
/// give up.
fn clear_cache_and_save(
&mut self,
current_state: Option<&mut StatePtr>,
) -> bool {
if self.cache.states.len() <= 2 {
// Why <= 2? Well, the states list always has its first two
// positions filled by marker states for STATE_UNKNOWN and
// STATE_DEAD. These states aren't actually used, but exist to
// make sure no other state lives in those locations. Therefore,
// a state vec with length <= 2 is actually "empty."