@willbooster/tree-sitter-c 5.1.1 → 6.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
@@ -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))
1011
1029
  )
1012
1030
  ),
1013
1031
 
1014
- while_statement: ($) => seq('while', field('condition', $.parenthesized_expression), field('body', $.statement)),
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)
1056
+ )
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)),
1064
+
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(
@@ -1352,7 +1404,13 @@ module.exports = Object.assign(
1352
1404
  'directive',
1353
1405
  alias(token(/#[ \t]*(embed|include|undef|error|warning|line|pragma)/), $.preproc_directive)
1354
1406
  ),
1355
- field('argument', optional(alias($._preproc_directive_arg, $.preproc_arg))),
1407
+ repeat(choice($.comment, $.pragma_operator)),
1408
+ optional(
1409
+ seq(
1410
+ field('argument', alias($._preproc_directive_arg, $.preproc_arg)),
1411
+ repeat(choice($.comment, $.pragma_operator))
1412
+ )
1413
+ ),
1356
1414
  alias(sym('_preproc_newline'), '\n')
1357
1415
  ),
1358
1416
 
@@ -1518,9 +1576,9 @@ function sizedTypeSpecifier(type) {
1518
1576
  function preprocIf(suffix, content, precedence = 0, repeatContent = true) {
1519
1577
  function alternativeBlock() {
1520
1578
  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')
1579
+ ...['preproc_else', 'preproc_elif', 'preproc_elifdef'].map((name) =>
1580
+ suffix ? alias(sym(name + suffix), sym(name)) : sym(name)
1581
+ )
1524
1582
  );
1525
1583
  }
1526
1584
 
@@ -1531,7 +1589,7 @@ function preprocIf(suffix, content, precedence = 0, repeatContent = true) {
1531
1589
  seq(
1532
1590
  preprocessor('if'),
1533
1591
  field('condition', sym('_preproc_expression')),
1534
- alias(sym('_preproc_newline'), '\n'),
1592
+ alias(token(/\r\n?|\n\r?/), '\n'),
1535
1593
  repeatContent ? repeat(content($)) : optional(content($)),
1536
1594
  field('alternative', optional(alternativeBlock())),
1537
1595
  preprocessor('endif')
@@ -1559,7 +1617,7 @@ function preprocIf(suffix, content, precedence = 0, repeatContent = true) {
1559
1617
  seq(
1560
1618
  preprocessor('elif'),
1561
1619
  field('condition', sym('_preproc_expression')),
1562
- alias(sym('_preproc_newline'), '\n'),
1620
+ alias(token(/\r\n?|\n\r?/), '\n'),
1563
1621
  repeatContent ? repeat(content($)) : optional(content($)),
1564
1622
  field('alternative', optional(alternativeBlock()))
1565
1623
  )
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.0",
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": {
@@ -1027,6 +1027,10 @@
1027
1027
  "multiple": true,
1028
1028
  "required": false,
1029
1029
  "types": [
1030
+ {
1031
+ "type": "attribute_declaration",
1032
+ "named": true
1033
+ },
1030
1034
  {
1031
1035
  "type": "declaration",
1032
1036
  "named": true
@@ -2866,6 +2870,20 @@
2866
2870
  }
2867
2871
  ]
2868
2872
  }
2873
+ },
2874
+ "children": {
2875
+ "multiple": true,
2876
+ "required": false,
2877
+ "types": [
2878
+ {
2879
+ "type": "comment",
2880
+ "named": true
2881
+ },
2882
+ {
2883
+ "type": "pragma_operator",
2884
+ "named": true
2885
+ }
2886
+ ]
2869
2887
  }
2870
2888
  },
2871
2889
  {
@@ -2892,6 +2910,20 @@
2892
2910
  }
2893
2911
  ]
2894
2912
  }
2913
+ },
2914
+ "children": {
2915
+ "multiple": true,
2916
+ "required": false,
2917
+ "types": [
2918
+ {
2919
+ "type": "comment",
2920
+ "named": true
2921
+ },
2922
+ {
2923
+ "type": "pragma_operator",
2924
+ "named": true
2925
+ }
2926
+ ]
2895
2927
  }
2896
2928
  },
2897
2929
  {
@@ -3278,6 +3310,20 @@
3278
3310
  }
3279
3311
  ]
3280
3312
  }
3313
+ },
3314
+ "children": {
3315
+ "multiple": true,
3316
+ "required": false,
3317
+ "types": [
3318
+ {
3319
+ "type": "comment",
3320
+ "named": true
3321
+ },
3322
+ {
3323
+ "type": "pragma_operator",
3324
+ "named": true
3325
+ }
3326
+ ]
3281
3327
  }
3282
3328
  },
3283
3329
  {
@@ -3558,6 +3604,20 @@
3558
3604
  }
3559
3605
  ]
3560
3606
  }
3607
+ },
3608
+ "children": {
3609
+ "multiple": true,
3610
+ "required": false,
3611
+ "types": [
3612
+ {
3613
+ "type": "comment",
3614
+ "named": true
3615
+ },
3616
+ {
3617
+ "type": "pragma_operator",
3618
+ "named": true
3619
+ }
3620
+ ]
3561
3621
  }
3562
3622
  },
3563
3623
  {
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.0",
20
20
  "license": "MIT",
21
21
  "description": "C grammar for tree-sitter",
22
22
  "authors": [