willbooster-tree-sitter-cpp 44.0.0

C++ grammar for tree-sitter
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
// code-gauge-allow-file fileNcss -- Preprocessor tokens and pragma operators share names, spacing, and literal readers.
#ifndef TREE_SITTER_C_PRAGMA_H_
#define TREE_SITTER_C_PRAGMA_H_

#include "tree_sitter/parser.h"
#include "identifier.h"
#include "gnuRawString.h"
#include <string.h>

static const char pragma_word[] = "_Pragma";

typedef struct {
    bool (*skip_empty)(TSLexer *, void *);
    void *empty_context;
} PragmaTrivia;

typedef struct {
    char *word;
    unsigned capacity;
    unsigned length;
    bool ascii;
    bool lookahead_only;
    bool preserve_end;
} MacroName;

typedef enum {
    PREPROC_ARGUMENT_TOKENS,
    PREPROC_ARGUMENT_TEXT,
    PREPROC_ARGUMENT_PRAGMA,
} PreprocArgumentMode;

typedef enum {
    MACRO_NAME_END,
    MACRO_NAME_CHARACTER,
    MACRO_NAME_SPLICE,
    MACRO_NAME_INVALID,
} MacroNameCharacter;

static bool scan_macro_name(TSLexer *lexer, MacroName *name);
static bool scan_macro_name_from(TSLexer *lexer, MacroName *name, bool consumed_backslash);
static bool scan_macro_name_tail(TSLexer *lexer, MacroName *name, bool has_name);
static bool scan_macro_name_ascii(TSLexer *lexer, MacroName *name);
static void scan_macro_name_start(TSLexer *lexer, const MacroName *name);
static MacroNameCharacter scan_macro_name_character(TSLexer *lexer, bool continuation, MacroName *name);
static bool scan_macro_name_ucn(TSLexer *lexer);
static void skip_leading_spaces_and_splices(TSLexer *lexer, bool (*is_space)(int32_t), bool *newline, bool *consumed_backslash);
static bool skip_leading_space_run(TSLexer *lexer, bool (*is_space)(int32_t), bool *newline);
static uint32_t skip_ascii_space_units(TSLexer *lexer, bool *newline);
static int32_t peek_after_ascii_spaces(const TSLexer *lexer);

static bool scan_pragma_argument(TSLexer *lexer);
static bool scan_pragma_spacing(TSLexer *lexer);
static bool scan_pragma_comment(TSLexer *lexer);
static bool scan_preproc_line_comment(TSLexer *lexer);
static bool scan_preproc_block_comment(TSLexer *lexer);
static bool scan_pragma_spacing_with(TSLexer *lexer, const PragmaTrivia *trivia);
static bool scan_pragma_empty(TSLexer *lexer, const PragmaTrivia *trivia);
static bool scan_pragma_suffix_end_with(TSLexer *lexer, bool mark_end, const PragmaTrivia *trivia);
static bool scan_pragma_suffix(TSLexer *lexer);
static bool scan_pragma_suffix_end(TSLexer *lexer, bool mark_end);
static inline bool pragma_space(int32_t c);
static bool scan_pragma_word(TSLexer *lexer);
static bool scan_preproc_newline(TSLexer *lexer, bool skip);
static bool scan_preproc_splices(TSLexer *lexer);
static bool scan_preproc_ucn(TSLexer *lexer, bool *in_number);
static bool preproc_word(int32_t c, bool continuation);
static bool preproc_identifier_character(int32_t c, bool continuation);
static bool scan_preproc_quote(TSLexer *lexer, int32_t quote, bool escaped, bool directive_text);
static bool scan_preproc_raw_quote(TSLexer *lexer);

static bool scan_pragma_with(TSLexer *lexer, const PragmaTrivia *trivia) {
    while (pragma_space(lexer->lookahead)) {
        lexer->advance(lexer, true);
    }
    if (!scan_pragma_word(lexer)) return false;
    return scan_pragma_suffix_end_with(lexer, true, trivia);
}

static inline bool scan_pragma(TSLexer *lexer) {
    return scan_pragma_with(lexer, NULL);
}

static bool scan_pragma_suffix(TSLexer *lexer) {
    return scan_pragma_suffix_end(lexer, true);
}

static bool scan_pragma_suffix_end(TSLexer *lexer, bool mark_end) {
    return scan_pragma_suffix_end_with(lexer, mark_end, NULL);
}

static bool scan_pragma_suffix_end_with(TSLexer *lexer, bool mark_end, const PragmaTrivia *trivia) {
    if (!scan_pragma_spacing_with(lexer, trivia) || lexer->lookahead != '(') return false;
    lexer->advance(lexer, false);
    if (!scan_pragma_spacing_with(lexer, trivia)) return false;
    if (!scan_pragma_argument(lexer)) return false;
    if (!scan_pragma_spacing_with(lexer, trivia) || lexer->lookahead != ')') return false;
    lexer->advance(lexer, false);
    if (mark_end) lexer->mark_end(lexer);
    return true;
}

static bool scan_pragma_string_prefix(TSLexer *lexer, bool *raw);

static bool scan_pragma_argument(TSLexer *lexer) {
    bool raw = false;
    if (!scan_pragma_string_prefix(lexer, &raw) || lexer->lookahead != '"') return false;
    lexer->advance(lexer, false);
    if (raw) return scan_gnu_raw_string_end(lexer);
    while (lexer->lookahead != '"') {
        if (pragma_eof(lexer) || lexer->lookahead == '\n' || lexer->lookahead == '\r') return false;
        if (lexer->lookahead == '\\') {
            lexer->advance(lexer, false);
            if (pragma_eof(lexer)) return false;
            if (scan_preproc_newline(lexer, false)) continue;
        }
        lexer->advance(lexer, false);
    }
    lexer->advance(lexer, false);
    return true;
}

static bool scan_pragma_string_prefix(TSLexer *lexer, bool *raw) {
    if (lexer->lookahead == 'L' || lexer->lookahead == 'u' || lexer->lookahead == 'U') {
        bool utf8 = lexer->lookahead == 'u';
        lexer->advance(lexer, false);
        if (!scan_preproc_splices(lexer)) return false;
        if (utf8 && lexer->lookahead == '8') lexer->advance(lexer, false);
        if (!scan_preproc_splices(lexer)) return false;
    }
    *raw = lexer->lookahead == 'R';
    if (*raw) lexer->advance(lexer, false);
    return scan_preproc_splices(lexer);
}

static bool scan_pragma_spacing_with(TSLexer *lexer, const PragmaTrivia *trivia) {
    for (;;) {
        if (!scan_pragma_spacing(lexer)) return false;
        if (!scan_pragma_empty(lexer, trivia)) return true;
    }
}

static bool scan_pragma_empty(TSLexer *lexer, const PragmaTrivia *trivia) {
    return trivia && trivia->skip_empty && trivia->skip_empty(lexer, trivia->empty_context);
}

static bool scan_pragma_spacing(TSLexer *lexer) {
    for (;;) {
        if (pragma_space(lexer->lookahead)) {
            lexer->advance(lexer, false);
        } else if (lexer->lookahead == '\\') {
            lexer->advance(lexer, false);
            if (!scan_preproc_newline(lexer, false)) return false;
        } else if (lexer->lookahead == '/') {
            if (!scan_pragma_comment(lexer)) return false;
        } else {
            return true;
        }
    }
}

static bool scan_pragma_comment(TSLexer *lexer) {
    lexer->advance(lexer, false);
    if (!scan_preproc_splices(lexer)) return false;
    if (lexer->lookahead == '/') return scan_preproc_line_comment(lexer);
    if (lexer->lookahead == '*') return scan_preproc_block_comment(lexer);
    return false;
}

static bool scan_preproc_line_comment(TSLexer *lexer) {
    lexer->advance(lexer, false);
    while (!pragma_eof(lexer) && lexer->lookahead != '\n' && lexer->lookahead != '\r') {
        int32_t c = lexer->lookahead;
        lexer->advance(lexer, false);
        if (c == '\\') scan_preproc_newline(lexer, false);
    }
    return true;
}

static bool scan_preproc_block_comment(TSLexer *lexer) {
    lexer->advance(lexer, false);
    bool star = false;
    while (!pragma_eof(lexer)) {
        int32_t c = lexer->lookahead;
        lexer->advance(lexer, false);
        if (c == '\\' && scan_preproc_newline(lexer, false)) continue;
        if (star && c == '/') return true;
        star = c == '*';
    }
    return false;
}

static PreprocArgumentMode preproc_argument_mode(bool tokens, bool directive, bool lparen) {
    if (tokens && directive && !lparen) return PREPROC_ARGUMENT_PRAGMA;
    return !tokens && directive ? PREPROC_ARGUMENT_TEXT : PREPROC_ARGUMENT_TOKENS;
}

static bool scan_preproc_arg(TSLexer *lexer, bool consumed_backslash, PreprocArgumentMode mode) {
    bool directive_text = mode != PREPROC_ARGUMENT_TOKENS;
    bool raw_allowed = mode != PREPROC_ARGUMENT_TEXT;
    bool has_content = consumed_backslash;
    while (!has_content && pragma_space(lexer->lookahead) && lexer->lookahead != '\n' && lexer->lookahead != '\r') {
        lexer->advance(lexer, true);
    }
    bool after_comment = false;
    bool in_number = false;
    bool in_identifier = consumed_backslash && scan_preproc_ucn(lexer, &in_number);
    GnuRawStringPrefix raw_prefix = {0};
    if (consumed_backslash) gnu_raw_string_prefix_invalidate(&raw_prefix);
    if (consumed_backslash) lexer->mark_end(lexer);
    int32_t number_last = 0;
    while (!pragma_eof(lexer) && lexer->lookahead != '\n' && lexer->lookahead != '\r') {
        int32_t c = lexer->lookahead;
        bool word = preproc_word(c, in_identifier || in_number);
        bool digit = c >= '0' && c <= '9';
        bool exponent_sign = in_number && (c == '+' || c == '-') &&
                             (number_last == 'e' || number_last == 'E' || number_last == 'p' || number_last == 'P');
        if (c == '.') in_identifier = false;
        if (!word && !digit && c != '.' && c != '\'' && c != '\\' && !exponent_sign) {
            in_number = false;
            in_identifier = false;
        }
        if (digit && !in_identifier) in_number = true;
        if (word && !in_number) in_identifier = true;
        if (in_number && c != '\\') number_last = c;
        if (c != '"' && c != '\\') {
            if (in_number) gnu_raw_string_prefix_invalidate(&raw_prefix);
            else if (word || digit || exponent_sign) gnu_raw_string_prefix_character(&raw_prefix, c);
            else raw_prefix = (GnuRawStringPrefix){0};
        }
        if (lexer->lookahead == '/') {
            lexer->advance(lexer, false);
            bool split_opener = lexer->lookahead == '\\';
            bool delimiter = scan_preproc_splices(lexer);
            if (delimiter && lexer->lookahead == '/') {
                if (!has_content || after_comment) break;
                while (!pragma_eof(lexer) && lexer->lookahead != '\n' && lexer->lookahead != '\r') {
                    if (lexer->lookahead == '\\') {
                        do {
                            lexer->advance(lexer, false);
                        } while (lexer->lookahead == '\\');
                        scan_preproc_newline(lexer, false);
                    } else {
                        lexer->advance(lexer, false);
                    }
                }
                lexer->mark_end(lexer);
                return true;
            }
            if (delimiter && lexer->lookahead == '*') {
                if (!has_content) return false;
                lexer->advance(lexer, false);
                bool star = false;
                bool split_delimiter = split_opener;
                while (!pragma_eof(lexer)) {
                    if (lexer->lookahead == '\\') {
                        if (!scan_preproc_splices(lexer)) star = false;
                        else if (star && lexer->lookahead == '/') split_delimiter = true;
                        continue;
                    }
                    if (star && lexer->lookahead == '/') break;
                    star = lexer->lookahead == '*';
                    lexer->advance(lexer, false);
                }
                if (pragma_eof(lexer)) break;
                lexer->advance(lexer, false);
                after_comment = !split_delimiter;
                if (split_delimiter) lexer->mark_end(lexer);
                continue;
            }
            if (!delimiter) in_identifier = scan_preproc_ucn(lexer, &in_number);
            has_content = true;
            after_comment = false;
        } else if (lexer->lookahead == '\\') {
            lexer->advance(lexer, false);
            if (scan_preproc_newline(lexer, false)) {
                if (!after_comment && has_content) lexer->mark_end(lexer);
                continue;
            }
            gnu_raw_string_prefix_invalidate(&raw_prefix);
            if (scan_preproc_ucn(lexer, &in_number)) {
                if (!in_number) in_identifier = true;
                number_last = 0;
            } else {
                in_number = false;
                in_identifier = false;
            }
            has_content = true;
            after_comment = false;
        } else if (lexer->lookahead == '"' || (raw_allowed && lexer->lookahead == '\'')) {
            int32_t quote = lexer->lookahead;
            bool raw = raw_allowed && quote == '"' && gnu_raw_string_prefix(&raw_prefix);
            lexer->advance(lexer, false);
            bool escaped = false;
            if (quote == '\'' && in_number) {
                bool spliced = scan_preproc_splices(lexer);
                int32_t next = lexer->lookahead;
                bool continuation = spliced && ((next >= '0' && next <= '9') || (next >= 'a' && next <= 'z') ||
                                                (next >= 'A' && next <= 'Z') || next == '_');
                escaped = !spliced;
                if (continuation) {
                    has_content = true;
                    after_comment = false;
                    number_last = 0;
                    lexer->mark_end(lexer);
                    continue;
                }
            }
            in_number = false;
            in_identifier = false;
            bool quoted = raw ? scan_preproc_raw_quote(lexer) : scan_preproc_quote(lexer, quote, escaped, directive_text);
            if (!quoted) return true;
            raw_prefix = (GnuRawStringPrefix){0};
            has_content = true;
            after_comment = false;
        } else {
            if (!pragma_space(lexer->lookahead)) {
                has_content = true;
                after_comment = false;
            }
            lexer->advance(lexer, false);
        }
        if (!after_comment && has_content) lexer->mark_end(lexer);
    }
    return has_content;
}

static bool scan_preproc_raw_quote(TSLexer *lexer) {
    if (scan_gnu_raw_string_end(lexer)) return true;
    while (!pragma_eof(lexer) && lexer->lookahead != '\n' && lexer->lookahead != '\r') lexer->advance(lexer, false);
    lexer->mark_end(lexer);
    return false;
}

typedef struct {
    bool literal, slash, block, line, star, split_comment, after_comment, directive_text;
} PreprocQuoteState;

static bool scan_preproc_quote_step(TSLexer *lexer, PreprocQuoteState *state, int32_t c);

// code-gauge-allow functionCognitiveComplexity -- Unterminated quote recovery tracks line splices alongside comment fallback boundaries.
static bool scan_preproc_quote(TSLexer *lexer, int32_t quote, bool escaped, bool directive_text) {
    lexer->mark_end(lexer);
    PreprocQuoteState state = {.literal = true, .directive_text = directive_text};
    while (!pragma_eof(lexer) && (state.block || (lexer->lookahead != '\n' && lexer->lookahead != '\r'))) {
        int32_t c = lexer->lookahead;
        lexer->advance(lexer, false);
        if (c == '\n' || c == '\r') state.literal = false;
        if (state.literal && c == quote && !escaped) {
            lexer->mark_end(lexer);
            return true;
        }
        if (c == '\\' && scan_preproc_newline(lexer, false)) {
            if (state.slash || state.star) state.split_comment = true;
            if (!state.block && !state.slash && !state.after_comment) lexer->mark_end(lexer);
            continue;
        }
        escaped = c == '\\' && !escaped;
        if (!scan_preproc_quote_step(lexer, &state, c)) return false;
    }
    if (state.slash) lexer->mark_end(lexer);
    return false;
}

// code-gauge-allow functionCognitiveComplexity -- Comment transitions in unterminated quotes share split-splice and mark-end state.
// code-gauge-allow functionNcss -- Comment transitions in unterminated quotes share split-splice and mark-end state.
static bool scan_preproc_quote_step(TSLexer *lexer, PreprocQuoteState *state, int32_t c) {
    if (state->line) {
        if (!state->after_comment) lexer->mark_end(lexer);
    } else if (state->block) {
        if (state->star && c == '/') {
            if (!state->literal) return false;
            state->block = false;
            state->after_comment = !state->split_comment;
            if (state->split_comment) lexer->mark_end(lexer);
            state->split_comment = false;
            state->star = false;
        } else {
            state->star = c == '*';
        }
    } else if (state->slash && c == '/') {
        state->line = state->directive_text;
        state->slash = false;
        if (!state->after_comment) lexer->mark_end(lexer);
    } else if (state->slash && c == '*') {
        state->block = true;
        state->slash = false;
    } else {
        if (state->slash || (c != '/' && !pragma_space(c))) state->after_comment = false;
        state->slash = c == '/';
        if (!state->slash) state->split_comment = false;
        if (!state->slash && !state->after_comment) lexer->mark_end(lexer);
    }
    return true;
}

// code-gauge-allow functionCognitiveComplexity -- Standard, delimited, and named universal character names share hex/name validation.
// code-gauge-allow functionNcss -- Standard, delimited, and named universal character names share hex/name validation.
static bool scan_preproc_ucn(TSLexer *lexer, bool *in_number) {
    while (!scan_preproc_splices(lexer)) *in_number = false;
    int32_t prefix = lexer->lookahead;
    if (prefix != 'u' && prefix != 'U' && prefix != 'N') return false;
    lexer->advance(lexer, false);
    if (!scan_preproc_splices(lexer)) return false;
    bool braced = prefix != 'U' && lexer->lookahead == '{';
    if (prefix == 'N' && !braced) return false;
    if (braced) lexer->advance(lexer, false);
    unsigned remaining = prefix == 'U' ? 8 : 4;
    bool content = false;
    for (;;) {
        if (!scan_preproc_splices(lexer)) return false;
        if (braced && lexer->lookahead == '}') {
            lexer->advance(lexer, false);
            return content;
        }
        int32_t c = lexer->lookahead;
        bool accepted = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
        if (prefix == 'N') accepted = (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == ' ' || c == '-';
        if (!accepted) return false;
        lexer->advance(lexer, false);
        content = true;
        if (!braced && --remaining == 0) return true;
    }
}

static bool preproc_word(int32_t c, bool continuation) {
    if (c == '\\') return false;
    if (continuation && c >= 0x80 && !pragma_space(c)) return true;
    return preproc_identifier_character(c, continuation);
}

static bool preproc_identifier_character(int32_t c, bool continuation) {
    if (c >= 0 && c < 0x80) {
        const uint64_t *ascii = continuation ? preproc_identifier_continue_ascii : preproc_identifier_start_ascii;
        return (ascii[c / 64] >> (c % 64)) & 1;
    }
    return continuation
        ? set_contains(preproc_identifier_continue, sizeof(preproc_identifier_continue) / sizeof(TSCharacterRange), c)
        : set_contains(preproc_identifier_start, sizeof(preproc_identifier_start) / sizeof(TSCharacterRange), c);
}

static bool scan_pragma_word(TSLexer *lexer) {
    const char *word = pragma_word;
    for (; *word; word++) {
        if (lexer->lookahead != *word) return false;
        lexer->advance(lexer, false);
    }
    return true;
}

static bool scan_preproc_newline(TSLexer *lexer, bool skip) {
    bool carriage_return = lexer->lookahead == '\r';
    if (carriage_return) lexer->advance(lexer, skip);
    if (lexer->lookahead == '\n') {
        lexer->advance(lexer, skip);
        if (!carriage_return && lexer->lookahead == '\r') lexer->advance(lexer, skip);
        return true;
    }
    return carriage_return;
}

static bool scan_preproc_splices(TSLexer *lexer) {
    while (lexer->lookahead == '\\') {
        lexer->advance(lexer, false);
        if (!scan_preproc_newline(lexer, false)) return false;
    }
    return true;
}

static inline bool pragma_space(int32_t c) {
    return c < 0x85
        ? c == ' ' || (uint32_t)(c - '\t') <= '\r' - '\t'
        : c == 0x85 || c == 0xA0 || c == 0x1680 || (uint32_t)(c - 0x2000) <= 0x200A - 0x2000 ||
          c == 0x2028 || c == 0x2029 || c == 0x202F || c == 0x205F || c == 0x3000;
}

static bool scan_function_macro_name(TSLexer *lexer, bool allow_pragma, TSSymbol pragma_symbol) {
    char word[sizeof(pragma_word)] = {0};
    MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true};
    if (!scan_macro_name(lexer, &name)) return false;
    bool adjacent = lexer->lookahead == '(';
    if (allow_pragma && name.ascii && strcmp(word, pragma_word) == 0 && scan_pragma_suffix(lexer)) {
        lexer->result_symbol = pragma_symbol;
        return true;
    }
    return adjacent;
}

static void skip_leading_spaces_and_splices(TSLexer *lexer, bool (*is_space)(int32_t), bool *newline, bool *consumed_backslash) {
    *consumed_backslash = false;
    if (lexer->lookahead == ' ') {
        lexer->advance(lexer, true);
        if (!is_space(lexer->lookahead) && lexer->lookahead != '\\') return;
    } else if (!is_space(lexer->lookahead) && lexer->lookahead != '\\') {
        return;
    }
    bool marked = false;
    for (;;) {
        marked |= skip_leading_space_run(lexer, is_space, newline);
        if (lexer->lookahead != '\\') break;
        TSLexerCheckpoint slash;
        lexer->get_checkpoint(lexer, &slash);
        lexer->advance(lexer, false);
        if (!scan_preproc_newline(lexer, false)) {
            lexer->mark_start(lexer, &slash);
            lexer->mark_end_at(lexer, &slash);
            *consumed_backslash = true;
            return;
        }
        marked = true;
    }
    if (!marked) return;
    TSLexerCheckpoint after;
    lexer->get_checkpoint(lexer, &after);
    lexer->mark_start(lexer, &after);
}

static bool skip_leading_space_run(TSLexer *lexer, bool (*is_space)(int32_t), bool *newline) {
    bool used_units = false;
    for (;;) {
        if (skip_ascii_space_units(lexer, newline)) {
            used_units = true;
            continue;
        }
        if (!is_space(lexer->lookahead)) return used_units;
        if (newline) *newline |= lexer->lookahead == '\n' || lexer->lookahead == '\r';
        lexer->advance(lexer, true);
    }
}

static uint32_t skip_ascii_space_units(TSLexer *lexer, bool *newline) {
    uint32_t count, size;
    const char *units = lexer->peek_units(lexer, &count, &size);
    if (!count) return 0;
    uint32_t i = 0, line_feeds = 0, tail = 0;
    while (i < count) {
        uint16_t c = (uint8_t)units[i * size];
        if (size == 2) memcpy(&c, units + i * size, sizeof c);
        if (c == '\n') {
            line_feeds++;
            tail = 0;
        } else if (c == ' ' || c == '\t' || c == '\f' || c == '\v') {
            tail++;
        } else {
            break;
        }
        i++;
    }
    if (!i) return 0;
    if (newline && line_feeds) *newline = true;
    lexer->advance_units(lexer, i, line_feeds, tail);
    return i;
}

static int32_t peek_after_ascii_spaces(const TSLexer *lexer) {
    int32_t look = lexer->lookahead;
    if (look > 0 && look < 0x80 && look != ' ' && (uint32_t)(look - '\t') >= 5 && look != '\\') return look;
    uint32_t count, size;
    const char *units = lexer->peek_units(lexer, &count, &size);
    if (!count) return -1;
    uint32_t i = (look == ' ' || (uint32_t)(look - '\t') < 5) ? 1 : 0;
    for (; i < count; i++) {
        uint16_t c = (uint8_t)units[i * size];
        if (size == 2) memcpy(&c, units + 2 * (size_t)i, sizeof c);
        if (c != ' ' && (uint32_t)(c - '\t') >= 5) return c < 0x80 && c != '\\' ? (int32_t)c : -1;
    }
    return -1;
}

static bool scan_macro_name(TSLexer *lexer, MacroName *name) {
    return scan_macro_name_from(lexer, name, false);
}

static bool scan_macro_name_from(TSLexer *lexer, MacroName *name, bool consumed_backslash) {
    if (consumed_backslash) {
        name->ascii = false;
        if (!scan_macro_name_ucn(lexer)) return false;
        if (!name->lookahead_only) lexer->mark_end(lexer);
        return scan_macro_name_tail(lexer, name, true);
    }
    scan_macro_name_start(lexer, name);
    if ((uint32_t)lexer->lookahead >= 0x80u || lexer->lookahead == '\\') {
        return scan_macro_name_tail(lexer, name, false);
    }
    if (!scan_macro_name_ascii(lexer, name)) return false;
    if ((uint32_t)lexer->lookahead < 0x80u && lexer->lookahead != '\\') return true;
    return scan_macro_name_tail(lexer, name, true);
}

static bool scan_macro_name_tail(TSLexer *lexer, MacroName *name, bool has_name) {
    for (;;) {
        if (!has_name) scan_macro_name_start(lexer, name);
        MacroNameCharacter character = scan_macro_name_character(lexer, has_name, name);
        if (character == MACRO_NAME_INVALID) return false;
        if (character == MACRO_NAME_END) return has_name;
        if (character == MACRO_NAME_SPLICE) {
            if (!has_name) continue;
            return scan_preproc_splices(lexer);
        }
        has_name = true;
        if (!name->lookahead_only) lexer->mark_end(lexer);
    }
}

static bool scan_macro_name_ascii(TSLexer *lexer, MacroName *name) {
    int32_t c = lexer->lookahead;
    if (!((preproc_identifier_start_ascii[c / 64] >> (c % 64)) & 1)) return false;
    do {
        if (name->ascii) {
            if (name->length < name->capacity) name->word[name->length++] = (char)c;
            else name->ascii = false;
        }
        lexer->advance(lexer, false);
        c = lexer->lookahead;
    } while ((uint32_t)c < 0x80u && c != '\\' && ((preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1));
    if (!name->lookahead_only) lexer->mark_end(lexer);
    return true;
}

static void scan_macro_name_start(TSLexer *lexer, const MacroName *name) {
    while (pragma_space(lexer->lookahead)) lexer->advance(lexer, !name->preserve_end);
    if (name->lookahead_only && !name->preserve_end) lexer->mark_end(lexer);
}

static MacroNameCharacter scan_macro_name_character(TSLexer *lexer, bool continuation, MacroName *name) {
    int32_t c = lexer->lookahead;
    if (c == '\\') {
        lexer->advance(lexer, false);
        if (scan_preproc_newline(lexer, !continuation && !name->preserve_end)) return MACRO_NAME_SPLICE;
        name->ascii = false;
        return scan_macro_name_ucn(lexer) ? MACRO_NAME_CHARACTER : MACRO_NAME_INVALID;
    }
    if (!preproc_identifier_character(c, continuation)) return MACRO_NAME_END;
    if (name->ascii) {
        if (c < 128 && name->length < name->capacity) name->word[name->length++] = (char)c;
        else name->ascii = false;
    }
    lexer->advance(lexer, false);
    return MACRO_NAME_CHARACTER;
}

static bool scan_macro_name_ucn(TSLexer *lexer) {
    unsigned digits = lexer->lookahead == 'u' ? 4 : lexer->lookahead == 'U' ? 8 : 0;
    if (!digits) return false;
    lexer->advance(lexer, false);
    for (unsigned i = 0; i < digits; i++) {
        int32_t c = lexer->lookahead;
        if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) return false;
        lexer->advance(lexer, false);
    }
    return true;
}

#endif