@willbooster/tree-sitter-c 33.0.0 → 34.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/grammar.js CHANGED
@@ -462,6 +462,16 @@ module.exports = Object.assign(
462
462
  [$.parameter_list, $._old_style_parameter_list],
463
463
  [$.function_declarator, $._function_declaration_declarator],
464
464
  [$.function_declarator, $._parameter_function_declarator],
465
+ [$._parameter_macro_argument_list],
466
+ [$._later_macro_call_expression, $.macro_specifier],
467
+ [$._later_macro_call_expression, $._macro_case_macro_specifier],
468
+ [$._macro_case_attributed_declarator],
469
+ [$._macro_case_parameter_declarator, $._macro_case_attributed_declarator],
470
+ [$._declarator, $._later_macro_call_expression, $.macro_specifier],
471
+ [$._macro_case_macro_specifier, $._macro_case_unattributed_declarator],
472
+ [$._later_macro_call_expression, $._macro_case_macro_specifier, $._macro_case_unattributed_declarator],
473
+ [$._parameter_pointer_declarator, $.pointer_declarator],
474
+ [$._declarator, $._leading_macro_call_expression, $.macro_specifier],
465
475
  [$._parameter_pointer_declarator, $.pointer_declarator, $.abstract_pointer_declarator],
466
476
  [$._parameter_declarator, $.macro_specifier],
467
477
  [$.pointer_declarator],
@@ -1134,6 +1144,14 @@ module.exports = Object.assign(
1134
1144
  seq($._declaration_specifiers, field('declarator', $._declarator), repeat($.attribute_specifier)),
1135
1145
 
1136
1146
  _macro_attribute_argument_list: ($) => macroAttributeArgumentList(macroAttributeArgument($)),
1147
+ // A list with a type ranks two below one without, once however many types it has, so a function declarator
1148
+ // reading of the same parentheses outranks a trailing annotation only with a type: `int *CC FOO(int)` declares a
1149
+ // function, while `char *buf __counted_by(len)` is annotated.
1150
+ _parameter_macro_argument_list: ($) =>
1151
+ choice(
1152
+ macroAttributeArgumentList(macroAttributeArgument($, false)),
1153
+ prec.dynamic(-2, macroAttributeArgumentList(macroAttributeArgument($, $.type_descriptor)))
1154
+ ),
1137
1155
  _braced_macro_attribute_argument_list: ($) =>
1138
1156
  macroAttributeArgumentList(
1139
1157
  choice(macroAttributeArgument($), alias(sym('_macro_attribute_parameter'), $.parameter_declaration))
@@ -1192,8 +1210,8 @@ module.exports = Object.assign(
1192
1210
 
1193
1211
  _macro_declaration_declarator: ($) => declarationDeclaratorRule($, $.function_declarator),
1194
1212
 
1195
- // A parameter keeps its trailing annotations however deeply its function declarator sits under pointers, as in
1196
- // `int *callback(int) unused`.
1213
+ // A function declarator under pointers, as in `int *callback(int) ATTR(1)`, leaves the annotations after it to
1214
+ // the parameter, unless its name and parentheses read as a leading call marker (see `_parameter_pointer_declarator`).
1197
1215
  _parameter_declarator: ($) =>
1198
1216
  choice(
1199
1217
  $.attributed_declarator,
@@ -1205,7 +1223,87 @@ module.exports = Object.assign(
1205
1223
  alias(VA_ARG_KEYWORDS, $.identifier)
1206
1224
  ),
1207
1225
 
1208
- _parameter_pointer_declarator: ($) => pointerDeclaratorRule($, $._parameter_declarator),
1226
+ // After a first leading marker, a macro-cased name lexes as `_macro_case_identifier`, since the marker may also
1227
+ // be the declarator and the name a trailing annotation, so later markers and the pointee may be named by it too,
1228
+ // as `FOO` in `int *CC FOO[3]`. It is not valid right after `*`, where it would take the operand of an
1229
+ // expression reading such as `foo(width * HEIGHT)`.
1230
+ _parameter_pointer_declarator: ($) => {
1231
+ const modifier = pointerDeclaratorModifier($);
1232
+ return prec.right(
1233
+ seq(
1234
+ ...pointerDeclaratorHead($),
1235
+ repeat(modifier),
1236
+ choice(
1237
+ field('declarator', $._parameter_declarator),
1238
+ seq(
1239
+ choice($.macro_specifier, alias($._leading_macro_call_expression, $.call_expression)),
1240
+ repeat(
1241
+ choice(
1242
+ modifier,
1243
+ $.macro_specifier,
1244
+ alias($._macro_case_macro_specifier, $.macro_specifier),
1245
+ alias($._later_macro_call_expression, $.call_expression)
1246
+ )
1247
+ ),
1248
+ field('declarator', choice($._parameter_declarator, $._macro_case_parameter_declarator))
1249
+ )
1250
+ )
1251
+ )
1252
+ );
1253
+ },
1254
+
1255
+ // A first leading marker with arguments, as in `const void *__sized_by(n) buffer`, outranks the reading of its
1256
+ // name and arguments as a function declarator whose following name is a trailing annotation (-1), whatever the
1257
+ // arguments are; system headers write this marker, while a function-typed parameter returning a pointer and
1258
+ // annotated after its parameter list is rare.
1259
+ _leading_macro_call_expression: ($) => leadingMacroCallRule($, 2, $.identifier),
1260
+
1261
+ // A later one, as `FOO(int)` in `int *CC FOO(int) UNUSED`, ranks below that reading.
1262
+ _later_macro_call_expression: ($) =>
1263
+ leadingMacroCallRule($, -2, choice($.identifier, alias($._macro_case_identifier, $.identifier))),
1264
+
1265
+ _macro_case_macro_specifier: ($) =>
1266
+ prec.dynamic(-2, field('name', alias($._macro_case_identifier, $.identifier))),
1267
+
1268
+ // The declarator forms that start with a name, split as `_declarator` is.
1269
+ _macro_case_parameter_declarator: ($) =>
1270
+ choice(
1271
+ alias($._macro_case_attributed_declarator, $.attributed_declarator),
1272
+ $._macro_case_unattributed_declarator
1273
+ ),
1274
+
1275
+ _macro_case_unattributed_declarator: ($) =>
1276
+ choice(
1277
+ alias($._macro_case_identifier, $.identifier),
1278
+ alias($._macro_case_array_declarator, $.array_declarator),
1279
+ alias($._macro_case_function_declarator, $.function_declarator)
1280
+ ),
1281
+
1282
+ _macro_case_attributed_declarator: ($) =>
1283
+ seq($._macro_case_unattributed_declarator, repeat1($.attribute_declaration)),
1284
+
1285
+ _macro_case_array_declarator: ($) =>
1286
+ prec(
1287
+ 1,
1288
+ seq(
1289
+ field('declarator', $._macro_case_parameter_declarator),
1290
+ '[',
1291
+ repeat(choice($.type_qualifier, 'static')),
1292
+ field('size', optional(choice($.expression, '*'))),
1293
+ ']'
1294
+ )
1295
+ ),
1296
+
1297
+ _macro_case_function_declarator: ($) =>
1298
+ prec.right(
1299
+ 2,
1300
+ seq(
1301
+ field('declarator', $._macro_case_parameter_declarator),
1302
+ field('parameters', $.parameter_list),
1303
+ optional($.gnu_asm_expression),
1304
+ repeat($.attribute_specifier)
1305
+ )
1306
+ ),
1209
1307
 
1210
1308
  _braced_macro_declarator: ($) => alias(sym('_braced_macro_function_declarator'), $.function_declarator),
1211
1309
  _braced_macro_function_declarator: ($) =>
@@ -2398,7 +2496,7 @@ module.exports = Object.assign(
2398
2496
 
2399
2497
  _trailing_macro_call_expression: ($) =>
2400
2498
  trailingAnnotationRule($, (name) =>
2401
- seq(field('function', name), field('arguments', alias($.preproc_argument_list, $.argument_list)))
2499
+ seq(field('function', name), field('arguments', alias($._parameter_macro_argument_list, $.argument_list)))
2402
2500
  ),
2403
2501
 
2404
2502
  _conditional_macro_specifier: () => alias(sym('_marker_preproc_ifdef'), sym('preproc_ifdef')),
@@ -2506,8 +2604,8 @@ function* keywordStrings(node) {
2506
2604
 
2507
2605
  /**
2508
2606
  * Ranks a trailing parameter annotation against a leading marker (-2) that reads `int *A B` the other way: an
2509
- * annotation named like a macro (`char **argv UNUSED`, `int *CC cb(int) ATTR(1)`) outranks it, and any other name ranks
2510
- * below it (`int *CC pointer`, `int *CC callback(int)`), since macro definitions are unknown.
2607
+ * annotation named like a macro (`char **argv UNUSED`) outranks it, and any other name ranks below it
2608
+ * (`int *CC pointer`), since macro definitions are unknown.
2511
2609
  * @param {import('./types/treeSitterDsl').GrammarSymbols<'identifier' | '_macro_case_identifier'>} $
2512
2610
  * @param {(name: import('./types/treeSitterDsl').Rule) => import('./types/treeSitterDsl').Rule} annotation
2513
2611
  */
@@ -2518,6 +2616,19 @@ function trailingAnnotationRule($, annotation) {
2518
2616
  );
2519
2617
  }
2520
2618
 
2619
+ /**
2620
+ * A leading marker with arguments in a parameter's pointer declarator.
2621
+ * @param {import('./types/treeSitterDsl').GrammarSymbols<'_parameter_macro_argument_list' | 'argument_list'>} $
2622
+ * @param {number} precedence
2623
+ * @param {import('./types/treeSitterDsl').Rule} name
2624
+ */
2625
+ function leadingMacroCallRule($, precedence, name) {
2626
+ return prec.dynamic(
2627
+ precedence,
2628
+ seq(field('function', name), field('arguments', alias($._parameter_macro_argument_list, $.argument_list)))
2629
+ );
2630
+ }
2631
+
2521
2632
  /**
2522
2633
  * Returns a regular expression source that matches every string of a character in `first` followed by characters in
2523
2634
  * `rest`, the empty string included, except the given words.
@@ -2607,25 +2718,43 @@ function parenthesizedDeclaratorRule($, declarator) {
2607
2718
  function pointerDeclaratorRule($, declarator) {
2608
2719
  return prec.right(
2609
2720
  seq(
2610
- optional($.ms_based_modifier),
2611
- optional(prec.dynamic(2, $.ms_call_modifier)),
2612
- '*',
2613
- repeat($.attribute_declaration),
2614
- repeat($.ms_pointer_modifier),
2615
- repeat(
2616
- choice(
2617
- $.type_qualifier,
2618
- $.attribute_specifier,
2619
- prec(-1, $.ms_call_modifier),
2620
- $.macro_specifier,
2621
- $._conditional_macro_specifier
2622
- )
2623
- ),
2721
+ ...pointerDeclaratorHead($),
2722
+ repeat(pointerDeclaratorModifier($, $.macro_specifier)),
2624
2723
  field('declarator', declarator)
2625
2724
  )
2626
2725
  );
2627
2726
  }
2628
2727
 
2728
+ /**
2729
+ * The tokens of a pointer declarator up to its `*` and the modifiers that only follow it.
2730
+ * @param {import('./types/treeSitterDsl').GrammarSymbols<'ms_based_modifier' | 'ms_call_modifier' | 'attribute_declaration' | 'ms_pointer_modifier'>} $
2731
+ */
2732
+ function pointerDeclaratorHead($) {
2733
+ return [
2734
+ optional($.ms_based_modifier),
2735
+ optional(prec.dynamic(2, $.ms_call_modifier)),
2736
+ '*',
2737
+ repeat($.attribute_declaration),
2738
+ repeat($.ms_pointer_modifier),
2739
+ ];
2740
+ }
2741
+
2742
+ /**
2743
+ * A pointer declarator's modifier between its `*` and its declarator: a qualifier, attribute, calling convention or
2744
+ * conditional marker, or one of `markers`.
2745
+ * @param {import('./types/treeSitterDsl').GrammarSymbols<'type_qualifier' | 'attribute_specifier' | 'ms_call_modifier' | '_conditional_macro_specifier'>} $
2746
+ * @param {...import('./types/treeSitterDsl').Rule} markers
2747
+ */
2748
+ function pointerDeclaratorModifier($, ...markers) {
2749
+ return choice(
2750
+ $.type_qualifier,
2751
+ $.attribute_specifier,
2752
+ prec(-1, $.ms_call_modifier),
2753
+ ...markers,
2754
+ $._conditional_macro_specifier
2755
+ );
2756
+ }
2757
+
2629
2758
  /**
2630
2759
  * @param {import('./types/treeSitterDsl').GrammarSymbols<'_guarded_type_specifiers' | '_marked_macro_type_specifiers' | '_macro_declaration_specifiers'>} $
2631
2760
  * @param {import('./types/treeSitterDsl').Rule} ordinary
@@ -2818,11 +2947,14 @@ function macroAttributeArgumentList(argument) {
2818
2947
  return seq('(', optional(argument), repeat(seq(',', optional(argument))), ')');
2819
2948
  }
2820
2949
 
2821
- /** @param {import('./types/treeSitterDsl').GrammarSymbols<'expression' | 'parameter_declaration' | 'type_descriptor' | '_preproc_number' | 'number_literal' | 'compound_statement' | 'initializer_pair' | 'initializer_list'>} $ */
2822
- function macroAttributeArgument($) {
2950
+ /**
2951
+ * @param {import('./types/treeSitterDsl').GrammarSymbols<'expression' | 'parameter_declaration' | 'type_descriptor' | '_preproc_number' | 'number_literal' | 'compound_statement' | 'initializer_pair' | 'initializer_list'>} $
2952
+ * @param {import('./types/treeSitterDsl').Rule | false} [type] The type argument form, or `false` for none.
2953
+ */
2954
+ function macroAttributeArgument($, type = prec.dynamic(-1, $.type_descriptor)) {
2823
2955
  return choice(
2824
2956
  $.expression,
2825
- prec.dynamic(-1, $.type_descriptor),
2957
+ ...(type ? [type] : []),
2826
2958
  alias($._preproc_number, $.number_literal),
2827
2959
  $.compound_statement,
2828
2960
  $.initializer_pair,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-c",
3
- "version": "33.0.0",
3
+ "version": "34.0.0",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "c",
@@ -3120,6 +3120,10 @@
3120
3120
  "type": "attribute_specifier",
3121
3121
  "named": true
3122
3122
  },
3123
+ {
3124
+ "type": "call_expression",
3125
+ "named": true
3126
+ },
3123
3127
  {
3124
3128
  "type": "macro_specifier",
3125
3129
  "named": true
Binary file
package/tree-sitter.json CHANGED
@@ -24,7 +24,7 @@
24
24
  }
25
25
  ],
26
26
  "metadata": {
27
- "version": "33.0.0",
27
+ "version": "34.0.0",
28
28
  "license": "MIT",
29
29
  "description": "C grammar for tree-sitter",
30
30
  "authors": [