regex-cli 0.2.3

A command line tool for debugging, ad hoc benchmarking and generating regular expressions.
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
use std::io::{stdout, Write};

use {
    anyhow::Context,
    bstr::ByteSlice,
    lexopt::Parser,
    regex_automata::{
        util::captures::{Captures, GroupInfo},
        Input, MatchError, PatternID,
    },
};

use crate::{
    args,
    util::{self, Table},
};

mod dfa;
mod nfa;

pub fn run(p: &mut Parser) -> anyhow::Result<()> {
    const USAGE: &'static str = "\
Executes a search for capturing groups.

This command is limited to regex engines that support resolving capture groups.
It prints each match, corresponding to possibly many matching capture groups,
on its own line. It is prefixed with the pattern ID of the match. Each group
contains both the spans matched and the actual substring that matches.

USAGE:
    regex-cli find capture <engine>

ENGINES:
    backtrack  Search with the bounded backtracker regex engine.
    lite       Search with the regex-lite engine.
    meta       Search with the meta regex engine.
    onepass    Search with the one-pass DFA regex engine.
    pikevm     Search with the PikeVM regex engine.
    regex      Search with the top-level API regex engine.
";
    let cmd = args::next_as_command(USAGE, p)?;
    match &*cmd {
        "backtrack" => nfa::run_backtrack(p),
        "lite" => run_lite(p),
        "meta" => run_meta(p),
        "onepass" => dfa::run_onepass(p),
        "pikevm" => nfa::run_pikevm(p),
        "regex" => run_regex(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 capture regex [-p <pattern> ...] <haystack-path>
    regex-cli find capture 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,
        ],
    )?;

    let pats = patterns.get()?;
    let syn = syntax.syntax()?;
    let mut table = Table::empty();
    let (re, time) = util::timeitr(|| api.from_patterns(&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();
    // The top-level API also doesn't use 'Captures' from regex-automata
    // directly, but we can map between them with some annoyance.
    let group_info = GroupInfo::new([re.capture_names()])
        .context("could not build capture group info")?;
    let mut locs = re.capture_locations();
    let search = |input: &Input<'_>, caps: &mut Captures| {
        caps.set_pattern(None);
        if !re
            .captures_read_at(&mut locs, input.haystack(), input.start())
            .is_some()
        {
            return Ok(());
        }
        caps.set_pattern(Some(PatternID::ZERO));
        for i in 0..locs.len() {
            use regex_automata::util::primitives::NonMaxUsize;

            let slot_start = i * 2;
            let slot_end = slot_start + 1;
            match locs.get(i) {
                None => {
                    caps.slots_mut()[slot_start] = None;
                    caps.slots_mut()[slot_end] = None;
                }
                Some((start, end)) => {
                    caps.slots_mut()[slot_start] = NonMaxUsize::new(start);
                    caps.slots_mut()[slot_end] = NonMaxUsize::new(end);
                }
            }
        }
        Ok(())
    };
    if find.count {
        run_counts(
            &mut table,
            &common,
            &find,
            &input,
            &haystack,
            &group_info,
            search,
        )?;
    } else {
        run_search(
            &mut table,
            &common,
            &find,
            &input,
            &haystack,
            &group_info,
            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 capture meta [-p <pattern> ...] <haystack-path>
    regex-cli find capture 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,
        ],
    )?;

    let pats = patterns.get()?;
    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<'_>, caps: &mut Captures| {
        Ok(re.search_captures(input, caps))
    };
    if find.count {
        run_counts(
            &mut table,
            &common,
            &find,
            &input,
            &haystack,
            re.group_info(),
            search,
        )?;
    } else {
        run_search(
            &mut table,
            &common,
            &find,
            &input,
            &haystack,
            re.group_info(),
            search,
        )?;
    }
    Ok(())
}

fn run_lite(p: &mut lexopt::Parser) -> anyhow::Result<()> {
    const USAGE: &'static str = "\
Executes a search for full matches using the top-level regex-lite engine.

USAGE:
    regex-cli find capture lite [-p <pattern> ...] <haystack-path>
    regex-cli find capture lite [-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 lite = args::lite::Config::default();
    let mut find = super::Config::default();
    args::configure(
        p,
        USAGE,
        &mut [
            &mut common,
            &mut patterns,
            &mut haystack,
            &mut syntax,
            &mut lite,
            &mut find,
        ],
    )?;

    let pats = patterns.get()?;
    let syn = syntax.syntax()?;
    let mut table = Table::empty();
    let (re, time) = util::timeitr(|| lite.from_patterns(&syn, &pats))?;
    table.add("build regex time", time);

    // Check that the haystack is valid UTF-8 since regex-lite doesn't support
    // searching arbitrary byte sequences. (At time of writing.)
    haystack.get()?.to_str()?;

    // The top-level API doesn't support regex-automata's more granular Input
    // abstraction.
    let input = args::input::Config::default();
    // The top-level API also doesn't use 'Captures' from regex-automata
    // directly, but we can map between them with some annoyance.
    let group_info = GroupInfo::new([re.capture_names()])
        .context("could not build capture group info")?;
    let mut locs = re.capture_locations();
    let search = |input: &Input<'_>, caps: &mut Captures| {
        let haystack = input.haystack().to_str().unwrap();
        caps.set_pattern(None);
        if !re.captures_read_at(&mut locs, haystack, input.start()).is_some() {
            return Ok(());
        }
        caps.set_pattern(Some(PatternID::ZERO));
        for i in 0..locs.len() {
            use regex_automata::util::primitives::NonMaxUsize;

            let slot_start = i * 2;
            let slot_end = slot_start + 1;
            match locs.get(i) {
                None => {
                    caps.slots_mut()[slot_start] = None;
                    caps.slots_mut()[slot_end] = None;
                }
                Some((start, end)) => {
                    caps.slots_mut()[slot_start] = NonMaxUsize::new(start);
                    caps.slots_mut()[slot_end] = NonMaxUsize::new(end);
                }
            }
        }
        Ok(())
    };
    if find.count {
        run_counts(
            &mut table,
            &common,
            &find,
            &input,
            &haystack,
            &group_info,
            search,
        )?;
    } else {
        run_search(
            &mut table,
            &common,
            &find,
            &input,
            &haystack,
            &group_info,
            search,
        )?;
    }
    Ok(())
}

/// A function that takes in a bunch of configuration, runs the given search
/// routine, and prints out a table of counts.
fn run_counts(
    table: &mut Table,
    common: &args::common::Config,
    find: &super::Config,
    input: &args::input::Config,
    haystack: &args::haystack::Config,
    group_info: &GroupInfo,
    mut search: impl FnMut(&Input<'_>, &mut Captures) -> Result<(), MatchError>,
) -> anyhow::Result<()> {
    let mut out = stdout();
    input.with(haystack, |input| {
        let (counts, time) = util::timeitr(|| {
            let mut counts = vec![vec![]; group_info.pattern_len()];
            for i in 0..group_info.pattern_len() {
                let pid = PatternID::new(i).context("invalid pattern ID")?;
                counts[i] = vec![0; group_info.group_len(pid)];
            }
            let mut caps = Captures::all(group_info.clone());
            for _ in 0..find.repeat() {
                let mut it =
                    regex_automata::util::iter::Searcher::new(input.clone());
                loop {
                    let m = it.try_advance(|input| {
                        search(input, &mut caps)?;
                        Ok(caps.get_match())
                    })?;
                    match m {
                        None => break,
                        Some(m) => {
                            for (i, span) in caps.iter().enumerate() {
                                if span.is_some() {
                                    counts[m.pattern()][i] += 1;
                                }
                            }
                        }
                    }
                }
            }
            Ok::<_, anyhow::Error>(counts)
        })?;
        table.add("search time", time);
        table.add("total matches", counts.iter().map(|c| c[0]).sum::<u64>());
        if common.table() {
            table.print(&mut out)?;
        }
        if !common.quiet {
            for (i, pattern_counts) in counts.iter().enumerate() {
                let pid = PatternID::new(i).context("invalid pattern ID")?;
                write!(out, "{}:{{ ", pid.as_usize())?;
                let names = group_info.pattern_names(pid);
                for (group_index, maybe_name) in names.enumerate() {
                    if group_index > 0 {
                        write!(out, ", ")?;
                    }
                    let count = pattern_counts[group_index];
                    if let Some(name) = maybe_name {
                        write!(out, "{group_index}/{name}: {count}")?;
                    } else {
                        write!(out, "{group_index}: {count}")?;
                    }
                }
                write!(out, " }}\n")?;
            }
        }
        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,
    group_info: &GroupInfo,
    mut search: impl FnMut(&Input<'_>, &mut Captures) -> Result<(), MatchError>,
) -> anyhow::Result<()> {
    let mut out = stdout();
    input.with(haystack, |input| {
        let (matches, time) = util::timeitr(|| {
            let mut matches = vec![];
            for _ in 0..find.repeat() {
                let caps = Captures::all(group_info.clone());
                let it =
                    regex_automata::util::iter::Searcher::new(input.clone())
                        .into_captures_iter(caps, &mut search);
                for caps in it {
                    matches.push(caps?);
                }
            }
            Ok::<_, anyhow::Error>(matches)
        })?;
        table.add("search time", time);
        table.add("total matches", matches.len());
        if common.table() {
            table.print(&mut out)?;
        }
        if !common.quiet {
            for caps in matches.iter() {
                let pid = caps.pattern().unwrap();
                write!(out, "{}:{{ ", pid.as_usize())?;
                let names = caps.group_info().pattern_names(pid);
                for (group_index, maybe_name) in names.enumerate() {
                    if group_index > 0 {
                        write!(out, ", ")?;
                    }
                    if let Some(name) = maybe_name {
                        write!(out, "{group_index}/{name}: ")?;
                    } else {
                        write!(out, "{group_index}: ")?;
                    }
                    match caps.get_group(group_index) {
                        None => write!(out, "NONE")?,
                        Some(sp) => {
                            let string = input.haystack()[sp].escape_bytes();
                            write!(
                                out,
                                "{}..{}/{}",
                                sp.start, sp.end, string
                            )?;
                        }
                    }
                }
                write!(out, " }}\n")?;
            }
        }
        Ok(())
    })
}