willbooster-tree-sitter-css 3.3.0

CSS 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
#include "tree_sitter/parser.h"
#include "tree_sitter/array.h"

#include <wctype.h>
#include <string.h>

enum TokenType {
    DESCENDANT_OP,
    ERROR_RECOVERY,
    CLASS_NAME_ESCAPE_WITH_SUFFIX,
    AT_RULE_SLASH,
    INCOMPLETE_DECLARATION_END,
    LAYER_NAME_IDENTIFIER,
    LAYER_NAME_PREFIX,
    IMPORT_SUPPORTS_START,
    IMPORT_LAYER_START,
    INVALID_IMPORT_SUPPORTS,
    INVALID_IMPORT_LAYER,
};

typedef Array(char) SupportsDelimiters;

static bool scan_import_condition_start(TSLexer *lexer, const bool *valid_symbols);
static bool scan_import_layer_body_end(TSLexer *lexer);
static void scan_invalid_import_end(TSLexer *lexer);
static bool scan_invalid_import_delimiter(TSLexer *lexer);
static bool scan_import_component_list(TSLexer *lexer, int32_t terminator);
static bool scan_import_components(TSLexer *lexer, SupportsDelimiters *delimiters, int32_t terminator);
static bool is_import_components_end(int32_t character, int32_t terminator);
static bool scan_import_component(TSLexer *lexer, SupportsDelimiters *delimiters, int32_t terminator, bool *component);
static bool scan_supports_body_end(TSLexer *lexer, bool supports_allowed);
static bool scan_layer_tail(TSLexer *lexer);
static bool is_import_tail_start(int32_t character);
static bool scan_supports_word(TSLexer *lexer);
static bool scan_import_word(TSLexer *lexer, char *name, unsigned capacity, unsigned *length);
static bool scan_supports_url(TSLexer *lexer);
static bool scan_supports_string(TSLexer *lexer);
static bool scan_supports_slash(TSLexer *lexer, bool *component, bool url_allowed);
static bool scan_double_slash(TSLexer *lexer, bool url_allowed);
static bool is_at_rule_url_character(int32_t character);
static bool is_regex_whitespace(int32_t character);
static bool scan_layer_name_identifier(TSLexer *lexer, const bool *valid_symbols);
static bool scan_layer_name_token(TSLexer *lexer);
static bool scan_layer_name_tail(TSLexer *lexer, char *prefix, unsigned length);
static bool scan_name_character(TSLexer *lexer, int32_t *character);
static bool scan_decoded_escape(TSLexer *lexer, int32_t *decoded);
static char keyword_character(int32_t character);
static void scan_escape_terminator(TSLexer *lexer);
static bool scan_component_gap(TSLexer *lexer, bool *whitespace);
static bool is_layer_name_start(int32_t character);
static bool is_css_whitespace(int32_t character);
static bool scan_incomplete_declaration_end(TSLexer *lexer, bool allow_descendant);
static bool scan_property_colon(TSLexer *lexer);
static bool scan_separator_comment(TSLexer *lexer);
static bool scan_at_rule_slash(TSLexer *lexer);
static bool scan_class_name_escape_with_suffix(TSLexer *lexer);
static bool scan_escape(TSLexer *lexer);
static bool scan_descendant_operator(TSLexer *lexer);
static bool is_name_character(int32_t character);
static bool is_name_start(int32_t character);
static bool is_hex_digit(int32_t character);
static bool is_digit(int32_t character);
static void advance(TSLexer *lexer);
static void skip(TSLexer *lexer);

void *tree_sitter_css_external_scanner_create() { return NULL; }

void tree_sitter_css_external_scanner_destroy(void *payload) {}

unsigned tree_sitter_css_external_scanner_serialize(void *payload, char *buffer) { return 0; }

void tree_sitter_css_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {}

bool tree_sitter_css_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
    if (valid_symbols[ERROR_RECOVERY]) {
        return false;
    }
    if (valid_symbols[IMPORT_SUPPORTS_START] || valid_symbols[IMPORT_LAYER_START]) {
        return scan_import_condition_start(lexer, valid_symbols);
    }
    if (valid_symbols[LAYER_NAME_IDENTIFIER] || valid_symbols[LAYER_NAME_PREFIX]) {
        return scan_layer_name_identifier(lexer, valid_symbols);
    }
    if (valid_symbols[INCOMPLETE_DECLARATION_END] && (iswspace(lexer->lookahead) || lexer->lookahead == '/')) {
        return scan_incomplete_declaration_end(lexer, valid_symbols[DESCENDANT_OP]);
    }
    if (valid_symbols[AT_RULE_SLASH]) {
        return scan_at_rule_slash(lexer);
    }
    if (valid_symbols[CLASS_NAME_ESCAPE_WITH_SUFFIX] && lexer->lookahead == '\\') {
        return scan_class_name_escape_with_suffix(lexer);
    }
    if (iswspace(lexer->lookahead) && valid_symbols[DESCENDANT_OP]) {
        return scan_descendant_operator(lexer);
    }
    return false;
}

static bool scan_import_condition_start(TSLexer *lexer, const bool *valid_symbols) {
    while (is_css_whitespace(lexer->lookahead)) skip(lexer);
    char name[9] = {0};
    unsigned length = 0;
    if (!scan_import_word(lexer, name, sizeof(name), &length)) return false;
    bool layer = length == 5 && valid_symbols[IMPORT_LAYER_START] && !strcmp(name, "layer");
    bool supports = length == 8 && valid_symbols[IMPORT_SUPPORTS_START] && !strcmp(name, "supports");
    if ((!layer && !supports) || lexer->lookahead != '(') return false;
    advance(lexer);
    lexer->mark_end(lexer);
    lexer->result_symbol = layer ? IMPORT_LAYER_START : IMPORT_SUPPORTS_START;
    if (layer ? scan_import_layer_body_end(lexer) : scan_import_component_list(lexer, ')')) return true;
    scan_invalid_import_end(lexer);
    lexer->result_symbol = layer ? INVALID_IMPORT_LAYER : INVALID_IMPORT_SUPPORTS;
    return true;
}

static void scan_invalid_import_end(TSLexer *lexer) {
    if (lexer->lookahead == '\'' || lexer->lookahead == '"') scan_supports_string(lexer);
    lexer->mark_end(lexer);
    for (;;) {
        bool whitespace = false;
        if (!scan_component_gap(lexer, &whitespace)) {
            lexer->mark_end(lexer);
            if (lexer->eof(lexer)) return;
            continue;
        }
        lexer->mark_end(lexer);
        if (!scan_invalid_import_delimiter(lexer)) return;
        lexer->mark_end(lexer);
    }
}

static bool scan_invalid_import_delimiter(TSLexer *lexer) {
    if (lexer->lookahead == '[') {
        advance(lexer);
        return lexer->lookahead == '[' || lexer->lookahead == ']' ||
            lexer->lookahead == ')' || lexer->lookahead == ';';
    }
    if (lexer->lookahead != ')' && lexer->lookahead != ';' && lexer->lookahead != ']') return false;
    advance(lexer);
    return true;
}

static bool scan_import_layer_body_end(TSLexer *lexer) {
    for (;;) {
        bool whitespace = false;
        if (!scan_component_gap(lexer, &whitespace)) return false;
        if (lexer->eof(lexer)) return false;
        switch (lexer->lookahead) {
            case ')': return scan_supports_body_end(lexer, true);
            case '\'': case '"': return false;
            case ';': case '{': case '}': return true;
            default: {
                int32_t character;
                if (!scan_name_character(lexer, &character)) return false;
            }
        }
    }
}

static bool scan_import_component_list(TSLexer *lexer, int32_t terminator) {
    SupportsDelimiters delimiters = array_new();
    bool valid = scan_import_components(lexer, &delimiters, terminator);
    array_delete(&delimiters);
    return valid;
}

static bool scan_import_components(TSLexer *lexer, SupportsDelimiters *delimiters, int32_t terminator) {
    bool component = false;
    while (!lexer->eof(lexer)) {
        int32_t character = lexer->lookahead;
        if (!delimiters->size) {
            if (is_import_components_end(character, terminator)) {
                return terminator == ';' || (component && scan_supports_body_end(lexer, false));
            }
            if (character == ')' || character == ']' || character == '}' || character == ';') return false;
            if (terminator == ';' && (character == '\'' || character == '"')) return false;
        }
        if (!scan_import_component(lexer, delimiters, terminator, &component)) return false;
    }
    return terminator == ';' && !delimiters->size;
}

static bool is_import_components_end(int32_t character, int32_t terminator) {
    // A rule, at-rule or block end after an import missing its `;` ends the tail, as it does without a condition.
    bool statement_boundary = character == '{' || character == '@' || character == '}';
    return character == terminator || (terminator == ';' && statement_boundary);
}

static bool scan_import_component(TSLexer *lexer, SupportsDelimiters *delimiters, int32_t terminator, bool *component) {
    int32_t character = lexer->lookahead;
    if (is_css_whitespace(character)) { advance(lexer); return true; }
    // Outside parentheses the media tail has no at_rule_url, so `//` there always starts a js_comment.
    if (character == '/') return scan_supports_slash(lexer, component, delimiters->size || terminator != ';');
    *component = true;
    if (character == '\'' || character == '"') return scan_supports_string(lexer);
    if (is_name_character(character) || character >= 0x80 || character == '\\' || character == '#' || character == '@') {
        return scan_supports_word(lexer);
    }
    switch (character) {
        case ')': case ']': case '}':
            if (!delimiters->size || array_pop(delimiters) != character) return false;
            break;
        case '(': array_push(delimiters, ')'); break;
        case '[': array_push(delimiters, ']'); break;
        case '{': array_push(delimiters, '}'); break;
    }
    advance(lexer);
    return true;
}

static bool scan_supports_body_end(TSLexer *lexer, bool supports_allowed) {
    advance(lexer);
    bool whitespace = false;
    if (!scan_component_gap(lexer, &whitespace)) return false;
    if (!lexer->eof(lexer) && !is_import_tail_start(lexer->lookahead)) return false;
    return supports_allowed ? scan_layer_tail(lexer) : scan_import_component_list(lexer, ';');
}

// Besides media queries, a tail may start the rule, at-rule or block end that follows an import missing its `;`. `[`
// and quotes are excluded so that malformed attribute selectors and quoted conditions recover as invalid fragments.
static bool is_import_tail_start(int32_t character) {
    switch (character) {
        case '-': case '\\': case '(': case ';': case '.': case '#': case ':': case '*': case '@': case '}':
            return true;
        default:
            return is_name_start(character);
    }
}

// Scans a supports() after a layer with the body rules of a direct supports() lookahead, so a malformed body recovers
// at the same position with or without a preceding layer.
static bool scan_layer_tail(TSLexer *lexer) {
    char name[9] = {0};
    unsigned length = 0;
    if (!scan_import_word(lexer, name, sizeof(name), &length)) return false;
    if (lexer->lookahead == '(' && length == 8 && !strcmp(name, "supports")) {
        advance(lexer);
        return scan_import_component_list(lexer, ')');
    }
    if (lexer->lookahead == '(' && length == 3 && !strcmp(name, "url") && !scan_supports_url(lexer)) return false;
    return scan_import_component_list(lexer, ';');
}

static bool scan_supports_word(TSLexer *lexer) {
    char name[4] = {0};
    unsigned length = 0;
    bool prefixed = lexer->lookahead == '#' || lexer->lookahead == '@';
    if (prefixed) advance(lexer);
    if (!scan_import_word(lexer, name, sizeof(name), &length)) return false;
    if (!prefixed && length == 3 && !strcmp(name, "url") && lexer->lookahead == '(') {
        return scan_supports_url(lexer);
    }
    return true;
}

static bool scan_import_word(TSLexer *lexer, char *name, unsigned capacity, unsigned *length) {
    while (is_name_character(lexer->lookahead) || lexer->lookahead >= 0x80 || lexer->lookahead == '\\') {
        int32_t character;
        if (!scan_name_character(lexer, &character)) return false;
        if (*length < capacity - 1) name[*length] = keyword_character(character);
        (*length)++;
    }
    return true;
}

static bool scan_supports_url(TSLexer *lexer) {
    advance(lexer);
    while (!lexer->eof(lexer)) {
        if (lexer->lookahead == '\\') {
            if (!scan_escape(lexer)) return false;
        } else if (lexer->lookahead == '\'' || lexer->lookahead == '"') {
            if (!scan_supports_string(lexer)) return false;
        } else if (lexer->lookahead == ')') {
            advance(lexer);
            return true;
        } else {
            advance(lexer);
        }
    }
    return false;
}

static bool scan_supports_string(TSLexer *lexer) {
    int32_t quote = lexer->lookahead;
    advance(lexer);
    while (!lexer->eof(lexer)) {
        int32_t character = lexer->lookahead;
        if (character == '\n' || character == '\r' || character == '\f') return false;
        advance(lexer);
        if (character == quote) return true;
        if (character == '\\' && !lexer->eof(lexer)) {
            bool carriage_return = lexer->lookahead == '\r';
            advance(lexer);
            if (carriage_return && lexer->lookahead == '\n') advance(lexer);
        }
    }
    return false;
}

static bool scan_supports_slash(TSLexer *lexer, bool *component, bool url_allowed) {
    advance(lexer);
    if (lexer->lookahead == '/') {
        advance(lexer);
        if (scan_double_slash(lexer, url_allowed)) *component = true;
        return true;
    }
    if (lexer->lookahead != '*') { *component = true; return true; }
    advance(lexer);
    while (!lexer->eof(lexer)) {
        bool star = lexer->lookahead == '*';
        advance(lexer);
        if (star && lexer->lookahead == '/') {
            advance(lexer);
            return true;
        }
    }
    return false;
}

static bool scan_double_slash(TSLexer *lexer, bool url_allowed) {
    // Mirrors grammar.js: the at_rule_url token outranks the lower-precedence js_comment extra.
    bool url = url_allowed && !lexer->eof(lexer) && is_at_rule_url_character(lexer->lookahead);
    while (!lexer->eof(lexer)) {
        bool line_end = lexer->lookahead == '\n' || lexer->lookahead == '\r';
        bool inside = url ? is_at_rule_url_character(lexer->lookahead) : !line_end;
        if (!inside) break;
        advance(lexer);
    }
    return url;
}

static bool is_at_rule_url_character(int32_t character) {
    switch (character) {
        case '(': case ')': case '{': case '}': case '[': case ']': case ';': case ',': case '"': case '\'': case '\\':
            return false;
        default:
            return !is_regex_whitespace(character);
    }
}

// The generated lexer matches the `\s` that grammar.js excludes from at_rule_url as ASCII whitespace only, which
// includes the vertical tab that CSS does not treat as whitespace.
static bool is_regex_whitespace(int32_t character) {
    return (character >= '\t' && character <= '\r') || character == ' ';
}

static bool scan_layer_name_identifier(TSLexer *lexer, const bool *valid_symbols) {
    while (is_css_whitespace(lexer->lookahead)) skip(lexer);
    if (!scan_layer_name_token(lexer)) {
        return false;
    }
    lexer->mark_end(lexer);
    lexer->result_symbol = LAYER_NAME_IDENTIFIER;
    bool whitespace = false;
    if (scan_component_gap(lexer, &whitespace) && lexer->lookahead == '.' && !whitespace) {
        advance(lexer);
        if (scan_component_gap(lexer, &whitespace) && !whitespace && scan_layer_name_token(lexer)) {
            lexer->result_symbol = LAYER_NAME_PREFIX;
        }
    }
    return valid_symbols[lexer->result_symbol];
}

static bool scan_layer_name_token(TSLexer *lexer) {
    bool hyphen = lexer->lookahead == '-';
    if (lexer->lookahead == '-') {
        advance(lexer);
        if (lexer->lookahead == '-') {
            advance(lexer);
            return scan_layer_name_tail(lexer, "--", 2);
        }
    }
    if (!(lexer->lookahead == '\\' || is_layer_name_start(lexer->lookahead))) {
        return false;
    }
    return scan_layer_name_tail(lexer, hyphen ? "-" : "", hyphen ? 1 : 0);
}

static bool scan_layer_name_tail(TSLexer *lexer, char *prefix, unsigned length) {
    char name[13] = {0};
    memcpy(name, prefix, length);
    while (is_layer_name_start(lexer->lookahead) || is_digit(lexer->lookahead) || lexer->lookahead == '-' || lexer->lookahead == '\\') {
        int32_t character;
        if (!scan_name_character(lexer, &character)) return false;
        if (length < sizeof(name) - 1) {
            name[length] = keyword_character(character);
        }
        length++;
    }
    if (length >= sizeof(name)) return true;
    return strcmp(name, "initial") && strcmp(name, "inherit") && strcmp(name, "unset") &&
        strcmp(name, "revert") && strcmp(name, "revert-layer") && strcmp(name, "revert-rule");
}

static bool scan_name_character(TSLexer *lexer, int32_t *character) {
    *character = lexer->lookahead;
    if (*character == '\\') return scan_decoded_escape(lexer, character);
    advance(lexer);
    return true;
}

static char keyword_character(int32_t character) {
    if (character >= 'A' && character <= 'Z') return (char)(character + 'a' - 'A');
    return character < 0x80 ? (char)character : '?';
}

static bool is_layer_name_start(int32_t character) {
    static const int32_t ranges[][2] = {
        {0xB7, 0xB7}, {0xC0, 0xD6}, {0xD8, 0xF6}, {0xF8, 0x37D}, {0x37F, 0x1FFF},
        {0x200C, 0x200D}, {0x203F, 0x2040}, {0x2070, 0x218F}, {0x2C00, 0x2FEF},
        {0x3001, 0xD7FF}, {0xF900, 0xFDCF}, {0xFDF0, 0xFFFD}, {0x10000, 0x10FFFF},
    };
    if ((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || character == '_') {
        return true;
    }
    for (unsigned i = 0; i < sizeof(ranges) / sizeof(ranges[0]); i++) {
        if (character >= ranges[i][0] && character <= ranges[i][1]) {
            return true;
        }
    }
    return false;
}

static bool scan_component_gap(TSLexer *lexer, bool *whitespace) {
    for (;;) {
        if (is_css_whitespace(lexer->lookahead)) {
            *whitespace = true;
            advance(lexer);
        } else if (lexer->lookahead == '/') {
            if (!scan_separator_comment(lexer)) return false;
        } else {
            return true;
        }
    }
}

static bool is_css_whitespace(int32_t character) {
    return character == ' ' || character == '\t' || character == '\n' || character == '\r' || character == '\f';
}

static bool scan_incomplete_declaration_end(TSLexer *lexer, bool allow_descendant) {
    bool newline = false;
    bool whitespace = false;
    lexer->mark_end(lexer);
    for (;;) {
        if (iswspace(lexer->lookahead)) {
            whitespace = true;
            newline |= lexer->lookahead == '\n' || lexer->lookahead == '\r' || lexer->lookahead == '\f';
            advance(lexer);
        } else if (lexer->lookahead == '/') {
            if (!scan_separator_comment(lexer)) {
                return false;
            }
        } else {
            break;
        }
    }
    bool descendant = allow_descendant && whitespace && (lexer->lookahead == '#' || lexer->lookahead == '.' ||
        lexer->lookahead == '[' || lexer->lookahead == '-' || lexer->lookahead == '*' ||
        lexer->lookahead == '&' || lexer->lookahead == ':' || iswalnum(lexer->lookahead));
    lexer->result_symbol = DESCENDANT_OP;
    if (!newline) {
        return descendant;
    }
    bool star_property = lexer->lookahead == '*';
    if (star_property) {
        advance(lexer);
    }
    if (!(is_name_start(lexer->lookahead) || lexer->lookahead == '-' ||
        (!star_property && lexer->lookahead == '\\'))) {
        return descendant;
    }
    if (!scan_property_colon(lexer)) {
        return descendant;
    }
    lexer->result_symbol = INCOMPLETE_DECLARATION_END;
    return true;
}

static bool scan_property_colon(TSLexer *lexer) {
    while (is_name_character(lexer->lookahead) || lexer->lookahead == '\\') {
        if (lexer->lookahead != '\\') {
            advance(lexer);
            continue;
        }
        if (!scan_escape(lexer)) {
            return false;
        }
    }
    while (iswspace(lexer->lookahead) || lexer->lookahead == '/') {
        if (lexer->lookahead != '/') {
            advance(lexer);
            continue;
        }
        if (!scan_separator_comment(lexer)) {
            return false;
        }
    }
    return lexer->lookahead == ':';
}

static bool scan_separator_comment(TSLexer *lexer) {
    advance(lexer);
    if (lexer->lookahead == '/') {
        while (!lexer->eof(lexer) && lexer->lookahead != '\n' && lexer->lookahead != '\r') {
            advance(lexer);
        }
        return true;
    }
    if (lexer->lookahead != '*') {
        return false;
    }
    advance(lexer);
    while (!lexer->eof(lexer)) {
        bool star = lexer->lookahead == '*';
        advance(lexer);
        if (star && lexer->lookahead == '/') {
            advance(lexer);
            return true;
        }
    }
    return false;
}

static bool scan_at_rule_slash(TSLexer *lexer) {
    while (iswspace(lexer->lookahead)) {
        skip(lexer);
    }
    if (lexer->lookahead != '/') {
        return false;
    }
    advance(lexer);
    lexer->mark_end(lexer);
    if (lexer->lookahead == '*' || lexer->lookahead == '/') {
        return false;
    }
    lexer->result_symbol = AT_RULE_SLASH;
    return true;
}

static bool scan_class_name_escape_with_suffix(TSLexer *lexer) {
    if (!scan_escape(lexer)) {
        return false;
    }
    if (lexer->lookahead == '-') {
        advance(lexer);
        if (is_name_start(lexer->lookahead) || lexer->lookahead == '-') {
            return false;
        }
    } else if (!is_digit(lexer->lookahead)) {
        return false;
    }
    while (is_name_character(lexer->lookahead)) {
        advance(lexer);
    }
    lexer->mark_end(lexer);
    lexer->result_symbol = CLASS_NAME_ESCAPE_WITH_SUFFIX;
    return true;
}

static bool scan_escape(TSLexer *lexer) {
    return scan_decoded_escape(lexer, NULL);
}

static bool scan_decoded_escape(TSLexer *lexer, int32_t *decoded) {
    advance(lexer);
    if (lexer->eof(lexer) || lexer->lookahead == '\n' || lexer->lookahead == '\r' || lexer->lookahead == '\f') {
        return false;
    }
    if (!is_hex_digit(lexer->lookahead)) {
        if (decoded) *decoded = lexer->lookahead;
        advance(lexer);
        return true;
    }
    unsigned digits = 0;
    int32_t value = 0;
    while (digits < 6 && is_hex_digit(lexer->lookahead)) {
        int32_t digit = lexer->lookahead;
        value = value * 16 + (digit <= '9' ? digit - '0' : (digit | 32) - 'a' + 10);
        advance(lexer);
        digits++;
    }
    if (decoded) *decoded = value == 0 ? 0xFFFD : value;
    scan_escape_terminator(lexer);
    return true;
}

static void scan_escape_terminator(TSLexer *lexer) {
    if (lexer->lookahead == '\r') {
        advance(lexer);
        if (lexer->lookahead == '\n') advance(lexer);
    } else if (is_css_whitespace(lexer->lookahead)) {
        advance(lexer);
    }
}

static bool scan_descendant_operator(TSLexer *lexer) {
    lexer->result_symbol = DESCENDANT_OP;
    skip(lexer);
    while (iswspace(lexer->lookahead)) {
        skip(lexer);
    }
    lexer->mark_end(lexer);
    if (lexer->lookahead == '#' || lexer->lookahead == '.' || lexer->lookahead == '[' || lexer->lookahead == '-' ||
        lexer->lookahead == '*' || lexer->lookahead == '&' || iswalnum(lexer->lookahead)) {
        return true;
    }
    return lexer->lookahead == ':';
}

static bool is_name_character(int32_t character) {
    return is_name_start(character) || is_digit(character) || character == '-';
}

static bool is_name_start(int32_t character) {
    return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || character == '_' ||
           (character >= 0xA0 && character <= 0xFF);
}

static bool is_hex_digit(int32_t character) {
    return is_digit(character) || (character >= 'a' && character <= 'f') || (character >= 'A' && character <= 'F');
}

static bool is_digit(int32_t character) { return character >= '0' && character <= '9'; }

static void advance(TSLexer *lexer) { lexer->advance(lexer, false); }

static void skip(TSLexer *lexer) { lexer->advance(lexer, true); }