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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use std::ffi::CStr;
use std::ffi::CString;
use std::fmt;
use std::mem::MaybeUninit;
use std::ptr::null;
use std::str::FromStr;

use foreign_types::{foreign_type, ForeignType, ForeignTypeRef};
use libc::c_char;

use crate::{
    chimera::{ffi, Database, Error as ChError, Pattern, Patterns},
    error::AsResult,
    Error, PlatformRef,
};

foreign_type! {
    /// Providing details of the compile error condition.
    pub unsafe type CompileError: Send + Sync {
        type CType = ffi::ch_compile_error_t;

        fn drop = free_compile_error;
    }
}

unsafe fn free_compile_error(err: *mut ffi::ch_compile_error_t) {
    ffi::ch_free_compile_error(err).expect("free compile error");
}

impl fmt::Display for CompileError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.message())
    }
}

impl fmt::Debug for CompileError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CompileError")
            .field("message", &self.message())
            .field("expression", &self.expression())
            .finish()
    }
}

impl PartialEq for CompileError {
    fn eq(&self, other: &Self) -> bool {
        self.as_ptr() == other.as_ptr()
    }
}

impl Eq for CompileError {}

impl CompileError {
    unsafe fn as_ref(&self) -> &ffi::ch_compile_error_t {
        self.as_ptr().as_ref().unwrap()
    }

    /// A human-readable error message describing the error.
    pub fn message(&self) -> &str {
        unsafe { CStr::from_ptr(self.as_ref().message).to_str().unwrap() }
    }

    /// The zero-based number of the expression that caused the error (if this can be determined).
    pub fn expression(&self) -> Option<usize> {
        let n = unsafe { self.as_ref().expression };

        if n < 0 {
            None
        } else {
            Some(n as usize)
        }
    }
}

pub trait AsCompileResult: Sized {
    type Output;
    type Err: fmt::Display;

    fn ok_or(self, err: *mut ffi::ch_compile_error_t) -> Result<Self::Output, Self::Err> {
        self.ok_or_else(|| err)
    }

    fn ok_or_else<F>(self, err: F) -> Result<Self::Output, Self::Err>
    where
        F: FnOnce() -> *mut ffi::ch_compile_error_t;
}

impl AsCompileResult for ffi::ch_error_t {
    type Output = ();
    type Err = Error;

    fn ok_or_else<F>(self, err: F) -> Result<Self::Output, Self::Err>
    where
        F: FnOnce() -> *mut ffi::ch_compile_error_t,
    {
        if self == ffi::CH_SUCCESS as ffi::ch_error_t {
            Ok(())
        } else if self == ffi::CH_COMPILER_ERROR {
            Err(ChError::CompileError(unsafe { CompileError::from_ptr(err()) }).into())
        } else {
            Err(ChError::from(self).into())
        }
    }
}

/// Compile mode flags
///
/// The mode flags are used as values for the mode parameter of the various
/// compile calls `Builder::build` for `Pattern` or `Patterns`.
///
/// By default, the matcher will only supply the start and end offsets of the
/// match when the match callback is called. Using mode flag `Mode::Groups`
/// will also fill the `captured' array with the start and end offsets of all
/// the capturing groups specified by the pattern that has matched.
#[repr(u32)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Mode {
    /// Disable capturing groups.
    NoGroups = ffi::CH_MODE_NOGROUPS,
    /// Enable capturing groups.
    Groups = ffi::CH_MODE_GROUPS,
}

impl Default for Mode {
    fn default() -> Self {
        Mode::NoGroups
    }
}

/// Define match limits for PCRE runtime.
pub struct MatchLimit {
    /// A limit from pcre_extra on the amount of match function called in PCRE to limit backtracking that can take place.
    pub max_matches: u64,
    /// A limit from pcre_extra on the recursion depth of match function in PCRE.
    pub recursion_depth: u64,
}

/// Compile an expression into a Chimera database.
///
/// # Examples
///
/// ```rust
/// # use hyperscan::chimera::prelude::*;
/// let db: Database = compile(r"/foo(bar)?/i").unwrap();
/// let mut s = db.alloc_scratch().unwrap();
///
/// let mut matches = vec![];
/// db.scan("hello foobar!", &mut s, |_, from, to, _, _| {
///     matches.push(from..to);
///     Matching::Continue
/// }, |_, _|{
///     Matching::Skip
/// }).unwrap();
///
/// assert_eq!(matches, vec![6..12]);
/// ```
pub fn compile<S: Builder>(expression: S) -> Result<Database, S::Err> {
    expression.build()
}

impl<S> Builder for S
where
    S: AsRef<str>,
{
    type Err = Error;

    /// Build an expression is compiled into a Hyperscan database for a target platform.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use hyperscan::chimera::prelude::*;
    /// let db: Database = r"/foo(bar)?/i".build().unwrap();
    /// let mut s = db.alloc_scratch().unwrap();
    ///
    /// let mut matches = vec![];
    /// db.scan("hello foobar!", &mut s, |_, from, to, _, _| {
    ///     matches.push(from..to);
    ///     Matching::Continue
    /// }, Matching::Skip).unwrap();
    ///
    /// assert_eq!(matches, vec![6..12]);
    /// ```
    fn for_platform(
        &self,
        mode: Mode,
        match_limit: Option<MatchLimit>,
        platform: Option<&PlatformRef>,
    ) -> Result<Database, Self::Err> {
        self.as_ref()
            .parse::<Pattern>()?
            .for_platform(mode, match_limit, platform)
    }
}

/// The regular expression pattern database builder.
pub trait Builder {
    /// The associated error which can be returned from compiling.
    type Err;

    /// Build an expression is compiled into a Chimera database which can be passed to the runtime functions.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use hyperscan::chimera::prelude::*;
    /// let pattern: Pattern = "/test/i".parse().unwrap();
    /// let db = pattern.build().unwrap();
    /// let scratch = db.alloc_scratch().unwrap();
    /// let mut matches = vec![];
    /// let mut errors = vec![];
    ///
    /// db.scan("some test data", &scratch, |id, from, to, _flags, captured| {
    ///     println!("found pattern {} : {} @ [{}, {})", id, pattern.expression, from, to);
    ///
    ///     matches.push((from, to));
    ///
    ///     Matching::Continue
    /// }, |error_type, id| {
    ///     errors.push((error_type, id));
    ///
    ///     Matching::Skip
    /// }).unwrap();
    ///
    /// assert_eq!(matches, vec![(5, 9)]);
    /// assert_eq!(errors, vec![]);
    /// ```
    fn build(&self) -> Result<Database, Self::Err> {
        self.for_platform(Mode::NoGroups, None, None)
    }

    /// Build an expression is compiled into a Chimera database that the database as a whole for capturing groups.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use hyperscan::chimera::prelude::*;
    /// let pattern: Pattern = r"/(?<word>\w+)/i".parse().unwrap();
    /// let db = pattern.with_groups().unwrap();
    /// let scratch = db.alloc_scratch().unwrap();
    /// let mut matches = vec![];
    /// let mut captures = vec![];
    /// let mut errors = vec![];
    ///
    /// db.scan("some test data", &scratch, |id, from, to, _flags, captured: Option<&[Capture]>| {
    ///     println!("found pattern {} : {} @ [{}, {}), captured {:?}", id, pattern.expression, from, to, captured);
    ///
    ///     matches.push((from, to));
    ///
    ///     if let Some(captured) = captured {
    ///         captures.push(captured.first().expect("captured").range());
    ///     }
    ///
    ///     Matching::Continue
    /// }, |error_type, id| {
    ///     errors.push((error_type, id));
    ///
    ///     Matching::Skip
    /// }).unwrap();
    ///
    /// assert_eq!(matches, vec![(0, 4), (5, 9), (10, 14)]);
    /// assert_eq!(captures, vec![0..4, 5..9, 10..14]);
    /// assert_eq!(errors, vec![]);
    /// ```
    fn with_groups(&self) -> Result<Database, Self::Err> {
        self.for_platform(Mode::Groups, None, None)
    }

    /// Build an expression is compiled into a Chimera database for a target platform.
    fn for_platform(
        &self,
        mode: Mode,
        match_limit: Option<MatchLimit>,
        platform: Option<&PlatformRef>,
    ) -> Result<Database, Self::Err>;
}

impl Builder for Pattern {
    type Err = Error;

    ///
    /// The basic regular expression compiler.
    ///
    /// This is the function call with which an expression is compiled into a Chimera database
    /// which can be passed to the runtime function.
    ///
    fn for_platform(
        &self,
        mode: Mode,
        match_limit: Option<MatchLimit>,
        platform: Option<&PlatformRef>,
    ) -> Result<Database, Self::Err> {
        let expr = CString::new(self.expression.as_str())?;
        let ptr = expr.as_bytes_with_nul().as_ptr() as *const c_char;
        let flags = self.flags.bits();
        let mut db = MaybeUninit::uninit();
        let mut err = MaybeUninit::uninit();

        unsafe {
            if let Some(MatchLimit {
                max_matches,
                recursion_depth,
            }) = match_limit
            {
                ffi::ch_compile_ext_multi(
                    &ptr,
                    &flags,
                    &0,
                    1,
                    mode as _,
                    max_matches,
                    recursion_depth,
                    platform.map_or_else(null, |platform| platform.as_ptr() as *const _),
                    db.as_mut_ptr(),
                    err.as_mut_ptr(),
                )
            } else {
                ffi::ch_compile(
                    ptr,
                    flags,
                    mode as u32,
                    platform.map_or_else(null, |platform| platform.as_ptr() as *const _),
                    db.as_mut_ptr(),
                    err.as_mut_ptr(),
                )
            }
            .ok_or_else(|| err.assume_init())
            .map(|_| Database::from_ptr(db.assume_init()))
        }
    }
}

impl Builder for Patterns {
    type Err = Error;

    ///
    /// The multiple regular expression compiler.
    ///
    /// This is the function call with which a set of expressions is compiled into a database
    /// which can be passed to the runtime functions.
    /// Each expression can be labelled with a unique integer
    // which is passed into the match callback to identify the pattern that has matched.
    ///
    fn for_platform(
        &self,
        mode: Mode,
        match_limit: Option<MatchLimit>,
        platform: Option<&PlatformRef>,
    ) -> Result<Database, Self::Err> {
        let expressions = self
            .iter()
            .map(|Pattern { expression, .. }| CString::new(expression.as_str()))
            .collect::<Result<Vec<_>, _>>()?;
        let ptrs = expressions
            .iter()
            .map(|expr| expr.as_ptr() as *const _)
            .collect::<Vec<_>>();
        let flags = self
            .iter()
            .map(|Pattern { flags, .. }| flags.bits() as _)
            .collect::<Vec<_>>();
        let ids = self
            .iter()
            .enumerate()
            .map(|(i, Pattern { id, .. })| id.unwrap_or(i) as _)
            .collect::<Vec<_>>();

        let mut db = MaybeUninit::uninit();
        let mut err = MaybeUninit::uninit();

        unsafe {
            if let Some(MatchLimit {
                max_matches,
                recursion_depth,
            }) = match_limit
            {
                ffi::ch_compile_ext_multi(
                    ptrs.as_ptr(),
                    flags.as_ptr(),
                    ids.as_ptr(),
                    self.len() as _,
                    mode as _,
                    max_matches,
                    recursion_depth,
                    platform.map_or_else(null, |platform| platform.as_ptr() as *const _),
                    db.as_mut_ptr(),
                    err.as_mut_ptr(),
                )
            } else {
                ffi::ch_compile_multi(
                    ptrs.as_ptr(),
                    flags.as_ptr(),
                    ids.as_ptr(),
                    self.len() as _,
                    mode as _,
                    platform.map_or_else(null, |platform| platform.as_ptr() as *const _),
                    db.as_mut_ptr(),
                    err.as_mut_ptr(),
                )
            }
            .ok_or_else(|| err.assume_init())
            .map(|_| Database::from_ptr(db.assume_init()))
        }
    }
}

impl FromStr for Database {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        s.parse::<Pattern>()?.build()
    }
}