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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
use std::{
    collections::{HashMap, HashSet},
    fmt::Debug,
    hash::Hash,
    marker::PhantomData,
    slice::Iter,
    str::FromStr,
};

use cfgrammar::{NewlineCache, Span};
use num_traits::{AsPrimitive, PrimInt, Unsigned};
use regex::{Regex, RegexBuilder};

use lrpar::{Lexeme, Lexer, LexerTypes, NonStreamingLexer};

use crate::{
    parser::{LexParser, StartState, StartStateOperation},
    LRLexError, LexBuildResult, StartStateId,
};

#[doc(hidden)]
/// Corresponds to the options for `regex::RegexBuilder`.
#[derive(Clone)]
pub struct RegexOptions {
    pub dot_matches_new_line: bool,
    pub multi_line: bool,
    pub octal: bool,
    pub case_insensitive: Option<bool>,
    pub swap_greed: Option<bool>,
    pub ignore_whitespace: Option<bool>,
    pub unicode: Option<bool>,
    pub size_limit: Option<usize>,
    pub dfa_size_limit: Option<usize>,
    pub nest_limit: Option<u32>,
}

pub const DEFAULT_REGEX_OPTIONS: RegexOptions = RegexOptions {
    dot_matches_new_line: true,
    multi_line: true,
    octal: true,
    case_insensitive: None,
    ignore_whitespace: None,
    swap_greed: None,
    unicode: None,
    size_limit: None,
    dfa_size_limit: None,
    nest_limit: None,
};

#[derive(Debug)]
#[doc(hidden)]
pub struct Rule<StorageT> {
    /// If `Some`, the ID that lexemes created against this rule will be given (lrlex gives such
    /// rules a guaranteed unique value, though that value can be overridden by clients who need to
    /// control the ID). If `None`, then this rule specifies lexemes which should not appear in the
    /// user's input.
    pub(super) tok_id: Option<StorageT>,
    /// This rule's name. If None, then text which matches this rule will be skipped (i.e. will not
    /// create a lexeme).
    pub name: Option<String>,
    pub name_span: Span,
    pub(super) re_str: String,
    re: Regex,
    /// Id(s) of permitted start conditions for the lexer to match this rule.
    pub start_states: Vec<usize>,
    /// If Some(_), successful matching of this rule will cause the current stack of start
    /// conditions in the lexer to be updated with the enclosed value, using the designated
    /// operation.
    /// If None, successful matching causes no change to the current start condition.
    pub target_state: Option<(usize, StartStateOperation)>,
}

impl<StorageT> Rule<StorageT> {
    /// Create a new `Rule`. This interface is unstable and should only be used by code generated
    /// by lrlex itself.
    #[doc(hidden)]
    #[allow(private_interfaces)]
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        _: crate::unstable_api::InternalPublicApi,
        tok_id: Option<StorageT>,
        name: Option<String>,
        name_span: Span,
        re_str: String,
        start_states: Vec<usize>,
        target_state: Option<(usize, StartStateOperation)>,
        re_opt: &RegexOptions,
    ) -> Result<Rule<StorageT>, regex::Error> {
        let mut re = RegexBuilder::new(&format!("\\A(?:{})", re_str));
        let mut re = re
            .octal(re_opt.octal)
            .multi_line(re_opt.multi_line)
            .dot_matches_new_line(re_opt.dot_matches_new_line);

        if let Some(flag) = re_opt.ignore_whitespace {
            re = re.ignore_whitespace(flag)
        }
        if let Some(flag) = re_opt.unicode {
            re = re.unicode(flag)
        }
        if let Some(flag) = re_opt.case_insensitive {
            re = re.case_insensitive(flag)
        }
        if let Some(flag) = re_opt.swap_greed {
            re = re.swap_greed(flag)
        }
        if let Some(sz) = re_opt.size_limit {
            re = re.size_limit(sz)
        }
        if let Some(sz) = re_opt.dfa_size_limit {
            re = re.dfa_size_limit(sz)
        }
        if let Some(lim) = re_opt.nest_limit {
            re = re.nest_limit(lim)
        }

        let re = re.build()?;
        Ok(Rule {
            tok_id,
            name,
            name_span,
            re_str,
            re,
            start_states,
            target_state,
        })
    }
}

/// Methods which all lexer definitions must implement.
pub trait LexerDef<LexerTypesT: LexerTypes>
where
    usize: AsPrimitive<LexerTypesT::StorageT>,
{
    #[doc(hidden)]
    /// Instantiate a lexer from a set of `Rule`s. This is only intended to be used by compiled
    /// lexers (see `ctbuilder.rs`).
    fn from_rules(start_states: Vec<StartState>, rules: Vec<Rule<LexerTypesT::StorageT>>) -> Self
    where
        Self: Sized;

    /// Instantiate a lexer from a string (e.g. representing a `.l` file).
    fn from_str(s: &str) -> LexBuildResult<Self>
    where
        Self: Sized;

    /// Get the `Rule` at index `idx`.
    fn get_rule(&self, idx: usize) -> Option<&Rule<LexerTypesT::StorageT>>;

    /// Get the `Rule` instance associated with a particular lexeme ID. Panics if no such rule
    /// exists.
    fn get_rule_by_id(&self, tok_id: LexerTypesT::StorageT) -> &Rule<LexerTypesT::StorageT>;

    /// Get the `Rule` instance associated with a particular name.
    fn get_rule_by_name(&self, n: &str) -> Option<&Rule<LexerTypesT::StorageT>>;

    /// Set the id attribute on rules to the corresponding value in `map`. This is typically used
    /// to synchronise a parser's notion of lexeme IDs with the lexers. While doing this, it keeps
    /// track of which lexemes:
    ///   1) are defined in the lexer but not referenced by the parser
    ///   2) and referenced by the parser but not defined in the lexer
    /// and returns them as a tuple `(Option<HashSet<&str>>, Option<HashSet<&str>>)` in the order
    /// (*defined_in_lexer_missing_from_parser*, *referenced_in_parser_missing_from_lexer*). Since
    /// in most cases both sets are expected to be empty, `None` is returned to avoid a `HashSet`
    /// allocation.
    ///
    /// Lexing and parsing can continue if either set is non-empty, so it is up to the caller as to
    /// what action they take if either return set is non-empty. A non-empty set #1 is often
    /// benign: some lexers deliberately define tokens which are not used (e.g. reserving future
    /// keywords). A non-empty set #2 is more likely to be an error since there are parts of the
    /// grammar where nothing the user can input will be parseable.
    fn set_rule_ids<'a>(
        &'a mut self,
        rule_ids_map: &HashMap<&'a str, LexerTypesT::StorageT>,
    ) -> (Option<HashSet<&'a str>>, Option<HashSet<&'a str>>);

    fn set_rule_ids_spanned<'a>(
        &'a mut self,
        rule_ids_map: &HashMap<&'a str, LexerTypesT::StorageT>,
    ) -> (Option<HashSet<&'a str>>, Option<HashSet<(&'a str, Span)>>);

    /// Returns an iterator over all rules in this AST.
    fn iter_rules(&self) -> Iter<Rule<LexerTypesT::StorageT>>;

    /// Returns an iterator over all start states in this AST.
    fn iter_start_states(&self) -> Iter<StartState>;
}

/// This struct represents, in essence, a .l file in memory. From it one can produce an
/// [LRNonStreamingLexer] which actually lexes inputs.
#[derive(Debug)]
pub struct LRNonStreamingLexerDef<LexerTypesT: LexerTypes>
where
    usize: AsPrimitive<LexerTypesT::StorageT>,
{
    rules: Vec<Rule<LexerTypesT::StorageT>>,
    start_states: Vec<StartState>,
    phantom: PhantomData<LexerTypesT>,
}

impl<LexerTypesT: LexerTypes> LexerDef<LexerTypesT> for LRNonStreamingLexerDef<LexerTypesT>
where
    usize: AsPrimitive<LexerTypesT::StorageT>,
    LexerTypesT::StorageT: TryFrom<usize>,
{
    fn from_rules(
        start_states: Vec<StartState>,
        rules: Vec<Rule<LexerTypesT::StorageT>>,
    ) -> LRNonStreamingLexerDef<LexerTypesT> {
        LRNonStreamingLexerDef {
            rules,
            start_states,
            phantom: PhantomData,
        }
    }

    fn from_str(s: &str) -> LexBuildResult<LRNonStreamingLexerDef<LexerTypesT>> {
        LexParser::<LexerTypesT>::new(s.to_string()).map(|p| LRNonStreamingLexerDef {
            rules: p.rules,
            start_states: p.start_states,
            phantom: PhantomData,
        })
    }

    fn get_rule(&self, idx: usize) -> Option<&Rule<LexerTypesT::StorageT>> {
        self.rules.get(idx)
    }

    fn get_rule_by_id(&self, tok_id: LexerTypesT::StorageT) -> &Rule<LexerTypesT::StorageT> {
        self.rules
            .iter()
            .find(|r| r.tok_id == Some(tok_id))
            .unwrap()
    }

    fn get_rule_by_name(&self, n: &str) -> Option<&Rule<LexerTypesT::StorageT>> {
        self.rules.iter().find(|r| r.name.as_deref() == Some(n))
    }

    fn set_rule_ids<'a>(
        &'a mut self,
        rule_ids_map: &HashMap<&'a str, LexerTypesT::StorageT>,
    ) -> (Option<HashSet<&'a str>>, Option<HashSet<&'a str>>) {
        let (missing_from_parser, missing_from_lexer) = self.set_rule_ids_spanned(rule_ids_map);
        let missing_from_lexer =
            missing_from_lexer.map(|missing| missing.iter().map(|(name, _)| *name).collect());
        (missing_from_parser, missing_from_lexer)
    }

    fn set_rule_ids_spanned<'a>(
        &'a mut self,
        rule_ids_map: &HashMap<&'a str, LexerTypesT::StorageT>,
    ) -> (Option<HashSet<&'a str>>, Option<HashSet<(&'a str, Span)>>) {
        // Because we have to iter_mut over self.rules, we can't easily store a reference to the
        // rule's name at the same time. Instead, we store the index of each such rule and
        // recover the names later. This has the unfortunate consequence of extended the mutable
        // borrow for the rest of the 'a lifetime. To avoid that we could return idx's here.
        // But the original `set_rule_ids` invalidates indexes.  In the spirit of keeping that
        // behavior consistent, this also returns the span.
        let mut missing_from_parser_idxs = Vec::new();
        let mut rules_with_names = 0;
        for (i, r) in self.rules.iter_mut().enumerate() {
            if let Some(ref n) = r.name {
                match rule_ids_map.get(&**n) {
                    Some(tok_id) => r.tok_id = Some(*tok_id),
                    None => {
                        r.tok_id = None;
                        missing_from_parser_idxs.push(i);
                    }
                }
                rules_with_names += 1;
            }
        }

        let missing_from_parser = if missing_from_parser_idxs.is_empty() {
            None
        } else {
            let mut mfp = HashSet::with_capacity(missing_from_parser_idxs.len());
            for i in &missing_from_parser_idxs {
                mfp.insert((
                    self.rules[*i].name.as_ref().unwrap().as_str(),
                    self.rules[*i].name_span,
                ));
            }
            Some(mfp)
        };

        let missing_from_lexer =
            if rules_with_names - missing_from_parser_idxs.len() == rule_ids_map.len() {
                None
            } else {
                Some(
                    rule_ids_map
                        .keys()
                        .cloned()
                        .collect::<HashSet<&str>>()
                        .difference(
                            &self
                                .rules
                                .iter()
                                .filter(|x| x.name.is_some())
                                .map(|x| &**x.name.as_ref().unwrap())
                                .collect::<HashSet<&str>>(),
                        )
                        .cloned()
                        .collect::<HashSet<&str>>(),
                )
            };

        (missing_from_lexer, missing_from_parser)
    }

    fn iter_rules(&self) -> Iter<Rule<LexerTypesT::StorageT>> {
        self.rules.iter()
    }

    fn iter_start_states(&self) -> Iter<StartState> {
        self.start_states.iter()
    }
}

impl<
        StorageT: 'static + Debug + Hash + PrimInt + Unsigned,
        LexerTypesT: LexerTypes<StorageT = StorageT>,
    > LRNonStreamingLexerDef<LexerTypesT>
where
    usize: AsPrimitive<StorageT>,
    LexerTypesT::StorageT: TryFrom<usize>,
{
    pub fn new_with_options(
        s: &str,
        re_opts: RegexOptions,
    ) -> LexBuildResult<LRNonStreamingLexerDef<LexerTypesT>> {
        LexParser::<LexerTypesT>::new_with_regex_options(s.to_string(), re_opts).map(|p| {
            LRNonStreamingLexerDef {
                rules: p.rules,
                start_states: p.start_states,
                phantom: PhantomData,
            }
        })
    }

    /// Return an [LRNonStreamingLexer] for the `String` `s` that will lex relative to this
    /// [LRNonStreamingLexerDef].
    pub fn lexer<'lexer, 'input: 'lexer>(
        &'lexer self,
        s: &'input str,
    ) -> LRNonStreamingLexer<'lexer, 'input, LexerTypesT> {
        let mut lexemes = vec![];
        let mut i = 0;
        let mut state_stack: Vec<(usize, &StartState)> = Vec::new();
        let initial_state = match self.get_start_state_by_id(0) {
            None => {
                lexemes.push(Err(LRLexError::new(Span::new(i, i))));
                return LRNonStreamingLexer::new(s, lexemes, NewlineCache::from_str(s).unwrap());
            }
            Some(state) => state,
        };
        state_stack.push((1, initial_state));

        while i < s.len() {
            let old_i = i;
            let mut longest = 0; // Length of the longest match
            let mut longest_ridx = 0; // This is only valid iff longest != 0
            let current_state = match state_stack.last() {
                None => {
                    lexemes.push(Err(LRLexError::new(Span::new(i, i))));
                    return LRNonStreamingLexer::new(
                        s,
                        lexemes,
                        NewlineCache::from_str(s).unwrap(),
                    );
                }
                Some((_, s)) => s,
            };
            for (ridx, r) in self.iter_rules().enumerate() {
                if !Self::state_matches(current_state, &r.start_states) {
                    continue;
                }
                if let Some(m) = r.re.find(&s[old_i..]) {
                    let len = m.end();
                    // Note that by using ">", we implicitly prefer an earlier over a later rule, if
                    // both match an input of the same length.
                    if len > longest {
                        longest = len;
                        longest_ridx = ridx;
                    }
                }
            }
            if longest > 0 {
                let r = self.get_rule(longest_ridx).unwrap();
                if r.name.is_some() {
                    match r.tok_id {
                        Some(tok_id) => {
                            lexemes.push(Ok(Lexeme::new(tok_id, old_i, longest)));
                        }
                        None => {
                            lexemes.push(Err(LRLexError::new(Span::new(old_i, old_i))));
                            break;
                        }
                    }
                }
                if let Some((target_state_id, op)) = &r.target_state {
                    let state = match self.get_start_state_by_id(*target_state_id) {
                        None => {
                            // TODO: I can see an argument for lexing state to be either `None` or `Some(target_state_id)` here
                            lexemes.push(Err(LRLexError::new(Span::new(old_i, old_i))));
                            break;
                        }
                        Some(state) => state,
                    };
                    let head = state_stack.last_mut();
                    match op {
                        StartStateOperation::ReplaceStack => {
                            state_stack.clear();
                            state_stack.push((1, state));
                        }
                        StartStateOperation::Push => match head {
                            Some((count, s)) if s.id == state.id => *count += 1,
                            _ => state_stack.push((1, state)),
                        },
                        StartStateOperation::Pop => match head {
                            Some((count, _s)) if *count > 1 => {
                                *count -= 1;
                            }
                            Some(_) => {
                                state_stack.pop();
                                if state_stack.is_empty() {
                                    state_stack.push((1, initial_state));
                                }
                            }
                            None => {
                                lexemes.push(Err(LRLexError::new(Span::new(old_i, old_i))));
                                break;
                            }
                        },
                    }
                }
                i += longest;
            } else {
                lexemes.push(Err(LRLexError::new_with_lexing_state(
                    Span::new(old_i, old_i),
                    StartStateId::new(current_state.id),
                )));
                break;
            }
        }
        LRNonStreamingLexer::new(s, lexemes, NewlineCache::from_str(s).unwrap())
    }

    fn state_matches(state: &StartState, rule_states: &[usize]) -> bool {
        if rule_states.is_empty() {
            !state.exclusive
        } else {
            rule_states.contains(&state.id)
        }
    }

    fn get_start_state_by_id(&self, id: usize) -> Option<&StartState> {
        self.start_states.iter().find(|state| state.id == id)
    }
}

/// An `LRNonStreamingLexer` holds a reference to a string and can lex it into [lrpar::Lexeme]s.
/// Although the struct is tied to a single string, no guarantees are made about whether the
/// lexemes are cached or not.
pub struct LRNonStreamingLexer<'lexer, 'input: 'lexer, LexerTypesT: LexerTypes>
where
    usize: AsPrimitive<LexerTypesT::StorageT>,
    LexerTypesT::StorageT: 'static + Debug + PrimInt,
{
    s: &'input str,
    lexemes: Vec<Result<LexerTypesT::LexemeT, LRLexError>>,
    newlines: NewlineCache,
    phantom: PhantomData<(&'lexer (), LexerTypesT::StorageT)>,
}

impl<
        'lexer,
        'input: 'lexer,
        StorageT: 'static + Debug + Hash + PrimInt + Unsigned,
        LexerTypesT: LexerTypes<StorageT = StorageT>,
    > LRNonStreamingLexer<'lexer, 'input, LexerTypesT>
where
    usize: AsPrimitive<StorageT>,
{
    /// Create a new `LRNonStreamingLexer` that read in: the input `s`; and derived `lexemes` and
    /// `newlines`.
    ///
    /// Note that if one or more lexemes or newlines was not created from `s`, subsequent calls to
    /// the `LRNonStreamingLexer` may cause `panic`s.
    pub fn new(
        s: &'input str,
        lexemes: Vec<Result<LexerTypesT::LexemeT, LRLexError>>,
        newlines: NewlineCache,
    ) -> LRNonStreamingLexer<'lexer, 'input, LexerTypesT> {
        LRNonStreamingLexer {
            s,
            lexemes,
            newlines,
            phantom: PhantomData,
        }
    }
}

impl<
        'lexer,
        'input: 'lexer,
        StorageT: 'static + Debug + Hash + PrimInt + Unsigned,
        LexerTypesT: LexerTypes<StorageT = StorageT, LexErrorT = LRLexError>,
    > Lexer<LexerTypesT> for LRNonStreamingLexer<'lexer, 'input, LexerTypesT>
where
    usize: AsPrimitive<StorageT>,
{
    fn iter<'a>(
        &'a self,
    ) -> Box<dyn Iterator<Item = Result<LexerTypesT::LexemeT, LexerTypesT::LexErrorT>> + 'a> {
        Box::new(self.lexemes.iter().cloned())
    }
}

impl<'lexer, 'input: 'lexer, LexerTypesT: LexerTypes<LexErrorT = LRLexError>>
    NonStreamingLexer<'input, LexerTypesT> for LRNonStreamingLexer<'lexer, 'input, LexerTypesT>
where
    usize: AsPrimitive<LexerTypesT::StorageT>,
{
    fn span_str(&self, span: Span) -> &'input str {
        if span.end() > self.s.len() {
            panic!(
                "Span {:?} exceeds known input length {}",
                span,
                self.s.len()
            );
        }
        &self.s[span.start()..span.end()]
    }

    fn span_lines_str(&self, span: Span) -> &'input str {
        debug_assert!(span.end() >= span.start());
        if span.end() > self.s.len() {
            panic!(
                "Span {:?} exceeds known input length {}",
                span,
                self.s.len()
            );
        }

        let (st, en) = self.newlines.span_line_bytes(span);
        &self.s[st..en]
    }

    fn line_col(&self, span: Span) -> ((usize, usize), (usize, usize)) {
        debug_assert!(span.end() >= span.start());
        if span.end() > self.s.len() {
            panic!(
                "Span {:?} exceeds known input length {}",
                span,
                self.s.len()
            );
        }

        (
            self.newlines
                .byte_to_line_num_and_col_num(self.s, span.start())
                .unwrap(),
            self.newlines
                .byte_to_line_num_and_col_num(self.s, span.end())
                .unwrap(),
        )
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::{DefaultLexeme, DefaultLexerTypes};
    use lrpar::LexError;
    use std::collections::HashMap;

    #[test]
    fn test_basic() {
        let src = r"
%%
[0-9]+ 'int'
[a-zA-Z]+ 'id'
[ \t] ;"
            .to_string();
        let mut lexerdef = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).unwrap();
        let mut map = HashMap::new();
        map.insert("int", 0);
        map.insert("id", 1);
        assert_eq!(lexerdef.set_rule_ids(&map), (None, None));

        let lexemes = lexerdef
            .lexer("abc 123")
            .iter()
            .map(|x| x.unwrap())
            .collect::<Vec<_>>();
        assert_eq!(lexemes.len(), 2);
        let lex1 = lexemes[0];
        assert_eq!(lex1.tok_id(), 1u8);
        assert_eq!(lex1.span().start(), 0);
        assert_eq!(lex1.span().len(), 3);
        let lex2 = lexemes[1];
        assert_eq!(lex2.tok_id(), 0);
        assert_eq!(lex2.span().start(), 4);
        assert_eq!(lex2.span().len(), 3);
    }

    #[test]
    fn test_basic_error() {
        let src = "
%%
[0-9]+ 'int'"
            .to_string();
        let lexerdef = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).unwrap();
        match lexerdef.lexer("abc").iter().next().unwrap() {
            Ok(_) => panic!("Invalid input lexed"),
            Err(e) => {
                if e.span().start() != 0 || e.span().end() != 0 {
                    panic!("Incorrect span returned {:?}", e.span());
                }
            }
        };
    }

    #[test]
    fn test_longest_match() {
        let src = "%%
if 'IF'
[a-z]+ 'ID'
[ ] ;"
            .to_string();
        let mut lexerdef = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).unwrap();
        let mut map = HashMap::new();
        map.insert("IF", 0);
        map.insert("ID", 1);
        assert_eq!(lexerdef.set_rule_ids(&map), (None, None));

        let lexemes = lexerdef
            .lexer("iff if")
            .iter()
            .map(|x| x.unwrap())
            .collect::<Vec<DefaultLexeme<u8>>>();
        assert_eq!(lexemes.len(), 2);
        let lex1 = lexemes[0];
        assert_eq!(lex1.tok_id(), 1u8);
        assert_eq!(lex1.span().start(), 0);
        assert_eq!(lex1.span().len(), 3);
        let lex2 = lexemes[1];
        assert_eq!(lex2.tok_id(), 0);
        assert_eq!(lex2.span().start(), 4);
        assert_eq!(lex2.span().len(), 2);
    }

    #[test]
    fn test_multibyte() {
        let src = "%%
[a❤]+ 'ID'
[ ] ;"
            .to_string();
        let mut lexerdef = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).unwrap();
        let mut map = HashMap::new();
        map.insert("ID", 0u8);
        assert_eq!(lexerdef.set_rule_ids(&map), (None, None));

        let lexer = lexerdef.lexer("a ❤ a");
        let lexemes = lexer
            .iter()
            .map(|x| x.unwrap())
            .collect::<Vec<DefaultLexeme<u8>>>();
        assert_eq!(lexemes.len(), 3);
        let lex1 = lexemes[0];
        assert_eq!(lex1.span().start(), 0);
        assert_eq!(lex1.span().len(), 1);
        assert_eq!(lexer.span_str(lex1.span()), "a");
        let lex2 = lexemes[1];
        assert_eq!(lex2.span().start(), 2);
        assert_eq!(lex2.span().len(), 3);
        assert_eq!(lexer.span_str(lex2.span()), "❤");
        let lex3 = lexemes[2];
        assert_eq!(lex3.span().start(), 6);
        assert_eq!(lex3.span().len(), 1);
        assert_eq!(lexer.span_str(lex3.span()), "a");
    }

    #[test]
    fn test_line_col() {
        let src = "%%
[a-z]+ 'ID'
[ \\n] ;"
            .to_string();
        let mut lexerdef = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).unwrap();
        let mut map = HashMap::new();
        map.insert("ID", 0u8);
        assert_eq!(lexerdef.set_rule_ids(&map), (None, None));

        let lexer = lexerdef.lexer("a b c");
        let lexemes = lexer
            .iter()
            .map(|x| x.unwrap())
            .collect::<Vec<DefaultLexeme<u8>>>();
        assert_eq!(lexemes.len(), 3);
        assert_eq!(lexer.line_col(lexemes[1].span()), ((1, 3), (1, 4)));
        assert_eq!(lexer.span_lines_str(lexemes[1].span()), "a b c");
        assert_eq!(lexer.span_lines_str(lexemes[2].span()), "a b c");

        let lexer = lexerdef.lexer("a b c\n");
        let lexemes = lexer.iter().map(|x| x.unwrap()).collect::<Vec<_>>();
        assert_eq!(lexemes.len(), 3);
        assert_eq!(lexer.line_col(lexemes[1].span()), ((1, 3), (1, 4)));
        assert_eq!(lexer.span_lines_str(lexemes[1].span()), "a b c");
        assert_eq!(lexer.span_lines_str(lexemes[2].span()), "a b c");

        let lexer = lexerdef.lexer(" a\nb\n  c d");
        let lexemes = lexer.iter().map(|x| x.unwrap()).collect::<Vec<_>>();
        assert_eq!(lexemes.len(), 4);
        assert_eq!(lexer.line_col(lexemes[0].span()), ((1, 2), (1, 3)));
        assert_eq!(lexer.line_col(lexemes[1].span()), ((2, 1), (2, 2)));
        assert_eq!(lexer.line_col(lexemes[2].span()), ((3, 3), (3, 4)));
        assert_eq!(lexer.line_col(lexemes[3].span()), ((3, 5), (3, 6)));
        assert_eq!(lexer.span_lines_str(lexemes[0].span()), " a");
        assert_eq!(lexer.span_lines_str(lexemes[1].span()), "b");
        assert_eq!(lexer.span_lines_str(lexemes[2].span()), "  c d");
        assert_eq!(lexer.span_lines_str(lexemes[3].span()), "  c d");

        let mut s = Vec::new();
        let mut offs = vec![0];
        for i in 0..71 {
            offs.push(offs[i] + i + 1);
            s.push(vec!["a"; i].join(" "));
        }
        let s = s.join("\n");
        let lexer = lexerdef.lexer(&s);
        let lexemes = lexer.iter().map(|x| x.unwrap()).collect::<Vec<_>>();
        assert_eq!(lexemes.len(), offs[70]);
        assert_eq!(lexer.span_lines_str(Span::new(0, 0)), "");
        assert_eq!(lexer.span_lines_str(Span::new(0, 2)), "\na");
        assert_eq!(lexer.span_lines_str(Span::new(0, 4)), "\na\na a");
        assert_eq!(lexer.span_lines_str(Span::new(0, 7)), "\na\na a\na a a");
        assert_eq!(lexer.span_lines_str(Span::new(4, 7)), "a a\na a a");
        assert_eq!(lexer.span_lines_str(lexemes[0].span()), "a");
        assert_eq!(lexer.span_lines_str(lexemes[1].span()), "a a");
        assert_eq!(lexer.span_lines_str(lexemes[3].span()), "a a a");
        for i in 0..70 {
            assert_eq!(
                lexer.span_lines_str(lexemes[offs[i]].span()),
                vec!["a"; i + 1].join(" ")
            );
        }
    }

    #[test]
    fn test_line_col_multibyte() {
        let src = "%%
[a-z❤]+ 'ID'
[ \\n] ;"
            .to_string();
        let mut lexerdef = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).unwrap();
        let mut map = HashMap::new();
        map.insert("ID", 0u8);
        assert_eq!(lexerdef.set_rule_ids(&map), (None, None));

        let lexer = lexerdef.lexer(" a\n❤ b");
        let lexemes = lexer
            .iter()
            .map(|x| x.unwrap())
            .collect::<Vec<DefaultLexeme<u8>>>();
        assert_eq!(lexemes.len(), 3);
        assert_eq!(lexer.line_col(lexemes[0].span()), ((1, 2), (1, 3)));
        assert_eq!(lexer.line_col(lexemes[1].span()), ((2, 1), (2, 2)));
        assert_eq!(lexer.line_col(lexemes[2].span()), ((2, 3), (2, 4)));
        assert_eq!(lexer.span_lines_str(lexemes[0].span()), " a");
        assert_eq!(lexer.span_lines_str(lexemes[1].span()), "❤ b");
        assert_eq!(lexer.span_lines_str(lexemes[2].span()), "❤ b");
    }

    #[test]
    #[should_panic]
    fn test_bad_line_col() {
        let src = "%%
[a-z]+ 'ID'
[ \\n] ;"
            .to_string();
        let mut lexerdef = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).unwrap();
        let mut map = HashMap::new();
        map.insert("ID", 0u8);
        assert_eq!(lexerdef.set_rule_ids(&map), (None, None));

        let lexer = lexerdef.lexer("a b c");

        lexer.line_col(Span::new(100, 100));
    }

    #[test]
    fn test_missing_from_lexer_and_parser() {
        let src = "%%
[a-z]+ 'ID'
[ \\n] ;"
            .to_string();
        let mut lexerdef = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).unwrap();
        let mut map = HashMap::new();
        map.insert("INT", 0u8);
        let mut missing_from_lexer = HashSet::new();
        missing_from_lexer.insert("INT");
        let mut missing_from_parser = HashSet::new();
        missing_from_parser.insert("ID");
        assert_eq!(
            lexerdef.set_rule_ids(&map),
            (Some(missing_from_lexer), Some(missing_from_parser))
        );

        match lexerdef.lexer(" a ").iter().next().unwrap() {
            Ok(_) => panic!("Invalid input lexed"),
            Err(e) => {
                if e.span().start() != 1 || e.span().end() != 1 {
                    panic!("Incorrect span returned {:?}", e.span());
                }
            }
        };
    }

    #[test]
    fn test_multiline_lexeme() {
        let src = "%%
'.*' 'STR'
[ \\n] ;"
            .to_string();
        let mut lexerdef = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).unwrap();
        let mut map = HashMap::new();
        map.insert("STR", 0u8);
        assert_eq!(lexerdef.set_rule_ids(&map), (None, None));

        let lexer = lexerdef.lexer("'a\nb'\n");
        let lexemes = lexer
            .iter()
            .map(|x| x.unwrap())
            .collect::<Vec<DefaultLexeme<u8>>>();
        assert_eq!(lexemes.len(), 1);
        assert_eq!(lexer.line_col(lexemes[0].span()), ((1, 1), (2, 3)));
        assert_eq!(lexer.span_lines_str(lexemes[0].span()), "'a\nb'");
    }

    #[test]
    fn test_token_span() {
        let src = "%%
a 'A'
b 'B'
[ \\n] ;"
            .to_string();
        let lexerdef = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).unwrap();
        assert_eq!(
            lexerdef.get_rule_by_name("A").unwrap().name_span,
            Span::new(6, 7)
        );
        assert_eq!(
            lexerdef.get_rule_by_name("B").unwrap().name_span,
            Span::new(12, 13)
        );
        let anonymous_rules = lexerdef
            .iter_rules()
            .filter(|rule| rule.name.is_none())
            .collect::<Vec<_>>();
        assert_eq!(anonymous_rules[0].name_span, Span::new(21, 21));
    }

    #[test]
    fn test_token_start_states() {
        let src = "%x EXCLUSIVE_START
%s INCLUSIVE_START
%%
a 'A'
b 'B'
[ \\n] ;"
            .to_string();
        let lexerdef = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).unwrap();
        assert_eq!(
            lexerdef.get_rule_by_name("A").unwrap().name_span,
            Span::new(44, 45)
        );
        assert_eq!(
            lexerdef.get_rule_by_name("B").unwrap().name_span,
            Span::new(50, 51)
        );
    }

    #[test]
    fn test_rule_start_states() {
        let src = "%x EXCLUSIVE_START
%s INCLUSIVE_START
%%
<EXCLUSIVE_START>a 'A'
<INCLUSIVE_START>b 'B'
[ \\n] ;"
            .to_string();
        let lexerdef = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::from_str(&src).unwrap();
        let a_rule = lexerdef.get_rule_by_name("A").unwrap();
        assert_eq!(a_rule.name_span, Span::new(61, 62));
        assert_eq!(a_rule.re_str, "a");

        let b_rule = lexerdef.get_rule_by_name("B").unwrap();
        assert_eq!(b_rule.name_span, Span::new(84, 85));
        assert_eq!(b_rule.re_str, "b");
    }

    #[test]
    fn test_state_matches_regular_no_rule_states() {
        let all_states = &[
            StartState::new(0, "INITIAL", false, Span::new(0, 0)),
            StartState::new(1, "EXCLUSIVE", true, Span::new(0, 0)),
        ];
        let rule_states = vec![];
        let current_state = &all_states[0];
        let m = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::state_matches(
            current_state,
            &rule_states,
        );
        assert!(m);
    }

    #[test]
    fn test_state_matches_exclusive_no_rule_states() {
        let all_states = &[
            StartState::new(0, "INITIAL", false, Span::new(0, 0)),
            StartState::new(1, "EXCLUSIVE", true, Span::new(0, 0)),
        ];
        let rule_states = vec![];
        let current_state = &all_states[1];
        let m = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::state_matches(
            current_state,
            &rule_states,
        );
        assert!(!m);
    }

    #[test]
    fn test_state_matches_regular_matching_rule_states() {
        let all_states = &[
            StartState::new(0, "INITIAL", false, Span::new(0, 0)),
            StartState::new(1, "EXCLUSIVE", true, Span::new(0, 0)),
        ];
        let rule_states = vec![0];
        let current_state = &all_states[0];
        let m = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::state_matches(
            current_state,
            &rule_states,
        );
        assert!(m);
    }

    #[test]
    fn test_state_matches_exclusive_matching_rule_states() {
        let all_states = &[
            StartState::new(0, "INITIAL", false, Span::new(0, 0)),
            StartState::new(1, "EXCLUSIVE", true, Span::new(0, 0)),
        ];
        let rule_states = vec![1];
        let current_state = &all_states[1];
        let m = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::state_matches(
            current_state,
            &rule_states,
        );
        assert!(m);
    }

    #[test]
    fn test_state_matches_regular_other_rule_states() {
        let all_states = &[
            StartState::new(0, "INITIAL", false, Span::new(0, 0)),
            StartState::new(1, "EXCLUSIVE", true, Span::new(0, 0)),
        ];
        let rule_states = vec![1];
        let current_state = &all_states[0];
        let m = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::state_matches(
            current_state,
            &rule_states,
        );
        assert!(!m);
    }

    #[test]
    fn test_state_matches_exclusive_other_rule_states() {
        let all_states = &[
            StartState::new(0, "INITIAL", false, Span::new(0, 0)),
            StartState::new(1, "EXCLUSIVE", true, Span::new(0, 0)),
        ];
        let rule_states = vec![0];
        let current_state = &all_states[1];
        let m = LRNonStreamingLexerDef::<DefaultLexerTypes<u8>>::state_matches(
            current_state,
            &rule_states,
        );
        assert!(!m);
    }
}