Repository navigation
Expand file tree
/
Copy pathdfa.rs
More file actions
319 lines (290 loc) · 9.85 KB
/
Copy pathdfa.rs
File metadata and controls
319 lines (290 loc) · 9.85 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
use regex_automata::Input;
use crate::{
args,
util::{self, Table},
};
pub fn run_hybrid(p: &mut lexopt::Parser) -> anyhow::Result<()> {
const USAGE: &'static str = "\
Executes a search for full matches using the lazy DFA regex engine.
USAGE:
regex-cli find match hybrid [-p <pattern> ...] <haystack-path>
regex-cli find match hybrid [-p <pattern> ...] -y <haystack>
TIP:
use -h for short docs and --help for long docs
OPTIONS:
%options%
";
let mut common = args::common::Config::default();
let mut input = args::input::Config::default();
let mut patterns = args::patterns::Config::only_flags();
let mut haystack = args::haystack::Config::default();
let mut syntax = args::syntax::Config::default();
let mut thompson = args::thompson::Config::default();
let mut hybrid = args::hybrid::Config::default();
let mut find = super::super::Config::default();
args::configure(
p,
USAGE,
&mut [
&mut common,
&mut input,
&mut patterns,
&mut haystack,
&mut syntax,
&mut thompson,
&mut hybrid,
&mut find,
],
)?;
let pats = patterns.get(&syntax)?;
let mut table = Table::empty();
let (asts, time) = util::timeitr(|| syntax.asts(&pats))?;
table.add("parse time", time);
let (hirs, time) = util::timeitr(|| syntax.hirs(&pats, &asts))?;
table.add("translate time", time);
let (nfafwd, time) = util::timeitr(|| thompson.from_hirs(&hirs))?;
table.add("compile forward nfa time", time);
let (nfarev, time) =
util::timeitr(|| thompson.reversed().from_hirs(&hirs))?;
table.add("compile reverse nfa time", time);
let (dfafwd, time) = util::timeitr(|| hybrid.from_nfa(&nfafwd))?;
table.add("build forward hybrid time", time);
let (dfarev, time) =
util::timeitr(|| hybrid.reversed().from_nfa(&nfarev))?;
table.add("build reverse hybrid time", time);
let (re, time) = util::timeit(|| {
regex_automata::hybrid::regex::Builder::new()
.build_from_dfas(dfafwd, dfarev)
});
table.add("build regex time", time);
let (mut cache, time) = util::timeit(|| re.create_cache());
table.add("cache creation time", time);
let search = |input: &Input<'_>| re.try_search(&mut cache, input);
if find.count {
super::run_counts(
&mut table,
&common,
&find,
&input,
&haystack,
re.pattern_len(),
search,
)?;
} else {
super::run_search(
&mut table, &common, &find, &input, &haystack, search,
)?;
}
Ok(())
}
pub fn run_dense(p: &mut lexopt::Parser) -> anyhow::Result<()> {
const USAGE: &'static str = "\
Executes a search for full matches using the dense DFA regex engine.
USAGE:
regex-cli find match dense [-p <pattern> ...] <haystack-path>
regex-cli find match dense [-p <pattern> ...] -y <haystack>
TIP:
use -h for short docs and --help for long docs
OPTIONS:
%options%
";
let mut common = args::common::Config::default();
let mut input = args::input::Config::default();
let mut patterns = args::patterns::Config::only_flags();
let mut haystack = args::haystack::Config::default();
let mut syntax = args::syntax::Config::default();
let mut thompson = args::thompson::Config::default();
let mut dense = args::dfa::Config::default();
let mut find = super::super::Config::default();
args::configure(
p,
USAGE,
&mut [
&mut common,
&mut input,
&mut patterns,
&mut haystack,
&mut syntax,
&mut thompson,
&mut dense,
&mut find,
],
)?;
let pats = patterns.get(&syntax)?;
let mut table = Table::empty();
let (asts, time) = util::timeitr(|| syntax.asts(&pats))?;
table.add("parse time", time);
let (hirs, time) = util::timeitr(|| syntax.hirs(&pats, &asts))?;
table.add("translate time", time);
let (nfafwd, time) = util::timeitr(|| thompson.from_hirs(&hirs))?;
table.add("compile forward nfa time", time);
let (nfarev, time) =
util::timeitr(|| thompson.reversed().from_hirs(&hirs))?;
table.add("compile reverse nfa time", time);
let (dfafwd, time) = util::timeitr(|| dense.from_nfa(&nfafwd))?;
table.add("build forward dense DFA time", time);
let (dfarev, time) = util::timeitr(|| dense.reversed().from_nfa(&nfarev))?;
table.add("build reverse dense DFA time", time);
let (re, time) = util::timeit(|| {
regex_automata::dfa::regex::Builder::new()
.build_from_dfas(dfafwd, dfarev)
});
table.add("build regex time", time);
let search = |input: &Input<'_>| re.try_search(input);
if find.count {
super::run_counts(
&mut table,
&common,
&find,
&input,
&haystack,
re.pattern_len(),
search,
)?;
} else {
super::run_search(
&mut table, &common, &find, &input, &haystack, search,
)?;
}
Ok(())
}
pub fn run_sparse(p: &mut lexopt::Parser) -> anyhow::Result<()> {
const USAGE: &'static str = "\
Executes a search for full matches using the sparse DFA regex engine.
USAGE:
regex-cli find match sparse [-p <pattern> ...] <haystack-path>
regex-cli find match sparse [-p <pattern> ...] -y <haystack>
TIP:
use -h for short docs and --help for long docs
OPTIONS:
%options%
";
let mut common = args::common::Config::default();
let mut input = args::input::Config::default();
let mut patterns = args::patterns::Config::only_flags();
let mut haystack = args::haystack::Config::default();
let mut syntax = args::syntax::Config::default();
let mut thompson = args::thompson::Config::default();
let mut sparse = args::dfa::Config::default();
let mut find = super::super::Config::default();
args::configure(
p,
USAGE,
&mut [
&mut common,
&mut input,
&mut patterns,
&mut haystack,
&mut syntax,
&mut thompson,
&mut sparse,
&mut find,
],
)?;
let pats = patterns.get(&syntax)?;
let mut table = Table::empty();
let (asts, time) = util::timeitr(|| syntax.asts(&pats))?;
table.add("parse time", time);
let (hirs, time) = util::timeitr(|| syntax.hirs(&pats, &asts))?;
table.add("translate time", time);
let (nfafwd, time) = util::timeitr(|| thompson.from_hirs(&hirs))?;
table.add("compile forward nfa time", time);
let (nfarev, time) =
util::timeitr(|| thompson.reversed().from_hirs(&hirs))?;
table.add("compile reverse nfa time", time);
let (dfafwd, time) = util::timeitr(|| sparse.from_nfa_sparse(&nfafwd))?;
table.add("build forward sparse DFA time", time);
let (dfarev, time) =
util::timeitr(|| sparse.reversed().from_nfa_sparse(&nfarev))?;
table.add("build reverse sparse DFA time", time);
let (re, time) = util::timeit(|| {
regex_automata::dfa::regex::Builder::new()
.build_from_dfas(dfafwd, dfarev)
});
table.add("build regex time", time);
let search = |input: &Input<'_>| re.try_search(input);
if find.count {
super::run_counts(
&mut table,
&common,
&find,
&input,
&haystack,
re.pattern_len(),
search,
)?;
} else {
super::run_search(
&mut table, &common, &find, &input, &haystack, search,
)?;
}
Ok(())
}
pub fn run_onepass(p: &mut lexopt::Parser) -> anyhow::Result<()> {
const USAGE: &'static str = "\
Executes a search for full matches using the one-pass DFA regex engine.
USAGE:
regex-cli find match onepass [-p <pattern> ...] <haystack-path>
regex-cli find match onepass [-p <pattern> ...] -y <haystack>
TIP:
use -h for short docs and --help for long docs
OPTIONS:
%options%
";
let mut common = args::common::Config::default();
let mut input = args::input::Config::default();
let mut patterns = args::patterns::Config::only_flags();
let mut haystack = args::haystack::Config::default();
let mut syntax = args::syntax::Config::default();
let mut thompson = args::thompson::Config::default();
let mut onepass = args::onepass::Config::default();
let mut find = super::super::Config::default();
args::configure(
p,
USAGE,
&mut [
&mut common,
&mut input,
&mut patterns,
&mut haystack,
&mut syntax,
&mut thompson,
&mut onepass,
&mut find,
],
)?;
let pats = patterns.get(&syntax)?;
let mut table = Table::empty();
let (asts, time) = util::timeitr(|| syntax.asts(&pats))?;
table.add("parse time", time);
let (hirs, time) = util::timeitr(|| syntax.hirs(&pats, &asts))?;
table.add("translate time", time);
let (nfa, time) = util::timeitr(|| thompson.from_hirs(&hirs))?;
table.add("compile nfa time", time);
let (re, time) = util::timeitr(|| onepass.from_nfa(&nfa))?;
table.add("build onepass time", time);
let (mut cache, time) = util::timeit(|| re.create_cache());
table.add("cache creation time", time);
let (mut caps, time) = util::timeit(|| re.create_captures());
table.add("captures creation time", time);
let search = |input: &Input<'_>| {
re.try_search(&mut cache, input, &mut caps)?;
Ok(caps.get_match())
};
if find.count {
super::run_counts(
&mut table,
&common,
&find,
&input,
&haystack,
re.pattern_len(),
search,
)?;
} else {
super::run_search(
&mut table, &common, &find, &input, &haystack, search,
)?;
}
Ok(())
}