@willbooster/tree-sitter-c 26.0.0 → 28.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 CHANGED
@@ -70,8 +70,12 @@ The scanner sources include `scanner.c`, `pragma.h`, `identifier.h`, `keywords.h
70
70
 
71
71
  Derived grammars must port the inherited external-token dispatch from `src/scanner.c`.
72
72
  Append `_directive_context` and `preproc_empty` in their inherited external-token slots and retain them as extras.
73
+ Append `_preproc_directive_start` in its inherited slot as a zero-width guard after every directive keyword token,
74
+ including keywords in overridden directive rules. It is not an extra. Retain the phase-two splice handling in
75
+ `preprocessorToken` when overriding keyword spelling.
73
76
  Port the scanner lifecycle functions together with `EmptyDirectives` from `emptyDirectives.h`; the source-boundary
74
77
  facts belong to one full parse and are freed by scanner destruction. Run `scan_directive_context` first,
78
+ emitting `_preproc_directive_start` only at `empty_directives_after_keyword` before null-directive consumption,
75
79
  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
76
80
  `empty_context` so declaration lookahead ignores null directives without moving the opening token's range.
77
81
  Keep preprocessor argument/newline handling ahead of annotation lookahead.
@@ -79,7 +83,8 @@ Port `preproc_argument_mode` selection together with `scan_preproc_arg` so token
79
83
  Append `_gnu_raw_string_body` and `_raw_macro_type_tail` in their inherited enum slots and port their dispatch from `src/scanner.c`.
80
84
  A derived grammar overriding `raw_string_literal` can keep its own body scanner, but must retain the inherited enum slots.
81
85
  Port `_conditional_marker_ifdef`, `_conditional_marker_ifndef`, `_leading_conditional_marker_ifdef`, and
82
- `_leading_conditional_marker_ifndef` with `scan_conditional_macro`. The leading pair checks a following type prefix;
86
+ `_leading_conditional_marker_ifndef` with `scan_conditional_macro`. Require `empty_directives_at_start` at the opening
87
+ `#` before this dispatch, including after skipped whitespace. The leading pair checks a following type prefix;
83
88
  when both contexts are valid, choose the leading role if its type proof succeeds, otherwise use the valid
84
89
  post-type role. Each token consumes only its opening directive keyword; the directive body, closing directive and following prefix are lookahead. Keep this dispatch ahead of
85
90
  the function-annotation guards and return immediately after a failed directive scan.
package/grammar.js CHANGED
@@ -456,6 +456,7 @@ module.exports = Object.assign(
456
456
  sym('_braced_macro_type_start'),
457
457
  sym('_directive_context'),
458
458
  sym('preproc_empty'),
459
+ sym('_preproc_directive_start'),
459
460
  ],
460
461
 
461
462
  extras: ($) => [
@@ -700,7 +701,7 @@ module.exports = Object.assign(
700
701
  preproc_arg: () => token(prec(-1, PREPROC_ARGUMENT)),
701
702
 
702
703
  _preproc_directive_arg: () => token(prec(-2, PREPROC_ARGUMENT)),
703
- preproc_directive: () => preprocessorToken(/[a-zA-Z0-9]\w*/),
704
+ preproc_directive: () => token(seq(PREPROC_PREFIX, /[a-zA-Z0-9]/, repeat(seq(repeat(LINE_SPLICE), /\w/)))),
704
705
 
705
706
  _preproc_expression: ($) =>
706
707
  choice(
@@ -2351,6 +2352,7 @@ function declarationSpecifiers($, braced = false) {
2351
2352
  function preprocCall($, directive, argument) {
2352
2353
  return seq(
2353
2354
  field('directive', directive),
2355
+ sym('_preproc_directive_start'),
2354
2356
  repeat(choice($.comment, $.pragma_operator)),
2355
2357
  optional(seq(field('argument', alias(argument, $.preproc_arg)), repeat(choice($.comment, $.pragma_operator)))),
2356
2358
  sym('_preproc_newline')
@@ -2727,15 +2729,18 @@ function preprocIf(suffix, content, precedence = 0, repeatContent = true) {
2727
2729
  *
2728
2730
  * @param {string} command
2729
2731
  *
2730
- * @returns {import('./types/treeSitterDsl').AliasRule}
2732
+ * @returns {import('./types/treeSitterDsl').SeqRule}
2731
2733
  */
2732
2734
  function preprocessor(command) {
2733
- return alias(preprocessorToken(command), '#' + command);
2735
+ return seq(alias(preprocessorToken(command), '#' + command), sym('_preproc_directive_start'));
2734
2736
  }
2735
2737
 
2736
- /** @param {string | RegExp} command */
2738
+ /** @param {string} command */
2737
2739
  function preprocessorToken(command) {
2738
- return token(seq(PREPROC_PREFIX, command));
2740
+ const spelling = Array.from(command, (character, index) =>
2741
+ index === 0 ? character : seq(repeat(LINE_SPLICE), character)
2742
+ );
2743
+ return token(seq(PREPROC_PREFIX, ...spelling));
2739
2744
  }
2740
2745
 
2741
2746
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-c",
3
- "version": "26.0.0",
3
+ "version": "28.0.0",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "c",
@@ -224,9 +224,7 @@ static bool conditional_macro_word(TSLexer *lexer, MacroName *name, bool escaped
224
224
  for (;;) {
225
225
  MacroNameCharacter character = scan_macro_name_character(lexer, has_name, name);
226
226
  if (character == MACRO_NAME_END) return has_name;
227
- if (character == MACRO_NAME_SPLICE) {
228
- return has_name && lexer->lookahead != '\\' && !preproc_word(lexer->lookahead, true);
229
- }
227
+ if (character == MACRO_NAME_SPLICE) continue;
230
228
  if (character != MACRO_NAME_CHARACTER) return false;
231
229
  has_name = true;
232
230
  }
@@ -1,3 +1,4 @@
1
+ // code-gauge-allow-file fileNcss 510 -- One source-fact pass shares compact character storage, directive lexing and physical-position conversion.
1
2
  #ifndef TREE_SITTER_C_EMPTY_DIRECTIVES_H_
2
3
  #define TREE_SITTER_C_EMPTY_DIRECTIVES_H_
3
4
 
@@ -5,18 +6,20 @@
5
6
  #include <stdlib.h>
6
7
 
7
8
  typedef struct {
8
- int32_t value;
9
- uint32_t byte;
10
- } DirectiveCharacter;
9
+ uint32_t index, byte, width;
10
+ } DirectivePosition;
11
11
 
12
12
  typedef struct {
13
- DirectiveCharacter *characters;
13
+ uint16_t **characters;
14
+ DirectivePosition *positions;
15
+ uint32_t position_count, position_capacity, position_index, previous_byte, before_previous_byte;
14
16
  uint8_t *prefix_barriers;
15
- uint32_t length, index, end;
17
+ uint32_t length, index, byte, end, page_count;
18
+ bool utf16, encoding_known;
16
19
  } DirectiveInput;
17
20
 
18
21
  typedef struct {
19
- uint8_t *starts, *ends, *line_ends;
22
+ uint8_t *starts, *ends, *line_ends, *directive_starts, *directive_ends;
20
23
  uint32_t bytes;
21
24
  bool built, ready;
22
25
  } EmptyDirectives;
@@ -32,6 +35,14 @@ typedef struct {
32
35
 
33
36
  static void directive_prefixes(EmptyDirectives *state, DirectiveInput *input);
34
37
  static bool directive_append(DirectiveInput *input, uint32_t *capacity, TSLexer *lexer);
38
+ static bool directive_reserve(DirectiveInput *input, uint32_t *capacity, unsigned width);
39
+ static bool directive_position(DirectiveInput *input, uint32_t index, uint32_t byte, uint32_t width);
40
+ static void directive_encode(DirectiveInput *input, uint32_t c);
41
+ static uint16_t directive_character(const DirectiveInput *input, uint32_t index);
42
+ static void directive_store(DirectiveInput *input, uint16_t c);
43
+ static void directive_release(DirectiveInput *input);
44
+ static unsigned directive_utf8_width(uint32_t c);
45
+ static void directive_barrier(DirectiveInput *input);
35
46
  static void directive_line_end(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
36
47
  static void directive_line_character(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
37
48
  static void directive_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
@@ -44,6 +55,7 @@ static uint32_t directive_splice_end(const DirectiveInput *input);
44
55
  static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer);
45
56
  static void empty_directives_find(EmptyDirectives *state, DirectiveInput *input);
46
57
  static int32_t directive_next(DirectiveInput *input);
58
+ static int32_t directive_physical_next(DirectiveInput *input);
47
59
  static int32_t directive_peek(const DirectiveInput *input);
48
60
  static bool directive_comment(DirectiveInput *input, bool *line);
49
61
  static bool directive_quoted(DirectiveInput *input, int32_t quote);
@@ -54,6 +66,7 @@ typedef struct {
54
66
  unsigned length;
55
67
  } DirectiveWord;
56
68
 
69
+ static uint32_t directive_keyword_end(const DirectiveInput *input);
57
70
  static DirectiveKind directive_word(DirectiveInput *input, int32_t first, bool raw);
58
71
  static DirectiveKind directive_word_kind(const DirectiveWord *word);
59
72
  static void directive_word_character(DirectiveWord *word, int32_t c);
@@ -75,6 +88,20 @@ static bool empty_directives_initialize(EmptyDirectives *state, TSLexer *lexer)
75
88
  return true;
76
89
  }
77
90
 
91
+ static bool empty_directives_after_keyword(const EmptyDirectives *state, TSLexer *lexer) {
92
+ if (!state || !state->directive_ends) return false;
93
+ TSLexerPosition position;
94
+ lexer->get_position(lexer, &position);
95
+ return position.bytes <= state->bytes && (state->directive_ends[position.bytes / 8] & (1u << (position.bytes % 8)));
96
+ }
97
+
98
+ static bool empty_directives_at_start(const EmptyDirectives *state, TSLexer *lexer) {
99
+ if (!state || !state->directive_starts) return false;
100
+ TSLexerPosition position;
101
+ lexer->get_position(lexer, &position);
102
+ return position.bytes < state->bytes && (state->directive_starts[position.bytes / 8] & (1u << (position.bytes % 8)));
103
+ }
104
+
78
105
  static bool empty_directives_at_line_end(const EmptyDirectives *state, TSLexer *lexer) {
79
106
  if (!state || !state->line_ends) return false;
80
107
  TSLexerPosition position;
@@ -102,12 +129,14 @@ static bool empty_directives_consume(TSLexer *lexer, EmptyDirectives *state, boo
102
129
  return true;
103
130
  }
104
131
 
132
+ // code-gauge-allow functionNcss 33 -- The source-fact buffers are allocated and released together so initialization fails atomically.
105
133
  static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
106
- DirectiveInput input = {0};
134
+ TSLexerPosition start;
135
+ lexer->get_position(lexer, &start);
136
+ DirectiveInput input = {.byte = start.bytes};
107
137
  uint32_t capacity = 0;
108
138
  while (!lexer->eof(lexer)) {
109
139
  if (!directive_append(&input, &capacity, lexer)) goto failed;
110
- lexer->advance(lexer, false);
111
140
  }
112
141
  TSLexerPosition end;
113
142
  lexer->get_position(lexer, &end);
@@ -115,50 +144,135 @@ static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
115
144
  state->bytes = end.bytes;
116
145
  state->starts = calloc((size_t)state->bytes / 8 + 1, 1);
117
146
  state->ends = calloc((size_t)state->bytes / 8 + 1, 1);
147
+ state->directive_starts = calloc((size_t)state->bytes / 8 + 1, 1);
148
+ state->directive_ends = calloc((size_t)state->bytes / 8 + 1, 1);
118
149
  input.prefix_barriers = calloc((size_t)state->bytes / 8 + 1, 1);
119
- if (!state->starts || !state->ends || !input.prefix_barriers) goto failed;
150
+ if (!state->starts || !state->ends || !state->directive_starts || !state->directive_ends || !input.prefix_barriers) goto failed;
120
151
  empty_directives_find(state, &input);
121
152
  directive_prefixes(state, &input);
122
153
  state->line_ends = input.prefix_barriers;
123
- free(input.characters);
154
+ directive_release(&input);
124
155
  return true;
125
156
  failed:
126
157
  free(input.prefix_barriers);
127
- free(input.characters);
158
+ directive_release(&input);
128
159
  free(state->starts);
129
160
  free(state->ends);
130
- state->starts = state->ends = NULL;
161
+ free(state->directive_starts);
162
+ free(state->directive_ends);
163
+ state->starts = state->ends = state->directive_starts = state->directive_ends = NULL;
131
164
  return false;
132
165
  }
133
166
 
167
+ // code-gauge-allow functionCognitiveComplexity 24 -- The reverse lexical pass applies sparse physical-position corrections while carrying null-prefix and line-end facts.
134
168
  static void directive_prefixes(EmptyDirectives *state, DirectiveInput *input) {
135
169
  bool prefix = false, line_end = false;
136
- for (uint32_t i = input->length; i > 0; i--) {
137
- DirectiveCharacter character = input->characters[i - 1];
138
- bool hash = character.value == '#' && (state->starts[character.byte / 8] & (1u << (character.byte % 8)));
139
- bool condition_end = input->prefix_barriers[character.byte / 8] & (1u << (character.byte % 8));
140
- line_end = condition_end || (line_end && directive_horizontal(character.value));
141
- if (line_end) input->prefix_barriers[character.byte / 8] |= 1u << (character.byte % 8);
142
- prefix = !condition_end && (hash || (prefix && (pragma_space(character.value) || character.value == '\\')));
143
- if (prefix) state->starts[character.byte / 8] |= 1u << (character.byte % 8);
170
+ uint32_t byte = input->end, position = input->position_count;
171
+ for (uint32_t i = input->length; i > 0;) {
172
+ i--;
173
+ if (i && directive_character(input, i) >= 0xdc00 && directive_character(input, i) <= 0xdfff &&
174
+ directive_character(input, i - 1) >= 0xd800 && directive_character(input, i - 1) <= 0xdbff) continue;
175
+ DirectiveInput character = *input;
176
+ character.index = i;
177
+ character.byte = 0;
178
+ character.position_count = 0;
179
+ int32_t c = directive_physical_next(&character);
180
+ if (position && input->positions[position - 1].index == i) byte = input->positions[--position].byte;
181
+ else {
182
+ if (character.byte > byte) return;
183
+ byte -= character.byte;
184
+ }
185
+ if (byte > state->bytes) return;
186
+ bool hash = c == '#' && (state->starts[byte / 8] & (1u << (byte % 8)));
187
+ bool condition_end = input->prefix_barriers[byte / 8] & (1u << (byte % 8));
188
+ line_end = condition_end || (line_end && directive_horizontal(c));
189
+ if (line_end) input->prefix_barriers[byte / 8] |= 1u << (byte % 8);
190
+ prefix = !condition_end && (hash || (prefix && (pragma_space(c) || c == '\\')));
191
+ if (prefix) state->starts[byte / 8] |= 1u << (byte % 8);
144
192
  }
145
193
  }
146
194
 
147
195
  static bool directive_append(DirectiveInput *input, uint32_t *capacity, TSLexer *lexer) {
148
- if (input->length == *capacity) {
149
- uint32_t next = *capacity ? *capacity * 2 : 1024;
196
+ uint32_t c = lexer->lookahead, index = input->length;
197
+ unsigned width = directive_utf8_width(c), utf16_width = c > 0xffff && c <= 0x10ffff ? 4 : 2;
198
+ if (!directive_reserve(input, capacity, c > 0xffff && c <= 0x10ffff ? 2 : 1)) return false;
199
+ directive_encode(input, c);
200
+ TSLexerPosition start, end;
201
+ lexer->get_position(lexer, &start);
202
+ lexer->advance(lexer, false);
203
+ lexer->get_position(lexer, &end);
204
+ uint32_t measured = end.bytes - start.bytes;
205
+ if (!input->encoding_known && c <= 0x10ffff && width != utf16_width && !lexer->is_at_included_range_start(lexer)) {
206
+ input->utf16 = measured == utf16_width;
207
+ input->encoding_known = true;
208
+ }
209
+ if (c > 0x10ffff || measured != (input->utf16 ? utf16_width : width)) {
210
+ return directive_position(input, index, start.bytes, measured);
211
+ }
212
+ return true;
213
+ }
214
+
215
+ static bool directive_position(DirectiveInput *input, uint32_t index, uint32_t byte, uint32_t width) {
216
+ if (input->position_count == input->position_capacity) {
217
+ uint32_t capacity = input->position_capacity ? input->position_capacity * 2 : 16;
218
+ if (capacity < input->position_capacity || (size_t)capacity > SIZE_MAX / sizeof(*input->positions)) return false;
219
+ void *positions = realloc(input->positions, (size_t)capacity * sizeof(*input->positions));
220
+ if (!positions) return false;
221
+ input->positions = positions;
222
+ input->position_capacity = capacity;
223
+ }
224
+ input->positions[input->position_count++] = (DirectivePosition){index, byte, width};
225
+ return true;
226
+ }
227
+
228
+ static bool directive_reserve(DirectiveInput *input, uint32_t *capacity, unsigned width) {
229
+ if (input->length > UINT32_MAX - width) return false;
230
+ uint32_t last = (input->length + width - 1) >> 16;
231
+ if (last >= *capacity) {
232
+ uint32_t next = *capacity ? *capacity * 2 : 16;
150
233
  if (next < *capacity || (size_t)next > SIZE_MAX / sizeof(*input->characters)) return false;
151
234
  void *characters = realloc(input->characters, (size_t)next * sizeof(*input->characters));
152
235
  if (!characters) return false;
153
236
  input->characters = characters;
154
237
  *capacity = next;
155
238
  }
156
- TSLexerPosition position;
157
- lexer->get_position(lexer, &position);
158
- input->characters[input->length++] = (DirectiveCharacter){lexer->lookahead, position.bytes};
239
+ while (input->page_count <= last) {
240
+ uint16_t *page = malloc(((size_t)1 << 16) * sizeof(*page));
241
+ if (!page) return false;
242
+ input->characters[input->page_count++] = page;
243
+ }
159
244
  return true;
160
245
  }
161
246
 
247
+ static void directive_encode(DirectiveInput *input, uint32_t c) {
248
+ if (c > 0x10ffff) directive_store(input, 0xdfff);
249
+ else if (c <= 0xffff) directive_store(input, c);
250
+ else {
251
+ c -= 0x10000;
252
+ directive_store(input, 0xd800 | (c >> 10));
253
+ directive_store(input, 0xdc00 | (c & 0x3ff));
254
+ }
255
+ }
256
+
257
+ static uint16_t directive_character(const DirectiveInput *input, uint32_t index) {
258
+ return input->characters[index >> 16][index & 0xffff];
259
+ }
260
+
261
+ static void directive_store(DirectiveInput *input, uint16_t c) {
262
+ input->characters[input->length >> 16][input->length & 0xffff] = c;
263
+ input->length++;
264
+ }
265
+
266
+ static void directive_release(DirectiveInput *input) {
267
+ for (uint32_t i = 0; i < input->page_count; i++) free(input->characters[i]);
268
+ free(input->characters);
269
+ free(input->positions);
270
+ }
271
+
272
+ static unsigned directive_utf8_width(uint32_t c) {
273
+ return c < 0x80 || c > 0x10ffff ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
274
+ }
275
+
162
276
  static void empty_directives_find(EmptyDirectives *state, DirectiveInput *input) {
163
277
  DirectiveLine line = {.beginning = true};
164
278
  while (input->index < input->length) {
@@ -184,7 +298,8 @@ static void directive_line_character(EmptyDirectives *state, DirectiveInput *inp
184
298
  line->pragma_depth = 0;
185
299
  line->kind = DIRECTIVE_ORDINARY;
186
300
  line->operand = false;
187
- line->start = input->characters[input->index - 1].byte;
301
+ line->start = input->previous_byte;
302
+ state->directive_starts[line->start / 8] |= 1u << (line->start % 8);
188
303
  line->beginning = false;
189
304
  line->pending = true;
190
305
  return;
@@ -192,6 +307,7 @@ static void directive_line_character(EmptyDirectives *state, DirectiveInput *inp
192
307
  directive_code(state, input, line, c);
193
308
  }
194
309
 
310
+ // code-gauge-allow functionCognitiveComplexity 17 -- Header names, quoted tokens and distinct keyword boundaries update one logical directive's lexical state.
195
311
  static void directive_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c) {
196
312
  bool name = line->pending;
197
313
  bool header = directive_header_ready(line, c);
@@ -201,12 +317,19 @@ static void directive_code(EmptyDirectives *state, DirectiveInput *input, Direct
201
317
  line->beginning = line->pending = false;
202
318
  if (header) {
203
319
  if (directive_header(input, c == '<' ? '>' : '"')) directive_line_end(state, input, line);
204
- } else if (c == '"' || c == '\'') {
320
+ return;
321
+ }
322
+ if (c == '"' || c == '\'') {
205
323
  if (directive_quoted(input, c)) directive_line_end(state, input, line);
206
- } else if (preproc_word(c, false) || (c >= '0' && c <= '9')) {
207
- DirectiveKind kind = directive_word(input, c, line->kind != DIRECTIVE_DIAGNOSTIC);
208
- directive_word_role(line, kind, name, pragma, operand, preproc_word(c, false));
324
+ return;
325
+ }
326
+ if (!preproc_word(c, false) && !(c >= '0' && c <= '9')) return;
327
+ uint32_t end = name ? directive_keyword_end(input) : 0;
328
+ DirectiveKind kind = directive_word(input, c, line->kind != DIRECTIVE_DIAGNOSTIC);
329
+ if (name) {
330
+ state->directive_ends[end / 8] |= 1u << (end % 8);
209
331
  }
332
+ directive_word_role(line, kind, name, pragma, operand, preproc_word(c, false));
210
333
  }
211
334
 
212
335
  static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool name, bool pragma, bool had_operand, bool identifier) {
@@ -276,17 +399,7 @@ static void directive_line_end(EmptyDirectives *state, DirectiveInput *input, Di
276
399
  line->pending = false;
277
400
  bool required = line->kind == DIRECTIVE_CONDITION ||
278
401
  ((line->kind == DIRECTIVE_REQUIRED || line->kind == DIRECTIVE_HEADER) && !line->operand && !line->pragma_pending);
279
- if (required && input->index &&
280
- directive_newline(input->characters[input->index - 1].value)) {
281
- uint32_t byte = input->characters[input->index - 1].byte;
282
- input->prefix_barriers[byte / 8] |= 1u << (byte % 8);
283
- if (input->index > 1) {
284
- DirectiveCharacter previous = input->characters[input->index - 2];
285
- if (directive_newline(previous.value) && previous.value != input->characters[input->index - 1].value) {
286
- input->prefix_barriers[previous.byte / 8] |= 1u << (previous.byte % 8);
287
- }
288
- }
289
- }
402
+ if (required) directive_barrier(input);
290
403
  line->header = 0;
291
404
  if (line->pragma_pending) line->pragma_newline = true;
292
405
  if (!line->pragma_depth && !line->pragma_pending) {
@@ -295,19 +408,29 @@ static void directive_line_end(EmptyDirectives *state, DirectiveInput *input, Di
295
408
  }
296
409
  }
297
410
 
411
+ static void directive_barrier(DirectiveInput *input) {
412
+ if (!input->index || !directive_newline(directive_character(input, input->index - 1))) return;
413
+ uint32_t byte = input->previous_byte;
414
+ input->prefix_barriers[byte / 8] |= 1u << (byte % 8);
415
+ if (input->index < 2) return;
416
+ int32_t previous = directive_character(input, input->index - 2);
417
+ if (directive_newline(previous) && previous != directive_character(input, input->index - 1)) {
418
+ byte = input->before_previous_byte;
419
+ input->prefix_barriers[byte / 8] |= 1u << (byte % 8);
420
+ }
421
+ }
422
+
298
423
  static int32_t directive_next(DirectiveInput *input) {
299
424
  while (input->index < input->length) {
300
- int32_t c = input->characters[input->index++].value;
301
- if (c == '\\') {
302
- uint32_t after = directive_splice_end(input);
303
- if (after != input->index) {
304
- input->index = after;
305
- continue;
306
- }
425
+ int32_t c = directive_physical_next(input);
426
+ uint32_t after = c == '\\' ? directive_splice_end(input) : input->index;
427
+ if (after != input->index) {
428
+ while (input->index < after) directive_physical_next(input);
429
+ continue;
307
430
  }
308
431
  if (directive_newline(c)) {
309
- if (input->index < input->length && directive_newline(input->characters[input->index].value) &&
310
- input->characters[input->index].value != c) input->index++;
432
+ if (input->index < input->length && directive_newline(directive_character(input, input->index)) &&
433
+ directive_character(input, input->index) != c) directive_physical_next(input);
311
434
  return '\n';
312
435
  }
313
436
  return c;
@@ -315,13 +438,30 @@ static int32_t directive_next(DirectiveInput *input) {
315
438
  return -1;
316
439
  }
317
440
 
441
+ static int32_t directive_physical_next(DirectiveInput *input) {
442
+ uint32_t index = input->index, c = directive_character(input, input->index++);
443
+ bool invalid = c == 0xdfff;
444
+ if (c >= 0xd800 && c <= 0xdbff && input->index < input->length &&
445
+ directive_character(input, input->index) >= 0xdc00 && directive_character(input, input->index) <= 0xdfff) {
446
+ c = 0x10000 + ((c - 0xd800) << 10) + (directive_character(input, input->index++) - 0xdc00);
447
+ }
448
+ input->before_previous_byte = input->previous_byte;
449
+ input->previous_byte = input->byte;
450
+ if (input->position_index < input->position_count && input->positions[input->position_index].index == index) {
451
+ DirectivePosition position = input->positions[input->position_index++];
452
+ input->previous_byte = position.byte;
453
+ input->byte = position.byte + position.width;
454
+ } else input->byte += input->utf16 ? (c > 0xffff ? 4 : 2) : invalid ? 1 : directive_utf8_width(c);
455
+ return invalid ? 0xfffd : c;
456
+ }
457
+
318
458
  static uint32_t directive_splice_end(const DirectiveInput *input) {
319
459
  uint32_t after = input->index;
320
- while (after < input->length && directive_horizontal(input->characters[after].value)) after++;
321
- if (after == input->length || !directive_newline(input->characters[after].value)) return input->index;
322
- int32_t newline = input->characters[after++].value;
323
- if (after < input->length && directive_newline(input->characters[after].value) &&
324
- input->characters[after].value != newline) after++;
460
+ while (after < input->length && directive_horizontal(directive_character(input, after))) after++;
461
+ if (after == input->length || !directive_newline(directive_character(input, after))) return input->index;
462
+ int32_t newline = directive_character(input, after++);
463
+ if (after < input->length && directive_newline(directive_character(input, after)) &&
464
+ directive_character(input, after) != newline) after++;
325
465
  return after;
326
466
  }
327
467
 
@@ -353,6 +493,14 @@ static bool directive_quoted(DirectiveInput *input, int32_t quote) {
353
493
  return false;
354
494
  }
355
495
 
496
+ static uint32_t directive_keyword_end(const DirectiveInput *input) {
497
+ DirectiveInput name = *input;
498
+ int32_t c;
499
+ while ((c = directive_peek(&name)) == '_' || (c >= 'a' && c <= 'z') ||
500
+ (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) directive_next(&name);
501
+ return directive_byte(&name);
502
+ }
503
+
356
504
  static DirectiveKind directive_word(DirectiveInput *input, int32_t first, bool raw) {
357
505
  DirectiveWord word = {0};
358
506
  directive_word_character(&word, first);
@@ -406,12 +554,13 @@ static bool directive_raw(DirectiveInput *input) {
406
554
  delimiter[length++] = c;
407
555
  }
408
556
  while (candidate.index < candidate.length) {
409
- uint32_t index = candidate.index++;
410
- if (candidate.characters[index].value != ')' || candidate.length - index < length + 2) continue;
557
+ if (directive_physical_next(&candidate) != ')') continue;
558
+ DirectiveInput closing = candidate;
411
559
  unsigned matched = 0;
412
- while (matched < length && candidate.characters[index + 1 + matched].value == delimiter[matched]) matched++;
413
- if (matched == length && candidate.characters[index + 1 + length].value == '"') {
414
- candidate.index = index + length + 2;
560
+ while (matched < length && closing.index < closing.length &&
561
+ directive_physical_next(&closing) == delimiter[matched]) matched++;
562
+ if (matched == length && closing.index < closing.length && directive_physical_next(&closing) == '"') {
563
+ candidate = closing;
415
564
  break;
416
565
  }
417
566
  }
@@ -428,7 +577,7 @@ static bool directive_newline(int32_t c) {
428
577
  }
429
578
 
430
579
  static uint32_t directive_byte(const DirectiveInput *input) {
431
- return input->index < input->length ? input->characters[input->index].byte : input->end;
580
+ return input->byte;
432
581
  }
433
582
 
434
583
  static void directive_record(EmptyDirectives *state, uint32_t start, uint32_t end) {
package/src/pragma.h CHANGED
@@ -1,4 +1,4 @@
1
- // code-gauge-allow-file fileNcss 482 -- Preprocessor tokens and pragma operators share names, spacing, and literal readers.
1
+ // code-gauge-allow-file fileNcss 484 -- 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
 
@@ -146,6 +146,7 @@ static bool scan_pragma_spacing(TSLexer *lexer) {
146
146
 
147
147
  static bool scan_pragma_comment(TSLexer *lexer) {
148
148
  lexer->advance(lexer, false);
149
+ if (!scan_preproc_splices(lexer)) return false;
149
150
  if (lexer->lookahead == '/') return scan_preproc_line_comment(lexer);
150
151
  if (lexer->lookahead == '*') return scan_preproc_block_comment(lexer);
151
152
  return false;
package/src/scanner.c CHANGED
@@ -7,13 +7,14 @@ typedef struct {
7
7
  MacroProofMemo proof_memo;
8
8
  } CScanner;
9
9
 
10
- enum TokenType { PRAGMA_OPERATOR, PREPROC_ARG, PREPROC_NEWLINE, PREPROC_LPAREN, PREPROC_DIRECTIVE_ARG, PREPROC_FUNCTION_NAME, FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX, TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START, CONDITIONAL_MARKER_IFDEF, CONDITIONAL_MARKER_IFNDEF, LEADING_CONDITIONAL_MARKER_IFDEF, LEADING_CONDITIONAL_MARKER_IFNDEF, GNU_RAW_STRING_BODY, RAW_MACRO_TYPE_TAIL, BRACED_FUNCTION_ATTRIBUTE_START, BRACED_MACRO_TYPE_START, DIRECTIVE_CONTEXT, PREPROC_EMPTY };
10
+ enum TokenType { PRAGMA_OPERATOR, PREPROC_ARG, PREPROC_NEWLINE, PREPROC_LPAREN, PREPROC_DIRECTIVE_ARG, PREPROC_FUNCTION_NAME, FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX, TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START, CONDITIONAL_MARKER_IFDEF, CONDITIONAL_MARKER_IFNDEF, LEADING_CONDITIONAL_MARKER_IFDEF, LEADING_CONDITIONAL_MARKER_IFNDEF, GNU_RAW_STRING_BODY, RAW_MACRO_TYPE_TAIL, BRACED_FUNCTION_ATTRIBUTE_START, BRACED_MACRO_TYPE_START, DIRECTIVE_CONTEXT, PREPROC_EMPTY, PREPROC_DIRECTIVE_START };
11
11
 
12
12
  static EmptyDirectives *scanner_directives(CScanner *scanner);
13
13
 
14
14
  static bool scan_preproc_tokens(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
15
15
  static bool scan_declaration_tokens(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols);
16
16
  static bool scan_directive_context(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
17
+ static bool scan_conditional_directive(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
17
18
 
18
19
  void *tree_sitter_c_external_scanner_create(void) {
19
20
  return calloc(1, sizeof(CScanner));
@@ -26,6 +27,8 @@ void tree_sitter_c_external_scanner_destroy(void *payload) {
26
27
  free(state->starts);
27
28
  free(state->ends);
28
29
  free(state->line_ends);
30
+ free(state->directive_starts);
31
+ free(state->directive_ends);
29
32
  macro_proof_destroy(&scanner->proof_memo);
30
33
  free(scanner);
31
34
  }
@@ -67,13 +70,18 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
67
70
  bool conditional = leading || valid_symbols[CONDITIONAL_MARKER_IFDEF] || valid_symbols[CONDITIONAL_MARKER_IFNDEF];
68
71
  if (!conditional) return scan_declaration_tokens(scanner, lexer, valid_symbols);
69
72
  while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
73
+ if (lexer->lookahead == '#') return scan_conditional_directive(state, lexer, valid_symbols);
74
+ return scan_declaration_tokens(scanner, lexer, valid_symbols);
75
+ }
76
+
77
+ static bool scan_conditional_directive(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
78
+ if (!empty_directives_at_start(state, lexer)) return false;
70
79
  const ConditionalMacroSymbols symbols = {
71
80
  .ifdef = CONDITIONAL_MARKER_IFDEF, .ifndef = CONDITIONAL_MARKER_IFNDEF,
72
81
  .leading_ifdef = LEADING_CONDITIONAL_MARKER_IFDEF, .leading_ifndef = LEADING_CONDITIONAL_MARKER_IFNDEF,
73
82
  .skip_empty = empty_directives_skip, .empty_context = state,
74
83
  };
75
- if (lexer->lookahead == '#') return scan_conditional_macro(lexer, &symbols, valid_symbols);
76
- return scan_declaration_tokens(scanner, lexer, valid_symbols);
84
+ return scan_conditional_macro(lexer, &symbols, valid_symbols);
77
85
  }
78
86
 
79
87
  static bool scan_directive_context(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
@@ -81,6 +89,11 @@ static bool scan_directive_context(EmptyDirectives *state, TSLexer *lexer, const
81
89
  lexer->result_symbol = DIRECTIVE_CONTEXT;
82
90
  return empty_directives_initialize(state, lexer);
83
91
  }
92
+ if (valid_symbols[PREPROC_DIRECTIVE_START] && empty_directives_after_keyword(state, lexer)) {
93
+ lexer->result_symbol = PREPROC_DIRECTIVE_START;
94
+ lexer->mark_end(lexer);
95
+ return true;
96
+ }
84
97
  bool directive_body = valid_symbols[PREPROC_NEWLINE] || valid_symbols[PREPROC_LPAREN] ||
85
98
  valid_symbols[PREPROC_ARG] || valid_symbols[PREPROC_DIRECTIVE_ARG] || valid_symbols[GNU_RAW_STRING_BODY];
86
99
  bool macro_name = valid_symbols[PREPROC_FUNCTION_NAME] && !valid_symbols[PREPROC_LPAREN];
Binary file
package/tree-sitter.json CHANGED
@@ -24,7 +24,7 @@
24
24
  }
25
25
  ],
26
26
  "metadata": {
27
- "version": "26.0.0",
27
+ "version": "28.0.0",
28
28
  "license": "MIT",
29
29
  "description": "C grammar for tree-sitter",
30
30
  "authors": [