@willbooster/tree-sitter-c 42.0.0 → 44.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
@@ -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 23.0.0 or later within 23.x, which runs in Node.js, Bun, browsers, and Cloudflare Workers. The parser carries the ABI number of the WillBooster/tree-sitter generator (`0x5742000d`), 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 23.1.0 or later within 23.x, which runs in Node.js, Bun, browsers, and Cloudflare Workers. The parser carries the ABI number of the WillBooster/tree-sitter generator (`0x5742000e`), 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';
@@ -66,19 +66,25 @@ 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`, `directiveScan.h`, `macroProof.h`, and `gnuRawString.h`.
69
+ `conditionalMacro.h`, `emptyDirectives.h`, `directiveFacts.h`, `directiveBuffer.h`, `directiveScan.h`,
70
+ `directiveUnits.h`, `macroProof.h`, and `gnuRawString.h`.
70
71
 
71
72
  Derived grammars must port the inherited external-token dispatch from `src/scanner.c`.
73
+ The headers call `peek_units` and `advance_units` of `TSLexer`, so generate the derived parser with the CLI of
74
+ WillBooster/tree-sitter 23.1.0 or later, whose `src/tree_sitter/parser.h` declares them, and run it on that runtime.
72
75
  Append `_directive_context` and `preproc_empty` in their inherited external-token slots and retain them as extras.
73
- Append `_preproc_directive_start` in its inherited slot as a zero-width guard after every directive keyword token,
74
- including keywords in overridden directive rules. It is not an extra. Retain the phase-two splice handling in
75
- `preprocessorToken` when overriding keyword spelling.
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 `empty_directives_release` in scanner destruction. When the scanner is ready and no external token
78
- other than `pragma_operator`, `_directive_context` and `preproc_empty` is valid, dispatch to `scan_extras` first;
79
- `only_extras_valid` checks the inherited slots only, so extend it to every external token the derived grammar adds,
80
- or the fast path drops those tokens. Otherwise run `scan_directive_context` first,
81
- emitting `_preproc_directive_start` only at `empty_directives_after_keyword` before null-directive consumption,
76
+ Append `_preproc_include`, `_preproc_define`, `_preproc_if`, `_preproc_ifdef`, `_preproc_ifndef`, `_preproc_else`,
77
+ `_preproc_elif`, `_preproc_elifdef`, `_preproc_elifndef`, `_preproc_endif`, `_preproc_pragma`, and `preproc_directive`
78
+ in their inherited slots, and scan `#` directives with `empty_directives_scan_word`, `empty_directives_has_keyword_end`,
79
+ and `empty_directives_select_symbol`, which check the logical-line start and keyword-end facts recorded in `EmptyDirectives`.
80
+ Port the scanner lifecycle functions together with `EmptyDirectives` from `directiveFacts.h` and `emptyDirectives.h`; the source-boundary
81
+ facts belong to one full parse and are freed by `empty_directives_release` in scanner destruction. The entry fast path in
82
+ `tree_sitter_c_external_scanner_scan` (together with `scan_c_token_spaced_fast`), `only_extras_valid`, `any_preproc_body_valid`,
83
+ `can_start_spaced_c_token`, and the `peek_after_ascii_spaces` precheck in `scan_active_tokens` inspect only the inherited external-token slots
84
+ and starter characters before `scan_c_token_slow` or token-specific lookahead runs; extend or bypass every one of those predicates for each
85
+ external token the derived grammar adds (including unspaced tokens, error-recovery slots, and spaced punctuation or identifier starters), or
86
+ those filters will reject the derived tokens before their scanner runs. When the scanner is ready and only extras, conditional markers,
87
+ `_trailing_object_declaration_start`, or external directive tokens are valid, dispatch to `scan_extras` first. Otherwise check null-directive consumption before directive-body tokens,
82
88
  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
83
89
  `empty_context` so declaration lookahead ignores null directives without moving the opening token's range.
84
90
  Keep preprocessor argument/newline handling ahead of annotation lookahead.
@@ -86,15 +92,15 @@ Port `preproc_argument_mode` selection together with `scan_preproc_arg` so token
86
92
  Append `_gnu_raw_string_body` and `_raw_macro_type_tail` in their inherited enum slots and port their dispatch from `src/scanner.c`.
87
93
  A derived grammar overriding `raw_string_literal` can keep its own body scanner, but must retain the inherited enum slots.
88
94
  Port `_conditional_marker_ifdef`, `_conditional_marker_ifndef`, `_leading_conditional_marker_ifdef`, and
89
- `_leading_conditional_marker_ifndef` with `scan_conditional_macro`. Require `empty_directives_at_start` at the opening
95
+ `_leading_conditional_marker_ifndef` with `scan_conditional_macro_after_keyword`. Require `empty_directives_at_start` at the opening
90
96
  `#` before this dispatch, including after skipped whitespace. The leading pair checks a following type prefix;
91
97
  when both contexts are valid, choose the leading role if its type proof succeeds, otherwise use the valid
92
98
  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
93
- the function-annotation guards and return immediately after a failed directive scan.
99
+ the function-annotation guards and fall back to the inherited directive token when the marker proof fails.
94
100
  The hidden `_function_attribute_start`, `_post_type_function_attribute_start`,
95
101
  `_post_type_function_attribute_suffix`, and `_braced_function_attribute_start` guards look ahead before the ordinary
96
102
  identifier token. Keep them outside the name field so identifier leaves and ordinary C lexing retain their shape.
97
- Port `scan_function_attribute_guard`, its `MacroAttributeSymbols` context, and the name-range handling in `pragma.h`.
103
+ Port `scan_function_attribute_guard_from`, its `MacroAttributeSymbols` context, and the name-range handling in `pragma.h`.
98
104
  Set `MacroAttributeSymbols.trivia` to `{empty_directives_skip, state}` so annotation, argument-group, and declaration-tail proofs skip the same null directives.
99
105
  Pass that `PragmaTrivia` context to the raw macro-type tail and balanced-group helpers, and use `scan_pragma_with` for the pragma-operator fallback.
100
106
  A derived scanner that omits this context cannot recognize null directives inside those lookahead paths.
@@ -102,7 +108,7 @@ Set `braced_start` to the inherited braced token and `allow_braced_start` from i
102
108
  Omitting these fields disables braced handoff in the current argument group and in prototype-tail lookahead.
103
109
  Preserve the inherited external-token order before adding tokens specific to the derived grammar.
104
110
 
105
- Port `_braced_macro_type_start` and `scan_braced_macro_type_start` before function-annotation lookahead.
111
+ Port `_braced_macro_type_start` and `scan_braced_macro_type_start` (or `scan_braced_macro_type_start_from` when a shared leading-trivia pass has already consumed an opening UCN backslash) before function-annotation lookahead.
106
112
  This separate zero-width guard proves marker words followed by a braced macro-type argument; the grammar still
107
113
  consumes each marker and validates its type. Keep `MacroName.preserve_end` in `pragma.h` so scanning subsequent words
108
114
  does not move the guard's end. Its default is false for ordinary name scans. Derived grammars can supply reserved
@@ -211,10 +217,10 @@ arrays, parenthesized/function declarators, initializers and multiple declarator
211
217
 
212
218
  In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-c) and on
213
219
  [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
214
- fuzzed with, at 23.x (`Cargo.lock` records the exact version):
220
+ fuzzed with, at 23.1 or later within 23.x (`Cargo.lock` records the exact version):
215
221
 
216
222
  ```sh
217
- cargo add willbooster-tree-sitter@23 --rename tree-sitter
223
+ cargo add willbooster-tree-sitter@23.1 --rename tree-sitter
218
224
  cargo add willbooster-tree-sitter-c --rename tree-sitter-c
219
225
  ```
220
226
 
package/grammar.js CHANGED
@@ -95,7 +95,6 @@ const BLOCK_COMMENT = seq(
95
95
  COMMENT_SPLICES,
96
96
  '/'
97
97
  );
98
- const PREPROC_PREFIX = seq('#', repeat(choice(/[ \t\v\f]/, LINE_SPLICE, BLOCK_COMMENT)));
99
98
  const PRAGMA_SPACING = repeat(choice(/\s/, /\\(?:\r\n?|\n\r?)/));
100
99
  const PREPROC_ARGUMENT = /\S([^/\n]|\/[^*]|\\\r?\n)*/;
101
100
  const VA_ARG_KEYWORDS = choice('va_arg', '__builtin_va_arg');
@@ -541,7 +540,31 @@ module.exports = Object.assign(
541
540
  sym('_braced_macro_type_start'),
542
541
  sym('_directive_context'),
543
542
  sym('preproc_empty'),
544
- sym('_preproc_directive_start'),
543
+ sym('_preproc_include'),
544
+ sym('_preproc_define'),
545
+ sym('_preproc_if'),
546
+ sym('_preproc_ifdef'),
547
+ sym('_preproc_ifndef'),
548
+ sym('_preproc_else'),
549
+ sym('_preproc_elif'),
550
+ sym('_preproc_elifdef'),
551
+ sym('_preproc_elifndef'),
552
+ sym('_preproc_endif'),
553
+ sym('_preproc_pragma'),
554
+ sym('preproc_directive'),
555
+ ],
556
+
557
+ mergeableExternals: () => [
558
+ sym('_preproc_if'),
559
+ sym('_preproc_ifdef'),
560
+ sym('_preproc_ifndef'),
561
+ sym('_preproc_else'),
562
+ sym('_preproc_elif'),
563
+ sym('_preproc_elifdef'),
564
+ sym('_preproc_elifndef'),
565
+ sym('_preproc_endif'),
566
+ sym('_preproc_pragma'),
567
+ sym('preproc_directive'),
545
568
  ],
546
569
 
547
570
  extras: ($) => [
@@ -752,10 +775,10 @@ module.exports = Object.assign(
752
775
  choice(
753
776
  preprocCall(
754
777
  $,
755
- alias(preprocessorToken('pragma'), $.preproc_directive),
778
+ alias(sym('_preproc_pragma'), sym('preproc_directive')),
756
779
  choice($._preproc_directive_arg, $.preproc_arg)
757
780
  ),
758
- preprocCall($, $.preproc_directive, $._preproc_directive_arg)
781
+ preprocCall($, sym('preproc_directive'), $._preproc_directive_arg)
759
782
  ),
760
783
 
761
784
  ...preprocIf('_in_header', () => sym('_header_preproc_item')),
@@ -764,20 +787,43 @@ module.exports = Object.assign(
764
787
  ...preprocIf('_in_single_case', () => sym('_single_case_body'), 0, true),
765
788
  ...preprocIf('_in_field_declaration_list', () => sym('_field_declaration_list_item')),
766
789
  ...preprocIf('_in_enumerator_list', () => sym('_enumerator_list_item')),
790
+ ...preprocIf('_in_enumerator_directives', () => sym('_enumerator_directive')),
767
791
  ...preprocIf(
768
792
  '_in_enumerator_list_no_comma',
769
793
  () =>
770
794
  choice(
771
- seq(repeat(sym('_enumerator_list_item')), sym('enumerator'), optional(sym('_enumerator_directives'))),
795
+ seq(
796
+ repeat(sym('_enumerator_list_item')),
797
+ choice(
798
+ sym('enumerator'),
799
+ alias(sym('preproc_if_in_enumerator_list_no_comma'), sym('preproc_if')),
800
+ alias(sym('preproc_ifdef_in_enumerator_list_no_comma'), sym('preproc_ifdef'))
801
+ ),
802
+ optional(sym('_enumerator_directives'))
803
+ ),
772
804
  repeat1(sym('_enumerator_list_item'))
773
805
  ),
774
806
  0,
775
807
  false
776
808
  ),
777
809
 
778
- _enumerator_list_item: ($) => choice(seq($.enumerator, ','), $._enumerator_macro, $._enumerator_directives),
810
+ _enumerator_list_item: ($) =>
811
+ choice(
812
+ seq($.enumerator, ','),
813
+ $._enumerator_macro,
814
+ prec.right(repeat1($.preproc_call)),
815
+ alias(sym('preproc_if_in_enumerator_list'), sym('preproc_if')),
816
+ alias(sym('preproc_ifdef_in_enumerator_list'), sym('preproc_ifdef'))
817
+ ),
818
+
819
+ _enumerator_directive: ($) =>
820
+ choice(
821
+ $.preproc_call,
822
+ alias(sym('preproc_if_in_enumerator_directives'), sym('preproc_if')),
823
+ alias(sym('preproc_ifdef_in_enumerator_directives'), sym('preproc_ifdef'))
824
+ ),
779
825
 
780
- _enumerator_directives: ($) => prec.right(repeat1($.preproc_call)),
826
+ _enumerator_directives: ($) => prec.right(repeat1($._enumerator_directive)),
781
827
 
782
828
  _preproc_newline: () => token.immediate(/\r?\n/),
783
829
 
@@ -786,7 +832,6 @@ module.exports = Object.assign(
786
832
  preproc_arg: () => token(prec(-1, PREPROC_ARGUMENT)),
787
833
 
788
834
  _preproc_directive_arg: () => token(prec(-2, PREPROC_ARGUMENT)),
789
- preproc_directive: () => token(seq(PREPROC_PREFIX, /[a-zA-Z0-9]/, repeat(seq(repeat(LINE_SPLICE), /\w/)))),
790
835
 
791
836
  _preproc_expression: ($) =>
792
837
  choice(
@@ -1684,10 +1729,10 @@ module.exports = Object.assign(
1684
1729
  $.enumerator,
1685
1730
  alias(sym('preproc_if_in_enumerator_list_no_comma'), sym('preproc_if')),
1686
1731
  alias(sym('preproc_ifdef_in_enumerator_list_no_comma'), sym('preproc_ifdef'))
1687
- )
1732
+ ),
1733
+ optional($._enumerator_directives)
1688
1734
  )
1689
1735
  ),
1690
- repeat($.preproc_call),
1691
1736
  '}'
1692
1737
  ),
1693
1738
 
@@ -2689,7 +2734,6 @@ function declarationSpecifiers($, braced = false) {
2689
2734
  function preprocCall($, directive, argument) {
2690
2735
  return seq(
2691
2736
  field('directive', directive),
2692
- sym('_preproc_directive_start'),
2693
2737
  repeat(choice($.comment, $.pragma_operator)),
2694
2738
  optional(seq(field('argument', alias(argument, $.preproc_arg)), repeat(choice($.comment, $.pragma_operator)))),
2695
2739
  sym('_preproc_newline')
@@ -3103,18 +3147,10 @@ function preprocIf(suffix, content, precedence = 0, repeatContent = true) {
3103
3147
  *
3104
3148
  * @param {string} command
3105
3149
  *
3106
- * @returns {import('./types/treeSitterDsl').SeqRule}
3150
+ * @returns {import('./types/treeSitterDsl').AliasRule}
3107
3151
  */
3108
3152
  function preprocessor(command) {
3109
- return seq(alias(preprocessorToken(command), '#' + command), sym('_preproc_directive_start'));
3110
- }
3111
-
3112
- /** @param {string} command */
3113
- function preprocessorToken(command) {
3114
- const spelling = Array.from(command, (character, index) =>
3115
- index === 0 ? character : seq(repeat(LINE_SPLICE), character)
3116
- );
3117
- return token(seq(PREPROC_PREFIX, ...spelling));
3153
+ return alias(sym('_preproc_' + command), '#' + command);
3118
3154
  }
3119
3155
 
3120
3156
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-c",
3
- "version": "42.0.0",
3
+ "version": "44.0.0",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "c",
@@ -26,8 +26,11 @@
26
26
  "src/functionMacro.h",
27
27
  "src/macroProof.h",
28
28
  "src/conditionalMacro.h",
29
+ "src/directiveFacts.h",
29
30
  "src/emptyDirectives.h",
30
31
  "src/directiveScan.h",
32
+ "src/directiveUnits.h",
33
+ "src/directiveBuffer.h",
31
34
  "src/gnuRawString.h",
32
35
  "src/scanner.c"
33
36
  ],
@@ -60,7 +63,7 @@
60
63
  "@vitest/browser-playwright": "5.1.0",
61
64
  "@willbooster/oxlint-config": "1.4.9",
62
65
  "@willbooster/wb": "23.6.1",
63
- "@willbooster/web-tree-sitter": "23.0.2",
66
+ "@willbooster/web-tree-sitter": "23.1.0",
64
67
  "conventional-changelog-conventionalcommits": "10.4.1",
65
68
  "lefthook": "2.2.2",
66
69
  "oxlint": "1.93.0",
@@ -71,7 +74,7 @@
71
74
  "wrangler": "4.156.0"
72
75
  },
73
76
  "peerDependencies": {
74
- "@willbooster/web-tree-sitter": "^23.0.0"
77
+ "@willbooster/web-tree-sitter": "^23.1.0"
75
78
  },
76
79
  "peerDependenciesMeta": {
77
80
  "@willbooster/web-tree-sitter": {
@@ -13,6 +13,7 @@ typedef struct {
13
13
  void *empty_context;
14
14
  } ConditionalMacroSymbols;
15
15
 
16
+ static bool scan_conditional_macro_after_keyword(TSLexer *lexer, bool ifdef, const ConditionalMacroSymbols *symbols, const bool *valid_symbols);
16
17
  static bool conditional_macro_body(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
17
18
  static bool conditional_macro_word(TSLexer *lexer, MacroName *name, bool escaped);
18
19
  static bool conditional_macro_spacing(TSLexer *lexer, bool *separated, bool *escaped, bool directive);
@@ -27,18 +28,12 @@ static bool conditional_macro_reserved_word(const ConditionalMacroSymbols *symbo
27
28
  static bool conditional_macro_modifier_group(TSLexer *lexer, const ConditionalMacroSymbols *symbols, const char *word, bool *escaped);
28
29
  static bool conditional_macro_attribute_declaration(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
29
30
 
30
- static bool scan_conditional_macro(TSLexer *lexer, const ConditionalMacroSymbols *symbols, const bool *valid_symbols) {
31
- char directive[7] = {0};
32
- MacroName command = {.word = directive, .capacity = sizeof(directive) - 1, .ascii = true};
33
- if (!conditional_macro_directive(lexer, &command) || !command.ascii) return false;
34
- bool ifdef = strcmp(directive, "ifdef") == 0;
35
- if (!ifdef && strcmp(directive, "ifndef") != 0) return false;
31
+ static bool scan_conditional_macro_after_keyword(TSLexer *lexer, bool ifdef, const ConditionalMacroSymbols *symbols, const bool *valid_symbols) {
36
32
  TSSymbol leading_symbol = ifdef ? symbols->leading_ifdef : symbols->leading_ifndef;
37
33
  TSSymbol generic_symbol = ifdef ? symbols->ifdef : symbols->ifndef;
38
34
  bool leading = valid_symbols[leading_symbol];
39
35
  bool generic = valid_symbols[generic_symbol];
40
36
  if (!leading && !generic) return false;
41
- lexer->mark_end(lexer);
42
37
 
43
38
  bool escaped = false;
44
39
  if (!conditional_macro_body(lexer, symbols, &escaped)) return false;