@willbooster/tree-sitter-c 30.0.0 → 32.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 +2 -2
- package/package.json +3 -3
- package/src/emptyDirectives.h +81 -15
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +1 -1
package/README.md
CHANGED
|
@@ -24,7 +24,7 @@ the trees that error recovery produces may change; such changes are released as
|
|
|
24
24
|
## Usage
|
|
25
25
|
|
|
26
26
|
The npm package ships `tree-sitter-c.wasm` for
|
|
27
|
-
[@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version
|
|
27
|
+
[@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version 17.0.1 or later within 17.x, which runs in Node.js, Bun, browsers, and Cloudflare Workers. The parser carries the ABI number of the WillBooster/tree-sitter generator (`0x5742000c`), so it loads only with that runtime; upstream tree-sitter runtimes reject it. In Node.js and Bun, load the grammar from its path:
|
|
28
28
|
|
|
29
29
|
```js
|
|
30
30
|
import { fileURLToPath } from 'node:url';
|
|
@@ -211,7 +211,7 @@ In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-
|
|
|
211
211
|
fuzzed with, at the version recorded in `Cargo.lock`:
|
|
212
212
|
|
|
213
213
|
```sh
|
|
214
|
-
cargo add willbooster-tree-sitter@
|
|
214
|
+
cargo add willbooster-tree-sitter@17.0.1 --rename tree-sitter
|
|
215
215
|
cargo add willbooster-tree-sitter-c --rename tree-sitter-c
|
|
216
216
|
```
|
|
217
217
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/tree-sitter-c",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "32.0.0",
|
|
4
4
|
"description": "C grammar for tree-sitter",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"c",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"@vitest/browser-playwright": "5.0.5",
|
|
61
61
|
"@willbooster/oxlint-config": "1.4.9",
|
|
62
62
|
"@willbooster/wb": "23.0.2",
|
|
63
|
-
"@willbooster/web-tree-sitter": "
|
|
63
|
+
"@willbooster/web-tree-sitter": "17.0.1",
|
|
64
64
|
"conventional-changelog-conventionalcommits": "10.4.1",
|
|
65
65
|
"lefthook": "2.2.2",
|
|
66
66
|
"oxlint": "1.89.0",
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
"wrangler": "4.152.0"
|
|
72
72
|
},
|
|
73
73
|
"peerDependencies": {
|
|
74
|
-
"@willbooster/web-tree-sitter": "^
|
|
74
|
+
"@willbooster/web-tree-sitter": "^17.0.1"
|
|
75
75
|
},
|
|
76
76
|
"peerDependenciesMeta": {
|
|
77
77
|
"@willbooster/web-tree-sitter": {
|
package/src/emptyDirectives.h
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// code-gauge-allow-file fileNcss
|
|
1
|
+
// code-gauge-allow-file fileNcss 575 -- 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
|
|
|
@@ -15,6 +15,7 @@ typedef struct {
|
|
|
15
15
|
uint32_t position_count, position_capacity, position_index, previous_byte, before_previous_byte;
|
|
16
16
|
uint8_t *prefix_barriers;
|
|
17
17
|
uint32_t length, index, byte, end, page_count;
|
|
18
|
+
uint32_t conditional_depth;
|
|
18
19
|
bool utf16, encoding_known;
|
|
19
20
|
} DirectiveInput;
|
|
20
21
|
|
|
@@ -24,13 +25,13 @@ typedef struct {
|
|
|
24
25
|
bool built, ready;
|
|
25
26
|
} EmptyDirectives;
|
|
26
27
|
|
|
27
|
-
typedef enum { DIRECTIVE_ORDINARY, DIRECTIVE_CONDITION, DIRECTIVE_DIAGNOSTIC, DIRECTIVE_REQUIRED, DIRECTIVE_PRAGMA, DIRECTIVE_HEADER } DirectiveKind;
|
|
28
|
+
typedef enum { DIRECTIVE_ORDINARY, DIRECTIVE_CONDITION, DIRECTIVE_DIAGNOSTIC, DIRECTIVE_REQUIRED, DIRECTIVE_UNDEF, DIRECTIVE_LINE, DIRECTIVE_PRAGMA, DIRECTIVE_HEADER } DirectiveKind;
|
|
28
29
|
|
|
29
30
|
typedef struct {
|
|
30
|
-
bool beginning, pending, operand, pragma_pending, pragma_newline;
|
|
31
|
+
bool beginning, pending, operand, pragma_pending, pragma_newline, pragma_name;
|
|
31
32
|
unsigned pragma_depth, header;
|
|
32
33
|
DirectiveKind kind;
|
|
33
|
-
uint32_t start;
|
|
34
|
+
uint32_t start, required_end;
|
|
34
35
|
} DirectiveLine;
|
|
35
36
|
|
|
36
37
|
static void directive_prefixes(EmptyDirectives *state, DirectiveInput *input);
|
|
@@ -44,6 +45,8 @@ static void directive_release(DirectiveInput *input);
|
|
|
44
45
|
static unsigned directive_utf8_width(uint32_t c);
|
|
45
46
|
static void directive_barrier(DirectiveInput *input);
|
|
46
47
|
static void directive_line_end(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
|
|
48
|
+
static void directive_complete_required(EmptyDirectives *state, DirectiveLine *line);
|
|
49
|
+
static bool directive_requires_operand(DirectiveKind kind);
|
|
47
50
|
static void directive_line_character(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
|
|
48
51
|
static void directive_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
|
|
49
52
|
static bool directive_pragma_part(DirectiveLine *line, int32_t c);
|
|
@@ -57,6 +60,9 @@ static void empty_directives_find(EmptyDirectives *state, DirectiveInput *input)
|
|
|
57
60
|
static int32_t directive_next(DirectiveInput *input);
|
|
58
61
|
static int32_t directive_physical_next(DirectiveInput *input);
|
|
59
62
|
static int32_t directive_peek(const DirectiveInput *input);
|
|
63
|
+
static void directive_pragma_name(DirectiveLine *line, const DirectiveInput *input);
|
|
64
|
+
static bool directive_pragma_operator(const DirectiveInput *input);
|
|
65
|
+
static int32_t directive_pragma_token(DirectiveInput *input);
|
|
60
66
|
static bool directive_comment(DirectiveInput *input, bool *line);
|
|
61
67
|
static bool directive_quoted(DirectiveInput *input, int32_t quote);
|
|
62
68
|
static bool directive_raw(DirectiveInput *input);
|
|
@@ -67,7 +73,8 @@ typedef struct {
|
|
|
67
73
|
} DirectiveWord;
|
|
68
74
|
|
|
69
75
|
static uint32_t directive_keyword_end(const DirectiveInput *input);
|
|
70
|
-
static DirectiveKind
|
|
76
|
+
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);
|
|
71
78
|
static DirectiveKind directive_word_kind(const DirectiveWord *word);
|
|
72
79
|
static void directive_word_character(DirectiveWord *word, int32_t c);
|
|
73
80
|
static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool name, bool pragma, bool had_operand, bool identifier);
|
|
@@ -280,6 +287,7 @@ static void empty_directives_find(EmptyDirectives *state, DirectiveInput *input)
|
|
|
280
287
|
if (c < 0) break;
|
|
281
288
|
directive_line_character(state, input, &line, c);
|
|
282
289
|
}
|
|
290
|
+
directive_complete_required(state, &line);
|
|
283
291
|
if (line.pending) directive_record(state, line.start, input->end);
|
|
284
292
|
}
|
|
285
293
|
|
|
@@ -307,7 +315,7 @@ static void directive_line_character(EmptyDirectives *state, DirectiveInput *inp
|
|
|
307
315
|
directive_code(state, input, line, c);
|
|
308
316
|
}
|
|
309
317
|
|
|
310
|
-
// code-gauge-allow functionCognitiveComplexity
|
|
318
|
+
// code-gauge-allow functionCognitiveComplexity 19 -- Header names, quoted tokens and distinct keyword boundaries update one logical directive's lexical state.
|
|
311
319
|
static void directive_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c) {
|
|
312
320
|
bool name = line->pending;
|
|
313
321
|
bool header = directive_header_ready(line, c);
|
|
@@ -325,10 +333,9 @@ static void directive_code(EmptyDirectives *state, DirectiveInput *input, Direct
|
|
|
325
333
|
}
|
|
326
334
|
if (!preproc_word(c, false) && !(c >= '0' && c <= '9')) return;
|
|
327
335
|
uint32_t end = name ? directive_keyword_end(input) : 0;
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
}
|
|
336
|
+
DirectiveWord word = directive_word(input, c, line->kind != DIRECTIVE_DIAGNOSTIC);
|
|
337
|
+
DirectiveKind kind = name ? directive_keyword(state, input, line, &word, end) : directive_word_kind(&word);
|
|
338
|
+
if (!name && !pragma && kind == DIRECTIVE_PRAGMA) directive_pragma_name(line, input);
|
|
332
339
|
directive_word_role(line, kind, name, pragma, operand, preproc_word(c, false));
|
|
333
340
|
}
|
|
334
341
|
|
|
@@ -339,7 +346,7 @@ static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool na
|
|
|
339
346
|
}
|
|
340
347
|
if (pragma) return;
|
|
341
348
|
if (kind == DIRECTIVE_PRAGMA) {
|
|
342
|
-
bool required = line->kind
|
|
349
|
+
bool required = directive_requires_operand(line->kind) && line->kind != DIRECTIVE_UNDEF;
|
|
343
350
|
if (required && !had_operand) {
|
|
344
351
|
line->operand = false;
|
|
345
352
|
line->pragma_pending = true;
|
|
@@ -394,11 +401,12 @@ static void directive_line_comment(EmptyDirectives *state, DirectiveInput *input
|
|
|
394
401
|
}
|
|
395
402
|
|
|
396
403
|
static void directive_line_end(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line) {
|
|
404
|
+
directive_complete_required(state, line);
|
|
397
405
|
if (line->pending) directive_record(state, line->start, directive_byte(input));
|
|
398
406
|
line->beginning = true;
|
|
399
407
|
line->pending = false;
|
|
400
408
|
bool required = line->kind == DIRECTIVE_CONDITION ||
|
|
401
|
-
((line->kind
|
|
409
|
+
(directive_requires_operand(line->kind) && !line->operand && !line->pragma_pending);
|
|
402
410
|
if (required) directive_barrier(input);
|
|
403
411
|
line->header = 0;
|
|
404
412
|
if (line->pragma_pending) line->pragma_newline = true;
|
|
@@ -408,6 +416,21 @@ static void directive_line_end(EmptyDirectives *state, DirectiveInput *input, Di
|
|
|
408
416
|
}
|
|
409
417
|
}
|
|
410
418
|
|
|
419
|
+
static void directive_complete_required(EmptyDirectives *state, DirectiveLine *line) {
|
|
420
|
+
bool pragma_name = line->pragma_name;
|
|
421
|
+
line->pragma_name = false;
|
|
422
|
+
if (!line->required_end) return;
|
|
423
|
+
if (line->operand || line->pragma_pending || pragma_name) {
|
|
424
|
+
uint32_t end = line->required_end;
|
|
425
|
+
state->directive_ends[end / 8] |= 1u << (end % 8);
|
|
426
|
+
}
|
|
427
|
+
line->required_end = 0;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
static bool directive_requires_operand(DirectiveKind kind) {
|
|
431
|
+
return kind == DIRECTIVE_REQUIRED || kind == DIRECTIVE_UNDEF || kind == DIRECTIVE_LINE || kind == DIRECTIVE_HEADER;
|
|
432
|
+
}
|
|
433
|
+
|
|
411
434
|
static void directive_barrier(DirectiveInput *input) {
|
|
412
435
|
if (!input->index || !directive_newline(directive_character(input, input->index - 1))) return;
|
|
413
436
|
uint32_t byte = input->previous_byte;
|
|
@@ -470,6 +493,35 @@ static int32_t directive_peek(const DirectiveInput *input) {
|
|
|
470
493
|
return directive_next(©);
|
|
471
494
|
}
|
|
472
495
|
|
|
496
|
+
// `_Pragma` is the macro name of `#define _Pragma(x)` or `#ifdef _Pragma`, but an operator before a string literal.
|
|
497
|
+
static void directive_pragma_name(DirectiveLine *line, const DirectiveInput *input) {
|
|
498
|
+
if (line->kind == DIRECTIVE_UNDEF || (line->kind == DIRECTIVE_REQUIRED && !directive_pragma_operator(input))) line->pragma_name = true;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// Mirrors the argument prefix accepted by `scan_pragma_argument` and the spacing a directive line can hold.
|
|
502
|
+
static bool directive_pragma_operator(const DirectiveInput *input) {
|
|
503
|
+
DirectiveInput copy = *input;
|
|
504
|
+
if (directive_pragma_token(©) != '(') return false;
|
|
505
|
+
int32_t c = directive_pragma_token(©);
|
|
506
|
+
if (c == 'L' || c == 'U') c = directive_next(©);
|
|
507
|
+
else if (c == 'u') {
|
|
508
|
+
c = directive_next(©);
|
|
509
|
+
if (c == '8') c = directive_next(©);
|
|
510
|
+
}
|
|
511
|
+
if (c == 'R') c = directive_next(©);
|
|
512
|
+
return c == '"';
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
static int32_t directive_pragma_token(DirectiveInput *input) {
|
|
516
|
+
for (;;) {
|
|
517
|
+
int32_t c = directive_next(input);
|
|
518
|
+
if (c == '/' && directive_peek(input) == '*') {
|
|
519
|
+
bool line = false;
|
|
520
|
+
directive_comment(input, &line);
|
|
521
|
+
} else if (!directive_horizontal(c)) return c;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
473
525
|
static bool directive_comment(DirectiveInput *input, bool *line) {
|
|
474
526
|
*line = directive_next(input) == '/';
|
|
475
527
|
int32_t previous = 0;
|
|
@@ -501,7 +553,19 @@ static uint32_t directive_keyword_end(const DirectiveInput *input) {
|
|
|
501
553
|
return directive_byte(&name);
|
|
502
554
|
}
|
|
503
555
|
|
|
504
|
-
static DirectiveKind
|
|
556
|
+
static DirectiveKind directive_keyword(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, const DirectiveWord *word, uint32_t end) {
|
|
557
|
+
DirectiveKind kind = directive_word_kind(word);
|
|
558
|
+
// Directives in a group that may be skipped need no operand, and the parser cannot tell which groups are skipped.
|
|
559
|
+
if (directive_requires_operand(kind) && !input->conditional_depth) line->required_end = end;
|
|
560
|
+
else state->directive_ends[end / 8] |= 1u << (end % 8);
|
|
561
|
+
if (word->length < sizeof word->directive) {
|
|
562
|
+
if (strcmp(word->directive, "if") == 0 || strcmp(word->directive, "ifdef") == 0 || strcmp(word->directive, "ifndef") == 0) input->conditional_depth++;
|
|
563
|
+
else if (strcmp(word->directive, "endif") == 0 && input->conditional_depth) input->conditional_depth--;
|
|
564
|
+
}
|
|
565
|
+
return kind;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
static DirectiveWord directive_word(DirectiveInput *input, int32_t first, bool raw) {
|
|
505
569
|
DirectiveWord word = {0};
|
|
506
570
|
directive_word_character(&word, first);
|
|
507
571
|
bool number = first >= '0' && first <= '9';
|
|
@@ -515,7 +579,7 @@ static DirectiveKind directive_word(DirectiveInput *input, int32_t first, bool r
|
|
|
515
579
|
directive_word_character(&word, previous);
|
|
516
580
|
}
|
|
517
581
|
if (raw && gnu_raw_string_prefix(&word.raw) && directive_peek(input) == '"') directive_raw(input);
|
|
518
|
-
return
|
|
582
|
+
return word;
|
|
519
583
|
}
|
|
520
584
|
|
|
521
585
|
static DirectiveKind directive_word_kind(const DirectiveWord *word) {
|
|
@@ -525,8 +589,10 @@ static DirectiveKind directive_word_kind(const DirectiveWord *word) {
|
|
|
525
589
|
if (strcmp(word->directive, "include") == 0 || strcmp(word->directive, "include_next") == 0 ||
|
|
526
590
|
strcmp(word->directive, "embed") == 0) return DIRECTIVE_HEADER;
|
|
527
591
|
if (strcmp(word->directive, "warning") == 0 || strcmp(word->directive, "error") == 0) return DIRECTIVE_DIAGNOSTIC;
|
|
592
|
+
if (strcmp(word->directive, "undef") == 0) return DIRECTIVE_UNDEF;
|
|
593
|
+
if (strcmp(word->directive, "line") == 0) return DIRECTIVE_LINE;
|
|
528
594
|
static const char *const required[] = {
|
|
529
|
-
"define", "ifdef", "ifndef", "elifdef", "elifndef",
|
|
595
|
+
"define", "ifdef", "ifndef", "elifdef", "elifndef",
|
|
530
596
|
};
|
|
531
597
|
for (unsigned i = 0; i < sizeof required / sizeof *required; i++) {
|
|
532
598
|
if (strcmp(word->directive, required[i]) == 0) return DIRECTIVE_REQUIRED;
|
package/tree-sitter-c.wasm
CHANGED
|
Binary file
|