@willbooster/tree-sitter-c 19.0.0 → 21.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
@@ -5,7 +5,7 @@
5
5
  [![Test](https://github.com/WillBooster/tree-sitter-c/actions/workflows/test.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-c/actions/workflows/test.yml)
6
6
  [![Test rust](https://github.com/WillBooster/tree-sitter-c/actions/workflows/test-rust.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-c/actions/workflows/test-rust.yml)
7
7
  [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
8
- [![wbfy](https://img.shields.io/badge/wbfy-20.48.9-1e90ff.svg)](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
8
+ [![wbfy](https://img.shields.io/badge/wbfy-20.51.1-1e90ff.svg)](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
9
9
  [![crates.io](https://img.shields.io/crates/v/willbooster-tree-sitter-c.svg)](https://crates.io/crates/willbooster-tree-sitter-c)
10
10
 
11
11
  C grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
@@ -15,10 +15,17 @@ to its authors and contributors. This is not an official release of that project
15
15
  This fork fixes parsing bugs and raises conformance with the ISO C standard
16
16
  ([C23 working draft N3220](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf)).
17
17
 
18
+ ## Support Scope
19
+
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. Incremental parsing works as in upstream tree-sitter
22
+ but is not tested here, and no queries (highlights, locals, injections, tags) are shipped. Node types, field names, and
23
+ the trees that error recovery produces may change; such changes are released as new major versions.
24
+
18
25
  ## Usage
19
26
 
20
27
  The npm package ships `tree-sitter-c.wasm` for
21
- [@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version 15.0.0 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:
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:
22
29
 
23
30
  ```js
24
31
  import { fileURLToPath } from 'node:url';
@@ -57,7 +64,7 @@ const parser = new Parser();
57
64
  parser.setLanguage(await Language.load(c));
58
65
  ```
59
66
 
60
- The package also ships `grammar.js`, scanner sources under `src/`, the queries in `queries/`,
67
+ The package also ships `grammar.js`, scanner sources under `src/`,
61
68
  and the node types in `src/node-types.json` for grammars extending C (such as C++).
62
69
  The scanner sources include `scanner.c`, `pragma.h`, `identifier.h`, `keywords.h`, `functionMacro.h`,
63
70
  `conditionalMacro.h`, and `gnuRawString.h`.
@@ -72,13 +79,44 @@ Port `_conditional_marker_ifdef`, `_conditional_marker_ifndef`, `_leading_condit
72
79
  when both contexts are valid, choose the leading role if its type proof succeeds, otherwise use the valid
73
80
  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
74
81
  the function-annotation guards and return immediately after a failed directive scan.
75
- The hidden `_function_attribute_start`, `_post_type_function_attribute_start`, and
76
- `_post_type_function_attribute_suffix` guards look ahead before the ordinary identifier token;
77
- keep them outside the name field so identifier leaves and ordinary C lexing retain their shape.
82
+ The hidden `_function_attribute_start`, `_post_type_function_attribute_start`,
83
+ `_post_type_function_attribute_suffix`, and `_braced_function_attribute_start` guards look ahead before the ordinary
84
+ identifier token. Keep them outside the name field so identifier leaves and ordinary C lexing retain their shape.
78
85
  Port `scan_function_attribute_guard`, its `MacroAttributeSymbols` context, and the name-range handling in `pragma.h`.
86
+ Set `braced_start` to the inherited braced token and `allow_braced_start` from its valid-symbol entry.
87
+ Omitting these fields disables braced handoff in the current argument group and in prototype-tail lookahead.
88
+ Preserve the inherited external-token order before adding tokens specific to the derived grammar.
89
+
90
+ Port `_braced_macro_type_start` and `scan_braced_macro_type_start` before function-annotation lookahead.
91
+ This separate zero-width guard proves marker words followed by a braced macro-type argument; the grammar still
92
+ consumes each marker and validates its type. Keep `MacroName.preserve_end` in `pragma.h` so scanning subsequent words
93
+ does not move the guard's end. Its default is false for ordinary name scans. Derived grammars can supply reserved
94
+ 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 both `functionMacro.h` and `pragma.h`
96
+ when porting this dispatch. Later pragma lookahead must preserve the zero-width token end.
97
+
98
+ When overriding declarations or function definitions, retain both guarded ordinary-type/declarator alternatives and
99
+ annotation alternatives in ordinary, block, and for-declaration builders. Accept the matching passive guard after calling conventions and before ordinary, builtin,
100
+ and initialized declarators. Derived custom declaration builders also need `_guarded_type_specifiers` and
101
+ `_marked_macro_type_specifiers`; adapt their conflicts alongside these productions. Recognized GNU and C23 attributes
102
+ also anchor the guarded type route. Retain that route in custom block and for-declaration builders. Keep the marked-type rule's
103
+ ordinary type and annotation alternatives after the prefix guard, so typedef-return prototypes remain prototypes.
104
+ Guard-owned initialized declarators also need their `_declarator` supertype alias; accepting the syntax alone does not
105
+ preserve subtype queries. Keep ordinary initializers on their existing declarator family. Custom declaration builders
106
+ must admit `_bare_marker_initializer_declaration` where they need the published bare-name initializer preference.
107
+ If their ordinary function declarators reject C's opaque trailing identifiers, adapt that private initializer route
108
+ separately and retain its `_declarator` alias.
109
+ If a derived `type_specifier` omits C macro types, its anchored `_guarded_type_specifier` must explicitly retain them
110
+ to preserve braced macro-return function bodies. Its annotation type preguard must retain the derived grammar's
111
+ anchor policy as well, so unanchored recovery keeps the complete return prefix. These adaptations do not enable
112
+ unanchored macro types globally.
113
+ Without these adapters, braced parameters, default arguments, or direct initializers can become annotations and lose
114
+ their declaration boundaries. Copy the scanner headers from the same published C version
115
+ as the grammar, update its external-token enum, then regenerate the derived parser.
79
116
  The `_trailing_object_declaration_start` guard checks an object prefix before declaration parsing;
80
117
  `_trailing_macro_attribute_name` consumes only the annotation name and preserves its ordinary identifier leaf.
81
118
  Keep the shared first-word dispatch so a failed lookahead never falls back from an advanced lexer cursor.
119
+ When both post-type guards are valid, prefer the continuation suffix so annotation chains remain on their existing path.
82
120
  First prototype guards check the prototype tail; post-type continuation guards inspect only their own argument group.
83
121
  Regenerate Unicode and keyword tables from the derived grammar's metadata.
84
122
  The generation source is `script/generateIdentifierRanges.js` in this repository.
@@ -86,13 +124,43 @@ The primitive keyword data shared by grammar generation and object-prefix classi
86
124
  The `_preproc_function_name` token ends before splices used to determine adjacency, so the `_preproc_lparen`
87
125
  scanner must skip those splices again to reach `(`.
88
126
 
89
- Prototype annotation lookahead accepts argument/prototype groups without unquoted braces, semicolons or preprocessing directives.
90
- Quoted punctuation remains literal content. GNU statement-expression, inline-tag, compound-literal
91
- and directive-bearing annotation arguments need a separate grammar design to keep malformed-input recovery bounded.
92
-
93
- Header annotation declarations use `_macro_modified_declaration`, `_macro_declaration_specifiers`,
127
+ Annotation lookahead hands the first unquoted brace in an argument group to the grammar instead of scanning the
128
+ whole braced body. The argument grammar handles compound literals, inline types, aggregate initializers, and GNU
129
+ statement expressions. Proved braced arguments also accept named declarations such as `register int a[sizeof(struct { int m; })]`,
130
+ including declaration specifiers and trailing GNU attributes. Their `parameter_declaration` nodes require a name and retain
131
+ public type and declarator subtype queries. Quoted punctuation and comments remain literal content. Semicolons and preprocessing directives
132
+ encountered before an unknown annotation's braced handoff reject that lookahead; directive-bearing arguments need
133
+ separate support. The marker proof balances recognized declaration-modifier groups, including inline types inside
134
+ attribute arguments, in one pass. Those recognized groups do not turn an attribute's internal brace into the following
135
+ macro type's handoff. Type-constructor operands and tagged definition bodies in a marked prefix
136
+ likewise keep their own braces.
137
+ Balanced groups followed by a later braced annotation retain their original guard, so typed annotation chains remain
138
+ annotations. Guarded macro types require a first type descriptor; additional arguments are grammar-parsed only under
139
+ the current-group braced hint. A semicolon can terminate an anchored declaration macro without consuming the next
140
+ declaration.
141
+
142
+ Bare initialized names remain ambiguous without preprocessing or name binding: `Word ATTR(struct { int m; }) value = {0};`
143
+ can declare an integer when `Word` is a typedef and `ATTR` is empty, or a structure when `Word` is empty and `ATTR`
144
+ selects its argument. For a genuinely bare prefix with an initialized first declarator, the grammar preserves the
145
+ published first type-name preference, even though the remaining declarator can be approximate. Uninitialized marked
146
+ types and prefixes with recognized declaration attributes or known specifiers keep the concrete macro-type route.
147
+ This preference preserves query fields; it does not resolve both macro expansions semantically.
148
+
149
+ A proved braced macro call can also supply a declarator. Its `function_declarator` keeps the macro name in `declarator`
150
+ and the structural argument group in `parameters`, so a physical semicolon, comma, initializer or function body
151
+ terminates that role without absorbing the next declaration or statement. Retain this guarded alternative and its
152
+ `_declarator` supertype alias in custom declaration-item and function-definition builders. Derived grammars with
153
+ constructor syntax keep their ordinary constructor preference; use guarded macro-call initializer/body alternatives
154
+ where that ordinary route cannot complete. The call ends at its argument group; a following identifier still needs
155
+ the annotation alternative. Normal and old-style GNU nested
156
+ function definitions retain the same guarded/marked type and annotation alternatives as top-level definitions. Macro calls and
157
+ marked function headers remain structural; they do not resolve preprocessing or name bindings.
158
+
159
+ Annotation declarations use `_macro_modified_declaration`, `_macro_declaration_specifiers`,
94
160
  `_macro_declaration_declarator`, and `_header_preproc_item`. Adapt these rules alongside public declaration/type
95
- overrides when extending C. The annotation declarator uses `function_declarator` to retain trailing macro calls.
161
+ overrides when extending C. Keep `_macro_only_declaration` as a complete semicolon-terminated item in header, block and for-declaration contexts. Allow
162
+ `_macro_declaration_specifiers` in function definitions so their bodies stay attached. The annotation declarator uses
163
+ `function_declarator` to retain trailing macro calls.
96
164
  The separate `_trailing_macro_modified_declaration` route uses public declaration specifiers and
97
165
  `_trailing_macro_object_declarator` / `_trailing_macro_pointer_declarator`; adapt these header rules when
98
166
  overriding declaration or declarator behavior.
@@ -122,12 +190,12 @@ arrays, parenthesized/function declarators, initializers and multiple declarator
122
190
 
123
191
  In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-c) and on
124
192
  [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
125
- fuzzed with, at version 15.0.0 or later within 15.x. No other runtime loads the parser:
193
+ fuzzed with, at version 15.0.3 or later within 15.x. No other runtime loads the parser:
126
194
 
127
195
  ```toml
128
196
  [dependencies]
129
- tree-sitter = { package = "willbooster-tree-sitter", version = "15.0.0" }
130
- tree-sitter-c = { package = "willbooster-tree-sitter-c", version = "18" }
197
+ tree-sitter = { package = "willbooster-tree-sitter", version = "15.0.3" }
198
+ tree-sitter-c = { package = "willbooster-tree-sitter-c", version = "20" }
131
199
  ```
132
200
 
133
201
  ```rust
@@ -163,9 +231,6 @@ and the release build regenerate the parsers before compiling them.
163
231
  `bun run test` runs:
164
232
 
165
233
  - the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK);
166
- - an incremental-parsing check (`test/unit/incremental.test.ts`): `script/fuzz-corpus` runs `tree-sitter fuzz`, which
167
- edits each corpus case at random, reparses it, undoes the edits, and reparses again. `TREE_SITTER_SEED`,
168
- `TREE_SITTER_ITERATIONS`, and `TREE_SITTER_EDITS` run other or more edits;
169
234
  - a check that the real-world C files in `examples/`, the checked-in ones and those of the cloned repositories, fail to
170
235
  parse exactly as listed in `script/known-failures.txt`. Many of the listed files use preprocessor conditionals or
171
236
  macros that tree-sitter parses without expanding them. The first run clones the repositories. The example
@@ -181,8 +246,7 @@ and the release build regenerate the parsers before compiling them.
181
246
  Cloudflare Workers with and without Node.js compatibility (`test/e2e/workers.test.ts`).
182
247
 
183
248
  The tests and `script/parse-examples` compile the parser into `.tmp/tree-sitter-lib` rather than the CLI's cache shared
184
- by every checkout; `script/fuzz-corpus` builds a parser of its own in `.tmp/fuzz` for each run and deletes it
185
- afterwards.
249
+ by every checkout.
186
250
 
187
251
  CI also runs these tests on Linux arm64 and macOS, where the Rust binding compiles the parser natively, and fuzzes the
188
252
  parser with libFuzzer and sanitizers (`.github/workflows/robustness.yml`).