tokit 0.0.0

Blazing fast parser combinators: parse-while-lexing (zero-copy), deterministic LALR-style parsing, no backtracking. Flexible emitters for fail-fast runtime or greedy compiler diagnostics
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
//! Escape sequence representation types for lexical analysis.
//!
//! This module provides types for representing escape sequences encountered during
//! lexing. These are **not error types** - they represent successfully recognized
//! escape sequence structures that may later be validated or transformed.
//!
//! # Design Philosophy
//!
//! Escape sequences in source code (like `\n`, `\t`, `\xFF`) consist of:
//! - A **backslash prefix** (`\`)
//! - The **escape content** (the character(s) after the backslash)
//!
//! These types capture both parts to enable:
//! - Precise error reporting with correct spans
//! - Distinguishing between the escape syntax and its semantic meaning
//! - Supporting various escape formats (single-char, hex, unicode, etc.)
//!
//! # Escape Sequence Types
//!
//! ## Single-Character Escapes: `\n`, `\t`, `\r`
//!
//! Use [`SingleCharEscape`] for escapes with a single character after the backslash:
//! - Simple escapes: `\n`, `\t`, `\r`, `\\`, `\'`, `\"`
//! - Prefix for longer escapes: `\x`, `\u` (before parsing the rest)
//!
//! ## Multi-Character Escapes: `\xFF`, `\u1234`
//!
//! Use [`MultiCharEscape`] for escapes with multiple characters after the backslash:
//! - Hex escapes: `\xAB` (the sequence "xAB")
//! - Unicode escapes: `\u1234`, `\u{10FFFF}` (the sequence after `\u`)
//!
//! ## Generic Escape Representation
//!
//! Use [`EscapedLexeme`] for a unified representation that can hold either type:
//! - Wraps a [`Lexeme`] (Char or SimpleSpan)
//! - Includes the full span of the escape sequence
//!
//! # Examples
//!
//! ## Representing a Simple Escape
//!
//! ```
//! use tokit::utils::{SingleCharEscape, PositionedChar, SimpleSpan};
//!
//! // For the escape sequence `\n` at position 10-12:
//! let escape = SingleCharEscape::from_positioned_char(
//!     SimpleSpan::new(10, 12),                      // Covers both '\' and 'n'
//!     PositionedChar::with_position('n', 11), // The 'n' at position 11
//! );
//!
//! assert_eq!(escape.char(), 'n');
//! assert_eq!(escape.position(), 11);
//! assert_eq!(escape.span(), SimpleSpan::new(10, 12));
//! ```
//!
//! ## Representing a Hex Escape
//!
//! ```
//! use tokit::utils::{MultiCharEscape, SimpleSpan};
//!
//! // For the escape sequence `\xFF` at position 5-9:
//! let escape = MultiCharEscape::new(
//!     SimpleSpan::new(6, 9),  // Just "xFF" (after the backslash)
//!     SimpleSpan::new(5, 9)   // Full escape including '\x'
//! );
//!
//! assert_eq!(escape.content(), SimpleSpan::new(6, 9));
//! assert_eq!(escape.span(), SimpleSpan::new(5, 9));
//! ```

use core::ops::AddAssign;

use crate::utils::{Lexeme, human_display::DisplayHuman};

use super::{PositionedChar, SimpleSpan};

/// A single-character escape sequence representation.
///
/// This type represents escape sequences with exactly one character after the
/// backslash, such as `\n`, `\t`, `\r`, `\\`, `\'`, `\"`.
///
/// # Structure
///
/// The type stores:
/// - **character**: The single character after the backslash (with its position)
/// - **span**: The full span covering both `\` and the character
///
/// For example, in `\n` at position 10-12:
/// - `character` would be `'n'` at position 11
/// - `span` would be 10..12 (covering both `\` and `n`)
///
/// # Type Parameters
///
/// * `Char` - The character type (typically `char` for UTF-8 or `u8` for bytes)
///
/// # Examples
///
/// ```
/// use tokit::utils::{SingleCharEscape, PositionedChar, SimpleSpan};
///
/// // Escape sequence `\n` at positions 10-12
/// let newline = SingleCharEscape::from_positioned_char(
///     SimpleSpan::new(10, 12),
///     PositionedChar::with_position('n', 11),
/// );
///
/// assert_eq!(newline.char(), 'n');
/// assert_eq!(newline.position(), 11);
/// assert_eq!(newline.span(), SimpleSpan::new(10, 12));
///
/// // Escape sequence `\t` at positions 20-22
/// let tab = SingleCharEscape::from_positioned_char(
///     SimpleSpan::new(20, 22),
///     PositionedChar::with_position('t', 21),
/// );
///
/// assert_eq!(tab.char(), 't');
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SingleCharEscape<Char = char, O = usize> {
  character: PositionedChar<Char, O>,
  span: SimpleSpan<O>,
}

impl<Char, O> core::fmt::Display for SingleCharEscape<Char, O>
where
  Char: DisplayHuman,
  O: core::fmt::Display,
{
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    write!(f, "\\{} at {}", self.char_ref().display(), self.span)
  }
}

impl<Char, O> SingleCharEscape<Char, O> {
  /// Creates a new single-character escape sequence.
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{SingleCharEscape, PositionedChar, SimpleSpan};
  ///
  /// let escape = SingleCharEscape::from_char(
  ///     SimpleSpan::new(14, 16),
  ///     15,
  ///    'r'
  /// );
  /// assert_eq!(escape.char(), 'r');
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn from_char(span: SimpleSpan<O>, pos: O, ch: Char) -> Self {
    Self::from_positioned_char(span, PositionedChar::with_position(ch, pos))
  }

  /// Creates a new single-character escape sequence.
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{SingleCharEscape, PositionedChar, SimpleSpan};
  ///
  /// let escape = SingleCharEscape::from_positioned_char(
  ///     SimpleSpan::new(14, 16),
  ///     PositionedChar::with_position('r', 15),
  /// );
  /// assert_eq!(escape.char(), 'r');
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn from_positioned_char(
    span: SimpleSpan<O>,
    character: PositionedChar<Char, O>,
  ) -> Self {
    Self { character, span }
  }

  /// Returns the character after the backslash.
  ///
  /// For example, for `\n`, this returns `'n'`.
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{SingleCharEscape, PositionedChar, SimpleSpan};
  ///
  /// let escape = SingleCharEscape::from_positioned_char(
  ///     SimpleSpan::new(4, 6),
  ///     PositionedChar::with_position('n', 5),
  /// );
  /// assert_eq!(escape.char(), 'n');
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn char(&self) -> Char
  where
    Char: Copy,
  {
    self.character.char()
  }

  /// Returns a reference to the character after the backslash.
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{SingleCharEscape, PositionedChar, SimpleSpan};
  ///
  /// let escape = SingleCharEscape::from_positioned_char(
  ///     SimpleSpan::new(9, 11),
  ///     PositionedChar::with_position('t', 10),
  /// );
  /// assert_eq!(*escape.char_ref(), 't');
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn char_ref(&self) -> &Char {
    self.character.char_ref()
  }

  /// Returns a mutable reference to the character after the backslash.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn char_mut(&mut self) -> &mut Char {
    self.character.char_mut()
  }
  /// Returns the position of the character (not including the backslash).
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{SingleCharEscape, PositionedChar, SimpleSpan};
  ///
  /// // Escape `\n` at positions 10-12: '\' at 10, 'n' at 11
  /// let escape = SingleCharEscape::from_positioned_char(
  ///     SimpleSpan::new(10, 12),
  ///     PositionedChar::with_position('n', 11),
  /// );
  /// assert_eq!(escape.position(), 11); // Position of 'n', not '\'
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn position(&self) -> O
  where
    O: Copy,
  {
    self.character.position()
  }

  /// Returns the position of the character (not including the backslash).
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{SingleCharEscape, PositionedChar, SimpleSpan};
  ///
  /// // Escape `\n` at positions 10-12: '\' at 10, 'n' at 11
  /// let escape = SingleCharEscape::from_positioned_char(
  ///     SimpleSpan::new(10, 12),
  ///     PositionedChar::with_position('n', 11),
  /// );
  /// assert_eq!(escape.position_ref(), &11); // Position of 'n', not '\'
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn position_ref(&self) -> &O {
    self.character.position_ref()
  }

  /// Returns the span of the entire escape sequence.
  ///
  /// This includes both the backslash and the character.
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{SingleCharEscape, PositionedChar, SimpleSpan};
  ///
  /// let escape = SingleCharEscape::from_positioned_char(
  ///     SimpleSpan::new(4, 6),
  ///     PositionedChar::with_position('r', 5),
  /// );
  /// assert_eq!(escape.span(), SimpleSpan::new(4, 6)); // Covers both '\' and 'r'
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn span(&self) -> SimpleSpan<O>
  where
    O: Copy,
  {
    self.span
  }

  /// Returns a reference to the span.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn span_ref(&self) -> SimpleSpan<&O> {
    self.span.as_ref()
  }

  /// Returns a mutable reference to the span.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn span_mut(&mut self) -> SimpleSpan<&mut O> {
    self.span.as_mut()
  }

  /// Bumps both the span and character position by `n`.
  ///
  /// This is useful when adjusting positions after processing or when
  /// combining escape sequences from different parsing contexts.
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{SingleCharEscape, PositionedChar, SimpleSpan};
  ///
  /// let mut escape = SingleCharEscape::from_positioned_char(
  ///     SimpleSpan::new(10, 12),
  ///     PositionedChar::with_position('n', 11),
  /// );
  ///
  /// escape.bump(5);
  ///
  /// assert_eq!(escape.position(), 16);      // Was 11, now 16
  /// assert_eq!(escape.span(), SimpleSpan::new(15, 17)); // Was 10-12, now 15-17
  /// ```
  #[inline]
  pub fn bump(&mut self, offset: &O) -> &mut Self
  where
    O: for<'a> AddAssign<&'a O> + Clone,
  {
    self.span.bump(offset);
    self.character.bump_position(offset);
    self
  }
}

/// A multi-character escape sequence representation.
///
/// This type represents escape sequences with multiple characters after the
/// backslash, such as `\xFF` (hex escape) or `\u1234` (unicode escape).
///
/// # Structure
///
/// The type stores:
/// - **content**: A span covering just the characters after the backslash
/// - **span**: The full span covering `\` and all following characters
///
/// For example, in `\xFF` at position 5-9:
/// - `content` would be 6..9 (just "xFF")
/// - `span` would be 5..9 (covering `\`, `x`, `F`, `F`)
///
/// # Examples
///
/// ```
/// use tokit::utils::{MultiCharEscape, SimpleSpan};
///
/// // Hex escape `\xFF` at positions 5-9
/// let hex = MultiCharEscape::new(
///     SimpleSpan::new(6, 9),  // Just "xFF"
///     SimpleSpan::new(5, 9)   // Full escape including backslash
/// );
///
/// assert_eq!(hex.content(), SimpleSpan::new(6, 9));
/// assert_eq!(hex.span(), SimpleSpan::new(5, 9));
///
/// // Unicode escape `\u1234` at positions 10-16
/// let unicode = MultiCharEscape::new(
///     SimpleSpan::new(11, 16), // Just "u1234"
///     SimpleSpan::new(10, 16)  // Full escape
/// );
///
/// assert_eq!(unicode.content(), SimpleSpan::new(11, 16));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MultiCharEscape<S = SimpleSpan> {
  content: S,
  span: S,
}

impl<S> core::fmt::Display for MultiCharEscape<S>
where
  S: core::fmt::Display,
{
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    write!(f, "escape sequence at {}", self.span)
  }
}

impl<S> MultiCharEscape<S> {
  /// Creates a new multi-character escape sequence.
  ///
  /// ## Parameters
  ///
  /// - `content`: SimpleSpan of the characters after the backslash
  /// - `span`: Full span including the backslash
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{MultiCharEscape, SimpleSpan};
  ///
  /// let escape = MultiCharEscape::new(
  ///     SimpleSpan::new(6, 9),  // "xFF"
  ///     SimpleSpan::new(5, 9)   // "\xFF"
  /// );
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(content: S, span: S) -> Self {
    Self { content, span }
  }

  /// Returns the span of the content (characters after the backslash).
  ///
  /// For `\xFF`, this returns the span covering "xFF" (not including `\`).
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{MultiCharEscape, SimpleSpan};
  ///
  /// let escape = MultiCharEscape::new(
  ///     SimpleSpan::new(6, 9),
  ///     SimpleSpan::new(5, 9)
  /// );
  /// assert_eq!(escape.content(), SimpleSpan::new(6, 9));
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn content(&self) -> S
  where
    S: Copy,
  {
    self.content
  }

  /// Returns a reference to the content span.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn content_ref(&self) -> &S {
    &self.content
  }

  /// Returns a mutable reference to the content span.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn content_mut(&mut self) -> &mut S {
    &mut self.content
  }

  /// Returns the span of the entire escape sequence.
  ///
  /// This includes both the backslash and all following characters.
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{MultiCharEscape, SimpleSpan};
  ///
  /// let escape = MultiCharEscape::new(
  ///     SimpleSpan::new(6, 9),
  ///     SimpleSpan::new(5, 9)
  /// );
  /// assert_eq!(escape.span(), SimpleSpan::new(5, 9)); // Full `\xFF`
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn span(&self) -> S
  where
    S: Copy,
  {
    self.span
  }

  /// Returns a reference to the span.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn span_ref(&self) -> &S {
    &self.span
  }

  /// Returns a mutable reference to the span.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn span_mut(&mut self) -> &mut S {
    &mut self.span
  }

  /// Bumps both the content span and full span by `n`.
  ///
  /// This is useful when adjusting positions after processing or when
  /// combining escape sequences from different parsing contexts.
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{MultiCharEscape, SimpleSpan};
  ///
  /// let mut escape = MultiCharEscape::new(
  ///     SimpleSpan::new(6, 9),
  ///     SimpleSpan::new(5, 9)
  /// );
  ///
  /// escape.bump(10);
  ///
  /// assert_eq!(escape.content(), SimpleSpan::new(16, 19));
  /// assert_eq!(escape.span(), SimpleSpan::new(15, 19));
  /// ```
  #[inline]
  pub fn bump(&mut self, offset: &S::Offset) -> &mut Self
  where
    S: crate::lexer::Span,
  {
    self.span.bump(offset);
    self.content.bump(offset);
    self
  }
}

/// A generic escape sequence representation using a lexeme.
///
/// This type provides a unified representation for escape sequences that can
/// contain either a single character or a multi-character sequence. It wraps
/// a [`Lexeme`] (which can be either [`Lexeme::Char`] or [`Lexeme::Range`]) along
/// with the full span of the escape.
///
/// # Use Cases
///
/// Use this type when you need to handle escape sequences generically without
/// knowing in advance whether they're single-character or multi-character:
///
/// - During initial lexing before categorizing escape types
/// - In error reporting where escape type doesn't matter
/// - When building AST nodes that accept any escape format
///
/// # Type Parameters
///
/// * `Char` - The character type (typically `char` for UTF-8 or `u8` for bytes)
///
/// # Examples
///
/// ## Creating from a Single Character
///
/// ```
/// use tokit::utils::{EscapedLexeme, PositionedChar, SimpleSpan};
///
/// let escape = EscapedLexeme::from_positioned_char(
///     SimpleSpan::new(10, 12),                       // Full span `\n`
///     PositionedChar::with_position('n', 11)   // Just the 'n'
/// );
///
/// assert_eq!(escape.span(), SimpleSpan::new(10, 12));
/// assert!(escape.lexeme_ref().is_char());
/// ```
///
/// ## Creating from a Sequence
///
/// ```
/// use tokit::utils::{EscapedLexeme, SimpleSpan};
///
/// let escape: EscapedLexeme = EscapedLexeme::from_sequence(
///     SimpleSpan::new(5, 9),   // Full span `\xFF`
///     SimpleSpan::new(6, 9)    // Just "xFF"
/// );
///
/// assert_eq!(escape.span(), SimpleSpan::new(5, 9));
/// assert!(escape.lexeme_ref().is_range());
/// ```
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct EscapedLexeme<Char = char, O = usize> {
  span: SimpleSpan<O>,
  lexeme: Lexeme<Char, O>,
}

impl<Char, O> core::fmt::Display for EscapedLexeme<Char, O>
where
  Char: DisplayHuman,
  O: core::fmt::Display,
{
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    match self.lexeme {
      Lexeme::Char(ref ch) => write!(f, "\\{} at {}", ch.char_ref().display(), ch.position_ref()),
      Lexeme::Range(ref range) => write!(f, "escape sequence at {}", range),
    }
  }
}

impl<Char, O> EscapedLexeme<Char, O> {
  /// Creates a new escaped lexeme.
  ///
  /// ## Parameters
  ///
  /// - `span`: The full span of the escape sequence (including backslash)
  /// - `lexeme`: The content after the backslash (as a Lexeme)
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{EscapedLexeme, Lexeme, PositionedChar, SimpleSpan};
  ///
  /// let lexeme = Lexeme::from(PositionedChar::with_position('n', 11));
  /// let escape = EscapedLexeme::new(SimpleSpan::new(10, 12), lexeme);
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(span: SimpleSpan<O>, lexeme: Lexeme<Char, O>) -> Self {
    Self { span, lexeme }
  }

  /// Creates an escaped lexeme from a span and positioned character.
  ///
  /// This is a convenience constructor for single-character escapes.
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{EscapedLexeme, PositionedChar, SimpleSpan};
  ///
  /// let escape = EscapedLexeme::from_positioned_char(
  ///     SimpleSpan::new(10, 12),
  ///     PositionedChar::with_position('t', 11)
  /// );
  ///
  /// assert!(escape.lexeme_ref().is_char());
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn from_positioned_char(span: SimpleSpan<O>, ch: PositionedChar<Char, O>) -> Self {
    Self::new(span, Lexeme::Char(ch))
  }

  /// Creates an escaped lexeme from a span and positioned character.
  ///
  /// This is a convenience constructor for single-character escapes.
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{EscapedLexeme, PositionedChar, SimpleSpan};
  ///
  /// let escape = EscapedLexeme::from_char(
  ///     SimpleSpan::new(10, 12),
  ///     11,
  ///    't'
  /// );
  ///
  /// assert!(escape.lexeme_ref().is_char());
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn from_char(span: SimpleSpan<O>, pos: O, ch: Char) -> Self {
    Self::from_positioned_char(span, PositionedChar::with_position(ch, pos))
  }

  /// Creates an escaped lexeme from a span and content span.
  ///
  /// This is a convenience constructor for multi-character escapes.
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{EscapedLexeme, SimpleSpan};
  ///
  /// let escape: EscapedLexeme = EscapedLexeme::from_sequence(
  ///     SimpleSpan::new(5, 9),   // Full `\xFF`
  ///     SimpleSpan::new(6, 9)    // Just "xFF"
  /// );
  ///
  /// assert!(escape.lexeme_ref().is_range());
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn from_sequence(span: SimpleSpan<O>, content: SimpleSpan<O>) -> Self {
    Self::new(span, Lexeme::Range(content))
  }

  /// Returns the span of the entire escape sequence.
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{EscapedLexeme, PositionedChar, SimpleSpan};
  ///
  /// let escape = EscapedLexeme::from_positioned_char(
  ///     SimpleSpan::new(10, 12),
  ///     PositionedChar::with_position('n', 11)
  /// );
  /// assert_eq!(escape.span(), SimpleSpan::new(10, 12));
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn span(&self) -> SimpleSpan<O>
  where
    O: Copy,
  {
    self.span
  }

  /// Returns a reference to the span.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn span_ref(&self) -> SimpleSpan<&O> {
    self.span.as_ref()
  }

  /// Returns a mutable reference to the span.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn span_mut(&mut self) -> SimpleSpan<&mut O> {
    self.span.as_mut()
  }

  /// Returns the lexeme representing the escape content.
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{EscapedLexeme, PositionedChar, SimpleSpan};
  ///
  /// let escape = EscapedLexeme::from_positioned_char(
  ///     SimpleSpan::new(10, 12),
  ///     PositionedChar::with_position('n', 11)
  /// );
  ///
  /// let lexeme = escape.lexeme();
  /// assert!(lexeme.is_char());
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn lexeme(&self) -> Lexeme<Char, O>
  where
    Char: Copy,
    O: Copy,
  {
    self.lexeme
  }

  /// Returns a reference to the lexeme.
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{EscapedLexeme, PositionedChar, SimpleSpan};
  ///
  /// let escape = EscapedLexeme::from_positioned_char(
  ///     SimpleSpan::new(10, 12),
  ///     PositionedChar::with_position('n', 11)
  /// );
  /// assert!(escape.lexeme_ref().is_char());
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn lexeme_ref(&self) -> &Lexeme<Char, O> {
    &self.lexeme
  }

  /// Returns a mutable reference to the lexeme.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn lexeme_mut(&mut self) -> &mut Lexeme<Char, O> {
    &mut self.lexeme
  }

  /// Bumps the span and lexeme by `n`.
  ///
  /// This is useful when adjusting positions after processing or when
  /// combining escape sequences from different parsing contexts.
  ///
  /// ## Examples
  ///
  /// ```
  /// use tokit::utils::{EscapedLexeme, PositionedChar, SimpleSpan};
  ///
  /// let mut escape = EscapedLexeme::from_positioned_char(
  ///     SimpleSpan::new(10, 12),
  ///     PositionedChar::with_position('n', 11)
  /// );
  ///
  /// escape.bump(5);
  /// assert_eq!(escape.span(), SimpleSpan::new(15, 17));
  /// ```
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn bump(&mut self, offset: &O) -> &mut Self
  where
    O: for<'a> AddAssign<&'a O> + Clone,
  {
    self.span.bump(offset);
    self.lexeme.bump(offset);
    self
  }
}