Parse the current set of patterns into their AST and extract literals.
(&self)
| 218 | |
| 219 | /// Parse the current set of patterns into their AST and extract literals. |
| 220 | fn parse(&self) -> Result<Parsed, Error> { |
| 221 | let mut exprs = Vec::with_capacity(self.options.pats.len()); |
| 222 | let mut prefixes = Some(Literals::empty()); |
| 223 | let mut suffixes = Some(Literals::empty()); |
| 224 | let mut bytes = false; |
| 225 | let is_set = self.options.pats.len() > 1; |
| 226 | // If we're compiling a regex set and that set has any anchored |
| 227 | // expressions, then disable all literal optimizations. |
| 228 | for pat in &self.options.pats { |
| 229 | let mut parser = |
| 230 | ParserBuilder::new() |
| 231 | .octal(self.options.octal) |
| 232 | .case_insensitive(self.options.case_insensitive) |
| 233 | .multi_line(self.options.multi_line) |
| 234 | .dot_matches_new_line(self.options.dot_matches_new_line) |
| 235 | .swap_greed(self.options.swap_greed) |
| 236 | .ignore_whitespace(self.options.ignore_whitespace) |
| 237 | .unicode(self.options.unicode) |
| 238 | .allow_invalid_utf8(!self.only_utf8) |
| 239 | .nest_limit(self.options.nest_limit) |
| 240 | .build(); |
| 241 | let expr = parser |
| 242 | .parse(pat) |
| 243 | .map_err(|e| Error::Syntax(e.to_string()))?; |
| 244 | bytes = bytes || !expr.is_always_utf8(); |
| 245 | |
| 246 | if !expr.is_anchored_start() && expr.is_any_anchored_start() { |
| 247 | // Partial anchors unfortunately make it hard to use prefixes, |
| 248 | // so disable them. |
| 249 | prefixes = None; |
| 250 | } else if is_set && expr.is_anchored_start() { |
| 251 | // Regex sets with anchors do not go well with literal |
| 252 | // optimizations. |
| 253 | prefixes = None; |
| 254 | } |
| 255 | prefixes = prefixes.and_then(|mut prefixes| { |
| 256 | if !prefixes.union_prefixes(&expr) { |
| 257 | None |
| 258 | } else { |
| 259 | Some(prefixes) |
| 260 | } |
| 261 | }); |
| 262 | |
| 263 | if !expr.is_anchored_end() && expr.is_any_anchored_end() { |
| 264 | // Partial anchors unfortunately make it hard to use suffixes, |
| 265 | // so disable them. |
| 266 | suffixes = None; |
| 267 | } else if is_set && expr.is_anchored_end() { |
| 268 | // Regex sets with anchors do not go well with literal |
| 269 | // optimizations. |
| 270 | suffixes = None; |
| 271 | } |
| 272 | suffixes = suffixes.and_then(|mut suffixes| { |
| 273 | if !suffixes.union_suffixes(&expr) { |
| 274 | None |
| 275 | } else { |
| 276 | Some(suffixes) |
| 277 | } |
no test coverage detected