Repository navigation
Expand file tree
/
Copy pathmod.rs
More file actions
241 lines (214 loc) · 7.06 KB
/
Copy pathmod.rs
File metadata and controls
241 lines (214 loc) · 7.06 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
use std::io::{stdout, Write};
use {
anyhow::Context,
lexopt::Parser,
regex_automata::{Input, MatchError, PatternID, PatternSet},
};
use crate::{
args,
util::{self, Table},
};
mod dfa;
mod nfa;
pub fn run(p: &mut Parser) -> anyhow::Result<()> {
const USAGE: &'static str = "\
Executes a 'which' search. This type of search reports *only* which patterns
match a haystack. It doesn't report positions or even how many times each
pattern matches. (Therefore, the -c/--count flag doesn't work with this
command.)
It is generally expected to use '--match-kind all' with this command, as the
intent is to report all overlapping matches.
Note that the search will usually scan the entire haystack. It can sometimes
short circuit if all patterns are anchored or if the search knows no more
patterns will match.
This type of search is somewhat of a legacy feature because of how the
top-level RegexSet API works in the 'regex' crate. Its API is pretty limited
and it is difficult to extend to the more flexible meta regex API in
regex-automata.
The 'backtrack' engine isn't supported here because it doesn't have a 'which'
search routine. In theory it could, but it would likely be slow and no better
than just running each regex over the haystack one at a time.
The 'onepass' engine also does not support this API. (At least, not currently.)
USAGE:
regex-cli find which <engine>
ENGINES:
dense Search with the dense DFA regex engine.
hybrid Search with the lazy DFA regex engine.
meta Search with the meta regex engine.
pikevm Search with the PikeVM regex engine.
regexset Search with the top-level API regex engine.
sparse Search with the sparse DFA regex engine.
";
let cmd = args::next_as_command(USAGE, p)?;
match &*cmd {
"dense" => dfa::run_dense(p),
"hybrid" => dfa::run_hybrid(p),
"meta" => run_meta(p),
"pikevm" => nfa::run_pikevm(p),
"regex" => run_regex(p),
"sparse" => dfa::run_sparse(p),
unk => anyhow::bail!("unrecognized command '{unk}'"),
}
}
fn run_regex(p: &mut lexopt::Parser) -> anyhow::Result<()> {
const USAGE: &'static str = "\
Executes a search for full matches using the top-level API regex engine.
USAGE:
regex-cli find match regex [-p <pattern> ...] <haystack-path>
regex-cli find match regex [-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 patterns = args::patterns::Config::only_flags();
let mut haystack = args::haystack::Config::default();
let mut syntax = args::syntax::Config::default();
let mut api = args::api::Config::default();
let mut find = super::Config::default();
args::configure(
p,
USAGE,
&mut [
&mut common,
&mut patterns,
&mut haystack,
&mut syntax,
&mut api,
&mut find,
],
)?;
anyhow::ensure!(
!find.count,
"'which' command does not support reporting counts",
);
let pats = patterns.get(&syntax)?;
let syn = syntax.syntax()?;
let mut table = Table::empty();
let (re, time) = util::timeitr(|| api.from_patterns_set(&syn, &pats))?;
table.add("build regex time", time);
// The top-level API doesn't support regex-automata's more granular Input
// abstraction.
let input = args::input::Config::default();
let search = |input: &Input<'_>, patset: &mut PatternSet| {
let matches = re.matches(input.haystack());
for pid in matches.iter() {
let pid = PatternID::new(pid).unwrap();
patset.try_insert(pid).unwrap();
}
Ok(())
};
run_search(
&mut table,
&common,
&find,
&input,
&haystack,
re.len(),
search,
)?;
Ok(())
}
fn run_meta(p: &mut lexopt::Parser) -> anyhow::Result<()> {
const USAGE: &'static str = "\
Executes a search for full matches using the meta regex engine.
USAGE:
regex-cli find match meta [-p <pattern> ...] <haystack-path>
regex-cli find match meta [-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 meta = args::meta::Config::default();
let mut find = super::Config::default();
args::configure(
p,
USAGE,
&mut [
&mut common,
&mut input,
&mut patterns,
&mut haystack,
&mut syntax,
&mut meta,
&mut find,
],
)?;
anyhow::ensure!(
!find.count,
"'which' command does not support reporting counts",
);
let pats = patterns.get(&syntax)?;
let mut table = Table::empty();
let re = if meta.build_from_patterns() {
let (re, time) = util::timeitr(|| meta.from_patterns(&syntax, &pats))?;
table.add("build meta time", time);
re
} else {
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 (re, time) = util::timeitr(|| meta.from_hirs(&hirs))?;
table.add("build meta time", time);
re
};
let search = |input: &Input<'_>, patset: &mut PatternSet| {
Ok(re.which_overlapping_matches(input, patset))
};
run_search(
&mut table,
&common,
&find,
&input,
&haystack,
re.pattern_len(),
search,
)?;
Ok(())
}
/// Like `run_counts`, but prints the actual matches instead.
fn run_search(
table: &mut Table,
common: &args::common::Config,
find: &super::Config,
input: &args::input::Config,
haystack: &args::haystack::Config,
pattern_len: usize,
mut search: impl FnMut(&Input<'_>, &mut PatternSet) -> Result<(), MatchError>,
) -> anyhow::Result<()> {
let mut out = stdout();
input.with(haystack, |input| {
let (patset, time) = util::timeitr(|| {
let mut patset = PatternSet::new(pattern_len);
for _ in 0..find.repeat() {
search(&input, &mut patset)?;
}
Ok::<_, anyhow::Error>(patset)
})?;
table.add("search time", time);
table.add("patterns that matched", patset.len());
if common.table() {
table.print(&mut out)?;
}
if !common.quiet {
for i in 0..pattern_len {
let pid = PatternID::new(i).context("invalid pattern ID")?;
writeln!(
out,
"{}:{:?}",
pid.as_usize(),
patset.contains(pid)
)?;
}
}
Ok(())
})
}