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
//! Frontend AST data types & related.
use std::cell::RefCell;
use std::rc::Rc;
use std::borrow::{Borrow, Cow};
use std::collections::{HashSet, VecDeque, LinkedList};
use std::iter::FromIterator;
use std::vec;
use serde::{Serialize, Deserialize};
use crate::frontend::data::*;

///////////////////////////////////////////////////////////////////////////////
// INDEXING DATA TYPES
///////////////////////////////////////////////////////////////////////////////

#[derive(Debug, Clone, Copy, PartialEq, Hash, Serialize, Deserialize)]
pub struct CharIndex {
    pub byte_index: usize,
    pub char_index: usize,
}

impl CharIndex {
    pub fn zero() -> Self {
        CharIndex{
            byte_index: 0,
            char_index: 0,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Hash, Serialize, Deserialize)]
pub struct CharRange {
    pub start: CharIndex,
    pub end: CharIndex,
}

impl CharRange {
    pub fn join(start: Option<CharIndex>, end: Option<CharIndex>) -> Option<Self> {
        if let Some(start) = start {
            if let Some(end) = end {
                return Some(CharRange{start, end})
            }
        }
        None
    }
    pub fn new(start: CharIndex, end: CharIndex) -> Self {
        CharRange{start, end}
    }
    pub fn byte_index_range<'a>(&self, source: &'a str) -> Option<(usize, usize)> {
        fn find_utf8_end(s: &str, i: usize) -> Option<usize> {
            s.char_indices().nth(i).map(|(_, x)| x.len_utf8())
        }
        let start_byte = self.start.byte_index;
        let end_byte = self.end.byte_index;
        let real_end_byte = source
            .get(start_byte..=end_byte)
            .map(|_| end_byte)
            .or_else(|| {
                let corrected_end = find_utf8_end(source, end_byte)?;
                source
                    .get(start_byte..=corrected_end)
                    .map(|_| corrected_end)
            });
        real_end_byte.map(|l| (start_byte, l))
    }
    pub fn substrng<'a>(&self, source: &'a str) -> Option<&'a str> {
        if let Some((start, end)) = self.byte_index_range(source) {
            let sub_str = source.get(start..end).unwrap();
            Some(sub_str)
        } else {
            None
        }
    }
    pub fn into_annotated_tree<T>(self, data: T) -> Ann<T> {
        Ann {
            range: Some(self),
            data,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Ann<T> {
    range: Option<CharRange>,
    pub data: T,
}

impl<T> Ann<T> {
    pub fn unannotated(data: T) -> Self {
        let range = None;
        Ann {range, data}
    }
    pub fn new(range: CharRange, data: T) -> Self {
        Ann {range: Some(range), data}
    }
    pub fn join(range: Option<CharRange>, data: T) -> Self {
        Ann {range, data}
    }
    pub fn range(&self) -> Option<CharRange> {
        self.range
    }
    pub fn start(&self) -> Option<CharIndex> {
        if let Some(range) = self.range {
            return Some(range.start)
        }
        None
    }
    pub fn end(&self) -> Option<CharIndex> {
        if let Some(range) = self.range {
            return Some(range.end)
        }
        None
    }
    pub fn map<U>(self, f: impl Fn(T) -> U) -> Ann<U> {
        Ann {
            range: self.range,
            data: f(self.data),
        }
    }
}


///////////////////////////////////////////////////////////////////////////////
// FRONTEND
///////////////////////////////////////////////////////////////////////////////

#[derive(Debug, Clone)]
pub struct Tag<'a> {
    pub name: Ann<Atom<'a>>,
    pub parameters: Option<Vec<Node<'a>>>,
    /// Each child node generally should be an `Enclosure` (with the `CurlyBrace` kind).
    /// Until perhaps the codegen.
    pub children: Vec<Node<'a>>,
    pub rewrite_rules: Vec<RewriteRule<Node<'a>>>,
}

impl<'a> Tag<'a> {
    /// Some tag with no parameters and just children.
    pub fn new(name: Ann<&'a str>, children: Vec<Node<'a>>) -> Self {
        Tag{
            name: Ann {
                range: name.range,
                data: Cow::Borrowed(name.data)
            },
            parameters: None,
            children,
            rewrite_rules: Vec::new(),
        }
    }
    pub fn has_name(&self, name: &str) -> bool {
        return self.name() == name
    }
    pub fn insert_parameter(&mut self, value: Ann<&str>) {
        let mut args = self.parameters.clone().unwrap_or(Vec::new());
        args.push(Node::String(Ann::join(
            value.range,
            Cow::Owned(value.data.to_owned()),
        )));
        self.parameters = Some(args);
    }
    pub fn insert_unannotated_parameter(&mut self, value: &str) {
        let mut args = self.parameters.clone().unwrap_or(Vec::new());
        args.push(Node::String(Ann::unannotated(
            Cow::Owned(value.to_owned())
        )));
        self.parameters = Some(args);
    }
    // /// Short for `Tag::insert_unannotated_parameter`
    // pub fn insert_attr(&mut self, value: &str) {
    //     self.insert_unannotated_parameter(value)
    // }
    pub fn get_parameter(&self, key: &str) -> Option<Ann<Atom<'a>>> {
        self.parameters
            .as_ref()
            .unwrap_or(&Vec::new())
            .iter()
            .filter_map(Node::unwrap_string)
            .find(|x| {
                let str: &str = &x.data;
                let str = str
                    .split_once("=")
                    .map(|(x, _)| x)
                    .unwrap_or(str);
                return str.trim() == key
            })
            .map(Clone::clone)
    }
    pub fn name(&self) -> &str {
        &self.name.data
    }
    pub fn to_string(&self) -> String {
        Node::Tag(self.clone()).to_string()
    }
    pub fn is_heading_node(&self) -> bool {
        HEADING_TAG_NAMES.contains(self.name())
    }
}

#[derive(Debug, Clone, Default)]
pub struct NodeEnvironment<'a> {
    pub parents: Vec<Atom<'a>>,
}

impl<'a> NodeEnvironment<'a> {
    pub fn push_parent(&mut self, name: Atom<'a>) {
        self.parents.push(name)
    }
    pub fn is_math_env(&self) -> bool {
        self.parents
            .iter()
            .any(|x| {
                let option1 = x == INLINE_MATH_TAG;
                let option2 = BLOCK_MATH_TAGS.iter().any(|y| {
                    x == y
                });
                option1 || option2
            })
    }
    pub fn is_default_env(&self) -> bool {
        !self.is_math_env()
    }
}


///////////////////////////////////////////////////////////////////////////////
// AST
///////////////////////////////////////////////////////////////////////////////

#[derive(Debug, Clone)]
pub enum Node<'a> {
    /// The parser doesn’t emit AST `Tag` nodes. This is done in a later
    /// processing phase.
    Tag(Tag<'a>),
    /// Some identifier that may or may not be followed by square parentheses
    /// and/or a curly brace enclosure. E.g. `\name`.
    Ident(Ann<Atom<'a>>),
    /// An enclosure can be a multitude of things:
    /// * Some syntactic enclosure: 
    ///     * Curly braces
    ///     * Parentheses
    ///     * Square parentheses
    /// * Some error with it’s invalid start & end token (i.e. a opening `[` and closing `}`)
    /// * Additionally, after parsing, an enclosure can also be a fragment (i.e. a list of AST nodes)
    Enclosure(Ann<Enclosure<'a, Node<'a>>>),
    /// Some string of arbitrary characters or a single special token.
    String(Ann<Atom<'a>>),
    /// Some unbalanced token that isn’t associated with an enclosure. 
    /// In Subscript, enclosure symbols must be balanced. If the author
    /// must use such in their publications, then use the tag version. 
    InvalidToken(Ann<Atom<'a>>),
}


impl<'a> Node<'a> {
    /// Some tag with no parameters and just children.
    pub fn new_tag(name: Ann<&'a str>, children: Vec<Node<'a>>) -> Self {
        Node::Tag(Tag::new(name, children))
    }
    pub fn new_ident(str: Ann<&'a str>) -> Self {
        Node::Ident(str.map(|x| Cow::Borrowed(x)))
    }
    pub fn new_enclosure(
        range: CharRange,
        kind: EnclosureKind<'a>,
        children: Vec<Node<'a>>,
    ) -> Self {
        Node::Enclosure(Ann::new(range, Enclosure {kind, children}))
    }
    pub fn new_string(str: Ann<&'a str>) -> Self {
        Node::Ident(str.map(|x| Cow::Borrowed(x)))
    }
    pub fn unannotated_tag(name: &'a str, children: Vec<Node<'a>>) -> Self {
        Node::Tag(Tag::new(
            Ann::unannotated(name),
            children
        ))
    }
    pub fn unannotated_tag_(name: &'a str, child: Node<'a>) -> Self {
        Node::Tag(Tag::new(
            Ann::unannotated(name),
            vec![child]
        ))
    }
    pub fn unannotated_ident(str: &'a str) -> Self {
        Node::Ident(Ann::unannotated(Cow::Borrowed(str)))
    }
    pub fn unannotated_enclosure(
        kind: EnclosureKind<'a>,
        children: Vec<Node<'a>>,
    ) -> Self {
        Node::Enclosure(Ann::unannotated(Enclosure {kind, children}))
    }
    pub fn unannotated_str(str: &'a str) -> Self {
        Node::String(Ann::unannotated(Cow::Borrowed(str)))
    }
    pub fn unannotated_string(str: String) -> Self {
        Node::String(Ann::unannotated(Cow::Owned(str)))
    }

    pub fn new_fragment(nodes: Vec<Self>) -> Self {
        let data = Enclosure {
            kind: EnclosureKind::Fragment,
            children: nodes,
        };
        Node::Enclosure(Ann::unannotated(data))
    }
    pub fn is_whitespace(&self) -> bool {
        match self {
            Node::String(txt) => {
                let x: &str = &txt.data;
                x.trim().is_empty()
            },
            _ => false
        }
    }
    pub fn is_tag(&self) -> bool {
        match self {
            Node::Tag(_) => true,
            _ => false,
        }
    }
    pub fn is_ident(&self) -> bool {
        match self {
            Node::Ident(_) => true,
            _ => false,
        }
    }
    pub fn is_enclosure(&self) -> bool {
        match self {
            Node::Enclosure(_) => true,
            _ => false,
        }
    }
    pub fn is_string(&self) -> bool {
        match self {
            Node::String(_) => true,
            _ => false,
        }
    }
    pub fn is_any_enclosure(&self) -> bool {
        match self {
            Node::Enclosure(_) => true,
            _ => false,
        }
    }
    pub fn is_enclosure_of_kind(&self, k: EnclosureKind) -> bool {
        match self {
            Node::Enclosure(Ann{data: Enclosure{kind, ..}, ..}) => {
                kind == &k
            },
            _ => false,
        }
    }
    pub fn is_named_block(&self, name: &str) -> bool {
        self.unwrap_tag()
            .map(|x| *x.name.data == *name)
            .unwrap_or(false)
    }
    pub fn get_string(&'a self) -> Option<Ann<Atom<'a>>> {
        match self {
            Node::String(cow) => Some(cow.clone()),
            _ => None,
        }
    }
    pub fn get_enclosure_children(&self, kind: EnclosureKind) -> Option<&Vec<Node<'a>>> {
        match self {
            Node::Enclosure(Ann{
                data: x,
                ..
            }) if x.kind == kind => {
                Some(x.children.as_ref())
            }
            _ => None,
        }
    }
    pub fn unblock(self) -> Vec<Self> {
        match self {
            Node::Enclosure(
                Ann{data: block, ..}
            ) if block.kind == EnclosureKind::CurlyBrace => {
                block.children
            }
            x => vec![x]
        }
    }
    /// Unpacks an `Node::Enclosure` with the `Fragment` kind or
    /// returns a singleton vec.
    pub fn into_fragment(self) -> Vec<Self> {
        match self {
            Node::Enclosure(Ann{
                data: Enclosure{
                    kind: EnclosureKind::Fragment,
                    children
                },
                ..
            }) => children,
            x => vec![x]
        }
    }
    pub fn unwrap_tag(&self) -> Option<&Tag<'a>> {
        match self {
            Node::Tag(x) => Some(x),
            _ => None,
        }
    }
    pub fn unwrap_tag_mut(&mut self) -> Option<&mut Tag<'a>> {
        match self {
            Node::Tag(x) => Some(x),
            _ => None,
        }
    }
    pub fn unwrap_ident<'b>(&'b self) -> Option<&'b Ann<Atom<'a>>> {
        match self {
            Node::Ident(x) => Some(x),
            _ => None,
        }
    }
    pub fn unwrap_enclosure<'b>(&'b self) -> Option<&'b Ann<Enclosure<'a, Node<'a>>>> {
        match self {
            Node::Enclosure(x) => Some(x),
            _ => None,
        }
    }
    pub fn unwrap_curly_brace<'b>(&'b self) -> Option<&'b Vec<Node<'a>>> {
        match self {
            Node::Enclosure(
                Ann{data, ..}
            ) if data.kind == EnclosureKind::CurlyBrace => Some(&data.children),
            _ => None,
        }
    }
    pub fn unwrap_string<'b>(&'b self) -> Option<&'b Ann<Atom<'a>>> {
        match self {
            Node::String(x) => Some(x),
            _ => None,
        }
    }
    pub fn unwrap_string_mut<'b>(&'b mut self) -> Option<&'b mut Ann<Atom<'a>>> {
        match self {
            Node::String(x) => Some(x),
            _ => None,
        }
    }
    pub fn into_tag(self) -> Option<Tag<'a>> {
        match self {
            Node::Tag(x) => Some(x),
            _ => None,
        }
    }

    /// Bottom up 'node to ndoe' transformation.
    pub fn transform<F: Fn(NodeEnvironment<'a>, Node<'a>) -> Node<'a>>(
        self,
        mut env: NodeEnvironment<'a>, f: Rc<F>
    ) -> Self {
        match self {
            Node::Tag(node) => {
                env.push_parent(node.name.data.clone());
                let children = node.children
                    .into_iter()
                    .map(|x| x.transform(env.clone(), f.clone()))
                    .collect();
                let rewrite_rules = node.rewrite_rules
                    .into_iter()
                    .map(|rule| -> RewriteRule<Node<'a>> {
                        RewriteRule {
                            from: rule.from.transform(env.clone(), f.clone()),
                            to: rule.to.transform(env.clone(), f.clone()),
                        }
                    })
                    .collect();
                let node = Tag {
                    name: node.name,
                    parameters: node.parameters,
                    children,
                    rewrite_rules,
                };
                f(env.clone(), Node::Tag(node))
            }
            Node::Enclosure(node) => {
                let kind = node.data.kind;
                let range = node.range;
                let children = node.data.children
                    .into_iter()
                    .map(|x| x.transform(env.clone(), f.clone()))
                    .collect();
                let data = Enclosure{
                    kind,
                    children,
                };
                let node = Node::Enclosure(Ann::join(range, data));
                f(env.clone(), node)
            }
            node @ Node::Ident(_) => {
                f(env.clone(), node)
            }
            node @ Node::String(_) => {
                f(env.clone(), node)
            }
            node @ Node::InvalidToken(_) => {
                f(env.clone(), node)
            }
        }
    }
    // pub fn transform_unit<U: Clone, F: Fn(NodeEnvironment<'a>, &Node<'a>) -> U>(
    //     &self,
    //     mut env: NodeEnvironment<'a>, f: Rc<F>
    // ) -> Vec<U> {
    //     match self {
    //         Node::Tag(node) => {
    //             env.push_parent(node.name.data.clone());
    //             let children = node.children
    //                 .into_iter()
    //                 .flat_map(|x| x.transform_unit(env.clone(), f.clone()))
    //                 .collect::<Vec<U>>();
    //             let rewrite_rules = node.rewrite_rules
    //                 .into_iter()
    //                 .flat_map(|rule| {
    //                     let from = rule.from.transform_unit(env.clone(), f.clone());
    //                     let to = rule.to.transform_unit(env.clone(), f.clone());
    //                     vec![from, to].concat()
    //                 })
    //                 .collect::<Vec<U>>();
    //             let node = f(env.clone(), &Node::Tag(node.clone()));
    //             vec![children, rewrite_rules, node].concat()
    //         }
    //         Node::Enclosure(node) => {
    //             let kind = node.data.kind;
    //             let range = node.range;
    //             let children = node.data.children
    //                 .into_iter()
    //                 .map(|x| x.transform_unit(env.clone(), f.clone()))
    //                 .collect();
    //             let data = Enclosure{
    //                 kind,
    //                 children,
    //             };
    //             let node = Node::Enclosure(Ann::join(range, data));
    //             f(env.clone(), node)
    //         }
    //         node @ Node::Ident(_) => {
    //             f(env.clone(), node)
    //         }
    //         node @ Node::String(_) => {
    //             f(env.clone(), node)
    //         }
    //         node @ Node::InvalidToken(_) => {
    //             f(env.clone(), node)
    //         }
    //     }
    // }
    pub fn transform_mut<F: FnMut(NodeEnvironment<'a>, Node<'a>) -> Node<'a>>(
        self,
        mut env: NodeEnvironment<'a>, f: Rc<RefCell<F>>
    ) -> Self {
        match self {
            Node::Tag(node) => {
                env.push_parent(node.name.data.clone());
                let children = node.children
                    .into_iter()
                    .map(|x| x.transform_mut(env.clone(), f.clone()))
                    .collect();
                let rewrite_rules = node.rewrite_rules
                    .into_iter()
                    .map(|rule| -> RewriteRule<Node<'a>> {
                        RewriteRule {
                            from: rule.from.transform_mut(env.clone(), f.clone()),
                            to: rule.to.transform_mut(env.clone(), f.clone()),
                        }
                    })
                    .collect();
                let node = Tag {
                    name: node.name,
                    parameters: node.parameters,
                    children,
                    rewrite_rules,
                };
                (f.borrow_mut())(env.clone(), Node::Tag(node))
            }
            Node::Enclosure(node) => {
                let kind = node.data.kind;
                let range = node.range;
                let children = node.data.children
                    .into_iter()
                    .map(|x| x.transform_mut(env.clone(), f.clone()))
                    .collect();
                let data = Enclosure{
                    kind,
                    children,
                };
                let node = Node::Enclosure(Ann::join(range, data));
                (f.borrow_mut())(env.clone(), node)
            }
            node @ Node::Ident(_) => {
                (f.borrow_mut())(env.clone(), node)
            }
            node @ Node::String(_) => {
                (f.borrow_mut())(env.clone(), node)
            }
            node @ Node::InvalidToken(_) => {
                (f.borrow_mut())(env.clone(), node)
            }
        }
    }
    /// Bottom up transformation of AST child nodes within the same enclosure.
    pub fn transform_children<F>(
        self,
        f: Rc<F>
    ) -> Self where F: Fn(Vec<Node<'a>>) -> Vec<Node<'a>> {
        match self {
            Node::Tag(node) => {
                let children = node.children
                    .into_iter()
                    .map(|x| -> Node {
                        x.transform_children(f.clone())
                    })
                    .collect::<Vec<_>>();
                let children = f(children);
                let rewrite_rules = node.rewrite_rules
                    .into_iter()
                    .map(|rule| -> RewriteRule<Node<'a>> {
                        RewriteRule {
                            from: rule.from.transform_children(f.clone()),
                            to: rule.to.transform_children(f.clone()),
                        }
                    })
                    .collect();
                let parameters = node.parameters;
                let node = Tag {
                    name: node.name,
                    parameters,
                    children,
                    rewrite_rules,
                };
                Node::Tag(node)
            }
            Node::Enclosure(node) => {
                let range = node.range();
                let children = node.data.children
                    .into_iter()
                    .map(|x| x.transform_children(f.clone()))
                    .collect();
                let children = (f)(children);
                let data = Enclosure{
                    kind: node.data.kind,
                    children,
                };
                Node::Enclosure(Ann::join(range, data))
            }
            node @ Node::Ident(_) => node,
            node @ Node::String(_) => node,
            node @ Node::InvalidToken(_) => node,
        }
    }
    /// For syntax highlighting VIA the compiler frontend.
    pub fn into_highlight_ranges(
        self,
        nesting: Vec<Atom<'a>>,
        binder: Option<Atom<'a>>,
    ) -> Vec<Highlight<'a>> {
        match self {
            Node::Tag(..) => {
                unimplemented!()
            }
            Node::Enclosure(node) => {
                let is_fragment = node.data.kind == EnclosureKind::Fragment;
                let range = node.range;
                let kind = match node.data.kind {
                    EnclosureKind::CurlyBrace => HighlightKind::CurlyBrace,
                    EnclosureKind::SquareParen => HighlightKind::SquareParen,
                    EnclosureKind::Parens => HighlightKind::Parens,
                    EnclosureKind::Fragment => HighlightKind::Fragment,
                    EnclosureKind::Error{open, close} => HighlightKind::Error{
                        open: open,
                        close: close,
                    },
                };
                let mut last_ident: Option<Atom> = None;
                let mut child_nesting = nesting.clone();
                if let Some(binder) = binder.clone() {
                    child_nesting.push(binder);
                }
                let children = node.data.children
                    .into_iter()
                    .flat_map(|x| {
                        if x.is_ident() {
                            let ident = x.unwrap_ident().unwrap().clone();
                            last_ident = Some(ident.data);
                        }
                        if x.is_string() && !x.is_whitespace() {
                            last_ident = None;
                        }
                        x.into_highlight_ranges(child_nesting.clone(), last_ident.clone())
                    })
                    .collect::<Vec<_>>();
                let highlight = Highlight {
                    kind,
                    range,
                    binder: binder.clone(),
                    nesting,
                };
                if is_fragment {
                    children
                } else {
                    let mut xs = vec![highlight];
                    xs.extend(children);
                    xs
                }
            }
            Node::Ident(value) => {
                let range = value.range;
                let highlight = Highlight {
                    kind: HighlightKind::Ident(value.data),
                    range,
                    binder: binder.clone(),
                    nesting,
                };
                vec![highlight]
            }
            Node::InvalidToken(value) => {
                let range = value.range;
                let highlight = Highlight {
                    kind: HighlightKind::InvalidToken(value.data),
                    range,
                    binder: binder.clone(),
                    nesting,
                };
                vec![highlight]
            }
            Node::String(value) => Vec::new(),
        }
    }

    pub fn to_string(&self) -> String {
        fn pack<'a>(x: Cow<'a, str>) -> String {
            match x {
                Cow::Borrowed(x) => String::from(x),
                Cow::Owned(x) => x,
            }
        }
        fn ident<'a>(x: Cow<'a, str>) -> String {
            let mut txt = pack(x);
            txt.insert(0, '\\');
            txt
        }
        fn enclosure<'a>(
            start: Atom<'a>,
            content: String,
            end: Option<Atom<'a>>,
        ) -> String {
            let end = end
                .map(|x| x.to_string())
                .unwrap_or(String::new());
            format!("{}{}{}", start, content, end)
        }
        fn enclosure_str<'a>(
            start: &str,
            content: String,
            end: &str,
        ) -> String {
            format!("{}{}{}", start, content, end)
        }
        match self {
            Node::Tag(tag) => {
                let name = pack(tag.name.data.clone());
                let children = tag.children
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<_>>()
                    .join("");
                format!("\\{}{}", name, children)
            }
            Node::Enclosure(Ann{data, ..}) => {
                let children = data.children
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<_>>()
                    .join("");
                match data.kind.clone() {
                    EnclosureKind::Fragment => {
                        children
                    }
                    EnclosureKind::CurlyBrace => {
                        enclosure_str("{", children, "}")
                    }
                    EnclosureKind::Parens => {
                        enclosure_str("(", children, ")")
                    }
                    EnclosureKind::SquareParen => {
                        enclosure_str("[", children, "]")
                    }
                    EnclosureKind::Error{open, close} => {
                        enclosure(open, children, close)
                    }
                }
            }
            Node::Ident(x) => ident(x.data.clone()),
            Node::String(x) => pack(x.data.clone()),
            Node::InvalidToken(x) => pack(x.data.clone()),
        }
    }
    pub fn syntactically_equal(&self, other: &Self) -> bool {
        match (self, other) {
            (Node::Tag(x1), Node::Tag(x2)) => {
                let check1 = x1.name() == x2.name();
                let check2 = x1.children.len() == x2.children.len();
                if check1 && check2 {
                    return x1.children
                        .iter()
                        .zip(x2.children.iter())
                        .all(|(x, y)| {
                            x.syntactically_equal(y)
                        })
                }
                false
            }
            (Node::Enclosure(x1), Node::Enclosure(x2)) => {
                let check1 = x1.data.kind == x2.data.kind;
                let check2 = x1.data.children.len() == x2.data.children.len();
                if check1 && check2 {
                    return x1.data.children
                        .iter()
                        .zip(x2.data.children.iter())
                        .all(|(x, y)| {
                            x.syntactically_equal(y)
                        })
                }
                false
            }
            (Node::Ident(x1), Node::Ident(x2)) => {
                &x1.data == &x2.data
            }
            (Node::String(x1), Node::String(x2)) => {
                &x1.data == &x2.data
            }
            (Node::InvalidToken(x1), Node::InvalidToken(x2)) => {
                &x1.data == &x2.data
            }
            (_, _) => unimplemented!()
        }
    }
    /// Push to a fragment or tag node.
    /// TODO: Should we also push to any `EnclosureKind`?
    pub fn push_child(self, child: Self) -> Self {
        match self {
            Node::Enclosure(mut node) if node.data.is_fragment() => {
                node.data.children.push(child);
                Node::Enclosure(node)
            }
            Node::Tag(mut tag) => {
                tag.children.push(child);
                Node::Tag(tag)
            }
            x => Node::new_fragment(vec![x, child])
        }
    }
}


///////////////////////////////////////////////////////////////////////////////
// HIGHLIGHTER RELATED DATA TYPES
///////////////////////////////////////////////////////////////////////////////

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Highlight<'a> {
    pub range: Option<CharRange>,
    pub kind: HighlightKind<'a>,
    pub binder: Option<Atom<'a>>,
    pub nesting: Vec<Atom<'a>>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum HighlightKind<'a> {
    CurlyBrace,
    SquareParen,
    Parens,
    Fragment,
    Error {
        open: Atom<'a>,
        close: Option<Atom<'a>>,
    },
    InvalidToken(Atom<'a>),
    Ident(Atom<'a>),
}