File size: 9,668 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#![feature(arbitrary_self_types_pointers)]

use std::vec;

use anyhow::{Result, bail};

pub fn register() {
    turbo_tasks::register();
    include!(concat!(env!("OUT_DIR"), "/register.rs"));
}

/// A simple regular expression implementation following ecmascript semantics
///
/// Delegates to the `regex` crate when possible and `regress` otherwise.
#[derive(Debug, Clone)]
#[turbo_tasks::value(eq = "manual", shared)]
#[serde(into = "RegexForm", try_from = "RegexForm")]
pub struct EsRegex {
    #[turbo_tasks(trace_ignore)]
    delegate: EsRegexImpl,
    // Store the original arguments used to construct
    // this regex to support equality and serialization.
    pub pattern: String,
    pub flags: String,
}

#[derive(Debug, Clone)]
enum EsRegexImpl {
    Regex(regex::Regex),
    Regress(regress::Regex),
}

/// Equality uses the source inputs since our delegate regex impls don't support
/// equality natively.
/// NOTE: there are multiple 'equivalent' ways to write a regex and this
/// approach does _not_ attempt to equate them.
impl PartialEq for EsRegex {
    fn eq(&self, other: &Self) -> bool {
        self.pattern == other.pattern && self.flags == other.flags
    }
}
impl Eq for EsRegex {}

impl TryFrom<RegexForm> for EsRegex {
    type Error = anyhow::Error;

    fn try_from(value: RegexForm) -> std::result::Result<Self, Self::Error> {
        EsRegex::new(&value.pattern, &value.flags)
    }
}

/// This is the serializable form for the `EsRegex` struct
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
struct RegexForm {
    pattern: String,
    flags: String,
}

impl From<EsRegex> for RegexForm {
    fn from(value: EsRegex) -> Self {
        Self {
            pattern: value.pattern,
            flags: value.flags,
        }
    }
}

impl EsRegex {
    /// Support ecmascript style regular expressions by selecting the `regex` crate when possible
    /// and using regress when not.
    pub fn new(pattern: &str, flags: &str) -> Result<Self> {
        // rust regex doesn't allow escaped slashes, but they are necessary in js
        let pattern = pattern.replace("\\/", "/");

        let mut applied_flags = String::new();
        for flag in flags.chars() {
            match flag {
                // indices for substring matches: not relevant for the regex itself
                'd' => {}
                // global: default in rust, ignore
                'g' => {}
                // case-insensitive: letters match both upper and lower case
                'i' => applied_flags.push('i'),
                // multi-line mode: ^ and $ match begin/end of line
                'm' => applied_flags.push('m'),
                // allow . to match \n
                's' => applied_flags.push('s'),
                // Unicode support (enabled by default)
                'u' => applied_flags.push('u'),
                // sticky search: not relevant for the regex itself
                'y' => {}
                _ => bail!("unsupported flag `{flag}` in regex: `{pattern}` with flags: `{flags}`"),
            }
        }

        let regex = if !applied_flags.is_empty() {
            regex::Regex::new(&format!("(?{applied_flags}){pattern}"))
        } else {
            regex::Regex::new(&pattern)
        };

        let delegate = match regex {
            Ok(reg) => Ok(EsRegexImpl::Regex(reg)),
            Err(_e) => {
                // We failed to parse as an regex:Regex, try using regress. Regress uses the es
                // flags format so we can pass the original flags value.
                match regress::Regex::with_flags(&pattern, regress::Flags::from(flags)) {
                    Ok(reg) => Ok(EsRegexImpl::Regress(reg)),
                    // Propagate the error as is, regress has useful error messages.
                    Err(e) => Err(e),
                }
            }
        }?;
        Ok(Self {
            delegate,
            pattern,
            flags: flags.to_string(),
        })
    }

    /// Returns true if there is any match for this regex in the `haystack`.
    pub fn is_match(&self, haystack: &str) -> bool {
        match &self.delegate {
            EsRegexImpl::Regex(r) => r.is_match(haystack),
            EsRegexImpl::Regress(r) => r.find(haystack).is_some(),
        }
    }

    /// Searches for the first match of the regex in the `haystack`, and iterates over the capture
    /// groups within that first match.
    ///
    /// `None` is returned if there is no match. Individual capture groups may be `None` if the
    /// capture group wasn't included in the match.
    ///
    /// The first capture group is always present ([`Some`]) and represents the entire match.
    ///
    /// Capture groups are represented as string slices of the `haystack`, and live for the lifetime
    /// of `haystack`.
    pub fn captures<'h>(&self, haystack: &'h str) -> Option<Captures<'h>> {
        let delegate = match &self.delegate {
            EsRegexImpl::Regex(r) => CapturesImpl::Regex {
                captures: r.captures(haystack)?,
                idx: 0,
            },
            EsRegexImpl::Regress(r) => {
                let re_match = r.find(haystack)?;
                CapturesImpl::Regress {
                    captures_iter: re_match.captures.into_iter(),
                    haystack,
                    match_range: Some(re_match.range),
                }
            }
        };
        Some(Captures { delegate })
    }
}

pub struct Captures<'h> {
    delegate: CapturesImpl<'h>,
}

enum CapturesImpl<'h> {
    // We have to use `regex::Captures` (which is not an iterator) here instead of
    // `regex::SubCaptureMatches` (an iterator) because `SubCaptureMatches` must have a reference
    // to `Capture`, and that would require a self-referential struct.
    //
    // Ideally, `regex::Capture` would implement `IntoIterator`, and we could use that here
    // instead.
    Regex {
        captures: regex::Captures<'h>,
        idx: usize,
    },
    // We can't use the iterator from `regress::Match::groups()` due to similar lifetime issues.
    Regress {
        captures_iter: vec::IntoIter<Option<regress::Range>>,
        haystack: &'h str,
        match_range: Option<regress::Range>,
    },
}

impl<'h> Iterator for Captures<'h> {
    type Item = Option<&'h str>;

    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.delegate {
            CapturesImpl::Regex { captures, idx } => {
                if *idx >= captures.len() {
                    None
                } else {
                    let capture = Some(captures.get(*idx).map(|sub_match| sub_match.as_str()));
                    *idx += 1;
                    capture
                }
            }
            CapturesImpl::Regress {
                captures_iter,
                haystack,
                match_range,
            } => {
                if let Some(range) = match_range.take() {
                    // always yield range first
                    Some(Some(&haystack[range]))
                } else {
                    Some(captures_iter.next()?.map(|range| &haystack[range]))
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{EsRegex, EsRegexImpl};

    #[test]
    fn round_trip_serialize() {
        let regex = EsRegex::new("[a-z]", "i").unwrap();
        let serialized = serde_json::to_string(&regex).unwrap();
        let parsed = serde_json::from_str::<EsRegex>(&serialized).unwrap();
        assert_eq!(regex, parsed);
    }

    #[test]
    fn es_regex_matches_simple() {
        let regex = EsRegex::new("a", "").unwrap();
        assert!(matches!(regex.delegate, EsRegexImpl::Regex { .. }));
        assert!(regex.is_match("a"));
    }

    #[test]
    fn es_regex_matches_negative_lookahead() {
        // This feature is not supported by the regex crate
        let regex = EsRegex::new("a(?!b)", "").unwrap();
        assert!(matches!(regex.delegate, EsRegexImpl::Regress { .. }));
        assert!(!regex.is_match("ab"));
        assert!(regex.is_match("ac"));
    }

    #[test]
    fn invalid_regex() {
        // This is invalid since there is nothing being repeated
        // Don't bother asserting on the message since we delegate
        // that to the underlying implementations.
        assert!(matches!(EsRegex::new("*", ""), Err { .. }))
    }

    #[test]
    fn captures_with_regex() {
        let regex = EsRegex::new(r"(notmatched)|(\d{4})-(\d{2})-(\d{2})", "").unwrap();
        assert!(matches!(regex.delegate, EsRegexImpl::Regex { .. }));

        let captures = regex.captures("Today is 2024-01-15");
        assert!(captures.is_some());
        let caps: Vec<_> = captures.unwrap().collect();
        assert_eq!(caps.len(), 5); // full match + 4 groups
        assert_eq!(caps[0], Some("2024-01-15")); // full match
        assert_eq!(caps[1], None); // 'notmatched' -- this branch isn't taken
        assert_eq!(caps[2], Some("2024")); // year
        assert_eq!(caps[3], Some("01")); // month
        assert_eq!(caps[4], Some("15")); // day
    }

    #[test]
    fn captures_with_regress() {
        let regex = EsRegex::new(r"(\w+)(?=baz)", "").unwrap();
        assert!(matches!(regex.delegate, EsRegexImpl::Regress { .. }));

        let captures = regex.captures("foobar");
        assert!(captures.is_none());

        let captures = regex.captures("foobaz");
        assert!(captures.is_some());
        let caps: Vec<_> = captures.unwrap().collect();
        assert_eq!(caps.len(), 2); // full match + 1 group
        assert_eq!(caps[0], Some("foo")); // full match
        assert_eq!(caps[1], Some("foo")); // captured group
    }
}