@willbooster/tree-sitter-c 34.0.0 → 34.0.2

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 CHANGED
@@ -74,7 +74,10 @@ Append `_preproc_directive_start` in its inherited slot as a zero-width guard af
74
74
  including keywords in overridden directive rules. It is not an extra. Retain the phase-two splice handling in
75
75
  `preprocessorToken` when overriding keyword spelling.
76
76
  Port the scanner lifecycle functions together with `EmptyDirectives` from `emptyDirectives.h`; the source-boundary
77
- facts belong to one full parse and are freed by scanner destruction. Run `scan_directive_context` first,
77
+ facts belong to one full parse and are freed by scanner destruction. When the scanner is ready and no external token
78
+ other than `pragma_operator`, `_directive_context` and `preproc_empty` is valid, dispatch to `scan_extras` first;
79
+ `only_extras_valid` checks the inherited slots only, so extend it to every external token the derived grammar adds,
80
+ or the fast path drops those tokens. Otherwise run `scan_directive_context` first,
78
81
  emitting `_preproc_directive_start` only at `empty_directives_after_keyword` before null-directive consumption,
79
82
  then reject external scans at `empty_directives_at_line_end` before the other token dispatch. Pass `empty_directives_skip` and its state through `ConditionalMacroSymbols.skip_empty` and
80
83
  `empty_context` so declaration lookahead ignores null directives without moving the opening token's range.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-c",
3
- "version": "34.0.0",
3
+ "version": "34.0.2",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "c",
@@ -1,4 +1,4 @@
1
- // code-gauge-allow-file fileNcss 575 -- One source-fact pass shares compact character storage, directive lexing and physical-position conversion.
1
+ // code-gauge-allow-file fileNcss 706 -- One source-fact pass shares compact character storage, directive lexing and physical-position conversion.
2
2
  #ifndef TREE_SITTER_C_EMPTY_DIRECTIVES_H_
3
3
  #define TREE_SITTER_C_EMPTY_DIRECTIVES_H_
4
4
 
@@ -35,7 +35,8 @@ typedef struct {
35
35
  } DirectiveLine;
36
36
 
37
37
  static void directive_prefixes(EmptyDirectives *state, DirectiveInput *input);
38
- static bool directive_append(DirectiveInput *input, uint32_t *capacity, TSLexer *lexer);
38
+ static uint32_t directive_skip_inert(const DirectiveInput *input, uint32_t end, uint32_t position, uint32_t *byte);
39
+ static bool directive_append(DirectiveInput *input, uint32_t *capacity, uint32_t *byte, TSLexer *lexer);
39
40
  static bool directive_reserve(DirectiveInput *input, uint32_t *capacity, unsigned width);
40
41
  static bool directive_position(DirectiveInput *input, uint32_t index, uint32_t byte, uint32_t width);
41
42
  static void directive_encode(DirectiveInput *input, uint32_t c);
@@ -50,16 +51,27 @@ static bool directive_requires_operand(DirectiveKind kind);
50
51
  static void directive_line_character(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
51
52
  static void directive_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
52
53
  static bool directive_pragma_part(DirectiveLine *line, int32_t c);
54
+ static bool directive_neutral(const DirectiveLine *line);
55
+ static void directive_neutral_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
53
56
  static bool directive_header_ready(DirectiveLine *line, int32_t c);
54
57
  static bool directive_header(DirectiveInput *input, int32_t close);
55
58
  static void directive_line_comment(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
56
59
  static uint32_t directive_splice_end(const DirectiveInput *input);
57
60
 
58
61
  static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer);
62
+ static bool empty_directives_line_end_at(const EmptyDirectives *state, uint32_t byte);
63
+ static bool empty_directives_start_at(const EmptyDirectives *state, uint32_t byte);
64
+ static void empty_directives_consume_started(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix);
59
65
  static void empty_directives_find(EmptyDirectives *state, DirectiveInput *input);
60
66
  static int32_t directive_next(DirectiveInput *input);
61
67
  static int32_t directive_physical_next(DirectiveInput *input);
68
+ static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t index, int32_t *c);
62
69
  static int32_t directive_peek(const DirectiveInput *input);
70
+ static int32_t directive_plain(const DirectiveInput *input);
71
+ static void directive_skip_plain(DirectiveInput *input);
72
+ typedef enum { DIRECTIVE_RUN_WORD, DIRECTIVE_RUN_CODE, DIRECTIVE_RUN_SPACE, DIRECTIVE_RUN_LINE_COMMENT, DIRECTIVE_RUN_BLOCK_COMMENT } DirectiveRun;
73
+ static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun run);
74
+ static bool directive_run_continues(DirectiveRun run, uint16_t c);
63
75
  static void directive_pragma_name(DirectiveLine *line, const DirectiveInput *input);
64
76
  static bool directive_pragma_operator(const DirectiveInput *input);
65
77
  static int32_t directive_pragma_token(DirectiveInput *input);
@@ -74,7 +86,8 @@ typedef struct {
74
86
 
75
87
  static uint32_t directive_keyword_end(const DirectiveInput *input);
76
88
  static DirectiveKind directive_keyword(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, const DirectiveWord *word, uint32_t end);
77
- static DirectiveWord directive_word(DirectiveInput *input, int32_t first, bool raw);
89
+ static DirectiveWord directive_word(DirectiveInput *input, int32_t first, bool raw, bool classify);
90
+ static void directive_word_run(DirectiveInput *input, DirectiveWord *word, bool classify);
78
91
  static DirectiveKind directive_word_kind(const DirectiveWord *word);
79
92
  static void directive_word_character(DirectiveWord *word, int32_t c);
80
93
  static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool name, bool pragma, bool had_operand, bool identifier);
@@ -110,10 +123,14 @@ static bool empty_directives_at_start(const EmptyDirectives *state, TSLexer *lex
110
123
  }
111
124
 
112
125
  static bool empty_directives_at_line_end(const EmptyDirectives *state, TSLexer *lexer) {
113
- if (!state || !state->line_ends) return false;
126
+ if (!state || !state->line_ends || !(directive_horizontal(lexer->lookahead) || directive_newline(lexer->lookahead))) return false;
114
127
  TSLexerPosition position;
115
128
  lexer->get_position(lexer, &position);
116
- return position.bytes < state->bytes && (state->line_ends[position.bytes / 8] & (1u << (position.bytes % 8)));
129
+ return empty_directives_line_end_at(state, position.bytes);
130
+ }
131
+
132
+ static bool empty_directives_line_end_at(const EmptyDirectives *state, uint32_t byte) {
133
+ return state->line_ends && byte < state->bytes && (state->line_ends[byte / 8] & (1u << (byte % 8)));
117
134
  }
118
135
 
119
136
  static bool empty_directives_consume(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix);
@@ -122,18 +139,32 @@ static bool empty_directives_skip(TSLexer *lexer, void *payload) {
122
139
  return empty_directives_consume(lexer, payload, false);
123
140
  }
124
141
 
142
+ // A null directive, including its prefix, starts only at `#`, `\\` or a space, so other characters need no position query,
143
+ // which would make the runtime allocate the token on the heap.
125
144
  static bool empty_directives_consume(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix) {
126
- if (!state || !state->starts) return false;
145
+ int32_t c = lexer->lookahead;
146
+ if (!state || !(c == '#' || c == '\\' || pragma_space(c))) return false;
127
147
  TSLexerPosition position;
128
148
  lexer->get_position(lexer, &position);
129
- if (position.bytes >= state->bytes || !(state->starts[position.bytes / 8] & (1u << (position.bytes % 8)))) return false;
149
+ if (!empty_directives_start_at(state, position.bytes)) return false;
150
+ empty_directives_consume_started(lexer, state, skip_prefix);
151
+ return true;
152
+ }
153
+
154
+ static bool empty_directives_start_at(const EmptyDirectives *state, uint32_t byte) {
155
+ return state->starts && byte < state->bytes && (state->starts[byte / 8] & (1u << (byte % 8)));
156
+ }
157
+
158
+ // Consumes the rest of a null directive whose start was confirmed at a position the lexer has not left except over
159
+ // prefix characters.
160
+ static void empty_directives_consume_started(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix) {
161
+ TSLexerPosition position;
130
162
  while (lexer->lookahead != '#' && !lexer->eof(lexer)) lexer->advance(lexer, skip_prefix);
131
163
  do {
132
164
  lexer->advance(lexer, false);
133
165
  lexer->get_position(lexer, &position);
134
166
  } while (!lexer->eof(lexer) && position.bytes < state->bytes &&
135
167
  !(state->ends[position.bytes / 8] & (1u << (position.bytes % 8))));
136
- return true;
137
168
  }
138
169
 
139
170
  // code-gauge-allow functionNcss 33 -- The source-fact buffers are allocated and released together so initialization fails atomically.
@@ -141,14 +172,12 @@ static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
141
172
  TSLexerPosition start;
142
173
  lexer->get_position(lexer, &start);
143
174
  DirectiveInput input = {.byte = start.bytes};
144
- uint32_t capacity = 0;
175
+ uint32_t capacity = 0, byte = start.bytes;
145
176
  while (!lexer->eof(lexer)) {
146
- if (!directive_append(&input, &capacity, lexer)) goto failed;
177
+ if (!directive_append(&input, &capacity, &byte, lexer)) goto failed;
147
178
  }
148
- TSLexerPosition end;
149
- lexer->get_position(lexer, &end);
150
- input.end = end.bytes;
151
- state->bytes = end.bytes;
179
+ input.end = byte;
180
+ state->bytes = byte;
152
181
  state->starts = calloc((size_t)state->bytes / 8 + 1, 1);
153
182
  state->ends = calloc((size_t)state->bytes / 8 + 1, 1);
154
183
  state->directive_starts = calloc((size_t)state->bytes / 8 + 1, 1);
@@ -171,23 +200,22 @@ failed:
171
200
  return false;
172
201
  }
173
202
 
174
- // code-gauge-allow functionCognitiveComplexity 24 -- The reverse lexical pass applies sparse physical-position corrections while carrying null-prefix and line-end facts.
203
+ // code-gauge-allow functionCognitiveComplexity 29 -- The reverse lexical pass applies sparse physical-position corrections while carrying null-prefix and line-end facts.
175
204
  static void directive_prefixes(EmptyDirectives *state, DirectiveInput *input) {
176
205
  bool prefix = false, line_end = false;
177
206
  uint32_t byte = input->end, position = input->position_count;
178
207
  for (uint32_t i = input->length; i > 0;) {
208
+ if (!prefix && !line_end) i = directive_skip_inert(input, i, position, &byte);
209
+ if (!i) break;
179
210
  i--;
180
211
  if (i && directive_character(input, i) >= 0xdc00 && directive_character(input, i) <= 0xdfff &&
181
212
  directive_character(input, i - 1) >= 0xd800 && directive_character(input, i - 1) <= 0xdbff) continue;
182
- DirectiveInput character = *input;
183
- character.index = i;
184
- character.byte = 0;
185
- character.position_count = 0;
186
- int32_t c = directive_physical_next(&character);
213
+ int32_t c;
214
+ uint32_t width = directive_expected_width(input, i, &c);
187
215
  if (position && input->positions[position - 1].index == i) byte = input->positions[--position].byte;
188
216
  else {
189
- if (character.byte > byte) return;
190
- byte -= character.byte;
217
+ if (width > byte) return;
218
+ byte -= width;
191
219
  }
192
220
  if (byte > state->bytes) return;
193
221
  bool hash = c == '#' && (state->starts[byte / 8] & (1u << (byte % 8)));
@@ -199,22 +227,39 @@ static void directive_prefixes(EmptyDirectives *state, DirectiveInput *input) {
199
227
  }
200
228
  }
201
229
 
202
- static bool directive_append(DirectiveInput *input, uint32_t *capacity, TSLexer *lexer) {
203
- uint32_t c = lexer->lookahead, index = input->length;
230
+ // Steps back from the character before `end` over characters that cannot change the reverse pass's state while no
231
+ // prefix or line end is carried: plain-width characters that are neither `#` nor barriers. Returns the new end.
232
+ static uint32_t directive_skip_inert(const DirectiveInput *input, uint32_t end, uint32_t position, uint32_t *byte) {
233
+ uint32_t stop = position ? input->positions[position - 1].index + 1 : 0, ascii_width = input->utf16 ? 2 : 1;
234
+ while (end > stop) {
235
+ uint32_t c = directive_character(input, end - 1);
236
+ uint32_t width = c < 0x80 ? ascii_width : input->utf16 && (c < 0xd800 || c > 0xdfff) ? 2 : 0;
237
+ if (!width || c == '#' || width > *byte) break;
238
+ uint32_t previous = *byte - width;
239
+ if (input->prefix_barriers[previous / 8] & (1u << (previous % 8))) break;
240
+ *byte = previous;
241
+ end--;
242
+ }
243
+ return end;
244
+ }
245
+
246
+ // `*byte` is the lexer's position before the character and is advanced past it.
247
+ static bool directive_append(DirectiveInput *input, uint32_t *capacity, uint32_t *byte, TSLexer *lexer) {
248
+ uint32_t c = lexer->lookahead, index = input->length, start = *byte;
204
249
  unsigned width = directive_utf8_width(c), utf16_width = c > 0xffff && c <= 0x10ffff ? 4 : 2;
205
250
  if (!directive_reserve(input, capacity, c > 0xffff && c <= 0x10ffff ? 2 : 1)) return false;
206
251
  directive_encode(input, c);
207
- TSLexerPosition start, end;
208
- lexer->get_position(lexer, &start);
252
+ TSLexerPosition end;
209
253
  lexer->advance(lexer, false);
210
254
  lexer->get_position(lexer, &end);
211
- uint32_t measured = end.bytes - start.bytes;
255
+ *byte = end.bytes;
256
+ uint32_t measured = end.bytes - start;
212
257
  if (!input->encoding_known && c <= 0x10ffff && width != utf16_width && !lexer->is_at_included_range_start(lexer)) {
213
258
  input->utf16 = measured == utf16_width;
214
259
  input->encoding_known = true;
215
260
  }
216
261
  if (c > 0x10ffff || measured != (input->utf16 ? utf16_width : width)) {
217
- return directive_position(input, index, start.bytes, measured);
262
+ return directive_position(input, index, start, measured);
218
263
  }
219
264
  return true;
220
265
  }
@@ -283,6 +328,8 @@ static unsigned directive_utf8_width(uint32_t c) {
283
328
  static void empty_directives_find(EmptyDirectives *state, DirectiveInput *input) {
284
329
  DirectiveLine line = {.beginning = true};
285
330
  while (input->index < input->length) {
331
+ // Horizontal spaces never change the line state.
332
+ directive_plain_run(input, DIRECTIVE_RUN_SPACE);
286
333
  int32_t c = directive_next(input);
287
334
  if (c < 0) break;
288
335
  directive_line_character(state, input, &line, c);
@@ -312,7 +359,8 @@ static void directive_line_character(EmptyDirectives *state, DirectiveInput *inp
312
359
  line->pending = true;
313
360
  return;
314
361
  }
315
- directive_code(state, input, line, c);
362
+ if (directive_neutral(line)) directive_neutral_code(state, input, line, c);
363
+ else directive_code(state, input, line, c);
316
364
  }
317
365
 
318
366
  // code-gauge-allow functionCognitiveComplexity 19 -- Header names, quoted tokens and distinct keyword boundaries update one logical directive's lexical state.
@@ -333,12 +381,38 @@ static void directive_code(EmptyDirectives *state, DirectiveInput *input, Direct
333
381
  }
334
382
  if (!preproc_word(c, false) && !(c >= '0' && c <= '9')) return;
335
383
  uint32_t end = name ? directive_keyword_end(input) : 0;
336
- DirectiveWord word = directive_word(input, c, line->kind != DIRECTIVE_DIAGNOSTIC);
384
+ DirectiveWord word = directive_word(input, c, line->kind != DIRECTIVE_DIAGNOSTIC, true);
337
385
  DirectiveKind kind = name ? directive_keyword(state, input, line, &word, end) : directive_word_kind(&word);
338
386
  if (!name && !pragma && kind == DIRECTIVE_PRAGMA) directive_pragma_name(line, input);
339
387
  directive_word_role(line, kind, name, pragma, operand, preproc_word(c, false));
340
388
  }
341
389
 
390
+ // Outside a directive name, a line of no directive kind with no `_Pragma` or header operand in progress keeps that
391
+ // state through every token, so its words need no classification.
392
+ static bool directive_neutral(const DirectiveLine *line) {
393
+ return !line->pending && line->kind == DIRECTIVE_ORDINARY && !line->pragma_depth && !line->pragma_pending && !line->header;
394
+ }
395
+
396
+ // Also consumes the following characters that leave the line state as it is, which is every plain character but `/`.
397
+ static void directive_neutral_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c) {
398
+ line->operand = true;
399
+ line->beginning = false;
400
+ for (;;) {
401
+ if (c == '"' || c == '\'') {
402
+ if (directive_quoted(input, c)) {
403
+ directive_line_end(state, input, line);
404
+ return;
405
+ }
406
+ } else if (preproc_word(c, false) || (c >= '0' && c <= '9')) {
407
+ directive_word(input, c, true, false);
408
+ }
409
+ directive_plain_run(input, DIRECTIVE_RUN_CODE);
410
+ c = directive_plain(input);
411
+ if (c < 0 || c == '/') return;
412
+ directive_skip_plain(input);
413
+ }
414
+ }
415
+
342
416
  static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool name, bool pragma, bool had_operand, bool identifier) {
343
417
  if (name) {
344
418
  line->kind = kind;
@@ -444,6 +518,11 @@ static void directive_barrier(DirectiveInput *input) {
444
518
  }
445
519
 
446
520
  static int32_t directive_next(DirectiveInput *input) {
521
+ int32_t plain = directive_plain(input);
522
+ if (plain >= 0) {
523
+ directive_skip_plain(input);
524
+ return plain;
525
+ }
447
526
  while (input->index < input->length) {
448
527
  int32_t c = directive_physical_next(input);
449
528
  uint32_t after = c == '\\' ? directive_splice_end(input) : input->index;
@@ -462,20 +541,36 @@ static int32_t directive_next(DirectiveInput *input) {
462
541
  }
463
542
 
464
543
  static int32_t directive_physical_next(DirectiveInput *input) {
465
- uint32_t index = input->index, c = directive_character(input, input->index++);
466
- bool invalid = c == 0xdfff;
467
- if (c >= 0xd800 && c <= 0xdbff && input->index < input->length &&
468
- directive_character(input, input->index) >= 0xdc00 && directive_character(input, input->index) <= 0xdfff) {
469
- c = 0x10000 + ((c - 0xd800) << 10) + (directive_character(input, input->index++) - 0xdc00);
470
- }
544
+ uint32_t index = input->index;
545
+ int32_t c;
546
+ uint32_t width = directive_expected_width(input, index, &c);
547
+ input->index += c > 0xffff ? 2 : 1;
471
548
  input->before_previous_byte = input->previous_byte;
472
549
  input->previous_byte = input->byte;
473
550
  if (input->position_index < input->position_count && input->positions[input->position_index].index == index) {
474
551
  DirectivePosition position = input->positions[input->position_index++];
475
552
  input->previous_byte = position.byte;
476
553
  input->byte = position.byte + position.width;
477
- } else input->byte += input->utf16 ? (c > 0xffff ? 4 : 2) : invalid ? 1 : directive_utf8_width(c);
478
- return invalid ? 0xfffd : c;
554
+ } else input->byte += width;
555
+ return c;
556
+ }
557
+
558
+ // Decodes the character at `index` into `*c` and returns its byte width when it has no recorded position.
559
+ static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t index, int32_t *c) {
560
+ uint32_t unit = directive_character(input, index);
561
+ if (unit == 0xdfff) {
562
+ *c = 0xfffd;
563
+ return input->utf16 ? 2 : 1;
564
+ }
565
+ if (unit >= 0xd800 && unit <= 0xdbff && index + 1 < input->length) {
566
+ uint32_t low = directive_character(input, index + 1);
567
+ if (low >= 0xdc00 && low <= 0xdfff) {
568
+ *c = (int32_t)(0x10000 + ((unit - 0xd800) << 10) + (low - 0xdc00));
569
+ return 4;
570
+ }
571
+ }
572
+ *c = (int32_t)unit;
573
+ return input->utf16 ? 2 : directive_utf8_width(unit);
479
574
  }
480
575
 
481
576
  static uint32_t directive_splice_end(const DirectiveInput *input) {
@@ -489,10 +584,68 @@ static uint32_t directive_splice_end(const DirectiveInput *input) {
489
584
  }
490
585
 
491
586
  static int32_t directive_peek(const DirectiveInput *input) {
587
+ int32_t plain = directive_plain(input);
588
+ if (plain >= 0) return plain;
492
589
  DirectiveInput copy = *input;
493
590
  return directive_next(&copy);
494
591
  }
495
592
 
593
+ // Consumes the longest run of plain characters that `run` accepts and returns its length.
594
+ static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun run) {
595
+ uint32_t index = input->index, end = input->length;
596
+ if (input->position_index < input->position_count && input->positions[input->position_index].index < end) {
597
+ end = input->positions[input->position_index].index;
598
+ }
599
+ while (index < end) {
600
+ uint16_t c = directive_character(input, index);
601
+ if (c >= 0x80 || c == '\\' || directive_newline(c) || !directive_run_continues(run, c)) break;
602
+ index++;
603
+ }
604
+ uint32_t length = index - input->index, width = input->utf16 ? 2 : 1;
605
+ if (!length) return 0;
606
+ input->before_previous_byte = length > 1 ? input->byte + (length - 2) * width : input->previous_byte;
607
+ input->previous_byte = input->byte + (length - 1) * width;
608
+ input->byte += length * width;
609
+ input->index = index;
610
+ return length;
611
+ }
612
+
613
+ // `c` is ASCII.
614
+ static bool directive_run_continues(DirectiveRun run, uint16_t c) {
615
+ bool identifier = (preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1;
616
+ switch (run) {
617
+ case DIRECTIVE_RUN_WORD:
618
+ return identifier;
619
+ case DIRECTIVE_RUN_CODE:
620
+ // The rest of a neutral line: characters that start no token.
621
+ return !identifier && c != '"' && c != '\'' && c != '/';
622
+ case DIRECTIVE_RUN_SPACE:
623
+ return directive_horizontal(c);
624
+ case DIRECTIVE_RUN_LINE_COMMENT:
625
+ return true;
626
+ case DIRECTIVE_RUN_BLOCK_COMMENT:
627
+ return c != '*';
628
+ }
629
+ return false;
630
+ }
631
+
632
+ static void directive_skip_plain(DirectiveInput *input) {
633
+ input->index++;
634
+ input->before_previous_byte = input->previous_byte;
635
+ input->previous_byte = input->byte;
636
+ input->byte += input->utf16 ? 2 : 1;
637
+ }
638
+
639
+ // The next character when it is ASCII, cannot start a line splice or a newline, and has its expected width; otherwise -1.
640
+ static int32_t directive_plain(const DirectiveInput *input) {
641
+ if (input->index >= input->length) return -1;
642
+ uint16_t c = directive_character(input, input->index);
643
+ if (c >= 0x80 || c == '\\' || directive_newline(c)) return -1;
644
+ bool positioned = input->position_index < input->position_count &&
645
+ input->positions[input->position_index].index == input->index;
646
+ return positioned ? -1 : c;
647
+ }
648
+
496
649
  // `_Pragma` is the macro name of `#define _Pragma(x)` or `#ifdef _Pragma`, but an operator before a string literal.
497
650
  static void directive_pragma_name(DirectiveLine *line, const DirectiveInput *input) {
498
651
  if (line->kind == DIRECTIVE_UNDEF || (line->kind == DIRECTIVE_REQUIRED && !directive_pragma_operator(input))) line->pragma_name = true;
@@ -525,7 +678,12 @@ static int32_t directive_pragma_token(DirectiveInput *input) {
525
678
  static bool directive_comment(DirectiveInput *input, bool *line) {
526
679
  *line = directive_next(input) == '/';
527
680
  int32_t previous = 0;
681
+ DirectiveRun run = *line ? DIRECTIVE_RUN_LINE_COMMENT : DIRECTIVE_RUN_BLOCK_COMMENT;
528
682
  while (input->index < input->length) {
683
+ if (previous != '*') {
684
+ uint32_t length = directive_plain_run(input, run);
685
+ if (length) previous = directive_character(input, input->index - 1);
686
+ }
529
687
  int32_t c = directive_next(input);
530
688
  if (*line && c == '\n') return true;
531
689
  if (!*line && previous == '*' && c == '/') return true;
@@ -565,11 +723,13 @@ static DirectiveKind directive_keyword(EmptyDirectives *state, DirectiveInput *i
565
723
  return kind;
566
724
  }
567
725
 
568
- static DirectiveWord directive_word(DirectiveInput *input, int32_t first, bool raw) {
726
+ // Without `classify`, only `raw` of the result is complete.
727
+ static DirectiveWord directive_word(DirectiveInput *input, int32_t first, bool raw, bool classify) {
569
728
  DirectiveWord word = {0};
570
729
  directive_word_character(&word, first);
571
730
  bool number = first >= '0' && first <= '9';
572
731
  int32_t previous = first;
732
+ if (!number) directive_word_run(input, &word, classify);
573
733
  for (;;) {
574
734
  int32_t c = directive_peek(input);
575
735
  bool exponent_sign = number && (c == '+' || c == '-') &&
@@ -582,6 +742,13 @@ static DirectiveWord directive_word(DirectiveInput *input, int32_t first, bool r
582
742
  return word;
583
743
  }
584
744
 
745
+ static void directive_word_run(DirectiveInput *input, DirectiveWord *word, bool classify) {
746
+ uint32_t start = input->index, length = directive_plain_run(input, DIRECTIVE_RUN_WORD);
747
+ // Both parts of the word stop changing after these many characters.
748
+ uint32_t significant = classify ? sizeof word->directive : sizeof word->raw.value;
749
+ for (uint32_t i = 0; i < length && i < significant; i++) directive_word_character(word, directive_character(input, start + i));
750
+ }
751
+
585
752
  static DirectiveKind directive_word_kind(const DirectiveWord *word) {
586
753
  if (word->length >= sizeof word->directive) return DIRECTIVE_ORDINARY;
587
754
  if (strcmp(word->directive, "if") == 0 || strcmp(word->directive, "elif") == 0) return DIRECTIVE_CONDITION;
@@ -1,4 +1,4 @@
1
- // code-gauge-allow-file fileNcss 567 -- Annotation and argument scanners share lexical helpers and keyword tables.
1
+ // code-gauge-allow-file fileNcss 575 -- Annotation and argument scanners share lexical helpers and keyword tables.
2
2
  #ifndef TREE_SITTER_C_FUNCTION_MACRO_H_
3
3
  #define TREE_SITTER_C_FUNCTION_MACRO_H_
4
4
 
@@ -165,14 +165,16 @@ typedef enum {
165
165
  MACRO_TYPE_PREFIX_KNOWN_TYPE, MACRO_TYPE_PREFIX_BALANCED_TYPE, MACRO_TYPE_PREFIX_TAGGED_TYPE,
166
166
  } MacroTypePrefix;
167
167
 
168
- static MacroTypePrefix function_macro_type_prefix_item(TSLexer *lexer, const MacroName *name, const char *const *reserved_words, unsigned reserved_count, bool type_argument, const PragmaTrivia *trivia);
169
-
170
168
  typedef struct {
171
169
  bool marker, known_type, tag, body, started;
172
170
  } MacroTypeState;
173
171
 
174
172
  typedef enum { MACRO_TYPE_CONTINUE, MACRO_TYPE_ACCEPT, MACRO_TYPE_REJECT } MacroTypeResult;
175
173
 
174
+ static MacroTypePrefix function_macro_type_prefix_item(TSLexer *lexer, const MacroName *name, const char *const *reserved_words, unsigned reserved_count, const MacroTypeState *state, const PragmaTrivia *trivia);
175
+ static MacroTypePrefix function_macro_known_type_item(TSLexer *lexer, const MacroName *name, const PragmaTrivia *trivia);
176
+ static MacroTypePrefix function_macro_macro_type_item(TSLexer *lexer, const MacroTypeState *state, const PragmaTrivia *trivia);
177
+
176
178
  static MacroTypeResult function_macro_type_transition(MacroTypeState *state, MacroTypePrefix kind);
177
179
  static MacroTypeResult function_macro_type_group(TSLexer *lexer, MacroTypeState *state, const PragmaTrivia *trivia);
178
180
  static MacroTypeResult function_macro_type_word(TSLexer *lexer, const MacroAttributeSymbols *symbols,
@@ -228,7 +230,7 @@ static MacroTypeResult function_macro_type_word(TSLexer *lexer, const MacroAttri
228
230
  if (handled) return accepted ? MACRO_TYPE_ACCEPT : MACRO_TYPE_REJECT;
229
231
  }
230
232
  state->started = true;
231
- MacroTypePrefix kind = function_macro_type_prefix_item(lexer, &name, reserved_words, reserved_count, !state->known_type, &symbols->trivia);
233
+ MacroTypePrefix kind = function_macro_type_prefix_item(lexer, &name, reserved_words, reserved_count, state, &symbols->trivia);
232
234
  return function_macro_type_transition(state, kind);
233
235
  }
234
236
 
@@ -257,8 +259,8 @@ static MacroTypeResult function_macro_type_transition(MacroTypeState *state, Mac
257
259
  return MACRO_TYPE_REJECT;
258
260
  }
259
261
 
260
- // code-gauge-allow functionCognitiveComplexity 28 -- Keyword, modifier, and group alternatives classify one consumed type-prefix word.
261
- static MacroTypePrefix function_macro_type_prefix_item(TSLexer *lexer, const MacroName *name, const char *const *reserved_words, unsigned reserved_count, bool type_argument, const PragmaTrivia *trivia) {
262
+ // code-gauge-allow functionCognitiveComplexity 20 -- Keyword, modifier, and group alternatives classify one consumed type-prefix word.
263
+ static MacroTypePrefix function_macro_type_prefix_item(TSLexer *lexer, const MacroName *name, const char *const *reserved_words, unsigned reserved_count, const MacroTypeState *state, const PragmaTrivia *trivia) {
262
264
  if (name->ascii && strcmp(name->word, pragma_word) == 0) {
263
265
  return scan_pragma_suffix_end_with(lexer, false, trivia) && scan_pragma_spacing_with(lexer, trivia)
264
266
  ? MACRO_TYPE_PREFIX_MODIFIER : MACRO_TYPE_PREFIX_INVALID;
@@ -268,24 +270,33 @@ static MacroTypePrefix function_macro_type_prefix_item(TSLexer *lexer, const Mac
268
270
  return lexer->lookahead == '(' && function_macro_modifier_balanced(lexer, '(', ')', trivia) && scan_pragma_spacing_with(lexer, trivia)
269
271
  ? MACRO_TYPE_PREFIX_MODIFIER : MACRO_TYPE_PREFIX_INVALID;
270
272
  }
273
+ // Before a marker, function_macro_type_transition rejects every type, so its parentheses need not be scanned.
271
274
  if (name->ascii && function_macro_word_in(name->word, function_macro_type_keywords,
272
275
  sizeof(function_macro_type_keywords) / sizeof(function_macro_type_keywords[0]))) {
273
- if (lexer->lookahead == '(' && (!function_macro_modifier_balanced(lexer, '(', ')', trivia) || !scan_pragma_spacing_with(lexer, trivia))) return MACRO_TYPE_PREFIX_INVALID;
274
- if (strcmp(name->word, "struct") == 0 || strcmp(name->word, "union") == 0 || strcmp(name->word, "enum") == 0) return MACRO_TYPE_PREFIX_TAGGED_TYPE;
275
- return MACRO_TYPE_PREFIX_KNOWN_TYPE;
276
+ return state->marker ? function_macro_known_type_item(lexer, name, trivia) : MACRO_TYPE_PREFIX_INVALID;
276
277
  }
277
278
  if (name->ascii && function_macro_word_in(name->word, function_macro_type_prefix_modifiers,
278
279
  sizeof(function_macro_type_prefix_modifiers) / sizeof(function_macro_type_prefix_modifiers[0]))) return MACRO_TYPE_PREFIX_MODIFIER;
279
280
  if (function_macro_type_prefix_keyword(name, reserved_words, reserved_count)) return MACRO_TYPE_PREFIX_INVALID;
280
281
  if (lexer->lookahead == '(') {
281
- MacroGroup group = function_macro_group_prefix(lexer, '(', ')', NULL, NULL, type_argument, trivia);
282
- if (group == MACRO_GROUP_BRACED) return MACRO_TYPE_PREFIX_TYPE;
283
- if (group == MACRO_GROUP_BALANCED && scan_pragma_spacing_with(lexer, trivia)) return MACRO_TYPE_PREFIX_BALANCED_TYPE;
284
- return MACRO_TYPE_PREFIX_INVALID;
282
+ return state->marker ? function_macro_macro_type_item(lexer, state, trivia) : MACRO_TYPE_PREFIX_INVALID;
285
283
  }
286
284
  return MACRO_TYPE_PREFIX_MARKER;
287
285
  }
288
286
 
287
+ static MacroTypePrefix function_macro_known_type_item(TSLexer *lexer, const MacroName *name, const PragmaTrivia *trivia) {
288
+ if (lexer->lookahead == '(' && (!function_macro_modifier_balanced(lexer, '(', ')', trivia) || !scan_pragma_spacing_with(lexer, trivia))) return MACRO_TYPE_PREFIX_INVALID;
289
+ if (strcmp(name->word, "struct") == 0 || strcmp(name->word, "union") == 0 || strcmp(name->word, "enum") == 0) return MACRO_TYPE_PREFIX_TAGGED_TYPE;
290
+ return MACRO_TYPE_PREFIX_KNOWN_TYPE;
291
+ }
292
+
293
+ static MacroTypePrefix function_macro_macro_type_item(TSLexer *lexer, const MacroTypeState *state, const PragmaTrivia *trivia) {
294
+ MacroGroup group = function_macro_group_prefix(lexer, '(', ')', NULL, NULL, !state->known_type, trivia);
295
+ if (group == MACRO_GROUP_BRACED) return MACRO_TYPE_PREFIX_TYPE;
296
+ if (group == MACRO_GROUP_BALANCED && scan_pragma_spacing_with(lexer, trivia)) return MACRO_TYPE_PREFIX_BALANCED_TYPE;
297
+ return MACRO_TYPE_PREFIX_INVALID;
298
+ }
299
+
289
300
  // code-gauge-allow functionCognitiveComplexity 16 -- Literal prefixes inspect one consumed first argument token without restarting the lexer.
290
301
  static bool function_macro_trailing_argument_prefix(TSLexer *lexer, const PragmaTrivia *trivia) {
291
302
  do {
@@ -604,7 +615,8 @@ static bool function_macro_word_in(const char *word, const char *const *words, u
604
615
  unsigned end = count;
605
616
  while (start < end) {
606
617
  unsigned middle = start + (end - start) / 2;
607
- int order = strcmp(word, words[middle]);
618
+ int order = (unsigned char)word[0] - (unsigned char)words[middle][0];
619
+ if (!order && word[0]) order = strcmp(word + 1, words[middle] + 1);
608
620
  if (order == 0) return true;
609
621
  if (order < 0) end = middle;
610
622
  else start = middle + 1;
package/src/identifier.h CHANGED
@@ -197,4 +197,7 @@ static const TSCharacterRange preproc_identifier_continue[] = {
197
197
  {0x31350, 0x323af}, {0xe0100, 0xe01ef},
198
198
  };
199
199
 
200
+ static const uint64_t preproc_identifier_start_ascii[] = {0x0000001000000000ull, 0x07fffffe97fffffeull};
201
+ static const uint64_t preproc_identifier_continue_ascii[] = {0x03ff001000000000ull, 0x07fffffe97fffffeull};
202
+
200
203
  #endif
package/src/pragma.h CHANGED
@@ -1,4 +1,4 @@
1
- // code-gauge-allow-file fileNcss 484 -- Preprocessor tokens and pragma operators share names, spacing, and literal readers.
1
+ // code-gauge-allow-file fileNcss 489 -- Preprocessor tokens and pragma operators share names, spacing, and literal readers.
2
2
  #ifndef TREE_SITTER_C_PRAGMA_H_
3
3
  #define TREE_SITTER_C_PRAGMA_H_
4
4
 
@@ -57,6 +57,7 @@ static bool scan_preproc_newline(TSLexer *lexer, bool skip);
57
57
  static bool scan_preproc_splices(TSLexer *lexer);
58
58
  static bool scan_preproc_ucn(TSLexer *lexer, bool *in_number);
59
59
  static bool preproc_word(int32_t c, bool continuation);
60
+ static bool preproc_identifier_character(int32_t c, bool continuation);
60
61
  static bool scan_preproc_quote(TSLexer *lexer, int32_t quote, bool escaped, bool directive_text);
61
62
  static bool scan_preproc_raw_quote(TSLexer *lexer);
62
63
 
@@ -401,6 +402,14 @@ static bool scan_preproc_ucn(TSLexer *lexer, bool *in_number) {
401
402
  static bool preproc_word(int32_t c, bool continuation) {
402
403
  if (c == '\\') return false;
403
404
  if (continuation && c >= 0x80 && !pragma_space(c)) return true;
405
+ return preproc_identifier_character(c, continuation);
406
+ }
407
+
408
+ static bool preproc_identifier_character(int32_t c, bool continuation) {
409
+ if (c >= 0 && c < 0x80) {
410
+ const uint64_t *ascii = continuation ? preproc_identifier_continue_ascii : preproc_identifier_start_ascii;
411
+ return (ascii[c / 64] >> (c % 64)) & 1;
412
+ }
404
413
  return continuation
405
414
  ? set_contains(preproc_identifier_continue, sizeof(preproc_identifier_continue) / sizeof(TSCharacterRange), c)
406
415
  : set_contains(preproc_identifier_start, sizeof(preproc_identifier_start) / sizeof(TSCharacterRange), c);
@@ -481,10 +490,7 @@ static MacroNameCharacter scan_macro_name_character(TSLexer *lexer, bool continu
481
490
  name->ascii = false;
482
491
  return scan_macro_name_ucn(lexer) ? MACRO_NAME_CHARACTER : MACRO_NAME_INVALID;
483
492
  }
484
- bool accepted = continuation
485
- ? set_contains(preproc_identifier_continue, sizeof(preproc_identifier_continue) / sizeof(TSCharacterRange), c)
486
- : set_contains(preproc_identifier_start, sizeof(preproc_identifier_start) / sizeof(TSCharacterRange), c);
487
- if (!accepted) return MACRO_NAME_END;
493
+ if (!preproc_identifier_character(c, continuation)) return MACRO_NAME_END;
488
494
  if (name->ascii) {
489
495
  if (c < 128 && name->length < name->capacity) name->word[name->length++] = (char)c;
490
496
  else name->ascii = false;
package/src/scanner.c CHANGED
@@ -11,6 +11,8 @@ enum TokenType { PRAGMA_OPERATOR, PREPROC_ARG, PREPROC_NEWLINE, PREPROC_LPAREN,
11
11
 
12
12
  static EmptyDirectives *scanner_directives(CScanner *scanner);
13
13
 
14
+ static bool only_extras_valid(const EmptyDirectives *state, const bool *valid_symbols);
15
+ static bool scan_extras(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
14
16
  static bool scan_preproc_tokens(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
15
17
  static bool scan_declaration_tokens(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols);
16
18
  static bool scan_directive_context(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
@@ -45,9 +47,11 @@ void tree_sitter_c_external_scanner_deserialize(void *payload, const char *buffe
45
47
  if (state) state->ready = length && buffer[0];
46
48
  }
47
49
 
50
+ // code-gauge-allow functionCognitiveComplexity 16 -- External-token validity dispatches each lexing mode to its scanner.
48
51
  bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
49
52
  CScanner *scanner = payload;
50
53
  EmptyDirectives *state = scanner_directives(scanner);
54
+ if (only_extras_valid(state, valid_symbols)) return scan_extras(state, lexer, valid_symbols);
51
55
  if (scan_directive_context(state, lexer, valid_symbols)) return true;
52
56
  if (empty_directives_at_line_end(state, lexer)) return false;
53
57
  if (valid_symbols[GNU_RAW_STRING_BODY] && !valid_symbols[PREPROC_LPAREN]) {
@@ -74,6 +78,38 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
74
78
  return scan_declaration_tokens(scanner, lexer, valid_symbols);
75
79
  }
76
80
 
81
+ static bool only_extras_valid(const EmptyDirectives *state, const bool *valid_symbols) {
82
+ if (!state || !state->ready) return false;
83
+ for (unsigned i = PREPROC_ARG; i < DIRECTIVE_CONTEXT; i++) {
84
+ if (valid_symbols[i]) return false;
85
+ }
86
+ return !valid_symbols[PREPROC_DIRECTIVE_START];
87
+ }
88
+
89
+ // Decides as the general path does in states where only extras are valid, but queries the position only once the first
90
+ // non-space character shows that a null directive or `_Pragma` may follow, since any position query makes the runtime
91
+ // allocate the token on the heap. A null directive's prefix holds only spaces and splices, and line ends are recorded
92
+ // only at spaces and newlines. The skipped spaces moved the lexer, so the start position is read back through the mark.
93
+ static bool scan_extras(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
94
+ TSLexerCheckpoint start;
95
+ lexer->get_checkpoint(lexer, &start);
96
+ while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
97
+ int32_t c = lexer->lookahead;
98
+ if (c != '#' && c != '\\' && c != '_') return false;
99
+ TSLexerPosition position;
100
+ lexer->mark_end_at(lexer, &start);
101
+ lexer->get_mark_end(lexer, &position);
102
+ if (valid_symbols[PREPROC_EMPTY] && empty_directives_start_at(state, position.bytes)) {
103
+ empty_directives_consume_started(lexer, state, true);
104
+ lexer->mark_end(lexer);
105
+ lexer->result_symbol = PREPROC_EMPTY;
106
+ return true;
107
+ }
108
+ if (!valid_symbols[PRAGMA_OPERATOR] || empty_directives_line_end_at(state, position.bytes)) return false;
109
+ lexer->result_symbol = PRAGMA_OPERATOR;
110
+ return scan_pragma_with(lexer, &(PragmaTrivia){empty_directives_skip, state});
111
+ }
112
+
77
113
  static bool scan_conditional_directive(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
78
114
  if (!empty_directives_at_start(state, lexer)) return false;
79
115
  const ConditionalMacroSymbols symbols = {
Binary file
package/tree-sitter.json CHANGED
@@ -24,7 +24,7 @@
24
24
  }
25
25
  ],
26
26
  "metadata": {
27
- "version": "34.0.0",
27
+ "version": "34.0.2",
28
28
  "license": "MIT",
29
29
  "description": "C grammar for tree-sitter",
30
30
  "authors": [