| 1 |
use { |
| 2 |
anyhow::Result, |
| 3 |
regex_automata::{ |
| 4 |
dfa::onepass::{self, DFA}, |
| 5 |
nfa::thompson, |
| 6 |
util::{iter, syntax}, |
| 7 |
}, |
| 8 |
regex_test::{ |
| 9 |
CompiledRegex, Match, RegexTest, SearchKind, Span, TestResult, |
| 10 |
TestRunner, |
| 11 |
}, |
| 12 |
}; |
| 13 |
|
| 14 |
use crate::{create_input, suite, testify_captures, untestify_kind}; |
| 15 |
|
| 16 |
const EXPANSIONS: &[&str] = &["is_match", "find", "captures"]; |
| 17 |
|
| 18 |
/// Tests the default configuration of the hybrid NFA/DFA. |
| 19 |
#[test] |
| 20 |
fn default() -> Result<()> { |
| 21 |
let builder = DFA::builder(); |
| 22 |
TestRunner::new()? |
| 23 |
.expand(EXPANSIONS, |t| t.compiles()) |
| 24 |
.test_iter(suite()?.iter(), compiler(builder)) |
| 25 |
.assert(); |
| 26 |
Ok(()) |
| 27 |
} |
| 28 |
|
| 29 |
/// Tests the hybrid NFA/DFA when 'starts_for_each_pattern' is enabled for all |
| 30 |
/// tests. |
| 31 |
#[test] |
| 32 |
fn starts_for_each_pattern() -> Result<()> { |
| 33 |
let mut builder = DFA::builder(); |
| 34 |
builder.configure(DFA::config().starts_for_each_pattern(true)); |
| 35 |
TestRunner::new()? |
| 36 |
.expand(EXPANSIONS, |t| t.compiles()) |
| 37 |
.test_iter(suite()?.iter(), compiler(builder)) |
| 38 |
.assert(); |
| 39 |
Ok(()) |
| 40 |
} |
| 41 |
|
| 42 |
/// Tests the hybrid NFA/DFA when byte classes are disabled. |
| 43 |
/// |
| 44 |
/// N.B. Disabling byte classes doesn't avoid any indirection at search time. |
| 45 |
/// All it does is cause every byte value to be its own distinct equivalence |
| 46 |
/// class. |
| 47 |
#[test] |
| 48 |
fn no_byte_classes() -> Result<()> { |
| 49 |
let mut builder = DFA::builder(); |
| 50 |
builder.configure(DFA::config().byte_classes(false)); |
| 51 |
TestRunner::new()? |
| 52 |
.expand(EXPANSIONS, |t| t.compiles()) |
| 53 |
.test_iter(suite()?.iter(), compiler(builder)) |
| 54 |
.assert(); |
| 55 |
Ok(()) |
| 56 |
} |
| 57 |
|
| 58 |
fn compiler( |
| 59 |
mut builder: onepass::Builder, |
| 60 |
) -> impl FnMut(&RegexTest, &[String]) -> Result<CompiledRegex> { |
| 61 |
move |test, regexes| { |
| 62 |
// Check if our regex contains things that aren't supported by DFAs. |
| 63 |
// That is, Unicode word boundaries when searching non-ASCII text. |
| 64 |
if !configure_onepass_builder(test, &mut builder) { |
| 65 |
return Ok(CompiledRegex::skip()); |
| 66 |
} |
| 67 |
let re = match builder.build_many(®exes) { |
| 68 |
Ok(re) => re, |
| 69 |
Err(err) => { |
| 70 |
let msg = err.to_string(); |
| 71 |
// This is pretty gross, but when a regex fails to compile as |
| 72 |
// a one-pass regex, then we want to be OK with that and just |
| 73 |
// skip the test. But we have to be careful to only skip it |
| 74 |
// when the expected result is that the regex compiles. If |
| 75 |
// the test is specifically checking that the regex does not |
| 76 |
// compile, then we should bubble up that error and allow the |
| 77 |
// test to pass. |
| 78 |
// |
| 79 |
// Since our error types are all generally opaque, we just |
| 80 |
// look for an error string. Not great, but not the end of the |
| 81 |
// world. |
| 82 |
if test.compiles() && msg.contains("not one-pass") { |
| 83 |
return Ok(CompiledRegex::skip()); |
| 84 |
} |
| 85 |
return Err(err.into()); |
| 86 |
} |
| 87 |
}; |
| 88 |
let mut cache = re.create_cache(); |
| 89 |
Ok(CompiledRegex::compiled(move |test| -> TestResult { |
| 90 |
run_test(&re, &mut cache, test) |
| 91 |
})) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
fn run_test( |
| 96 |
re: &DFA, |
| 97 |
cache: &mut onepass::Cache, |
| 98 |
test: &RegexTest, |
| 99 |
) -> TestResult { |
| 100 |
let input = create_input(test); |
| 101 |
match test.additional_name() { |
| 102 |
"is_match" => { |
| 103 |
TestResult::matched(re.is_match(cache, input.earliest(true))) |
| 104 |
} |
| 105 |
"find" => match test.search_kind() { |
| 106 |
SearchKind::Earliest | SearchKind::Leftmost => { |
| 107 |
let input = |
| 108 |
input.earliest(test.search_kind() == SearchKind::Earliest); |
| 109 |
let mut caps = re.create_captures(); |
| 110 |
let it = iter::Searcher::new(input) |
| 111 |
.into_matches_iter(|input| { |
| 112 |
re.try_search(cache, input, &mut caps)?; |
| 113 |
Ok(caps.get_match()) |
| 114 |
}) |
| 115 |
.infallible() |
| 116 |
.take(test.match_limit().unwrap_or(std::usize::MAX)) |
| 117 |
.map(|m| Match { |
| 118 |
id: m.pattern().as_usize(), |
| 119 |
span: Span { start: m.start(), end: m.end() }, |
| 120 |
}); |
| 121 |
TestResult::matches(it) |
| 122 |
} |
| 123 |
SearchKind::Overlapping => { |
| 124 |
// The one-pass DFA does not support any kind of overlapping |
| 125 |
// search. This is not just a matter of not having the API. |
| 126 |
// It's fundamentally incompatible with the one-pass concept. |
| 127 |
// If overlapping matches were possible, then the one-pass DFA |
| 128 |
// would fail to build. |
| 129 |
TestResult::skip() |
| 130 |
} |
| 131 |
}, |
| 132 |
"captures" => match test.search_kind() { |
| 133 |
SearchKind::Earliest | SearchKind::Leftmost => { |
| 134 |
let input = |
| 135 |
input.earliest(test.search_kind() == SearchKind::Earliest); |
| 136 |
let it = iter::Searcher::new(input) |
| 137 |
.into_captures_iter(re.create_captures(), |input, caps| { |
| 138 |
re.try_search(cache, input, caps) |
| 139 |
}) |
| 140 |
.infallible() |
| 141 |
.take(test.match_limit().unwrap_or(std::usize::MAX)) |
| 142 |
.map(|caps| testify_captures(&caps)); |
| 143 |
TestResult::captures(it) |
| 144 |
} |
| 145 |
SearchKind::Overlapping => { |
| 146 |
// The one-pass DFA does not support any kind of overlapping |
| 147 |
// search. This is not just a matter of not having the API. |
| 148 |
// It's fundamentally incompatible with the one-pass concept. |
| 149 |
// If overlapping matches were possible, then the one-pass DFA |
| 150 |
// would fail to build. |
| 151 |
TestResult::skip() |
| 152 |
} |
| 153 |
}, |
| 154 |
name => TestResult::fail(&format!("unrecognized test name: {}", name)), |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/// Configures the given regex builder with all relevant settings on the given |
| 159 |
/// regex test. |
| 160 |
/// |
| 161 |
/// If the regex test has a setting that is unsupported, then this returns |
| 162 |
/// false (implying the test should be skipped). |
| 163 |
fn configure_onepass_builder( |
| 164 |
test: &RegexTest, |
| 165 |
builder: &mut onepass::Builder, |
| 166 |
) -> bool { |
| 167 |
if !test.anchored() { |
| 168 |
return false; |
| 169 |
} |
| 170 |
let match_kind = match untestify_kind(test.match_kind()) { |
| 171 |
None => return false, |
| 172 |
Some(k) => k, |
| 173 |
}; |
| 174 |
|
| 175 |
let config = DFA::config().match_kind(match_kind); |
| 176 |
builder |
| 177 |
.configure(config) |
| 178 |
.syntax(config_syntax(test)) |
| 179 |
.thompson(config_thompson(test)); |
| 180 |
true |
| 181 |
} |
| 182 |
|
| 183 |
/// Configuration of a Thompson NFA compiler from a regex test. |
| 184 |
fn config_thompson(test: &RegexTest) -> thompson::Config { |
| 185 |
let mut lookm = regex_automata::util::look::LookMatcher::new(); |
| 186 |
lookm.set_line_terminator(test.line_terminator()); |
| 187 |
thompson::Config::new().utf8(test.utf8()).look_matcher(lookm) |
| 188 |
} |
| 189 |
|
| 190 |
/// Configuration of the regex parser from a regex test. |
| 191 |
fn config_syntax(test: &RegexTest) -> syntax::Config { |
| 192 |
syntax::Config::new() |
| 193 |
.case_insensitive(test.case_insensitive()) |
| 194 |
.unicode(test.unicode()) |
| 195 |
.utf8(test.utf8()) |
| 196 |
.line_terminator(test.line_terminator()) |
| 197 |
} |