| 1 |
use core::str; |
| 2 |
|
| 3 |
use lazy_static::lazy_static; |
| 4 |
|
| 5 |
use crate::file_patterns::PatternError; |
| 6 |
|
| 7 |
lazy_static! { |
| 8 |
static ref RE_ESCAPE: Vec<Vec<u8>> = { |
| 9 |
let mut v: Vec<Vec<u8>> = (0..=255).map(|byte| vec![byte]).collect(); |
| 10 |
let to_escape = b"()[]{}?*+-|^$\\.&~#\t\n\r\x0b\x0c"; |
| 11 |
for byte in to_escape { |
| 12 |
v[*byte as usize].insert(0, b'\\'); |
| 13 |
} |
| 14 |
v |
| 15 |
}; |
| 16 |
} |
| 17 |
|
| 18 |
pub fn escape_char_for_re(c: u8) -> &'static [u8] { |
| 19 |
&RE_ESCAPE[c as usize] |
| 20 |
} |
| 21 |
|
| 22 |
/// An intermediate regular expression representation, that can be used |
| 23 |
/// both to compile down to a `Regex` for matching, or converted to |
| 24 |
/// a string directly for diagnostics. |
| 25 |
#[derive(Debug, Clone)] |
| 26 |
pub enum PreRegex { |
| 27 |
Empty, |
| 28 |
Dot, |
| 29 |
DotStar, |
| 30 |
Eof, |
| 31 |
NonslashStar, |
| 32 |
Byte(u8), |
| 33 |
Bytes(Vec<u8>), |
| 34 |
SlashOrEof, |
| 35 |
Re((regex_syntax::hir::Hir, Vec<u8>)), |
| 36 |
Maybe(Box<Self>), |
| 37 |
Alternation(Vec<Self>), |
| 38 |
Sequence(Vec<Self>), |
| 39 |
} |
| 40 |
|
| 41 |
mod to_hir { |
| 42 |
use itertools::Itertools; |
| 43 |
use regex_syntax::hir::Class; |
| 44 |
use regex_syntax::hir::ClassBytes; |
| 45 |
use regex_syntax::hir::ClassBytesRange; |
| 46 |
use regex_syntax::hir::Dot; |
| 47 |
use regex_syntax::hir::Hir; |
| 48 |
use regex_syntax::hir::Look; |
| 49 |
use regex_syntax::hir::Repetition; |
| 50 |
|
| 51 |
use super::PreRegex; |
| 52 |
|
| 53 |
fn hir_star(hir: Hir) -> Hir { |
| 54 |
Hir::repetition(Repetition { |
| 55 |
min: 0, |
| 56 |
max: None, |
| 57 |
greedy: false, |
| 58 |
sub: Box::new(hir), |
| 59 |
}) |
| 60 |
} |
| 61 |
|
| 62 |
fn hir_eof() -> Hir { |
| 63 |
Hir::look(Look::End) |
| 64 |
} |
| 65 |
|
| 66 |
fn hir_nonslash() -> Hir { |
| 67 |
let mut class = |
| 68 |
Class::Bytes(ClassBytes::new([ClassBytesRange::new(b'/', b'/')])); |
| 69 |
Class::negate(&mut class); |
| 70 |
Hir::class(class) |
| 71 |
} |
| 72 |
|
| 73 |
fn hir_byte(b: u8) -> Hir { |
| 74 |
let class = Class::Bytes(ClassBytes::new([ClassBytesRange::new(b, b)])); |
| 75 |
Hir::class(class) |
| 76 |
} |
| 77 |
|
| 78 |
fn hir_literal(text: &[u8]) -> Hir { |
| 79 |
let b: Box<[u8]> = Box::from(text); |
| 80 |
Hir::literal(b) |
| 81 |
} |
| 82 |
|
| 83 |
pub(crate) fn to_hir(re: &PreRegex) -> regex_syntax::hir::Hir { |
| 84 |
match re { |
| 85 |
PreRegex::Empty => Hir::empty(), |
| 86 |
PreRegex::Dot => Hir::dot(Dot::AnyByte), |
| 87 |
PreRegex::DotStar => hir_star(Hir::dot(Dot::AnyByte)), |
| 88 |
PreRegex::Eof => hir_eof(), |
| 89 |
PreRegex::NonslashStar => hir_star(hir_nonslash()), |
| 90 |
PreRegex::Byte(b) => hir_byte(*b), |
| 91 |
PreRegex::Bytes(bs) => hir_literal(bs), |
| 92 |
PreRegex::SlashOrEof => { |
| 93 |
Hir::alternation(vec![hir_byte(b'/'), hir_eof()]) |
| 94 |
} |
| 95 |
PreRegex::Re((hir, _)) => hir.clone(), |
| 96 |
PreRegex::Maybe(s) => { |
| 97 |
Hir::alternation(vec![Hir::empty(), s.to_hir()]) |
| 98 |
} |
| 99 |
PreRegex::Alternation(alt) => { |
| 100 |
let alt = alt.iter().map(|r| r.to_hir()).collect_vec(); |
| 101 |
Hir::alternation(alt) |
| 102 |
} |
| 103 |
PreRegex::Sequence(seq) => { |
| 104 |
let seq = seq.iter().map(|r| r.to_hir()).collect_vec(); |
| 105 |
Hir::concat(seq) |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
impl PreRegex { |
| 112 |
pub fn to_hir(&self) -> regex_syntax::hir::Hir { |
| 113 |
to_hir::to_hir(self) |
| 114 |
} |
| 115 |
|
| 116 |
fn to_bytes_rec(&self, out: &mut Vec<u8>) { |
| 117 |
match self { |
| 118 |
PreRegex::Empty => (), |
| 119 |
PreRegex::Dot => out.push(b'.'), |
| 120 |
PreRegex::DotStar => out.extend_from_slice(&b".*"[..]), |
| 121 |
PreRegex::Eof => out.push(b'$'), |
| 122 |
PreRegex::NonslashStar => out.extend_from_slice(&b"[^/]*"[..]), |
| 123 |
PreRegex::Byte(b) => out.extend_from_slice(escape_char_for_re(*b)), |
| 124 |
PreRegex::Bytes(bytes) => { |
| 125 |
for b in bytes { |
| 126 |
out.extend_from_slice(escape_char_for_re(*b)) |
| 127 |
} |
| 128 |
} |
| 129 |
PreRegex::SlashOrEof => out.extend_from_slice(&b"(?:/|$)"[..]), |
| 130 |
PreRegex::Re((_hir, src)) => out.extend_from_slice(src), |
| 131 |
PreRegex::Alternation(alt) => { |
| 132 |
if alt.is_empty() { |
| 133 |
// something that can never match |
| 134 |
out.extend_from_slice(&b" ^"[..]) |
| 135 |
} else { |
| 136 |
out.extend_from_slice(&b"(?:"[..]); |
| 137 |
let mut first = true; |
| 138 |
for r in alt { |
| 139 |
if first { |
| 140 |
first = false |
| 141 |
} else { |
| 142 |
out.extend_from_slice(&b"|"[..]); |
| 143 |
} |
| 144 |
r.to_bytes_rec(out) |
| 145 |
} |
| 146 |
out.extend_from_slice(&b")"[..]); |
| 147 |
} |
| 148 |
} |
| 149 |
PreRegex::Sequence(seq) => { |
| 150 |
for r in seq { |
| 151 |
r.to_bytes_rec(out) |
| 152 |
} |
| 153 |
} |
| 154 |
PreRegex::Maybe(r) => { |
| 155 |
out.extend_from_slice(&b"(?:"[..]); |
| 156 |
r.to_bytes_rec(out); |
| 157 |
out.extend_from_slice(&b")?"[..]); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
pub fn parse(re: &[u8]) -> Result<Self, PatternError> { |
| 163 |
let re_str = str::from_utf8(re) |
| 164 |
.map_err(|err| PatternError::UnsupportedSyntax(err.to_string()))?; |
| 165 |
Ok(Self::Re(( |
| 166 |
regex_syntax::parse(re_str).map_err(|err| { |
| 167 |
PatternError::UnsupportedSyntax(err.to_string()) |
| 168 |
})?, |
| 169 |
re.to_vec(), |
| 170 |
))) |
| 171 |
} |
| 172 |
|
| 173 |
pub fn to_bytes(&self) -> Vec<u8> { |
| 174 |
let mut out = vec![]; |
| 175 |
self.to_bytes_rec(&mut out); |
| 176 |
out |
| 177 |
} |
| 178 |
|
| 179 |
pub fn literal(prefix: &[u8]) -> PreRegex { |
| 180 |
Self::Bytes(prefix.to_vec()) |
| 181 |
} |
| 182 |
|
| 183 |
pub fn preceding_dir_components() -> Self { |
| 184 |
Self::Maybe(Box::new(Self::Sequence(vec![ |
| 185 |
Self::DotStar, |
| 186 |
Self::Byte(b'/'), |
| 187 |
]))) |
| 188 |
} |
| 189 |
} |