oxilean-parse 0.1.2

OxiLean parser - Concrete syntax to abstract syntax
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
//! Auto-generated module (split from types.rs)
//!
//! Second half of type definitions and impl blocks.

use super::super::functions::*;
use super::defs::*;
use std::collections::HashMap;
use std::ops::Range;

/// Represents the "reachability" of tokens from a parse entry point.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct TokenReachability {
    reachable: std::collections::HashSet<usize>,
}
impl TokenReachability {
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn new() -> Self {
        Self {
            reachable: std::collections::HashSet::new(),
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn mark_reachable(&mut self, offset: usize) {
        self.reachable.insert(offset);
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn is_reachable(&self, offset: usize) -> bool {
        self.reachable.contains(&offset)
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn reachable_count(&self) -> usize {
        self.reachable.len()
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn coverage_fraction(&self, total_tokens: usize) -> f64 {
        if total_tokens == 0 {
            0.0
        } else {
            self.reachable.len() as f64 / total_tokens as f64
        }
    }
}
/// A pool of parse fibers for parallel/concurrent parsing.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct FiberPool {
    fibers: Vec<ParseFiber>,
    next_id: u64,
}
impl FiberPool {
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn new() -> Self {
        Self {
            fibers: Vec::new(),
            next_id: 0,
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn spawn(&mut self, start: usize, depth: usize, state: impl Into<String>) -> u64 {
        let id = self.next_id;
        self.next_id += 1;
        self.fibers.push(ParseFiber::new(id, start, depth, state));
        id
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn get(&self, id: u64) -> Option<&ParseFiber> {
        self.fibers.iter().find(|f| f.id == id)
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn remove(&mut self, id: u64) {
        self.fibers.retain(|f| f.id != id);
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn active_count(&self) -> usize {
        self.fibers.len()
    }
}
/// Manages multiple snapshots with a limit.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct SnapshotManager {
    snapshots: Vec<ParseSnapshot>,
    max_snapshots: usize,
}
impl SnapshotManager {
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn new(max_snapshots: usize) -> Self {
        Self {
            snapshots: Vec::new(),
            max_snapshots,
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn save(&mut self, snapshot: ParseSnapshot) {
        if self.snapshots.len() >= self.max_snapshots {
            self.snapshots.remove(0);
        }
        self.snapshots.push(snapshot);
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn best(&self) -> Option<&ParseSnapshot> {
        self.snapshots.iter().min_by_key(|s| s.error_count)
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn latest(&self) -> Option<&ParseSnapshot> {
        self.snapshots.last()
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn count(&self) -> usize {
        self.snapshots.len()
    }
}
/// A simple edit buffer that accumulates edits before applying them.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct EditBuffer {
    pending: Vec<SourceEdit>,
    max_pending: usize,
}
impl EditBuffer {
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn new(max_pending: usize) -> Self {
        Self {
            pending: Vec::new(),
            max_pending,
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn add(&mut self, edit: SourceEdit) -> bool {
        if self.pending.len() >= self.max_pending {
            return false;
        }
        self.pending.push(edit);
        true
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn flush(&mut self) -> Vec<SourceEdit> {
        std::mem::take(&mut self.pending)
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn pending_count(&self) -> usize {
        self.pending.len()
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn is_empty(&self) -> bool {
        self.pending.is_empty()
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn total_delta(&self) -> i64 {
        self.pending.iter().map(|e| e.delta()).sum()
    }
}
/// A session-level incremental parse manager.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct IncrementalSession {
    pub source: SimpleRope,
    pub version: ParseVersion,
    pub errors: IncrementalErrorMap,
    pub stats: IncrParseStats,
}
impl IncrementalSession {
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn new(source: impl Into<String>) -> Self {
        Self {
            source: SimpleRope::new(source),
            version: ParseVersion::new(),
            errors: IncrementalErrorMap::new(),
            stats: IncrParseStats::new(),
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn apply_edit(&mut self, edit: SourceEdit) {
        let v = self.version.increment();
        let _ = v;
        let start = edit.start;
        let end = edit.end + edit.new_text.len();
        self.errors.clear_range(start, end);
        self.stats.total_edits += 1;
        let delta = edit.delta();
        let src = self.source.as_string();
        let new_src = apply_edits(&src, &[edit]);
        self.source = SimpleRope::new(new_src);
        let _ = delta;
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn source_text(&self) -> String {
        self.source.as_string()
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn has_errors(&self) -> bool {
        self.errors.total_error_count() > 0
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn current_version(&self) -> u64 {
        self.version.current()
    }
}
/// Represents the set of changed lines in a diff.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct LineDiff {
    changed_lines: Vec<(usize, String, String)>,
}
impl LineDiff {
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn new() -> Self {
        Self {
            changed_lines: Vec::new(),
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn add_change(&mut self, line: usize, old: impl Into<String>, new: impl Into<String>) {
        self.changed_lines.push((line, old.into(), new.into()));
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn count(&self) -> usize {
        self.changed_lines.len()
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn affected_lines(&self) -> Vec<usize> {
        self.changed_lines.iter().map(|(l, _, _)| *l).collect()
    }
}
/// A fingerprint computed from a slice of tokens.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[allow(missing_docs)]
pub struct TokenFingerprint(u64);
impl TokenFingerprint {
    #[allow(missing_docs)]
    pub fn compute(tokens: &[&str]) -> Self {
        let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
        for tok in tokens {
            for byte in tok.bytes() {
                hash ^= byte as u64;
                hash = hash.wrapping_mul(0x0000_0100_0000_01B3);
            }
            hash ^= 0x1f;
        }
        TokenFingerprint(hash)
    }
    #[allow(missing_docs)]
    pub fn value(&self) -> u64 {
        self.0
    }
}
/// A transaction groups multiple `TextChange`s into an atomic unit.
#[allow(missing_docs)]
pub struct Transaction {
    changes: Vec<TextChange>,
    snapshot: Option<String>,
}
impl Transaction {
    #[allow(missing_docs)]
    pub fn new() -> Self {
        Self {
            changes: Vec::new(),
            snapshot: None,
        }
    }
    #[allow(missing_docs)]
    pub fn begin(source: &str) -> Self {
        Self {
            changes: Vec::new(),
            snapshot: Some(source.to_string()),
        }
    }
    #[allow(missing_docs)]
    pub fn add(&mut self, change: TextChange) {
        self.changes.push(change);
    }
    #[allow(missing_docs)]
    pub fn commit(&self, source: &str) -> String {
        let mut s = source.to_string();
        let mut sorted = self.changes.clone();
        sorted.sort_by_key(|b| std::cmp::Reverse(b.range.start));
        for change in &sorted {
            s = change.apply(&s);
        }
        s
    }
    #[allow(missing_docs)]
    pub fn rollback(&self) -> Option<&str> {
        self.snapshot.as_deref()
    }
    #[allow(missing_docs)]
    pub fn len(&self) -> usize {
        self.changes.len()
    }
    #[allow(missing_docs)]
    pub fn is_empty(&self) -> bool {
        self.changes.is_empty()
    }
}
/// A map of byte offset to error messages for incremental error tracking.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct IncrementalErrorMap {
    errors: std::collections::BTreeMap<usize, Vec<String>>,
}
impl IncrementalErrorMap {
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn new() -> Self {
        Self {
            errors: std::collections::BTreeMap::new(),
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn add_error(&mut self, offset: usize, msg: impl Into<String>) {
        self.errors.entry(offset).or_default().push(msg.into());
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn clear_range(&mut self, start: usize, end: usize) {
        let keys: Vec<_> = self.errors.range(start..end).map(|(&k, _)| k).collect();
        for k in keys {
            self.errors.remove(&k);
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn errors_in_range(&self, start: usize, end: usize) -> Vec<&String> {
        self.errors
            .range(start..end)
            .flat_map(|(_, msgs)| msgs.iter())
            .collect()
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn total_error_count(&self) -> usize {
        self.errors.values().map(|v| v.len()).sum()
    }
}
/// A cached parse result for one declaration
#[derive(Debug, Clone)]
#[allow(missing_docs)]
pub struct ParsedDecl {
    pub source_range: Range<usize>,
    pub name: Option<String>,
    pub decl_text: String,
    pub valid: bool,
}
/// The incremental parser state — tracks source + cache
#[allow(missing_docs)]
pub struct IncrementalParser {
    source: String,
    cache: HashMap<usize, ParsedDecl>,
    dirty_ranges: Vec<Range<usize>>,
    version: u32,
}
impl IncrementalParser {
    #[allow(missing_docs)]
    pub fn new(source: impl Into<String>) -> Self {
        let source = source.into();
        let mut parser = IncrementalParser {
            source,
            cache: HashMap::new(),
            dirty_ranges: Vec::new(),
            version: 0,
        };
        parser.reparse_dirty();
        parser
    }
    #[allow(missing_docs)]
    pub fn apply_change(&mut self, change: TextChange) {
        let affected_start = change.range.start;
        let affected_end = change.range.start + change.new_text.len();
        self.source = change.apply(&self.source);
        self.version += 1;
        let dirty_end = affected_end.max(change.range.end);
        self.mark_dirty(affected_start..dirty_end);
    }
    #[allow(missing_docs)]
    pub fn apply_changes(&mut self, mut changes: Vec<TextChange>) {
        changes.sort_by_key(|b| std::cmp::Reverse(b.range.start));
        for change in changes {
            self.apply_change(change);
        }
    }
    #[allow(missing_docs)]
    pub fn source(&self) -> &str {
        &self.source
    }
    #[allow(missing_docs)]
    pub fn version(&self) -> u32 {
        self.version
    }
    #[allow(missing_docs)]
    pub fn split_declarations(source: &str) -> Vec<(usize, &str)> {
        let keywords = [
            "def ",
            "theorem ",
            "axiom ",
            "inductive ",
            "structure ",
            "class ",
        ];
        let mut result = Vec::new();
        let mut current_start: Option<usize> = None;
        let mut pos = 0usize;
        for line in source.split_inclusive('\n') {
            let is_decl_start = keywords.iter().any(|kw| line.starts_with(kw));
            if is_decl_start {
                if let Some(start) = current_start {
                    result.push((start, &source[start..pos]));
                }
                current_start = Some(pos);
            }
            pos += line.len();
        }
        if let Some(start) = current_start {
            result.push((start, &source[start..]));
        }
        result
    }
    #[allow(missing_docs)]
    pub fn reparse_dirty(&mut self) {
        let source = self.source.clone();
        let decls = Self::split_declarations(&source);
        for (start, text) in decls {
            let end = start + text.len();
            let range = start..end;
            if self.dirty_ranges.is_empty() || self.is_dirty(&range) {
                let name = Self::extract_decl_name(text);
                let entry = ParsedDecl {
                    source_range: range,
                    name,
                    decl_text: text.to_string(),
                    valid: true,
                };
                self.cache.insert(start, entry);
            }
        }
        self.clear_dirty();
    }
    #[allow(missing_docs)]
    pub fn declarations(&self) -> Vec<&ParsedDecl> {
        let mut decls: Vec<&ParsedDecl> = self.cache.values().collect();
        decls.sort_by_key(|d| d.source_range.start);
        decls
    }
    #[allow(missing_docs)]
    pub fn decl_at(&self, offset: usize) -> Option<&ParsedDecl> {
        self.cache
            .values()
            .find(|d| d.source_range.contains(&offset))
    }
    fn mark_dirty(&mut self, range: Range<usize>) {
        for decl in self.cache.values_mut() {
            if decl.source_range.start < range.end && decl.source_range.end > range.start {
                decl.valid = false;
            }
        }
        self.dirty_ranges.push(range);
    }
    fn is_dirty(&self, range: &Range<usize>) -> bool {
        self.dirty_ranges
            .iter()
            .any(|d| d.start < range.end && d.end > range.start)
    }
    fn clear_dirty(&mut self) {
        self.dirty_ranges.clear();
    }
    #[allow(missing_docs)]
    pub fn cache_size(&self) -> usize {
        self.cache.len()
    }
    #[allow(missing_docs)]
    pub fn dirty_count(&self) -> usize {
        self.dirty_ranges.len()
    }
    fn extract_decl_name(text: &str) -> Option<String> {
        let keywords = [
            "def ",
            "theorem ",
            "axiom ",
            "inductive ",
            "structure ",
            "class ",
        ];
        for kw in &keywords {
            if let Some(rest) = text.strip_prefix(kw) {
                let name: String = rest
                    .chars()
                    .take_while(|c| c.is_alphanumeric() || *c == '_' || *c == '\'')
                    .collect();
                if !name.is_empty() {
                    return Some(name);
                }
            }
        }
        None
    }
    #[allow(missing_docs)]
    pub fn invalid_declarations(&self) -> Vec<&ParsedDecl> {
        let mut decls: Vec<&ParsedDecl> = self.cache.values().filter(|d| !d.valid).collect();
        decls.sort_by_key(|d| d.source_range.start);
        decls
    }
    #[allow(missing_docs)]
    pub fn invalidate_by_name(&mut self, name: &str) {
        for decl in self.cache.values_mut() {
            if decl.name.as_deref() == Some(name) {
                decl.valid = false;
            }
        }
    }
}
/// A reparse request indicating which region to re-parse.
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct ReparseRequest {
    pub start_byte: usize,
    pub end_byte: usize,
    pub source_version: u64,
    pub priority: ReparsePriority,
}
impl ReparseRequest {
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn new(start: usize, end: usize, version: u64) -> Self {
        Self {
            start_byte: start,
            end_byte: end,
            source_version: version,
            priority: ReparsePriority::Normal,
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn with_priority(mut self, p: ReparsePriority) -> Self {
        self.priority = p;
        self
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn byte_span(&self) -> usize {
        self.end_byte.saturating_sub(self.start_byte)
    }
}
/// Incremental parse cache: maps dirty-region hashes to parse results.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct IncrementalParseCache {
    entries: std::collections::HashMap<u64, IncrParseEntry>,
    max_entries: usize,
    hits: u64,
    misses: u64,
}
impl IncrementalParseCache {
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn new(max_entries: usize) -> Self {
        Self {
            entries: std::collections::HashMap::new(),
            max_entries,
            hits: 0,
            misses: 0,
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn lookup(&mut self, region_hash: u64) -> Option<&IncrParseEntry> {
        if self.entries.contains_key(&region_hash) {
            self.hits += 1;
            self.entries.get(&region_hash)
        } else {
            self.misses += 1;
            None
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn store(&mut self, entry: IncrParseEntry) {
        if self.entries.len() >= self.max_entries {
            if let Some(&k) = self.entries.keys().next() {
                self.entries.remove(&k);
            }
        }
        self.entries.insert(entry.region_hash, entry);
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn hit_rate(&self) -> f64 {
        let total = self.hits + self.misses;
        if total == 0 {
            0.0
        } else {
            self.hits as f64 / total as f64
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn stats(&self) -> (u64, u64) {
        (self.hits, self.misses)
    }
}
/// A "change detector" that tracks whether a portion of source has changed.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct ChangeDetector {
    hashes: std::collections::HashMap<(usize, usize), u64>,
}
impl ChangeDetector {
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn new() -> Self {
        Self {
            hashes: std::collections::HashMap::new(),
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn record(&mut self, source: &str, start: usize, end: usize) {
        let end = end.min(source.len());
        let start = start.min(end);
        let h = {
            let data = &source.as_bytes()[start..end];
            let mut hash = 14695981039346656037u64;
            for &b in data {
                hash = hash.wrapping_mul(1099511628211u64) ^ b as u64;
            }
            hash
        };
        self.hashes.insert((start, end), h);
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn has_changed(&self, source: &str, start: usize, end: usize) -> bool {
        let end = end.min(source.len());
        let start = start.min(end);
        let current = {
            let data = &source.as_bytes()[start..end];
            let mut hash = 14695981039346656037u64;
            for &b in data {
                hash = hash.wrapping_mul(1099511628211u64) ^ b as u64;
            }
            hash
        };
        self.hashes
            .get(&(start, end))
            .map_or(true, |&stored| stored != current)
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn recorded_count(&self) -> usize {
        self.hashes.len()
    }
}
/// Represents a snapshot of incremental parse state for rollback.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct ParseSnapshot {
    pub source: String,
    pub version: u64,
    pub node_count: usize,
    pub error_count: usize,
}
impl ParseSnapshot {
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn capture(source: &str, version: u64, node_count: usize, error_count: usize) -> Self {
        Self {
            source: source.to_string(),
            version,
            node_count,
            error_count,
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn is_cleaner_than(&self, other: &Self) -> bool {
        self.error_count < other.error_count
    }
}
/// Tracks which declarations are affected by an edit.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct DeclDependencyTracker {
    decl_ranges: Vec<(String, usize, usize)>,
}
impl DeclDependencyTracker {
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn new() -> Self {
        Self {
            decl_ranges: Vec::new(),
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn register_decl(&mut self, name: impl Into<String>, start: usize, end: usize) {
        self.decl_ranges.push((name.into(), start, end));
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn affected_by_edit(&self, edit: &SourceEdit) -> Vec<&str> {
        self.decl_ranges
            .iter()
            .filter(|(_, s, e)| edit.start < *e && edit.end > *s)
            .map(|(n, _, _)| n.as_str())
            .collect()
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn decl_count(&self) -> usize {
        self.decl_ranges.len()
    }
}
/// Statistics for an incremental parsing session.
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Default, Debug)]
pub struct IncrParseStats {
    pub total_edits: u64,
    pub partial_reparses: u64,
    pub full_reparses: u64,
    pub tokens_reused: u64,
    #[allow(missing_docs)]
    pub tokens_relexed: u64,
    pub nodes_reused: u64,
    pub nodes_rebuilt: u64,
}
impl IncrParseStats {
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn new() -> Self {
        Self::default()
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn reuse_fraction_tokens(&self) -> f64 {
        let total = self.tokens_reused + self.tokens_relexed;
        if total == 0 {
            0.0
        } else {
            self.tokens_reused as f64 / total as f64
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn reuse_fraction_nodes(&self) -> f64 {
        let total = self.nodes_reused + self.nodes_rebuilt;
        if total == 0 {
            0.0
        } else {
            self.nodes_reused as f64 / total as f64
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn summary(&self) -> String {
        format!(
            "edits={} partial={} full={} token_reuse={:.1}% node_reuse={:.1}%",
            self.total_edits,
            self.partial_reparses,
            self.full_reparses,
            self.reuse_fraction_tokens() * 100.0,
            self.reuse_fraction_nodes() * 100.0,
        )
    }
}
/// Tracks a history of edits for undo/redo.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct EditHistory {
    history: Vec<SourceEdit>,
    undo_stack: Vec<SourceEdit>,
    max_history: usize,
}
impl EditHistory {
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn new(max_history: usize) -> Self {
        Self {
            history: Vec::new(),
            undo_stack: Vec::new(),
            max_history,
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn push(&mut self, edit: SourceEdit) {
        if self.history.len() >= self.max_history {
            self.history.remove(0);
        }
        self.history.push(edit);
        self.undo_stack.clear();
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn undo(&mut self) -> Option<SourceEdit> {
        let edit = self.history.pop()?;
        self.undo_stack.push(edit.clone());
        Some(edit)
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn redo(&mut self) -> Option<SourceEdit> {
        let edit = self.undo_stack.pop()?;
        self.history.push(edit.clone());
        Some(edit)
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn history_len(&self) -> usize {
        self.history.len()
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn undo_count(&self) -> usize {
        self.undo_stack.len()
    }
}
/// Incremental lexer: re-lexes only the invalidated region.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct IncrementalLexerExt {
    source: String,
    validity: TokenValidity,
    version: u64,
}
impl IncrementalLexerExt {
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn new(source: impl Into<String>) -> Self {
        Self {
            source: source.into(),
            validity: TokenValidity::new(),
            version: 0,
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn apply_edit(&mut self, edit: SourceEdit) {
        let inv = compute_invalidated_range(&edit, 64);
        self.validity.invalidate(&inv);
        self.source = apply_edits(&self.source, &[edit]);
        self.version += 1;
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn source(&self) -> &str {
        &self.source
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn version(&self) -> u64 {
        self.version
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn valid_token_count(&self) -> usize {
        self.validity.valid_count()
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn needs_relex(&self, pos: usize) -> bool {
        !self.validity.is_valid_at(pos)
    }
}
/// A concurrency-safe version counter for incremental state.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct AtomicVersion {
    inner: std::sync::atomic::AtomicU64,
}
impl AtomicVersion {
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn new() -> Self {
        Self {
            inner: std::sync::atomic::AtomicU64::new(0),
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn increment(&self) -> u64 {
        self.inner.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn load(&self) -> u64 {
        self.inner.load(std::sync::atomic::Ordering::SeqCst)
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn reset(&self) {
        self.inner.store(0, std::sync::atomic::Ordering::SeqCst);
    }
}
/// The kind of a syntax node.
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum SyntaxKind {
    Root,
    Def,
    Theorem,
    Axiom,
    Ident,
    Literal,
    Token(String),
    Error,
}
/// The result of an incremental parse attempt.
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug)]
pub struct IncrementalParseResult {
    pub success: bool,
    pub reused_nodes: usize,
    pub rebuilt_nodes: usize,
    pub parse_time_us: u64,
    #[allow(missing_docs)]
    pub errors: Vec<String>,
}
impl IncrementalParseResult {
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn new(success: bool, reused: usize, rebuilt: usize, time_us: u64) -> Self {
        Self {
            success,
            reused_nodes: reused,
            rebuilt_nodes: rebuilt,
            parse_time_us: time_us,
            errors: Vec::new(),
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn add_error(&mut self, e: impl Into<String>) {
        self.errors.push(e.into());
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn reuse_ratio(&self) -> f64 {
        let total = self.reused_nodes + self.rebuilt_nodes;
        if total == 0 {
            0.0
        } else {
            self.reused_nodes as f64 / total as f64
        }
    }
    #[allow(dead_code)]
    #[allow(missing_docs)]
    pub fn has_errors(&self) -> bool {
        !self.errors.is_empty()
    }
}
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Clone)]
pub struct IncrParseEntry {
    pub region_hash: u64,
    pub result_repr: String,
    pub version: u64,
}