willbooster-tree-sitter-cpp 23.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
#ifndef TREE_SITTER_C_PRAGMA_H_
#define TREE_SITTER_C_PRAGMA_H_

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

static const char pragma_word[] = "_Pragma";

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

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 void scan_macro_name_start(TSLexer *lexer, bool lookahead_only);
static MacroNameCharacter scan_macro_name_character(TSLexer *lexer, bool continuation, MacroName *name);
static bool scan_macro_name_ucn(TSLexer *lexer);

static bool scan_pragma_spacing(TSLexer *lexer);
static bool scan_pragma_suffix(TSLexer *lexer);
static 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 scan_preproc_quote(TSLexer *lexer, int32_t quote, bool escaped, bool directive_text);

static bool scan_pragma(TSLexer *lexer) {
    while (pragma_space(lexer->lookahead)) {
        lexer->advance(lexer, true);
    }
    if (!scan_pragma_word(lexer)) return false;
    return scan_pragma_suffix(lexer);
}

static bool scan_pragma_suffix(TSLexer *lexer) {
    if (!scan_pragma_spacing(lexer) || lexer->lookahead != '(') return false;
    lexer->advance(lexer, false);
    if (!scan_pragma_spacing(lexer)) return false;
    if (lexer->lookahead == 'L' || lexer->lookahead == 'u' || lexer->lookahead == 'U') {
        bool utf8 = lexer->lookahead == 'u';
        lexer->advance(lexer, false);
        if (utf8 && lexer->lookahead == '8') lexer->advance(lexer, false);
    }
    if (lexer->lookahead != '"') return false;
    lexer->advance(lexer, false);
    while (lexer->lookahead != '"') {
        if (lexer->eof(lexer) || lexer->lookahead == '\n' || lexer->lookahead == '\r') return false;
        if (lexer->lookahead == '\\') {
            lexer->advance(lexer, false);
            if (lexer->eof(lexer)) return false;
            if (scan_preproc_newline(lexer, false)) continue;
        }
        lexer->advance(lexer, false);
    }
    lexer->advance(lexer, false);
    if (!scan_pragma_spacing(lexer) || lexer->lookahead != ')') return false;
    lexer->advance(lexer, false);
    lexer->mark_end(lexer);
    return true;
}

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 == '/') {
            lexer->advance(lexer, false);
            if (lexer->lookahead == '/') {
                lexer->advance(lexer, false);
                while (!lexer->eof(lexer) && lexer->lookahead != '\n') {
                    if (lexer->lookahead == '\\') {
                        do {
                            lexer->advance(lexer, false);
                        } while (lexer->lookahead == '\\');
                        if (lexer->lookahead == '\r') lexer->advance(lexer, false);
                        if (lexer->eof(lexer)) return false;
                    }
                    lexer->advance(lexer, false);
                }
            } else if (lexer->lookahead == '*') {
                lexer->advance(lexer, false);
                bool star = false;
                for (;;) {
                    if (lexer->eof(lexer)) return false;
                    if (star && lexer->lookahead == '/') {
                        lexer->advance(lexer, false);
                        break;
                    }
                    star = lexer->lookahead == '*';
                    lexer->advance(lexer, false);
                }
            } else {
                return false;
            }
        } else {
            return true;
        }
    }
}

static bool scan_preproc_arg(TSLexer *lexer, bool consumed_backslash, bool directive_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);
    if (consumed_backslash) lexer->mark_end(lexer);
    int32_t number_last = 0;
    while (!lexer->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 (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 (!lexer->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 (!lexer->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 (lexer->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;
            }
            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 == '"' || (!directive_text && lexer->lookahead == '\'')) {
            int32_t quote = lexer->lookahead;
            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;
            if (!scan_preproc_quote(lexer, quote, escaped, directive_text)) return true;
            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_quote(TSLexer *lexer, int32_t quote, bool escaped, bool directive_text) {
    lexer->mark_end(lexer);
    bool literal = true;
    bool slash = false;
    bool block = false;
    bool line = false;
    bool star = false;
    bool split_comment = false;
    bool after_comment = false;
    while (!lexer->eof(lexer) && (block || (lexer->lookahead != '\n' && lexer->lookahead != '\r'))) {
        int32_t c = lexer->lookahead;
        lexer->advance(lexer, false);
        if (c == '\n' || c == '\r') literal = false;
        if (literal && c == quote && !escaped) {
            lexer->mark_end(lexer);
            return true;
        }
        if (c == '\\' && scan_preproc_newline(lexer, false)) {
            if (slash || star) split_comment = true;
            if (!block && !slash && !after_comment) lexer->mark_end(lexer);
            continue;
        }
        escaped = c == '\\' && !escaped;
        if (line) {
            if (!after_comment) lexer->mark_end(lexer);
        } else if (block) {
            if (star && c == '/') {
                if (!literal) return false;
                block = false;
                after_comment = !split_comment;
                if (split_comment) lexer->mark_end(lexer);
                split_comment = false;
                star = false;
            } else {
                star = c == '*';
            }
        } else if (slash && c == '/') {
            line = directive_text;
            slash = false;
            if (!after_comment) lexer->mark_end(lexer);
        } else if (slash && c == '*') {
            block = true;
            slash = false;
        } else {
            if (slash || (c != '/' && !pragma_space(c))) after_comment = false;
            slash = c == '/';
            if (!slash) split_comment = false;
            if (!slash && !after_comment) lexer->mark_end(lexer);
        }
    }
    if (slash) lexer->mark_end(lexer);
    return false;
}

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 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 bool pragma_space(int32_t c) {
    return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v' ||
           c == 0x85 || c == 0xA0 || c == 0x1680 || (c >= 0x2000 && c <= 0x200A) ||
           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 bool scan_macro_name(TSLexer *lexer, MacroName *name) {
    bool has_name = false;
    for (;;) {
        if (!has_name) scan_macro_name_start(lexer, name->lookahead_only);
        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 void scan_macro_name_start(TSLexer *lexer, bool lookahead_only) {
    while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
    if (lookahead_only) 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)) return MACRO_NAME_SPLICE;
        name->ascii = false;
        return scan_macro_name_ucn(lexer) ? MACRO_NAME_CHARACTER : MACRO_NAME_INVALID;
    }
    bool accepted = 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);
    if (!accepted) 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