@willbooster/tree-sitter-c 24.0.0 → 25.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 +20 -7
- package/grammar.js +9 -1
- package/package.json +3 -1
- package/src/conditionalMacro.h +23 -19
- package/src/emptyDirectives.h +439 -0
- package/src/functionMacro.h +159 -100
- package/src/macroProof.h +100 -0
- package/src/node-types.json +5 -0
- package/src/pragma.h +33 -5
- package/src/scanner.c +73 -23
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +3 -1
package/README.md
CHANGED
|
@@ -18,14 +18,13 @@ This fork fixes parsing bugs and raises conformance with the ISO C standard
|
|
|
18
18
|
## Support Scope
|
|
19
19
|
|
|
20
20
|
This fork maintains only full parsing: valid input yields a correct tree, and invalid input yields `ERROR` or `MISSING`
|
|
21
|
-
nodes without crashing, hanging, or taking more than linear time.
|
|
22
|
-
but is not tested here, and no queries (highlights, locals, injections, tags) are shipped. Node types, field names, and
|
|
21
|
+
nodes without crashing, hanging, or taking more than linear time. No queries (highlights, locals, injections, tags) are shipped. Node types, field names, and
|
|
23
22
|
the trees that error recovery produces may change; such changes are released as new major versions.
|
|
24
23
|
|
|
25
24
|
## Usage
|
|
26
25
|
|
|
27
26
|
The npm package ships `tree-sitter-c.wasm` for
|
|
28
|
-
[@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version 15.0.3 or later within 15.x, which runs in Node.js, Bun, browsers, and Cloudflare Workers. The parser carries the ABI number of the WillBooster/tree-sitter generator (`0x5742000b`), so it loads only with that runtime; upstream tree-sitter runtimes reject it. In Node.js and Bun, load the grammar from its path:
|
|
27
|
+
[@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version 15.0.3 or later within 15.x, or 16.x, which runs in Node.js, Bun, browsers, and Cloudflare Workers. The parser carries the ABI number of the WillBooster/tree-sitter generator (`0x5742000b`), so it loads only with that runtime; upstream tree-sitter runtimes reject it. In Node.js and Bun, load the grammar from its path:
|
|
29
28
|
|
|
30
29
|
```js
|
|
31
30
|
import { fileURLToPath } from 'node:url';
|
|
@@ -67,9 +66,14 @@ parser.setLanguage(await Language.load(c));
|
|
|
67
66
|
The package also ships `grammar.js`, scanner sources under `src/`,
|
|
68
67
|
and the node types in `src/node-types.json` for grammars extending C (such as C++).
|
|
69
68
|
The scanner sources include `scanner.c`, `pragma.h`, `identifier.h`, `keywords.h`, `functionMacro.h`,
|
|
70
|
-
`conditionalMacro.h`, and `gnuRawString.h`.
|
|
69
|
+
`conditionalMacro.h`, `emptyDirectives.h`, `macroProof.h`, and `gnuRawString.h`.
|
|
71
70
|
|
|
72
71
|
Derived grammars must port the inherited external-token dispatch from `src/scanner.c`.
|
|
72
|
+
Append `_directive_context` and `preproc_empty` in their inherited external-token slots and retain them as extras.
|
|
73
|
+
Port the scanner lifecycle functions together with `EmptyDirectives` from `emptyDirectives.h`; the source-boundary
|
|
74
|
+
facts belong to one full parse and are freed by scanner destruction. Run `scan_directive_context` first,
|
|
75
|
+
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
|
+
`empty_context` so declaration lookahead ignores null directives without moving the opening token's range.
|
|
73
77
|
Keep preprocessor argument/newline handling ahead of annotation lookahead.
|
|
74
78
|
Port `preproc_argument_mode` selection together with `scan_preproc_arg` so tokenized pragmas retain raw bodies.
|
|
75
79
|
Append `_gnu_raw_string_body` and `_raw_macro_type_tail` in their inherited enum slots and port their dispatch from `src/scanner.c`.
|
|
@@ -83,6 +87,9 @@ The hidden `_function_attribute_start`, `_post_type_function_attribute_start`,
|
|
|
83
87
|
`_post_type_function_attribute_suffix`, and `_braced_function_attribute_start` guards look ahead before the ordinary
|
|
84
88
|
identifier token. Keep them outside the name field so identifier leaves and ordinary C lexing retain their shape.
|
|
85
89
|
Port `scan_function_attribute_guard`, its `MacroAttributeSymbols` context, and the name-range handling in `pragma.h`.
|
|
90
|
+
Set `MacroAttributeSymbols.trivia` to `{empty_directives_skip, state}` so annotation, argument-group, and declaration-tail proofs skip the same null directives.
|
|
91
|
+
Pass that `PragmaTrivia` context to the raw macro-type tail and balanced-group helpers, and use `scan_pragma_with` for the pragma-operator fallback.
|
|
92
|
+
A derived scanner that omits this context cannot recognize null directives inside those lookahead paths.
|
|
86
93
|
Set `braced_start` to the inherited braced token and `allow_braced_start` from its valid-symbol entry.
|
|
87
94
|
Omitting these fields disables braced handoff in the current argument group and in prototype-tail lookahead.
|
|
88
95
|
Preserve the inherited external-token order before adding tokens specific to the derived grammar.
|
|
@@ -92,9 +99,15 @@ This separate zero-width guard proves marker words followed by a braced macro-ty
|
|
|
92
99
|
consumes each marker and validates its type. Keep `MacroName.preserve_end` in `pragma.h` so scanning subsequent words
|
|
93
100
|
does not move the guard's end. Its default is false for ordinary name scans. Derived grammars can supply reserved
|
|
94
101
|
starter words and the same `MacroAttributeSymbols` context to the guard helper; the C scanner supplies an empty table.
|
|
95
|
-
The helper shares pragma and scalar-object dispatch with annotation guards. Copy
|
|
102
|
+
The helper shares pragma and scalar-object dispatch with annotation guards. Copy `functionMacro.h`, `macroProof.h`, and `pragma.h`
|
|
96
103
|
when porting this dispatch. Later pragma lookahead must preserve the zero-width token end.
|
|
97
104
|
|
|
105
|
+
The C scanner owns a `MacroProofMemo` for each full parse and frees it on scanner destruction.
|
|
106
|
+
Pass it as `MacroAttributeSymbols.proof_memo` to reuse continuation proofs by physical byte position and lexical state.
|
|
107
|
+
The first-word dispatch stays uncached because it depends on the valid external tokens. The memo binds to one reserved-word table and trivia callback/context;
|
|
108
|
+
a different table or trivia context disables reuse. Derived scanners can leave `proof_memo` unset, or own a memo with the same lifetime.
|
|
109
|
+
These source facts need no serialized bytes.
|
|
110
|
+
|
|
98
111
|
When overriding declarations or function definitions, retain both guarded ordinary-type/declarator alternatives and
|
|
99
112
|
annotation alternatives in ordinary, block, and for-declaration builders. Accept the matching passive guard after calling conventions and before ordinary, builtin,
|
|
100
113
|
and initialized declarators. Derived custom declaration builders also need `_guarded_type_specifiers` and
|
|
@@ -190,10 +203,10 @@ arrays, parenthesized/function declarators, initializers and multiple declarator
|
|
|
190
203
|
|
|
191
204
|
In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-c) and on
|
|
192
205
|
[willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
|
|
193
|
-
fuzzed with, at version
|
|
206
|
+
fuzzed with, at the version recorded in `Cargo.lock`:
|
|
194
207
|
|
|
195
208
|
```sh
|
|
196
|
-
cargo add willbooster-tree-sitter@
|
|
209
|
+
cargo add willbooster-tree-sitter@16.0.0 --rename tree-sitter
|
|
197
210
|
cargo add willbooster-tree-sitter-c --rename tree-sitter-c
|
|
198
211
|
```
|
|
199
212
|
|
package/grammar.js
CHANGED
|
@@ -454,9 +454,17 @@ module.exports = Object.assign(
|
|
|
454
454
|
sym('_raw_macro_type_tail'),
|
|
455
455
|
sym('_braced_function_attribute_start'),
|
|
456
456
|
sym('_braced_macro_type_start'),
|
|
457
|
+
sym('_directive_context'),
|
|
458
|
+
sym('preproc_empty'),
|
|
457
459
|
],
|
|
458
460
|
|
|
459
|
-
extras: ($) => [
|
|
461
|
+
extras: ($) => [
|
|
462
|
+
sym('_directive_context'),
|
|
463
|
+
sym('preproc_empty'),
|
|
464
|
+
$.pragma_operator,
|
|
465
|
+
/\s|\\(?:\r\n?|\n\r?)/,
|
|
466
|
+
$.comment,
|
|
467
|
+
],
|
|
460
468
|
|
|
461
469
|
inline: ($) => [
|
|
462
470
|
$._bare_marker_initializer_declaration,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/tree-sitter-c",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "25.0.0",
|
|
4
4
|
"description": "C grammar for tree-sitter",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"c",
|
|
@@ -25,7 +25,9 @@
|
|
|
25
25
|
"src/identifier.h",
|
|
26
26
|
"src/keywords.h",
|
|
27
27
|
"src/functionMacro.h",
|
|
28
|
+
"src/macroProof.h",
|
|
28
29
|
"src/conditionalMacro.h",
|
|
30
|
+
"src/emptyDirectives.h",
|
|
29
31
|
"src/gnuRawString.h",
|
|
30
32
|
"src/scanner.c"
|
|
31
33
|
],
|
package/src/conditionalMacro.h
CHANGED
|
@@ -9,21 +9,23 @@ typedef struct {
|
|
|
9
9
|
// Excludes unknown type aliases and marker names; recognized C type/modifier prefixes are unchanged.
|
|
10
10
|
const char *const *reserved_words;
|
|
11
11
|
unsigned reserved_word_count;
|
|
12
|
+
bool (*skip_empty)(TSLexer *, void *);
|
|
13
|
+
void *empty_context;
|
|
12
14
|
} ConditionalMacroSymbols;
|
|
13
15
|
|
|
14
16
|
static bool conditional_macro_body(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
|
|
15
17
|
static bool conditional_macro_word(TSLexer *lexer, MacroName *name, bool escaped);
|
|
16
18
|
static bool conditional_macro_spacing(TSLexer *lexer, bool *separated, bool *escaped, bool directive);
|
|
17
19
|
static bool conditional_macro_space(int32_t c, bool directive);
|
|
18
|
-
static bool conditional_macro_next_line(TSLexer *lexer, bool *escaped);
|
|
20
|
+
static bool conditional_macro_next_line(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
|
|
19
21
|
static bool conditional_macro_directive(TSLexer *lexer, MacroName *name);
|
|
20
22
|
static bool conditional_macro_marker_line(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
|
|
21
23
|
static bool conditional_macro_closing(TSLexer *lexer);
|
|
22
24
|
static bool conditional_macro_type_prefix(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool escaped);
|
|
23
|
-
static bool conditional_macro_skip_lines(TSLexer *lexer, bool *escaped);
|
|
25
|
+
static bool conditional_macro_skip_lines(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
|
|
24
26
|
static bool conditional_macro_reserved_word(const ConditionalMacroSymbols *symbols, const char *word);
|
|
25
|
-
static bool conditional_macro_modifier_group(TSLexer *lexer, const char *word, bool *escaped);
|
|
26
|
-
static bool conditional_macro_attribute_declaration(TSLexer *lexer, bool *escaped);
|
|
27
|
+
static bool conditional_macro_modifier_group(TSLexer *lexer, const ConditionalMacroSymbols *symbols, const char *word, bool *escaped);
|
|
28
|
+
static bool conditional_macro_attribute_declaration(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
|
|
27
29
|
|
|
28
30
|
static bool scan_conditional_macro(TSLexer *lexer, const ConditionalMacroSymbols *symbols, const bool *valid_symbols) {
|
|
29
31
|
char directive[7] = {0};
|
|
@@ -52,9 +54,9 @@ static bool conditional_macro_body(TSLexer *lexer, const ConditionalMacroSymbols
|
|
|
52
54
|
bool separated = false;
|
|
53
55
|
MacroName condition = {.ascii = true};
|
|
54
56
|
if (!conditional_macro_spacing(lexer, &separated, escaped, false) || !separated ||
|
|
55
|
-
!conditional_macro_word(lexer, &condition, *escaped) || !conditional_macro_next_line(lexer, escaped)) return false;
|
|
57
|
+
!conditional_macro_word(lexer, &condition, *escaped) || !conditional_macro_next_line(lexer, symbols, escaped)) return false;
|
|
56
58
|
if (!conditional_macro_marker_line(lexer, symbols, escaped) || !conditional_macro_closing(lexer)) return false;
|
|
57
|
-
return conditional_macro_skip_lines(lexer, escaped) && !lexer->eof(lexer) && lexer->lookahead != '#';
|
|
59
|
+
return conditional_macro_skip_lines(lexer, symbols, escaped) && !lexer->eof(lexer) && lexer->lookahead != '#';
|
|
58
60
|
}
|
|
59
61
|
|
|
60
62
|
static bool conditional_macro_closing(TSLexer *lexer) {
|
|
@@ -92,17 +94,17 @@ static bool conditional_macro_type_prefix(TSLexer *lexer, const ConditionalMacro
|
|
|
92
94
|
|
|
93
95
|
static ConditionalMacroPrefix conditional_macro_prefix(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped) {
|
|
94
96
|
if (lexer->lookahead == '[') {
|
|
95
|
-
return conditional_macro_attribute_declaration(lexer, escaped) ? CONDITIONAL_MODIFIER : CONDITIONAL_INVALID;
|
|
97
|
+
return conditional_macro_attribute_declaration(lexer, symbols, escaped) ? CONDITIONAL_MODIFIER : CONDITIONAL_INVALID;
|
|
96
98
|
}
|
|
97
99
|
char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
|
|
98
100
|
MacroName type = {.word = word, .capacity = sizeof(word) - 1, .ascii = true};
|
|
99
101
|
if (!conditional_macro_word(lexer, &type, *escaped)) return CONDITIONAL_INVALID;
|
|
100
102
|
ConditionalMacroPrefix kind = conditional_macro_prefix_kind(symbols, &type, word);
|
|
101
103
|
if (kind == CONDITIONAL_TYPE) return kind;
|
|
102
|
-
if (!conditional_macro_skip_lines(lexer, escaped)) return CONDITIONAL_INVALID;
|
|
104
|
+
if (!conditional_macro_skip_lines(lexer, symbols, escaped)) return CONDITIONAL_INVALID;
|
|
103
105
|
if (kind == CONDITIONAL_TYPE_MODIFIER && lexer->lookahead == '(') return CONDITIONAL_TYPE;
|
|
104
106
|
if (kind == CONDITIONAL_MODIFIER || kind == CONDITIONAL_TYPE_MODIFIER) {
|
|
105
|
-
return conditional_macro_modifier_group(lexer, word, escaped) ? CONDITIONAL_MODIFIER : CONDITIONAL_INVALID;
|
|
107
|
+
return conditional_macro_modifier_group(lexer, symbols, word, escaped) ? CONDITIONAL_MODIFIER : CONDITIONAL_INVALID;
|
|
106
108
|
}
|
|
107
109
|
return kind;
|
|
108
110
|
}
|
|
@@ -130,29 +132,30 @@ static bool conditional_macro_reserved_word(const ConditionalMacroSymbols *symbo
|
|
|
130
132
|
return false;
|
|
131
133
|
}
|
|
132
134
|
|
|
133
|
-
static bool conditional_macro_modifier_group(TSLexer *lexer, const char *word, bool *escaped) {
|
|
135
|
+
static bool conditional_macro_modifier_group(TSLexer *lexer, const ConditionalMacroSymbols *symbols, const char *word, bool *escaped) {
|
|
134
136
|
bool grouped = strcmp(word, pragma_word) == 0 || function_macro_word_in(word, function_macro_group_modifiers,
|
|
135
137
|
sizeof(function_macro_group_modifiers) / sizeof(function_macro_group_modifiers[0]));
|
|
136
138
|
if (!grouped || lexer->lookahead != '(') return true;
|
|
137
139
|
bool literal = false;
|
|
138
|
-
return function_macro_balanced(lexer, '(', ')', &literal, NULL) && conditional_macro_skip_lines(lexer, escaped);
|
|
140
|
+
return function_macro_balanced(lexer, '(', ')', &literal, NULL, &(PragmaTrivia){symbols->skip_empty, symbols->empty_context}) && conditional_macro_skip_lines(lexer, symbols, escaped);
|
|
139
141
|
}
|
|
140
142
|
|
|
141
|
-
static bool conditional_macro_attribute_declaration(TSLexer *lexer, bool *escaped) {
|
|
143
|
+
static bool conditional_macro_attribute_declaration(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped) {
|
|
142
144
|
lexer->advance(lexer, false);
|
|
143
|
-
if (!conditional_macro_skip_lines(lexer, escaped) || lexer->lookahead != '[') return false;
|
|
145
|
+
if (!conditional_macro_skip_lines(lexer, symbols, escaped) || lexer->lookahead != '[') return false;
|
|
144
146
|
bool literal = false;
|
|
145
|
-
if (!function_macro_balanced(lexer, '[', ']', &literal, NULL) ||
|
|
146
|
-
!conditional_macro_skip_lines(lexer, escaped) || lexer->lookahead != ']') return false;
|
|
147
|
+
if (!function_macro_balanced(lexer, '[', ']', &literal, NULL, &(PragmaTrivia){symbols->skip_empty, symbols->empty_context}) ||
|
|
148
|
+
!conditional_macro_skip_lines(lexer, symbols, escaped) || lexer->lookahead != ']') return false;
|
|
147
149
|
lexer->advance(lexer, false);
|
|
148
|
-
return conditional_macro_skip_lines(lexer, escaped);
|
|
150
|
+
return conditional_macro_skip_lines(lexer, symbols, escaped);
|
|
149
151
|
}
|
|
150
152
|
|
|
151
|
-
static bool conditional_macro_skip_lines(TSLexer *lexer, bool *escaped) {
|
|
153
|
+
static bool conditional_macro_skip_lines(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped) {
|
|
152
154
|
bool separated = false;
|
|
153
155
|
for (;;) {
|
|
154
156
|
*escaped = false;
|
|
155
157
|
if (!conditional_macro_spacing(lexer, &separated, escaped, false)) return false;
|
|
158
|
+
if (symbols->skip_empty && symbols->skip_empty(lexer, symbols->empty_context)) continue;
|
|
156
159
|
if (*escaped || !scan_preproc_newline(lexer, false)) return true;
|
|
157
160
|
}
|
|
158
161
|
}
|
|
@@ -162,7 +165,7 @@ static bool conditional_macro_marker_line(TSLexer *lexer, const ConditionalMacro
|
|
|
162
165
|
MacroName marker = {.word = word, .capacity = sizeof(word) - 1, .ascii = true};
|
|
163
166
|
if (!conditional_macro_word(lexer, &marker, *escaped) ||
|
|
164
167
|
(marker.ascii && (function_macro_keyword(word) || conditional_macro_reserved_word(symbols, word)))) return false;
|
|
165
|
-
return conditional_macro_next_line(lexer, escaped);
|
|
168
|
+
return conditional_macro_next_line(lexer, symbols, escaped);
|
|
166
169
|
}
|
|
167
170
|
|
|
168
171
|
static bool conditional_macro_directive(TSLexer *lexer, MacroName *name) {
|
|
@@ -174,13 +177,14 @@ static bool conditional_macro_directive(TSLexer *lexer, MacroName *name) {
|
|
|
174
177
|
conditional_macro_word(lexer, name, false);
|
|
175
178
|
}
|
|
176
179
|
|
|
177
|
-
static bool conditional_macro_next_line(TSLexer *lexer, bool *escaped) {
|
|
180
|
+
static bool conditional_macro_next_line(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped) {
|
|
178
181
|
bool separated = false;
|
|
179
182
|
*escaped = false;
|
|
180
183
|
if (!conditional_macro_spacing(lexer, &separated, escaped, false) || *escaped ||
|
|
181
184
|
!scan_preproc_newline(lexer, false)) return false;
|
|
182
185
|
for (;;) {
|
|
183
186
|
if (!conditional_macro_spacing(lexer, &separated, escaped, false) || lexer->eof(lexer)) return false;
|
|
187
|
+
if (symbols->skip_empty && symbols->skip_empty(lexer, symbols->empty_context)) continue;
|
|
184
188
|
if (*escaped || !scan_preproc_newline(lexer, false)) return true;
|
|
185
189
|
}
|
|
186
190
|
}
|