crossandra 1.0.1

A fast and simple lexical tokenization library.
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
//! Crossandra is a straightforward tokenization library designed for seamless text processing.
//!
//! # Examples
//! ## [Brainfuck](https://en.wikipedia.org/wiki/Brainfuck)
//! ```rust
//! use crossandra::Tokenizer;
//!
//! # fn main() {
//! let bf_tok = Tokenizer::default()
//!     .with_literals(&[
//!         ("add", "+"),
//!         ("sub", "-"),
//!         ("left", "<"),
//!         ("right", ">"),
//!         ("read", ","),
//!         ("write", "."),
//!         ("begin_loop", "["),
//!         ("end_loop", "]"),
//!     ])
//!     .expect("all literals should be ≥1 characters long");
//!
//! for token in bf_tok.tokenize("cat program: ,[.,]").filter_map(Result::ok) {
//!    println!("{:?}", token);
//! }
//! # }
//! ```
pub use rustc_hash::{FxHashMap, FxHashSet};

use fancy_regex::Regex;
use rayon::prelude::*;

pub mod common;

mod error;
pub use error::Error;

mod stream;

mod token;
pub use token::Token;

mod tree;
use tree::{Tree, generate_tree};

mod patterns;

const WHITESPACE: [char; 6] = [' ', '\x0c', '\t', '\x0b', '\r', '\n'];

/// The Crossandra tokenizer, operating on literals and patterns.
///
/// ## Literals
/// Literals indicate values that have to be exactly matched by the tokenizer. They are represented
/// by a slice of (name, value) pairs. For example, a literal map for Brainfuck would be defined
/// like this:
/// ```rust
/// # use crossandra::Tokenizer;
/// let literals = [
///     ("add", "+"),
///     ("sub", "-"),
///     ("left", "<"),
///     ("right", ">"),
///     ("read", ","),
///     ("write", "."),
///     ("begin_loop", "["),
///     ("end_loop", "]"),
/// ];
/// # assert!(Tokenizer::default().with_literals(&literals).is_ok());
/// ```
/// Literals take precedence over patterns.
///
/// ## Patterns
/// Patterns are regular expressions that match more complex token structures. They are represented
/// as pairs of strings (name, pattern) in a [`Vec`] to maintain a consistent matching order.
///
/// The order of patterns matters as the tokenizer will use the first matching pattern it finds.
/// Duplicate pattern names are not allowed and will result in an error. This crate also provides a
/// collection of commonly used patterns in the [`common`] module. For example, patterns covering
/// binary, octal, and hexadecimal literals could be defined like this:
/// ```rust
/// # use crossandra::Tokenizer;
/// let patterns = [
///     ("binary", r"0[bB][01]+"),
///     ("octal", r"0[Oo][0-7]+"),
///     ("hexadecimal", r"(?i)0x[0-9a-f]+"),
/// ];
/// # assert!(Tokenizer::default().with_patterns(&patterns).is_ok());
/// ```
///
/// ## Other options
///
/// ### `ignore_whitespace`
/// Whether to ignore the following whitespace characters:
///
/// | Code   | Character              |
/// |--------|------------------------|
/// | `0x9`  | Tab (`\t`)             |
/// | `0xa`  | Line feed (`\n`)       |
/// | `0xb`  | Vertical tab           |
/// | `0xc`  | Form feed              |
/// | `0xd`  | Carriage return (`\r`) |
/// | `0x20` | Space (` `)            |
///
/// Defaults to `false`.
///
/// ### `ignored_characters`
/// A set of characters to ignore during tokenization. Defaults to an empty [`Vec`].
///
/// ## Fast Mode
/// When all literals are of length 1 and there are no patterns, Crossandra uses a simpler
/// tokenization method.
///
/// For instance, tokenizing a 1MB random Brainfuck file with 10% of the file being comments is
/// ~300x faster with Fast Mode (32.5s vs 110ms on Apple M2).
///
/// Do note that this is a rather extreme case; for a 1KB file, the speedup is ~2.3x.
#[derive(Debug, Clone)]
pub struct Tokenizer<'a> {
    literals: FxHashMap<&'a str, &'a str>,
    patterns: Vec<(&'a str, Regex)>,
    ignore_whitespace: bool,
    ignored_characters: FxHashSet<char>,
    tree: Tree<'a>,
}

impl PartialEq for Tokenizer<'_> {
    fn eq(&self, other: &Self) -> bool {
        self.literals == other.literals
            && self.ignore_whitespace == other.ignore_whitespace
            && self.ignored_characters == other.ignored_characters
            && self.patterns.len() == other.patterns.len()
            && self
                .patterns
                .iter()
                .zip(&other.patterns)
                .all(|(a, b)| a.0 == b.0 && a.1.as_str() == b.1.as_str())
    }
}

impl Eq for Tokenizer<'_> {}

impl<'a> Tokenizer<'a> {
    /// Creates a new [`Tokenizer`] with the specified configuration.
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// * any [literal](Tokenizer#literals) is empty,
    /// * there are duplicate [patterns](Tokenizer#patterns), or
    /// * any [pattern](Tokenizer#patterns) regex is invalid.
    pub fn new(
        literals: &[(&'a str, &'a str)],
        patterns: &[(&'a str, &'a str)],
        ignored_characters: FxHashSet<char>,
        ignore_whitespace: bool,
    ) -> Result<Self, Error> {
        validate_literals(literals)?;
        let literals = stream::build_hashmap(literals);
        Ok(Self {
            tree: generate_tree(&literals),
            literals,
            patterns: patterns::prepare(patterns)?,
            ignored_characters,
            ignore_whitespace,
        })
    }

    fn can_use_fast_mode(&self) -> bool {
        self.patterns.is_empty() && self.literals.keys().all(|v| v.len() == 1)
    }

    fn prepare_ignored(&self) -> FxHashSet<char> {
        let ignored = self.ignored_characters.iter().copied();
        if self.ignore_whitespace {
            ignored.chain(WHITESPACE).collect()
        } else {
            ignored.collect()
        }
    }

    /// Tokenizes the given source code and returns an [`Iterator`] of [`Token`]s.
    #[must_use]
    pub fn tokenize(
        &'a self,
        source: &'a str,
    ) -> Box<dyn Iterator<Item = Result<Token<'a>, Error>> + 'a> {
        let ignored = self.prepare_ignored();
        if self.can_use_fast_mode() {
            Box::new(stream::Fast::new(self, source, ignored))
        } else {
            Box::new(stream::Core::new(self, source, ignored))
        }
    }

    /// Splits the given source code into lines and tokenizes each line separately.
    /// Returns an [`Iterator`] of [`Vec`]s of [`Token`]s.
    ///
    /// # Errors
    ///
    /// This function will return an error if any line fails to tokenize.
    #[must_use]
    pub fn tokenize_lines(
        &'a self,
        source: &'a str,
    ) -> impl ParallelIterator<Item = Result<Vec<Token<'a>>, Error>> + 'a {
        source
            .par_split('\n')
            .map(|line| self.tokenize(line).collect())
    }

    /// Sets the [literals](Tokenizer#literals) of this [`Tokenizer`] and returns itself.
    ///
    /// # Errors
    /// This function will return an error if any literal is empty.
    pub fn with_literals(mut self, literals: &[(&'a str, &'a str)]) -> Result<Self, Error> {
        self.set_literals(literals)?;
        Ok(self)
    }

    /// Sets the [patterns](Tokenizer#patterns) of this [`Tokenizer`] and returns itself.
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// * there are duplicate patterns, or
    /// * any pattern regex is invalid.
    pub fn with_patterns(mut self, patterns: &[(&'a str, &'a str)]) -> Result<Self, Error> {
        self.set_patterns(patterns)?;
        Ok(self)
    }

    /// Sets the [ignored characters](Tokenizer#ignored_characters) of this [`Tokenizer`] and
    /// returns itself.
    #[must_use]
    pub fn with_ignored_characters(mut self, ignored_characters: FxHashSet<char>) -> Self {
        self.ignored_characters = ignored_characters;
        self
    }

    /// Sets the [`ignore_whitespace`](Tokenizer#ignore_whitespace) option of this [`Tokenizer`] and
    /// returns itself.
    #[must_use]
    pub fn with_ignore_whitespace(mut self, ignore_whitespace: bool) -> Self {
        self.ignore_whitespace = ignore_whitespace;
        self
    }

    /// Sets the [literals](Tokenizer#literals) of this [`Tokenizer`].
    ///
    /// # Errors
    ///
    /// This function will return an error if any literal is empty.
    pub fn set_literals(&mut self, literals: &[(&'a str, &'a str)]) -> Result<(), Error> {
        validate_literals(literals)?;
        self.literals = stream::build_hashmap(literals);
        self.tree = generate_tree(&self.literals);
        Ok(())
    }

    /// Sets the [patterns](Tokenizer#patterns) of this [`Tokenizer`].
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// * there are duplicate patterns, or
    /// * any pattern regex is invalid.
    pub fn set_patterns(&mut self, patterns: &[(&'a str, &'a str)]) -> Result<(), Error> {
        self.patterns = patterns::prepare(patterns)?;
        Ok(())
    }

    /// Sets the [ignored characters](Tokenizer#ignored_characters) of this [`Tokenizer`].
    pub fn set_ignored_characters(&mut self, ignored_characters: FxHashSet<char>) {
        self.ignored_characters = ignored_characters;
    }

    /// Sets the [`ignore_whitespace`](Tokenizer#ignore_whitespace) option of this [`Tokenizer`].
    pub fn set_ignore_whitespace(&mut self, ignore_whitespace: bool) {
        self.ignore_whitespace = ignore_whitespace;
    }
}

impl Default for Tokenizer<'_> {
    fn default() -> Self {
        Self::new(&[], &[], FxHashSet::default(), false)
            .expect("an empty tokenizer should be correct")
    }
}

fn validate_literals<'a>(literals: &[(&'a str, &'a str)]) -> Result<(), Error> {
    literals
        .iter()
        .all(|(_name, token)| !token.is_empty())
        .then_some(())
        .ok_or(Error::EmptyLiteral)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn literal_validation_err() {
        assert!(matches!(
            validate_literals(&[("x", "x"), ("y", "")]),
            Err(Error::EmptyLiteral)
        ));
    }

    #[test]
    fn literal_validation_ok() {
        assert!(validate_literals(&[("x", "x"), ("", "y")]).is_ok());
    }

    #[test]
    fn fast_mode() {
        let tests: [(&[_], &[_], bool); 5] = [
            (&[], &[], true),
            (&[], &[("", "")], false),
            (&[("", "a")], &[], true),
            (&[], &[], true),
            (&[("", "a"), ("", "b"), ("", "c!")], &[], false),
        ];
        for (literals, patterns, expected) in tests {
            assert_eq!(
                Tokenizer::default()
                    .with_literals(literals)
                    .unwrap()
                    .with_patterns(patterns)
                    .unwrap()
                    .can_use_fast_mode(),
                expected
            );
        }
    }

    #[test]
    fn ignored_preparation() {
        let tests = [
            (FxHashSet::default(), false, FxHashSet::default()),
            (
                FxHashSet::from_iter(['a', 'b', 'c']),
                false,
                "abc".chars().collect(),
            ),
            (
                FxHashSet::from_iter(['a', 'b', 'c']),
                true,
                "abc \x0c\t\x0b\r\n".chars().collect(),
            ),
        ];
        for (ignored_characters, ignore_whitespace, expected) in tests {
            assert_eq!(
                Tokenizer::default()
                    .with_ignore_whitespace(ignore_whitespace)
                    .with_ignored_characters(ignored_characters)
                    .prepare_ignored(),
                expected
            );
        }
    }

    #[test]
    fn comparison() {
        let def = Tokenizer::default();
        assert_eq!(def, Tokenizer::default());
        assert_eq!(
            def,
            Tokenizer::default().with_ignored_characters(FxHashSet::default())
        );
        assert_ne!(
            def,
            Tokenizer::default().with_ignored_characters(FxHashSet::from_iter(['x']))
        );
        assert_ne!(def, Tokenizer::default().with_ignore_whitespace(true));
        assert_ne!(
            def,
            Tokenizer::default().with_literals(&[("1", "2")]).unwrap()
        );
        assert_eq!(
            def.clone().with_literals(&[("1", "2")]).unwrap(),
            Tokenizer::default().with_literals(&[("1", "2")]).unwrap()
        );
        assert_ne!(
            def,
            Tokenizer::default().with_patterns(&[("1", "2")]).unwrap()
        );
    }

    #[test]
    fn builder_equivalence() {
        let literals = [("a", "b")];
        let patterns = vec![("a", "b")];
        let ignored_chars: FxHashSet<_> = FxHashSet::from_iter(['x']);

        let mut tok1 = Tokenizer::default();
        tok1.set_ignore_whitespace(true);
        tok1.set_ignored_characters(ignored_chars.clone());
        tok1.set_literals(&literals).unwrap();
        tok1.set_patterns(&patterns).unwrap();

        let tok2 = Tokenizer::default()
            .with_ignore_whitespace(true)
            .with_ignored_characters(ignored_chars.clone())
            .with_literals(&literals)
            .unwrap()
            .with_patterns(&patterns)
            .unwrap();

        let tok3 = Tokenizer::new(&literals, &patterns, ignored_chars, true).unwrap();

        assert_eq!(tok1, tok2);
        assert_eq!(tok1, tok3);
        assert_eq!(tok2, tok3);
    }

    #[test]
    fn builder_processing_literals() {
        let mut tok = Tokenizer::default();
        assert_eq!(tok.tree, Tree::Node(FxHashMap::default()));

        assert!(tok.set_literals(&[("a", "b")]).is_ok());

        let flipped_literals = FxHashMap::from_iter([("b", "a")]);
        let expected_tree = generate_tree(&flipped_literals);
        assert_eq!(tok.literals, flipped_literals);
        assert_eq!(tok.tree, expected_tree);

        assert!(tok.set_literals(&[("a", "")]).is_err());
    }

    #[test]
    fn builder_processing_patterns() {
        let mut tok = Tokenizer::default();
        assert!(tok.patterns.is_empty());

        let pattern = ("a", r"\d+");
        assert!(tok.set_patterns(&[pattern]).is_ok());
        assert_eq!(tok.patterns.first().unwrap().1.as_str(), r"^(?:\d+)");

        assert!(tok.set_patterns(&[pattern, pattern]).is_ok());
        assert!(tok.set_patterns(&[("a", "+")]).is_err());
    }

    #[test]
    fn empty_tokenizer() {
        let tok = Tokenizer::default();

        assert!(tok.tokenize("").next().is_none());

        match tok.tokenize("source").next() {
            Some(Err(Error::BadToken(c, p))) => assert_eq!((c, p), ('s', 0)),
            _ => panic!("tokenization didn't fail with BadToken"),
        };
    }

    #[test]
    fn brainfuck_fast_tokenizer() {
        let tok = Tokenizer::default()
            .with_literals(&[
                ("add", "+"),
                ("sub", "-"),
                ("left", "<"),
                ("right", ">"),
                ("read", ","),
                ("write", "."),
                ("begin_loop", "["),
                ("end_loop", "]"),
            ])
            .unwrap();

        if let Ok(tokens) = tok.tokenize(",[.,]").collect::<Result<Vec<_>, _>>() {
            assert!(
                tokens
                    .iter()
                    .map(|t| t.name)
                    .eq("read begin_loop write read end_loop".split_whitespace())
            );
        } else {
            panic!();
        };
    }

    fn make_output<'a>(tokens: Vec<((&'a str, &'a str), usize)>) -> Vec<Token<'a>> {
        tokens
            .into_iter()
            .map(|((n, v), p)| Token::from((n, v, p)))
            .collect()
    }

    #[test]
    fn arithmetic_tokenizer() {
        let tests = [
            (
                "2 * 2 + 3 - 7",
                make_output(vec![
                    (("int", "2"), 0),
                    (("mul", "*"), 2),
                    (("int", "2"), 4),
                    (("add", "+"), 6),
                    (("int", "3"), 8),
                    (("sub", "-"), 10),
                    (("int", "7"), 12),
                ]),
            ),
            (
                "2**3",
                make_output(vec![
                    (("int", "2"), 0),
                    (("pow", "**"), 1),
                    (("int", "3"), 3),
                ]),
            ),
            (
                "-5",
                make_output(vec![(("sub", "-"), 0), (("int", "5"), 1)]),
            ),
            (
                "100 + -5",
                make_output(vec![
                    (("int", "100"), 0),
                    (("add", "+"), 4),
                    (("sub", "-"), 6),
                    (("int", "5"), 7),
                ]),
            ),
            (
                "4 - 2 ** 5 / 2",
                make_output(vec![
                    (("int", "4"), 0),
                    (("sub", "-"), 2),
                    (("int", "2"), 4),
                    (("pow", "**"), 6),
                    (("int", "5"), 9),
                    (("div", "/"), 11),
                    (("int", "2"), 13),
                ]),
            ),
            (
                "10 % 3",
                make_output(vec![
                    (("int", "10"), 0),
                    (("mod", "%"), 3),
                    (("int", "3"), 5),
                ]),
            ),
        ];

        let tok = Tokenizer::default()
            .with_literals(&[
                ("add", "+"),
                ("sub", "-"),
                ("mul", "*"),
                ("div", "/"),
                ("pow", "**"),
                ("mod", "%"),
            ])
            .unwrap()
            .with_patterns(&[common::INT.clone()])
            .unwrap();

        for (input, output) in tests {
            assert_eq!(
                tok.tokenize(input)
                    .filter_map(Result::ok)
                    .collect::<Vec<_>>(),
                output
            );
        }
    }

    #[test]
    fn line_tokenization() {
        let tok = Tokenizer::default()
            .with_ignore_whitespace(true)
            .with_patterns(&[common::WORD.clone()])
            .unwrap();
        let Ok(lines) = tok
            .tokenize_lines("a b\nc\rde")
            .collect::<Result<Vec<Vec<Token>>, _>>()
        else {
            panic!("tokenization failed");
        };
        assert_eq!(
            lines,
            vec![
                make_output(vec![(("word", "a"), 0), (("word", "b"), 2)]),
                make_output(vec![(("word", "c"), 0), (("word", "de"), 2)]),
            ]
        );
    }

    #[test]
    fn line_tokenization_fast() {
        let (a, b) = (("a", "a"), ("b", "b"));
        let tok = Tokenizer::default()
            .with_ignore_whitespace(true)
            .with_literals(&[a, b])
            .unwrap();
        let Ok(lines) = tok
            .tokenize_lines("a b\nb\ra")
            .collect::<Result<Vec<Vec<Token>>, _>>()
        else {
            panic!("tokenization failed");
        };
        assert_eq!(
            lines,
            vec![
                make_output(vec![(a, 0), (b, 2)]),
                make_output(vec![(b, 0), (a, 2)])
            ]
        );
    }

    #[test]
    fn breakpoint_tokenization() {
        let (x, y, z) = (("x", "abc"), ("y", "a"), ("z", "b"));
        let tok = Tokenizer::default().with_literals(&[x, y, z]).unwrap();
        let Ok(tokens) = tok.tokenize("ababaababc").collect::<Result<Vec<_>, _>>() else {
            panic!("tokenization failed");
        };
        assert_eq!(
            tokens,
            make_output(vec![
                (y, 0),
                (z, 1),
                (y, 2),
                (z, 3),
                (y, 4),
                (y, 5),
                (z, 6),
                (x, 7)
            ])
        );
    }

    #[test]
    fn multichar_breakpoint_tokenization() {
        let (x, y, z) = (("x", "ab"), ("y", "bc"), ("z", "abcd"));
        let tok = Tokenizer::default().with_literals(&[x, y, z]).unwrap();
        let source = "ccddbabcaabcccdcbaaabdaabcbaabbbabaaaccabcdabaabadbcacddacbddbcb";
        let tokens: Vec<_> = tok.tokenize(source).flatten().collect();
        assert_eq!(
            tokens,
            make_output(vec![
                (x, 5),
                (x, 9),
                (x, 19),
                (x, 23),
                (x, 28),
                (x, 32),
                (z, 39),
                (x, 43),
                (x, 46),
                (y, 50),
                (y, 61)
            ])
        );
    }

    #[test]
    fn fast_tokenization_with_ignoreset() {
        let (foo, bar) = (("foo", "x"), ("bar", "y"));
        let tok = Tokenizer::default()
            .with_literals(&[foo, bar])
            .unwrap()
            .with_ignored_characters(FxHashSet::from_iter(['z']));
        let Ok(tokens) = tok.tokenize("xzy").collect::<Result<Vec<_>, _>>() else {
            panic!("tokenization failed");
        };
        assert_eq!(tokens, make_output(vec![(foo, 0), (bar, 2)]));
    }

    #[test]
    fn core_tokenization_with_ignoreset() {
        let (foo, bar) = (("foo", "xz"), ("bar", "yz"));
        let tok = Tokenizer::default()
            .with_literals(&[foo, bar])
            .unwrap()
            .with_ignored_characters(FxHashSet::from_iter(['z']));
        let Ok(tokens) = tok
            .tokenize("zxzyzxzyzzzyzzxzyzzzxzz")
            .collect::<Result<Vec<_>, _>>()
        else {
            panic!("tokenization failed");
        };
        assert_eq!(
            tokens,
            make_output(vec![
                (foo, 1),
                (bar, 3),
                (foo, 5),
                (bar, 7),
                (bar, 11),
                (foo, 14),
                (bar, 16),
                (foo, 20)
            ])
        );
    }

    #[test]
    fn whitespace_tokenization() {
        let (cr, ln, space) = (("cr", "\r"), ("ln", "\n"), ("space", " "));
        let tok = Tokenizer::default()
            .with_literals(&[cr, ln, space])
            .unwrap();
        let source = " \r\n \r \n ";

        let Ok(tokens) = tok.tokenize(source).collect::<Result<Vec<_>, _>>() else {
            panic!("tokenization failed");
        };
        assert_eq!(
            tokens,
            make_output(vec![
                (space, 0),
                (cr, 1),
                (ln, 2),
                (space, 3),
                (cr, 4),
                (space, 5),
                (ln, 6),
                (space, 7)
            ])
        );

        let Ok(lines) = tok
            .tokenize_lines(source)
            .collect::<Result<Vec<Vec<Token>>, _>>()
        else {
            panic!("tokenization failed");
        };
        assert_eq!(
            lines,
            vec![
                make_output(vec![(space, 0), (cr, 1)]),
                make_output(vec![(space, 0), (cr, 1), (space, 2)]),
                make_output(vec![(space, 0)]),
            ]
        );
    }

    #[test]
    fn bad_tokenization_fast() {
        let tok = Tokenizer::default();
        let Some(Err(Error::BadToken(err_value, err_position))) = tok.tokenize("x").last() else {
            panic!("tokenization didn't fail with BadToken");
        };
        assert_eq!(err_value, 'x');
        assert_eq!(err_position, 0);
    }

    #[test]
    fn bad_tokenization_core() {
        let tok = Tokenizer::default().with_literals(&[("xy", "xy")]).unwrap();
        let Some(Err(Error::BadToken(err_value, err_position))) =
            tok.tokenize("xyz").find(Result::is_err)
        else {
            panic!("tokenization didn't fail with BadToken");
        };
        assert_eq!(err_value, 'z');
        assert_eq!(err_position, 2);
    }

    #[test]
    fn word_tokenization() {
        let tok = Tokenizer::default()
            .with_patterns(&[common::WORD.clone()])
            .unwrap();
        let tokens: Vec<_> = tok.tokenize("Hello, world!").flatten().collect();
        assert_eq!(
            tokens,
            make_output(vec![(("word", "Hello"), 0), (("word", "world"), 7)])
        );
    }

    #[test]
    fn fast_tokenization_continues_after_bad_token() {
        let (a, b) = (("a", "a"), ("b", "b"));
        let tok = Tokenizer::default().with_literals(&[a, b]).unwrap();
        let mut expected_tokens =
            make_output(vec![(a, 0), (b, 3), (b, 4), (a, 5), (a, 7)]).into_iter();
        let mut expected_error_indexes = [1, 2, 6].into_iter();

        let tokens: Vec<_> = tok.tokenize("axxbbaxa").collect();
        assert_eq!(tokens.len(), 8);

        for result in &tokens {
            match result {
                Ok(tok) => assert_eq!(tok, &expected_tokens.next().unwrap()),
                Err(Error::BadToken(c, p)) => {
                    assert_eq!((*c, *p), ('x', expected_error_indexes.next().unwrap()));
                }
                _ => panic!("unexpected error"),
            }
        }
    }

    #[test]
    fn core_tokenization_continues_after_bad_token() {
        let tok = Tokenizer::default()
            .with_literals(&[("a", "axe")])
            .unwrap()
            .with_patterns(&[("b", "box")])
            .unwrap();
        let expected_tokens = [
            ("a", "axe", 0),
            ("b", "box", 4),
            ("a", "axe", 8),
            ("a", "axe", 12),
            ("b", "box", 16),
            ("b", "box", 20),
        ];
        let expected_errors = ['&', ',', ',', '.', '.'];

        let tokens: Vec<_> = tok.tokenize("axe&box,axe,axe.box.box").collect();
        assert_eq!(tokens.len(), 11);

        for i in (0..tokens.len()).step_by(2) {
            assert_eq!(
                tokens[i].as_ref().unwrap(),
                &Token::from(expected_tokens[i / 2])
            );
        }
        for i in (1..tokens.len()).step_by(2) {
            let expected_err = (expected_errors[i / 2], i * 2 + 1);
            assert!(matches!(tokens[i], Err(Error::BadToken(c, p)) if (c, p) == expected_err));
        }
    }

    #[test]
    fn backreference_pattern() {
        let tok = Tokenizer::default()
            .with_patterns(&[("a", r"(a)b\1")])
            .unwrap();
        let out: Vec<_> = tok.tokenize("abaaba").flatten().collect();
        assert_eq!(out, make_output(vec![(("a", "aba"), 0), (("a", "aba"), 3)]));
    }

    #[test]
    fn duplicate_literal_names() {
        let (a, b) = (("a", "a"), ("a", "b"));
        let tok = Tokenizer::default().with_literals(&[a, b]).unwrap();
        let tokens: Vec<_> = tok.tokenize("ab").flatten().collect();
        assert_eq!(tokens, make_output(vec![(a, 0), (b, 1)]));
    }
}