Debian Codegraph @

debian/rust-vendor/regex-automata/tests/nfa/thompson/pikevm/suite.rs

raw
1 use {
2 anyhow::Result,
3 regex_automata::{
4 nfa::thompson::{
5 self,
6 pikevm::{self, PikeVM},
7 },
8 util::{prefilter::Prefilter, syntax},
9 PatternSet,
10 },
11 regex_test::{
12 CompiledRegex, Match, RegexTest, SearchKind, Span, TestResult,
13 TestRunner,
14 },
15 };
16
17 use crate::{create_input, suite, testify_captures, untestify_kind};
18
19 /// Tests the default configuration of the hybrid NFA/DFA.
20 #[test]
21 fn default() -> Result<()> {
22 let builder = PikeVM::builder();
23 let mut runner = TestRunner::new()?;
24 runner.expand(&["is_match", "find", "captures"], |test| test.compiles());
25 runner.test_iter(suite()?.iter(), compiler(builder)).assert();
26 Ok(())
27 }
28
29 /// Tests the PikeVM with prefilters enabled.
30 #[test]
31 fn prefilter() -> Result<()> {
32 let my_compiler = |test: &RegexTest, regexes: &[String]| {
33 // Parse regexes as HIRs so we can get literals to build a prefilter.
34 let mut hirs = vec![];
35 for pattern in regexes.iter() {
36 hirs.push(syntax::parse_with(pattern, &config_syntax(test))?);
37 }
38 let kind = match untestify_kind(test.match_kind()) {
39 None => return Ok(CompiledRegex::skip()),
40 Some(kind) => kind,
41 };
42 let pre = Prefilter::from_hirs_prefix(kind, &hirs);
43 let mut builder = PikeVM::builder();
44 builder.configure(PikeVM::config().prefilter(pre));
45 compiler(builder)(test, regexes)
46 };
47 let mut runner = TestRunner::new()?;
48 runner.expand(&["is_match", "find", "captures"], |test| test.compiles());
49 runner.test_iter(suite()?.iter(), my_compiler).assert();
50 Ok(())
51 }
52
53 fn compiler(
54 mut builder: pikevm::Builder,
55 ) -> impl FnMut(&RegexTest, &[String]) -> Result<CompiledRegex> {
56 move |test, regexes| {
57 if !configure_pikevm_builder(test, &mut builder) {
58 return Ok(CompiledRegex::skip());
59 }
60 let re = builder.build_many(&regexes)?;
61 let mut cache = re.create_cache();
62 Ok(CompiledRegex::compiled(move |test| -> TestResult {
63 run_test(&re, &mut cache, test)
64 }))
65 }
66 }
67
68 fn run_test(
69 re: &PikeVM,
70 cache: &mut pikevm::Cache,
71 test: &RegexTest,
72 ) -> TestResult {
73 let input = create_input(test);
74 match test.additional_name() {
75 "is_match" => TestResult::matched(re.is_match(cache, input)),
76 "find" => match test.search_kind() {
77 SearchKind::Earliest => {
78 let it = re
79 .find_iter(cache, input.earliest(true))
80 .take(test.match_limit().unwrap_or(std::usize::MAX))
81 .map(|m| Match {
82 id: m.pattern().as_usize(),
83 span: Span { start: m.start(), end: m.end() },
84 });
85 TestResult::matches(it)
86 }
87 SearchKind::Leftmost => {
88 let it = re
89 .find_iter(cache, input)
90 .take(test.match_limit().unwrap_or(std::usize::MAX))
91 .map(|m| Match {
92 id: m.pattern().as_usize(),
93 span: Span { start: m.start(), end: m.end() },
94 });
95 TestResult::matches(it)
96 }
97 SearchKind::Overlapping => {
98 let mut patset = PatternSet::new(re.get_nfa().pattern_len());
99 re.which_overlapping_matches(cache, &input, &mut patset);
100 TestResult::which(patset.iter().map(|p| p.as_usize()))
101 }
102 },
103 "captures" => match test.search_kind() {
104 SearchKind::Earliest => {
105 let it = re
106 .captures_iter(cache, input.earliest(true))
107 .take(test.match_limit().unwrap_or(std::usize::MAX))
108 .map(|caps| testify_captures(&caps));
109 TestResult::captures(it)
110 }
111 SearchKind::Leftmost => {
112 let it = re
113 .captures_iter(cache, input)
114 .take(test.match_limit().unwrap_or(std::usize::MAX))
115 .map(|caps| testify_captures(&caps));
116 TestResult::captures(it)
117 }
118 SearchKind::Overlapping => {
119 // There is no overlapping PikeVM API that supports captures.
120 TestResult::skip()
121 }
122 },
123 name => TestResult::fail(&format!("unrecognized test name: {name}")),
124 }
125 }
126
127 /// Configures the given regex builder with all relevant settings on the given
128 /// regex test.
129 ///
130 /// If the regex test has a setting that is unsupported, then this returns
131 /// false (implying the test should be skipped).
132 fn configure_pikevm_builder(
133 test: &RegexTest,
134 builder: &mut pikevm::Builder,
135 ) -> bool {
136 let match_kind = match untestify_kind(test.match_kind()) {
137 None => return false,
138 Some(k) => k,
139 };
140 let pikevm_config = PikeVM::config().match_kind(match_kind);
141 builder
142 .configure(pikevm_config)
143 .syntax(config_syntax(test))
144 .thompson(config_thompson(test));
145 true
146 }
147
148 /// Configuration of a Thompson NFA compiler from a regex test.
149 fn config_thompson(test: &RegexTest) -> thompson::Config {
150 let mut lookm = regex_automata::util::look::LookMatcher::new();
151 lookm.set_line_terminator(test.line_terminator());
152 thompson::Config::new().utf8(test.utf8()).look_matcher(lookm)
153 }
154
155 /// Configuration of the regex parser from a regex test.
156 fn config_syntax(test: &RegexTest) -> syntax::Config {
157 syntax::Config::new()
158 .case_insensitive(test.case_insensitive())
159 .unicode(test.unicode())
160 .utf8(test.utf8())
161 .line_terminator(test.line_terminator())
162 }