@willbooster/tree-sitter-c 42.0.0 → 44.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -17
- package/grammar.js +57 -21
- package/package.json +6 -3
- package/src/conditionalMacro.h +2 -7
- package/src/directiveBuffer.h +431 -0
- package/src/directiveFacts.h +363 -0
- package/src/directiveScan.h +44 -14
- package/src/directiveUnits.h +544 -0
- package/src/emptyDirectives.h +552 -633
- package/src/functionMacro.h +151 -16
- package/src/macroProof.h +1 -1
- package/src/pragma.h +129 -7
- package/src/scanner.c +252 -63
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +4 -1
- package/types/treeSitterDsl.d.ts +6 -0
package/src/functionMacro.h
CHANGED
|
@@ -116,18 +116,46 @@ static bool function_macro_first_word(TSLexer *lexer, const MacroName *name, con
|
|
|
116
116
|
return false;
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
+
typedef enum {
|
|
120
|
+
PEEK_EXTRAS_NONE,
|
|
121
|
+
PEEK_EXTRAS_DIRECTIVE,
|
|
122
|
+
PEEK_EXTRAS_DECLARATION,
|
|
123
|
+
PEEK_EXTRAS_UNKNOWN,
|
|
124
|
+
} PeekExtrasKind;
|
|
125
|
+
|
|
126
|
+
typedef struct {
|
|
127
|
+
const char *units;
|
|
128
|
+
uint32_t count, size;
|
|
129
|
+
} PeekUnitsView;
|
|
130
|
+
|
|
131
|
+
static bool scan_function_attribute_guard_from(TSLexer *lexer, bool prototype, bool require_literal, bool trailing, bool consumed_backslash, const MacroAttributeSymbols *symbols);
|
|
132
|
+
static PeekExtrasKind peek_extras_kind(const TSLexer *lexer, bool allow_directive, bool allow_object, bool allow_pragma);
|
|
133
|
+
static PeekExtrasKind peek_extras_first(uint16_t first, bool allow_directive, bool allow_object, bool allow_pragma);
|
|
134
|
+
static PeekExtrasKind peek_extras_word(PeekUnitsView view, uint32_t i, bool allow_object, bool allow_pragma);
|
|
135
|
+
static bool peek_object_declarator_units(PeekUnitsView view, uint32_t i);
|
|
136
|
+
static uint32_t peek_skip_ident(PeekUnitsView view, uint32_t i, bool *non_plain);
|
|
137
|
+
static inline bool peek_object_starter(uint16_t c);
|
|
138
|
+
static inline uint16_t peek_unit_at(const char *units, uint32_t size, uint32_t index);
|
|
139
|
+
static inline bool peek_ascii_space(uint16_t c);
|
|
140
|
+
static inline bool peek_ascii_ident(uint16_t c, bool continuation);
|
|
141
|
+
|
|
119
142
|
// code-gauge-allow functionCognitiveComplexity -- Attribute roles share first-word dispatch, token ranges, and group results.
|
|
120
143
|
// code-gauge-allow functionNcss -- Attribute roles share first-word dispatch, token ranges, and group results.
|
|
121
|
-
static bool
|
|
144
|
+
static bool scan_function_attribute_guard_from(TSLexer *lexer, bool prototype, bool require_literal, bool trailing, bool consumed_backslash, const MacroAttributeSymbols *symbols) {
|
|
122
145
|
const PragmaTrivia *trivia = &symbols->trivia;
|
|
123
146
|
bool braced_only = symbols->allow_braced_start && lexer->result_symbol == symbols->braced_start;
|
|
124
147
|
char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
|
|
125
148
|
MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true, .lookahead_only = !trailing};
|
|
126
|
-
if (!
|
|
149
|
+
if (!scan_macro_name_from(lexer, &name, consumed_backslash)) return false;
|
|
127
150
|
bool handled;
|
|
128
151
|
bool accepted = function_macro_first_word(lexer, &name, symbols, &handled);
|
|
129
152
|
if (handled) return accepted;
|
|
130
153
|
if (lexer->result_symbol == symbols->object_start) return false;
|
|
154
|
+
while (lexer->lookahead == ' ' || lexer->lookahead == '\t') lexer->advance(lexer, false);
|
|
155
|
+
int32_t next = lexer->lookahead;
|
|
156
|
+
if (next != '(' && (uint32_t)next < 0x80u && next != '\\' && next != '/' && next != '#' && !pragma_space(next)) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
131
159
|
if (name.ascii && function_macro_attribute_keyword(word, trailing)) return false;
|
|
132
160
|
if (!scan_pragma_spacing_with(lexer, trivia) || lexer->lookahead != '(') return false;
|
|
133
161
|
if (trailing) return function_macro_trailing_argument_prefix(lexer, trivia);
|
|
@@ -177,37 +205,50 @@ static MacroTypePrefix function_macro_macro_type_item(TSLexer *lexer, const Macr
|
|
|
177
205
|
|
|
178
206
|
static MacroTypeResult function_macro_type_transition(MacroTypeState *state, MacroTypePrefix kind);
|
|
179
207
|
static MacroTypeResult function_macro_type_group(TSLexer *lexer, MacroTypeState *state, const PragmaTrivia *trivia);
|
|
180
|
-
static MacroTypeResult function_macro_type_word(TSLexer *lexer, const MacroAttributeSymbols *symbols,
|
|
208
|
+
static MacroTypeResult function_macro_type_word(TSLexer *lexer, bool consumed_backslash, const MacroAttributeSymbols *symbols,
|
|
181
209
|
const char *const *reserved_words, unsigned reserved_count, MacroTypeState *state);
|
|
182
210
|
|
|
183
|
-
static bool function_macro_prove_braced_type(TSLexer *lexer, const MacroAttributeSymbols *symbols,
|
|
211
|
+
static bool function_macro_prove_braced_type(TSLexer *lexer, bool consumed_backslash, const MacroAttributeSymbols *symbols,
|
|
184
212
|
const char *const *reserved_words, unsigned reserved_count, MacroProofMemo *memo);
|
|
213
|
+
static unsigned function_macro_type_trivia(TSLexer *lexer, const MacroTypeState *state, const PragmaTrivia *trivia, MacroProofMemo *memo);
|
|
185
214
|
|
|
186
|
-
static bool
|
|
215
|
+
static inline bool scan_braced_macro_type_start_from(TSLexer *lexer, bool consumed_backslash, const MacroAttributeSymbols *symbols, const char *const *reserved_words, unsigned reserved_count) {
|
|
187
216
|
MacroProofMemo *memo = symbols->proof_memo;
|
|
188
217
|
if (!macro_proof_begin(memo, reserved_words, reserved_count, &symbols->trivia)) memo = NULL;
|
|
189
|
-
bool accepted = function_macro_prove_braced_type(lexer, symbols, reserved_words, reserved_count, memo);
|
|
218
|
+
bool accepted = function_macro_prove_braced_type(lexer, consumed_backslash, symbols, reserved_words, reserved_count, memo);
|
|
190
219
|
macro_proof_finish(memo, accepted);
|
|
191
220
|
return accepted;
|
|
192
221
|
}
|
|
193
222
|
|
|
194
|
-
static bool
|
|
223
|
+
static inline bool scan_braced_macro_type_start(TSLexer *lexer, const MacroAttributeSymbols *symbols, const char *const *reserved_words, unsigned reserved_count) {
|
|
224
|
+
return scan_braced_macro_type_start_from(lexer, false, symbols, reserved_words, reserved_count);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
static bool function_macro_prove_braced_type(TSLexer *lexer, bool consumed_backslash, const MacroAttributeSymbols *symbols,
|
|
195
228
|
const char *const *reserved_words, unsigned reserved_count, MacroProofMemo *memo) {
|
|
196
229
|
const PragmaTrivia *trivia = &symbols->trivia;
|
|
197
230
|
MacroTypeState state = {0};
|
|
198
231
|
for (;;) {
|
|
199
|
-
|
|
200
|
-
uint8_t key = state.marker | (state.known_type << 1) | (state.tag << 2) | (state.body << 3);
|
|
201
|
-
unsigned cached = state.started ? macro_proof_step(memo, lexer, key) : 0;
|
|
232
|
+
unsigned cached = consumed_backslash ? 0 : function_macro_type_trivia(lexer, &state, trivia, memo);
|
|
202
233
|
if (cached) return cached == 2;
|
|
203
|
-
|
|
204
|
-
|
|
234
|
+
bool escaped = consumed_backslash;
|
|
235
|
+
consumed_backslash = false;
|
|
236
|
+
MacroTypeResult result = !escaped && (lexer->lookahead == '[' || lexer->lookahead == '{')
|
|
205
237
|
? function_macro_type_group(lexer, &state, trivia)
|
|
206
|
-
: function_macro_type_word(lexer, symbols, reserved_words, reserved_count, &state);
|
|
238
|
+
: function_macro_type_word(lexer, escaped, symbols, reserved_words, reserved_count, &state);
|
|
207
239
|
if (result != MACRO_TYPE_CONTINUE) return result == MACRO_TYPE_ACCEPT;
|
|
208
240
|
}
|
|
209
241
|
}
|
|
210
242
|
|
|
243
|
+
static unsigned function_macro_type_trivia(TSLexer *lexer, const MacroTypeState *state, const PragmaTrivia *trivia, MacroProofMemo *memo) {
|
|
244
|
+
uint8_t key = state->marker | (state->known_type << 1) | (state->tag << 2) | (state->body << 3);
|
|
245
|
+
for (;;) {
|
|
246
|
+
while (pragma_space(lexer->lookahead)) lexer->advance(lexer, !state->started);
|
|
247
|
+
unsigned cached = state->started ? macro_proof_step(memo, lexer, key) : 0;
|
|
248
|
+
if (cached || !scan_pragma_empty(lexer, trivia)) return cached;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
211
252
|
static MacroTypeResult function_macro_type_group(TSLexer *lexer, MacroTypeState *state, const PragmaTrivia *trivia) {
|
|
212
253
|
if (lexer->lookahead == '[') {
|
|
213
254
|
if (!state->started) lexer->mark_end(lexer);
|
|
@@ -219,11 +260,11 @@ static MacroTypeResult function_macro_type_group(TSLexer *lexer, MacroTypeState
|
|
|
219
260
|
return MACRO_TYPE_CONTINUE;
|
|
220
261
|
}
|
|
221
262
|
|
|
222
|
-
static MacroTypeResult function_macro_type_word(TSLexer *lexer, const MacroAttributeSymbols *symbols,
|
|
263
|
+
static MacroTypeResult function_macro_type_word(TSLexer *lexer, bool consumed_backslash, const MacroAttributeSymbols *symbols,
|
|
223
264
|
const char *const *reserved_words, unsigned reserved_count, MacroTypeState *state) {
|
|
224
265
|
char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
|
|
225
|
-
MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true, .lookahead_only = true, .preserve_end = state->started};
|
|
226
|
-
if (!
|
|
266
|
+
MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true, .lookahead_only = true, .preserve_end = state->started || consumed_backslash};
|
|
267
|
+
if (!scan_macro_name_from(lexer, &name, consumed_backslash)) return MACRO_TYPE_REJECT;
|
|
227
268
|
if (!state->started) {
|
|
228
269
|
bool handled;
|
|
229
270
|
bool accepted = function_macro_first_word(lexer, &name, symbols, &handled);
|
|
@@ -360,6 +401,100 @@ static bool function_macro_scalar_type_keyword(const char *word) {
|
|
|
360
401
|
sizeof(function_macro_scalar_type_keywords) / sizeof(function_macro_scalar_type_keywords[0]));
|
|
361
402
|
}
|
|
362
403
|
|
|
404
|
+
// Conservative lookahead: rules out `#` directives, `_Pragma`, and scalar-typed object declarations from
|
|
405
|
+
// `lexer->lookahead` and buffered ASCII units without moving the lexer cursor.
|
|
406
|
+
static PeekExtrasKind peek_extras_kind(const TSLexer *lexer, bool allow_directive, bool allow_object, bool allow_pragma) {
|
|
407
|
+
int32_t look = lexer->lookahead;
|
|
408
|
+
if (look > 0 && look < 0x80 && !peek_ascii_space((uint16_t)look) && look != '\\') {
|
|
409
|
+
PeekExtrasKind direct = peek_extras_first((uint16_t)look, allow_directive, allow_object, allow_pragma);
|
|
410
|
+
if (direct != PEEK_EXTRAS_UNKNOWN) return direct;
|
|
411
|
+
}
|
|
412
|
+
PeekUnitsView view = {0};
|
|
413
|
+
view.units = lexer->peek_units(lexer, &view.count, &view.size);
|
|
414
|
+
if (!view.count) return PEEK_EXTRAS_UNKNOWN;
|
|
415
|
+
uint32_t i = 0;
|
|
416
|
+
while (i < view.count && peek_ascii_space(peek_unit_at(view.units, view.size, i))) i++;
|
|
417
|
+
if (i >= view.count) return PEEK_EXTRAS_UNKNOWN;
|
|
418
|
+
uint16_t first = peek_unit_at(view.units, view.size, i);
|
|
419
|
+
if (first >= 0x80 || first == '\\') return PEEK_EXTRAS_UNKNOWN;
|
|
420
|
+
PeekExtrasKind direct = peek_extras_first(first, allow_directive, allow_object, allow_pragma);
|
|
421
|
+
if (direct != PEEK_EXTRAS_UNKNOWN) return direct;
|
|
422
|
+
return peek_extras_word(view, i, allow_object, allow_pragma);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
static PeekExtrasKind peek_extras_first(uint16_t first, bool allow_directive, bool allow_object, bool allow_pragma) {
|
|
426
|
+
if (first == '#') return allow_directive ? PEEK_EXTRAS_DIRECTIVE : PEEK_EXTRAS_NONE;
|
|
427
|
+
bool pragma_possible = allow_pragma && first == '_';
|
|
428
|
+
bool object_possible = allow_object && peek_object_starter(first);
|
|
429
|
+
return object_possible || pragma_possible ? PEEK_EXTRAS_UNKNOWN : PEEK_EXTRAS_NONE;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
static PeekExtrasKind peek_extras_word(PeekUnitsView view, uint32_t i, bool allow_object, bool allow_pragma) {
|
|
433
|
+
char word[12] = {0};
|
|
434
|
+
unsigned length = 0;
|
|
435
|
+
while (i < view.count) {
|
|
436
|
+
uint16_t c = peek_unit_at(view.units, view.size, i);
|
|
437
|
+
if (c >= 0x80 || c == '\\') return PEEK_EXTRAS_UNKNOWN;
|
|
438
|
+
if (!peek_ascii_ident(c, true)) break;
|
|
439
|
+
if (length < sizeof word - 1) word[length++] = (char)c;
|
|
440
|
+
else length = sizeof word;
|
|
441
|
+
i++;
|
|
442
|
+
}
|
|
443
|
+
if (i >= view.count) return PEEK_EXTRAS_UNKNOWN;
|
|
444
|
+
if (allow_pragma && length == sizeof pragma_word - 1 && strcmp(word, pragma_word) == 0) return PEEK_EXTRAS_DECLARATION;
|
|
445
|
+
if (!allow_object || length >= sizeof word || !function_macro_scalar_type_keyword(word)) return PEEK_EXTRAS_NONE;
|
|
446
|
+
return peek_object_declarator_units(view, i) ? PEEK_EXTRAS_DECLARATION : PEEK_EXTRAS_NONE;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
static bool peek_object_declarator_units(PeekUnitsView view, uint32_t i) {
|
|
450
|
+
while (i < view.count && (peek_ascii_space(peek_unit_at(view.units, view.size, i)) || peek_unit_at(view.units, view.size, i) == '*')) i++;
|
|
451
|
+
if (i >= view.count) return true;
|
|
452
|
+
uint16_t decl = peek_unit_at(view.units, view.size, i);
|
|
453
|
+
if (decl >= 0x80 || decl == '\\' || decl == '/' || decl == '#') return true;
|
|
454
|
+
if (!peek_ascii_ident(decl, false)) return false;
|
|
455
|
+
bool non_plain = false;
|
|
456
|
+
i = peek_skip_ident(view, i + 1, &non_plain);
|
|
457
|
+
if (non_plain) return true;
|
|
458
|
+
while (i < view.count && peek_ascii_space(peek_unit_at(view.units, view.size, i))) i++;
|
|
459
|
+
if (i >= view.count) return true;
|
|
460
|
+
uint16_t after = peek_unit_at(view.units, view.size, i);
|
|
461
|
+
return after >= 0x80 || after == '\\' || after == '/' || after == '#' || peek_ascii_ident(after, false);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
static uint32_t peek_skip_ident(PeekUnitsView view, uint32_t i, bool *non_plain) {
|
|
465
|
+
for (; i < view.count; i++) {
|
|
466
|
+
uint16_t c = peek_unit_at(view.units, view.size, i);
|
|
467
|
+
if (c >= 0x80 || c == '\\') {
|
|
468
|
+
*non_plain = true;
|
|
469
|
+
return i;
|
|
470
|
+
}
|
|
471
|
+
if (!peek_ascii_ident(c, true)) break;
|
|
472
|
+
}
|
|
473
|
+
return i;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
static inline bool peek_object_starter(uint16_t c) {
|
|
477
|
+
static const uint64_t starter_hi = (1ULL << ('_' - 64)) | (1ULL << ('b' - 64)) | (1ULL << ('c' - 64)) |
|
|
478
|
+
(1ULL << ('d' - 64)) | (1ULL << ('f' - 64)) | (1ULL << ('i' - 64)) | (1ULL << ('m' - 64)) |
|
|
479
|
+
(1ULL << ('n' - 64)) | (1ULL << ('p' - 64)) | (1ULL << ('s' - 64)) | (1ULL << ('u' - 64)) | (1ULL << ('v' - 64));
|
|
480
|
+
return c >= 64 && ((starter_hi >> (c - 64)) & 1ULL) != 0;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
static inline uint16_t peek_unit_at(const char *units, uint32_t size, uint32_t index) {
|
|
484
|
+
uint16_t c = (uint8_t)units[index * size];
|
|
485
|
+
if (size == 2) memcpy(&c, units + index * size, sizeof c);
|
|
486
|
+
return c;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
static inline bool peek_ascii_space(uint16_t c) {
|
|
490
|
+
return c == ' ' || (uint32_t)(c - '\t') < 5;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
static inline bool peek_ascii_ident(uint16_t c, bool continuation) {
|
|
494
|
+
return c < 0x80 && ((preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1) &&
|
|
495
|
+
(continuation || c < '0' || c > '9');
|
|
496
|
+
}
|
|
497
|
+
|
|
363
498
|
static bool function_macro_object_prefix_keyword(const char *word) {
|
|
364
499
|
return function_macro_word_in(word, function_macro_object_prefix_keywords,
|
|
365
500
|
sizeof(function_macro_object_prefix_keywords) / sizeof(function_macro_object_prefix_keywords[0]));
|
package/src/macroProof.h
CHANGED
|
@@ -57,7 +57,7 @@ static void macro_proof_finish(MacroProofMemo *memo, bool accepted) {
|
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
static void macro_proof_destroy(MacroProofMemo *memo) {
|
|
60
|
+
static inline void macro_proof_destroy(MacroProofMemo *memo) {
|
|
61
61
|
if (!memo) return;
|
|
62
62
|
if (memo->root) {
|
|
63
63
|
macro_proof_free_node(memo->root, 3);
|
package/src/pragma.h
CHANGED
|
@@ -37,9 +37,16 @@ typedef enum {
|
|
|
37
37
|
} MacroNameCharacter;
|
|
38
38
|
|
|
39
39
|
static bool scan_macro_name(TSLexer *lexer, MacroName *name);
|
|
40
|
-
static
|
|
40
|
+
static bool scan_macro_name_from(TSLexer *lexer, MacroName *name, bool consumed_backslash);
|
|
41
|
+
static bool scan_macro_name_tail(TSLexer *lexer, MacroName *name, bool has_name);
|
|
42
|
+
static bool scan_macro_name_ascii(TSLexer *lexer, MacroName *name);
|
|
43
|
+
static void scan_macro_name_start(TSLexer *lexer, const MacroName *name);
|
|
41
44
|
static MacroNameCharacter scan_macro_name_character(TSLexer *lexer, bool continuation, MacroName *name);
|
|
42
45
|
static bool scan_macro_name_ucn(TSLexer *lexer);
|
|
46
|
+
static void skip_leading_spaces_and_splices(TSLexer *lexer, bool (*is_space)(int32_t), bool *newline, bool *consumed_backslash);
|
|
47
|
+
static bool skip_leading_space_run(TSLexer *lexer, bool (*is_space)(int32_t), bool *newline);
|
|
48
|
+
static uint32_t skip_ascii_space_units(TSLexer *lexer, bool *newline);
|
|
49
|
+
static int32_t peek_after_ascii_spaces(const TSLexer *lexer);
|
|
43
50
|
|
|
44
51
|
static bool scan_pragma_argument(TSLexer *lexer);
|
|
45
52
|
static bool scan_pragma_spacing(TSLexer *lexer);
|
|
@@ -478,10 +485,110 @@ static bool scan_function_macro_name(TSLexer *lexer, bool allow_pragma, TSSymbol
|
|
|
478
485
|
return adjacent;
|
|
479
486
|
}
|
|
480
487
|
|
|
488
|
+
static void skip_leading_spaces_and_splices(TSLexer *lexer, bool (*is_space)(int32_t), bool *newline, bool *consumed_backslash) {
|
|
489
|
+
*consumed_backslash = false;
|
|
490
|
+
if (lexer->lookahead == ' ') {
|
|
491
|
+
lexer->advance(lexer, true);
|
|
492
|
+
if (!is_space(lexer->lookahead) && lexer->lookahead != '\\') return;
|
|
493
|
+
} else if (!is_space(lexer->lookahead) && lexer->lookahead != '\\') {
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
bool marked = false;
|
|
497
|
+
for (;;) {
|
|
498
|
+
marked |= skip_leading_space_run(lexer, is_space, newline);
|
|
499
|
+
if (lexer->lookahead != '\\') break;
|
|
500
|
+
TSLexerCheckpoint slash;
|
|
501
|
+
lexer->get_checkpoint(lexer, &slash);
|
|
502
|
+
lexer->advance(lexer, false);
|
|
503
|
+
if (!scan_preproc_newline(lexer, false)) {
|
|
504
|
+
lexer->mark_start(lexer, &slash);
|
|
505
|
+
lexer->mark_end_at(lexer, &slash);
|
|
506
|
+
*consumed_backslash = true;
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
marked = true;
|
|
510
|
+
}
|
|
511
|
+
if (!marked) return;
|
|
512
|
+
TSLexerCheckpoint after;
|
|
513
|
+
lexer->get_checkpoint(lexer, &after);
|
|
514
|
+
lexer->mark_start(lexer, &after);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
static bool skip_leading_space_run(TSLexer *lexer, bool (*is_space)(int32_t), bool *newline) {
|
|
518
|
+
bool used_units = false;
|
|
519
|
+
for (;;) {
|
|
520
|
+
if (skip_ascii_space_units(lexer, newline)) {
|
|
521
|
+
used_units = true;
|
|
522
|
+
continue;
|
|
523
|
+
}
|
|
524
|
+
if (!is_space(lexer->lookahead)) return used_units;
|
|
525
|
+
if (newline) *newline |= lexer->lookahead == '\n' || lexer->lookahead == '\r';
|
|
526
|
+
lexer->advance(lexer, true);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
static uint32_t skip_ascii_space_units(TSLexer *lexer, bool *newline) {
|
|
531
|
+
uint32_t count, size;
|
|
532
|
+
const char *units = lexer->peek_units(lexer, &count, &size);
|
|
533
|
+
if (!count) return 0;
|
|
534
|
+
uint32_t i = 0, line_feeds = 0, tail = 0;
|
|
535
|
+
while (i < count) {
|
|
536
|
+
uint16_t c = (uint8_t)units[i * size];
|
|
537
|
+
if (size == 2) memcpy(&c, units + i * size, sizeof c);
|
|
538
|
+
if (c == '\n') {
|
|
539
|
+
line_feeds++;
|
|
540
|
+
tail = 0;
|
|
541
|
+
} else if (c == ' ' || c == '\t' || c == '\f' || c == '\v') {
|
|
542
|
+
tail++;
|
|
543
|
+
} else {
|
|
544
|
+
break;
|
|
545
|
+
}
|
|
546
|
+
i++;
|
|
547
|
+
}
|
|
548
|
+
if (!i) return 0;
|
|
549
|
+
if (newline && line_feeds) *newline = true;
|
|
550
|
+
lexer->advance_units(lexer, i, line_feeds, tail);
|
|
551
|
+
return i;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
static int32_t peek_after_ascii_spaces(const TSLexer *lexer) {
|
|
555
|
+
int32_t look = lexer->lookahead;
|
|
556
|
+
if (look > 0 && look < 0x80 && look != ' ' && (uint32_t)(look - '\t') >= 5 && look != '\\') return look;
|
|
557
|
+
uint32_t count, size;
|
|
558
|
+
const char *units = lexer->peek_units(lexer, &count, &size);
|
|
559
|
+
if (!count) return -1;
|
|
560
|
+
uint32_t i = (look == ' ' || (uint32_t)(look - '\t') < 5) ? 1 : 0;
|
|
561
|
+
for (; i < count; i++) {
|
|
562
|
+
uint16_t c = (uint8_t)units[i * size];
|
|
563
|
+
if (size == 2) memcpy(&c, units + 2 * (size_t)i, sizeof c);
|
|
564
|
+
if (c != ' ' && (uint32_t)(c - '\t') >= 5) return c < 0x80 && c != '\\' ? (int32_t)c : -1;
|
|
565
|
+
}
|
|
566
|
+
return -1;
|
|
567
|
+
}
|
|
568
|
+
|
|
481
569
|
static bool scan_macro_name(TSLexer *lexer, MacroName *name) {
|
|
482
|
-
|
|
570
|
+
return scan_macro_name_from(lexer, name, false);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
static bool scan_macro_name_from(TSLexer *lexer, MacroName *name, bool consumed_backslash) {
|
|
574
|
+
if (consumed_backslash) {
|
|
575
|
+
name->ascii = false;
|
|
576
|
+
if (!scan_macro_name_ucn(lexer)) return false;
|
|
577
|
+
if (!name->lookahead_only) lexer->mark_end(lexer);
|
|
578
|
+
return scan_macro_name_tail(lexer, name, true);
|
|
579
|
+
}
|
|
580
|
+
scan_macro_name_start(lexer, name);
|
|
581
|
+
if ((uint32_t)lexer->lookahead >= 0x80u || lexer->lookahead == '\\') {
|
|
582
|
+
return scan_macro_name_tail(lexer, name, false);
|
|
583
|
+
}
|
|
584
|
+
if (!scan_macro_name_ascii(lexer, name)) return false;
|
|
585
|
+
if ((uint32_t)lexer->lookahead < 0x80u && lexer->lookahead != '\\') return true;
|
|
586
|
+
return scan_macro_name_tail(lexer, name, true);
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
static bool scan_macro_name_tail(TSLexer *lexer, MacroName *name, bool has_name) {
|
|
483
590
|
for (;;) {
|
|
484
|
-
if (!has_name) scan_macro_name_start(lexer, name
|
|
591
|
+
if (!has_name) scan_macro_name_start(lexer, name);
|
|
485
592
|
MacroNameCharacter character = scan_macro_name_character(lexer, has_name, name);
|
|
486
593
|
if (character == MACRO_NAME_INVALID) return false;
|
|
487
594
|
if (character == MACRO_NAME_END) return has_name;
|
|
@@ -494,16 +601,31 @@ static bool scan_macro_name(TSLexer *lexer, MacroName *name) {
|
|
|
494
601
|
}
|
|
495
602
|
}
|
|
496
603
|
|
|
497
|
-
static
|
|
498
|
-
|
|
499
|
-
if (
|
|
604
|
+
static bool scan_macro_name_ascii(TSLexer *lexer, MacroName *name) {
|
|
605
|
+
int32_t c = lexer->lookahead;
|
|
606
|
+
if (!((preproc_identifier_start_ascii[c / 64] >> (c % 64)) & 1)) return false;
|
|
607
|
+
do {
|
|
608
|
+
if (name->ascii) {
|
|
609
|
+
if (name->length < name->capacity) name->word[name->length++] = (char)c;
|
|
610
|
+
else name->ascii = false;
|
|
611
|
+
}
|
|
612
|
+
lexer->advance(lexer, false);
|
|
613
|
+
c = lexer->lookahead;
|
|
614
|
+
} while ((uint32_t)c < 0x80u && c != '\\' && ((preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1));
|
|
615
|
+
if (!name->lookahead_only) lexer->mark_end(lexer);
|
|
616
|
+
return true;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
static void scan_macro_name_start(TSLexer *lexer, const MacroName *name) {
|
|
620
|
+
while (pragma_space(lexer->lookahead)) lexer->advance(lexer, !name->preserve_end);
|
|
621
|
+
if (name->lookahead_only && !name->preserve_end) lexer->mark_end(lexer);
|
|
500
622
|
}
|
|
501
623
|
|
|
502
624
|
static MacroNameCharacter scan_macro_name_character(TSLexer *lexer, bool continuation, MacroName *name) {
|
|
503
625
|
int32_t c = lexer->lookahead;
|
|
504
626
|
if (c == '\\') {
|
|
505
627
|
lexer->advance(lexer, false);
|
|
506
|
-
if (scan_preproc_newline(lexer, !continuation)) return MACRO_NAME_SPLICE;
|
|
628
|
+
if (scan_preproc_newline(lexer, !continuation && !name->preserve_end)) return MACRO_NAME_SPLICE;
|
|
507
629
|
name->ascii = false;
|
|
508
630
|
return scan_macro_name_ucn(lexer) ? MACRO_NAME_CHARACTER : MACRO_NAME_INVALID;
|
|
509
631
|
}
|