@willbooster/tree-sitter-c 19.0.0 → 20.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 +74 -13
- package/grammar.js +527 -81
- package/package.json +3 -3
- package/src/functionMacro.h +209 -29
- package/src/keywords.h +37 -0
- package/src/node-types.json +60 -22
- package/src/pragma.h +8 -2
- package/src/scanner.c +9 -3
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +1 -1
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ This fork fixes parsing bugs and raises conformance with the ISO C standard
|
|
|
18
18
|
## Usage
|
|
19
19
|
|
|
20
20
|
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.
|
|
21
|
+
[@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
22
|
|
|
23
23
|
```js
|
|
24
24
|
import { fileURLToPath } from 'node:url';
|
|
@@ -72,13 +72,44 @@ Port `_conditional_marker_ifdef`, `_conditional_marker_ifndef`, `_leading_condit
|
|
|
72
72
|
when both contexts are valid, choose the leading role if its type proof succeeds, otherwise use the valid
|
|
73
73
|
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
74
|
the function-annotation guards and return immediately after a failed directive scan.
|
|
75
|
-
The hidden `_function_attribute_start`, `_post_type_function_attribute_start`,
|
|
76
|
-
`_post_type_function_attribute_suffix` guards look ahead before the ordinary
|
|
77
|
-
|
|
75
|
+
The hidden `_function_attribute_start`, `_post_type_function_attribute_start`,
|
|
76
|
+
`_post_type_function_attribute_suffix`, and `_braced_function_attribute_start` guards look ahead before the ordinary
|
|
77
|
+
identifier token. Keep them outside the name field so identifier leaves and ordinary C lexing retain their shape.
|
|
78
78
|
Port `scan_function_attribute_guard`, its `MacroAttributeSymbols` context, and the name-range handling in `pragma.h`.
|
|
79
|
+
Set `braced_start` to the inherited braced token and `allow_braced_start` from its valid-symbol entry.
|
|
80
|
+
Omitting these fields disables braced handoff in the current argument group and in prototype-tail lookahead.
|
|
81
|
+
Preserve the inherited external-token order before adding tokens specific to the derived grammar.
|
|
82
|
+
|
|
83
|
+
Port `_braced_macro_type_start` and `scan_braced_macro_type_start` before function-annotation lookahead.
|
|
84
|
+
This separate zero-width guard proves marker words followed by a braced macro-type argument; the grammar still
|
|
85
|
+
consumes each marker and validates its type. Keep `MacroName.preserve_end` in `pragma.h` so scanning subsequent words
|
|
86
|
+
does not move the guard's end. Its default is false for ordinary name scans. Derived grammars can supply reserved
|
|
87
|
+
starter words and the same `MacroAttributeSymbols` context to the guard helper; the C scanner supplies an empty table.
|
|
88
|
+
The helper shares pragma and scalar-object dispatch with annotation guards. Copy both `functionMacro.h` and `pragma.h`
|
|
89
|
+
when porting this dispatch. Later pragma lookahead must preserve the zero-width token end.
|
|
90
|
+
|
|
91
|
+
When overriding declarations or function definitions, retain both guarded ordinary-type/declarator alternatives and
|
|
92
|
+
annotation alternatives in ordinary, block, and for-declaration builders. Accept the matching passive guard after calling conventions and before ordinary, builtin,
|
|
93
|
+
and initialized declarators. Derived custom declaration builders also need `_guarded_type_specifiers` and
|
|
94
|
+
`_marked_macro_type_specifiers`; adapt their conflicts alongside these productions. Recognized GNU and C23 attributes
|
|
95
|
+
also anchor the guarded type route. Retain that route in custom block and for-declaration builders. Keep the marked-type rule's
|
|
96
|
+
ordinary type and annotation alternatives after the prefix guard, so typedef-return prototypes remain prototypes.
|
|
97
|
+
Guard-owned initialized declarators also need their `_declarator` supertype alias; accepting the syntax alone does not
|
|
98
|
+
preserve subtype queries. Keep ordinary initializers on their existing declarator family. Custom declaration builders
|
|
99
|
+
must admit `_bare_marker_initializer_declaration` where they need the published bare-name initializer preference.
|
|
100
|
+
If their ordinary function declarators reject C's opaque trailing identifiers, adapt that private initializer route
|
|
101
|
+
separately and retain its `_declarator` alias.
|
|
102
|
+
If a derived `type_specifier` omits C macro types, its anchored `_guarded_type_specifier` must explicitly retain them
|
|
103
|
+
to preserve braced macro-return function bodies. Its annotation type preguard must retain the derived grammar's
|
|
104
|
+
anchor policy as well, so unanchored recovery keeps the complete return prefix. These adaptations do not enable
|
|
105
|
+
unanchored macro types globally.
|
|
106
|
+
Without these adapters, braced parameters, default arguments, or direct initializers can become annotations and lose
|
|
107
|
+
their declaration boundaries. Copy the scanner headers from the same published C version
|
|
108
|
+
as the grammar, update its external-token enum, then regenerate the derived parser.
|
|
79
109
|
The `_trailing_object_declaration_start` guard checks an object prefix before declaration parsing;
|
|
80
110
|
`_trailing_macro_attribute_name` consumes only the annotation name and preserves its ordinary identifier leaf.
|
|
81
111
|
Keep the shared first-word dispatch so a failed lookahead never falls back from an advanced lexer cursor.
|
|
112
|
+
When both post-type guards are valid, prefer the continuation suffix so annotation chains remain on their existing path.
|
|
82
113
|
First prototype guards check the prototype tail; post-type continuation guards inspect only their own argument group.
|
|
83
114
|
Regenerate Unicode and keyword tables from the derived grammar's metadata.
|
|
84
115
|
The generation source is `script/generateIdentifierRanges.js` in this repository.
|
|
@@ -86,13 +117,43 @@ The primitive keyword data shared by grammar generation and object-prefix classi
|
|
|
86
117
|
The `_preproc_function_name` token ends before splices used to determine adjacency, so the `_preproc_lparen`
|
|
87
118
|
scanner must skip those splices again to reach `(`.
|
|
88
119
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
120
|
+
Annotation lookahead hands the first unquoted brace in an argument group to the grammar instead of scanning the
|
|
121
|
+
whole braced body. The argument grammar handles compound literals, inline types, aggregate initializers, and GNU
|
|
122
|
+
statement expressions. Proved braced arguments also accept named declarations such as `register int a[sizeof(struct { int m; })]`,
|
|
123
|
+
including declaration specifiers and trailing GNU attributes. Their `parameter_declaration` nodes require a name and retain
|
|
124
|
+
public type and declarator subtype queries. Quoted punctuation and comments remain literal content. Semicolons and preprocessing directives
|
|
125
|
+
encountered before an unknown annotation's braced handoff reject that lookahead; directive-bearing arguments need
|
|
126
|
+
separate support. The marker proof balances recognized declaration-modifier groups, including inline types inside
|
|
127
|
+
attribute arguments, in one pass. Those recognized groups do not turn an attribute's internal brace into the following
|
|
128
|
+
macro type's handoff. Type-constructor operands and tagged definition bodies in a marked prefix
|
|
129
|
+
likewise keep their own braces.
|
|
130
|
+
Balanced groups followed by a later braced annotation retain their original guard, so typed annotation chains remain
|
|
131
|
+
annotations. Guarded macro types require a first type descriptor; additional arguments are grammar-parsed only under
|
|
132
|
+
the current-group braced hint. A semicolon can terminate an anchored declaration macro without consuming the next
|
|
133
|
+
declaration.
|
|
134
|
+
|
|
135
|
+
Bare initialized names remain ambiguous without preprocessing or name binding: `Word ATTR(struct { int m; }) value = {0};`
|
|
136
|
+
can declare an integer when `Word` is a typedef and `ATTR` is empty, or a structure when `Word` is empty and `ATTR`
|
|
137
|
+
selects its argument. For a genuinely bare prefix with an initialized first declarator, the grammar preserves the
|
|
138
|
+
published first type-name preference, even though the remaining declarator can be approximate. Uninitialized marked
|
|
139
|
+
types and prefixes with recognized declaration attributes or known specifiers keep the concrete macro-type route.
|
|
140
|
+
This preference preserves query fields; it does not resolve both macro expansions semantically.
|
|
141
|
+
|
|
142
|
+
A proved braced macro call can also supply a declarator. Its `function_declarator` keeps the macro name in `declarator`
|
|
143
|
+
and the structural argument group in `parameters`, so a physical semicolon, comma, initializer or function body
|
|
144
|
+
terminates that role without absorbing the next declaration or statement. Retain this guarded alternative and its
|
|
145
|
+
`_declarator` supertype alias in custom declaration-item and function-definition builders. Derived grammars with
|
|
146
|
+
constructor syntax keep their ordinary constructor preference; use guarded macro-call initializer/body alternatives
|
|
147
|
+
where that ordinary route cannot complete. The call ends at its argument group; a following identifier still needs
|
|
148
|
+
the annotation alternative. Normal and old-style GNU nested
|
|
149
|
+
function definitions retain the same guarded/marked type and annotation alternatives as top-level definitions. Macro calls and
|
|
150
|
+
marked function headers remain structural; they do not resolve preprocessing or name bindings.
|
|
151
|
+
|
|
152
|
+
Annotation declarations use `_macro_modified_declaration`, `_macro_declaration_specifiers`,
|
|
94
153
|
`_macro_declaration_declarator`, and `_header_preproc_item`. Adapt these rules alongside public declaration/type
|
|
95
|
-
overrides when extending C.
|
|
154
|
+
overrides when extending C. Keep `_macro_only_declaration` as a complete semicolon-terminated item in header, block and for-declaration contexts. Allow
|
|
155
|
+
`_macro_declaration_specifiers` in function definitions so their bodies stay attached. The annotation declarator uses
|
|
156
|
+
`function_declarator` to retain trailing macro calls.
|
|
96
157
|
The separate `_trailing_macro_modified_declaration` route uses public declaration specifiers and
|
|
97
158
|
`_trailing_macro_object_declarator` / `_trailing_macro_pointer_declarator`; adapt these header rules when
|
|
98
159
|
overriding declaration or declarator behavior.
|
|
@@ -122,12 +183,12 @@ arrays, parenthesized/function declarators, initializers and multiple declarator
|
|
|
122
183
|
|
|
123
184
|
In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-c) and on
|
|
124
185
|
[willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
|
|
125
|
-
fuzzed with, at version 15.0.
|
|
186
|
+
fuzzed with, at version 15.0.3 or later within 15.x. No other runtime loads the parser:
|
|
126
187
|
|
|
127
188
|
```toml
|
|
128
189
|
[dependencies]
|
|
129
|
-
tree-sitter = { package = "willbooster-tree-sitter", version = "15.0.
|
|
130
|
-
tree-sitter-c = { package = "willbooster-tree-sitter-c", version = "
|
|
190
|
+
tree-sitter = { package = "willbooster-tree-sitter", version = "15.0.3" }
|
|
191
|
+
tree-sitter-c = { package = "willbooster-tree-sitter-c", version = "20" }
|
|
131
192
|
```
|
|
132
193
|
|
|
133
194
|
```rust
|