tabnas-c 0.5.10

C23 concrete-syntax-tree grammar plugin for the tabnas parsing engine, over the jsonic base grammar and the expr operator plugin
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/* Copyright (c) 2026 Richard Rodger and contributors, MIT License */

//! Focused lex matchers for the C parser. Each one does a single job
//! and returns either a token or `None`, meaning "not my prefix".
//!
//! Port of `ts/src/matchers.ts`. The canonical matchers are driven by
//! JavaScript regular expressions whose character classes are written
//! out in full (`[A-Za-z_$]`, `[0-9a-fA-F]`, and so on). They are hand
//! written scanners here rather than `regex` patterns, both because the
//! canonical ones index by character code and because `\d`, `\w`, `\s`
//! and `\b` in the `regex` crate are Unicode aware where JavaScript's
//! are not. Nothing in this file widens a class past ASCII.

use std::collections::HashMap;
use std::sync::Arc;

use tabnas::{Context, ImperativeLexMatcher, Lexer, Rule, Tin, Token, Value};

use crate::state::with_state;
use crate::tokens::{is_reserved, keyword_token_name_unchecked};

/// Token identities the matchers emit, resolved once at install time.
#[derive(Debug, Clone, Default)]
pub struct Tins {
    map: HashMap<String, Tin>,
}

impl Tins {
    pub fn insert(&mut self, name: &str, tin: Tin) {
        self.map.insert(name.to_string(), tin);
    }

    pub fn get(&self, name: &str) -> Tin {
        self.map.get(name).copied().unwrap_or(-1)
    }
}

/// Emit a token of `name` covering `consumed` characters of the
/// remaining source, then advance the cursor. The canonical `emit`
/// builds the token before advancing so its position is the start of
/// the token; the captured `point` does the same here.
fn emit(lexer: &mut Lexer<'_>, tins: &Tins, name: &str, consumed: usize) -> Option<Token> {
    let text: String = lexer.remaining().chars().take(consumed).collect();
    if text.is_empty() {
        return None;
    }
    let point = lexer.point();
    let token = lexer.token(
        name,
        tins.get(name),
        Value::String(text.clone()),
        text.as_str(),
        point,
    );
    lexer.advance_chars(text.chars().count());
    Some(token)
}

/// A diagnostic token over `[from, to)` character offsets relative to
/// the cursor, as the canonical `lex.bad(code, sI, end)` produces.
fn bad(lexer: &Lexer<'_>, code: &str, from_chars: usize, to_chars: usize) -> Option<Token> {
    let base = lexer.point().site.pos;
    Some(lexer.bad_span(code, base + from_chars, base + to_chars))
}

/// The remaining source as characters, so the scanners can index it the
/// way the canonical matchers index the source with `charCodeAt`.
///
/// The characters are the WHOLE source, built once per parse and shared
/// (see `CState::source_chars`); this is a handle onto the tail of it,
/// so taking one costs a reference count rather than a copy of
/// everything left to read.
struct Tail {
    all: std::rc::Rc<Vec<char>>,
    from: usize,
}

impl std::ops::Deref for Tail {
    type Target = [char];

    fn deref(&self) -> &[char] {
        self.all.get(self.from..).unwrap_or(&[])
    }
}

fn chars(lexer: &Lexer<'_>) -> Tail {
    let source = lexer.source();
    let key = (source.as_ptr() as usize, source.len());
    let all = with_state(|state| {
        if let Some((address, length, cached)) = &state.source_chars {
            if (*address, *length) == key {
                return std::rc::Rc::clone(cached);
            }
        }
        let built = std::rc::Rc::new(source.chars().collect::<Vec<char>>());
        state.source_chars = Some((key.0, key.1, std::rc::Rc::clone(&built)));
        built
    });
    Tail {
        all,
        // `Site::pos` is the cursor's position in Unicode scalar values,
        // which is what indexes the vector; `Site::si` is its byte
        // position, which does not.
        from: lexer.point().site.pos,
    }
}

fn at(text: &[char], index: usize) -> char {
    text.get(index).copied().unwrap_or('\0')
}

// ---------------------------------------------------------------------
// Whitespace
// ---------------------------------------------------------------------

/// C whitespace, minus the newline that terminates a preprocessor
/// directive.
fn whitespace(lexer: &mut Lexer<'_>, tins: &Tins) -> Option<Token> {
    let text = chars(lexer);
    let first = at(&text, 0);
    if text.is_empty() || !matches!(first, ' ' | '\t' | '\u{0b}' | '\u{0c}' | '\r' | '\n') {
        return None;
    }
    let in_directive = with_state(|state| state.mode.in_directive);
    let mut index = 0;
    while index < text.len() {
        match text[index] {
            ' ' | '\t' | '\u{0b}' | '\u{0c}' | '\r' => index += 1,
            '\n' => {
                if in_directive {
                    break;
                }
                index += 1;
            }
            _ => break,
        }
    }
    if index == 0 {
        return None;
    }
    emit(lexer, tins, "#SP", index)
}

// ---------------------------------------------------------------------
// Line continuation
// ---------------------------------------------------------------------

fn line_cont(lexer: &mut Lexer<'_>, tins: &Tins) -> Option<Token> {
    let text = chars(lexer);
    if at(&text, 0) != '\\' {
        return None;
    }
    let consumed = if at(&text, 1) == '\n' {
        2
    } else if at(&text, 1) == '\r' && at(&text, 2) == '\n' {
        3
    } else if at(&text, 1) == '\r' {
        2
    } else {
        return None;
    };
    emit(lexer, tins, "TRIVIA_LINE_CONT", consumed)
}

// ---------------------------------------------------------------------
// Comments
// ---------------------------------------------------------------------

fn line_comment(lexer: &mut Lexer<'_>, tins: &Tins) -> Option<Token> {
    let text = chars(lexer);
    if at(&text, 0) != '/' || at(&text, 1) != '/' {
        return None;
    }
    let mut index = 2;
    while index < text.len() && !matches!(text[index], '\n' | '\r') {
        index += 1;
    }
    emit(lexer, tins, "TRIVIA_LINE_COMMENT", index)
}

fn block_comment(lexer: &mut Lexer<'_>, tins: &Tins) -> Option<Token> {
    let text = chars(lexer);
    if at(&text, 0) != '/' || at(&text, 1) != '*' {
        return None;
    }
    let mut index = 2;
    while index + 1 < text.len() {
        if text[index] == '*' && text[index + 1] == '/' {
            return emit(lexer, tins, "TRIVIA_BLOCK_COMMENT", index + 2);
        }
        index += 1;
    }
    // The canonical matcher reports the span from the opener to the end
    // of the WHOLE source, so the end offset is absolute there.
    let end = lexer.source().chars().count();
    let base = lexer.point().site.pos;
    Some(lexer.bad_span("unterminated_comment", base, end))
}

// ---------------------------------------------------------------------
// Preprocessor boundaries
// ---------------------------------------------------------------------

/// True when only whitespace separates this offset from the start of a
/// logical line.
fn at_line_start(before: &str) -> bool {
    for character in before.chars().rev() {
        match character {
            '\n' | '\r' => return true,
            ' ' | '\t' => continue,
            _ => return false,
        }
    }
    true
}

fn pp_directive_open(lexer: &mut Lexer<'_>, tins: &Tins) -> Option<Token> {
    let text = chars(lexer);
    let consumed = if at(&text, 0) == '#' {
        1
    } else if at(&text, 0) == '%' && at(&text, 1) == ':' {
        2
    } else {
        return None;
    };
    let position = lexer.point().site.si;
    if !at_line_start(&lexer.source()[..position]) {
        return None;
    }
    with_state(|state| {
        state.mode.in_directive = true;
        state.mode.directive_name = None;
        state.mode.expect_header_name = false;
    });
    emit(lexer, tins, "PP_HASH", consumed)
}

fn pp_newline(lexer: &mut Lexer<'_>, tins: &Tins) -> Option<Token> {
    if !with_state(|state| state.mode.in_directive) {
        return None;
    }
    let text = chars(lexer);
    let first = at(&text, 0);
    if first != '\n' && first != '\r' {
        return None;
    }
    let consumed = if first == '\r' && at(&text, 1) == '\n' {
        2
    } else {
        1
    };
    with_state(|state| {
        state.mode.in_directive = false;
        state.mode.directive_name = None;
        state.mode.expect_header_name = false;
    });
    emit(lexer, tins, "PP_NEWLINE", consumed)
}

fn header_name(lexer: &mut Lexer<'_>, tins: &Tins) -> Option<Token> {
    let armed = with_state(|state| state.mode.in_directive && state.mode.expect_header_name);
    if !armed {
        return None;
    }
    let text = chars(lexer);
    let close = match at(&text, 0) {
        '<' => '>',
        '"' => '"',
        _ => return None,
    };
    let mut index = 1;
    while index < text.len() {
        let character = text[index];
        if character == '\n' {
            return bad(lexer, "unterminated_header_name", 0, index);
        }
        if character == close {
            index += 1;
            with_state(|state| state.mode.expect_header_name = false);
            return emit(lexer, tins, "LIT_HEADER_NAME", index);
        }
        index += 1;
    }
    bad(lexer, "unterminated_header_name", 0, text.len())
}

// ---------------------------------------------------------------------
// Identifiers, keywords, typedef names and macro names
// ---------------------------------------------------------------------

fn is_id_start(character: char) -> bool {
    character.is_ascii_alphabetic() || character == '_' || character == '$'
}

fn is_id_part(character: char) -> bool {
    character.is_ascii_alphanumeric() || character == '_' || character == '$'
}

fn identifier(lexer: &mut Lexer<'_>, tins: &Tins) -> Option<Token> {
    let text = chars(lexer);
    if text.is_empty() || !is_id_start(text[0]) {
        return None;
    }
    let mut index = 1;
    while index < text.len() && is_id_part(text[index]) {
        index += 1;
    }
    let word: String = text[..index].iter().collect();

    if is_reserved(&word) {
        let name = keyword_token_name_unchecked(&word);
        with_state(|state| {
            if state.mode.in_directive && state.mode.directive_name.is_none() {
                state.mode.directive_name = Some(word.clone());
            }
        });
        return emit(lexer, tins, &name, index);
    }

    let names_directive = with_state(|state| {
        if state.mode.in_directive && state.mode.directive_name.is_none() {
            state.mode.directive_name = Some(word.clone());
            if word == "include" || word == "embed" || word == "include_next" {
                state.mode.expect_header_name = true;
            }
            true
        } else {
            false
        }
    });
    if names_directive {
        return emit(lexer, tins, "ID", index);
    }

    let name = with_state(|state| {
        if !state.mode.in_directive && state.symbols.is_typedef(&word) {
            "TYPEDEF_NAME"
        } else if !state.mode.in_directive && state.macros.has(&word) {
            "MACRO_NAME"
        } else {
            "ID"
        }
    });
    emit(lexer, tins, name, index)
}

// ---------------------------------------------------------------------
// Numeric literals
// ---------------------------------------------------------------------

/// Digits of a run that may carry C23 `'` separators: one leading digit
/// accepted by `first`, then any number of `'` or digits accepted by
/// `rest`. Returns the number of characters consumed, or zero.
fn digit_run(text: &[char], from: usize, accept: impl Fn(char) -> bool + Copy) -> usize {
    if from >= text.len() || !accept(text[from]) {
        return 0;
    }
    let mut index = from + 1;
    while index < text.len() && (text[index] == '\'' || accept(text[index])) {
        index += 1;
    }
    index - from
}

fn is_octal(character: char) -> bool {
    ('0'..='7').contains(&character)
}

fn is_binary(character: char) -> bool {
    character == '0' || character == '1'
}

/// The integer-literal scanner, spelling out `INT_RE`. Returns the
/// length of the numeric part and the length of the whole match
/// (numeric part plus suffix).
fn scan_integer(text: &[char]) -> Option<(usize, usize)> {
    let number = if at(text, 0) == '0' && matches!(at(text, 1), 'x' | 'X') {
        let run = digit_run(text, 2, |character| character.is_ascii_hexdigit());
        if run == 0 {
            0
        } else {
            2 + run
        }
    } else {
        0
    };
    let number = if number > 0 {
        number
    } else if at(text, 0) == '0' && matches!(at(text, 1), 'b' | 'B') {
        let run = digit_run(text, 2, is_binary);
        if run == 0 {
            0
        } else {
            2 + run
        }
    } else {
        0
    };
    let number = if number > 0 {
        number
    } else if at(text, 0) == '0' {
        // Octal, which also matches a lone `0`.
        let mut index = 1;
        while index < text.len() && (text[index] == '\'' || is_octal(text[index])) {
            index += 1;
        }
        index
    } else if ('1'..='9').contains(&at(text, 0)) {
        digit_run(text, 0, |character| character.is_ascii_digit())
    } else {
        0
    };
    if number == 0 {
        return None;
    }
    // `([uUlL]*[wWbBzZ]*[uUlL]*)?`, greedy and without backtracking:
    // the three classes are disjoint.
    let mut index = number;
    while index < text.len() && matches!(text[index], 'u' | 'U' | 'l' | 'L') {
        index += 1;
    }
    while index < text.len() && matches!(text[index], 'w' | 'W' | 'b' | 'B' | 'z' | 'Z') {
        index += 1;
    }
    while index < text.len() && matches!(text[index], 'u' | 'U' | 'l' | 'L') {
        index += 1;
    }
    Some((number, index))
}

fn integer(lexer: &mut Lexer<'_>, tins: &Tins) -> Option<Token> {
    let text = chars(lexer);
    if !at(&text, 0).is_ascii_digit() {
        return None;
    }
    let (number, whole) = scan_integer(&text)?;
    let hex = at(&text, 0) == '0' && matches!(at(&text, 1), 'x' | 'X');
    let after = at(&text, whole);
    if after == '.' || after == 'e' || after == 'E' {
        // For a hex literal only `p`/`P` introduces an exponent, so a
        // trailing `e` stays part of the integer.
        if !hex {
            return None;
        }
    }
    if hex && (after == '.' || after == 'p' || after == 'P') {
        return None;
    }
    let _ = number;
    emit(lexer, tins, "LIT_INT", whole)
}

/// `FLOAT_HEX_RE`.
fn scan_float_hex(text: &[char]) -> Option<usize> {
    if at(text, 0) != '0' || !matches!(at(text, 1), 'x' | 'X') {
        return None;
    }
    let hexit = |character: char| character.is_ascii_hexdigit();
    let mantissa = {
        let run = digit_run(text, 2, hexit);
        if run > 0 && at(text, 2 + run) == '.' {
            // `hex+ . hex*`
            let fraction = digit_run(text, 2 + run + 1, hexit);
            Some(2 + run + 1 + fraction)
        } else if at(text, 2) == '.' {
            // `. hex+`
            let fraction = digit_run(text, 3, hexit);
            if fraction == 0 {
                None
            } else {
                Some(3 + fraction)
            }
        } else if run > 0 {
            // `hex+`
            Some(2 + run)
        } else {
            None
        }
    }?;
    let mut index = mantissa;
    if !matches!(at(text, index), 'p' | 'P') {
        return None;
    }
    index += 1;
    if matches!(at(text, index), '+' | '-') {
        index += 1;
    }
    let exponent = digit_run(text, index, |character| character.is_ascii_digit());
    if exponent == 0 {
        return None;
    }
    index += exponent;
    if matches!(at(text, index), 'f' | 'F' | 'l' | 'L') {
        index += 1;
    }
    Some(index)
}

/// The optional decimal exponent `(?:[eE][+-]?[0-9](['0-9])*)?`.
/// Returns the characters consumed, which is zero when absent.
fn decimal_exponent(text: &[char], from: usize) -> usize {
    if !matches!(at(text, from), 'e' | 'E') {
        return 0;
    }
    let mut index = from + 1;
    if matches!(at(text, index), '+' | '-') {
        index += 1;
    }
    let digits = digit_run(text, index, |character| character.is_ascii_digit());
    if digits == 0 {
        return 0;
    }
    index + digits - from
}

/// `FLOAT_DEC_RE`, alternative by alternative in the order the pattern
/// declares them.
fn scan_float_dec(text: &[char]) -> Option<usize> {
    let digit = |character: char| character.is_ascii_digit();
    let lead = digit_run(text, 0, digit);

    // `(?:[0-9](['0-9])*)?\.[0-9](['0-9])*(?:exp)?`
    for start in [lead, 0] {
        if start > 0 && lead == 0 {
            continue;
        }
        if at(text, start) == '.' {
            let fraction = digit_run(text, start + 1, digit);
            if fraction > 0 {
                let mut index = start + 1 + fraction;
                index += decimal_exponent(text, index);
                return Some(index + float_suffix(text, index));
            }
        }
        if start == 0 {
            break;
        }
    }

    // `[0-9](['0-9])*\.(?:exp)?`
    if lead > 0 && at(text, lead) == '.' {
        let mut index = lead + 1;
        index += decimal_exponent(text, index);
        return Some(index + float_suffix(text, index));
    }

    // `[0-9](['0-9])*[eE][+-]?[0-9](['0-9])*`
    if lead > 0 {
        let exponent = decimal_exponent(text, lead);
        if exponent > 0 {
            let index = lead + exponent;
            return Some(index + float_suffix(text, index));
        }
    }

    None
}

/// `[fFlLdD]?[fFlL]?`.
fn float_suffix(text: &[char], from: usize) -> usize {
    let mut index = from;
    if matches!(at(text, index), 'f' | 'F' | 'l' | 'L' | 'd' | 'D') {
        index += 1;
    }
    if matches!(at(text, index), 'f' | 'F' | 'l' | 'L') {
        index += 1;
    }
    index - from
}

fn float(lexer: &mut Lexer<'_>, tins: &Tins) -> Option<Token> {
    let text = chars(lexer);
    let first = at(&text, 0);
    let starts_digit = first.is_ascii_digit();
    let starts_dot = first == '.' && at(&text, 1).is_ascii_digit();
    if !starts_digit && !starts_dot {
        return None;
    }
    let length = scan_float_hex(&text).or_else(|| scan_float_dec(&text))?;
    let matched = &text[..length];
    let hex = matched.len() >= 2 && matched[0] == '0' && matches!(matched[1], 'x' | 'X');
    let has_float_mark = matched.iter().any(|character| {
        matches!(
            character,
            '.' | 'e' | 'E' | 'p' | 'P' | 'f' | 'F' | 'l' | 'L'
        )
    });
    if !has_float_mark && !hex {
        return None;
    }
    emit(lexer, tins, "LIT_FLOAT", length)
}

// ---------------------------------------------------------------------
// Character and string literals
// ---------------------------------------------------------------------

/// `^(L|u8|u|U)?'`, returning the prefix length.
fn char_prefix(text: &[char]) -> Option<usize> {
    for prefix in [&['L'][..], &['u', '8'][..], &['u'][..], &['U'][..], &[][..]] {
        if text.len() > prefix.len()
            && text[..prefix.len()] == *prefix
            && text[prefix.len()] == '\''
        {
            return Some(prefix.len() + 1);
        }
    }
    None
}

fn char_literal(lexer: &mut Lexer<'_>, tins: &Tins) -> Option<Token> {
    let text = chars(lexer);
    let mut index = char_prefix(&text)?;
    while index < text.len() {
        let character = text[index];
        if character == '\n' {
            return bad(lexer, "unterminated_char", 0, index);
        }
        if character == '\\' {
            index += 2;
            continue;
        }
        if character == '\'' {
            return emit(lexer, tins, "LIT_CHAR", index + 1);
        }
        index += 1;
    }
    bad(lexer, "unterminated_char", 0, text.len())
}

/// `^(u8|u|U|L)?(R)?"`, returning the prefix length and whether the
/// literal is a raw one.
fn string_prefix(text: &[char]) -> Option<(usize, bool)> {
    for encoding in [&['u', '8'][..], &['u'][..], &['U'][..], &['L'][..], &[][..]] {
        if text.len() < encoding.len() || text[..encoding.len()] != *encoding {
            continue;
        }
        let mut index = encoding.len();
        let raw = at(text, index) == 'R';
        if raw {
            index += 1;
        }
        if at(text, index) == '"' {
            return Some((index + 1, raw));
        }
    }
    None
}

fn string_literal(lexer: &mut Lexer<'_>, tins: &Tins) -> Option<Token> {
    let text = chars(lexer);
    let (prefix, raw) = string_prefix(&text)?;
    let mut index = prefix;
    if raw {
        // `R"delim( ... )delim"`
        let mut delimiter_end = index;
        while delimiter_end < text.len() && text[delimiter_end] != '(' {
            delimiter_end += 1;
        }
        if delimiter_end >= text.len() {
            return bad(lexer, "unterminated_string", 0, text.len());
        }
        let delimiter: String = text[index..delimiter_end].iter().collect();
        let closer: Vec<char> = format!("){delimiter}\"").chars().collect();
        let mut scan = delimiter_end + 1;
        let close = loop {
            if scan + closer.len() > text.len() {
                break None;
            }
            if text[scan..scan + closer.len()] == closer[..] {
                break Some(scan);
            }
            scan += 1;
        };
        let Some(close) = close else {
            return bad(lexer, "unterminated_string", 0, text.len());
        };
        return emit(lexer, tins, "LIT_STRING", close + closer.len());
    }
    while index < text.len() {
        let character = text[index];
        if character == '\n' {
            return bad(lexer, "unterminated_string", 0, index);
        }
        if character == '\\' {
            index += 2;
            continue;
        }
        if character == '"' {
            return emit(lexer, tins, "LIT_STRING", index + 1);
        }
        index += 1;
    }
    bad(lexer, "unterminated_string", 0, text.len())
}

// ---------------------------------------------------------------------
// Punctuators
// ---------------------------------------------------------------------

/// The punctuator catalog, longest source first, so the dispatcher
/// matches the longest form.
fn sorted_punctuators() -> Vec<(&'static str, &'static str)> {
    let mut sorted: Vec<(&str, &str)> = crate::tokens::PUNCTUATORS.to_vec();
    // A stable sort by descending source length, as the canonical
    // `[...PUNCTUATORS].sort((a, b) => b[1].length - a[1].length)` is.
    sorted.sort_by_key(|entry| std::cmp::Reverse(entry.1.len()));
    sorted
}

fn punctuator(
    lexer: &mut Lexer<'_>,
    tins: &Tins,
    sorted: &[(&'static str, &'static str)],
) -> Option<Token> {
    let remaining = lexer.remaining();
    for (name, source) in sorted {
        if remaining.starts_with(source) {
            let name = *name;
            let consumed = source.chars().count();
            return emit(lexer, tins, name, consumed);
        }
    }
    None
}

// ---------------------------------------------------------------------
// The catalog
// ---------------------------------------------------------------------

/// Name and order of every matcher, in the canonical declaration order.
/// Lower order runs first.
pub const MATCHER_ORDER: &[(&str, f64)] = &[
    ("c_line_cont", 100.0),
    ("c_block_comment", 110.0),
    ("c_line_comment", 120.0),
    ("c_pp_newline", 130.0),
    ("c_pp_open", 140.0),
    ("c_header_name", 150.0),
    ("c_whitespace", 160.0),
    ("c_string", 200.0),
    ("c_char", 210.0),
    ("c_float", 220.0),
    ("c_int", 230.0),
    ("c_identifier", 240.0),
    ("c_punctuator", 900.0),
];

/// Build the matcher named `name`.
pub fn make(name: &str, tins: Tins) -> ImperativeLexMatcher {
    match name {
        "c_line_cont" => wrap(tins, line_cont),
        "c_block_comment" => wrap(tins, block_comment),
        "c_line_comment" => wrap(tins, line_comment),
        "c_pp_newline" => wrap(tins, pp_newline),
        "c_pp_open" => wrap(tins, pp_directive_open),
        "c_header_name" => wrap(tins, header_name),
        "c_whitespace" => wrap(tins, whitespace),
        "c_string" => wrap(tins, string_literal),
        "c_char" => wrap(tins, char_literal),
        "c_float" => wrap(tins, float),
        "c_int" => wrap(tins, integer),
        "c_identifier" => wrap(tins, identifier),
        "c_punctuator" => {
            let sorted = sorted_punctuators();
            Arc::new(
                move |lexer: &mut Lexer<'_>, _rule: &mut Rule, _context: &mut Context| {
                    punctuator(lexer, &tins, &sorted)
                },
            )
        }
        _ => wrap(tins, |_lexer, _tins| None),
    }
}

fn wrap(
    tins: Tins,
    body: impl Fn(&mut Lexer<'_>, &Tins) -> Option<Token> + Send + Sync + 'static,
) -> ImperativeLexMatcher {
    Arc::new(
        move |lexer: &mut Lexer<'_>, _rule: &mut Rule, _context: &mut Context| body(lexer, &tins),
    )
}