mf2_parser 0.1.1

Parser and AST definitions for MessageFormat 2
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
use std::fmt;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::ops::Add;
use std::ops::Range;
use std::str::Chars;

type Peek = Option<(Location, char)>;

enum Peeked {
  None,
  Single(Peek),
  Double((Location, char), Peek),
}

/// The source text, represented as a series of `char`s.
///
/// The source text maintains the offset of characters in the original string
/// and provides methods to iterate over the characters. It provides a method
/// to advance the iterator, and a method to peek the next character.
pub struct SourceTextIterator<'text> {
  original: &'text str,
  front_loc: Location,
  str_index: u32,
  iter: Chars<'text>,
  peeked: Peeked,
  utf8_line_starts: Vec<u32>,
  prev_char_was_cr: bool,
}

impl<'text> SourceTextIterator<'text> {
  pub fn new(s: &'text str) -> Self {
    assert!(
      s.len() <= u32::MAX as usize,
      "source text is longer than u32::MAX"
    );
    SourceTextIterator {
      original: s,
      front_loc: Location(0),
      str_index: 0,
      iter: s.chars(),
      peeked: Peeked::None,
      utf8_line_starts: vec![0],
      prev_char_was_cr: false,
    }
  }

  /// Resets the iterator to the given location.
  ///
  /// The location reset to must be before the current location to ensure line
  /// start tracking is correct.
  ///
  /// ## Panics
  ///
  /// Panics if the location falls outside of the source text, or if the
  /// location is not at a character boundary.
  pub fn reset_to(&mut self, loc: Location) {
    assert!(loc <= self.front_loc);
    assert!(loc.0 <= self.end_location().0);
    self.front_loc = loc;
    self.str_index = loc.0;
    self.peeked = Peeked::None;
    self.iter = self.original[self.str_index as usize..].chars();
    self.prev_char_was_cr =
      self.original[..self.str_index as usize].ends_with('\r');
  }

  fn iter_next(&mut self) -> Option<char> {
    self.iter.next().map(|ch| {
      match ch {
        '\n' => {
          if *self.utf8_line_starts.last().unwrap() < self.str_index + 1 {
            self.utf8_line_starts.push(self.str_index + 1);
          }
          self.prev_char_was_cr = false;
        }
        _ => {
          if self.prev_char_was_cr
            && *self.utf8_line_starts.last().unwrap() < self.str_index
          {
            self.utf8_line_starts.push(self.str_index);
          }
          self.prev_char_was_cr = ch == '\r';
        }
      }
      self.str_index += ch.len_utf8() as u32;
      ch
    })
  }

  pub fn next(&mut self) -> Option<(Location, char)> {
    match self.peeked {
      Peeked::None => self.iter_next().map(|ch| {
        let loc = self.front_loc;
        self.front_loc = Location(self.str_index);
        (loc, ch)
      }),
      Peeked::Single(None) => None,
      Peeked::Single(Some(peek)) | Peeked::Double(peek, None) => {
        self.front_loc = Location(self.str_index);
        self.peeked = Peeked::None;
        Some(peek)
      }
      Peeked::Double(peek1, peek2 @ Some((loc, _))) => {
        self.front_loc = loc;
        self.peeked = Peeked::Single(peek2);
        Some(peek1)
      }
    }
  }

  pub fn peek(&mut self) -> Peek {
    match &self.peeked {
      Peeked::Single(peek) => *peek,
      Peeked::Double(peek, _) => Some(*peek),
      Peeked::None => {
        let peeked = self.iter_next().map(|ch| (self.front_loc, ch));
        self.peeked = Peeked::Single(peeked);
        peeked
      }
    }
  }

  pub fn peek2(&mut self) -> Peek {
    if let Peeked::Double(_, peek) = self.peeked {
      return peek;
    }
    match self.peek() {
      None => None,
      Some(peek1) => {
        let loc = Location(self.str_index);
        let peek2 = self.iter_next().map(|ch2| (loc, ch2));
        self.peeked = Peeked::Double(peek1, peek2);
        peek2
      }
    }
  }

  pub fn current_location(&self) -> Location {
    self.front_loc
  }

  pub fn start_location(&self) -> Location {
    Location(0)
  }

  pub fn end_location(&self) -> Location {
    Location(self.original.len() as u32)
  }

  pub fn slice(&self, range: Range<Location>) -> &'text str {
    &self.original[range.start.0 as usize..range.end.0 as usize]
  }

  pub fn into_info(mut self) -> SourceTextInfo<'text> {
    assert_eq!(self.str_index, self.original.len() as u32);
    if self.prev_char_was_cr
      && *self.utf8_line_starts.last().unwrap() < self.str_index
    {
      self.utf8_line_starts.push(self.str_index);
    }
    SourceTextInfo {
      text: self.original,
      utf8_line_starts: self.utf8_line_starts,
    }
  }
}

/// A view onto the source text, with additional information about the source
/// text that was derived during parsing.
///
/// This struct provides methods to convert between opaque [Location] values,
/// UTF-8 line and column indices, and UTF-16 line and column indices. It also
/// provides methods to calculate the length of a span in UTF-8 bytes or UTF-16
/// code units.
pub struct SourceTextInfo<'text> {
  text: &'text str,
  utf8_line_starts: Vec<u32>,
}

impl Spanned for SourceTextInfo<'_> {
  fn span(&self) -> Span {
    Span {
      start: Location(0),
      end: Location(self.text.len() as u32),
    }
  }
}

impl SourceTextInfo<'_> {
  /// Returns a UTF-8 line and column index pair given a [Location].
  ///
  /// It is undefined behavior to pass a location that is out of bounds for the
  /// source text.
  pub fn utf8_line_col(&self, loc: Location) -> LineColUtf8 {
    let result = self.utf8_line_starts.binary_search_by(|&x| x.cmp(&loc.0));
    match result {
      Ok(line) => LineColUtf8 {
        line: line as u32,
        col: 0,
      },
      Err(line) => {
        let line = line - 1;
        let col = loc.0 - self.utf8_line_starts[line];
        LineColUtf8 {
          line: line as u32,
          col,
        }
      }
    }
  }

  /// Returns a UTF-16 line and column index pair given a [Location].
  ///
  /// It is undefined behavior to pass a location that is out of bounds for the
  /// source text.
  pub fn utf16_line_col(&self, loc: Location) -> LineColUtf16 {
    let result = self.utf8_line_starts.binary_search_by(|&x| x.cmp(&loc.0));
    match result {
      Ok(line) => LineColUtf16 {
        line: line as u32,
        col: 0,
      },
      Err(line) => {
        let line = line - 1;
        let line_text =
          &self.text[self.utf8_line_starts[line] as usize..loc.0 as usize];
        let col = line_text
          .chars()
          .fold(0, |acc, c| acc + c.len_utf16() as u32);
        LineColUtf16 {
          line: line as u32,
          col,
        }
      }
    }
  }

  /// Returns the length of the given span in UTF-8 bytes.
  pub fn utf8_len(&self, span: Span) -> u32 {
    span.end.0 - span.start.0
  }

  /// Returns the length of the given span in UTF-16 code units.
  pub fn utf16_len(&self, span: Span) -> u32 {
    let text = &self.text[span.start.0 as usize..span.end.0 as usize];
    text.chars().fold(0, |acc, c| acc + c.len_utf16() as u32)
  }

  /// Returns the location of the given UTF-8 line and column index pair.
  ///
  /// If the line index is out of bounds, returns a location pointing to the end
  /// of the source text.
  ///
  /// If the column index is greater than the line length, it is clamped to the
  /// line length. If the column index points to within a multi-byte character,
  /// the location will point to the the start of that character.
  pub fn utf8_loc(&self, line_col: LineColUtf8) -> Location {
    let line = line_col.line as usize;
    let line_start = match self.utf8_line_starts.get(line) {
      Some(&x) => x as usize,
      None => return Location(self.text.len() as u32),
    };
    let line_end = self
      .utf8_line_starts
      .get(line + 1)
      .map(|&x| x as usize)
      .unwrap_or_else(|| self.text.len());
    let line_text = &self.text[line_start..line_end];

    let mut col = line_col.col as usize;
    let mut location = Location(line_start as u32);
    for ch in line_text.chars() {
      col = match col.checked_sub(ch.len_utf8()) {
        Some(x) => x,
        None => break,
      };
      location = location + ch;
      if col == 0 {
        break;
      }
    }
    location
  }

  /// Returns the location of the given UTF-16 line and column index pair.
  ///
  /// If the line index is out of bounds, returns a location pointing to the end
  /// of the source text.
  ///
  /// If the column index is greater than the line length, it is clamped to the
  /// line length. If the column index points to within a multi-byte character,
  /// the location will point to the the start of that character.
  pub fn utf16_loc(&self, line_col: LineColUtf16) -> Location {
    let line = line_col.line as usize;
    let line_start = match self.utf8_line_starts.get(line) {
      Some(&x) => x as usize,
      None => return Location(self.text.len() as u32),
    };
    let line_end = self
      .utf8_line_starts
      .get(line + 1)
      .map(|&x| x as usize)
      .unwrap_or_else(|| self.text.len());
    let line_text = &self.text[line_start..line_end];

    let mut col = line_col.col as usize;
    let mut location = Location(line_start as u32);
    for ch in line_text.chars() {
      col = match col.checked_sub(ch.len_utf16()) {
        Some(x) => x,
        None => break,
      };
      location = location + ch;
      if col == 0 {
        break;
      }
    }
    location
  }
}

/// A location is an opaque value that is used to represent a position in the
/// source text. It can be mapped to UTF-8 byte indices, UTF-8 line and column,
/// or UTF-16 line and column indices in the source text using the
/// [SourceTextInfo] struct.
#[derive(Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
pub struct Location(u32);

impl Location {
  #[doc(hidden)]
  pub fn new_for_test(byte: u32) -> Location {
    Location(byte)
  }

  #[doc(hidden)]
  pub fn inner_byte_index_for_test(&self) -> u32 {
    self.0
  }

  pub(crate) fn inner(&self) -> u32 {
    self.0
  }
}

impl Debug for Location {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(f, "@{}", self.0)
  }
}

impl Add<&'_ str> for Location {
  type Output = Location;

  fn add(self, rhs: &'_ str) -> Self::Output {
    Location(self.0 + rhs.len() as u32)
  }
}

impl Add<char> for Location {
  type Output = Location;

  fn add(self, rhs: char) -> Self::Output {
    Location(self.0 + rhs.len_utf8() as u32)
  }
}

impl Add<LengthShort> for Location {
  type Output = Location;

  fn add(self, rhs: LengthShort) -> Self::Output {
    Location(self.0 + rhs.0 as u32)
  }
}

/// A span is a pair of [Location]s that represent a range in the source text.
///
/// The start location is inclusive, and the end location is exclusive. A span
/// with the same start and end location is considered empty.
#[derive(Clone, Copy)]
pub struct Span {
  pub start: Location,
  pub end: Location,
}

impl Span {
  /// Creates a new span from a range of [Location]s. The range must be valid, i.e.
  /// the start location must be less than or equal to the end location.
  ///
  /// ### Panics
  ///
  /// In debug builds, panics if the range is invalid.
  pub fn new(range: Range<Location>) -> Self {
    debug_assert!(range.start <= range.end);
    Span {
      start: range.start,
      end: range.end,
    }
  }

  /// Whether the span contains the given [Location].
  pub fn contains_loc(&self, loc: Location) -> bool {
    self.start.0 <= loc.0 && self.end.0 > loc.0
  }

  /// Whether the span fully contains the given span. This includes the case
  /// where the spans are equal.
  pub fn contains(&self, other: &Span) -> bool {
    self.start.0 <= other.start.0 && self.end.0 >= other.end.0
  }

  /// Whether the span is empty.
  pub fn is_empty(&self) -> bool {
    self.start == self.end
  }
}

impl Debug for Span {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(f, "@{}..{}", self.start.0, self.end.0)
  }
}

pub trait Spanned {
  fn span(&self) -> Span;
}

/// A short length (maximum u16)
#[derive(Clone, Copy)]
pub struct LengthShort(u16);

impl Debug for LengthShort {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(f, "{}", self.0)
  }
}

impl LengthShort {
  pub fn new(range: Range<Location>) -> LengthShort {
    LengthShort((range.end.0 - range.start.0) as u16)
  }

  pub fn new_from_str(str: &str) -> LengthShort {
    LengthShort(str.len() as u16)
  }

  pub fn inner(&self) -> u16 {
    self.0
  }
}

/// A line and column index pair, both 0-based, for the UTF-8 encoding of the
/// source text.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct LineColUtf8 {
  pub line: u32,
  pub col: u32,
}

impl Debug for LineColUtf8 {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(f, "{}:{}", self.line, self.col)
  }
}

/// A line and column index pair, both 0-based, for the UTF-16 encoding of the
/// source text.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct LineColUtf16 {
  pub line: u32,
  pub col: u32,
}

impl Debug for LineColUtf16 {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(f, "{}:{}", self.line, self.col)
  }
}

#[cfg(test)]
mod tests {
  const SOURCE: &str = "a\nbc\r\nf\rπŸŠπŸ˜…πŸŽƒ\r\nasd🍊a";

  #[test]
  fn source_text_line_col_from_loc() {
    let mut source_text = super::SourceTextIterator::new(SOURCE);
    while source_text.next().is_some() {}
    let info = source_text.into_info();

    macro_rules! assert_utf8_line_col {
      ($byte:literal == ($line:literal, $col:literal)) => {
        assert_eq!(
          info.utf8_line_col(super::Location($byte)),
          super::LineColUtf8 {
            line: $line,
            col: $col
          },
          "byte {}",
          $byte
        );
      };
    }

    assert_utf8_line_col!(0 == (0, 0));
    assert_utf8_line_col!(1 == (0, 1));
    assert_utf8_line_col!(2 == (1, 0));
    assert_utf8_line_col!(3 == (1, 1));
    assert_utf8_line_col!(4 == (1, 2));
    assert_utf8_line_col!(5 == (1, 3));
    assert_utf8_line_col!(6 == (2, 0));
    assert_utf8_line_col!(7 == (2, 1));
    assert_utf8_line_col!(8 == (3, 0));
    // 9, 10, 11 are in the middle of the multi-byte character 🍊
    assert_utf8_line_col!(12 == (3, 4));
    // 13, 14, 15 are in the middle of the multi-byte character πŸ˜…
    assert_utf8_line_col!(16 == (3, 8));
    // 17, 18, 19 are in the middle of the multi-byte character πŸŽƒ
    assert_utf8_line_col!(20 == (3, 12));
    assert_utf8_line_col!(21 == (3, 13));
    assert_utf8_line_col!(22 == (4, 0));
    assert_utf8_line_col!(23 == (4, 1));
    assert_utf8_line_col!(24 == (4, 2));
    assert_utf8_line_col!(25 == (4, 3));
    // 26, 27, 28 are in the middle of the multi-byte character 🍊
    assert_utf8_line_col!(29 == (4, 7));
    assert_utf8_line_col!(30 == (4, 8));

    macro_rules! assert_utf16_line_col {
      ($byte:literal == ($line:literal, $col:literal)) => {
        assert_eq!(
          info.utf16_line_col(super::Location($byte)),
          super::LineColUtf16 {
            line: $line,
            col: $col
          },
          "byte {}",
          $byte
        );
      };
    }

    assert_utf16_line_col!(0 == (0, 0));
    assert_utf16_line_col!(1 == (0, 1));
    assert_utf16_line_col!(2 == (1, 0));
    assert_utf16_line_col!(3 == (1, 1));
    assert_utf16_line_col!(4 == (1, 2));
    assert_utf16_line_col!(5 == (1, 3));
    assert_utf16_line_col!(6 == (2, 0));
    assert_utf16_line_col!(7 == (2, 1));
    assert_utf16_line_col!(8 == (3, 0));
    // 9, 10, 11 are in the middle of the multi-byte character 🍊
    assert_utf16_line_col!(12 == (3, 2));
    // 13, 14, 15 are in the middle of the multi-byte character πŸ˜…
    assert_utf16_line_col!(16 == (3, 4));
    // 17, 18, 19 are in the middle of the multi-byte character πŸŽƒ
    assert_utf16_line_col!(20 == (3, 6));
    assert_utf16_line_col!(21 == (3, 7));
    assert_utf16_line_col!(22 == (4, 0));
    assert_utf16_line_col!(23 == (4, 1));
    assert_utf16_line_col!(24 == (4, 2));
    assert_utf16_line_col!(25 == (4, 3));
    // 26, 27, 28 are in the middle of the multi-byte character 🍊
    assert_utf16_line_col!(29 == (4, 5));
    assert_utf16_line_col!(30 == (4, 6));
  }

  #[test]
  fn source_text_loc_from_line_col() {
    let mut source_text = super::SourceTextIterator::new(SOURCE);
    while source_text.next().is_some() {}
    let info = source_text.into_info();

    macro_rules! assert_utf8_loc {
      (($line:literal, $col:literal) == $byte:literal) => {
        assert_eq!(
          info.utf8_loc(super::LineColUtf8 {
            line: $line,
            col: $col
          }),
          super::Location($byte),
          "loc {}:{}",
          $line,
          $col
        );
      };
    }

    assert_utf8_loc!((0, 0) == 0);
    assert_utf8_loc!((0, 1) == 1);
    assert_utf8_loc!((1, 0) == 2);
    assert_utf8_loc!((1, 1) == 3);
    assert_utf8_loc!((1, 2) == 4);
    assert_utf8_loc!((1, 3) == 5);
    assert_utf8_loc!((2, 0) == 6);
    assert_utf8_loc!((2, 1) == 7);
    assert_utf8_loc!((3, 0) == 8);
    assert_utf8_loc!((3, 1) == 8);
    assert_utf8_loc!((3, 2) == 8);
    assert_utf8_loc!((3, 3) == 8);
    assert_utf8_loc!((3, 4) == 12);
    assert_utf8_loc!((3, 5) == 12);
    assert_utf8_loc!((3, 6) == 12);
    assert_utf8_loc!((3, 7) == 12);
    assert_utf8_loc!((3, 8) == 16);
    assert_utf8_loc!((3, 9) == 16);
    assert_utf8_loc!((3, 10) == 16);
    assert_utf8_loc!((3, 11) == 16);
    assert_utf8_loc!((3, 12) == 20);
    assert_utf8_loc!((3, 13) == 21);
    assert_utf8_loc!((4, 0) == 22);
    assert_utf8_loc!((4, 1) == 23);
    assert_utf8_loc!((4, 2) == 24);
    assert_utf8_loc!((4, 3) == 25);
    assert_utf8_loc!((4, 4) == 25);
    assert_utf8_loc!((4, 5) == 25);
    assert_utf8_loc!((4, 6) == 25);
    assert_utf8_loc!((4, 7) == 29);
    assert_utf8_loc!((4, 8) == 30);

    // Out of bounds line index
    assert_utf8_loc!((5, 0) == 30);

    // Out of bounds column index
    assert_utf8_loc!((0, 10) == 2);

    macro_rules! assert_utf16_loc {
      (($line:literal, $col:literal) == $byte:literal) => {
        assert_eq!(
          info.utf16_loc(super::LineColUtf16 {
            line: $line,
            col: $col
          }),
          super::Location($byte),
          "loc {}:{}",
          $line,
          $col
        );
      };
    }

    assert_utf16_loc!((0, 0) == 0);
    assert_utf16_loc!((0, 1) == 1);
    assert_utf16_loc!((1, 0) == 2);
    assert_utf16_loc!((1, 1) == 3);
    assert_utf16_loc!((1, 2) == 4);
    assert_utf16_loc!((1, 3) == 5);
    assert_utf16_loc!((2, 0) == 6);
    assert_utf16_loc!((2, 1) == 7);
    assert_utf16_loc!((3, 0) == 8);
    assert_utf16_loc!((3, 1) == 8);
    assert_utf16_loc!((3, 2) == 12);
    assert_utf16_loc!((3, 3) == 12);
    assert_utf16_loc!((3, 4) == 16);
    assert_utf16_loc!((3, 5) == 16);
    assert_utf16_loc!((3, 6) == 20);
    assert_utf16_loc!((3, 7) == 21);
    assert_utf16_loc!((4, 0) == 22);
    assert_utf16_loc!((4, 1) == 23);
    assert_utf16_loc!((4, 2) == 24);
    assert_utf16_loc!((4, 3) == 25);
    assert_utf16_loc!((4, 4) == 25);
    assert_utf16_loc!((4, 5) == 29);
    assert_utf16_loc!((4, 6) == 30);

    // Out of bounds line index
    assert_utf16_loc!((5, 0) == 30);

    // Out of bounds column index
    assert_utf16_loc!((0, 10) == 2);
  }

  #[test]
  fn source_text_line_col_reset() {
    let source = "a\rb";
    let mut source_text = super::SourceTextIterator::new(source);
    assert_eq!(source_text.next(), Some((super::Location(0), 'a')));
    assert_eq!(source_text.next(), Some((super::Location(1), '\r')));
    source_text.reset_to(super::Location(2)); // doesn't change anything, but \r tracking must be set correctly now
    assert_eq!(source_text.next(), Some((super::Location(2), 'b')));
    assert_eq!(source_text.next(), None);
    let info = source_text.into_info();
    assert_eq!(info.utf8_line_starts, vec![0, 2]);
  }

  #[test]
  fn source_text_span_len() {
    let source = "a\nbc\r\nf\rπŸŠπŸ˜…πŸŽƒ\r\nasd🍊a";
    let mut source_text = super::SourceTextIterator::new(source);
    while source_text.next().is_some() {}

    let info = source_text.into_info();
    assert_eq!(
      info.utf8_len(super::Span::new(super::Location(0)..super::Location(0))),
      0
    );
    assert_eq!(
      info.utf8_len(super::Span::new(super::Location(0)..super::Location(1))),
      1
    );
    assert_eq!(
      info.utf8_len(super::Span::new(super::Location(0)..super::Location(2))),
      2
    );
    assert_eq!(
      info.utf8_len(super::Span::new(super::Location(8)..super::Location(12))),
      4
    );

    assert_eq!(
      info.utf16_len(super::Span::new(super::Location(0)..super::Location(0))),
      0
    );
    assert_eq!(
      info.utf16_len(super::Span::new(super::Location(0)..super::Location(1))),
      1
    );
    assert_eq!(
      info.utf16_len(super::Span::new(super::Location(0)..super::Location(2))),
      2
    );
    assert_eq!(
      info.utf16_len(super::Span::new(super::Location(8)..super::Location(12))),
      2
    );
  }
}