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
use std::fmt::{Debug, Display, Formatter};
use std::ops::Range;

use dyn_clone::DynClone;
use fancy_regex::{Error, Regex};

use super::markers::PositionMarker;
use super::segments::meta::EndOfFile;
use crate::core::config::FluffConfig;
use crate::core::dialects::base::Dialect;
use crate::core::errors::{SQLLexError, ValueError};
use crate::core::parser::segments::base::{
    Segment, SegmentConstructorFn, UnlexableSegment, UnlexableSegmentNewArgs,
};
use crate::core::slice_helpers::{is_zero_slice, offset_slice};
use crate::core::templaters::base::TemplatedFile;
use crate::helpers::Boxed;

/// An element matched during lexing.
#[derive(Debug, Clone)]
pub struct LexedElement {
    raw: String,
    matcher: Box<dyn Matcher>,
}

impl LexedElement {
    pub fn new(raw: String, matcher: Box<dyn Matcher>) -> Self {
        LexedElement { raw, matcher }
    }
}

/// A LexedElement, bundled with it's position in the templated file.
#[derive(Debug)]
pub struct TemplateElement {
    raw: String,
    template_slice: Range<usize>,
    matcher: Box<dyn Matcher>,
}

impl TemplateElement {
    /// Make a TemplateElement from a LexedElement.
    pub fn from_element(element: LexedElement, template_slice: Range<usize>) -> Self {
        TemplateElement { raw: element.raw, template_slice, matcher: element.matcher }
    }

    pub fn to_segment(
        &self,
        pos_marker: PositionMarker,
        subslice: Option<Range<usize>>,
    ) -> Box<dyn Segment> {
        let slice = subslice.map_or_else(|| self.raw.clone(), |slice| self.raw[slice].to_string());
        self.matcher.construct_segment(slice, pos_marker)
    }
}

/// A class to hold matches from the lexer.
#[derive(Debug)]
pub struct LexMatch {
    forward_string: String,
    pub elements: Vec<LexedElement>,
}

#[allow(clippy::needless_arbitrary_self_type)]
impl LexMatch {
    /// A LexMatch is truthy if it contains a non-zero number of matched
    /// elements.
    pub fn is_non_empty(self: &Self) -> bool {
        !self.elements.is_empty()
    }
}

pub trait CloneMatcher {
    fn clone_box(&self) -> Box<dyn Matcher>;
}

impl<T: Matcher + DynClone> CloneMatcher for T {
    fn clone_box(&self) -> Box<dyn Matcher> {
        dyn_clone::clone(self).boxed()
    }
}

#[allow(clippy::needless_arbitrary_self_type)]
pub trait Matcher: Debug + DynClone + CloneMatcher + 'static {
    /// The name of the matcher.
    fn get_name(self: &Self) -> String;
    /// Given a string, match what we can and return the rest.
    fn match_(self: &Self, forward_string: String) -> Result<LexMatch, ValueError>;
    /// Use regex to find a substring.
    fn search(self: &Self, forward_string: &str) -> Option<Range<usize>>;

    /// Access methods that need to be implemented by the subclass.

    /// Get the sub-divider for this matcher.
    fn get_sub_divider(self: &Self) -> Option<Box<dyn Matcher>>;

    fn get_trim_post_subdivide(self: &Self) -> Option<Box<dyn Matcher>>;

    fn _subdivide(self: &Self, matched: LexedElement) -> Vec<LexedElement> {
        if let Some(sub_divider) = &self.get_sub_divider() {
            let mut elem_buff: Vec<LexedElement> = vec![];

            let mut str_buff = matched.raw;
            while !str_buff.is_empty() {
                // Iterate through subdividing as appropriate
                let div_pos = sub_divider.clone().search(&str_buff);
                if let Some(div_pos) = div_pos {
                    // Found a division
                    let trimmed_elems =
                        self._trim_match(str_buff[..div_pos.start].to_string().as_str());
                    let div_elem = LexedElement::new(
                        str_buff[div_pos.start..div_pos.end].to_string(),
                        sub_divider.clone(),
                    );
                    elem_buff.extend_from_slice(&trimmed_elems);
                    elem_buff.push(div_elem);
                    str_buff = str_buff[div_pos.end..].to_string();
                } else {
                    // No more division matches. Trim?
                    let trimmed_elems = self._trim_match(&str_buff);
                    elem_buff.extend_from_slice(&trimmed_elems);
                    break;
                }
            }
            elem_buff
        } else {
            vec![matched]
        }
    }

    /// Given a string, trim if we are allowed to.
    fn _trim_match(self: &Self, matched_str: &str) -> Vec<LexedElement> {
        let mut elem_buff = Vec::new();
        let mut content_buff = String::new();
        let mut str_buff = String::from(matched_str);

        if let Some(trim_post_subdivide) = self.get_trim_post_subdivide() {
            while !str_buff.is_empty() {
                if let Some(trim_pos) = trim_post_subdivide.clone().search(&str_buff) {
                    let start = trim_pos.start;
                    let end = trim_pos.end;

                    if start == 0 {
                        elem_buff.push(LexedElement::new(
                            str_buff[..end].to_string(),
                            trim_post_subdivide.clone(),
                        ));
                        str_buff = str_buff[end..].to_string();
                    } else if end == str_buff.len() {
                        elem_buff.push(LexedElement::new(
                            format!("{}{}", content_buff, &str_buff[..start]),
                            trim_post_subdivide.clone(),
                        ));
                        elem_buff.push(LexedElement::new(
                            str_buff[start..end].to_string(),
                            trim_post_subdivide.clone(),
                        ));
                        content_buff.clear();
                        str_buff.clear();
                    } else {
                        content_buff.push_str(&str_buff[..end]);
                        str_buff = str_buff[end..].to_string();
                    }
                } else {
                    break;
                }
            }
            if !content_buff.is_empty() || !str_buff.is_empty() {
                elem_buff.push(LexedElement::new(
                    format!("{}{}", content_buff, str_buff),
                    self.clone_box(),
                ));
            }
        }

        elem_buff
    }

    fn construct_segment(&self, _raw: String, _pos_marker: PositionMarker) -> Box<dyn Segment> {
        unimplemented!("{}", std::any::type_name::<Self>());
    }
}

dyn_clone::clone_trait_object!(Matcher);

impl Display for dyn Matcher {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "Matcher({})", self.get_name())
    }
}

/// This singleton matcher matches strings exactly.
/// This is the simplest usable matcher, but it also defines some of the
/// mechanisms for more complicated matchers, which may simply override the
/// `_match` function rather than the public `match` function.  This acts as
/// the base class for matchers.
#[derive(Clone)]
pub struct StringLexer<SegmentArgs: 'static + Clone> {
    name: &'static str,
    template: &'static str,
    segment_constructor: SegmentConstructorFn<SegmentArgs>,
    segment_args: SegmentArgs,
    sub_divider: Option<Box<dyn Matcher>>,
    trim_post_subdivide: Option<Box<dyn Matcher>>,
}

impl<SegmentArgs: Clone + Debug> StringLexer<SegmentArgs> {
    pub fn new(
        name: &'static str,
        template: &'static str,
        segment_constructor: SegmentConstructorFn<SegmentArgs>,
        segment_args: SegmentArgs,
        sub_divider: Option<Box<dyn Matcher>>,
        trim_post_subdivide: Option<Box<dyn Matcher>>,
    ) -> Self {
        StringLexer {
            name,
            template,
            segment_constructor,
            segment_args,
            sub_divider,
            trim_post_subdivide,
        }
    }

    /// The private match function. Just look for a literal string.
    fn _match(&self, forward_string: &str) -> Option<LexedElement> {
        if forward_string.starts_with(self.template) {
            Some(LexedElement { raw: self.template.to_string(), matcher: Box::new(self.clone()) })
        } else {
            None
        }
    }

    /// Given a string, trim if we are allowed to.
    fn _trim_match(&self, _matched_string: String) -> Vec<LexedElement> {
        panic!("Not implemented")
    }

    /// Given a string, subdivide if we area allowed to.
    fn _subdivide(&self, matched: LexedElement) -> Vec<LexedElement> {
        if let Some(sub_divider) = &self.sub_divider {
            let mut elem_buff: Vec<LexedElement> = vec![];

            let mut str_buff = matched.raw;
            while !str_buff.is_empty() {
                // Iterate through subdividing as appropriate
                let div_pos = self.sub_divider.clone().unwrap().search(&str_buff);
                if let Some(div_pos) = div_pos {
                    // Found a division
                    let trimmed_elems = self._trim_match(str_buff[..div_pos.start].to_string());
                    let div_elem = LexedElement::new(
                        str_buff[div_pos.start..div_pos.end].to_string(),
                        sub_divider.clone(),
                    );
                    elem_buff.extend_from_slice(&trimmed_elems);
                    elem_buff.push(div_elem);
                    str_buff = str_buff[div_pos.end..].to_string();
                } else {
                    // No more division matches. Trim?
                    let trimmed_elems = self._trim_match(str_buff);
                    elem_buff.extend_from_slice(&trimmed_elems);
                    break;
                }
            }
            elem_buff
        } else {
            vec![matched]
        }
    }
}

impl<SegmentArgs: Debug + Clone> Debug for StringLexer<SegmentArgs> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "StringLexer({})", self.name)
    }
}

impl<SegmentArgs: Clone + Debug> Display for StringLexer<SegmentArgs> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "StringLexer({})", self.template)
    }
}

impl<SegmentArgs: Clone + Debug> Matcher for StringLexer<SegmentArgs> {
    fn get_name(&self) -> String {
        self.template.to_string()
    }

    /// Given a string, match what we can and return the rest.
    fn match_(&self, forward_string: String) -> Result<LexMatch, ValueError> {
        if forward_string.len() == 0 {
            return Err(ValueError::new(String::from("Unexpected empty string!")));
        };
        let matched = self._match(&forward_string);
        match matched {
            Some(matched) => {
                let length = matched.raw.len();
                let new_elements = self._subdivide(matched);
                Ok(LexMatch {
                    forward_string: forward_string[length..].to_string(),
                    elements: new_elements,
                })
            }
            None => Ok(LexMatch { forward_string: forward_string.to_string(), elements: vec![] }),
        }
    }

    fn search(&self, forward_string: &str) -> Option<Range<usize>> {
        let start = forward_string.find(&self.template);
        if start.is_some() {
            Some(start.unwrap()..start.unwrap() + self.template.len())
        } else {
            None
        }
    }

    fn get_sub_divider(&self) -> Option<Box<dyn Matcher>> {
        self.sub_divider.clone()
    }

    fn get_trim_post_subdivide(&self) -> Option<Box<dyn Matcher>> {
        self.trim_post_subdivide.clone()
    }

    fn construct_segment(&self, raw: String, pos_marker: PositionMarker) -> Box<dyn Segment> {
        (self.segment_constructor)(&raw, &pos_marker, self.segment_args.clone())
    }
}

/// This RegexLexer matches based on regular expressions.
#[derive(Clone)]
pub struct RegexLexer<SegmentArgs: 'static + Clone> {
    name: &'static str,
    template: Regex,
    segment_constructor: SegmentConstructorFn<SegmentArgs>,
    segment_args: SegmentArgs,
    sub_divider: Option<Box<dyn Matcher>>,
    trim_post_subdivide: Option<Box<dyn Matcher>>,
}

impl<SegmentArgs: Clone + Debug> RegexLexer<SegmentArgs> {
    pub fn new(
        name: &'static str,
        regex: &str,
        segment_constructor: SegmentConstructorFn<SegmentArgs>,
        segment_args: SegmentArgs,
        sub_divider: Option<Box<dyn Matcher>>,
        trim_post_subdivide: Option<Box<dyn Matcher>>,
    ) -> Result<Self, Error> {
        Ok(RegexLexer {
            name,
            template: Regex::new(regex)?,
            segment_constructor,
            segment_args,
            sub_divider,
            trim_post_subdivide,
        })
    }

    /// Use regexes to match chunks.
    pub fn _match(&self, forward_string: &str) -> Option<LexedElement> {
        if let Ok(Some(matched)) = self.template.find(forward_string) {
            if matched.start() == 0 {
                return Some(LexedElement {
                    raw: matched.as_str().to_string(),
                    matcher: Box::new(self.clone()),
                });
            }
        }
        None
    }
}

impl<SegmentArgs: Debug + Clone> Debug for RegexLexer<SegmentArgs> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "RegexLexer({})", self.name)
    }
}

impl<SegmentArgs: Clone + Debug> Display for RegexLexer<SegmentArgs> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "RegexLexer({})", self.get_name())
    }
}

impl<SegmentArgs: Clone + Debug> Matcher for RegexLexer<SegmentArgs> {
    fn get_name(&self) -> String {
        self.template.as_str().to_string()
    }

    /// Given a string, match what we can and return the rest.
    fn match_(&self, forward_string: String) -> Result<LexMatch, ValueError> {
        if forward_string.len() == 0 {
            return Err(ValueError::new(String::from("Unexpected empty string!")));
        };
        let matched = self._match(&forward_string);
        match matched {
            Some(matched) => {
                let length = matched.raw.len();
                let new_elements = self._subdivide(matched);

                Ok(LexMatch {
                    forward_string: forward_string[length..].to_string(),
                    elements: new_elements,
                })
            }
            None => Ok(LexMatch { forward_string: forward_string.to_string(), elements: vec![] }),
        }
    }

    /// Use regex to find a substring.
    fn search(&self, forward_string: &str) -> Option<Range<usize>> {
        if let Ok(Some(matched)) = self.template.find(forward_string) {
            let match_str = matched.as_str();
            if !match_str.is_empty() {
                return Some(matched.range());
            } else {
                panic!(
                    "Zero length Lex item returned from '{}'. Report this as a bug.",
                    self.get_name()
                );
            }
        }
        None
    }

    fn get_sub_divider(&self) -> Option<Box<dyn Matcher>> {
        self.sub_divider.clone()
    }

    fn get_trim_post_subdivide(&self) -> Option<Box<dyn Matcher>> {
        self.trim_post_subdivide.clone()
    }

    fn construct_segment(&self, raw: String, pos_marker: PositionMarker) -> Box<dyn Segment> {
        (self.segment_constructor)(&raw, &pos_marker, self.segment_args.clone())
    }
}

/// The Lexer class actually does the lexing step.
pub struct Lexer {
    config: FluffConfig,
    last_resort_lexer: Box<dyn Matcher>,
}

pub enum StringOrTemplate {
    String(String),
    Template(TemplatedFile),
}

impl Lexer {
    /// Create a new lexer.
    pub fn new(config: FluffConfig, dialect: Option<Dialect>) -> Self {
        let fluff_config = FluffConfig::from_kwargs(Some(config), dialect, None);
        let last_resort_lexer = RegexLexer::new(
            "last_resort",
            "[^\t\n.]*",
            &UnlexableSegment::new,
            UnlexableSegmentNewArgs { expected: None },
            None,
            None,
        )
        .expect("Unable to create last resort lexer");
        Lexer { config: fluff_config, last_resort_lexer: Box::new(last_resort_lexer) }
    }

    pub fn lex(
        &self,
        raw: StringOrTemplate,
    ) -> Result<(Vec<Box<dyn Segment>>, Vec<SQLLexError>), ValueError> {
        // Make sure we've got a string buffer and a template regardless of what was
        // passed in.
        let (mut str_buff, template) = match raw {
            StringOrTemplate::String(s) => (s.clone(), TemplatedFile::from_string(s.to_string())),
            StringOrTemplate::Template(f) => (f.to_string(), f),
        };

        // Lex the string to get a tuple of LexedElement
        let mut element_buffer: Vec<LexedElement> = Vec::new();
        loop {
            let res =
                Lexer::lex_match(&str_buff, self.config.get_dialect().lexer_matchers()).unwrap();
            element_buffer.extend(res.elements);
            if !res.forward_string.is_empty() {
                // If we STILL can't match, then just panic out.
                let resort_res = self.last_resort_lexer.match_(str_buff.to_string())?;
                str_buff = resort_res.forward_string;
                element_buffer.extend(resort_res.elements);
            } else {
                break;
            }
        }

        // Map tuple LexedElement to list of TemplateElement.
        // This adds the template_slice to the object.
        let templated_buffer = Lexer::map_template_slices(element_buffer, template.clone());
        // Turn lexed elements into segments.
        let segments = self.elements_to_segments(templated_buffer, template);

        Ok((segments, Vec::new()))
    }

    /// Generate any lexing errors for any un-lex-ables.
    ///
    /// TODO: Taking in an iterator, also can make the typing better than use
    /// unwrap.
    fn violations_from_segments<T: Debug + Clone>(segments: Vec<impl Segment>) -> Vec<SQLLexError> {
        segments
            .into_iter()
            .filter(|s| s.is_type("unlexable"))
            .map(|s| {
                SQLLexError::new(
                    format!(
                        "Unable to lex characters: {}",
                        s.get_raw().unwrap().chars().take(10).collect::<String>()
                    ),
                    s.get_position_marker().unwrap(),
                )
            })
            .collect()
    }

    /// Iteratively match strings using the selection of sub-matchers.
    fn lex_match(
        forward_string: &str,
        lexer_matchers: &[Box<dyn Matcher>],
    ) -> Result<LexMatch, ValueError> {
        let mut elem_buff: Vec<LexedElement> = vec![];
        let mut forward_string = forward_string.to_string();

        loop {
            if forward_string.is_empty() {
                return Ok(LexMatch {
                    forward_string: forward_string.to_string(),
                    elements: elem_buff,
                });
            };

            let mut matched = false;

            for matcher in lexer_matchers {
                let res = matcher.match_(forward_string.to_string())?;
                if !res.elements.is_empty() {
                    // If we have new segments then whoop!
                    elem_buff.append(res.elements.clone().as_mut());
                    forward_string = res.forward_string;
                    // Cycle back around again and start with the top
                    // matcher again.
                    matched = true;
                    break;
                }
            }

            // We've got so far, but now can't match. Return
            if !matched {
                return Ok(LexMatch {
                    forward_string: forward_string.to_string(),
                    elements: elem_buff,
                });
            }
        }
    }

    /// Create a tuple of TemplateElement from a tuple of LexedElement.
    ///
    /// This adds slices in the templated file to the original lexed
    /// elements. We'll need this to work out the position in the source
    /// file.
    /// TODO Can this vec be turned into an iterator and return iterator to make
    /// lazy?
    fn map_template_slices(
        elements: Vec<LexedElement>,
        template: TemplatedFile,
    ) -> Vec<TemplateElement> {
        let mut idx = 0;
        let mut templated_buff: Vec<TemplateElement> = vec![];
        for element in elements {
            let template_slice = offset_slice(idx, element.raw.len());
            idx += element.raw.len();

            templated_buff
                .push(TemplateElement::from_element(element.clone(), template_slice.clone()));

            let templated_string = template.get_templated_string().unwrap();
            if templated_string[template_slice.clone()] != element.raw {
                panic!(
                    "Template and lexed elements do not match. This should never happen {:?} != \
                     {:?}",
                    element.raw, &templated_string[template_slice]
                );
            }
        }
        return templated_buff;
    }

    /// Convert a tuple of lexed elements into a tuple of segments.

    fn elements_to_segments(
        &self,
        elements: Vec<TemplateElement>,
        templated_file: TemplatedFile,
    ) -> Vec<Box<dyn Segment>> {
        let mut segments = iter_segments(elements, templated_file.clone());

        // Add an end of file marker
        let position_maker = segments
            .last()
            .map(|segment| segment.get_position_marker().unwrap())
            .unwrap_or_else(|| PositionMarker::from_point(0, 0, templated_file, None, None));
        segments.push(EndOfFile::new(position_maker));

        segments
    }
}

fn iter_segments(
    lexed_elements: Vec<TemplateElement>,
    templated_file: TemplatedFile,
) -> Vec<Box<dyn Segment>> {
    let mut result = Vec::new();
    // An index to track where we've got to in the templated file.
    let tfs_idx = 0;
    // We keep a map of previous block locations in case they re-occur.
    // let block_stack = BlockTracker()
    let templated_file_slices = templated_file.clone().sliced_file;

    // Now work out source slices, and add in template placeholders.
    for (_idx, element) in lexed_elements.into_iter().enumerate() {
        let consumed_element_length = 0;
        let mut stashed_source_idx = None;

        for (mut tfs_idx, tfs) in templated_file_slices
            .iter()
            .skip(tfs_idx)
            .enumerate()
            .map(|(i, tfs)| (i + tfs_idx, tfs))
        {
            // Is it a zero slice?
            if is_zero_slice(tfs.templated_slice.clone()) {
                let _slice = if tfs_idx + 1 < templated_file_slices.len() {
                    templated_file_slices[tfs_idx + 1].clone().into()
                } else {
                    None
                };

                _handle_zero_length_slice();

                continue;
            }

            if tfs.slice_type == "literal" {
                let tfs_offset = tfs.source_slice.start - tfs.templated_slice.start;

                // NOTE: Greater than OR EQUAL, to include the case of it matching
                // length exactly.
                if element.template_slice.end <= tfs.templated_slice.end {
                    let slice_start = stashed_source_idx.unwrap_or_else(|| {
                        element.template_slice.start + consumed_element_length + tfs_offset
                    });

                    result.push(element.to_segment(
                        PositionMarker::new(
                            slice_start..element.template_slice.end + tfs_offset,
                            element.template_slice.clone(),
                            templated_file.clone(),
                            None,
                            None,
                        ),
                        Some(consumed_element_length..element.raw.len()),
                    ));

                    // If it was an exact match, consume the templated element too.
                    if element.template_slice.end == tfs.templated_slice.end {
                        tfs_idx += 1
                    }
                    // In any case, we're done with this element. Move on
                    break;
                } else if element.template_slice.start == tfs.templated_slice.end {
                    // Did we forget to move on from the last tfs and there's
                    // overlap?
                    // NOTE: If the rest of the logic works, this should never
                    // happen.
                    // lexer_logger.debug("     NOTE: Missed Skip")  # pragma: no cover
                    continue;
                } else {
                    // This means that the current lexed element spans across
                    // multiple templated file slices.
                    // lexer_logger.debug("     Consuming whole spanning literal")
                    // This almost certainly means there's a templated element
                    // in the middle of a whole lexed element.

                    // What we do here depends on whether we're allowed to split
                    // lexed elements. This is basically only true if it's whitespace.
                    // NOTE: We should probably make this configurable on the
                    // matcher object, but for now we're going to look for the
                    // name of the lexer.
                    if element.matcher.get_name() == "whitespace" {
                        if stashed_source_idx.is_some() {
                            panic!("Found literal whitespace with stashed idx!")
                        }

                        let incremental_length =
                            tfs.templated_slice.end - element.template_slice.start;
                        result.push(element.to_segment(
                            PositionMarker::new(
                                element.template_slice.start + consumed_element_length + tfs_offset
                                    ..tfs.templated_slice.end + tfs_offset,
                                element.template_slice.clone(),
                                templated_file.clone(),
                                None,
                                None,
                            ),
                            offset_slice(consumed_element_length, incremental_length).into(),
                        ));
                    } else {
                        // We can't split it. We're going to end up yielding a segment
                        // which spans multiple slices. Stash the type, and if we haven't
                        // set the start yet, stash it too.
                        // lexer_logger.debug("     Spilling over literal slice.")
                        if stashed_source_idx.is_none() {
                            stashed_source_idx = (element.template_slice.start + tfs_idx).into();
                            // lexer_logger.debug(
                            //     "     Stashing a source start. %s", stashed_source_idx
                            // )
                            continue;
                        }
                    }
                }
            } else if matches!(tfs.slice_type.as_str(), "templated" | "block_start") {
                unimplemented!();
            }
        }
    }

    result
}

fn _handle_zero_length_slice() {
    // impl me
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::parser::segments::base::{
        CodeSegment, CodeSegmentNewArgs, NewlineSegment, NewlineSegmentNewArgs,
    };

    /// Assert that a matcher does or doesn't work on a string.
    ///
    /// The optional `matchstring` argument, which can optionally
    /// be None, allows to either test positive matching of a
    /// particular string or negative matching (that it explicitly)
    /// doesn't match.
    fn assert_matches(in_string: &str, matcher: &impl Matcher, match_string: Option<&str>) {
        let res = matcher.match_(in_string.to_string()).unwrap();
        if let Some(match_string) = match_string {
            assert_eq!(res.forward_string, in_string[match_string.len()..]);
            assert_eq!(res.elements.len(), 1);
            assert_eq!(res.elements[0].raw, match_string);
        } else {
            assert_eq!(res.forward_string, in_string);
            assert_eq!(res.elements.len(), 0);
        }
    }

    /// Test a RegexLexer with a trim_post_subdivide function.
    #[test]
    // TODO Implement Test
    fn test__parser__lexer_trim_post_subdivide() {
        let matcher: Vec<Box<dyn Matcher>> = vec![Box::new(
            RegexLexer::new(
                "function_script_terminator",
                r";\s+(?!\*)\/(?!\*)|\s+(?!\*)\/(?!\*)",
                &CodeSegment::new,
                CodeSegmentNewArgs {
                    code_type: "function_script_terminator",
                    instance_types: vec![],
                    trim_start: None,
                    trim_chars: None,
                    source_fixes: None,
                },
                Some(Box::new(StringLexer::new(
                    "semicolon",
                    ";",
                    &CodeSegment::new,
                    CodeSegmentNewArgs {
                        code_type: "semicolon",
                        instance_types: vec![],
                        trim_start: None,
                        trim_chars: None,
                        source_fixes: None,
                    },
                    None,
                    None,
                ))),
                Some(Box::new(
                    RegexLexer::new(
                        "newline",
                        r"(\n|\r\n)+",
                        &NewlineSegment::new,
                        NewlineSegmentNewArgs {},
                        None,
                        None,
                    )
                    .unwrap(),
                )),
            )
            .unwrap(),
        )];

        let res = Lexer::lex_match(";\n/\n", &matcher).unwrap();
        assert_eq!(res.elements[0].raw, ";");
        assert_eq!(res.elements[1].raw, "\n");
        assert_eq!(res.elements[2].raw, "/");
        assert_eq!(res.elements.len(), 3);
    }

    /// Test the RegexLexer.
    #[test]
    fn test__parser__lexer_regex() {
        let tests = &[
            ("fsaljk", "f", "f"),
            ("fsaljk", r"f", "f"),
            ("fsaljk", r"[fas]*", "fsa"),
            // Matching whitespace segments
            ("   \t   fsaljk", r"[^\S\r\n]*", "   \t   "),
            // Matching whitespace segments (with a newline)
            ("   \t \n  fsaljk", r"[^\S\r\n]*", "   \t "),
            // Matching quotes containing stuff
            ("'something boring'   \t \n  fsaljk", r"'[^']*'", "'something boring'"),
            (
                "' something exciting \t\n '   \t \n  fsaljk",
                r"'[^']*'",
                "' something exciting \t\n '",
            ),
        ];

        for (raw, reg, res) in tests {
            let matcher = RegexLexer::new(
                "test",
                reg,
                &CodeSegment::new,
                CodeSegmentNewArgs {
                    code_type: "",
                    instance_types: vec![],
                    trim_start: None,
                    trim_chars: None,
                    source_fixes: None,
                },
                None,
                None,
            )
            .unwrap();

            assert_matches(raw, &matcher, Some(res));
        }
    }

    /// Test the lexer string
    #[test]
    fn test__parser__lexer_string() {
        let matcher = StringLexer::new(
            "dot",
            ".",
            &CodeSegment::new,
            CodeSegmentNewArgs {
                code_type: "dot",
                instance_types: vec![],
                trim_start: None,
                trim_chars: None,
                source_fixes: None,
            },
            None,
            None,
        );
        assert_matches(".fsaljk", &matcher, Some("."));
        assert_matches("fsaljk", &matcher, None);
    }

    /// Test the RepeatedMultiMatcher
    #[test]
    fn test__parser__lexer_lex_match() {
        let matchers: Vec<Box<dyn Matcher>> = vec![
            Box::new(StringLexer::new(
                "dot",
                ".",
                &CodeSegment::new,
                CodeSegmentNewArgs {
                    code_type: "",
                    instance_types: vec![],
                    trim_start: None,
                    trim_chars: None,
                    source_fixes: None,
                },
                None,
                None,
            )),
            Box::new(
                RegexLexer::new(
                    "test",
                    r"#[^#]*#",
                    &CodeSegment::new,
                    CodeSegmentNewArgs {
                        code_type: "",
                        instance_types: vec![],
                        trim_start: None,
                        trim_chars: None,
                        source_fixes: None,
                    },
                    None,
                    None,
                )
                .unwrap(),
            ),
        ];

        let res = Lexer::lex_match("..#..#..#", &matchers).unwrap();

        assert_eq!(res.forward_string, "#");
        assert_eq!(res.elements.len(), 5);
        assert_eq!(res.elements[2].raw, "#..#");
    }
}