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
use std::fmt;
use classes::ByteClasses;
pub use nfa::compiler::Builder;
mod compiler;
mod map;
mod range_trie;
pub type StateID = usize;
#[derive(Clone)]
pub struct NFA {
anchored: bool,
start: StateID,
states: Vec<State>,
byte_classes: ByteClasses,
}
impl NFA {
pub fn always_match() -> NFA {
NFA {
anchored: false,
start: 0,
states: vec![State::Match],
byte_classes: ByteClasses::empty(),
}
}
pub fn never_match() -> NFA {
NFA {
anchored: false,
start: 0,
states: vec![State::Fail],
byte_classes: ByteClasses::empty(),
}
}
pub fn is_anchored(&self) -> bool {
self.anchored
}
pub fn len(&self) -> usize {
self.states.len()
}
pub fn start(&self) -> StateID {
self.start
}
pub fn state(&self, id: StateID) -> &State {
&self.states[id]
}
pub fn byte_classes(&self) -> &ByteClasses {
&self.byte_classes
}
}
impl fmt::Debug for NFA {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, state) in self.states.iter().enumerate() {
let status = if i == self.start { '>' } else { ' ' };
writeln!(f, "{}{:06}: {:?}", status, i, state)?;
}
Ok(())
}
}
#[derive(Clone, Eq, PartialEq)]
pub enum State {
Range { range: Transition },
Sparse { ranges: Box<[Transition]> },
Union { alternates: Box<[StateID]> },
Fail,
Match,
}
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct Transition {
pub start: u8,
pub end: u8,
pub next: StateID,
}
impl State {
pub fn is_epsilon(&self) -> bool {
match *self {
State::Range { .. }
| State::Sparse { .. }
| State::Fail
| State::Match => false,
State::Union { .. } => true,
}
}
fn remap(&mut self, remap: &[StateID]) {
match *self {
State::Range { ref mut range } => range.next = remap[range.next],
State::Sparse { ref mut ranges } => {
for r in ranges.iter_mut() {
r.next = remap[r.next];
}
}
State::Union { ref mut alternates } => {
for alt in alternates.iter_mut() {
*alt = remap[*alt];
}
}
State::Fail => {}
State::Match => {}
}
}
}
impl fmt::Debug for State {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
State::Range { ref range } => range.fmt(f),
State::Sparse { ref ranges } => {
let rs = ranges
.iter()
.map(|t| format!("{:?}", t))
.collect::<Vec<String>>()
.join(", ");
write!(f, "sparse({})", rs)
}
State::Union { ref alternates } => {
let alts = alternates
.iter()
.map(|id| format!("{}", id))
.collect::<Vec<String>>()
.join(", ");
write!(f, "alt({})", alts)
}
State::Fail => write!(f, "FAIL"),
State::Match => write!(f, "MATCH"),
}
}
}
impl fmt::Debug for Transition {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let Transition { start, end, next } = *self;
if self.start == self.end {
write!(f, "{} => {}", escape(start), next)
} else {
write!(f, "{}-{} => {}", escape(start), escape(end), next)
}
}
}
fn escape(b: u8) -> String {
use std::ascii;
String::from_utf8(ascii::escape_default(b).collect::<Vec<_>>()).unwrap()
}
#[cfg(test)]
mod tests {
use super::*;
use dense;
use dfa::DFA;
#[test]
fn always_match() {
let nfa = NFA::always_match();
let dfa = dense::Builder::new().build_from_nfa::<usize>(&nfa).unwrap();
assert_eq!(Some(0), dfa.find_at(b"", 0));
assert_eq!(Some(0), dfa.find_at(b"a", 0));
assert_eq!(Some(1), dfa.find_at(b"a", 1));
assert_eq!(Some(0), dfa.find_at(b"ab", 0));
assert_eq!(Some(1), dfa.find_at(b"ab", 1));
assert_eq!(Some(2), dfa.find_at(b"ab", 2));
}
#[test]
fn never_match() {
let nfa = NFA::never_match();
let dfa = dense::Builder::new().build_from_nfa::<usize>(&nfa).unwrap();
assert_eq!(None, dfa.find_at(b"", 0));
assert_eq!(None, dfa.find_at(b"a", 0));
assert_eq!(None, dfa.find_at(b"a", 1));
assert_eq!(None, dfa.find_at(b"ab", 0));
assert_eq!(None, dfa.find_at(b"ab", 1));
assert_eq!(None, dfa.find_at(b"ab", 2));
}
}