hyperast 0.2.0

Temporal code analyses at scale
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
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
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
//! Tree Generators
//!
//! This module contains facilities to help you build an HyperAST.
//! - [`TreeGen::make`] is where a subtree is pushed in the HyperAST
//!   - You should also use [`crate::store::nodes::legion::NodeStore::prepare_insertion`]
//!     to insert subtrees in the HyperAST while deduplicating identical ones
//! - To visit parsers with a zipper/cursor interface you should implement [`ZippedTreeGen`]
//!   - [`crate::parser::TreeCursor`] should be implemented to wrap you parser's interface
//!
//!
//! ## Important Note
//! To make code analysis incremental in the HyperAST,
//! we locally persist locally derived values, we call them metadata.
//! To save memory, we also deduplicate identical nodes using the type, label and children of a subtree.
//! In other word, in the HyperAST, you store Metadata (derived values) along subtrees of the HyperAST,
//! and deduplicate subtree using identifying data.
//! To ensure derived data are unique per subtree,
//! metadata should only be derived from local identifying values.

pub mod parser;

use std::fmt::Debug;

use crate::{hashed::NodeHashs, nodes::Space};

use self::parser::Visibility;

pub type Spaces = Vec<Space>;

/// Builder of a node for the hyperAST
pub trait Accumulator {
    type Node;
    fn push(&mut self, full_node: Self::Node);
}

pub trait WithByteRange {
    fn has_children(&self) -> bool {
        todo!()
    }
    fn begin_byte(&self) -> usize;
    fn end_byte(&self) -> usize;
}

// TODO merge with other node traits?
pub trait WithChildren<Id: Clone> {
    fn children(&self) -> &[Id];
    fn child_count(&self) -> usize {
        let cs = self.children();
        cs.len()
    }
    fn child(&self, idx: usize) -> Option<Id> {
        let cs = self.children();
        cs.get(idx).cloned()
    }
}
// TODO merge with other node traits?
pub trait WithRole<R> {
    fn role_at(&self, idx: usize) -> Option<R>;
}

pub trait WithLabel {
    type L: Clone + AsRef<str>;
}

pub struct BasicAccumulator<T, Id> {
    pub kind: T,
    pub children: Vec<Id>,
}

impl<T, Id> BasicAccumulator<T, Id> {
    pub fn new(kind: T) -> Self {
        Self {
            kind,
            children: vec![],
        }
    }

    #[cfg(feature = "legion")]
    pub fn add_primary<L, K>(
        self,
        dyn_builder: &mut impl crate::store::nodes::EntityBuilder,
        interned_kind: K,
        label_id: Option<L>,
    ) where
        K: 'static + std::marker::Send + std::marker::Sync,
        L: 'static + std::marker::Send + std::marker::Sync,
        Id: 'static + std::marker::Send + std::marker::Sync + Eq,
    {
        // TODO better handle the interneds
        // TODO the "static" interning should be hanled more specifically
        dyn_builder.add(interned_kind);
        if let Some(label_id) = label_id {
            dyn_builder.add(label_id);
        }

        let children = self.children;
        if children.len() == 1 {
            let Ok(cs) = children.try_into() else {
                unreachable!();
            };
            dyn_builder.add(crate::store::nodes::legion::compo::CS0::<_, 1>(cs));
        } else if children.len() == 2 {
            let Ok(cs) = children.try_into() else {
                unreachable!();
            };
            dyn_builder.add(crate::store::nodes::legion::compo::CS0::<_, 2>(cs));
        } else if !children.is_empty() {
            // TODO make global components, at least for primaries.
            dyn_builder.add(crate::store::nodes::legion::compo::CS(
                children.into_boxed_slice(),
            ));
        }
    }
}

#[cfg(feature = "legion")]
pub fn add_cs_no_spaces(
    dyn_builder: &mut impl crate::store::nodes::EntityBuilder,
    children: Vec<crate::store::nodes::legion::NodeIdentifier>,
) {
    use crate::store::nodes::legion::compo;
    if children.len() == 1 {
        let Ok(cs) = children.try_into() else {
            unreachable!();
        };
        dyn_builder.add(compo::NoSpacesCS0::<_, 1>(cs));
    } else if children.len() == 2 {
        let Ok(cs) = children.try_into() else {
            unreachable!();
        };
        dyn_builder.add(compo::NoSpacesCS0::<_, 2>(cs));
    } else if !children.is_empty() {
        // TODO make global components, at least for primaries.
        dyn_builder.add(compo::NoSpacesCS(children.into_boxed_slice()));
    }
}

impl<T: Debug, Id> Debug for BasicAccumulator<T, Id> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BasicAccumulator")
            .field("kind", &self.kind)
            .field("children", &self.children.len())
            .finish()
    }
}

impl<T, Id> Accumulator for BasicAccumulator<T, Id> {
    type Node = Id;
    fn push(&mut self, node: Self::Node) {
        self.children.push(node);
    }
}

/// Builder of a node aware of its indentation for the hyperAST
pub trait AccIndentation: Accumulator {
    fn indentation<'a>(&'a self) -> &'a Spaces;
}

#[derive(Default, Debug, Clone, Copy)]
pub struct SubTreeMetrics<U> {
    pub hashs: U,
    pub size: u32,
    pub height: u32,
    pub size_no_spaces: u32,
    /// should include lines inside labels
    pub line_count: u16, // TODO u16 is definitely not enough at the directory level e.g. 1.6MLoCs for Hadoop
                         // pub byte_len: u32,
}

impl<U: NodeHashs> SubTreeMetrics<U> {
    pub fn acc(&mut self, other: Self) {
        self.height = self.height.max(other.height);
        self.size += other.size;
        self.size_no_spaces += other.size_no_spaces;
        self.hashs.acc(&other.hashs);
        self.line_count = self.line_count.saturating_add(other.line_count);
    }
}

impl<U> SubTreeMetrics<U> {
    pub fn map_hashs<V>(self, f: impl Fn(U) -> V) -> SubTreeMetrics<V> {
        SubTreeMetrics {
            hashs: f(self.hashs),
            size: self.size,
            height: self.height,
            size_no_spaces: self.size_no_spaces,
            line_count: self.line_count,
        }
    }

    #[must_use]
    #[cfg(feature = "legion")]
    pub fn add_md_metrics(
        self,
        dyn_builder: &mut impl crate::store::nodes::EntityBuilder,
        children_is_empty: bool,
    ) -> U {
        use crate::store::nodes::legion::compo;
        if !children_is_empty {
            dyn_builder.add(compo::Size(self.size));
            dyn_builder.add(compo::SizeNoSpaces(self.size_no_spaces));
            dyn_builder.add(compo::Height(self.height));
        }

        if self.line_count > 0 {
            dyn_builder.add(compo::LineCount(self.line_count));
        }

        self.hashs
    }
}

impl<U: crate::hashed::ComputableNodeHashs> SubTreeMetrics<U> {
    pub fn finalize<K: ?Sized + std::hash::Hash, L: ?Sized + std::hash::Hash>(
        self,
        k: &K,
        l: &L,
        line_count: u16,
    ) -> SubTreeMetrics<crate::hashed::HashesBuilder<U>> {
        let size_no_spaces = self.size_no_spaces + 1;
        use crate::hashed::IndexingHashBuilder;
        let hashs = crate::hashed::HashesBuilder::new(self.hashs, k, l, size_no_spaces);
        SubTreeMetrics {
            hashs,
            size: self.size + 1,
            height: self.height + 1,
            size_no_spaces,
            line_count: self.line_count + line_count,
        }
    }
}

pub trait GlobalData {
    fn up(&mut self);
    fn right(&mut self);
    fn down(&mut self);
}

#[derive(Debug, Clone, Copy)]
pub struct BasicGlobalData {
    depth: usize,
    /// preorder position
    position: usize,
}

impl Default for BasicGlobalData {
    fn default() -> Self {
        Self {
            depth: 1,
            position: 0,
        }
    }
}

impl GlobalData for BasicGlobalData {
    fn up(&mut self) {
        self.depth -= 1;
        // TODO fix, there are issues the depth count is too big, I am probably missing a up somewhere
    }

    fn right(&mut self) {
        self.position += 1;
        // self.depth -= 1;
    }

    /// goto the first children
    fn down(&mut self) {
        self.position += 1;
        self.depth += 1;
    }
}
pub trait TotalBytesGlobalData {
    fn set_sum_byte_length(&mut self, sum_byte_length: usize);
}

#[derive(Debug, Clone, Copy)]
pub struct TextedGlobalData<'a, GD = BasicGlobalData> {
    text: &'a [u8],
    inner: GD,
}

impl<'a, GD> TextedGlobalData<'a, GD> {
    pub fn new(inner: GD, text: &'a [u8]) -> Self {
        Self { text, inner }
    }
    pub fn text(self) -> &'a [u8] {
        self.text
    }
}

impl<'a, GD: GlobalData> GlobalData for TextedGlobalData<'a, GD> {
    fn up(&mut self) {
        self.inner.up();
    }

    fn right(&mut self) {
        self.inner.right();
    }

    /// goto the first children
    fn down(&mut self) {
        self.inner.down();
    }
}

#[derive(Debug, Clone, Copy)]
pub struct SpacedGlobalData<'a, GD = BasicGlobalData> {
    sum_byte_length: usize,
    inner: TextedGlobalData<'a, GD>,
}

impl<'a, GD> std::ops::Deref for SpacedGlobalData<'a, GD> {
    type Target = GD;

    fn deref(&self) -> &Self::Target {
        &self.inner.inner
    }
}
impl<'a, GD> std::ops::DerefMut for SpacedGlobalData<'a, GD> {

    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner.inner
    }
}

impl<'a, GD> From<TextedGlobalData<'a, GD>> for SpacedGlobalData<'a, GD> {
    fn from(inner: TextedGlobalData<'a, GD>) -> Self {
        Self {
            sum_byte_length: 0,
            inner,
        }
    }
}
impl<'a, GD: Clone> SpacedGlobalData<'a, GD> {
    pub fn simple(&self) -> GD {
        self.inner.inner.clone()
    }
}

impl<'a, GD: Clone> TextedGlobalData<'a, GD> {
    pub fn simple(&self) -> GD {
        self.inner.clone()
    }
}

impl<'a, GD> SpacedGlobalData<'a, GD> {
    pub fn sum_byte_length(&self) -> usize {
        self.sum_byte_length
    }
}

impl<'a, GD> TotalBytesGlobalData for SpacedGlobalData<'a, GD> {
    fn set_sum_byte_length(&mut self, sum_byte_length: usize) {
        // assert!(self.sum_byte_length <= sum_byte_length);
        assert!(
            self.sum_byte_length <= sum_byte_length,
            "new byte offset is smaller: {} > {}",
            self.sum_byte_length,
            sum_byte_length
        );
        self.sum_byte_length = sum_byte_length;
    }
}

impl<'a, GD: GlobalData> GlobalData for SpacedGlobalData<'a, GD> {
    fn up(&mut self) {
        self.inner.up();
    }

    fn right(&mut self) {
        self.inner.right();
    }

    /// goto the first children
    fn down(&mut self) {
        self.inner.down();
    }
}

mod global_stats {
    use super::*;
    #[derive(Debug, Clone)]
    pub struct StatsGlobalData<GD = BasicGlobalData> {
        #[cfg(feature = "subtree-stats")]
        pub height_counts: Vec<u32>,
        inner: GD,
    }

    impl<GD: Default> Default for StatsGlobalData<GD> {
        fn default() -> Self {
            Self::new(Default::default())
        }
    }

    impl<GD: TotalBytesGlobalData> TotalBytesGlobalData for StatsGlobalData<GD> {
        fn set_sum_byte_length(&mut self, sum_byte_length: usize) {
            self.inner.set_sum_byte_length(sum_byte_length)
        }
    }

    impl<GD: GlobalData> GlobalData for StatsGlobalData<GD> {
        fn up(&mut self) {
            self.inner.up();
        }

        fn right(&mut self) {
            self.inner.right();
        }

        fn down(&mut self) {
            self.inner.down();
        }
    }

    impl<GD> StatsGlobalData<GD> {
        fn new(inner: GD) -> Self {
            Self {
                #[cfg(feature = "subtree-stats")]
                height_counts: Vec::with_capacity(30),
                inner,
            }
        }
    }

    impl StatsGlobalData<SpacedGlobalData<'_>> {
        pub fn sum_byte_length(&self) -> usize {
            self.inner.sum_byte_length()
        }
    }
}

pub use global_stats::StatsGlobalData;

/// Primary trait to implement to generate AST.
pub trait TreeGen {
    /// Container holding data waiting to be added to the HyperAST
    /// Note: needs WithByteRange to handle hidden node properly, it allows to go back up without using the cursor. When Treesitter is "fixed" change that
    type Acc: AccIndentation + WithByteRange;
    /// Container holding global data used during generation.
    ///
    /// Useful for transient data needed during generation,
    /// this way you avoid cluttering [TreeGen::Acc].
    ///
    /// WARN make sure it does not leaks contextual data in subtrees.
    type Global: GlobalData;
    fn make(
        &mut self,
        global: &mut Self::Global,
        acc: <Self as TreeGen>::Acc,
        label: Option<String>,
    ) -> <<Self as TreeGen>::Acc as Accumulator>::Node;
}

#[derive(Debug)]
pub struct Parents<Acc>(Vec<P<Acc>>);
impl<Acc> From<Acc> for Parents<Acc> {
    fn from(value: Acc) -> Self {
        Self::new(P::Visible(value))
    }
}

#[derive(Debug)]
enum P<Acc> {
    ManualyHidden,
    BothHidden,
    Hidden(Acc),
    Visible(Acc),
}

impl<Acc> P<Acc> {
    fn s(&self) -> &str {
        match self {
            P::ManualyHidden => "ManualyHidden",
            P::BothHidden => "BothHidden",
            P::Hidden(_) => "Hidden",
            P::Visible(_) => "Visible",
        }
    }
    fn is_both_hidden(&self) -> bool {
        match self {
            P::BothHidden => true,
            _ => false,
        }
    }
    fn unwrap(self) -> Acc {
        match self {
            P::ManualyHidden => panic!(),
            P::BothHidden => panic!(),
            P::Hidden(p) => p,
            P::Visible(p) => p,
        }
    }
    fn as_ref(&self) -> P<&Acc> {
        match self {
            P::ManualyHidden => P::ManualyHidden,
            P::BothHidden => P::BothHidden,
            P::Hidden(t) => P::Hidden(t),
            P::Visible(t) => P::Visible(t),
        }
    }
    fn as_mut(&mut self) -> P<&mut Acc> {
        match self {
            P::ManualyHidden => P::ManualyHidden,
            P::BothHidden => P::BothHidden,
            P::Hidden(t) => P::Hidden(t),
            P::Visible(t) => P::Visible(t),
        }
    }
}

impl<Acc> P<Acc> {
    fn ok(self) -> Option<Acc> {
        match self {
            P::ManualyHidden => None,
            P::BothHidden => None,
            P::Hidden(p) => Some(p),
            P::Visible(p) => Some(p),
        }
    }
    fn visibility(self) -> Option<(Visibility, Acc)> {
        match self {
            P::ManualyHidden => None,
            P::BothHidden => None,
            P::Hidden(a) => Some((Visibility::Hidden, a)),
            P::Visible(a) => Some((Visibility::Visible, a)),
        }
    }
}

impl<Acc> Parents<Acc> {
    fn new(value: P<Acc>) -> Self {
        Self(vec![value])
    }
    pub fn finalize(mut self) -> Acc {
        assert_eq!(self.0.len(), 1);
        self.0.pop().unwrap().unwrap()
    }
    fn push(&mut self, value: P<Acc>) {
        self.0.push(value)
    }
    fn pop(&mut self) -> Option<P<Acc>> {
        self.0.pop()
    }
    pub fn parent(&self) -> Option<&Acc> {
        self.0.iter().rev().find_map(|x| x.as_ref().ok())
    }
    fn parent_mut(&mut self) -> Option<&mut Acc> {
        self.0.iter_mut().rev().find_map(|x| x.as_mut().ok())
    }
    fn parent_mut_with_vis(&mut self) -> Option<(Visibility, &mut Acc)> {
        self.0
            .iter_mut()
            .rev()
            .find_map(|x| x.as_mut().visibility())
    }

    fn len(&self) -> usize {
        self.0.len()
    }
}

pub struct RoleAcc<R> {
    pub current: Option<R>,
    pub roles: Vec<R>,
    pub offsets: Vec<u8>,
}

impl<R> Default for RoleAcc<R> {
    fn default() -> Self {
        Self {
            current: None,
            roles: Default::default(),
            offsets: Default::default(),
        }
    }
}

impl<R> RoleAcc<R> {
    pub fn acc(&mut self, role: R, o: usize) {
        use num::ToPrimitive;
        if let Some(o) = o.to_u8() {
            self.roles.push(role);
            self.offsets.push(o);
        } else {
            log::warn!("overflowed 255 offseted role...");
            debug_assert!(false);
            // TODO could increase to u16,
            // at least on some variants.
            // TODO could also use the repeat nodes to break down nodes with way to many children...
        }
    }

    #[cfg(feature = "legion")]
    pub fn add_md(self, dyn_builder: &mut impl crate::store::nodes::EntityBuilder)
    where
        R: 'static + std::marker::Send + std::marker::Sync,
    {
        debug_assert!(self.current.is_none());
        if self.roles.len() > 0 {
            dyn_builder.add(self.roles.into_boxed_slice());
            use crate::store::nodes::legion::compo;
            dyn_builder.add(compo::RoleOffsets(self.offsets.into_boxed_slice()));
        }
    }
}

#[cfg(feature = "legion")]
pub fn add_md_precomp_queries(
    dyn_builder: &mut impl crate::store::nodes::EntityBuilder,
    precomp_queries: PrecompQueries,
) {
    use crate::store::nodes::legion::compo;
    if precomp_queries > 0 {
        dyn_builder.add(compo::Precomp(precomp_queries));
    } else {
        dyn_builder.add(compo::PrecompFlag);
    }
}

#[cfg(feature = "ts")]
pub mod zipped;
#[cfg(feature = "ts")]
pub use zipped::PreResult;
#[cfg(feature = "ts")]
pub use zipped::ZippedTreeGen;

/// utils for generating code with tree-sitter
#[cfg(feature = "ts")]
pub mod utils_ts {

    pub trait TsEnableTS: crate::types::ETypeStore
    where
        Self::Ty2: TsType,
    {
        fn obtain_type<N: crate::tree_gen::parser::NodeWithU16TypeId>(n: &N) -> Self::Ty2;
    }

    pub trait TsType: crate::types::HyperType + Copy {
        fn spaces() -> Self;
        fn is_repeat(&self) -> bool;
    }

    pub fn tree_sitter_parse(
        text: &[u8],
        language: &tree_sitter::Language,
    ) -> Result<tree_sitter::Tree, tree_sitter::Tree> {
        let mut parser = tree_sitter::Parser::new();
        // TODO see if a timeout of a cancellation flag could be useful
        // const MINUTE: u64 = 60 * 1000 * 1000;
        // parser.set_timeout_micros(timeout_micros);
        // parser.set_cancellation_flag(flag);
        parser.set_language(language).unwrap();
        let tree = parser.parse(text, None).unwrap();
        if tree.root_node().has_error() {
            Err(tree)
        } else {
            Ok(tree)
        }
    }

    use super::parser::Visibility;

    #[repr(C)]
    #[derive(Debug, Copy, Clone)]
    #[allow(dead_code)] // NOTE: created by tree sitter
    pub(crate) enum TreeCursorStep {
        TreeCursorStepNone,
        TreeCursorStepHidden,
        TreeCursorStepVisible,
    }

    impl TreeCursorStep {
        pub(crate) fn ok(&self) -> Option<Visibility> {
            match self {
                TreeCursorStep::TreeCursorStepNone => None,
                TreeCursorStep::TreeCursorStepHidden => Some(Visibility::Hidden),
                TreeCursorStep::TreeCursorStepVisible => Some(Visibility::Visible),
            }
        }
    }

    extern "C" {
        fn ts_tree_cursor_goto_first_child_internal(
            self_: *mut tree_sitter::ffi::TSTreeCursor,
        ) -> TreeCursorStep;
        fn ts_tree_cursor_goto_next_sibling_internal(
            self_: *mut tree_sitter::ffi::TSTreeCursor,
        ) -> TreeCursorStep;
    }

    #[repr(transparent)]
    pub struct TNode<'a>(pub tree_sitter::Node<'a>);

    impl<'a> crate::tree_gen::parser::Node for TNode<'a> {
        fn kind(&self) -> &str {
            self.0.kind()
        }

        fn start_byte(&self) -> usize {
            self.0.start_byte()
        }

        fn end_byte(&self) -> usize {
            self.0.end_byte()
        }

        fn child_count(&self) -> usize {
            self.0.child_count()
        }

        fn child(&self, i: usize) -> Option<Self> {
            self.0.child(i).map(TNode)
        }

        fn is_named(&self) -> bool {
            self.0.is_named()
        }

        fn is_missing(&self) -> bool {
            self.0.is_missing()
        }

        fn is_error(&self) -> bool {
            self.0.is_error()
        }
    }

    impl<'a> crate::tree_gen::parser::NodeWithU16TypeId for TNode<'a> {
        fn kind_id(&self) -> u16 {
            self.0.kind_id()
        }
    }

    #[repr(transparent)]
    #[derive(Clone)]
    pub struct TTreeCursor<'a, const HIDDEN_NODES: bool = false>(pub tree_sitter::TreeCursor<'a>);

    impl<'a, const HIDDEN_NODES: bool> std::fmt::Debug for TTreeCursor<'a, HIDDEN_NODES> {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.debug_tuple("TTreeCursor")
                .field(&self.0.node().kind())
                .finish()
        }
    }

    impl<'a, const HIDDEN_NODES: bool> crate::tree_gen::parser::TreeCursor
        for TTreeCursor<'a, HIDDEN_NODES>
    {
        type N = TNode<'a>;
        fn node(&self) -> TNode<'a> {
            TNode(self.0.node())
        }

        fn role(&self) -> Option<std::num::NonZeroU16> {
            self.0.field_id()
        }

        fn goto_parent(&mut self) -> bool {
            self.0.goto_parent()
        }

        fn goto_first_child(&mut self) -> bool {
            self.goto_first_child_extended().is_some()
        }

        fn goto_next_sibling(&mut self) -> bool {
            self.goto_next_sibling_extended().is_some()
        }

        fn goto_first_child_extended(&mut self) -> Option<Visibility> {
            if HIDDEN_NODES {
                unsafe {
                    let s = &mut self.0;
                    let s: *mut tree_sitter::ffi::TSTreeCursor = std::mem::transmute(s);
                    ts_tree_cursor_goto_first_child_internal(s)
                }
                .ok()
            } else {
                if self.0.goto_first_child() {
                    Some(Visibility::Visible)
                } else {
                    None
                }
            }
        }

        fn goto_next_sibling_extended(&mut self) -> Option<Visibility> {
            if HIDDEN_NODES {
                let r = unsafe {
                    let s = &mut self.0;
                    let s: *mut tree_sitter::ffi::TSTreeCursor = std::mem::transmute(s);
                    ts_tree_cursor_goto_next_sibling_internal(s)
                }
                .ok();
                r
            } else {
                if self.0.goto_next_sibling() {
                    Some(Visibility::Visible)
                } else {
                    None
                }
            }
        }
    }

    /// Guaranteed to work even when considering hidden nodes,
    /// i.e., goto_next_cchildren() skips hidden parents...
    pub struct PrePost<C> {
        has: super::zipped::Has,
        stack: Vec<C>,
        vis: bitvec::vec::BitVec,
    }

    impl<'a, C: super::parser::TreeCursor + Clone> PrePost<C> {
        pub fn new(cursor: &C) -> Self {
            use bitvec::prelude::Lsb0;
            let mut vis = bitvec::bitvec![];
            vis.push(Visibility::Hidden == Visibility::Hidden);
            let pre_post = Self {
                has: super::zipped::Has::Down,
                stack: vec![cursor.clone()],
                vis,
            };
            pre_post
        }

        pub fn current(&mut self) -> Option<(&C, &mut super::zipped::Has)> {
            self.stack.last().map(|c| (c, &mut self.has))
        }

        pub fn next(&mut self) -> Option<Visibility> {
            use super::zipped::Has;
            use crate::tree_gen::parser::Node;
            if self.vis.is_empty() {
                return None;
            };
            let Some(cursor) = self.stack.last_mut() else {
                return None;
            };
            let mut cursor = cursor.clone();
            if self.has != Has::Up
                && let Some(visibility) = cursor.goto_first_child_extended()
            {
                self.stack.push(cursor);
                self.has = Has::Down;
                self.vis.push(visibility == Visibility::Hidden);
                Some(visibility)
            } else {
                use std::ops::Deref;
                if let Some(visibility) = cursor.goto_next_sibling_extended() {
                    let _ = self.stack.pop().unwrap();
                    let c = self.stack.last_mut().unwrap();
                    if c.node().end_byte() <= cursor.node().start_byte() {
                        self.has = Has::Up;
                        let vis = if *self.vis.last().unwrap().deref() {
                            Visibility::Hidden
                        } else {
                            Visibility::Visible
                        };
                        return Some(vis);
                    }
                    self.stack.push(cursor);
                    self.vis.push(visibility == Visibility::Hidden);
                    self.has = Has::Right;
                    Some(visibility)
                } else if let Some(c) = self.stack.pop() {
                    self.has = Has::Up;
                    if self.stack.is_empty() {
                        self.stack.push(c);
                        None
                        // depends on usage
                        // let vis = if self.vis.pop().unwrap() {
                        //     Visibility::Hidden
                        // } else {
                        //     Visibility::Visible
                        // };
                        // Some(vis)
                    } else {
                        let vis = if *self.vis.last().unwrap().deref() {
                            Visibility::Hidden
                        } else {
                            Visibility::Visible
                        };
                        Some(vis)
                    }
                } else {
                    None
                }
            }
        }
    }
}

#[cfg(feature = "ts")]
mod zipped_ts;
#[cfg(feature = "ts")]
mod zipped_ts0;
#[doc(hidden)]
#[cfg(feature = "ts")]
pub mod zipped_ts_no_goto_parent;
#[doc(hidden)]
#[cfg(feature = "ts")]
pub mod zipped_ts_no_goto_parent_a;
#[doc(hidden)]
#[cfg(feature = "ts")]
pub mod zipped_ts_simp;
#[doc(hidden)]
#[cfg(feature = "ts")]
pub mod zipped_ts_simp0;
#[doc(hidden)]
#[cfg(feature = "ts")]
pub mod zipped_ts_simp1;

pub(crate) fn things_after_last_lb<'b>(lb: &[u8], spaces: &'b [u8]) -> Option<&'b [u8]> {
    spaces
        .windows(lb.len())
        .rev()
        .position(|window| window == lb)
        .and_then(|i| Some(&spaces[spaces.len() - i - 1..]))
}

pub fn compute_indentation<'a>(
    line_break: &Vec<u8>,
    text: &'a [u8],
    pos: usize,
    padding_start: usize,
    parent_indentation: &'a [Space],
) -> Vec<Space> {
    let spaces = { &text[padding_start..pos] };
    // let spaces = text.get(padding_start.min(text.len()-1)..pos.min(text.len()));
    // let Some(spaces) = spaces else {
    //     return parent_indentation.to_vec()
    // };
    let spaces_after_lb = things_after_last_lb(&*line_break, spaces);
    match spaces_after_lb {
        Some(s) => Space::format_indentation(s),
        None => parent_indentation.to_vec(),
    }
}

pub fn try_compute_indentation<'a>(
    line_break: &Vec<u8>,
    text: &'a [u8],
    pos: usize,
    padding_start: usize,
    parent_indentation: &'a [Space],
) -> Vec<Space> {
    let spaces = { &text[padding_start..pos] };
    let spaces_after_lb = things_after_last_lb(&*line_break, spaces);
    match spaces_after_lb {
        Some(s) => Space::try_format_indentation(s).unwrap_or(parent_indentation.to_vec()),
        None => parent_indentation.to_vec(),
    }
}

pub fn get_spacing(
    padding_start: usize,
    pos: usize,
    text: &[u8],
    _parent_indentation: &Spaces,
) -> Option<Vec<u8>> {
    // TODO change debug assert to assert if you want to strictly enforce spaces, issues with other char leaking is often caused by "bad" grammar.
    if padding_start != pos {
        let spaces = &text[padding_start..pos];
        // let spaces = Space::format_indentation(spaces);
        let mut bslash = false;
        spaces.iter().for_each(|x| {
            if bslash && (*x == b'\n' || *x == b'\r') {
                bslash = false
            } else if *x == b'\\' {
                debug_assert!(!bslash);
                bslash = true
            } else {
                debug_assert!(
                    *x == b' ' || *x == b'\n' || *x == b'\t' || *x == b'\r',
                    "{} {} {:?}",
                    x,
                    padding_start,
                    std::str::from_utf8(&spaces).unwrap()
                )
            }
        });
        debug_assert!(
            !bslash,
            "{}",
            std::str::from_utf8(&&text[padding_start.saturating_sub(100)..pos + 50]).unwrap()
        );
        let spaces = spaces.to_vec();
        // let spaces = Space::replace_indentation(parent_indentation, &spaces);
        // TODO put back the relativisation later, can pose issues when computing len of a subtree (contextually if we make the optimisation)
        Some(spaces)
    } else {
        None
    }
}

pub fn try_get_spacing(
    padding_start: usize,
    pos: usize,
    text: &[u8],
    _parent_indentation: &Spaces,
) -> Option<Vec<u8>> {
    // ) -> Option<Spaces> {
    if padding_start != pos {
        let spaces = &text[padding_start..pos];
        // println!("{:?}",std::str::from_utf8(spaces).unwrap());
        if spaces
            .iter()
            .find(|&x| *x != b' ' && *x != b'\n' && *x != b'\t' && *x != b'\r')
            .is_some()
        {
            return None;
        }
        let spaces = spaces.to_vec();

        // let spaces = Space::try_format_indentation(spaces)?;
        // let spaces = Space::replace_indentation(parent_indentation, &spaces);
        // TODO put back the relativisation later, can pose issues when computing len of a subtree (contextually if we make the optimisation)
        Some(spaces)
    } else {
        None
    }
}

pub fn has_final_space(depth: &usize, sum_byte_length: usize, text: &[u8]) -> bool {
    // TODO not sure about depth
    *depth == 0 && sum_byte_length < text.len()
}

pub fn hash32<T: ?Sized + std::hash::Hash>(t: &T) -> u32 {
    crate::utils::clamp_u64_to_u32(&crate::utils::hash(t))
}

pub trait Prepro<T> {
    const USING: bool;
    fn preprocessing(&self, ty: T) -> Result<crate::scripting::Acc, String>;
}

impl<HAST, Acc, T> Prepro<T> for NoOpMore<HAST, Acc> {
    const USING: bool = false;
    fn preprocessing(&self, _t: T) -> Result<crate::scripting::Acc, String> {
        Ok(todo!())
    }
}

pub type PrecompQueries = u16;

pub trait More {
    type TS;
    type T: crate::types::Tree;
    type Acc: WithChildren<<Self::T as crate::types::Stored>::TreeId>;
    const ENABLED: bool;
    fn match_precomp_queries<
        'a,
        HAST: crate::types::HyperAST<
                'a,
                IdN = <Self::T as crate::types::Stored>::TreeId,
                TS = Self::TS,
                T = Self::T,
            > + std::clone::Clone,
    >(
        &self,
        stores: HAST,
        acc: &Self::Acc,
        label: Option<&str>,
    ) -> crate::tree_gen::PrecompQueries
    where
        HAST::IdN: Copy;
}

pub struct NoOpMore<T, Acc>(std::marker::PhantomData<(T, Acc)>);

impl<T, Acc> Default for NoOpMore<T, Acc> {
    fn default() -> Self {
        Self(Default::default())
    }
}

impl<TS, T, Acc> More for NoOpMore<(TS, T), Acc>
where
    T: crate::types::Tree,
    Acc: WithChildren<<T as crate::types::Stored>::TreeId>,
{
    type TS = TS;
    type T = T;
    type Acc = Acc;
    const ENABLED: bool = false;
    fn match_precomp_queries<
        'a,
        HAST: crate::types::HyperAST<
                'a,
                IdN = <Self::T as crate::types::Stored>::TreeId,
                TS = Self::TS,
                T = Self::T,
            > + std::clone::Clone,
    >(
        &self,
        _stores: HAST,
        _acc: &Acc,
        _label: Option<&str>,
    ) -> PrecompQueries {
        Default::default()
    }
}

impl<'a, TS, T, Acc> PreproTSG<'a> for NoOpMore<(TS, T), Acc>
where
    T: crate::types::Tree,
    Acc: WithChildren<<T as crate::types::Stored>::TreeId>,
{
    const GRAPHING: bool = false;
    fn compute_tsg<
        HAST: 'static
            + crate::types::HyperAST<
                'a,
                IdN = <Self::T as crate::types::Stored>::TreeId,
                Idx = <Self::T as crate::types::WithChildren>::ChildIdx,
                TS = Self::TS,
                T = Self::T,
            >
            + std::clone::Clone,
    >(
        &self,
        stores: HAST,
        acc: &<Self as More>::Acc,
        label: Option<&str>,
    ) -> Result<usize, String> {
        Ok(0)
    }
}

pub trait PreproTSG<'a>: More {
    const GRAPHING: bool;
    fn compute_tsg<
        HAST: 'static
            + crate::types::HyperAST<
                'a,
                IdN = <Self::T as crate::types::Stored>::TreeId,
                Idx = <Self::T as crate::types::WithChildren>::ChildIdx,
                TS = Self::TS,
                T = Self::T,
            >
            + std::clone::Clone,
    >(
        &self,
        stores: HAST,
        acc: &Self::Acc,
        label: Option<&str>,
    ) -> Result<usize, String>;
}

pub mod metric_definition;