scnr 0.8.0

Scanner/Lexer with regex patterns and multiple modes
Documentation
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
use regex_syntax::ast::{
    Ast, ClassAscii, ClassAsciiKind, ClassBracketed, ClassPerl, ClassPerlKind, ClassSet,
    ClassSetBinaryOp, ClassSetBinaryOpKind, ClassSetItem, ClassSetRange, ClassSetUnion,
    ClassUnicode,
    ClassUnicodeKind::{Named, NamedValue, OneLetter},
    Literal,
};
use seshat::unicode::{props::Gc, Ucd};

use crate::{Result, ScnrError};

macro_rules! unsupported {
    ($feature:expr) => {
        ScnrError::new($crate::ScnrErrorKind::UnsupportedFeature(
            $feature.to_string(),
        ))
    };
}

pub(crate) struct MatchFn(Box<dyn Fn(char) -> bool + 'static + Send + Sync>);

impl MatchFn {
    pub(crate) fn new<F>(f: F) -> Self
    where
        F: Fn(char) -> bool + 'static + Send + Sync,
    {
        MatchFn(Box::new(f))
    }

    /// Flatten the MatchFn to a function pointer for performance.
    /// This is safe because MatchFn is a thin wrapper around a function pointer.
    #[inline]
    pub(crate) fn inner(&self) -> &(dyn Fn(char) -> bool + 'static + Send + Sync) {
        &*self.0
    }
}

/// A function that takes a character and returns a boolean.
pub(crate) struct MatchFunction {
    pub(crate) match_fn: MatchFn,
}

impl MatchFunction {
    /// Create a new match function from a closure.
    pub(crate) fn new<F>(f: F) -> Self
    where
        F: Fn(char) -> bool + 'static + Send + Sync,
    {
        MatchFunction {
            match_fn: MatchFn::new(f),
        }
    }

    /// Call the match function with a character.
    #[inline]
    pub(crate) fn call(&self, c: char) -> bool {
        self.match_fn.inner()(c)
    }
}

impl TryFrom<&Literal> for MatchFn {
    type Error = ScnrError;

    #[inline(always)]
    fn try_from(l: &Literal) -> Result<Self> {
        let Literal {
            ref c, ref kind, ..
        } = *l;
        let c = *c;
        if c == '.' && *kind == regex_syntax::ast::LiteralKind::Verbatim {
            Ok(MatchFn::new(|ch| ch != '\n' && ch != '\r'))
        } else {
            Ok(MatchFn::new(move |ch| ch == c))
        }
    }
}

impl TryFrom<&ClassSetUnion> for MatchFn {
    type Error = ScnrError;

    #[inline(always)]
    fn try_from(union: &ClassSetUnion) -> Result<Self> {
        union
            .items
            .iter()
            .try_fold(MatchFn::new(|_| false), |acc, s| {
                (s, false)
                    .try_into()
                    .map(|f: MatchFn| MatchFn::new(move |ch| acc.inner()(ch) || f.inner()(ch)))
            })
    }
}

impl TryFrom<&ClassBracketed> for MatchFn {
    type Error = ScnrError;

    #[inline(always)]
    fn try_from(bracketed: &ClassBracketed) -> Result<Self> {
        let negated = bracketed.negated;
        match &bracketed.kind {
            ClassSet::Item(item) => (item, negated).try_into(),
            ClassSet::BinaryOp(bin_op) => (bin_op, negated).try_into(),
        }
    }
}

impl TryFrom<&ClassUnicode> for MatchFn {
    type Error = ScnrError;

    #[inline(always)]
    fn try_from(unicode: &ClassUnicode) -> Result<Self> {
        let kind = unicode.kind.clone();
        let match_function = match kind {
            OneLetter(ch) => {
                match ch {
                    // Unicode class for Letters
                    'L' => MatchFn::new(|ch| ch.alpha()),
                    // Unicode class for Numbers
                    'N' => MatchFn::new(|ch| ch.is_numeric()),
                    // Unicode class for Whitespace
                    'Z' => MatchFn::new(|ch| ch.is_whitespace()),
                    // Unicode class Terminal_Punctuation
                    'P' => MatchFn::new(|ch| ch.term()),
                    // Unicode class for Control characters
                    'C' => MatchFn::new(|ch| ch.is_control()),
                    _ => return Err(unsupported!(format!("Unicode class {}", ch))),
                }
            }
            Named(name) => match name.as_str() {
                "Alphabetic" => MatchFn::new(|ch| ch.alpha()),
                "ASCII_Hex_Digit" => MatchFn::new(|ch| ch.ahex()),
                "Bidi_Control" => MatchFn::new(|ch| ch.bidi_c()),
                "Case_Ignorable" => MatchFn::new(|ch| ch.ci()),
                "Cased" => MatchFn::new(|ch| ch.cased()),
                "Composition_Exclusion" => MatchFn::new(|ch| ch.ce()),
                "Dash" => MatchFn::new(|ch| ch.dash()),
                "Default_Ignorable_Code_Point" => MatchFn::new(|ch| ch.di()),
                "Deprecated" => MatchFn::new(|ch| ch.dep()),
                "Diacritic" => MatchFn::new(|ch| ch.dia()),
                "Emoji_Component" => MatchFn::new(|ch| ch.ecomp()),
                "Emoji_Modifier_Base" => MatchFn::new(|ch| ch.ebase()),
                "Emoji_Modifier" => MatchFn::new(|ch| ch.emod()),
                "Emoji_Presentation" => MatchFn::new(|ch| ch.epres()),
                "Emoji" => MatchFn::new(|ch| ch.emoji()),
                "Extended_Pictographic" => MatchFn::new(|ch| ch.ext_pict()),
                "Extender" => MatchFn::new(|ch| ch.ext()),
                "Full_Composition_Exclusion" => MatchFn::new(|ch| ch.comp_ex()),
                "Grapheme_Extend" => MatchFn::new(|ch| ch.gr_ext()),
                "Hex_Digit" => MatchFn::new(|ch| ch.hex()),
                "Hyphen" => MatchFn::new(|ch| ch.hyphen()),
                "ID_Continue" => MatchFn::new(|ch| ch.idc()),
                "ID_Start" => MatchFn::new(|ch| ch.ids()),
                "Ideographic" => MatchFn::new(|ch| ch.ideo()),
                "IDS_Binary_Operator" => MatchFn::new(|ch| ch.idsb()),
                "IDS_Trinary_Operator" => MatchFn::new(|ch| ch.idst()),
                "Join_Control" => MatchFn::new(|ch| ch.join_c()),
                "Logical_Order_Exception" => MatchFn::new(|ch| ch.loe()),
                "Lowercase" => MatchFn::new(|ch| ch.lower()),
                "Math" => MatchFn::new(|ch| ch.math()),
                "Noncharacter_Code_Point" => MatchFn::new(|ch| ch.nchar()),
                "Other_Alphabetic" => MatchFn::new(|ch| ch.oalpha()),
                "Other_Default_Ignorable_Code_Point" => MatchFn::new(|ch| ch.odi()),
                "Other_Grapheme_Extend" => MatchFn::new(|ch| ch.ogr_ext()),
                "Other_ID_Continue" => MatchFn::new(|ch| ch.oidc()),
                "Other_ID_Start" => MatchFn::new(|ch| ch.oids()),
                "Other_Lowercase" => MatchFn::new(|ch| ch.olower()),
                "Other_Math" => MatchFn::new(|ch| ch.omath()),
                "Other_Uppercase" => MatchFn::new(|ch| ch.oupper()),
                "Pattern_Syntax" => MatchFn::new(|ch| ch.pat_syn()),
                "Pattern_White_Space" => MatchFn::new(|ch| ch.pat_ws()),
                "Prepended_Concatenation_Mark" => MatchFn::new(|ch| ch.pcm()),
                "Quotation_Mark" => MatchFn::new(|ch| ch.qmark()),
                "Radical" => MatchFn::new(|ch| ch.radical()),
                "Regional_Indicator" => MatchFn::new(|ch| ch.ri()),
                "Sentence_Terminal" => MatchFn::new(|ch| ch.sterm()),
                "Soft_Dotted" => MatchFn::new(|ch| ch.sd()),
                "Terminal_Punctuation" => MatchFn::new(|ch| ch.term()),
                "Unified_Ideograph" => MatchFn::new(|ch| ch.uideo()),
                "Uppercase" => MatchFn::new(|ch| ch.upper()),
                "Variation_Selector" => MatchFn::new(|ch| ch.vs()),
                "White_Space" => MatchFn::new(|ch| ch.wspace()),
                "XID_Continue" => MatchFn::new(|ch| ch.xidc()),
                "XID_Start" => MatchFn::new(|ch| ch.xids()),
                _ => return Err(unsupported!(format!("Unicode named class {}", name))),
            },
            NamedValue { name, value, .. } => {
                return Err(unsupported!(format!("Named value {}={}", name, value)));
            }
        };
        Ok(if unicode.is_negated() {
            MatchFn::new(move |ch| !match_function.inner()(ch))
        } else {
            match_function
        })
    }
}

impl TryFrom<&ClassPerl> for MatchFn {
    type Error = ScnrError;

    #[inline(always)]
    fn try_from(perl: &ClassPerl) -> Result<Self> {
        let ClassPerl { negated, kind, .. } = perl;
        let match_function = match kind {
            ClassPerlKind::Digit => MatchFn::new(|ch| ch.is_numeric()),
            ClassPerlKind::Space => MatchFn::new(|ch| ch.is_whitespace()),
            ClassPerlKind::Word => MatchFn::new(|ch| {
                ch.is_alphanumeric() || ch.join_c() || ch.gc() == Gc::Pc || ch.gc() == Gc::Mn
            }),
        };
        Ok(if *negated {
            MatchFn::new(move |ch| !match_function.inner()(ch))
        } else {
            match_function
        })
    }
}

impl TryFrom<&ClassSet> for MatchFn {
    type Error = ScnrError;

    #[inline(always)]
    fn try_from(set: &ClassSet) -> Result<Self> {
        let negated = false;
        match set {
            ClassSet::Item(item) => (item, negated).try_into(),
            ClassSet::BinaryOp(bin_op) => (bin_op, negated).try_into(),
        }
    }
}

impl TryFrom<(&ClassSetItem, bool)> for MatchFn {
    type Error = ScnrError;

    #[inline(always)]
    fn try_from((item, negated): (&ClassSetItem, bool)) -> Result<Self> {
        let match_function = match item {
            ClassSetItem::Empty(_) => MatchFn::new(|_| false),
            ClassSetItem::Literal(ref l) => l.try_into()?,
            ClassSetItem::Range(ref r) => {
                let ClassSetRange {
                    ref start, ref end, ..
                } = *r;
                let start = start.c;
                let end = end.c;
                MatchFn::new(move |ch| start <= ch && ch <= end)
            }
            ClassSetItem::Ascii(ref a) => {
                let ClassAscii {
                    ref kind, negated, ..
                } = *a;
                let match_function = match kind {
                    ClassAsciiKind::Alnum => MatchFn::new(|ch| ch.is_alphanumeric()),
                    ClassAsciiKind::Alpha => MatchFn::new(|ch| ch.is_alphabetic()),
                    ClassAsciiKind::Ascii => MatchFn::new(|ch| ch.is_ascii()),
                    ClassAsciiKind::Blank => MatchFn::new(|ch| ch.is_ascii_whitespace()),
                    ClassAsciiKind::Cntrl => MatchFn::new(|ch| ch.is_ascii_control()),
                    ClassAsciiKind::Digit => MatchFn::new(|ch| ch.is_numeric()),
                    ClassAsciiKind::Graph => MatchFn::new(|ch| ch.is_ascii_graphic()),
                    ClassAsciiKind::Lower => MatchFn::new(|ch| ch.is_lowercase()),
                    ClassAsciiKind::Print => MatchFn::new(|ch| ch.is_ascii_graphic()),
                    ClassAsciiKind::Punct => MatchFn::new(|ch| ch.is_ascii_punctuation()),
                    ClassAsciiKind::Space => MatchFn::new(|ch| ch.is_whitespace()),
                    ClassAsciiKind::Upper => MatchFn::new(|ch| ch.is_uppercase()),
                    ClassAsciiKind::Word => MatchFn::new(|ch| {
                        ch.is_alphanumeric()
                            || ch.join_c()
                            || ch.gc() == Gc::Pc
                            || ch.gc() == Gc::Mn
                    }),
                    ClassAsciiKind::Xdigit => MatchFn::new(|ch| ch.is_ascii_hexdigit()),
                };
                if negated {
                    MatchFn::new(move |ch| !match_function.inner()(ch))
                } else {
                    match_function
                }
            }
            ClassSetItem::Unicode(ref c) => c.try_into()?,
            ClassSetItem::Perl(ref c) => c.try_into()?,
            ClassSetItem::Bracketed(ref c) => c.as_ref().try_into()?,
            ClassSetItem::Union(ref c) => c.try_into()?,
        };
        Ok(if negated {
            MatchFn::new(move |ch| !match_function.inner()(ch))
        } else {
            match_function
        })
    }
}

impl TryFrom<(&ClassSetBinaryOp, bool)> for MatchFn {
    type Error = ScnrError;

    #[inline(always)]
    fn try_from((bin_op, negated): (&ClassSetBinaryOp, bool)) -> Result<Self> {
        let ClassSetBinaryOp { kind, lhs, rhs, .. } = bin_op;
        let lhs: MatchFn = lhs.as_ref().try_into()?;
        let rhs: MatchFn = rhs.as_ref().try_into()?;
        let match_function = match kind {
            ClassSetBinaryOpKind::Intersection => {
                MatchFn::new(move |ch| lhs.inner()(ch) && rhs.inner()(ch))
            }
            ClassSetBinaryOpKind::Difference => {
                MatchFn::new(move |ch| lhs.inner()(ch) && !rhs.inner()(ch))
            }
            ClassSetBinaryOpKind::SymmetricDifference => {
                MatchFn::new(move |ch| lhs.inner()(ch) != rhs.inner()(ch))
            }
        };
        Ok(if negated {
            MatchFn::new(move |ch| !match_function.inner()(ch))
        } else {
            match_function
        })
    }
}

impl TryFrom<&Ast> for MatchFunction {
    type Error = ScnrError;

    #[inline(always)]
    fn try_from(ast: &Ast) -> Result<Self> {
        let match_function = match ast {
            Ast::Empty(_) => {
                // An empty AST matches everything.
                Self::new(|_| true)
            }
            Ast::Dot(_) => {
                // A dot AST matches any character except newline.
                Self::new(|ch| ch != '\n' && ch != '\r')
            }
            Ast::Literal(ref l) => {
                // A literal AST matches a single character.
                Self {
                    match_fn: l.as_ref().try_into()?,
                }
            }
            Ast::ClassUnicode(ref c) => Self {
                match_fn: c.as_ref().try_into()?,
            },
            Ast::ClassPerl(ref c) => Self {
                match_fn: c.as_ref().try_into()?,
            },
            Ast::ClassBracketed(ref c) => Self {
                match_fn: c.as_ref().try_into()?,
            },
            _ => return Err(unsupported!(format!("{:#?}", ast))),
        };
        Ok(match_function)
    }
}

impl std::fmt::Debug for MatchFunction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "MatchFunction")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use regex_syntax::ast::parse::Parser;

    #[test]
    fn test_match_function_unicode_class() {
        let ast = Parser::new().parse(r"\pL").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(match_function.call('a'));
        assert!(match_function.call('A'));
        assert!(!match_function.call('1'));
        assert!(!match_function.call(' '));
    }

    #[test]
    fn test_match_function_perl_class() {
        let ast = Parser::new().parse(r"\d").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(match_function.call('1'));
        assert!(!match_function.call('a'));
    }

    #[test]
    fn test_match_function_bracketed_class() {
        let ast = Parser::new().parse(r"[a-z]").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(match_function.call('a'));
        assert!(match_function.call('z'));
        assert!(!match_function.call('A'));
        assert!(!match_function.call('1'));
    }

    #[test]
    fn test_match_function_binary_op_class_intersection() {
        // Intersection (matching x or y)
        let ast = Parser::new().parse(r"[a-y&&xyz]").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(match_function.call('x'));
        assert!(match_function.call('y'));
        assert!(!match_function.call('a'));
        assert!(!match_function.call('z'));
    }

    #[test]
    fn test_match_function_union_class() {
        let ast = Parser::new().parse(r"[0-9a-z]").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(match_function.call('a'));
        assert!(match_function.call('z'));
        assert!(match_function.call('0'));
        assert!(match_function.call('9'));
        assert!(!match_function.call('!'));
    }

    #[test]
    fn test_match_function_negated_bracketed_class() {
        let ast = Parser::new().parse(r"[^a-z]").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(!match_function.call('a'));
        assert!(!match_function.call('z'));
        assert!(match_function.call('A'));
        assert!(match_function.call('1'));
    }

    #[test]
    fn test_match_function_negated_binary_op_class() {
        let ast = Parser::new().parse(r"[a-z&&[^aeiou]]").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(!match_function.call('a'));
        assert!(!match_function.call('e'));
        assert!(match_function.call('z'));
        assert!(!match_function.call('1'));
    }

    // [[:alpha:]]   ASCII character class ([A-Za-z])
    #[test]
    fn test_match_function_ascci_class() {
        let ast = Parser::new().parse(r"[[:alpha:]]").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(match_function.call('a'));
        assert!(match_function.call('A'));
        assert!(match_function.call('ä'));
        assert!(!match_function.call('1'));
        assert!(!match_function.call(' '));
    }

    // [[:^alpha:]]  Negated ASCII character class ([^A-Za-z])
    #[test]
    fn test_match_function_negated_ascii_class() {
        let ast = Parser::new().parse(r"[^[:alpha:]]").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(!match_function.call('a'));
        assert!(!match_function.call('A'));
        assert!(!match_function.call('ä'));
        assert!(match_function.call('1'));
        assert!(match_function.call(' '));
    }

    #[test]
    fn test_match_function_empty() {
        let ast = Parser::new().parse(r"").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(match_function.call('a'));
        assert!(match_function.call('A'));
        assert!(match_function.call('1'));
        assert!(match_function.call(' '));
    }

    // [x[^xyz]]     Nested/grouping character class (matching any character except y and z)
    #[test]
    fn test_nested_classes() {
        let ast = Parser::new().parse(r"[x[^xyz]]").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(match_function.call('a'));
        assert!(match_function.call('x'));
        assert!(match_function.call('1'));
        assert!(match_function.call(' '));
        assert!(!match_function.call('y'));
        assert!(!match_function.call('z'));
    }

    // [0-9&&[^4]]   Subtraction using intersection and negation (matching 0-9 except 4)
    #[test]
    fn test_subtraction() {
        let ast = Parser::new().parse(r"[0-9&&[^4]]").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(match_function.call('0'));
        assert!(match_function.call('9'));
        assert!(!match_function.call('4'));
        assert!(!match_function.call('a'));
    }

    // [0-9--4]      Direct subtraction (matching 0-9 except 4)
    #[test]
    fn test_direct_subtraction() {
        let ast = Parser::new().parse(r"[0-9--4]").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(match_function.call('0'));
        assert!(match_function.call('9'));
        assert!(!match_function.call('4'));
        assert!(!match_function.call('a'));
    }

    // [a-g~~b-h]    Symmetric difference (matching `a` and `h` only)
    #[test]
    fn test_symmetric_difference() {
        let ast = Parser::new().parse(r"[a-g~~b-h]").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(match_function.call('a'));
        assert!(match_function.call('h'));
        assert!(!match_function.call('b'));
        assert!(!match_function.call('z'));
    }

    // [\[\]]        Escaping in character classes (matching [ or ])
    #[test]
    fn test_escaping() {
        let ast = Parser::new().parse(r"[\[\]]").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(match_function.call('['));
        assert!(match_function.call(']'));
        assert!(!match_function.call('a'));
        assert!(!match_function.call('1'));
    }

    // [a&&b]        An empty character class matching nothing
    #[test]
    fn test_empty_intersection() {
        let ast = Parser::new().parse(r"[a&&b]").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(!match_function.call('a'));
        assert!(!match_function.call('b'));
        assert!(!match_function.call('1'));
        assert!(!match_function.call(' '));
    }

    // [^a--b]       A negated subtraction character class (matching b and everything but a)
    //               This is equivalent to [^a]
    #[test]
    fn test_negated_subtraction() {
        let ast = Parser::new().parse(r"[^a--b]").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(!match_function.call('a'));
        assert!(match_function.call('b'));
        assert!(match_function.call('1'));
        assert!(match_function.call(' '));
    }

    // \p{XID_Start} and \p{XID_Continue} are supported by unicode_xid
    #[test]
    fn test_named_classes() {
        let ast = Parser::new().parse(r"\p{XID_Start}").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(match_function.call('a'));
        assert!(match_function.call('A'));
        assert!(!match_function.call('1'));
        assert!(!match_function.call(' '));

        let ast = Parser::new().parse(r"\p{XID_Continue}").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(match_function.call('a'));
        assert!(match_function.call('A'));
        assert!(match_function.call('1'));
        assert!(!match_function.call(' '));
    }

    #[test]
    fn test_evaluate_general_category() {
        assert_eq!('_'.gc(), Gc::Pc);
        let ast = Parser::new().parse(r"\w").unwrap();
        let match_function = MatchFunction::try_from(&ast).unwrap();
        assert!(match_function.call('_'));
    }
}