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
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
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use std::collections::HashMap;

/// A registered notation declaration.
///
/// This captures the full information from a `notation`, `prefix`, `infix`,
/// etc. declaration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NotationEntry {
    /// The kind of notation (prefix, infix, mixfix, etc.)
    pub kind: NotationKind,
    /// Name of the notation (often the operator symbol)
    pub name: String,
    /// The pattern parts describing the syntax
    pub pattern: Vec<NotationPart>,
    /// The expansion string (the right-hand side of `=>`)
    pub expansion: String,
    /// Priority / precedence level
    pub priority: u32,
    /// Scopes in which this notation is active
    pub scopes: Vec<String>,
}
impl NotationEntry {
    /// Create a new notation entry.
    pub fn new(
        kind: NotationKind,
        name: String,
        pattern: Vec<NotationPart>,
        expansion: String,
        priority: u32,
    ) -> Self {
        Self {
            kind,
            name,
            pattern,
            expansion,
            priority,
            scopes: Vec::new(),
        }
    }
    /// Create a new notation entry with scopes.
    pub fn with_scopes(mut self, scopes: Vec<String>) -> Self {
        self.scopes = scopes;
        self
    }
    /// Check whether this entry belongs to a given scope.
    pub fn in_scope(&self, scope: &str) -> bool {
        self.scopes.iter().any(|s| s == scope)
    }
    /// Check whether this entry is unscoped (global).
    pub fn is_global(&self) -> bool {
        self.scopes.is_empty()
    }
}
/// A scope guard that automatically pops when dropped.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct ScopeGuard<'a> {
    pub(super) env: &'a mut NotationEnv,
}
impl<'a> ScopeGuard<'a> {
    /// Create a new scope guard, pushing a new scope.
    #[allow(dead_code)]
    pub fn new(env: &'a mut NotationEnv) -> Self {
        env.push_scope();
        ScopeGuard { env }
    }
}
/// An operator alias entry.
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct OperatorAlias {
    /// The alias (surface notation)
    pub alias: String,
    /// The canonical form
    pub canonical: String,
}
impl OperatorAlias {
    /// Create a new alias.
    #[allow(dead_code)]
    pub fn new(alias: &str, canonical: &str) -> Self {
        OperatorAlias {
            alias: alias.to_string(),
            canonical: canonical.to_string(),
        }
    }
}
/// A notation category (e.g. term, tactic, command).
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum NotationCategory {
    /// Term-level notation
    Term,
    /// Tactic-level notation
    Tactic,
    /// Command-level notation
    Command,
    /// Custom category
    Custom(String),
}
/// A registered operator (simpler than a full notation entry).
///
/// Operators are the common case: a single symbol with a fixity and precedence
/// that expands to a function application.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OperatorEntry {
    /// The operator symbol (e.g. `+`, `*`, `->`)
    pub symbol: String,
    /// The fixity of the operator
    pub fixity: Fixity,
    /// Precedence level
    pub precedence: u32,
    /// The expansion (fully qualified function name)
    pub expansion: String,
}
impl OperatorEntry {
    /// Create a new operator entry.
    pub fn new(symbol: String, fixity: Fixity, precedence: u32, expansion: String) -> Self {
        Self {
            symbol,
            fixity,
            precedence,
            expansion,
        }
    }
    /// Check whether this operator is a prefix operator.
    pub fn is_prefix(&self) -> bool {
        self.fixity == Fixity::Prefix
    }
    /// Check whether this operator is an infix operator (left or right).
    pub fn is_infix(&self) -> bool {
        matches!(self.fixity, Fixity::Infixl | Fixity::Infixr)
    }
    /// Check whether this operator is a postfix operator.
    pub fn is_postfix(&self) -> bool {
        self.fixity == Fixity::Postfix
    }
    /// Check whether this operator is left-associative.
    pub fn is_left_assoc(&self) -> bool {
        self.fixity == Fixity::Infixl
    }
    /// Check whether this operator is right-associative.
    pub fn is_right_assoc(&self) -> bool {
        self.fixity == Fixity::Infixr
    }
}
/// A registry of operator overloads.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct OverloadRegistry {
    /// All overload entries
    pub entries: Vec<OverloadEntry>,
}
impl OverloadRegistry {
    /// Create a new empty registry.
    #[allow(dead_code)]
    pub fn new() -> Self {
        OverloadRegistry {
            entries: Vec::new(),
        }
    }
    /// Register an overload.
    #[allow(dead_code)]
    pub fn register(&mut self, entry: OverloadEntry) {
        self.entries.push(entry);
    }
    /// Find the highest-priority overload for a symbol.
    #[allow(dead_code)]
    pub fn best_overload(&self, symbol: &str) -> Option<&OverloadEntry> {
        self.entries
            .iter()
            .filter(|e| e.symbol == symbol)
            .max_by_key(|e| e.priority)
    }
    /// Returns all overloads for a symbol.
    #[allow(dead_code)]
    pub fn all_overloads(&self, symbol: &str) -> Vec<&OverloadEntry> {
        self.entries.iter().filter(|e| e.symbol == symbol).collect()
    }
}
/// The main notation/operator table.
///
/// Contains all registered notations and operators, supports scoped activation,
/// and provides fast lookup by symbol.
#[derive(Debug, Clone)]
pub struct NotationTable {
    /// All registered notation entries.
    entries: Vec<NotationEntry>,
    /// Fast lookup by operator symbol.
    operators: HashMap<String, OperatorEntry>,
    /// Scope name -> indices into `entries` that belong to that scope.
    scoped_entries: HashMap<String, Vec<usize>>,
    /// Currently active (opened) scopes.
    active_scopes: Vec<String>,
}
impl NotationTable {
    /// Create an empty notation table.
    pub fn new() -> Self {
        Self {
            entries: Vec::new(),
            operators: HashMap::new(),
            scoped_entries: HashMap::new(),
            active_scopes: Vec::new(),
        }
    }
    /// Register a notation entry.
    pub fn register_notation(&mut self, entry: NotationEntry) {
        let idx = self.entries.len();
        for scope in &entry.scopes {
            self.scoped_entries
                .entry(scope.clone())
                .or_default()
                .push(idx);
        }
        self.entries.push(entry);
    }
    /// Register an operator entry.
    pub fn register_operator(&mut self, entry: OperatorEntry) {
        self.operators.insert(entry.symbol.clone(), entry);
    }
    /// Look up a prefix operator by symbol.
    pub fn lookup_prefix(&self, symbol: &str) -> Option<&OperatorEntry> {
        self.operators
            .get(symbol)
            .filter(|op| op.fixity == Fixity::Prefix)
    }
    /// Look up an infix operator by symbol (either left- or right-associative).
    pub fn lookup_infix(&self, symbol: &str) -> Option<&OperatorEntry> {
        self.operators
            .get(symbol)
            .filter(|op| matches!(op.fixity, Fixity::Infixl | Fixity::Infixr))
    }
    /// Look up a postfix operator by symbol.
    pub fn lookup_postfix(&self, symbol: &str) -> Option<&OperatorEntry> {
        self.operators
            .get(symbol)
            .filter(|op| op.fixity == Fixity::Postfix)
    }
    /// Look up any operator by symbol regardless of fixity.
    pub fn lookup_operator(&self, symbol: &str) -> Option<&OperatorEntry> {
        self.operators.get(symbol)
    }
    /// Get the precedence of an operator by symbol.
    pub fn get_precedence(&self, symbol: &str) -> Option<u32> {
        self.operators.get(symbol).map(|op| op.precedence)
    }
    /// Get a notation entry by index.
    pub fn get_entry(&self, index: usize) -> Option<&NotationEntry> {
        self.entries.get(index)
    }
    /// Get the number of registered notation entries.
    pub fn notation_count(&self) -> usize {
        self.entries.len()
    }
    /// Get the number of registered operators.
    pub fn operator_count(&self) -> usize {
        self.operators.len()
    }
    /// Open a scope, making its notations active.
    ///
    /// Returns the notation entries that belong to that scope.
    pub fn open_scope(&mut self, scope: &str) -> Vec<&NotationEntry> {
        if !self.active_scopes.contains(&scope.to_string()) {
            self.active_scopes.push(scope.to_string());
        }
        self.scoped_entries
            .get(scope)
            .map(|indices| {
                indices
                    .iter()
                    .filter_map(|&idx| self.entries.get(idx))
                    .collect()
            })
            .unwrap_or_default()
    }
    /// Close a scope, deactivating its notations.
    pub fn close_scope(&mut self, scope: &str) {
        self.active_scopes.retain(|s| s != scope);
    }
    /// Check whether a scope is currently active.
    pub fn is_scope_active(&self, scope: &str) -> bool {
        self.active_scopes.contains(&scope.to_string())
    }
    /// Get all currently active scopes.
    pub fn active_scope_names(&self) -> &[String] {
        &self.active_scopes
    }
    /// Get all currently active notations (global + active scopes).
    pub fn active_notations(&self) -> Vec<&NotationEntry> {
        self.entries
            .iter()
            .filter(|entry| {
                entry.is_global() || entry.scopes.iter().any(|s| self.active_scopes.contains(s))
            })
            .collect()
    }
    /// Find all notation entries matching a given name.
    pub fn find_by_name(&self, name: &str) -> Vec<&NotationEntry> {
        self.entries.iter().filter(|e| e.name == name).collect()
    }
    /// Find all notation entries of a given kind.
    pub fn find_by_kind(&self, kind: &NotationKind) -> Vec<&NotationEntry> {
        self.entries.iter().filter(|e| &e.kind == kind).collect()
    }
    /// Find all operators with a given fixity.
    pub fn find_operators_by_fixity(&self, fixity: &Fixity) -> Vec<&OperatorEntry> {
        self.operators
            .values()
            .filter(|op| &op.fixity == fixity)
            .collect()
    }
    /// Get all operator symbols, sorted alphabetically.
    pub fn all_operator_symbols(&self) -> Vec<&str> {
        let mut symbols: Vec<&str> = self.operators.keys().map(|s| s.as_str()).collect();
        symbols.sort();
        symbols
    }
    /// Compare the precedence of two symbols.
    ///
    /// Returns `Some(Ordering)` if both symbols are registered, `None` otherwise.
    pub fn compare_precedence(&self, a: &str, b: &str) -> Option<std::cmp::Ordering> {
        let pa = self.get_precedence(a)?;
        let pb = self.get_precedence(b)?;
        Some(pa.cmp(&pb))
    }
    /// Create a notation table pre-populated with standard Lean 4 operators.
    ///
    /// Includes arithmetic, comparison, logical, arrow, and assignment operators
    /// with their standard precedences.
    pub fn builtin_operators() -> Self {
        let mut table = Self::new();
        table.register_operator(OperatorEntry::new(
            "+".to_string(),
            Fixity::Infixl,
            65,
            "HAdd.hAdd".to_string(),
        ));
        table.register_operator(OperatorEntry::new(
            "-".to_string(),
            Fixity::Infixl,
            65,
            "HSub.hSub".to_string(),
        ));
        table.register_operator(OperatorEntry::new(
            "*".to_string(),
            Fixity::Infixl,
            70,
            "HMul.hMul".to_string(),
        ));
        table.register_operator(OperatorEntry::new(
            "/".to_string(),
            Fixity::Infixl,
            70,
            "HDiv.hDiv".to_string(),
        ));
        table.register_operator(OperatorEntry::new(
            "%".to_string(),
            Fixity::Infixl,
            70,
            "HMod.hMod".to_string(),
        ));
        table.register_operator(OperatorEntry::new(
            "^".to_string(),
            Fixity::Infixr,
            75,
            "HPow.hPow".to_string(),
        ));
        table.register_operator(OperatorEntry::new(
            "=".to_string(),
            Fixity::Infixl,
            50,
            "Eq".to_string(),
        ));
        table.register_operator(OperatorEntry::new(
            "!=".to_string(),
            Fixity::Infixl,
            50,
            "Ne".to_string(),
        ));
        table.register_operator(OperatorEntry::new(
            "<".to_string(),
            Fixity::Infixl,
            50,
            "LT.lt".to_string(),
        ));
        table.register_operator(OperatorEntry::new(
            ">".to_string(),
            Fixity::Infixl,
            50,
            "GT.gt".to_string(),
        ));
        table.register_operator(OperatorEntry::new(
            "<=".to_string(),
            Fixity::Infixl,
            50,
            "LE.le".to_string(),
        ));
        table.register_operator(OperatorEntry::new(
            ">=".to_string(),
            Fixity::Infixl,
            50,
            "GE.ge".to_string(),
        ));
        table.register_operator(OperatorEntry::new(
            "&&".to_string(),
            Fixity::Infixl,
            35,
            "And".to_string(),
        ));
        table.register_operator(OperatorEntry::new(
            "||".to_string(),
            Fixity::Infixl,
            30,
            "Or".to_string(),
        ));
        table.register_operator(OperatorEntry::new(
            "!".to_string(),
            Fixity::Prefix,
            100,
            "Not".to_string(),
        ));
        table.register_operator(OperatorEntry::new(
            "->".to_string(),
            Fixity::Infixr,
            25,
            "Arrow".to_string(),
        ));
        table.register_operator(OperatorEntry::new(
            ":=".to_string(),
            Fixity::Infixl,
            1,
            "Assign".to_string(),
        ));
        let ops: Vec<(String, NotationKind, u32, String)> = vec![
            ("+".into(), NotationKind::Infixl, 65, "HAdd.hAdd".into()),
            ("-".into(), NotationKind::Infixl, 65, "HSub.hSub".into()),
            ("*".into(), NotationKind::Infixl, 70, "HMul.hMul".into()),
            ("/".into(), NotationKind::Infixl, 70, "HDiv.hDiv".into()),
            ("%".into(), NotationKind::Infixl, 70, "HMod.hMod".into()),
            ("^".into(), NotationKind::Infixr, 75, "HPow.hPow".into()),
            ("=".into(), NotationKind::Infixl, 50, "Eq".into()),
            ("!=".into(), NotationKind::Infixl, 50, "Ne".into()),
            ("<".into(), NotationKind::Infixl, 50, "LT.lt".into()),
            (">".into(), NotationKind::Infixl, 50, "GT.gt".into()),
            ("<=".into(), NotationKind::Infixl, 50, "LE.le".into()),
            (">=".into(), NotationKind::Infixl, 50, "GE.ge".into()),
            ("&&".into(), NotationKind::Infixl, 35, "And".into()),
            ("||".into(), NotationKind::Infixl, 30, "Or".into()),
            ("!".into(), NotationKind::Prefix, 100, "Not".into()),
            ("->".into(), NotationKind::Infixr, 25, "Arrow".into()),
            (":=".into(), NotationKind::Infixl, 1, "Assign".into()),
        ];
        for (sym, kind, prec, expansion) in ops {
            let pattern = match &kind {
                NotationKind::Prefix => {
                    vec![
                        NotationPart::Literal(sym.clone()),
                        NotationPart::Placeholder("x".into()),
                    ]
                }
                NotationKind::Postfix => {
                    vec![
                        NotationPart::Placeholder("x".into()),
                        NotationPart::Literal(sym.clone()),
                    ]
                }
                _ => {
                    vec![
                        NotationPart::Placeholder("lhs".into()),
                        NotationPart::Literal(sym.clone()),
                        NotationPart::Placeholder("rhs".into()),
                    ]
                }
            };
            table.register_notation(NotationEntry::new(kind, sym, pattern, expansion, prec));
        }
        table
    }
}
/// A simple notation formatter that expands `_ op _` patterns.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct NotationFormatter {
    /// Registry to use for expansion
    pub registry: NotationRegistry,
}
impl NotationFormatter {
    /// Create a new formatter.
    #[allow(dead_code)]
    pub fn new(reg: NotationRegistry) -> Self {
        NotationFormatter { registry: reg }
    }
    /// Count total rules.
    #[allow(dead_code)]
    pub fn rule_count(&self) -> usize {
        self.registry.rules.len()
    }
}
/// A user-defined notation rule.
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct NotationRule {
    /// The notation pattern (e.g. "_ + _")
    pub pattern: String,
    /// The expansion template
    pub expansion: String,
    /// The precedence level
    pub prec: PrecLevel,
    /// The namespace this rule belongs to
    pub namespace: Option<String>,
}
impl NotationRule {
    /// Create a new notation rule.
    #[allow(dead_code)]
    pub fn new(pattern: &str, expansion: &str, prec: PrecLevel) -> Self {
        NotationRule {
            pattern: pattern.to_string(),
            expansion: expansion.to_string(),
            prec,
            namespace: None,
        }
    }
    /// Set the namespace.
    #[allow(dead_code)]
    pub fn in_namespace(mut self, ns: &str) -> Self {
        self.namespace = Some(ns.to_string());
        self
    }
}
/// A notation token kind for pattern matching.
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NotationTokenKind {
    /// A literal symbol to match exactly
    Literal(String),
    /// A hole that matches any expression
    Hole,
    /// A named hole
    NamedHole(String),
}
/// An overloaded operator entry.
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct OverloadEntry {
    /// The operator symbol
    pub symbol: String,
    /// The type class providing this operator
    pub typeclass: String,
    /// Priority (higher = preferred)
    pub priority: u32,
}
impl OverloadEntry {
    /// Create a new overload entry.
    #[allow(dead_code)]
    pub fn new(symbol: &str, typeclass: &str, priority: u32) -> Self {
        OverloadEntry {
            symbol: symbol.to_string(),
            typeclass: typeclass.to_string(),
            priority,
        }
    }
}
/// A simple notation registry that maps patterns to rules.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct NotationRegistry {
    /// All registered rules
    pub rules: Vec<NotationRule>,
}
impl NotationRegistry {
    /// Create an empty registry.
    #[allow(dead_code)]
    pub fn new() -> Self {
        NotationRegistry { rules: Vec::new() }
    }
    /// Register a new rule.
    #[allow(dead_code)]
    pub fn register(&mut self, rule: NotationRule) {
        self.rules.push(rule);
    }
    /// Find rules by pattern (exact match).
    #[allow(dead_code)]
    pub fn find_by_pattern(&self, pattern: &str) -> Vec<&NotationRule> {
        self.rules.iter().filter(|r| r.pattern == pattern).collect()
    }
    /// Find rules in a given namespace.
    #[allow(dead_code)]
    pub fn find_by_namespace(&self, ns: &str) -> Vec<&NotationRule> {
        self.rules
            .iter()
            .filter(|r| r.namespace.as_deref() == Some(ns))
            .collect()
    }
    /// Returns the total number of rules.
    #[allow(dead_code)]
    pub fn len(&self) -> usize {
        self.rules.len()
    }
    /// Returns true if the registry is empty.
    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.rules.is_empty()
    }
}
/// A precedence table for built-in operators.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct BuiltinPrecTable {
    /// Entries: (operator, precedence, left_assoc)
    pub entries: Vec<(String, u32, bool)>,
}
impl BuiltinPrecTable {
    /// Create the standard Lean-like precedence table.
    #[allow(dead_code)]
    pub fn standard() -> Self {
        BuiltinPrecTable {
            entries: vec![
                ("=".to_string(), 50, false),
                ("<".to_string(), 50, false),
                (">".to_string(), 50, false),
                ("<=".to_string(), 50, false),
                (">=".to_string(), 50, false),
                ("≠".to_string(), 50, false),
                ("∧".to_string(), 35, true),
                ("∨".to_string(), 30, true),
                ("→".to_string(), 25, false),
                ("↔".to_string(), 20, false),
                ("+".to_string(), 65, true),
                ("-".to_string(), 65, true),
                ("*".to_string(), 70, true),
                ("/".to_string(), 70, true),
                ("%".to_string(), 70, true),
                ("^".to_string(), 75, false),
            ],
        }
    }
    /// Look up the precedence of an operator.
    #[allow(dead_code)]
    pub fn lookup(&self, op: &str) -> Option<(u32, bool)> {
        self.entries
            .iter()
            .find(|(o, _, _)| o == op)
            .map(|(_, p, l)| (*p, *l))
    }
}
/// A notation scope for open/close semantics.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct NotationScope {
    /// Name of this scope
    pub name: String,
    /// Rules added by opening this scope
    pub rules: Vec<NotationRule>,
}
impl NotationScope {
    /// Create a new notation scope.
    #[allow(dead_code)]
    pub fn new(name: &str) -> Self {
        NotationScope {
            name: name.to_string(),
            rules: Vec::new(),
        }
    }
    /// Add a rule to this scope.
    #[allow(dead_code)]
    pub fn add_rule(&mut self, rule: NotationRule) {
        self.rules.push(rule);
    }
}
/// A macro rule with parameters.
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct MacroRule {
    /// The macro name
    pub name: String,
    /// Parameter names
    pub params: Vec<String>,
    /// The body template
    pub body: String,
}
impl MacroRule {
    /// Create a new macro rule.
    #[allow(dead_code)]
    pub fn new(name: &str, params: Vec<&str>, body: &str) -> Self {
        MacroRule {
            name: name.to_string(),
            params: params.into_iter().map(|s| s.to_string()).collect(),
            body: body.to_string(),
        }
    }
    /// Instantiate the macro with given arguments (simple text substitution).
    #[allow(dead_code)]
    pub fn instantiate(&self, args: &[&str]) -> String {
        if args.len() != self.params.len() {
            return format!(
                "(macro-error: {} expects {} args, got {})",
                self.name,
                self.params.len(),
                args.len()
            );
        }
        let mut result = self.body.clone();
        for (param, arg) in self.params.iter().zip(args.iter()) {
            result = result.replace(param.as_str(), arg);
        }
        result
    }
}
/// A collection of syntax sugar definitions.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct SyntaxSugarLibrary {
    /// All registered sugars
    pub sugars: Vec<SyntaxSugar>,
}
impl SyntaxSugarLibrary {
    /// Create an empty library.
    #[allow(dead_code)]
    pub fn new() -> Self {
        SyntaxSugarLibrary { sugars: Vec::new() }
    }
    /// Add a sugar.
    #[allow(dead_code)]
    pub fn add(&mut self, sugar: SyntaxSugar) {
        self.sugars.push(sugar);
    }
    /// Find a sugar by name.
    #[allow(dead_code)]
    pub fn find(&self, name: &str) -> Option<&SyntaxSugar> {
        self.sugars.iter().find(|s| s.name == name)
    }
    /// Returns the number of sugars.
    #[allow(dead_code)]
    pub fn len(&self) -> usize {
        self.sugars.len()
    }
    /// Returns true if empty.
    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.sugars.is_empty()
    }
}
/// A syntax extension hook.
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct SyntaxExtension {
    /// Name of the extension
    pub name: String,
    /// Whether this is an infix extension
    pub is_infix: bool,
    /// Whether this is a prefix extension
    pub is_prefix: bool,
    /// Whether this is a postfix extension
    pub is_postfix: bool,
}
impl SyntaxExtension {
    /// Create a new infix syntax extension.
    #[allow(dead_code)]
    pub fn infix(name: &str) -> Self {
        SyntaxExtension {
            name: name.to_string(),
            is_infix: true,
            is_prefix: false,
            is_postfix: false,
        }
    }
    /// Create a new prefix syntax extension.
    #[allow(dead_code)]
    pub fn prefix(name: &str) -> Self {
        SyntaxExtension {
            name: name.to_string(),
            is_infix: false,
            is_prefix: true,
            is_postfix: false,
        }
    }
}
/// A collection of operator aliases.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct OperatorAliasTable {
    /// All entries
    pub entries: Vec<OperatorAlias>,
}
impl OperatorAliasTable {
    /// Create a new empty alias table.
    #[allow(dead_code)]
    pub fn new() -> Self {
        OperatorAliasTable {
            entries: Vec::new(),
        }
    }
    /// Add an alias.
    #[allow(dead_code)]
    pub fn add(&mut self, alias: OperatorAlias) {
        self.entries.push(alias);
    }
    /// Resolve an alias to its canonical form.
    #[allow(dead_code)]
    pub fn resolve(&self, op: &str) -> String {
        self.entries
            .iter()
            .find(|e| e.alias == op)
            .map(|e| e.canonical.clone())
            .unwrap_or_else(|| op.to_string())
    }
    /// Returns the number of entries.
    #[allow(dead_code)]
    pub fn len(&self) -> usize {
        self.entries.len()
    }
    /// Returns true if empty.
    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}
/// A notation group that bundles related notation rules.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct NotationGroup {
    /// Group name
    pub name: String,
    /// Rules in this group
    pub rules: Vec<NotationRule>,
    /// Whether this group is currently active
    pub active: bool,
}
impl NotationGroup {
    /// Create a new active group.
    #[allow(dead_code)]
    pub fn new(name: &str) -> Self {
        NotationGroup {
            name: name.to_string(),
            rules: Vec::new(),
            active: true,
        }
    }
    /// Add a rule.
    #[allow(dead_code)]
    pub fn add(&mut self, rule: NotationRule) {
        self.rules.push(rule);
    }
    /// Deactivate this group.
    #[allow(dead_code)]
    pub fn deactivate(&mut self) {
        self.active = false;
    }
    /// Returns active rules.
    #[allow(dead_code)]
    pub fn active_rules(&self) -> Vec<&NotationRule> {
        if self.active {
            self.rules.iter().collect()
        } else {
            Vec::new()
        }
    }
}
/// A priority queue for notation rules (by precedence).
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct NotationPriorityQueue {
    /// Rules sorted by precedence (descending)
    pub rules: Vec<NotationRule>,
}
impl NotationPriorityQueue {
    /// Create a new empty queue.
    #[allow(dead_code)]
    pub fn new() -> Self {
        NotationPriorityQueue { rules: Vec::new() }
    }
    /// Insert a rule (maintaining sort order).
    #[allow(dead_code)]
    pub fn insert(&mut self, rule: NotationRule) {
        let pos = self
            .rules
            .partition_point(|r| r.prec.value >= rule.prec.value);
        self.rules.insert(pos, rule);
    }
    /// Returns rules with at least the given precedence.
    #[allow(dead_code)]
    pub fn rules_at_or_above(&self, prec: u32) -> Vec<&NotationRule> {
        self.rules.iter().filter(|r| r.prec.value >= prec).collect()
    }
    /// Returns the number of rules.
    #[allow(dead_code)]
    pub fn len(&self) -> usize {
        self.rules.len()
    }
    /// Returns true if empty.
    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.rules.is_empty()
    }
}
/// The kind of a notation declaration.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum NotationKind {
    /// Prefix operator (e.g. `!`, `-`)
    Prefix,
    /// Postfix operator (e.g. `!` factorial)
    Postfix,
    /// Left-associative infix operator (e.g. `+`, `-`)
    Infixl,
    /// Right-associative infix operator (e.g. `->`, `^`)
    Infixr,
    /// General mixfix notation (e.g. `if _ then _ else _`)
    Notation,
}
/// An abstract syntax sugar definition.
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct SyntaxSugar {
    /// Name of this syntactic sugar
    pub name: String,
    /// The surface form
    pub surface: String,
    /// The desugared core form
    pub core: String,
}
impl SyntaxSugar {
    /// Create a new syntax sugar definition.
    #[allow(dead_code)]
    pub fn new(name: &str, surface: &str, core: &str) -> Self {
        SyntaxSugar {
            name: name.to_string(),
            surface: surface.to_string(),
            core: core.to_string(),
        }
    }
}
/// A simple token pattern matcher for notation expansion.
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct PatternMatcher {
    /// The pattern to match (tokens separated by spaces, `_` = hole)
    pub pattern: Vec<String>,
}
impl PatternMatcher {
    /// Create from a pattern string.
    #[allow(dead_code)]
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(s: &str) -> Self {
        PatternMatcher {
            pattern: s.split_whitespace().map(|t| t.to_string()).collect(),
        }
    }
    /// Count holes (underscores) in the pattern.
    #[allow(dead_code)]
    pub fn hole_count(&self) -> usize {
        self.pattern.iter().filter(|t| t.as_str() == "_").count()
    }
    /// Returns true if the pattern is entirely holes.
    #[allow(dead_code)]
    pub fn all_holes(&self) -> bool {
        self.pattern.iter().all(|t| t.as_str() == "_")
    }
}
/// Fixity of an operator entry.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Fixity {
    /// Prefix operator
    Prefix,
    /// Left-associative infix operator
    Infixl,
    /// Right-associative infix operator
    Infixr,
    /// Postfix operator
    Postfix,
}
/// A notation conflict: two rules with the same pattern but different expansions.
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct NotationConflict {
    /// The conflicting pattern
    pub pattern: String,
    /// First expansion
    pub expansion_a: String,
    /// Second expansion
    pub expansion_b: String,
}
/// A notation environment with scoped rules.
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct NotationEnv {
    /// Stack of scopes, outermost first
    pub scopes: Vec<Vec<NotationRule>>,
}
impl NotationEnv {
    /// Create a new notation environment with one empty scope.
    #[allow(dead_code)]
    pub fn new() -> Self {
        NotationEnv {
            scopes: vec![Vec::new()],
        }
    }
    /// Push a new scope.
    #[allow(dead_code)]
    pub fn push_scope(&mut self) {
        self.scopes.push(Vec::new());
    }
    /// Pop the current scope.
    #[allow(dead_code)]
    pub fn pop_scope(&mut self) {
        if self.scopes.len() > 1 {
            self.scopes.pop();
        }
    }
    /// Add a rule to the current scope.
    #[allow(dead_code)]
    pub fn add(&mut self, rule: NotationRule) {
        if let Some(scope) = self.scopes.last_mut() {
            scope.push(rule);
        }
    }
    /// Look up all rules for a pattern (innermost scope first).
    #[allow(dead_code)]
    pub fn lookup(&self, pattern: &str) -> Vec<&NotationRule> {
        let mut result = Vec::new();
        for scope in self.scopes.iter().rev() {
            for rule in scope {
                if rule.pattern == pattern {
                    result.push(rule);
                }
            }
        }
        result
    }
    /// Total number of rules across all scopes.
    #[allow(dead_code)]
    pub fn total_rules(&self) -> usize {
        self.scopes.iter().map(|s| s.len()).sum()
    }
}
/// A notation precedence level with associativity.
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PrecLevel {
    /// The precedence value (higher = tighter binding)
    pub value: u32,
    /// Whether this is left-associative
    pub left_assoc: bool,
    /// Whether this is right-associative
    pub right_assoc: bool,
}
impl PrecLevel {
    /// Create a new left-associative precedence level.
    #[allow(dead_code)]
    pub fn left(value: u32) -> Self {
        PrecLevel {
            value,
            left_assoc: true,
            right_assoc: false,
        }
    }
    /// Create a new right-associative precedence level.
    #[allow(dead_code)]
    pub fn right(value: u32) -> Self {
        PrecLevel {
            value,
            left_assoc: false,
            right_assoc: true,
        }
    }
    /// Create a non-associative level.
    #[allow(dead_code)]
    pub fn none(value: u32) -> Self {
        PrecLevel {
            value,
            left_assoc: false,
            right_assoc: false,
        }
    }
}
/// A single part of a notation pattern.
///
/// Notation patterns are sequences of literals and placeholders:
/// ```text
/// notation:50 lhs " + " rhs => HAdd.hAdd lhs rhs
///              ^^^  ^^^^^  ^^^
///          Placeholder Literal Placeholder
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NotationPart {
    /// A literal string token (e.g. `" + "`, `"if"`)
    Literal(String),
    /// A placeholder for a variable position (e.g. `lhs`, `rhs`)
    Placeholder(String),
    /// A precedence annotation on a placeholder
    Prec(u32),
}