codehelion-frontend-rust 0.1.0

Rust Fast-mode lexer and unit-boundary frontend for the codehelion source-audit tool.
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
//! Error-tolerant Rust lexer.
//!
//! Whitespace and comments are dropped. Every other lexeme becomes a token
//! carrying its raw text and a reporting-only source span. Malformed spans
//! (unterminated strings, characters and block comments) are recorded as
//! diagnostics and lexing resumes, so a single broken construct never discards
//! the rest of the file. Macros are not expanded: `name!` is an identifier
//! followed by punctuation, and the invocation's delimiters are ordinary
//! tokens.

use codehelion_core::frontend::{
    Diagnostic, DiagnosticKind, LexemeInterner, LiteralKind, SourceSpan, Token, TokenKind,
};
use std::str::Chars;

/// Rust keywords. `true`/`false` are lexed as keywords rather than boolean
/// literals, keeping token granularity uniform, which the minimum-clone-length
/// threshold relies on.
const KEYWORDS: &[&str] = &[
    "as", "async", "await", "break", "const", "continue", "crate", "dyn", "else", "enum", "extern",
    "false", "fn", "for", "if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub",
    "ref", "return", "self", "Self", "static", "struct", "super", "trait", "true", "type",
    "unsafe", "use", "where", "while",
];

/// Multi-character operators, matched greedily (longest first).
const MULTI_PUNCT: &[&str] = &[
    "<<=", ">>=", "..=", "...", "::", "->", "=>", "==", "!=", "<=", ">=", "+=", "-=", "*=", "/=",
    "%=", "^=", "&=", "|=", "&&", "||", "<<", ">>", "..",
];

fn is_ident_start(c: char) -> bool {
    c.is_alphabetic() || c == '_'
}

fn is_ident_continue(c: char) -> bool {
    c.is_alphanumeric() || c == '_'
}

struct Lexer<'s> {
    source: &'s str,
    chars: Chars<'s>,
    byte: usize,
    line: u32,
    column: u32,
    interner: LexemeInterner,
    tokens: Vec<Token>,
    diagnostics: Vec<Diagnostic>,
}

/// A position captured at the start of a token.
#[derive(Clone, Copy)]
struct Mark {
    byte: usize,
    line: u32,
    column: u32,
}

impl<'s> Lexer<'s> {
    fn new(source: &'s str) -> Self {
        Self {
            source,
            chars: source.chars(),
            byte: 0,
            line: 1,
            column: 1,
            interner: LexemeInterner::new(),
            tokens: Vec::new(),
            diagnostics: Vec::new(),
        }
    }

    /// The source text between a mark and the current position.
    fn text_from(&self, start: Mark) -> &'s str {
        &self.source[start.byte..self.byte]
    }

    fn peek(&self, ahead: usize) -> Option<char> {
        self.chars.clone().nth(ahead)
    }

    const fn mark(&self) -> Mark {
        Mark {
            byte: self.byte,
            line: self.line,
            column: self.column,
        }
    }

    /// Consume the current character, tracking line and column.
    fn bump(&mut self) {
        if let Some(c) = self.chars.next() {
            if c == '\n' {
                self.line += 1;
                self.column = 1;
            } else {
                self.column += 1;
            }
            self.byte += c.len_utf8();
        }
    }

    const fn span_from(&self, start: Mark) -> SourceSpan {
        SourceSpan {
            start_byte: start.byte,
            end_byte: self.byte,
            start_line: start.line,
            start_column: start.column,
        }
    }

    fn push(&mut self, kind: TokenKind, start: Mark) {
        let text = self.interner.intern(self.text_from(start));
        self.tokens.push(Token {
            kind,
            text,
            span: self.span_from(start),
        });
    }

    fn diagnose(&mut self, kind: DiagnosticKind, start: Mark) {
        let span = self.span_from(start);
        self.diagnostics.push(Diagnostic { kind, span });
    }

    fn run(mut self) -> (Vec<Token>, Vec<Diagnostic>) {
        while let Some(c) = self.peek(0) {
            // A UTF-8 BOM is an encoding marker, not source text. Consume it
            // only at byte zero. Keep its byte width in spans without letting
            // it shift the source column visible to users.
            if self.byte == 0 && c == '\u{feff}' {
                let _ = self.chars.next();
                self.byte += c.len_utf8();
                continue;
            }
            if c.is_whitespace() {
                self.bump();
                continue;
            }
            if c == '/' && self.peek(1) == Some('/') {
                self.consume_line_comment();
                continue;
            }
            if c == '/' && self.peek(1) == Some('*') {
                self.consume_block_comment();
                continue;
            }
            if self.try_prefixed_literal() {
                continue;
            }
            if c == '\'' {
                self.consume_quote();
                continue;
            }
            if c == '"' {
                self.consume_string();
                continue;
            }
            if c.is_ascii_digit() {
                self.consume_number();
                continue;
            }
            if is_ident_start(c) {
                self.consume_ident();
                continue;
            }
            self.consume_punct();
        }
        // Streams are long-lived (the whole scan holds every file's tokens),
        // so growth slack is returned to the allocator.
        self.tokens.shrink_to_fit();
        (self.tokens, self.diagnostics)
    }

    fn consume_line_comment(&mut self) {
        while let Some(c) = self.peek(0) {
            if c == '\n' {
                break;
            }
            self.bump();
        }
    }

    fn consume_block_comment(&mut self) {
        let start = self.mark();
        // Skip the opening `/*`.
        self.bump();
        self.bump();
        let mut depth = 1u32;
        while depth > 0 {
            match (self.peek(0), self.peek(1)) {
                (Some('/'), Some('*')) => {
                    self.bump();
                    self.bump();
                    depth += 1;
                }
                (Some('*'), Some('/')) => {
                    self.bump();
                    self.bump();
                    depth -= 1;
                }
                (Some(_), _) => self.bump(),
                (None, _) => {
                    self.diagnose(DiagnosticKind::UnterminatedBlockComment, start);
                    return;
                }
            }
        }
    }

    /// Handle raw strings, byte strings and byte/raw-byte literals, plus raw
    /// identifiers. Returns `true` if a token was produced.
    fn try_prefixed_literal(&mut self) -> bool {
        let c = self.peek(0);
        let n1 = self.peek(1);
        match (c, n1) {
            (Some('r'), Some('"')) => {
                let start = self.mark();
                self.bump();
                self.consume_raw_string_body(start);
                true
            }
            (Some('r'), Some('#')) if self.raw_string_opens_at(1) => {
                let start = self.mark();
                self.bump();
                self.consume_raw_string_body(start);
                true
            }
            (Some('b' | 'c'), Some('"')) => {
                let start = self.mark();
                self.bump();
                self.consume_string_from(start);
                true
            }
            (Some('b' | 'c'), Some('r')) if self.raw_string_opens_at(2) => {
                let start = self.mark();
                self.bump();
                self.bump();
                self.consume_raw_string_body(start);
                true
            }
            (Some('b'), Some('\'')) => {
                let start = self.mark();
                self.bump();
                self.consume_char_from(start);
                true
            }
            _ => false,
        }
    }

    /// Whether `offset` begins the hash delimiter of a raw string literal.
    ///
    /// The delimiter contains zero or more `#` characters followed by `"`.
    /// Checking its full shape here keeps multi-hash raw strings separate from
    /// raw identifiers such as `r#type`.
    fn raw_string_opens_at(&self, offset: usize) -> bool {
        let mut index = offset;
        while self.peek(index) == Some('#') {
            index += 1;
        }
        self.peek(index) == Some('"')
    }

    /// A `'`: either a character literal or a lifetime/label.
    fn consume_quote(&mut self) {
        // A lifetime is `'` followed by an identifier that is not immediately
        // closed by another `'` (which would make it a character literal).
        if self.peek(1).is_some_and(is_ident_start) && self.peek(2) != Some('\'') {
            let start = self.mark();
            self.bump();
            while self.peek(0).is_some_and(is_ident_continue) {
                self.bump();
            }
            self.push(TokenKind::Lifetime, start);
            return;
        }
        let start = self.mark();
        self.consume_char_from(start);
    }

    fn consume_char_from(&mut self, start: Mark) {
        // At entry, the current char is the opening `'` (possibly preceded by a
        // consumed `b`). Consume it, one char or escape, then the closing `'`.
        self.bump();
        if self.peek(0) == Some('\\') {
            self.bump();
            match self.peek(0) {
                Some('x') => {
                    self.bump();
                    for _ in 0..2 {
                        self.bump();
                    }
                }
                Some('u') if self.peek(1) == Some('{') => {
                    self.bump();
                    self.bump();
                    while let Some(ch) = self.peek(0) {
                        self.bump();
                        if ch == '}' {
                            break;
                        }
                    }
                }
                Some(_) => self.bump(),
                None => {}
            }
        } else if self.peek(0).is_some_and(|c| c != '\'') {
            self.bump();
        }
        if self.peek(0) == Some('\'') {
            self.bump();
            self.push(TokenKind::Literal(LiteralKind::Char), start);
        } else {
            // No closing quote: still emit what we have, with a diagnostic.
            self.push(TokenKind::Literal(LiteralKind::Char), start);
            self.diagnose(DiagnosticKind::UnterminatedChar, start);
        }
    }

    fn consume_string(&mut self) {
        let start = self.mark();
        self.consume_string_from(start);
    }

    fn consume_string_from(&mut self, start: Mark) {
        // Opening `"`.
        self.bump();
        loop {
            match self.peek(0) {
                None => {
                    self.push(TokenKind::Literal(LiteralKind::String), start);
                    self.diagnose(DiagnosticKind::UnterminatedString, start);
                    return;
                }
                Some('\\') => {
                    self.bump();
                    self.bump();
                }
                Some('"') => {
                    self.bump();
                    self.push(TokenKind::Literal(LiteralKind::String), start);
                    return;
                }
                Some(_) => self.bump(),
            }
        }
    }

    /// Consume a raw string body starting at the leading `#`s or `"`. `start`
    /// marks the beginning of the whole literal (including any `r`/`br`).
    fn consume_raw_string_body(&mut self, start: Mark) {
        let mut hashes = 0usize;
        while self.peek(0) == Some('#') {
            hashes += 1;
            self.bump();
        }
        // Opening quote.
        if self.peek(0) == Some('"') {
            self.bump();
        }
        loop {
            match self.peek(0) {
                None => {
                    self.push(TokenKind::Literal(LiteralKind::String), start);
                    self.diagnose(DiagnosticKind::UnterminatedString, start);
                    return;
                }
                Some('"') => {
                    // A closing quote followed by `hashes` `#`s ends the string.
                    if (1..=hashes).all(|k| self.peek(k) == Some('#')) {
                        self.bump();
                        for _ in 0..hashes {
                            self.bump();
                        }
                        self.push(TokenKind::Literal(LiteralKind::String), start);
                        return;
                    }
                    self.bump();
                }
                Some(_) => self.bump(),
            }
        }
    }

    fn consume_number(&mut self) {
        let start = self.mark();
        let is_hex_oct_bin = self.peek(0) == Some('0')
            && matches!(self.peek(1), Some('x' | 'X' | 'o' | 'O' | 'b' | 'B'));
        if is_hex_oct_bin {
            self.bump();
            self.bump();
            while self.peek(0).is_some_and(is_ident_continue) {
                self.bump();
            }
            self.push(TokenKind::Literal(LiteralKind::Integer), start);
            return;
        }

        while self
            .peek(0)
            .is_some_and(|ch| ch.is_ascii_digit() || ch == '_')
        {
            self.bump();
        }
        let mut is_float = false;
        // Do not swallow `..` ranges or a method call on a literal.
        if self.peek(0) == Some('.')
            && !matches!(self.peek(1), Some('.' | '_'))
            && !self.peek(1).is_some_and(char::is_alphabetic)
        {
            is_float = true;
            self.bump();
            while self
                .peek(0)
                .is_some_and(|ch| ch.is_ascii_digit() || ch == '_')
            {
                self.bump();
            }
        }
        if matches!(self.peek(0), Some('e' | 'E'))
            && (self.peek(1).is_some_and(|ch| ch.is_ascii_digit())
                || (matches!(self.peek(1), Some('+' | '-'))
                    && self.peek(2).is_some_and(|ch| ch.is_ascii_digit())))
        {
            is_float = true;
            self.bump();
            if matches!(self.peek(0), Some('+' | '-')) {
                self.bump();
            }
            while self
                .peek(0)
                .is_some_and(|ch| ch.is_ascii_digit() || ch == '_')
            {
                self.bump();
            }
        }
        while self.peek(0).is_some_and(is_ident_continue) {
            self.bump();
        }
        let kind = if is_float {
            LiteralKind::Float
        } else {
            LiteralKind::Integer
        };
        self.push(TokenKind::Literal(kind), start);
    }

    fn consume_ident(&mut self) {
        let start = self.mark();
        // Raw identifier `r#name`.
        if self.peek(0) == Some('r') && self.peek(1) == Some('#') {
            self.bump();
            self.bump();
        }
        while self.peek(0).is_some_and(is_ident_continue) {
            self.bump();
        }
        let kind = if KEYWORDS.contains(&self.text_from(start)) {
            TokenKind::Keyword
        } else {
            TokenKind::Identifier
        };
        self.push(kind, start);
    }

    fn consume_punct(&mut self) {
        let start = self.mark();
        for op in MULTI_PUNCT {
            let len = op.chars().count();
            if self.matches_ahead(op, len) {
                for _ in 0..len {
                    self.bump();
                }
                self.push(TokenKind::Punctuation, start);
                return;
            }
        }
        // Single character. ASCII punctuation is punctuation; anything else that
        // reached here is not lexable.
        let c = self.peek(0).unwrap_or('\0');
        self.bump();
        if c.is_ascii() && !c.is_alphanumeric() {
            self.push(TokenKind::Punctuation, start);
        } else {
            self.push(TokenKind::Unknown, start);
            self.diagnose(DiagnosticKind::UnexpectedCharacter, start);
        }
    }

    fn matches_ahead(&self, op: &str, len: usize) -> bool {
        op.chars()
            .enumerate()
            .all(|(k, ch)| self.peek(k) == Some(ch))
            && len > 0
    }
}

/// Lex `source` into tokens and diagnostics.
///
/// Crate-internal: the public entry point is
/// [`RustFrontend`](crate::RustFrontend).
#[must_use]
#[allow(clippy::redundant_pub_crate)] // crate-internal API reached from the crate root
pub(crate) fn lex(source: &str) -> (Vec<Token>, Vec<Diagnostic>) {
    Lexer::new(source).run()
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
    use super::*;

    fn kinds(source: &str) -> Vec<TokenKind> {
        lex(source).0.into_iter().map(|t| t.kind).collect()
    }

    fn texts(source: &str) -> Vec<String> {
        lex(source).0.iter().map(|t| t.text.to_string()).collect()
    }

    #[test]
    fn splits_keywords_identifiers_and_operators() {
        let (tokens, diags) = lex("fn add(a: i32) -> i32 { a + 1 }");
        assert!(diags.is_empty());
        let pairs: Vec<_> = tokens.iter().map(|t| (t.kind, t.text.as_str())).collect();
        assert_eq!(pairs[0], (TokenKind::Keyword, "fn"));
        assert_eq!(pairs[1], (TokenKind::Identifier, "add"));
        assert!(pairs.contains(&(TokenKind::Punctuation, "->")));
        assert!(
            pairs
                .iter()
                .any(|(k, t)| *k == TokenKind::Literal(LiteralKind::Integer) && *t == "1")
        );
    }

    #[test]
    fn cursor_tracks_utf8_bytes_without_a_character_index() {
        let mut lexer = Lexer::new("é fn");
        assert_eq!(lexer.chars.as_str(), "é fn");
        lexer.bump();
        assert_eq!(lexer.byte, "é".len());
        assert_eq!(lexer.chars.as_str(), " fn");
    }

    #[test]
    fn drops_comments_and_whitespace() {
        let src = "let x = 1; // trailing\n/* block /* nested */ */ let y = 2;";
        let texts = texts(src);
        assert!(!texts.iter().any(|t| t.contains("trailing")));
        assert!(!texts.iter().any(|t| t.contains("nested")));
        assert!(texts.contains(&"x".to_string()));
        assert!(texts.contains(&"y".to_string()));
    }

    #[test]
    fn distinguishes_lifetimes_from_char_literals() {
        let k = kinds("fn f<'a>(x: &'a str) -> char { 'z' }");
        assert!(k.contains(&TokenKind::Lifetime));
        assert!(k.contains(&TokenKind::Literal(LiteralKind::Char)));
    }

    #[test]
    fn keeps_hex_and_unicode_character_escapes_together() {
        let (tokens, diagnostics) =
            lex("let a = '\\x41'; let b = '\\u{1f980}'; let c = b'\\xFF'; let tail = 1;");
        assert!(diagnostics.is_empty());
        let characters: Vec<_> = tokens
            .iter()
            .filter(|token| token.kind == TokenKind::Literal(LiteralKind::Char))
            .map(|token| token.text.as_str())
            .collect();
        assert_eq!(characters, vec!["'\\x41'", "'\\u{1f980}'", "b'\\xFF'"]);
        assert!(tokens.iter().any(|token| token.text.as_str() == "tail"));
    }

    #[test]
    fn integer_suffixes_do_not_turn_into_exponents() {
        let (tokens, diagnostics) = lex("a[1usize+x]; b[1isize+y]; c[1e2+z]");
        assert!(diagnostics.is_empty());
        let literals: Vec<_> = tokens
            .iter()
            .filter(|token| matches!(token.kind, TokenKind::Literal(_)))
            .map(|token| (token.kind, token.text.as_str()))
            .collect();
        assert_eq!(
            literals,
            vec![
                (TokenKind::Literal(LiteralKind::Integer), "1usize"),
                (TokenKind::Literal(LiteralKind::Integer), "1isize"),
                (TokenKind::Literal(LiteralKind::Float), "1e2"),
            ]
        );
    }

    #[test]
    fn handles_raw_and_byte_strings() {
        // Source: let a = r#"x "q" y"#; let b = b"z"; let c = br#"w"#;
        let src = "let a = r#\"x \"q\" y\"#; let b = b\"z\"; let c = br#\"w\"#;";
        let (tokens, diags) = lex(src);
        assert!(diags.is_empty());
        let strings: Vec<_> = tokens
            .iter()
            .filter(|t| t.kind == TokenKind::Literal(LiteralKind::String))
            .map(|t| t.text.as_str())
            .collect();
        assert_eq!(strings, vec!["r#\"x \"q\" y\"#", "b\"z\"", "br#\"w\"#"]);
    }

    #[test]
    fn keeps_multi_hash_raw_strings_and_following_tokens() {
        let hashes_255 = "#".repeat(255);
        let source = format!(
            "let a = r##\"a \" b\"##; let b = r###\"c \" d\"###; let c = r{hashes_255}\"e \" f\"{hashes_255}; let tail = 1;"
        );
        let (tokens, diagnostics) = lex(&source);
        assert!(diagnostics.is_empty());

        let strings: Vec<_> = tokens
            .iter()
            .filter(|token| token.kind == TokenKind::Literal(LiteralKind::String))
            .map(|token| token.text.as_str())
            .collect();
        assert_eq!(
            strings,
            vec![
                "r##\"a \" b\"##",
                "r###\"c \" d\"###",
                format!("r{hashes_255}\"e \" f\"{hashes_255}").as_str(),
            ]
        );
        assert!(tokens.iter().any(|token| token.text.as_str() == "tail"));
    }

    #[test]
    fn handles_c_and_raw_c_strings() {
        let (tokens, diagnostics) = lex("let a = c\"path\"; let b = cr#\"raw # path\"#; tail();");
        assert!(diagnostics.is_empty());
        let strings: Vec<_> = tokens
            .iter()
            .filter(|token| token.kind == TokenKind::Literal(LiteralKind::String))
            .map(|token| token.text.as_str())
            .collect();
        assert_eq!(strings, vec!["c\"path\"", "cr#\"raw # path\"#"]);
        assert!(tokens.iter().any(|token| token.text.as_str() == "tail"));
    }

    #[test]
    fn unterminated_string_is_diagnosed() {
        // A string runs to end of file (Rust strings may span newlines), so an
        // unterminated one is diagnosed rather than silently accepted.
        let (_tokens, diags) = lex("let s = \"open;\nfn next() {}");
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].kind, DiagnosticKind::UnterminatedString);
    }

    #[test]
    fn recovers_after_an_unexpected_character() {
        // A stray non-lexable character is diagnosed, then lexing continues and
        // the following function is still tokenized.
        let (tokens, diags) = lex("let x = \u{20ac}; fn next() {}");
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].kind, DiagnosticKind::UnexpectedCharacter);
        assert!(
            tokens
                .iter()
                .any(|t| t.kind == TokenKind::Keyword && t.text == "fn")
        );
    }

    #[test]
    fn macro_invocation_is_plain_tokens() {
        let pairs: Vec<_> = lex("println!(\"{}\", x);")
            .0
            .into_iter()
            .map(|t| (t.kind, t.text))
            .collect();
        assert_eq!(pairs[0].0, TokenKind::Identifier);
        assert_eq!(pairs[0].1, "println");
        assert_eq!(pairs[1].0, TokenKind::Punctuation);
        assert_eq!(pairs[1].1, "!");
    }

    #[test]
    fn spans_are_byte_accurate_for_multibyte_source() {
        // The identifier `x` follows a two-byte `é` and a space.
        let (tokens, _) = lex("é x");
        let x = tokens.iter().find(|t| t.text == "x").expect("x token");
        assert_eq!(x.span.start_byte, 3);
        assert_eq!(x.span.end_byte, 4);
    }

    #[test]
    fn skips_a_leading_utf8_bom_without_shifting_source_columns() {
        let (tokens, diagnostics) = lex("\u{feff}fn f() {}");
        assert!(diagnostics.is_empty());

        let keyword = &tokens[0];
        assert_eq!(keyword.kind, TokenKind::Keyword);
        assert_eq!(keyword.text, "fn");
        assert_eq!(keyword.span.start_byte, 3);
        assert_eq!(keyword.span.start_line, 1);
        assert_eq!(keyword.span.start_column, 1);
    }
}