@willbooster/tree-sitter-c 40.0.3 → 40.0.5

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
@@ -66,7 +66,7 @@ parser.setLanguage(await Language.load(c));
66
66
  The package also ships `grammar.js`, scanner sources under `src/`,
67
67
  and the node types in `src/node-types.json` for grammars extending C (such as C++).
68
68
  The scanner sources include `scanner.c`, `pragma.h`, `identifier.h`, `keywords.h`, `functionMacro.h`,
69
- `conditionalMacro.h`, `emptyDirectives.h`, `macroProof.h`, and `gnuRawString.h`.
69
+ `conditionalMacro.h`, `emptyDirectives.h`, `directiveScan.h`, `macroProof.h`, and `gnuRawString.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.
@@ -74,7 +74,7 @@ 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. When the scanner is ready and no external token
77
+ facts belong to one full parse and are freed by `empty_directives_release` in scanner destruction. When the scanner is ready and no external token
78
78
  other than `pragma_operator`, `_directive_context` and `preproc_empty` is valid, dispatch to `scan_extras` first;
79
79
  `only_extras_valid` checks the inherited slots only, so extend it to every external token the derived grammar adds,
80
80
  or the fast path drops those tokens. Otherwise run `scan_directive_context` first,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-c",
3
- "version": "40.0.3",
3
+ "version": "40.0.5",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "c",
@@ -27,6 +27,7 @@
27
27
  "src/macroProof.h",
28
28
  "src/conditionalMacro.h",
29
29
  "src/emptyDirectives.h",
30
+ "src/directiveScan.h",
30
31
  "src/gnuRawString.h",
31
32
  "src/scanner.c"
32
33
  ],
@@ -58,7 +59,7 @@
58
59
  "@types/bun": "1.4.4",
59
60
  "@vitest/browser-playwright": "5.1.0",
60
61
  "@willbooster/oxlint-config": "1.4.9",
61
- "@willbooster/wb": "23.5.0",
62
+ "@willbooster/wb": "23.6.1",
62
63
  "@willbooster/web-tree-sitter": "23.0.2",
63
64
  "conventional-changelog-conventionalcommits": "10.4.1",
64
65
  "lefthook": "2.2.2",
@@ -0,0 +1,397 @@
1
+ #ifndef TREE_SITTER_C_DIRECTIVE_SCAN_H_
2
+ #define TREE_SITTER_C_DIRECTIVE_SCAN_H_
3
+
4
+ #include "pragma.h"
5
+ #include <stdlib.h>
6
+
7
+ // Bytes from `start` to before `end` that hold contiguous characters, and possibly bytes that an included range leaves
8
+ // out after them, which the lexer never reads.
9
+ typedef struct {
10
+ uint32_t start, end;
11
+ } DirectiveSpan;
12
+
13
+ // A run of characters in byte order, split where included ranges leave bytes out.
14
+ typedef struct {
15
+ DirectiveSpan *items;
16
+ uint32_t count, capacity;
17
+ bool failed;
18
+ } DirectiveSpans;
19
+
20
+ // The bytes a stored character contributes to a span at most: a span needs only its characters' starts, and the position
21
+ // after a character also includes the bytes an included range leaves out after it.
22
+ #define DIRECTIVE_CHARACTER_BYTES 4
23
+
24
+ typedef enum { SCAN_CODE, SCAN_SLASH, SCAN_LINE_COMMENT, SCAN_BLOCK_COMMENT, SCAN_QUOTE, SCAN_WORD, SCAN_RAW_DELIMITER, SCAN_RAW_BODY } DirectiveScanMode;
25
+
26
+ // The line-state pass of `emptyDirectives.h` in the state it has on ordinary code, before the first directive and after
27
+ // each directive line that ends with no directive state left, fed one physical character at a time so that the code is
28
+ // never stored. It also tracks the run of spaces and backslashes before the next character, which a null directive's
29
+ // prefix covers: the first `run_spans` spans in `run` and the current one, from `run_start` to before `run_end`.
30
+ typedef struct {
31
+ DirectiveScanMode mode;
32
+ GnuRawStringPrefix raw;
33
+ DirectiveSpans *run;
34
+ int32_t quote, previous, pair;
35
+ uint32_t spaces, run_start, run_end, run_spans;
36
+ unsigned delimiter_length, closing_length, closing_end;
37
+ char delimiter[16], closing[18];
38
+ bool beginning, number, escape, star, splice, in_run;
39
+ } DirectiveScan;
40
+
41
+ static inline bool directive_scan_plain(DirectiveScan *scan, TSLexer *lexer);
42
+ static inline bool directive_scan_plain_word(DirectiveScan *scan, TSLexer *lexer, int32_t c);
43
+ static inline bool directive_scan_plain_code(DirectiveScan *scan, TSLexer *lexer, int32_t c);
44
+ static inline bool directive_scan_plain_text(DirectiveScan *scan, TSLexer *lexer, int32_t c);
45
+ static inline void directive_scan_lexer_run(DirectiveScan *scan, TSLexer *lexer, bool member);
46
+ static inline bool directive_scan_ascii_word(int32_t c);
47
+ static inline bool directive_scan_ascii_special(int32_t c);
48
+ static bool directive_scan_pending(DirectiveScan *scan, int32_t c);
49
+ static bool directive_scan_splice(DirectiveScan *scan, int32_t c);
50
+ static bool directive_scan_logical(DirectiveScan *scan, int32_t c);
51
+ static bool directive_scan_slash(DirectiveScan *scan, int32_t c);
52
+ static void directive_scan_quote(DirectiveScan *scan, int32_t c);
53
+ static bool directive_scan_code(DirectiveScan *scan, int32_t c);
54
+ static void directive_scan_word_start(DirectiveScan *scan, int32_t c);
55
+ static bool directive_scan_word(DirectiveScan *scan, int32_t c);
56
+ static bool directive_scan_raw_delimiter(DirectiveScan *scan, int32_t c);
57
+ static void directive_scan_raw_body(DirectiveScan *scan, int32_t c);
58
+ static bool directive_horizontal(int32_t c);
59
+ static bool directive_newline(int32_t c);
60
+ static void directive_spans_push(DirectiveSpans *spans, uint32_t start, uint32_t end);
61
+ static void directive_scan_close_run(DirectiveScan *scan, uint32_t limit, bool skipped);
62
+ static void directive_scan_push(DirectiveScan *scan, uint32_t start, uint32_t end);
63
+ static inline void directive_scan_extend(DirectiveScan *scan, uint32_t start, bool skipped);
64
+
65
+ // Consumes the lexer's ASCII characters that need no physical-layer work and change no state but the line beginning,
66
+ // the current word, the end of a comment and the run of spaces, which is most of the input, and stops at a character
67
+ // that `directive_scan_feed` has to handle.
68
+ static void directive_scan_plain_run(DirectiveScan *target, TSLexer *lexer) {
69
+ if (target->pair || target->splice) return;
70
+ // The lexer's calls cannot reach this copy, so it can stay in registers.
71
+ DirectiveScan copy = *target;
72
+ while (directive_scan_plain(&copy, lexer)) lexer->advance(lexer, false);
73
+ *target = copy;
74
+ }
75
+
76
+ static inline bool directive_scan_plain(DirectiveScan *scan, TSLexer *lexer) {
77
+ int32_t c = lexer->lookahead;
78
+ if (c <= 0 || c >= 0x80) return false;
79
+ switch (scan->mode) {
80
+ case SCAN_WORD:
81
+ return directive_scan_plain_word(scan, lexer, c);
82
+ case SCAN_CODE:
83
+ return directive_scan_plain_code(scan, lexer, c);
84
+ case SCAN_LINE_COMMENT:
85
+ return directive_scan_plain_text(scan, lexer, c);
86
+ case SCAN_BLOCK_COMMENT:
87
+ if (!directive_scan_plain_text(scan, lexer, c)) return false;
88
+ if (scan->star && c == '/') scan->mode = SCAN_CODE;
89
+ scan->star = c == '*';
90
+ return true;
91
+ case SCAN_QUOTE:
92
+ return !scan->escape && c != scan->quote && directive_scan_plain_text(scan, lexer, c);
93
+ case SCAN_RAW_BODY:
94
+ if (c == '"' || !directive_scan_plain_text(scan, lexer, c)) return false;
95
+ directive_scan_raw_body(scan, c);
96
+ return true;
97
+ default:
98
+ return false;
99
+ }
100
+ }
101
+
102
+ static inline bool directive_scan_plain_word(DirectiveScan *scan, TSLexer *lexer, int32_t c) {
103
+ if (!directive_scan_ascii_word(c)) {
104
+ // A line splice may continue the word, and the others may continue a number or open a raw string.
105
+ if (directive_scan_ascii_special(c) || (scan->number && (c == '.' || c == '+' || c == '-'))) return false;
106
+ scan->mode = SCAN_CODE;
107
+ return directive_scan_plain_code(scan, lexer, c);
108
+ }
109
+ scan->in_run = false;
110
+ scan->previous = c;
111
+ if (scan->raw.length < sizeof scan->raw.value) gnu_raw_string_prefix_character(&scan->raw, c);
112
+ return true;
113
+ }
114
+
115
+ static inline bool directive_scan_plain_code(DirectiveScan *scan, TSLexer *lexer, int32_t c) {
116
+ if (directive_scan_ascii_special(c)) return false;
117
+ bool space = directive_horizontal(c);
118
+ directive_scan_lexer_run(scan, lexer, space);
119
+ if (space) return true;
120
+ scan->beginning = false;
121
+ if (directive_scan_ascii_word(c)) directive_scan_word_start(scan, c);
122
+ return true;
123
+ }
124
+
125
+ // The body of a comment or a literal.
126
+ static inline bool directive_scan_plain_text(DirectiveScan *scan, TSLexer *lexer, int32_t c) {
127
+ if (c == '\n' || c == '\r' || c == '\\') return false;
128
+ directive_scan_lexer_run(scan, lexer, directive_horizontal(c));
129
+ return true;
130
+ }
131
+
132
+ // Extends or ends the run of spaces and backslashes with the lookahead, as `directive_scan_run` does. A span read from
133
+ // the lexer ends just after its last character's start, since the lexer may skip to the next included range after it.
134
+ static inline void directive_scan_lexer_run(DirectiveScan *scan, TSLexer *lexer, bool member) {
135
+ if (!member) {
136
+ scan->in_run = false;
137
+ return;
138
+ }
139
+ TSLexerPosition position;
140
+ lexer->get_position(lexer, &position);
141
+ directive_scan_extend(scan, position.bytes, scan->in_run && lexer->is_at_included_range_start(lexer));
142
+ scan->run_end = position.bytes + 1;
143
+ }
144
+
145
+ // Starts the run at a character from `start`, or a new span of it when bytes were left out before the character.
146
+ static inline void directive_scan_extend(DirectiveScan *scan, uint32_t start, bool skipped) {
147
+ if (!scan->in_run) {
148
+ scan->run_spans = 0;
149
+ scan->run_start = start;
150
+ scan->in_run = true;
151
+ } else if (skipped) {
152
+ directive_scan_push(scan, scan->run_start, scan->run_end);
153
+ scan->run_start = start;
154
+ }
155
+ }
156
+
157
+ // Stores the current span, which reaches `limit` unless bytes were left out before it.
158
+ static void directive_scan_close_run(DirectiveScan *scan, uint32_t limit, bool skipped) {
159
+ directive_scan_push(scan, scan->run_start, skipped ? scan->run_end : limit);
160
+ }
161
+
162
+ // The list is written only here, so that the run's start leaves memory alone.
163
+ static void directive_scan_push(DirectiveScan *scan, uint32_t start, uint32_t end) {
164
+ scan->run->count = scan->run_spans;
165
+ directive_spans_push(scan->run, start, end);
166
+ scan->run_spans = scan->run->count;
167
+ }
168
+
169
+ static inline bool directive_scan_ascii_word(int32_t c) {
170
+ return c != '\\' && ((preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1);
171
+ }
172
+
173
+ // NUL, newlines, `\`, `/`, quotes and `#`, which code leaves to `directive_scan_feed`.
174
+ static inline bool directive_scan_ascii_special(int32_t c) {
175
+ static const uint64_t special[] = {
176
+ 1ull | (1ull << '\n') | (1ull << '\r') | (1ull << '"') | (1ull << '#') | (1ull << '\'') | (1ull << '/'),
177
+ 1ull << ('\\' - 64),
178
+ };
179
+ return (special[c / 64] >> (c % 64)) & 1;
180
+ }
181
+
182
+ // Extends or ends the run with the stored character `c` from `start` to `end`.
183
+ static void directive_scan_run(DirectiveScan *scan, int32_t c, uint32_t start, uint32_t end) {
184
+ if (!pragma_space(c) && c != '\\') {
185
+ scan->in_run = false;
186
+ return;
187
+ }
188
+ directive_scan_extend(scan, start, scan->in_run && start != scan->run_end);
189
+ scan->run_end = end - start > DIRECTIVE_CHARACTER_BYTES ? start + DIRECTIVE_CHARACTER_BYTES : end;
190
+ }
191
+
192
+ // Applies the physical layer of `directive_next` (line splices, paired newlines, and the character mapping of
193
+ // `directive_encode`) and returns true when `c` starts a directive, which the caller leaves unconsumed.
194
+ static bool directive_scan_feed(DirectiveScan *scan, int32_t c) {
195
+ if ((uint32_t)c > 0x10ffff) c = 0xfffd;
196
+ if (scan->mode == SCAN_RAW_BODY) {
197
+ directive_scan_raw_body(scan, c);
198
+ return false;
199
+ }
200
+ if (directive_scan_pending(scan, c)) return false;
201
+ if (c == '\\') {
202
+ scan->splice = true;
203
+ return false;
204
+ }
205
+ if (directive_newline(c)) {
206
+ scan->pair = c;
207
+ c = '\n';
208
+ }
209
+ return directive_scan_logical(scan, c);
210
+ }
211
+
212
+ // Resolves the newline and the line splice that wait for `c`; true when `c` belongs to them.
213
+ static bool directive_scan_pending(DirectiveScan *scan, int32_t c) {
214
+ if (scan->pair) {
215
+ int32_t pair = scan->pair;
216
+ scan->pair = 0;
217
+ if (directive_newline(c) && c != pair) return true;
218
+ }
219
+ return scan->splice && directive_scan_splice(scan, c);
220
+ }
221
+
222
+ static bool directive_scan_splice(DirectiveScan *scan, int32_t c) {
223
+ if (directive_horizontal(c)) {
224
+ scan->spaces++;
225
+ return true;
226
+ }
227
+ scan->splice = false;
228
+ if (directive_newline(c)) {
229
+ scan->spaces = 0;
230
+ scan->pair = c;
231
+ return true;
232
+ }
233
+ directive_scan_logical(scan, '\\');
234
+ for (; scan->spaces; scan->spaces--) directive_scan_logical(scan, ' ');
235
+ return false;
236
+ }
237
+
238
+ // Mirrors `directive_line_character` on a line with no directive state, character by character.
239
+ static bool directive_scan_logical(DirectiveScan *scan, int32_t c) {
240
+ switch (scan->mode) {
241
+ case SCAN_CODE:
242
+ return directive_scan_code(scan, c);
243
+ case SCAN_SLASH:
244
+ return directive_scan_slash(scan, c);
245
+ case SCAN_LINE_COMMENT:
246
+ if (c == '\n') {
247
+ scan->mode = SCAN_CODE;
248
+ scan->beginning = true;
249
+ }
250
+ return false;
251
+ case SCAN_BLOCK_COMMENT:
252
+ if (scan->star && c == '/') scan->mode = SCAN_CODE;
253
+ scan->star = c == '*';
254
+ return false;
255
+ case SCAN_QUOTE:
256
+ directive_scan_quote(scan, c);
257
+ return false;
258
+ case SCAN_WORD:
259
+ return directive_scan_word(scan, c);
260
+ case SCAN_RAW_DELIMITER:
261
+ return directive_scan_raw_delimiter(scan, c);
262
+ case SCAN_RAW_BODY:
263
+ break;
264
+ }
265
+ return false;
266
+ }
267
+
268
+ static bool directive_scan_slash(DirectiveScan *scan, int32_t c) {
269
+ scan->mode = c == '/' ? SCAN_LINE_COMMENT : c == '*' ? SCAN_BLOCK_COMMENT : SCAN_CODE;
270
+ scan->star = false;
271
+ if (scan->mode != SCAN_CODE) return false;
272
+ scan->beginning = false;
273
+ return directive_scan_code(scan, c);
274
+ }
275
+
276
+ // Mirrors `directive_quoted`.
277
+ static void directive_scan_quote(DirectiveScan *scan, int32_t c) {
278
+ if (scan->escape) scan->escape = false;
279
+ else if (c == scan->quote) scan->mode = SCAN_CODE;
280
+ else if (c == '\\') scan->escape = true;
281
+ if (c == '\n' && scan->mode == SCAN_QUOTE) {
282
+ scan->mode = SCAN_CODE;
283
+ scan->beginning = true;
284
+ }
285
+ }
286
+
287
+ static bool directive_scan_code(DirectiveScan *scan, int32_t c) {
288
+ if (c == '\n') {
289
+ scan->beginning = true;
290
+ return false;
291
+ }
292
+ if (c == '/') {
293
+ scan->mode = SCAN_SLASH;
294
+ return false;
295
+ }
296
+ if (directive_horizontal(c) || (scan->beginning && pragma_space(c))) return false;
297
+ if (c == '#' && scan->beginning) return true;
298
+ scan->beginning = false;
299
+ if (c == '"' || c == '\'') {
300
+ scan->mode = SCAN_QUOTE;
301
+ scan->quote = c;
302
+ scan->escape = false;
303
+ } else {
304
+ directive_scan_word_start(scan, c);
305
+ }
306
+ return false;
307
+ }
308
+
309
+ static void directive_scan_word_start(DirectiveScan *scan, int32_t c) {
310
+ if (!preproc_word(c, false) && !(c >= '0' && c <= '9')) return;
311
+ scan->mode = SCAN_WORD;
312
+ scan->number = c >= '0' && c <= '9';
313
+ scan->previous = c;
314
+ scan->raw = (GnuRawStringPrefix){0};
315
+ gnu_raw_string_prefix_character(&scan->raw, c);
316
+ }
317
+
318
+ // Mirrors `directive_word` with `raw` and without `classify`.
319
+ static bool directive_scan_word(DirectiveScan *scan, int32_t c) {
320
+ int32_t previous = scan->previous;
321
+ bool exponent_sign = scan->number && (c == '+' || c == '-') &&
322
+ (previous == 'e' || previous == 'E' || previous == 'p' || previous == 'P');
323
+ if (preproc_word(c, true) || (scan->number && (c == '.' || c == '\'' || exponent_sign))) {
324
+ scan->previous = c;
325
+ gnu_raw_string_prefix_character(&scan->raw, c);
326
+ return false;
327
+ }
328
+ scan->mode = SCAN_CODE;
329
+ if (c == '"' && gnu_raw_string_prefix(&scan->raw)) {
330
+ scan->mode = SCAN_RAW_DELIMITER;
331
+ scan->delimiter_length = 0;
332
+ return false;
333
+ }
334
+ return directive_scan_code(scan, c);
335
+ }
336
+
337
+ // Mirrors the delimiter check of `directive_raw`. On an invalid delimiter, the `"` opens an ordinary string literal
338
+ // instead, so the characters read since are fed again. They hold no `(`, so they cannot start a raw string body.
339
+ static bool directive_scan_raw_delimiter(DirectiveScan *scan, int32_t c) {
340
+ if (c == '(') {
341
+ scan->mode = SCAN_RAW_BODY;
342
+ scan->closing_length = scan->closing_end = 0;
343
+ return false;
344
+ }
345
+ if (scan->delimiter_length < sizeof scan->delimiter && c >= 0x21 && c <= 0x7e && c != ')' && c != '\\') {
346
+ scan->delimiter[scan->delimiter_length++] = (char)c;
347
+ return false;
348
+ }
349
+ char replay[sizeof scan->delimiter];
350
+ unsigned length = scan->delimiter_length;
351
+ memcpy(replay, scan->delimiter, length);
352
+ scan->mode = SCAN_QUOTE;
353
+ scan->quote = '"';
354
+ scan->escape = false;
355
+ for (unsigned i = 0; i < length; i++) directive_scan_logical(scan, replay[i]);
356
+ return directive_scan_logical(scan, c);
357
+ }
358
+
359
+ // Mirrors the physical closing search of `directive_raw` with the last characters kept in a ring.
360
+ static void directive_scan_raw_body(DirectiveScan *scan, int32_t c) {
361
+ unsigned size = scan->delimiter_length + 2;
362
+ unsigned oldest = scan->closing_end;
363
+ scan->closing[oldest] = c > 0 && c < 0x80 ? (char)c : 0;
364
+ scan->closing_end = oldest + 1 == size ? 0 : oldest + 1;
365
+ if (scan->closing_length < size) scan->closing_length++;
366
+ if (c != '"' || scan->closing_length < size || scan->closing[scan->closing_end] != ')') return;
367
+ for (unsigned i = 0; i < scan->delimiter_length; i++) {
368
+ if (scan->closing[(scan->closing_end + 1 + i) % size] != scan->delimiter[i]) return;
369
+ }
370
+ scan->mode = SCAN_CODE;
371
+ }
372
+
373
+ static bool directive_horizontal(int32_t c) {
374
+ return c == ' ' || c == '\t' || c == '\v' || c == '\f';
375
+ }
376
+
377
+ static bool directive_newline(int32_t c) {
378
+ return c == '\n' || c == '\r';
379
+ }
380
+
381
+ static void directive_spans_push(DirectiveSpans *spans, uint32_t start, uint32_t end) {
382
+ if (spans->count == spans->capacity) {
383
+ uint32_t capacity = spans->capacity ? spans->capacity * 2 : 8;
384
+ DirectiveSpan *items = capacity > spans->capacity && (size_t)capacity <= SIZE_MAX / sizeof(*items)
385
+ ? realloc(spans->items, (size_t)capacity * sizeof(*items))
386
+ : NULL;
387
+ if (!items) {
388
+ spans->failed = true;
389
+ return;
390
+ }
391
+ spans->items = items;
392
+ spans->capacity = capacity;
393
+ }
394
+ spans->items[spans->count++] = (DirectiveSpan){start, end};
395
+ }
396
+
397
+ #endif