@willbooster/tree-sitter-c 5.1.1 → 6.0.1

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
@@ -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 2.0.0 or later, which runs in Node.js, Bun, browsers, and Cloudflare Workers. The parser carries the ABI number of the WillBooster/tree-sitter generator (`0x57420001`), so it loads only with that runtime; upstream tree-sitter runtimes reject it. In Node.js and Bun, load the grammar from its path:
21
+ [@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version 4.0.2 or later, which runs in Node.js, Bun, browsers, and Cloudflare Workers. The parser carries the ABI number of the WillBooster/tree-sitter generator (`0x57420003`), 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';
@@ -65,12 +65,12 @@ adjacency, so the parameter scanner must skip them again to reach `(`.
65
65
 
66
66
  In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-c) and on
67
67
  [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
68
- fuzzed with, at version 2.0.0 or later. No other runtime loads the parser:
68
+ fuzzed with, at version 4.0.2 or later. No other runtime loads the parser:
69
69
 
70
70
  ```toml
71
71
  [dependencies]
72
- tree-sitter = { package = "willbooster-tree-sitter", version = "2" }
73
- tree-sitter-c = { package = "willbooster-tree-sitter-c", version = "5" }
72
+ tree-sitter = { package = "willbooster-tree-sitter", version = "4.0.2" }
73
+ tree-sitter-c = { package = "willbooster-tree-sitter-c", version = "6" }
74
74
  ```
75
75
 
76
76
  ```rust
package/grammar.js CHANGED
@@ -93,8 +93,10 @@ module.exports = Object.assign(
93
93
  [$.type_definition, $.sized_type_specifier],
94
94
  [$.type_definition, $._sized_bit_int_specifier],
95
95
  [$.attributed_statement],
96
+ [$.case_statement],
96
97
  [$._single_attributed_statement],
97
- [$._declaration_modifiers, $.attributed_statement],
98
+ [$._declaration_modifiers, $._case_body_attributed_statement, $._case_label_attributes],
99
+ [$._declaration_modifiers, $.attributed_statement, $.case_statement],
98
100
  [$.enum_specifier],
99
101
  [sym('preproc_if_in_enumerator_list'), sym('preproc_if_in_enumerator_list_no_comma')],
100
102
  [sym('preproc_ifdef_in_enumerator_list'), sym('preproc_ifdef_in_enumerator_list_no_comma')],
@@ -104,7 +106,6 @@ module.exports = Object.assign(
104
106
  [$.type_specifier, $._old_style_parameter_list],
105
107
  [$.parameter_list, $._old_style_parameter_list],
106
108
  [$.function_declarator, $._function_declaration_declarator],
107
- [$._block_item, $.statement],
108
109
  [$._top_level_item, $._top_level_statement],
109
110
  [$.type_specifier, $._top_level_expression_statement],
110
111
  [$.type_qualifier, $.extension_expression],
@@ -129,7 +130,6 @@ module.exports = Object.assign(
129
130
  $._type_identifier,
130
131
  $._field_identifier,
131
132
  $._statement_identifier,
132
- $._non_case_statement,
133
133
  $._assignment_left_expression,
134
134
  $._expression_not_binary,
135
135
  ],
@@ -138,6 +138,13 @@ module.exports = Object.assign(
138
138
  $.expression,
139
139
  $.statement,
140
140
  $._single_noncompound_statement,
141
+ $._non_case_statement,
142
+ $._single_non_case_statement,
143
+ $._case_body_statement,
144
+ $._case_body_unattributed_statement,
145
+ $._case_label_tail,
146
+ $._case_label_statement,
147
+ $._case_label_chain_statement,
141
148
  $.type_specifier,
142
149
  $._declarator,
143
150
  $._old_style_declarator,
@@ -176,7 +183,6 @@ module.exports = Object.assign(
176
183
  $.linkage_specification,
177
184
  $.declaration,
178
185
  $.statement,
179
- $.attributed_statement,
180
186
  $.type_definition,
181
187
  $._empty_declaration,
182
188
  sym('preproc_if'),
@@ -217,6 +223,7 @@ module.exports = Object.assign(
217
223
  alias($.preproc_call_expression, $.call_expression)
218
224
  )
219
225
  ),
226
+ repeat(choice($.comment, $.pragma_operator)),
220
227
  sym('_preproc_newline')
221
228
  ),
222
229
 
@@ -224,7 +231,8 @@ module.exports = Object.assign(
224
231
  seq(
225
232
  preprocessor('define'),
226
233
  field('name', $.identifier),
227
- field('value', optional($.preproc_arg)),
234
+ repeat(choice($.comment, $.pragma_operator)),
235
+ optional(seq(field('value', $.preproc_arg), repeat(choice($.comment, $.pragma_operator)))),
228
236
  sym('_preproc_newline')
229
237
  ),
230
238
 
@@ -233,7 +241,8 @@ module.exports = Object.assign(
233
241
  preprocessor('define'),
234
242
  field('name', alias(sym('_preproc_function_name'), $.identifier)),
235
243
  field('parameters', $.preproc_params),
236
- field('value', optional($.preproc_arg)),
244
+ repeat(choice($.comment, $.pragma_operator)),
245
+ optional(seq(field('value', $.preproc_arg), repeat(choice($.comment, $.pragma_operator)))),
237
246
  sym('_preproc_newline')
238
247
  ),
239
248
 
@@ -242,7 +251,13 @@ module.exports = Object.assign(
242
251
  preproc_call: ($) =>
243
252
  seq(
244
253
  field('directive', $.preproc_directive),
245
- field('argument', optional(alias($._preproc_directive_arg, $.preproc_arg))),
254
+ repeat(choice($.comment, $.pragma_operator)),
255
+ optional(
256
+ seq(
257
+ field('argument', alias($._preproc_directive_arg, $.preproc_arg)),
258
+ repeat(choice($.comment, $.pragma_operator))
259
+ )
260
+ ),
246
261
  sym('_preproc_newline')
247
262
  ),
248
263
 
@@ -877,14 +892,16 @@ module.exports = Object.assign(
877
892
  repeat($.attribute_specifier)
878
893
  ),
879
894
 
880
- attributed_statement: ($) => seq(repeat1($.attribute_declaration), $.statement),
895
+ attributed_statement: ($) => seq(repeat1($.attribute_declaration), alias($._non_case_statement, $.statement)),
896
+
897
+ statement: ($) => choice($.case_statement, alias($._non_case_statement, $.statement)),
898
+
899
+ _non_case_statement: ($) => choice($.attributed_statement, $._unattributed_non_case_statement),
881
900
 
882
- statement: ($) => choice($.case_statement, $._non_case_statement),
901
+ _unattributed_non_case_statement: ($) => choice($.labeled_statement, $._unlabeled_non_case_statement),
883
902
 
884
- _non_case_statement: ($) =>
903
+ _unlabeled_non_case_statement: ($) =>
885
904
  choice(
886
- $.attributed_statement,
887
- $.labeled_statement,
888
905
  $.compound_statement,
889
906
  $.expression_statement,
890
907
  $.if_statement,
@@ -930,12 +947,12 @@ module.exports = Object.assign(
930
947
  seq(
931
948
  'if',
932
949
  field('condition', $.parenthesized_expression),
933
- field('consequence', $.statement),
950
+ field('consequence', $._single_statement),
934
951
  optional(field('alternative', $.else_clause))
935
952
  )
936
953
  ),
937
954
 
938
- else_clause: ($) => seq('else', $.statement),
955
+ else_clause: ($) => seq('else', $._single_statement),
939
956
 
940
957
  switch_statement: ($) =>
941
958
  seq('switch', field('condition', $.parenthesized_expression), field('body', $._single_statement)),
@@ -943,6 +960,9 @@ module.exports = Object.assign(
943
960
  _single_statement: ($) => choice($.compound_statement, alias($._single_noncompound_statement, $.statement)),
944
961
 
945
962
  _single_noncompound_statement: ($) =>
963
+ choice(alias($._single_case_statement, $.case_statement), alias($._single_non_case_statement, $.statement)),
964
+
965
+ _single_non_case_statement: ($) =>
946
966
  choice(
947
967
  $.expression_statement,
948
968
  $.switch_statement,
@@ -952,71 +972,103 @@ module.exports = Object.assign(
952
972
  $.goto_statement,
953
973
  $.seh_try_statement,
954
974
  $.seh_leave_statement,
955
- alias($._single_case_statement, $.case_statement),
956
975
  alias($._single_labeled_statement, $.labeled_statement),
957
- alias($._single_if_statement, $.if_statement),
958
- alias($._single_while_statement, $.while_statement),
959
- alias($._single_do_statement, $.do_statement),
960
- alias($._single_for_statement, $.for_statement),
976
+ $.if_statement,
977
+ $.while_statement,
978
+ $.do_statement,
979
+ $.for_statement,
961
980
  alias($._single_attributed_statement, $.attributed_statement)
962
981
  ),
963
982
 
964
983
  _single_case_statement: ($) =>
965
- prec.right(
966
- seq(
967
- choice(
968
- seq('case', field('value', $.expression), optional(seq('...', field('end_value', $.expression)))),
969
- 'default'
970
- ),
971
- ':',
972
- optional($._single_case_body)
984
+ prec.dynamic(
985
+ 1,
986
+ prec.right(
987
+ seq(
988
+ repeat($.attribute_declaration),
989
+ choice(
990
+ seq('case', field('value', $.expression), optional(seq('...', field('end_value', $.expression)))),
991
+ 'default'
992
+ ),
993
+ ':',
994
+ optional($._single_case_body)
995
+ )
973
996
  )
974
997
  ),
975
998
  _single_case_body: ($) =>
976
- choice(
977
- $._single_statement,
978
- $.declaration,
979
- $.type_definition,
980
- alias(sym('preproc_if_in_single_case'), sym('preproc_if')),
981
- alias(sym('preproc_ifdef_in_single_case'), sym('preproc_ifdef'))
999
+ prec.dynamic(
1000
+ 1,
1001
+ choice(
1002
+ $._single_statement,
1003
+ $.declaration,
1004
+ $.type_definition,
1005
+ alias(sym('preproc_if_in_single_case'), sym('preproc_if')),
1006
+ alias(sym('preproc_ifdef_in_single_case'), sym('preproc_ifdef'))
1007
+ )
982
1008
  ),
983
1009
  _single_labeled_statement: ($) =>
984
1010
  seq(field('label', $._statement_identifier), ':', choice($._single_statement, $.declaration)),
985
- _single_if_statement: ($) =>
986
- prec.right(
987
- seq(
988
- 'if',
989
- field('condition', $.parenthesized_expression),
990
- field('consequence', $._single_statement),
991
- optional(field('alternative', alias($._single_else_clause, $.else_clause)))
992
- )
1011
+ _single_attributed_statement: ($) =>
1012
+ seq(
1013
+ repeat1($.attribute_declaration),
1014
+ choice($.compound_statement, alias($._single_non_case_statement, $.statement))
993
1015
  ),
994
- _single_else_clause: ($) => seq('else', $._single_statement),
995
- _single_while_statement: ($) =>
996
- seq('while', field('condition', $.parenthesized_expression), field('body', $._single_statement)),
997
- _single_do_statement: ($) =>
998
- seq('do', field('body', $._single_statement), 'while', field('condition', $.parenthesized_expression), ';'),
999
- _single_for_statement: ($) => seq('for', '(', $._for_statement_body, ')', field('body', $._single_statement)),
1000
- _single_attributed_statement: ($) => seq(repeat1($.attribute_declaration), $._single_statement),
1001
1016
 
1002
1017
  case_statement: ($) =>
1003
- prec.right(
1018
+ prec.dynamic(
1019
+ 1,
1004
1020
  seq(
1021
+ repeat($.attribute_declaration),
1005
1022
  choice(
1006
1023
  seq('case', field('value', $.expression), optional(seq('...', field('end_value', $.expression)))),
1007
1024
  'default'
1008
1025
  ),
1009
1026
  ':',
1010
- repeat(choice($._non_case_statement, $.declaration, $.type_definition))
1027
+ repeat($._case_statement_item),
1028
+ optional(alias($._case_label_tail, $.statement))
1029
+ )
1030
+ ),
1031
+
1032
+ _case_statement_item: ($) =>
1033
+ prec.dynamic(1, choice(alias($._case_body_statement, $.statement), $.declaration, $.type_definition)),
1034
+
1035
+ _case_body_statement: ($) =>
1036
+ choice(
1037
+ alias($._case_body_unattributed_statement, $.statement),
1038
+ alias($._case_body_attributed_statement, $.attributed_statement)
1039
+ ),
1040
+ _case_body_unattributed_statement: ($) =>
1041
+ choice($._unlabeled_non_case_statement, alias($._case_body_labeled_statement, $.labeled_statement)),
1042
+ _case_body_labeled_statement: ($) =>
1043
+ seq(
1044
+ field('label', $._statement_identifier),
1045
+ ':',
1046
+ choice($.declaration, alias($._case_body_statement, $.statement))
1047
+ ),
1048
+ _case_body_attributed_statement: ($) =>
1049
+ prec(1, seq(repeat1($.attribute_declaration), alias($._case_body_unattributed_statement, $.statement))),
1050
+ _case_label_tail: ($) =>
1051
+ prec.dynamic(
1052
+ 1,
1053
+ choice(
1054
+ alias($._case_label_chain, $.labeled_statement),
1055
+ alias($._case_label_attributes, $.attributed_statement)
1011
1056
  )
1012
1057
  ),
1058
+ _case_label_attributes: ($) =>
1059
+ seq(repeat1($.attribute_declaration), alias($._case_label_chain_statement, $.statement)),
1060
+ _case_label_chain_statement: ($) => alias($._case_label_chain, $.labeled_statement),
1061
+ _case_label_chain: ($) =>
1062
+ seq(field('label', $._statement_identifier), ':', alias($._case_label_statement, $.statement)),
1063
+ _case_label_statement: ($) => choice($.case_statement, alias($._case_label_tail, $.statement)),
1013
1064
 
1014
- while_statement: ($) => seq('while', field('condition', $.parenthesized_expression), field('body', $.statement)),
1065
+ while_statement: ($) =>
1066
+ seq('while', field('condition', $.parenthesized_expression), field('body', $._single_statement)),
1015
1067
 
1016
1068
  do_statement: ($) =>
1017
- seq('do', field('body', $.statement), 'while', field('condition', $.parenthesized_expression), ';'),
1069
+ seq('do', field('body', $._single_statement), 'while', field('condition', $.parenthesized_expression), ';'),
1018
1070
 
1019
- for_statement: ($) => seq('for', '(', $._for_statement_body, ')', field('body', $.statement)),
1071
+ for_statement: ($) => seq('for', '(', $._for_statement_body, ')', field('body', $._single_statement)),
1020
1072
  _for_statement_body: ($) =>
1021
1073
  seq(
1022
1074
  choice(
@@ -1284,8 +1336,14 @@ module.exports = Object.assign(
1284
1336
 
1285
1337
  extension_expression: ($) => seq('__extension__', $.expression),
1286
1338
 
1287
- // The compound_statement is added to parse macros taking statements as arguments, e.g. MYFORLOOP(1, 10, i, { foo(i); bar(i); })
1288
- argument_list: ($) => seq('(', commaSep(choice($.expression, $.compound_statement)), ')'),
1339
+ // Compound statements, designated initializers, and a trailing comma parse macro arguments, e.g.
1340
+ // MYFORLOOP(1, 10, i, { foo(i); bar(i); }) and PARAMS(.foo = foo, .bar = { bar, 1 },)
1341
+ argument_list: ($) =>
1342
+ seq(
1343
+ '(',
1344
+ optional(seq(commaSep1(choice($.expression, $.compound_statement, $.initializer_pair)), optional(','))),
1345
+ ')'
1346
+ ),
1289
1347
 
1290
1348
  field_expression: ($) =>
1291
1349
  seq(
@@ -1352,7 +1410,13 @@ module.exports = Object.assign(
1352
1410
  'directive',
1353
1411
  alias(token(/#[ \t]*(embed|include|undef|error|warning|line|pragma)/), $.preproc_directive)
1354
1412
  ),
1355
- field('argument', optional(alias($._preproc_directive_arg, $.preproc_arg))),
1413
+ repeat(choice($.comment, $.pragma_operator)),
1414
+ optional(
1415
+ seq(
1416
+ field('argument', alias($._preproc_directive_arg, $.preproc_arg)),
1417
+ repeat(choice($.comment, $.pragma_operator))
1418
+ )
1419
+ ),
1356
1420
  alias(sym('_preproc_newline'), '\n')
1357
1421
  ),
1358
1422
 
@@ -1518,9 +1582,9 @@ function sizedTypeSpecifier(type) {
1518
1582
  function preprocIf(suffix, content, precedence = 0, repeatContent = true) {
1519
1583
  function alternativeBlock() {
1520
1584
  return choice(
1521
- suffix ? alias(sym('preproc_else' + suffix), sym('preproc_else')) : sym('preproc_else'),
1522
- suffix ? alias(sym('preproc_elif' + suffix), sym('preproc_elif')) : sym('preproc_elif'),
1523
- suffix ? alias(sym('preproc_elifdef' + suffix), sym('preproc_elifdef')) : sym('preproc_elifdef')
1585
+ ...['preproc_else', 'preproc_elif', 'preproc_elifdef'].map((name) =>
1586
+ suffix ? alias(sym(name + suffix), sym(name)) : sym(name)
1587
+ )
1524
1588
  );
1525
1589
  }
1526
1590
 
@@ -1531,7 +1595,7 @@ function preprocIf(suffix, content, precedence = 0, repeatContent = true) {
1531
1595
  seq(
1532
1596
  preprocessor('if'),
1533
1597
  field('condition', sym('_preproc_expression')),
1534
- alias(sym('_preproc_newline'), '\n'),
1598
+ alias(token(/\r\n?|\n\r?/), '\n'),
1535
1599
  repeatContent ? repeat(content($)) : optional(content($)),
1536
1600
  field('alternative', optional(alternativeBlock())),
1537
1601
  preprocessor('endif')
@@ -1559,7 +1623,7 @@ function preprocIf(suffix, content, precedence = 0, repeatContent = true) {
1559
1623
  seq(
1560
1624
  preprocessor('elif'),
1561
1625
  field('condition', sym('_preproc_expression')),
1562
- alias(sym('_preproc_newline'), '\n'),
1626
+ alias(token(/\r\n?|\n\r?/), '\n'),
1563
1627
  repeatContent ? repeat(content($)) : optional(content($)),
1564
1628
  field('alternative', optional(alternativeBlock()))
1565
1629
  )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-c",
3
- "version": "5.1.1",
3
+ "version": "6.0.1",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "incremental",
@@ -51,7 +51,7 @@
51
51
  "@willbooster/oxfmt-config": "1.2.2",
52
52
  "@willbooster/oxlint-config": "1.4.8",
53
53
  "@willbooster/wb": "22.26.1",
54
- "@willbooster/web-tree-sitter": "2.0.25",
54
+ "@willbooster/web-tree-sitter": "4.0.2",
55
55
  "conventional-changelog-conventionalcommits": "10.4.1",
56
56
  "lefthook": "2.2.1",
57
57
  "oxfmt": "0.72.0",
@@ -64,7 +64,7 @@
64
64
  "wrangler": "4.149.0"
65
65
  },
66
66
  "peerDependencies": {
67
- "@willbooster/web-tree-sitter": "^2.0.0"
67
+ "@willbooster/web-tree-sitter": "^4.0.2"
68
68
  },
69
69
  "peerDependenciesMeta": {
70
70
  "@willbooster/web-tree-sitter": {
@@ -527,6 +527,10 @@
527
527
  "type": "expression",
528
528
  "named": true
529
529
  },
530
+ {
531
+ "type": "initializer_pair",
532
+ "named": true
533
+ },
530
534
  {
531
535
  "type": "preproc_defined",
532
536
  "named": true
@@ -1027,6 +1031,10 @@
1027
1031
  "multiple": true,
1028
1032
  "required": false,
1029
1033
  "types": [
1034
+ {
1035
+ "type": "attribute_declaration",
1036
+ "named": true
1037
+ },
1030
1038
  {
1031
1039
  "type": "declaration",
1032
1040
  "named": true
@@ -2866,6 +2874,20 @@
2866
2874
  }
2867
2875
  ]
2868
2876
  }
2877
+ },
2878
+ "children": {
2879
+ "multiple": true,
2880
+ "required": false,
2881
+ "types": [
2882
+ {
2883
+ "type": "comment",
2884
+ "named": true
2885
+ },
2886
+ {
2887
+ "type": "pragma_operator",
2888
+ "named": true
2889
+ }
2890
+ ]
2869
2891
  }
2870
2892
  },
2871
2893
  {
@@ -2892,6 +2914,20 @@
2892
2914
  }
2893
2915
  ]
2894
2916
  }
2917
+ },
2918
+ "children": {
2919
+ "multiple": true,
2920
+ "required": false,
2921
+ "types": [
2922
+ {
2923
+ "type": "comment",
2924
+ "named": true
2925
+ },
2926
+ {
2927
+ "type": "pragma_operator",
2928
+ "named": true
2929
+ }
2930
+ ]
2895
2931
  }
2896
2932
  },
2897
2933
  {
@@ -3278,6 +3314,20 @@
3278
3314
  }
3279
3315
  ]
3280
3316
  }
3317
+ },
3318
+ "children": {
3319
+ "multiple": true,
3320
+ "required": false,
3321
+ "types": [
3322
+ {
3323
+ "type": "comment",
3324
+ "named": true
3325
+ },
3326
+ {
3327
+ "type": "pragma_operator",
3328
+ "named": true
3329
+ }
3330
+ ]
3281
3331
  }
3282
3332
  },
3283
3333
  {
@@ -3558,6 +3608,20 @@
3558
3608
  }
3559
3609
  ]
3560
3610
  }
3611
+ },
3612
+ "children": {
3613
+ "multiple": true,
3614
+ "required": false,
3615
+ "types": [
3616
+ {
3617
+ "type": "comment",
3618
+ "named": true
3619
+ },
3620
+ {
3621
+ "type": "pragma_operator",
3622
+ "named": true
3623
+ }
3624
+ ]
3561
3625
  }
3562
3626
  },
3563
3627
  {
Binary file
package/tree-sitter.json CHANGED
@@ -16,7 +16,7 @@
16
16
  }
17
17
  ],
18
18
  "metadata": {
19
- "version": "5.1.1",
19
+ "version": "6.0.1",
20
20
  "license": "MIT",
21
21
  "description": "C grammar for tree-sitter",
22
22
  "authors": [