tecta-peg 0.5.0

PDA-based TECTA PEG parser
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
use std::{
    collections::BTreeMap,
    fmt::{Debug, Display},
};

use tecta_lex::{
    Delimiter as GroupDelimiter, Span, Spanned, SpanningChars, pat_ident_body, pat_ident_start,
    pat_punct,
};

/// A delimiter of a group in a token tree, or a sequence.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum AnyDelimiter {
    /// `()`
    Parenthesis,
    /// `[]`
    Bracket,
    /// `{}`
    Brace,
    /// `<>`,
    AngleBrackets,
}
impl AnyDelimiter {
    pub fn to_group(&self) -> Option<GroupDelimiter> {
        match self {
            Self::Parenthesis => Some(GroupDelimiter::Parenthesis),
            Self::Bracket => Some(GroupDelimiter::Bracket),
            Self::Brace => Some(GroupDelimiter::Brace),
            Self::AngleBrackets => None,
        }
    }
    pub fn opener(&self) -> char {
        match self {
            Self::Parenthesis => '(',
            Self::Bracket => '[',
            Self::Brace => '{',
            Self::AngleBrackets => '<',
        }
    }
    pub fn closer(&self) -> char {
        match self {
            Self::Parenthesis => ')',
            Self::Bracket => ']',
            Self::Brace => '}',
            Self::AngleBrackets => '>',
        }
    }
}
impl From<GroupDelimiter> for AnyDelimiter {
    fn from(value: GroupDelimiter) -> Self {
        match value {
            GroupDelimiter::Parenthesis => Self::Parenthesis,
            GroupDelimiter::Bracket => Self::Bracket,
            GroupDelimiter::Brace => Self::Brace,
        }
    }
}

/// A transient control rule; cleared by other control characters.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Control(pub ControlKind, pub Span);
impl Display for Control {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// A specific variant of control rule.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum ControlKind {
    /// Start of a sequence rule, beginning with `<` and ending with `>`.
    SequenceStart,
    /// Start of a group rule, beginning with one of `(`, `[`, or `{`, and ending with, respectively, `)`, `]`, or `}`.
    GroupStart(GroupDelimiter),
}
impl Display for ControlKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ControlKind::SequenceStart => write!(f, "<"),
            ControlKind::GroupStart(delimiter) => write!(f, "{}", delimiter.opener()),
        }
    }
}

/// At least some number of times.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum AtLeast {
    /// Matches a rule zero or more times. Delimited by `*`.
    Zero,
    /// Matches a rule one or more times. Delimited by `+`.
    One,
}
impl Display for AtLeast {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::One => write!(f, "+"),
            Self::Zero => write!(f, "*"),
        }
    }
}

/// Repeats a rule a number of times, the repetition choice being decided by the operator used:
/// - `*` repeats zero or more times
/// - `+` repeats one or more times
///
/// The first operand is the element and the second is the separator.
/// For example, `"x" ',' *` matches multiple instances of the keyword `x`, separated by commas.
/// Trailing is enabled with the `~` modifier.
#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct RepeatRule {
    pub element: Box<Rule>,
    pub separator: Box<Rule>,
    pub at_least: AtLeast,
    pub allow_trailing: bool,
}

/// A grammar rule.
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Rule(pub RuleKind, pub Span);
impl Rule {
    pub fn sequence(rules: Vec<Rule>, weak: bool, span: Span) -> Self {
        Self(RuleKind::Sequence { rules, weak }, span)
    }
    pub fn record(rules: Vec<(Spanned<Option<String>>, Rule)>, span: Span) -> Self {
        Self(RuleKind::Record(rules), span)
    }

    pub fn choice(rules: Vec<Rule>, span: Span) -> Self {
        Self(RuleKind::Choice(rules), span)
    }
    pub fn named_choice(rules: Vec<(Spanned<String>, Rule)>, span: Span) -> Self {
        Self(RuleKind::NamedChoice(rules), span)
    }

    pub fn group(delimiter: GroupDelimiter, rule: Rule, span: Span) -> Self {
        Self(RuleKind::Group(delimiter, Box::new(rule)), span)
    }

    pub fn repeat(element: Rule, separator: Rule, at_least: AtLeast, span: Span) -> Self {
        Self(
            RuleKind::Repeat(RepeatRule {
                element: Box::new(element),
                separator: Box::new(separator),
                at_least,
                allow_trailing: false,
            }),
            span,
        )
    }

    pub fn optional(rule: Rule, span: Span) -> Self {
        Self(RuleKind::Optional(Box::new(rule)), span)
    }
    pub fn boxed(rule: Rule, span: Span) -> Self {
        Self(RuleKind::Boxed(Box::new(rule)), span)
    }

    pub fn punctuation(repr: String, span: Span) -> Self {
        Self(RuleKind::Punctuation(repr), span)
    }
    pub fn keyword(repr: String, span: Span) -> Self {
        Self(RuleKind::Keyword(repr), span)
    }
    pub fn other(repr: String, span: Span) -> Self {
        Self(RuleKind::Other(repr), span)
    }
    pub fn builtin(repr: String, span: Span) -> Self {
        Self(RuleKind::Builtin(repr), span)
    }

    pub fn peg(self) -> Peg {
        Peg::Rule(self)
    }
}
impl Debug for Rule {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?} @{}", self.0, self.1)
    }
}
impl Display for Rule {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// A specific grammar rule variant.
#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum RuleKind {
    /// Matches a sequence of rules, one after another.
    Sequence { rules: Vec<Rule>, weak: bool },
    /// Like a sequence, but every rule can have a name.
    Record(Vec<(Spanned<Option<String>>, Rule)>),

    /// Begins all rules at the same point, using the first one that matches.
    Choice(Vec<Rule>),
    /// Like a choice, but every rule can have a name.
    NamedChoice(Vec<(Spanned<String>, Rule)>),

    /// Matches inside a token group.
    Group(GroupDelimiter, Box<Rule>),

    /// A [repeating rule][`RepeatRule`], delimited with `*` or `+`.
    Repeat(RepeatRule),

    /// Makes a rule optional (allowed to fail). Delimited with `?`.
    Optional(Box<Rule>),
    /// Boxes a rule (permits recursion). Delimited with `^`.
    Boxed(Box<Rule>),

    /// Matches a punctuation token.
    Punctuation(String),
    /// Matches a keyword token.
    Keyword(String),

    /// Matches a different rule.
    Other(String),
    /// Matches a built-in rule (denoted with `@`).
    Builtin(String),
}
impl RuleKind {
    pub fn with(self, span: impl Into<Span>) -> Rule {
        Rule(self, span.into())
    }
}
impl Display for RuleKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RuleKind::Sequence { rules, weak } => match &rules[..] {
                [] => {
                    if *weak {
                        Ok(())
                    } else {
                        write!(f, "<>")
                    }
                }
                [most @ .., last] => {
                    if !*weak {
                        write!(f, "<")?;
                    }
                    for rule in most {
                        write!(f, "{rule} ")?;
                    }
                    write!(f, "{last}")?;
                    if *weak {
                        Ok(())
                    } else {
                        write!(f, ">")
                    }
                }
            },
            RuleKind::Record(fields) => {
                write!(f, "&{{ ")?;
                for (Spanned(_, name), rule) in fields {
                    let name = match name {
                        Some(name) => name,
                        None => "!",
                    };
                    write!(f, "{name}: {rule}; ")?;
                }
                write!(f, "}}")
            }

            RuleKind::Choice(rules) => match &rules[..] {
                [] => write!(f, "!"),
                [most @ .., last] => {
                    write!(f, "<")?;
                    for rule in most {
                        write!(f, "{rule} | ")?;
                    }
                    write!(f, "{last}>")
                }
            },
            RuleKind::NamedChoice(rules) => {
                write!(f, "&[ ")?;
                for (Spanned(_, name), rule) in rules {
                    write!(f, "{name}: {rule}; ")?;
                }
                write!(f, "]")
            }

            RuleKind::Group(delimiter, inner) => {
                write!(f, "{}{}{}", delimiter.opener(), inner, delimiter.closer())
            }
            RuleKind::Repeat(RepeatRule {
                element,
                separator,
                at_least,
                allow_trailing,
            }) => write!(
                f,
                "{} {} {}{}",
                element,
                separator,
                at_least,
                if *allow_trailing { "~" } else { "" }
            ),
            RuleKind::Optional(rule) => write!(f, "{rule}?"),
            RuleKind::Boxed(rule) => write!(f, "{rule}^"),
            RuleKind::Punctuation(punct) => write!(f, "'{punct}'"),
            RuleKind::Keyword(kw) => write!(f, "\"{kw}\""),
            RuleKind::Other(name) => write!(f, "{name}"),
            RuleKind::Builtin(builtin) => write!(f, "@{builtin}"),
        }
    }
}

/// An element of a PEG grammar stack.
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum Peg {
    /// A control element. Should not appear on the stack by the end of parsing.
    Control(Control),
    /// A grammar rule element.
    Rule(Rule),
}
impl Peg {
    pub fn sequence_start(span: Span) -> Self {
        Self::Control(Control(ControlKind::SequenceStart, span))
    }
    pub fn group_start(delimiter: GroupDelimiter, span: Span) -> Self {
        Self::Control(Control(ControlKind::GroupStart(delimiter), span))
    }

    pub fn try_as_rule(self) -> Result<Rule> {
        match self {
            Peg::Rule(rule) => Ok(rule),
            Peg::Control(control) => Err(ErrorKind::StrayControl(control.0).with(control.1)),
        }
    }
    pub fn try_as_mut_rule(&mut self) -> Result<&mut Rule> {
        match self {
            Peg::Rule(rule) => Ok(rule),
            Peg::Control(control) => Err(ErrorKind::StrayControl(control.0).with(control.1)),
        }
    }
    pub fn span(&self) -> Span {
        let (Peg::Control(Control(_, span)) | Peg::Rule(Rule(_, span))) = self;
        *span
    }
}
impl Display for Peg {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Peg::Control(control) => write!(f, "{control}"),
            Peg::Rule(rule) => write!(f, "{rule}"),
        }
    }
}

/// A PEG grammar stack.
pub struct PegStack(pub Vec<Peg>);
impl PegStack {
    pub fn raw_pop_rule(&mut self, operator_span: Span) -> Result<Rule> {
        match self.0.pop() {
            Some(Peg::Rule(rule)) => Ok(rule),
            Some(Peg::Control(control)) => Err(ErrorKind::StrayControl(control.0).with(control.1)),
            None => Err(ErrorKind::StackEmpty("expected rule".into()).with(operator_span)),
        }
    }
    pub fn take_rule(&mut self, operator_span: Span) -> Result<Rule> {
        match self.0.pop() {
            Some(Peg::Rule(Rule(RuleKind::Choice(mut choices), span))) if !choices.is_empty() => {
                let rule = match choices
                    .pop()
                    .expect("internal parser error: choices was in fact empty")
                {
                    Rule(
                        RuleKind::Sequence {
                            mut rules,
                            weak: true,
                        },
                        span,
                    ) => {
                        let rule = rules.pop().ok_or(
                            ErrorKind::StackEmpty(
                                "no more rules to take from choice variant".to_owned(),
                            )
                            .with(operator_span),
                        )?;
                        choices.push(Rule(RuleKind::Sequence { rules, weak: true }, span));
                        rule
                    }
                    other => {
                        choices.push(Rule(
                            RuleKind::Sequence {
                                rules: vec![],
                                weak: true,
                            },
                            span,
                        ));
                        other
                    }
                };

                self.0
                    .push(Peg::Rule(Rule(RuleKind::Choice(choices), span)));
                Ok(rule)
            }
            Some(Peg::Rule(other_rule)) => Ok(other_rule),
            Some(Peg::Control(control)) => Err(ErrorKind::StrayControl(control.0).with(control.1)),
            None => Err(ErrorKind::StackEmpty("expected rule".into()).with(operator_span)),
        }
    }
    pub fn add_rule(&mut self, rule: Rule) {
        match self.0.pop() {
            Some(Peg::Rule(Rule(RuleKind::Choice(mut variants), span))) => {
                if let Some(old_last_variant) = variants.pop() {
                    let total_span = old_last_variant.1 + span;
                    if let RuleKind::Sequence {
                        mut rules,
                        weak: true,
                    } = old_last_variant.0
                    {
                        rules.push(rule);
                        variants.push(Rule(RuleKind::Sequence { rules, weak: true }, total_span));
                    } else {
                        variants.push(Rule::sequence(
                            vec![old_last_variant, rule],
                            true,
                            total_span,
                        ));
                    }
                } else {
                    variants.push(rule);
                }
                self.0.push(Rule::choice(variants, span).peg());
            }
            Some(other) => {
                self.0.push(other);
                self.0.push(rule.peg());
            }
            None => {
                self.0.push(rule.peg());
            }
        }
    }
    pub fn add_rule_kind(&mut self, kind: RuleKind, span: Span) {
        self.add_rule(Rule(kind, span));
    }
}
impl Display for PegStack {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let [most @ .., last] = &self.0[..] else {
            return Ok(());
        };
        for peg in most {
            write!(f, "{peg} ")?;
        }
        write!(f, "{last}")
    }
}

/// Input to a PEG parsing function.
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ParseInput<I: Iterator<Item = Spanned<char>>> {
    chars: I,
    end_span: Span,
}
impl<I: Iterator<Item = Spanned<char>>> ParseInput<I> {
    pub fn new(chars: I, end_span: Span) -> Self {
        Self { chars, end_span }
    }
    pub fn current_span(&self) -> Span
    where
        I: Clone,
    {
        match self.chars.clone().next() {
            Some(Spanned(span, _)) => span,
            None => self.end_span,
        }
    }
    fn eof(&self) -> Error {
        Error::eof(self.end_span)
    }
}
impl<I: Iterator<Item = Spanned<char>>> Iterator for ParseInput<I> {
    type Item = Spanned<char>;
    fn next(&mut self) -> Option<Self::Item> {
        self.chars.next()
    }
}

macro_rules! parse_input {
    () => {
        ParseInput<impl Iterator<Item = Spanned<char>> + Clone>
    };
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
enum ErrorKind {
    ExpectedFound(String, String),
    StackEmpty(String),
    StrayControl(ControlKind),
    EOF,
    InvalidCloser {
        expected: AnyDelimiter,
        got: AnyDelimiter,
    },
    ExistingPreamble(String),
}
impl ErrorKind {
    fn with(self, span: impl Into<Span>) -> Error {
        Error(self, span.into())
    }
}
impl Display for ErrorKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ErrorKind::ExpectedFound(expected, found) => {
                write!(f, "expected {expected}, found {found}")
            }
            ErrorKind::StackEmpty(expected) => write!(f, "stack empty; {expected}"),
            ErrorKind::StrayControl(control) => {
                write!(f, "expected a rule, got a control ({control:?})")
            }
            ErrorKind::EOF => write!(f, "unexpected end of file"),
            ErrorKind::InvalidCloser { expected, got } => write!(
                f,
                "expected {} to match {}, got {}",
                expected.closer(),
                expected.opener(),
                got.closer()
            ),
            ErrorKind::ExistingPreamble(preamble) => {
                write!(f, "preamble #{preamble} already exists")
            }
        }
    }
}
impl core::error::Error for ErrorKind {}

/// The error type.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Error(ErrorKind, Span);
impl Error {
    pub fn span(&self) -> Span {
        self.1
    }
    fn eof(end_span: Span) -> Self {
        ErrorKind::EOF.with(end_span)
    }
}
impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} (at {})", self.0, self.1)
    }
}
impl core::error::Error for Error {}
pub type Result<T> = core::result::Result<T, Error>;

/// Skips over as many whitespace characters as possible.
///
/// Used between many parsing functions.
pub fn skip_ws(input: &mut parse_input!()) {
    while let Some(Spanned(_, ch)) = input.clone().next() {
        if !ch.is_whitespace() {
            return;
        }
        input.next();
    }
}

/// Attempts to parse an identifier. If already at EOF, `Ok(None)` is returned.
pub fn expect_identifier(input: &mut parse_input!()) -> Result<Option<Spanned<String>>> {
    let (mut span, ch) = match input.next() {
        Some(Spanned(span, ch @ pat_ident_start!())) => (span, ch),
        Some(Spanned(span, other)) => {
            return Err(ErrorKind::ExpectedFound(
                "identifier".to_owned(),
                format!("character `{other}`"),
            )
            .with(span));
        }
        None => return Ok(None),
    };
    let mut output = String::from(ch);
    consume_identifier_rest(input, &mut span, &mut output);
    Ok(Some(Spanned(span, output)))
}

/// Similar to `expect_identifier`, but errors on EOF.
pub fn identifier_or_eof(input: &mut parse_input!()) -> Result<Spanned<String>> {
    expect_identifier(input)?.ok_or(input.eof())
}

/// After parsing the first character of an identifier, this can be used to parse the rest of the characters.
pub fn consume_identifier_rest(
    input: &mut parse_input!(),
    into_span: &mut Span,
    into: &mut String,
) {
    while let Some(Spanned(span, ch @ pat_ident_body!())) = input.clone().next() {
        input.next();
        *into_span += span;
        into.push(ch);
    }
}

/// Expects some literal text content to appear in the character stream.
pub fn expect_exactly(input: &mut parse_input!(), string: &str) -> Result<Span> {
    let mut collected = String::new();
    let mut acc_span = input.current_span();
    for ch in string.chars() {
        let next = input.next();
        if let Some(Spanned(span, test_ch)) = next {
            acc_span += span;
            collected.push(test_ch);
            if test_ch != ch {
                return Err(ErrorKind::ExpectedFound(
                    format!("`{string}`"),
                    format!("`{collected}`"),
                )
                .with(span));
            }
        } else {
            return Err(input.eof());
        }
    }
    Ok(acc_span)
}

/// Parses a list of record or named choice fields.
pub fn parse_fields(
    input: &mut parse_input!(),
    terminator: char,
) -> Result<Spanned<Vec<(Spanned<Option<String>>, Rule)>>> {
    let mut record_fields = vec![];
    let mut total_span = input.current_span();
    loop {
        skip_ws(input);
        if let Some(Spanned(span, ch)) = input.clone().next()
            && ch == terminator
        {
            total_span += span;
            input.next();
            break;
        }

        let identifier = if let Some(Spanned(span, '!')) = input.clone().next() {
            input.next();
            Spanned(span, None)
        } else {
            let identifier = expect_identifier(input)?.ok_or(input.eof())?;
            Spanned(identifier.0, Some(identifier.1))
        };
        total_span += identifier.0;

        skip_ws(input);
        total_span += expect_exactly(input, ":")?;

        skip_ws(input);
        let start = input.current_span();
        let mut rule_stack = PegStack(vec![]);
        while let RuleStatus::Continue = next_rule(input, &mut rule_stack)? {
            skip_ws(input);
        }

        let mut rules = rule_stack
            .0
            .into_iter()
            .map(|rule| rule.try_as_rule())
            .collect::<Result<Vec<_>>>()?;
        let rule_span = rules.iter().map(|rule| rule.1).fold(start, |a, b| a + b);
        total_span += rule_span;

        record_fields.push((
            identifier,
            if rules.len() == 1 {
                rules.pop().unwrap()
            } else {
                Rule::sequence(rules, true, rule_span)
            },
        ));
    }
    Ok(Spanned(total_span, record_fields))
}

/// Whether or not rule parsing should continue.
pub enum RuleStatus {
    Continue,
    End,
}

/// Parses the next rule, modifying the stack accordingly. Returns whether or not rule parsing should continue.
pub fn next_rule(input: &mut parse_input!(), stack: &mut PegStack) -> Result<RuleStatus> {
    let Spanned(mut peg_span, ch) = input.next().ok_or(input.eof())?;
    match ch {
        '"' => {
            let mut keyword = String::new();
            loop {
                let Spanned(span, ch) = input.next().ok_or(input.eof())?;
                peg_span += span;
                if ch == '"' {
                    break;
                }
                keyword.push(ch);
            }
            stack.add_rule(Rule::keyword(keyword, peg_span));
        }

        '\'' => {
            let mut punct = String::new();
            loop {
                let Spanned(span, ch) = input.next().ok_or(input.eof())?;
                if ch == '\'' {
                    peg_span += span;
                    break;
                }
                if !matches!(ch, pat_punct!()) {
                    return Err(ErrorKind::ExpectedFound(
                        "punctuation".to_owned(),
                        format!("character `{ch}`"),
                    )
                    .with(span));
                }
                peg_span += span;
                punct.push(ch);
            }
            stack.add_rule(Rule::punctuation(punct, peg_span));
        }

        '*' => {
            let separator = stack.raw_pop_rule(peg_span)?;
            let element = stack.take_rule(peg_span)?;
            let other_span = element.1 + separator.1;
            stack.add_rule(Rule::repeat(
                element,
                separator,
                AtLeast::Zero,
                peg_span + other_span,
            ));
        }
        '+' => {
            let separator = stack.raw_pop_rule(peg_span)?;
            let element = stack.take_rule(peg_span)?;
            let other_span = element.1 + separator.1;
            stack.add_rule(Rule::repeat(
                element,
                separator,
                AtLeast::One,
                peg_span + other_span,
            ));
        }
        '~' => {
            let Rule(kind, span) = stack.take_rule(peg_span)?;
            let RuleKind::Repeat(mut repeat) = kind else {
                return Err(ErrorKind::ExpectedFound(
                    "repetition rule".to_owned(),
                    "other rule".to_owned(),
                )
                .with(peg_span));
            };
            repeat.allow_trailing = true;
            stack.add_rule_kind(RuleKind::Repeat(repeat), peg_span + span);
        }

        '?' => {
            let element = stack.take_rule(peg_span)?;
            let element_span = element.1;
            stack.add_rule(Rule::optional(element, peg_span + element_span));
        }
        '^' => {
            let element = stack.take_rule(peg_span)?;
            let element_span = element.1;
            stack.add_rule(Rule::boxed(element, peg_span + element_span));
        }

        '.' => stack.add_rule(Rule::sequence(vec![], false, peg_span)),
        '<' => stack.0.push(Peg::sequence_start(peg_span)),
        '(' => stack
            .0
            .push(Peg::group_start(GroupDelimiter::Parenthesis, peg_span)),
        '[' => stack
            .0
            .push(Peg::group_start(GroupDelimiter::Bracket, peg_span)),
        '{' => stack
            .0
            .push(Peg::group_start(GroupDelimiter::Brace, peg_span)),
        '>' => {
            let mut sequence = vec![];
            let mut total_span = peg_span;
            loop {
                let peg = stack.0.pop().ok_or(
                    ErrorKind::StackEmpty("missing start control".to_owned()).with(peg_span),
                )?;
                total_span += peg.span();
                match peg {
                    Peg::Rule(rule) => {
                        total_span += rule.1;
                        sequence.push(rule);
                    }
                    Peg::Control(Control(ControlKind::GroupStart(delimiter), span)) => {
                        return Err(ErrorKind::ExpectedFound(
                            "sequence start control (`<`)".into(),
                            format!("group start control ({})", delimiter.opener()),
                        )
                        .with(span));
                    }
                    Peg::Control(Control(ControlKind::SequenceStart, span)) => {
                        sequence.reverse();
                        stack.add_rule(Rule::sequence(sequence, false, total_span + span));
                        break;
                    }
                }
            }
        }
        ch @ (')' | ']' | '}') => {
            let closer = match ch {
                ')' => GroupDelimiter::Parenthesis,
                ']' => GroupDelimiter::Bracket,
                '}' => GroupDelimiter::Brace,
                _ => unreachable!(),
            };
            let mut sequence = vec![];
            let mut inner_span = Span::default();
            loop {
                let peg = stack.0.pop().ok_or(
                    ErrorKind::StackEmpty("missing start control".to_owned()).with(peg_span),
                )?;
                peg_span += peg.span();
                match peg {
                    Peg::Rule(rule) => {
                        inner_span += rule.1;
                        sequence.push(rule);
                    }
                    Peg::Control(Control(ControlKind::GroupStart(opener), span)) => {
                        peg_span += span + inner_span;
                        if opener == closer {
                            sequence.reverse();
                            stack.add_rule(Rule::group(
                                opener,
                                if sequence.len() == 1 {
                                    sequence.pop().unwrap()
                                } else {
                                    Rule::sequence(sequence, true, inner_span)
                                },
                                peg_span,
                            ));
                            break;
                        } else {
                            return Err(ErrorKind::InvalidCloser {
                                expected: opener.into(),
                                got: closer.into(),
                            }
                            .with(span));
                        }
                    }
                    Peg::Control(Control(ControlKind::SequenceStart, span)) => {
                        return Err(ErrorKind::InvalidCloser {
                            expected: AnyDelimiter::AngleBrackets,
                            got: closer.into(),
                        }
                        .with(span));
                    }
                }
            }
        }

        '|' => {
            let after = parse_single_grammar(input)?;
            let first = stack.raw_pop_rule(peg_span)?;
            let rule = match first {
                Rule(RuleKind::Choice(mut choices), span) => {
                    choices.push(after);
                    Rule::choice(choices, peg_span + span)
                }
                other => {
                    let mut first_variant_span = other.1;
                    let mut first_variant_sequence = vec![other];
                    while let Some(peg) = stack.0.pop() {
                        match peg {
                            Peg::Control(control) => {
                                stack.0.push(Peg::Control(control));
                                peg_span += control.1;
                                break;
                            }
                            Peg::Rule(rule) => {
                                first_variant_span += rule.1;
                                first_variant_sequence.push(rule);
                            }
                        }
                    }
                    first_variant_sequence.reverse();
                    let first_variant_rule = if first_variant_sequence.len() == 1 {
                        first_variant_sequence.pop().unwrap()
                    } else {
                        Rule::sequence(first_variant_sequence, true, first_variant_span)
                    };
                    let after_span = after.1;
                    Rule::choice(
                        vec![first_variant_rule, after],
                        first_variant_span + peg_span + after_span,
                    )
                }
            };
            stack.add_rule(rule);
        }

        '@' => {
            let Spanned(span, builtin) = expect_identifier(input)?.ok_or(input.eof())?;
            stack.add_rule(Rule::builtin(builtin, span));
        }

        '&' => match input.next().ok_or(input.eof())? {
            Spanned(span, '{') => {
                peg_span += span;
                let Spanned(span, fields) = parse_fields(input, '}')?;
                peg_span += span;
                stack.add_rule(Rule::record(fields, peg_span));
            }
            Spanned(span, '[') => {
                peg_span += span;
                let Spanned(span, fields) = parse_fields(input, ']')?;
                let fields = fields
                    .into_iter()
                    // TODO ts ugly ah
                    .map(|(name, rule)| {
                        Ok((
                            Spanned(
                                name.0,
                                name.1.ok_or_else(|| {
                                    ErrorKind::ExpectedFound(
                                        "name".to_owned(),
                                        "`!` (named choices cannot have anonymous fields)"
                                            .to_owned(),
                                    )
                                    .with(name.0)
                                })?,
                            ),
                            rule,
                        ))
                    })
                    .collect::<Result<Vec<_>>>()?;
                peg_span += span;
                stack.add_rule(Rule::named_choice(fields, peg_span));
            }
            other => {
                return Err(ErrorKind::ExpectedFound(
                    "one of `{` or `[`".to_owned(),
                    format!("`{}`", other.1),
                )
                .with(other.0));
            }
        },

        ';' => return Ok(RuleStatus::End),

        ch @ pat_ident_start!() => {
            let mut rule_name = String::from(ch);
            consume_identifier_rest(input, &mut peg_span, &mut rule_name);
            stack.add_rule(Rule::other(rule_name, peg_span));
        }

        other => {
            return Err(ErrorKind::ExpectedFound(
                "rule".to_owned(),
                format!("character `{other}`"),
            )
            .with(peg_span));
        }
    }
    Ok(RuleStatus::Continue)
}

/// Parses a full grammar;
/// creates a new stack and calls `next_rule` repeatedly until no control grammars are on the stack and the stack is not empty.
pub fn parse_single_grammar(input: &mut parse_input!()) -> Result<Rule> {
    let mut stack = PegStack(vec![]);
    while stack.0.is_empty()
        || stack
            .0
            .iter()
            .any(|peg: &Peg| matches!(&peg, Peg::Control(_)))
    {
        skip_ws(input);
        if let RuleStatus::End = next_rule(input, &mut stack)? {
            return Err(input.eof());
        }
    }
    Ok(stack.0.pop().unwrap().try_as_rule()?)
}

/// A `#keywords` preamble.
#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Keywords {
    pub soft: Vec<Spanned<String>>,
    pub hard: Vec<Spanned<String>>,
}
impl Display for Keywords {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let [most @ .., last] = &self.soft[..] {
            write!(f, "#keywords soft: ")?;
            for Spanned(_, word) in most {
                write!(f, "{word} ")?;
            }
            writeln!(f, "{};", last.1)?;
        }
        if let [most @ .., last] = &self.hard[..] {
            write!(f, "#keywords hard: ")?;
            for Spanned(_, word) in most {
                write!(f, "{word} ")?;
            }
            writeln!(f, "{};", last.1)?;
        }
        Ok(())
    }
}

/// The set of preambles.
#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Preambles {
    pub keywords: Keywords,
}
impl Display for Preambles {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "{}", self.keywords)
    }
}

/// A TECTA PEG module.
#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct TectaPegModule {
    pub preambles: Preambles,
    pub rules: BTreeMap<String, Vec<Rule>>,
}
impl Display for TectaPegModule {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.preambles)?;
        for (rule_name, rules) in &self.rules {
            write!(f, "{rule_name} =")?;
            for rule in rules {
                write!(f, " {rule}")?;
            }
            writeln!(f, ";")?;
        }
        Ok(())
    }
}

/// Parses a list of identifiers.
pub fn parse_basic_identifier_list(input: &mut parse_input!()) -> Result<Vec<Spanned<String>>> {
    let mut identifiers = vec![];
    loop {
        skip_ws(input);
        if let Some(Spanned(_, ';')) = input.clone().next() {
            return Ok(identifiers);
        }
        identifiers.push(identifier_or_eof(input)?);
    }
}

/// Parses a TECTA PEG module; a set of preambles and rule definitions.
pub fn parse_module_inner(input: &mut parse_input!()) -> Result<TectaPegModule> {
    let mut module = TectaPegModule::default();
    loop {
        skip_ws(input);
        // TODO: split preamble into function
        if let Some(Spanned(_, '#')) = input.clone().next() {
            input.next();
            let Spanned(span, name) = identifier_or_eof(input)?;
            match &name[..] {
                "keywords" => {
                    skip_ws(input);
                    let Spanned(span, name) = identifier_or_eof(input)?;
                    let is_hard = match &name[..] {
                        "hard" => true,
                        "soft" => false,
                        other => {
                            return Err(ErrorKind::ExpectedFound(
                                "keyword hardness".into(),
                                format!("`{other}`"),
                            )
                            .with(span));
                        }
                    };

                    let colon_span = expect_exactly(input, ":")?;
                    let specified_keywords = parse_basic_identifier_list(input)?;

                    if specified_keywords.is_empty() {
                        return Err(ErrorKind::ExpectedFound(
                            "non-empty keyword list".into(),
                            "empty list".into(),
                        )
                        .with(colon_span));
                    }

                    let target_keyword_set = if is_hard {
                        &mut module.preambles.keywords.hard
                    } else {
                        &mut module.preambles.keywords.soft
                    };
                    if !target_keyword_set.is_empty() {
                        return Err(ErrorKind::ExistingPreamble(format!(
                            "keywords {}",
                            if is_hard { "hard" } else { "soft" }
                        ))
                        .with(colon_span));
                    }

                    *target_keyword_set = specified_keywords;
                }
                other => {
                    return Err(
                        ErrorKind::ExpectedFound("preamble".into(), format!("`{other}`"))
                            .with(span),
                    );
                }
            }
            expect_exactly(input, ";")?;
        } else if let Some(Spanned(_, rule_name)) = expect_identifier(input)? {
            skip_ws(input);
            let _eq_span = expect_exactly(input, "=")?;
            skip_ws(input);

            let mut peg_stack = PegStack(vec![]);
            while let RuleStatus::Continue = next_rule(input, &mut peg_stack)? {
                skip_ws(input);
            }
            skip_ws(input);

            let sequence = peg_stack
                .0
                .into_iter()
                .map(Peg::try_as_rule)
                .collect::<Result<Vec<_>>>()?;
            module.rules.insert(rule_name, sequence);
        } else {
            break;
        }
    }
    Ok(module)
}

/// Parses a TECTA PEG module from a string. See [`parse_module`].
pub fn parse_module(str: &str) -> Result<TectaPegModule> {
    let end_span = match str.lines().enumerate().last() {
        Some((index, line)) => Span {
            start_line: index + 1,
            end_line: index + 1,
            start_column: line.len(),
            end_column: line.len(),
        },
        None => Span::default(),
    };
    parse_module_inner(&mut ParseInput {
        chars: SpanningChars::new(str.chars()),
        end_span,
    })
}