1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
//! Lexer rules.

use core::fmt;
use std::ops::Range;
use std::ops::RangeBounds;

use byteyarn::Yarn;
use twie::Trie;

use crate::token;
use crate::Never;
use crate::WrongKind;

/// A general rule.
///
/// This trait is implemented by all other types in this module that represent
/// a rule.
pub trait Rule: fmt::Debug + TryFrom<Any> + Into<Any> + 'static {
  /// This rule's corresponding token type.
  ///
  /// [`Comment`], which has no corresponding token, has this as [`Never`].
  type Token<'lex>: token::Token<'lex>;

  /// Converts a reference to [`Any`] to a reference to this kind of rule.
  fn try_from_ref(value: &Any) -> Result<&Self, WrongKind>;
}

pub use crate::token::Sign;

/// Any of the possible rule types in a [`Spec`][crate::Spec].
#[derive(Debug)]
#[allow(missing_docs)]
pub enum Any {
  Keyword(Keyword),
  Bracket(Bracket),
  Ident(Ident),
  Quoted(Quoted),
  Comment(Comment),
  Digital(Digital),
}

impl Any {
  /// The bare name shown for whatever this token is in `fmt::Debug`.
  pub(crate) fn debug_name(&self) -> &'static str {
    match self {
      Any::Keyword(_) => "Keyword",
      Any::Bracket(_) => "Bracket",
      Any::Ident(_) => "Ident",
      Any::Digital(_) => "Digital",
      Any::Quoted(_) => "Quoted",
      Any::Comment(_) => "Comment",
    }
  }
}

impl Rule for Any {
  type Token<'lex> = token::Any<'lex>;

  fn try_from_ref(value: &Any) -> Result<&Self, WrongKind> {
    Ok(value)
  }
}

/// The end-of-file.
///
/// This rule only exists so that [`token::Eof`] can have a corresponding rule.
/// It is not constructible.
#[derive(Debug)]
pub struct Eof(Never);

impl Rule for Eof {
  type Token<'lex> = token::Eof<'lex>;

  fn try_from_ref(value: &Any) -> Result<&Self, WrongKind> {
    Err(WrongKind { want: "Eof", got: value.debug_name() })
  }
}

impl From<Eof> for Any {
  fn from(value: Eof) -> Self {
    value.0.from_nothing_anything()
  }
}

impl TryFrom<Any> for Eof {
  type Error = WrongKind;

  fn try_from(value: Any) -> Result<Self, Self::Error> {
    Err(WrongKind { want: "Eof", got: value.debug_name() })
  }
}

/// A keyword, i.e., an exact well-known string, such as `+`, `class`, and
/// `#define`.
///
/// Keywords are similar to identifiers, but their content is always the same
/// fixed string.
#[derive(Debug)]
pub struct Keyword {
  pub(crate) value: Yarn,
}

impl Keyword {
  /// Constructs a new keyword rule with the exact string it matches.
  pub fn new(value: impl Into<Yarn>) -> Self {
    Self { value: value.into() }
  }
}

impl<Y: Into<Yarn>> From<Y> for Keyword {
  fn from(value: Y) -> Self {
    Keyword::new(value)
  }
}

impl Rule for Keyword {
  type Token<'lex> = token::Keyword<'lex>;

  fn try_from_ref(value: &Any) -> Result<&Self, WrongKind> {
    match value {
      Any::Keyword(rule) => Ok(rule),
      _ => Err(WrongKind { want: "Keyword", got: value.debug_name() }),
    }
  }
}

impl From<Keyword> for Any {
  fn from(value: Keyword) -> Self {
    Any::Keyword(value)
  }
}

impl TryFrom<Any> for Keyword {
  type Error = WrongKind;

  fn try_from(value: Any) -> Result<Self, Self::Error> {
    match value {
      Any::Keyword(rule) => Ok(rule),
      _ => Err(WrongKind { want: "Keyword", got: value.debug_name() }),
    }
  }
}

/// A paired bracket, such as `(..)`.
///
/// Brackets are pairs of delimiters with tokens between them. They are used as
/// both standalone rules and to define other rules, such as [`Quoted`]s
#[derive(Debug)]
pub struct Bracket {
  pub(crate) kind: BracketKind,
}

impl Bracket {
  /// An ordinary pair of delimiters: an opening string and its matching
  /// closing string.
  ///
  /// # Panics
  ///
  /// Panics if either of `open` or `close` is empty.
  pub fn paired(open: impl Into<Yarn>, close: impl Into<Yarn>) -> Self {
    let open = open.into();
    let close = close.into();
    assert!(
      !open.is_empty() && !close.is_empty(),
      "both arguments to Bracket::paired() must be non-empty"
    );

    Self { kind: BracketKind::Paired(open, close) }
  }

  /// A Rust raw string-like bracket. This corresponds to `##"foo"##` raw
  /// strings in Rust.
  ///
  /// This kind of bracket must be special-cased, since it makes the grammar
  /// non-context-sensitive. To lex it, first we try to lex `open_start` if
  /// present, then we try to lex as many copies of `repeating` as possible,
  /// and then an `open_end`. Then we lex the contents until we lex a
  /// `close_start`, then the same number of copies of `repeating`,
  /// and then a `close_end`.
  ///
  /// To specify the exact syntax from Rust, you would write
  /// `Bracket::rust_style(('#', 0), ('r', '"'), ('"', ""))`.
  ///
  /// # Panics
  ///
  /// Panics if `repeating` is empty or if both of `open_start` and `open_end`,
  /// or `close_start` and `close_end`, are empty, or if either `open_end` or
  /// `close_end` start with `repeating`.
  #[track_caller]
  pub fn rust_style(
    repeating: impl Into<Yarn>,
    (open_start, open_end): (impl Into<Yarn>, impl Into<Yarn>),
    (close_start, close_end): (impl Into<Yarn>, impl Into<Yarn>),
  ) -> Self {
    let repeating = repeating.into();
    let open = (open_start.into(), open_end.into());
    let close = (close_start.into(), close_end.into());

    assert!(
      !repeating.is_empty(),
      "the repeating argument of Bracket::rust_style() cannot be empty"
    );
    assert!(
      !open.0.is_empty() || !open.1.is_empty(),
      "open_start and open_end cannot both be empty"
    );
    assert!(
      !close.0.is_empty() || !close.1.is_empty(),
      "close_start and close_end cannot both be empty"
    );
    assert!(
      !open.1.starts_with(repeating.as_str())
        && !close.1.starts_with(repeating.as_str()),
      "open_end and close_end cannot start with the repeating string"
    );

    Self {
      kind: BracketKind::RustLike { repeating, open, close },
    }
  }

  /// A C++ raw string-like bracket. This corresponds to `R"xyz(foo)xyz"` raw
  /// strings in C++.
  ///
  /// This is similar to [`Bracket::rust_style()`], but for C++'s raw
  /// strings. Instead of parsing repeated copies of some string, we parse a
  /// whole identifier (prefixes and suffixes and all) and expect it to be at
  /// the other end.
  ///
  /// To specify the exact syntax from C++, you would write
  /// `Bracket::cxx_style(Ident::new(), ("R\"", '('), (')', '"'))`.
  ///
  /// # Panics
  ///
  /// Panics if `ident` has any affixes or if both of `open_start` and `open_end`
  /// are empty, or `close_start` and `close_end` are empty and `ident` has a
  /// minimum length of zero.
  #[track_caller]
  pub fn cxx_style(
    ident: Ident,
    (open_start, open_end): (impl Into<Yarn>, impl Into<Yarn>),
    (close_start, close_end): (impl Into<Yarn>, impl Into<Yarn>),
  ) -> Self {
    assert!(
      ident.affixes.prefixes.is_empty() && ident.affixes.prefixes.is_empty(),
      "Bracket::cxx_style() requires an identifier with no affixes"
    );

    let open = (open_start.into(), open_end.into());
    let close = (close_start.into(), close_end.into());
    assert!(
      !open.0.is_empty() || !open.1.is_empty(),
      "open_start and open_end cannot both be empty"
    );
    assert!(
      !close.0.is_empty() || !close.1.is_empty() || ident.min_len > 0,
      "close_start and close_end cannot both be empty with ident having zero minimum length"
    );

    Self {
      kind: BracketKind::CxxLike { ident_rule: ident, open, close },
    }
  }
}

#[derive(Debug)]
pub(crate) enum BracketKind {
  Paired(Yarn, Yarn),
  RustLike {
    repeating: Yarn,
    open: (Yarn, Yarn),
    close: (Yarn, Yarn),
  },
  CxxLike {
    ident_rule: Ident,
    open: (Yarn, Yarn),
    close: (Yarn, Yarn),
  },
}

impl Rule for Bracket {
  type Token<'lex> = token::Bracket<'lex>;

  fn try_from_ref(value: &Any) -> Result<&Self, WrongKind> {
    match value {
      Any::Bracket(rule) => Ok(rule),
      _ => Err(WrongKind { want: "Bracket", got: value.debug_name() }),
    }
  }
}

impl From<Bracket> for Any {
  fn from(value: Bracket) -> Self {
    Any::Bracket(value)
  }
}

impl<Y: Into<Yarn>, Z: Into<Yarn>> From<(Y, Z)> for Bracket {
  fn from((y, z): (Y, Z)) -> Self {
    Bracket::paired(y, z)
  }
}

impl TryFrom<Any> for Bracket {
  type Error = WrongKind;

  fn try_from(value: Any) -> Result<Self, Self::Error> {
    match value {
      Any::Bracket(rule) => Ok(rule),
      _ => Err(WrongKind { want: "Bracket", got: value.debug_name() }),
    }
  }
}

#[derive(Debug, Default)]
pub(crate) struct Affixes {
  prefixes: Vec<Yarn>,
  suffixes: Vec<Yarn>,
}

impl Affixes {
  const EMPTY: &'static [Yarn] = &[Yarn::new("")];
  pub fn prefixes(&self) -> &[Yarn] {
    if self.prefixes.is_empty() {
      return Self::EMPTY;
    }
    &self.prefixes
  }
  pub fn suffixes(&self) -> &[Yarn] {
    if self.suffixes.is_empty() {
      return Self::EMPTY;
    }
    &self.suffixes
  }
}

impl fmt::Display for Affixes {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    if !self.prefixes.is_empty() {
      for (i, pre) in self.prefixes.iter().enumerate() {
        if i != 0 {
          f.write_str(", ")?;
        }
        write!(f, "`{pre}`-")?;
      }

      f.write_str("prefixed")?;
    }

    if !self.suffixes.is_empty() {
      if !self.prefixes.is_empty() {
        f.write_str(", ")?;
      }

      for (i, pre) in self.suffixes.iter().enumerate() {
        if i != 0 {
          f.write_str(", ")?;
        }
        write!(f, "`{pre}`-")?;
      }

      f.write_str("suffixed")?;
    }

    if !self.prefixes.is_empty() || !self.suffixes.is_empty() {
      f.write_str(" ")?;
    }

    Ok(())
  }
}
macro_rules! affixes {
  () => {
    /// Adds a prefix for this rule.
    ///
    /// If *any* prefixes are added, this rule *must* start with one of them.
    /// To make prefixes optional, add `""` as a prefix.
    pub fn prefix(self, prefix: impl Into<Yarn>) -> Self {
      self.prefixes([prefix])
    }

    /// Adds a suffix for this rule.
    ///
    /// If *any* suffixes are added, this rule *must* end with one of them.
    /// To make suffixes optional, add `""` as a suffix.
    pub fn suffix(self, suffix: impl Into<Yarn>) -> Self {
      self.suffixes([suffix])
    }

    /// Adds prefixes for this rule.
    ///
    /// If *any* prefixes are added, this rule *must* start with one of them.
    /// To make prefixes optional, add `""` as a prefix.
    pub fn prefixes<Y: Into<Yarn>>(
      mut self,
      prefixes: impl IntoIterator<Item = Y>,
    ) -> Self {
      self
        .affixes
        .prefixes
        .extend(prefixes.into_iter().map(Y::into));
      self
    }

    /// Adds suffixes for this rule.
    ///
    /// If *any* suffixes are added, this rule *must* end with one of them.
    /// To make suffixes optional, add `""` as a suffix.
    pub fn suffixes<Y: Into<Yarn>>(
      mut self,
      suffixes: impl IntoIterator<Item = Y>,
    ) -> Self {
      self
        .affixes
        .suffixes
        .extend(suffixes.into_iter().map(Y::into));
      self
    }
  };
}

/// A identifier rule.
///
/// Identifiers are self-delimiting "words" like `foo` and `黒猫`.
#[derive(Default, Debug)]
pub struct Ident {
  pub(crate) ascii_only: bool,
  pub(crate) extra_starts: String,
  pub(crate) extra_continues: String,
  pub(crate) affixes: Affixes,
  pub(crate) min_len: usize,
}

impl Ident {
  /// Creates a new identifier rule.
  ///
  /// By default, this rule accepts any
  /// [Unicode XID](https://unicode.org/reports/tr31/).
  pub fn new() -> Self {
    Self::default()
  }

  /// Makes this rule reject any non-ASCII characters (i.e., outside of
  /// the `[A-Za-z0-9_]` range).
  pub fn ascii_only(mut self) -> Self {
    self.ascii_only = true;
    self
  }

  /// Adds an additional start character for this rule.
  ///
  /// Start characters are any characters that can appear anywhere on an
  /// identifier, including the start.
  pub fn extra_start(self, c: char) -> Self {
    self.extra_starts([c])
  }

  /// Adds additional start characters for this rule.
  ///
  /// Start characters are any characters that can appear anywhere on an
  /// identifier, including the start.
  pub fn extra_starts(mut self, chars: impl IntoIterator<Item = char>) -> Self {
    self.extra_starts.extend(chars);
    self
  }

  /// Adds an additional continue character for this rule.
  ///
  /// Continue characters are any characters that can appear anywhere on an
  /// identifier, except the start.
  pub fn extra_continue(self, c: char) -> Self {
    self.extra_continues([c])
  }

  /// Adds additional continue characters for this rule.
  ///
  /// Continue characters are any characters that can appear anywhere on an
  /// identifier, except the start.
  pub fn extra_continues(
    mut self,
    chars: impl IntoIterator<Item = char>,
  ) -> Self {
    self.extra_continues.extend(chars);
    self
  }

  /// Sets the minimum length of this identifier, in Unicode scalars (i.e.,
  /// `char`s).
  ///
  /// This may be set to zero, but zero-length identifiers will never generate
  /// a token; they may be used as part of other rules.
  pub fn min_len(mut self, len: usize) -> Self {
    self.min_len = len;
    self
  }

  affixes!();
}

impl Rule for Ident {
  type Token<'lex> = token::Ident<'lex>;

  fn try_from_ref(value: &Any) -> Result<&Self, WrongKind> {
    match value {
      Any::Ident(rule) => Ok(rule),
      _ => Err(WrongKind { want: "Ident", got: value.debug_name() }),
    }
  }
}

impl From<Ident> for Any {
  fn from(value: Ident) -> Self {
    Any::Ident(value)
  }
}

impl TryFrom<Any> for Ident {
  type Error = WrongKind;

  fn try_from(value: Any) -> Result<Self, Self::Error> {
    match value {
      Any::Ident(rule) => Ok(rule),
      _ => Err(WrongKind { want: "Ident", got: value.debug_name() }),
    }
  }
}

/// A quoted string rule.
///
/// Quoted strings consist of one or more [`Bracket`] which capture the
/// Unicode scalars between them. No lexing occurs between these brackets.
///
/// Escape sequences are processed, which generate `u32` codes (which can be
/// used to represent values not representable as `char`, particularly for
/// non-Unicode target encodings).
#[derive(Debug)]
pub struct Quoted {
  pub(crate) bracket: Bracket,
  pub(crate) escapes: Trie<str, Escape>,
  pub(crate) affixes: Affixes,
}

impl Quoted {
  /// Creates a new quoted string rule with the given quote character..
  ///
  /// This function is intended for the extremely common case that both sides of
  /// a quoted thing have the exact same bracket on either side.
  pub fn new(quote: impl Into<Yarn>) -> Self {
    let quote = quote.into();
    Self::with(Bracket::paired(quote.clone(), quote))
  }

  /// Creates a new quoted string rule with the given bracket.
  pub fn with(bracket: Bracket) -> Self {
    Self {
      bracket,
      escapes: Trie::new(),
      affixes: Affixes::default(),
    }
  }

  /// Adds a basic escape rule to this rule.
  ///
  /// A basic escape is one that just appears literally in the string,
  /// like `\n`.
  ///
  /// ```
  /// # use ilex::rule::*;
  /// Quoted::new('"')
  ///   .escape(r"\n");
  /// ```
  ///
  /// # Panics
  ///
  /// Panics if the key is empty.
  pub fn escape(self, key: impl Into<Yarn>) -> Self {
    self.escapes([key])
  }

  /// Adds multiple new basic escape rules to this rule.
  ///
  /// Basic escapes are the most common type of escape, so they get a
  /// dedicated multi-helper.
  ///
  /// ```
  /// # use ilex::rule::*;
  /// Quoted::new('"')
  ///   .escapes([r"\n", r"\\"]);
  /// ```
  ///
  /// # Panics
  ///
  /// Panics if any of the keys are empty.
  pub fn escapes<Y: Into<Yarn>>(
    mut self,
    keys: impl IntoIterator<Item = Y>,
  ) -> Self {
    for key in keys {
      let key = key.into();
      assert!(!key.is_empty());
      self.escapes.insert(&key, Escape::Basic);
    }
    self
  }

  /// Adds an invalid escape rule to this rule.
  ///
  /// This is intended for catching things that look like escape sequences
  /// but aren't, and should be diagnosed, like a single \ in many langauges.
  ///
  /// ```
  /// # use ilex::rule::*;
  /// Quoted::new('"')
  ///   .escape(r"\");
  /// ```
  ///
  /// # Panics
  ///
  /// Panics if the key is empty.
  pub fn invalid_escape(mut self, key: impl Into<Yarn>) -> Self {
    let key = key.into();
    assert!(!key.is_empty());
    self.escapes.insert(&key, Escape::Invalid);
    self
  }

  /// Adds a fixed-length escape rule to this rule.
  ///
  /// A fixed-length escape requires some number of characters after
  /// its key (which may not be the string's quotation characters). For example,
  /// `\x` in Rust is a fixed-length escape.
  ///
  /// ```
  /// # use ilex::rule::*;
  /// Quoted::new('"')
  ///   .fixed_length_escape(r"\x", 2);
  /// ```
  ///
  /// # Panics
  ///
  /// Panics if `len == 0`, or if the key is empty.
  pub fn fixed_length_escape(mut self, key: impl Into<Yarn>, len: u32) -> Self {
    let key = key.into();
    assert!(!key.is_empty());
    assert!(len != 0, "cannot create a fixed length escape with length zero");
    self.escapes.insert(&key, Escape::Fixed(len));
    self
  }

  /// Adds a bracketed escape rule to this rule.
  ///
  /// A fixed-length escape is followed by bracket-delimited characters, such as
  /// `\u{...}` in Rust.
  ///
  /// ```
  /// # use ilex::rule::*;
  /// Quoted::new('"')
  ///   .fixed_length_escape(r"\x", 2);
  /// ```
  ///
  /// # Panics
  ///
  /// Panics if either bracket is empty.
  pub fn bracketed_escape(
    mut self,
    key: impl Into<Yarn>,
    open: impl Into<Yarn>,
    close: impl Into<Yarn>,
  ) -> Self {
    let key = key.into();
    assert!(!key.is_empty());
    let (open, close) = (open.into(), close.into());
    assert!(
      !open.is_empty() && !close.is_empty(),
      "cannot create a bracketed escape with empty brackets"
    );
    self.escapes.insert(&key, Escape::Bracketed(open, close));
    self
  }

  /// Adds the Rust escaping rules to this rule.
  pub fn add_rust_escapes(self) -> Self {
    self
      .invalid_escape(r"\")
      .escapes([r"\0", r"\n", r"\r", r"\t", r"\\", "\\\"", r"\'"])
      .fixed_length_escape(r"\x", 2)
      .bracketed_escape(r"\u", '{', '}')
  }

  affixes!();
}

impl From<Bracket> for Quoted {
  fn from(value: Bracket) -> Self {
    Self::with(value)
  }
}

impl Rule for Quoted {
  type Token<'lex> = token::Quoted<'lex>;

  fn try_from_ref(value: &Any) -> Result<&Self, WrongKind> {
    match value {
      Any::Quoted(rule) => Ok(rule),
      _ => Err(WrongKind { want: "Quoted", got: value.debug_name() }),
    }
  }
}

impl From<Quoted> for Any {
  fn from(value: Quoted) -> Self {
    Any::Quoted(value)
  }
}

impl TryFrom<Any> for Quoted {
  type Error = WrongKind;

  fn try_from(value: Any) -> Result<Self, Self::Error> {
    match value {
      Any::Quoted(rule) => Ok(rule),
      _ => Err(WrongKind { want: "Quoted", got: value.debug_name() }),
    }
  }
}

/// A rule to apply to resolve an escape sequence.
#[derive(Debug)]
pub(crate) enum Escape {
  /// This escape is always invalid. Useful for catching e.g. a single \ that
  /// is not followed by an actually-valid escape.
  Invalid,

  /// The escape is just a literal for another character, such as `\n`.
  Basic,

  /// The escape consumes the next `char_count` Unicode scalars after the
  /// key (the character after the escape initiation character) and passes
  /// them to `parse`, which converts it into a `u32` character code.
  ///
  /// This can be used to implement escapes like `\x` (aka `\xNN`) and the
  /// C++ version of `\u` (aka `\uNNNN`). This can also be used to implement
  /// something like C's octal escapes (aka `\NNN`) using an escape key of `\`.
  Fixed(u32),

  /// The escape text delimited by `bracket` after the
  /// key (the character after the escape initiation character) and passes
  /// them to `parse`, which converts it into a `u32` character code.
  ///
  /// This can be used to implement escapes like Rust's version of `\u`
  /// (aka `\u{NNNN}`).
  Bracketed(Yarn, Yarn),
}

/// A digital literal rule.
///
/// Digital tokens are things that resemble numbers `1`, `0xdeadbeef` and `3.14`.
/// However, this rule can be used to parse other things, like LLVM's integer
/// types `i32`, version numbers like `v1.0.2`, and more.
#[derive(Debug)]
pub struct Digital {
  pub(crate) mant: Digits,

  pub(crate) exps: Vec<(Yarn, Digits)>,
  pub(crate) max_exps: u32,

  pub(crate) separator: Yarn,
  pub(crate) corner_cases: SeparatorCornerCases,

  pub(crate) point: Yarn,

  pub(crate) affixes: Affixes,
}

/// Places in which a separator in a [`Digital`] is allowed.
///
/// There is no configuration for whether the separator is permitted
/// "internally", since that is always allowed. (e.g., `1_000`).
///
/// See [`Digital::separator_with()`].
#[derive(Debug)]
pub struct SeparatorCornerCases {
  /// As a prefix to the whole [`Digital`] (after the sign and prefix). E.g.,
  /// is `-_12.34e56` allowed?
  ///
  /// Defaults to false.
  pub prefix: bool,
  /// As a suffix to the whole [`Digital`] (before the literal's suffix). E.g.,
  /// are `12_`, `12.34_`, or `12e56_` allowed?
  ///
  /// Defaults to false.
  pub suffix: bool,
  /// Around a point. E.g. is `12_.34` or `12._34` allowed?
  ///
  /// Defaults to true.
  pub around_point: bool,
  /// Around a an exponent marker. E.g. is `12_e34` or `12e_34` allowed?
  ///
  /// Defaults to true.
  pub around_exp: bool,
}

impl Default for SeparatorCornerCases {
  fn default() -> Self {
    SeparatorCornerCases {
      prefix: false,
      suffix: false,
      around_point: true,
      around_exp: true,
    }
  }
}

impl Digital {
  /// Creates a new rule with the given radix (which must be between 2 and 16).
  ///
  /// For example, `Digital::new(16)` creates a rule for hexadecimal.
  pub fn new(radix: u8) -> Self {
    assert!(
      (2..=16).contains(&radix),
      "radix must be within 2..=16, got {radix}"
    );

    Self::from_digits(Digits::new(radix))
  }

  /// Creates a new rule from a [`Digits`].
  pub fn from_digits(digits: Digits) -> Self {
    Self {
      mant: digits,
      exps: Vec::new(),
      max_exps: 1,
      separator: "".into(),
      corner_cases: Default::default(),
      point: ".".into(),
      affixes: Affixes::default(),
    }
  }

  /// Sets the digit separator for this rule.
  ///
  /// A separator is a character that can occur within a number but which is
  /// ignored, like `_` in Rust or `'` in C++.
  pub fn separator(self, sep: impl Into<Yarn>) -> Self {
    self.separator_with(sep, Default::default())
  }

  /// Sets the digit separator for this rule, and its "corner case" behaviors.
  ///
  /// A separator is a character that can occur within a number but which is
  /// ignored, like `_` in Rust or `'` in C++.
  pub fn separator_with(
    mut self,
    sep: impl Into<Yarn>,
    corner_cases: SeparatorCornerCases,
  ) -> Self {
    self.separator = sep.into();
    self.corner_cases = corner_cases;
    self
  }

  /// Sets the point (e.g. decimal point) for this rule.
  ///
  /// This defaults to `.`, but could be repurposed into, say, `/` for a
  /// date literal
  pub fn point(mut self, x: impl Into<Yarn>) -> Self {
    self.point = x.into();
    assert!(!self.point.is_empty(), "the point separator cannot be empty");
    self
  }

  /// Adds a new kind of sign to this rule.
  ///
  /// Signs can appear in front of a block of digits and specify a [`Sign`]
  /// value. If this is value represents the mantissa digits of a [`Digital`],
  /// it
  pub fn sign(mut self, prefix: impl Into<Yarn>, value: Sign) -> Self {
    self.mant = self.mant.sign(prefix, value);
    self
  }

  /// Adds the plus sign with the usual meaning.
  pub fn plus(self) -> Self {
    self.sign('+', Sign::Pos)
  }

  /// Adds the mins sign with the usual meaning.
  pub fn minus(self) -> Self {
    self.sign('-', Sign::Neg)
  }

  /// Sets the maximum number of decimal points; defailts to `..=0`.
  pub fn point_limit(mut self, range: Range<u32>) -> Self {
    self.mant = self.mant.point_limit(range);
    self
  }

  /// Sets the exponent part information, for e.g. scientific notation in
  /// floating point numbers.
  ///
  /// `delim` is the character that introduces this type of exponent. A digital
  /// rule may have multiple kinds of exponents.
  ///
  /// A digital rule can have multiple exponents, and a corresponding token
  /// can have multiple exponents in sequence, if that is configured.
  pub fn exponent(mut self, delim: impl Into<Yarn>, exp: Digits) -> Self {
    self.exps.push((delim.into(), exp));
    self
  }

  /// Convenience function for setting an exponent with many delimiters.
  pub fn exponents<Y: Into<Yarn>>(
    mut self,
    delims: impl IntoIterator<Item = Y>,
    exp: Digits,
  ) -> Self {
    for delim in delims {
      self.exps.push((delim.into(), exp.clone()));
    }
    self
  }

  /// Sets the maximum number of exponents a token matched from this rule can
  /// have.
  ///
  /// Defaults to 1 (although if there are *no* configured exponent rules, it
  /// will never have one).
  pub fn max_exponents(mut self, limit: u32) -> Self {
    self.max_exps = limit;
    self
  }

  affixes!();
}

/// A digit chunk within a [`Digital`].
///
/// This is used to describe the format of both main chunk of digits
/// (the mantissa), and its exponents.
#[derive(Debug, Clone)]
pub struct Digits {
  pub(crate) radix: u8,
  pub(crate) signs: Vec<(Yarn, Sign)>,
  pub(crate) min_chunks: u32,
  pub(crate) max_chunks: u32,
}

impl Digits {
  /// Creates a new base, with the given radix (which must be between 2 and 16).
  ///
  /// For example, `Digital::new(16)` creates a base for hexadecimal.
  pub fn new(radix: u8) -> Self {
    assert!(
      (2..=16).contains(&radix),
      "radix must be within 2..=16, got {radix}"
    );

    Self {
      radix,
      signs: Vec::new(),
      min_chunks: 1,
      max_chunks: 1,
    }
  }

  /// Returns the name of this rule's radix (e.g., "binary").
  /// Useful for diagnostics.
  pub fn radix_name(&self) -> &'static str {
    match self.radix {
      2 => "binary",
      3 => "ternary",
      4 => "quaternary",
      5 => "quinary",
      6 => "senary",
      7 => "septenary",
      8 => "octal",
      9 => "nonary",
      10 => "decimal",
      11 => "undecimal",
      12 => "duodecimal",
      13 => "tridecimal",
      14 => "tetradecimal",
      15 => "pentadecimal",
      16 => "hexadecmial",
      _ => unreachable!(),
    }
  }

  /// Adds a new kind of sign to this digit block.
  ///
  /// Signs can appear in front of a block of digits and specify a [`Sign`]
  /// value. If this is value represents the mantissa digits of a [`Digital`],
  /// it
  pub fn sign(mut self, prefix: impl Into<Yarn>, value: Sign) -> Self {
    self.signs.push((prefix.into(), value));
    self
  }

  /// Adds the plus sign with the usual meaning.
  pub fn plus(self) -> Self {
    self.sign('+', Sign::Pos)
  }

  /// Adds the mins sign with the usual meaning.
  pub fn minus(self) -> Self {
    self.sign('-', Sign::Neg)
  }

  /// Sets the maximum number of decimal points.
  ///
  /// This may be zero for an integer, or one for a floating point number.
  ///
  /// It may also be set to higher values, which allows parsing of things that
  /// look like version strings, e.g. `1.0.0`.
  pub fn point_limit(mut self, range: impl RangeBounds<u32>) -> Self {
    self.min_chunks = match range.start_bound() {
      std::ops::Bound::Included(&x) => x.saturating_add(1),
      std::ops::Bound::Excluded(&x) => x.saturating_add(2),
      std::ops::Bound::Unbounded => 1,
    };
    self.max_chunks = match range.end_bound() {
      std::ops::Bound::Included(&x) => x.saturating_add(1),
      std::ops::Bound::Excluded(&x) => x,
      std::ops::Bound::Unbounded => u32::MAX,
    };

    self
  }
}

impl Rule for Digital {
  type Token<'lex> = token::Digital<'lex>;

  fn try_from_ref(value: &Any) -> Result<&Self, WrongKind> {
    match value {
      Any::Digital(rule) => Ok(rule),
      _ => Err(WrongKind { want: "Digital", got: value.debug_name() }),
    }
  }
}

impl From<Digital> for Any {
  fn from(value: Digital) -> Self {
    Any::Digital(value)
  }
}

impl TryFrom<Any> for Digital {
  type Error = WrongKind;

  fn try_from(value: Any) -> Result<Self, Self::Error> {
    match value {
      Any::Digital(rule) => Ok(rule),
      _ => Err(WrongKind { want: "Digital", got: value.debug_name() }),
    }
  }
}

/// A comment rule.
///
/// Comments do not generate tokens, unlike most rules. Instead, they are
/// attached to the span of a token, and can be inspected through
/// [`Span::comments()`][crate::Span::comments].
#[derive(Debug)]
pub struct Comment {
  pub(crate) bracket: Bracket,
  pub(crate) can_nest: bool,
}

impl Comment {
  /// Creates a new line comment. Line comments cannot nest, and run from
  /// starting delimiter (which is something like `//` or `#`) to the next
  /// `'\n'` character (not including it).
  pub fn line(delim: impl Into<Yarn>) -> Self {
    Self::non_nesting((delim, "\n").into())
  }

  /// Creates a new nestable block comment with paired delimiters.
  pub fn block(open: impl Into<Yarn>, close: impl Into<Yarn>) -> Self {
    Self::nesting((open, close).into())
  }

  /// Creates a new comment that can nest. For example, Rust block comments
  /// can nest: `/* /* */ */`
  pub fn nesting(bracket: Bracket) -> Self {
    Self { bracket, can_nest: true }
  }

  /// Creates a new comment that can't nest. For example, a line comment is a
  /// non-nesting comment where a newline '\n' is the closing delimiter.
  pub fn non_nesting(bracket: Bracket) -> Self {
    Self { bracket, can_nest: false }
  }
}

impl From<&'static str> for Comment {
  fn from(value: &'static str) -> Self {
    Self::line(value)
  }
}

impl From<char> for Comment {
  fn from(value: char) -> Self {
    Self::line(value)
  }
}

impl From<Yarn> for Comment {
  fn from(value: Yarn) -> Self {
    Self::line(value)
  }
}

impl<Y: Into<Yarn>, Z: Into<Yarn>> From<(Y, Z)> for Comment {
  fn from((y, z): (Y, Z)) -> Self {
    Self::block(y, z)
  }
}

impl Rule for Comment {
  type Token<'lex> = Never;

  fn try_from_ref(value: &Any) -> Result<&Self, WrongKind> {
    match value {
      Any::Comment(rule) => Ok(rule),
      _ => Err(WrongKind { want: "Comment", got: value.debug_name() }),
    }
  }
}

impl From<Comment> for Any {
  fn from(value: Comment) -> Self {
    Any::Comment(value)
  }
}

impl TryFrom<Any> for Comment {
  type Error = WrongKind;

  fn try_from(value: Any) -> Result<Self, Self::Error> {
    match value {
      Any::Comment(rule) => Ok(rule),
      _ => Err(WrongKind { want: "Comment", got: value.debug_name() }),
    }
  }
}

impl Rule for Never {
  type Token<'lex> = token::Eof<'lex>;

  fn try_from_ref(value: &Any) -> Result<&Self, WrongKind> {
    Err(WrongKind { want: "Never", got: value.debug_name() })
  }
}

impl From<Never> for Any {
  fn from(value: Never) -> Self {
    value.from_nothing_anything()
  }
}

impl TryFrom<Any> for Never {
  type Error = WrongKind;

  fn try_from(value: Any) -> Result<Self, Self::Error> {
    Err(WrongKind { want: "Never", got: value.debug_name() })
  }
}