@willbooster/tree-sitter-c 10.0.0 → 12.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
@@ -65,20 +65,34 @@ Keep preprocessor argument/newline handling ahead of annotation lookahead.
65
65
  The hidden `_function_attribute_start`, `_post_type_function_attribute_start`, and
66
66
  `_post_type_function_attribute_suffix` guards look ahead before the ordinary identifier token;
67
67
  keep them outside the name field so identifier leaves and ordinary C lexing retain their shape.
68
- Port `scan_function_attribute_guard` and the lookahead-only name-range handling in `pragma.h`.
69
- First guards check the prototype tail; post-type continuation guards inspect only their own argument group.
68
+ Port `scan_function_attribute_guard`, its `MacroAttributeSymbols` context, and the name-range handling in `pragma.h`.
69
+ The `_trailing_object_declaration_start` guard checks an object prefix before declaration parsing;
70
+ `_trailing_macro_attribute_name` consumes only the annotation name and preserves its ordinary identifier leaf.
71
+ Keep the shared first-word dispatch so a failed lookahead never falls back from an advanced lexer cursor.
72
+ First prototype guards check the prototype tail; post-type continuation guards inspect only their own argument group.
70
73
  Regenerate Unicode and keyword tables from the derived grammar's metadata.
71
74
  The generation source is `script/generateIdentifierRanges.js` in this repository.
75
+ The primitive keyword data shared by grammar generation and object-prefix classification is in `src/grammarKeywords.cjs`.
72
76
  The `_preproc_function_name` token ends before splices used to determine adjacency, so the `_preproc_lparen`
73
77
  scanner must skip those splices again to reach `(`.
74
78
 
75
- Header annotation lookahead accepts argument/prototype groups without unquoted braces, semicolons or preprocessing directives.
79
+ Prototype annotation lookahead accepts argument/prototype groups without unquoted braces, semicolons or preprocessing directives.
76
80
  Quoted punctuation remains literal content. GNU statement-expression, inline-tag, compound-literal
77
81
  and directive-bearing annotation arguments need a separate grammar design to keep malformed-input recovery bounded.
78
82
 
79
83
  Header annotation declarations use `_macro_modified_declaration`, `_macro_declaration_specifiers`,
80
84
  `_macro_declaration_declarator`, and `_header_preproc_item`. Adapt these rules alongside public declaration/type
81
85
  overrides when extending C. The annotation declarator uses `function_declarator` to retain trailing macro calls.
86
+ The separate `_trailing_macro_modified_declaration` route uses public declaration specifiers and
87
+ `_trailing_macro_object_declarator` / `_trailing_macro_pointer_declarator`; adapt these header rules when
88
+ overriding declaration or declarator behavior.
89
+
90
+ Trailing object annotations support one complete `primitive_type` token, plain pointer stars and an identifier,
91
+ for example `int *value ATTR((1));`. The first argument token after initial parentheses must be numeric,
92
+ a string/character quote or a comma. Comments and whitespace may separate the prefix tokens; the remaining
93
+ arguments use the structural annotation-argument grammar. Sized-only starts such as `unsigned` or `long`,
94
+ qualified/multiword/unknown-type prefixes,
95
+ arrays, parenthesized/function declarators, initializers and multiple declarators need separate support.
82
96
 
83
97
  In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-c) and on
84
98
  [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
@@ -87,7 +101,7 @@ fuzzed with, at version 7.0.5 or later. No other runtime loads the parser:
87
101
  ```toml
88
102
  [dependencies]
89
103
  tree-sitter = { package = "willbooster-tree-sitter", version = "7.0.5" }
90
- tree-sitter-c = { package = "willbooster-tree-sitter-c", version = "9" }
104
+ tree-sitter-c = { package = "willbooster-tree-sitter-c", version = "11" }
91
105
  ```
92
106
 
93
107
  ```rust
package/grammar.js CHANGED
@@ -11,6 +11,9 @@
11
11
  const { grammar, alias, choice, field, optional, prec, repeat, repeat1, seq, sym, token } =
12
12
  /** @type {typeof globalThis & typeof import('./types/treeSitterDsl')} */ (globalThis);
13
13
 
14
+ // oxlint-disable-next-line unicorn/prefer-module -- Tree-sitter loads this grammar as CommonJS.
15
+ const { primitiveTypeKeywords } = require('./src/grammarKeywords.cjs');
16
+
14
17
  const PREC = {
15
18
  PAREN_DECLARATOR: -10,
16
19
  ASSIGNMENT: -2,
@@ -68,11 +71,27 @@ module.exports = Object.assign(
68
71
  name: 'c',
69
72
 
70
73
  conflicts: ($) => [
71
- [$._macro_declaration_specifiers],
74
+ [$._statement_declaration, $._statement_declaration_modifiers],
75
+ [$.declaration, $._declaration_modifiers],
76
+ [$._statement_declaration, $._statement_type_definition, $._statement_declaration_modifiers],
77
+ [$.declaration, $.type_definition, $._macro_declaration_specifiers, $._declaration_modifiers],
78
+ [$.type_definition, $.type_specifier],
79
+ [$.type_definition, $._macro_declaration_specifiers],
80
+ [$._macro_declaration_specifiers, $._declaration_modifiers, $.attributed_statement, $.case_statement],
81
+ [$._macro_declaration_specifiers, $.type_specifier, $.expression, $.macro_specifier],
82
+ [$._macro_declaration_specifiers, $.type_specifier, $.macro_specifier],
83
+ [$.macro_specifier, $._type_declarator],
84
+ [$.macro_specifier, $._field_declarator],
85
+ [$.macro_specifier, $._declarator],
86
+ [$.macro_specifier, $._declarator, $.attributed_declarator, $.type_specifier],
87
+ [$.macro_specifier, $._declarator, $.attributed_declarator],
88
+ [$.macro_specifier, $.sized_type_specifier],
89
+ [$.macro_specifier, $.type_specifier, $.sized_type_specifier],
90
+ [$.macro_specifier, $.expression],
91
+ [$.macro_specifier, $.type_specifier],
72
92
  [$._macro_declaration_specifiers, $.type_definition, $.type_specifier],
73
93
  [$._macro_declaration_specifiers, $.type_specifier, $.expression],
74
94
  [$._macro_declaration_specifiers, $.type_specifier],
75
- [$._macro_declaration_specifiers, $.type_definition, $._declaration_modifiers],
76
95
  [$._macro_declaration_specifiers, $._declaration_modifiers, $._empty_declaration],
77
96
  [$._macro_declaration_specifiers, $._declaration_modifiers],
78
97
  [$.preproc_include_argument],
@@ -82,20 +101,25 @@ module.exports = Object.assign(
82
101
  [$.comma_expression, $._initializer_element],
83
102
  [$.comma_expression, $.initializer_pair],
84
103
  [$._block_item, $._initializer_directive],
85
- [sym('preproc_if'), sym('preproc_if_in_initializer_list')],
86
- [sym('preproc_ifdef'), sym('preproc_ifdef_in_initializer_list')],
87
- [sym('preproc_else'), sym('preproc_else_in_initializer_list')],
88
- [sym('preproc_elif'), sym('preproc_elif_in_initializer_list')],
89
- [sym('preproc_elifdef'), sym('preproc_elifdef_in_initializer_list')],
104
+ [sym('preproc_if_in_statement'), sym('preproc_if_in_initializer_list')],
105
+ [sym('preproc_ifdef_in_statement'), sym('preproc_ifdef_in_initializer_list')],
106
+ [sym('preproc_else_in_statement'), sym('preproc_else_in_initializer_list')],
107
+ [sym('preproc_elif_in_statement'), sym('preproc_elif_in_initializer_list')],
108
+ [sym('preproc_elifdef_in_statement'), sym('preproc_elifdef_in_initializer_list')],
90
109
 
91
110
  [$.type_specifier, $.expression, $.va_arg_expression, $.macro_type_specifier],
92
111
  [$.va_arg_expression, $.expression],
93
112
  [$.type_definition, $._declaration_modifiers],
94
- [$.type_definition, $.type_specifier],
113
+ [$._statement_type_definition, $._statement_declaration_modifiers],
114
+ [$.type_definition, $._declaration_modifiers, $._empty_declaration],
115
+ [$._statement_type_definition, $.type_specifier],
95
116
  [$.type_definition, $.type_qualifier, $.extension_expression],
117
+ [$._statement_type_definition, $.type_qualifier, $.extension_expression],
96
118
  [$.type_definition, $.type_qualifier],
97
- [$._declaration_modifiers, $._empty_declaration],
119
+ [$._statement_type_definition, $.type_qualifier],
120
+ [$._statement_declaration_modifiers, $._statement_empty_declaration],
98
121
  [$.declaration, $.storage_class_specifier],
122
+ [$._statement_declaration, $.storage_class_specifier],
99
123
  [$.sized_type_specifier, $.enum_specifier],
100
124
  [$.type_specifier, $._declarator],
101
125
  [$.type_specifier, $._declarator, $.macro_type_specifier],
@@ -115,8 +139,8 @@ module.exports = Object.assign(
115
139
  [$.attributed_statement],
116
140
  [$.case_statement],
117
141
  [$._single_attributed_statement],
118
- [$._declaration_modifiers, $._case_body_attributed_statement, $._case_label_attributes],
119
- [$._declaration_modifiers, $.attributed_statement, $.case_statement],
142
+ [$._statement_declaration_modifiers, $._case_body_attributed_statement, $._case_label_attributes],
143
+ [$._statement_declaration_modifiers, $.attributed_statement, $.case_statement],
120
144
  [$.enum_specifier],
121
145
  [sym('preproc_if_in_enumerator_list'), sym('preproc_if_in_enumerator_list_no_comma')],
122
146
  [sym('preproc_ifdef_in_enumerator_list'), sym('preproc_ifdef_in_enumerator_list_no_comma')],
@@ -127,6 +151,7 @@ module.exports = Object.assign(
127
151
  [$.parameter_list, $._old_style_parameter_list],
128
152
  [$.function_declarator, $._function_declaration_declarator],
129
153
  [$.pointer_declarator],
154
+ [$.pointer_declarator, $.abstract_pointer_declarator],
130
155
  [$.pointer_field_declarator],
131
156
  [$.pointer_type_declarator],
132
157
  [$.abstract_pointer_declarator],
@@ -143,6 +168,7 @@ module.exports = Object.assign(
143
168
  [$.attributed_field_declarator, $._field_declarator],
144
169
  [$.attributed_type_declarator],
145
170
  [$.attributed_type_declarator, $._type_declarator],
171
+ [$._declaration_modifiers, $._field_declaration_list_item],
146
172
  [$._declaration_specifiers],
147
173
  [$._compound_literal_storage_class_specifier, $.type_specifier],
148
174
  [$._compound_literal_constexpr_specifier, $.type_qualifier],
@@ -158,12 +184,15 @@ module.exports = Object.assign(
158
184
  sym('_function_attribute_start'),
159
185
  sym('_post_type_function_attribute_start'),
160
186
  sym('_post_type_function_attribute_suffix'),
187
+ sym('_trailing_macro_attribute_name'),
188
+ sym('_trailing_object_declaration_start'),
161
189
  ],
162
190
 
163
191
  extras: ($) => [$.pragma_operator, /\s|\\(?:\r\n?|\n\r?)/, $.comment],
164
192
 
165
193
  inline: ($) => [
166
194
  $._non_identifier_type_specifier,
195
+ $._non_macro_declaration_modifiers,
167
196
  $._unattributed_declarator,
168
197
  $._unattributed_field_declarator,
169
198
  $._unattributed_type_declarator,
@@ -205,6 +234,7 @@ module.exports = Object.assign(
205
234
  $.linkage_specification,
206
235
  $.declaration,
207
236
  alias(sym('_macro_modified_declaration'), $.declaration),
237
+ alias(sym('_trailing_macro_modified_declaration'), $.declaration),
208
238
  $._top_level_statement,
209
239
  $.attributed_statement,
210
240
  $.type_definition,
@@ -217,9 +247,41 @@ module.exports = Object.assign(
217
247
  $.preproc_call
218
248
  ),
219
249
 
220
- _block_item: ($) => blockItemRule($, false),
250
+ _block_item: ($) =>
251
+ choice(
252
+ alias($._statement_function_definition, $.function_definition),
253
+ alias($._statement_old_style_function_definition, $.function_definition),
254
+ $.linkage_specification,
255
+ alias($._statement_declaration, $.declaration),
256
+ $.statement,
257
+ alias($._statement_type_definition, $.type_definition),
258
+ $._statement_empty_declaration,
259
+ alias(sym('preproc_if_in_statement'), sym('preproc_if')),
260
+ alias(sym('preproc_ifdef_in_statement'), sym('preproc_ifdef')),
261
+ $.preproc_include,
262
+ $.preproc_def,
263
+ $.preproc_function_def,
264
+ $.preproc_call
265
+ ),
221
266
 
222
- _header_preproc_item: ($) => blockItemRule($, true),
267
+ _header_preproc_item: ($) =>
268
+ choice(
269
+ $.function_definition,
270
+ alias($._old_style_function_definition, $.function_definition),
271
+ $.linkage_specification,
272
+ $.declaration,
273
+ alias(sym('_macro_modified_declaration'), $.declaration),
274
+ alias(sym('_trailing_macro_modified_declaration'), $.declaration),
275
+ $.statement,
276
+ $.type_definition,
277
+ $._empty_declaration,
278
+ alias(sym('preproc_if_in_header'), sym('preproc_if')),
279
+ alias(sym('preproc_ifdef_in_header'), sym('preproc_ifdef')),
280
+ $.preproc_include,
281
+ $.preproc_def,
282
+ $.preproc_function_def,
283
+ $.preproc_call
284
+ ),
223
285
 
224
286
  // Derived grammars that replace externals still need this lexical fallback. Comment-rich spacing makes the
225
287
  // lexer too large for the pinned Node Wasm optimizer; the external scanner handles those forms.
@@ -312,8 +374,8 @@ module.exports = Object.assign(
312
374
  ),
313
375
 
314
376
  ...preprocIf('_in_header', () => sym('_header_preproc_item')),
315
-
316
377
  ...preprocIf('', () => sym('_block_item')),
378
+ ...preprocIf('_in_statement', () => sym('_block_item')),
317
379
  ...preprocIf('_in_single_case', () => sym('_single_case_body'), 0, true),
318
380
  ...preprocIf('_in_field_declaration_list', () => sym('_field_declaration_list_item')),
319
381
  ...preprocIf('_in_enumerator_list', () => choice(seq(sym('enumerator'), ','), sym('_enumerator_directives'))),
@@ -386,23 +448,11 @@ module.exports = Object.assign(
386
448
  );
387
449
  },
388
450
 
389
- function_definition: ($) =>
390
- seq(
391
- optional($.ms_call_modifier),
392
- $._declaration_specifiers,
393
- optional($.ms_call_modifier),
394
- field('declarator', $._declarator),
395
- field('body', $.compound_statement)
396
- ),
397
-
398
- _old_style_function_definition: ($) =>
399
- seq(
400
- optional($.ms_call_modifier),
401
- $._declaration_specifiers,
402
- field('declarator', alias($._old_style_declarator, $._declarator)),
403
- repeat1($.declaration),
404
- field('body', $.compound_statement)
405
- ),
451
+ function_definition: ($) => functionDefinitionRule($, $._declaration_specifiers, false),
452
+ _statement_function_definition: ($) => functionDefinitionRule($, $._statement_declaration_specifiers, false),
453
+ _old_style_function_definition: ($) => functionDefinitionRule($, $._declaration_specifiers, true),
454
+ _statement_old_style_function_definition: ($) =>
455
+ functionDefinitionRule($, $._statement_declaration_specifiers, true),
406
456
 
407
457
  _old_style_declarator: ($) =>
408
458
  choice(
@@ -420,64 +470,83 @@ module.exports = Object.assign(
420
470
  '*',
421
471
  repeat($.attribute_declaration),
422
472
  repeat($.ms_pointer_modifier),
423
- repeat(choice($.type_qualifier, $.attribute_specifier)),
473
+ repeat(choice($.type_qualifier, $.attribute_specifier, $.macro_specifier)),
424
474
  field('declarator', alias($._old_style_declarator, $._declarator))
425
475
  )
426
476
  )
427
477
  ),
428
478
 
429
- declaration: ($) => declarationRule($, $._declaration_specifiers, true),
479
+ declaration: ($) => declarationRule($, $._declaration_specifiers, $._declaration_modifiers),
480
+ _statement_declaration: ($) =>
481
+ declarationRule($, $._statement_declaration_specifiers, $._statement_declaration_modifiers),
482
+
483
+ type_definition: ($) => typeDefinitionRule($, $._declaration_modifiers, true),
484
+ _statement_type_definition: ($) => typeDefinitionRule($, $._statement_declaration_modifiers, false),
430
485
 
431
486
  _macro_modified_declaration: ($) =>
432
- declarationRule($, $._macro_declaration_specifiers, false, $._macro_declaration_declarator),
487
+ declarationRule(
488
+ $,
489
+ $._macro_declaration_specifiers,
490
+ $._non_macro_declaration_modifiers,
491
+ false,
492
+ $._macro_declaration_declarator
493
+ ),
494
+
495
+ _trailing_macro_modified_declaration: ($) =>
496
+ seq(
497
+ sym('_trailing_object_declaration_start'),
498
+ $._declaration_specifiers,
499
+ field('declarator', $._trailing_macro_object_declarator),
500
+ repeat1(alias($._trailing_macro_attribute_specifier, $.attribute_specifier)),
501
+ ';'
502
+ ),
503
+
504
+ _trailing_macro_object_declarator: ($) =>
505
+ choice($.identifier, alias($._trailing_macro_pointer_declarator, $.pointer_declarator)),
506
+
507
+ _trailing_macro_pointer_declarator: ($) => pointerDeclaratorRule($, $._trailing_macro_object_declarator),
433
508
 
434
509
  _macro_declaration_specifiers: ($) => {
435
- const modifier = choice($._declaration_modifiers, alias($._macro_attribute_suffix, $.attribute_specifier));
510
+ const modifier = choice(
511
+ $._non_macro_declaration_modifiers,
512
+ alias($._macro_attribute_suffix, $.attribute_specifier)
513
+ );
436
514
  const postModifier = choice(
437
- $._declaration_modifiers,
515
+ $._non_macro_declaration_modifiers,
438
516
  alias($._post_type_macro_attribute_suffix, $.attribute_specifier)
439
517
  );
440
518
  return choice(
441
519
  seq(
442
520
  choice($.storage_class_specifier, $.type_qualifier, $.ms_declspec_modifier),
443
- repeat($._declaration_modifiers),
521
+ repeat($._non_macro_declaration_modifiers),
444
522
  alias($._macro_attribute_specifier, $.attribute_specifier),
445
523
  repeat(modifier),
446
524
  field('type', $._non_identifier_type_specifier),
447
525
  repeat(postModifier)
448
526
  ),
449
527
  seq(
450
- repeat($._declaration_modifiers),
528
+ repeat($._non_macro_declaration_modifiers),
451
529
  field('type', choice($._non_identifier_type_specifier, $._type_identifier)),
452
- repeat($._declaration_modifiers),
530
+ repeat($._non_macro_declaration_modifiers),
453
531
  alias($._post_type_macro_attribute_specifier, $.attribute_specifier),
454
532
  repeat(postModifier)
455
533
  )
456
534
  );
457
535
  },
458
536
 
459
- type_definition: ($) =>
537
+ _type_definition_type: ($) =>
460
538
  seq(
461
- optional(prec.dynamic(1, '__extension__')),
462
- choice(
463
- seq(repeat($.type_qualifier), 'typedef', $._type_definition_type),
464
- seq(
465
- repeat($._declaration_modifiers),
466
- field('type', $._non_identifier_type_specifier),
467
- repeat($.type_qualifier),
468
- 'typedef',
469
- repeat(choice($.type_qualifier, $._non_identifier_type_specifier))
470
- )
471
- ),
472
- $._type_definition_declarators,
473
- repeat($.attribute_specifier),
474
- ';'
539
+ repeat(choice($.type_qualifier, $.macro_specifier)),
540
+ field('type', $.type_specifier),
541
+ repeat($.type_qualifier)
475
542
  ),
476
- _type_definition_type: ($) =>
477
- seq(repeat($.type_qualifier), field('type', $.type_specifier), repeat($.type_qualifier)),
478
543
  _type_definition_declarators: ($) => commaSep1(field('declarator', $._type_declarator)),
479
544
 
480
- _declaration_modifiers: ($) =>
545
+ _declaration_modifiers: ($) => choice($._non_macro_declaration_modifiers, $.macro_specifier),
546
+
547
+ _statement_declaration_modifiers: ($) => $._non_macro_declaration_modifiers,
548
+
549
+ _non_macro_declaration_modifiers: ($) =>
481
550
  choice(
482
551
  $.storage_class_specifier,
483
552
  $.type_qualifier,
@@ -487,7 +556,18 @@ module.exports = Object.assign(
487
556
  ),
488
557
 
489
558
  _declaration_specifiers: ($) =>
490
- seq(repeat($._declaration_modifiers), field('type', $.type_specifier), repeat($._declaration_modifiers)),
559
+ seq(
560
+ repeat($._declaration_modifiers),
561
+ field('type', $.type_specifier),
562
+ repeat($._non_macro_declaration_modifiers)
563
+ ),
564
+
565
+ _statement_declaration_specifiers: ($) =>
566
+ seq(
567
+ repeat($._statement_declaration_modifiers),
568
+ field('type', $.type_specifier),
569
+ repeat($._statement_declaration_modifiers)
570
+ ),
491
571
 
492
572
  linkage_specification: ($) =>
493
573
  seq(
@@ -504,6 +584,8 @@ module.exports = Object.assign(
504
584
 
505
585
  _macro_attribute_suffix: ($) => macroAttributeRule($, 'continuation'),
506
586
 
587
+ _trailing_macro_attribute_specifier: ($) => macroAttributeRule($, 'trailing'),
588
+
507
589
  _post_type_macro_attribute_suffix: ($) => macroAttributeRule($, 'post-type-continuation'),
508
590
 
509
591
  _macro_attribute_argument_list: ($) => {
@@ -541,7 +623,7 @@ module.exports = Object.assign(
541
623
  $.ms_pointer_size_modifier
542
624
  ),
543
625
 
544
- declaration_list: ($) => seq('{', repeat($._block_item), '}'),
626
+ declaration_list: ($) => seq('{', repeat($._top_level_item), '}'),
545
627
 
546
628
  _declarator: ($) => choice($.attributed_declarator, $._unattributed_declarator),
547
629
 
@@ -607,21 +689,7 @@ module.exports = Object.assign(
607
689
  attributed_field_declarator: ($) => seq($._unattributed_field_declarator, repeat1($.attribute_declaration)),
608
690
  attributed_type_declarator: ($) => seq($._unattributed_type_declarator, repeat1($.attribute_declaration)),
609
691
 
610
- pointer_declarator: ($) =>
611
- prec.dynamic(
612
- 1,
613
- prec.right(
614
- seq(
615
- optional($.ms_based_modifier),
616
- optional(prec.dynamic(2, $.ms_call_modifier)),
617
- '*',
618
- repeat($.attribute_declaration),
619
- repeat($.ms_pointer_modifier),
620
- repeat(choice($.type_qualifier, $.attribute_specifier)),
621
- field('declarator', $._declarator)
622
- )
623
- )
624
- ),
692
+ pointer_declarator: ($) => pointerDeclaratorRule($, $._declarator),
625
693
  pointer_field_declarator: ($) =>
626
694
  prec.dynamic(
627
695
  1,
@@ -632,7 +700,7 @@ module.exports = Object.assign(
632
700
  '*',
633
701
  repeat($.attribute_declaration),
634
702
  repeat($.ms_pointer_modifier),
635
- repeat(choice($.type_qualifier, $.attribute_specifier)),
703
+ repeat(choice($.type_qualifier, $.attribute_specifier, $.macro_specifier)),
636
704
  field('declarator', $._field_declarator)
637
705
  )
638
706
  )
@@ -647,7 +715,7 @@ module.exports = Object.assign(
647
715
  '*',
648
716
  repeat($.attribute_declaration),
649
717
  repeat($.ms_pointer_modifier),
650
- repeat(choice($.type_qualifier, $.attribute_specifier)),
718
+ repeat(choice($.type_qualifier, $.attribute_specifier, $.macro_specifier)),
651
719
  field('declarator', $._type_declarator)
652
720
  )
653
721
  )
@@ -817,19 +885,14 @@ module.exports = Object.assign(
817
885
  bit_int_specifier: ($) => seq('_BitInt', '(', $.expression, ')'),
818
886
 
819
887
  sized_type_specifier: ($) =>
820
- sizedTypeSpecifier(optional(choice(prec.dynamic(-1, $._type_identifier), $.primitive_type))),
888
+ sizedTypeSpecifier(optional(choice(prec.dynamic(-3, $._type_identifier), $.primitive_type))),
821
889
 
822
890
  _sized_bit_int_specifier: ($) => sizedTypeSpecifier($.bit_int_specifier),
823
891
 
824
892
  primitive_type: () =>
825
893
  token(
826
894
  choice(
827
- 'bool',
828
- 'char',
829
- 'int',
830
- 'float',
831
- 'double',
832
- 'void',
895
+ ...primitiveTypeKeywords,
833
896
  'size_t',
834
897
  'ssize_t',
835
898
  'ptrdiff_t',
@@ -934,6 +997,7 @@ module.exports = Object.assign(
934
997
  _field_declaration_list_item: ($) =>
935
998
  choice(
936
999
  $.field_declaration,
1000
+ prec.dynamic(-1, $.macro_specifier),
937
1001
  $.preproc_def,
938
1002
  $.preproc_function_def,
939
1003
  $.preproc_call,
@@ -1007,7 +1071,12 @@ module.exports = Object.assign(
1007
1071
  $.goto_statement
1008
1072
  ),
1009
1073
 
1010
- labeled_statement: ($) => seq(field('label', $._statement_identifier), ':', choice($.declaration, $.statement)),
1074
+ labeled_statement: ($) =>
1075
+ seq(
1076
+ field('label', $._statement_identifier),
1077
+ ':',
1078
+ choice(alias($._statement_declaration, $.declaration), $.statement)
1079
+ ),
1011
1080
 
1012
1081
  // This is missing binary expressions, others were kept so that macro code can be parsed better and code examples
1013
1082
  _top_level_expression_statement: ($) => seq(optional($._expression_not_binary), ';'),
@@ -1072,14 +1141,18 @@ module.exports = Object.assign(
1072
1141
  1,
1073
1142
  choice(
1074
1143
  $._single_statement,
1075
- $.declaration,
1076
- $.type_definition,
1144
+ alias($._statement_declaration, $.declaration),
1145
+ alias($._statement_type_definition, $.type_definition),
1077
1146
  alias(sym('preproc_if_in_single_case'), sym('preproc_if')),
1078
1147
  alias(sym('preproc_ifdef_in_single_case'), sym('preproc_ifdef'))
1079
1148
  )
1080
1149
  ),
1081
1150
  _single_labeled_statement: ($) =>
1082
- seq(field('label', $._statement_identifier), ':', choice($._single_statement, $.declaration)),
1151
+ seq(
1152
+ field('label', $._statement_identifier),
1153
+ ':',
1154
+ choice($._single_statement, alias($._statement_declaration, $.declaration))
1155
+ ),
1083
1156
  _single_attributed_statement: ($) =>
1084
1157
  seq(
1085
1158
  repeat1($.attribute_declaration),
@@ -1102,7 +1175,14 @@ module.exports = Object.assign(
1102
1175
  ),
1103
1176
 
1104
1177
  _case_statement_item: ($) =>
1105
- prec.dynamic(1, choice(alias($._case_body_statement, $.statement), $.declaration, $.type_definition)),
1178
+ prec.dynamic(
1179
+ 1,
1180
+ choice(
1181
+ alias($._case_body_statement, $.statement),
1182
+ alias($._statement_declaration, $.declaration),
1183
+ alias($._statement_type_definition, $.type_definition)
1184
+ )
1185
+ ),
1106
1186
 
1107
1187
  _case_body_statement: ($) =>
1108
1188
  choice(
@@ -1115,7 +1195,7 @@ module.exports = Object.assign(
1115
1195
  seq(
1116
1196
  field('label', $._statement_identifier),
1117
1197
  ':',
1118
- choice($.declaration, alias($._case_body_statement, $.statement))
1198
+ choice(alias($._statement_declaration, $.declaration), alias($._case_body_statement, $.statement))
1119
1199
  ),
1120
1200
  _case_body_attributed_statement: ($) =>
1121
1201
  prec(1, seq(repeat1($.attribute_declaration), alias($._case_body_unattributed_statement, $.statement))),
@@ -1144,7 +1224,7 @@ module.exports = Object.assign(
1144
1224
  _for_statement_body: ($) =>
1145
1225
  seq(
1146
1226
  choice(
1147
- field('initializer', $.declaration),
1227
+ field('initializer', alias($._statement_declaration, $.declaration)),
1148
1228
  seq(field('initializer', optional(choice($.expression, $.comma_expression))), ';')
1149
1229
  ),
1150
1230
  field('condition', optional(choice($.expression, $.comma_expression))),
@@ -1600,7 +1680,10 @@ module.exports = Object.assign(
1600
1680
  _field_identifier: ($) => alias(choice($.identifier, VA_ARG_KEYWORDS), sym('field_identifier')),
1601
1681
  _statement_identifier: ($) => alias(choice($.identifier, VA_ARG_KEYWORDS), sym('statement_identifier')),
1602
1682
 
1603
- _empty_declaration: ($) => seq(repeat($.ms_declspec_modifier), $.type_specifier, ';'),
1683
+ _empty_declaration: ($) => seq(repeat(choice($.ms_declspec_modifier, $.macro_specifier)), $.type_specifier, ';'),
1684
+ _statement_empty_declaration: ($) => seq(repeat($.ms_declspec_modifier), $.type_specifier, ';'),
1685
+
1686
+ macro_specifier: ($) => prec.dynamic(-2, field('name', $.identifier)),
1604
1687
 
1605
1688
  macro_type_specifier: ($) =>
1606
1689
  prec.dynamic(
@@ -1623,32 +1706,96 @@ module.exports = Object.assign(
1623
1706
  );
1624
1707
 
1625
1708
  /**
1626
- * @param {import('./types/treeSitterDsl').GrammarSymbols<'storage_class_specifier' | 'ms_call_modifier' | '_declaration_declarator' | 'gnu_asm_expression' | 'init_declarator' | '_declaration_modifiers'>} $
1627
- * @param {import('./types/treeSitterDsl').Rule} specifiers
1628
- * @param {boolean} allowAuto
1709
+ * @param {import('./types/treeSitterDsl').GrammarSymbols<'ms_based_modifier' | 'ms_call_modifier' | 'attribute_declaration' | 'ms_pointer_modifier' | 'type_qualifier' | 'attribute_specifier' | 'macro_specifier'>} $
1629
1710
  * @param {import('./types/treeSitterDsl').Rule} declarator
1630
1711
  */
1631
- function declarationRule($, specifiers, allowAuto, declarator = $._declaration_declarator) {
1712
+ function pointerDeclaratorRule($, declarator) {
1713
+ return prec.right(
1714
+ seq(
1715
+ optional($.ms_based_modifier),
1716
+ optional(prec.dynamic(2, $.ms_call_modifier)),
1717
+ '*',
1718
+ repeat($.attribute_declaration),
1719
+ repeat($.ms_pointer_modifier),
1720
+ repeat(choice($.type_qualifier, $.attribute_specifier, $.macro_specifier)),
1721
+ field('declarator', declarator)
1722
+ )
1723
+ );
1724
+ }
1725
+
1726
+ /**
1727
+ * @param {import('./types/treeSitterDsl').GrammarSymbols<'ms_call_modifier' | '_old_style_declarator' | '_declarator' | 'declaration' | 'compound_statement'>} $
1728
+ * @param {import('./types/treeSitterDsl').Rule} specifiers
1729
+ * @param {boolean} oldStyle
1730
+ */
1731
+ function functionDefinitionRule($, specifiers, oldStyle) {
1632
1732
  return seq(
1633
- allowAuto
1634
- ? choice(
1635
- specifiers,
1636
- prec.dynamic(
1637
- PREC.PAREN_DECLARATOR - 1,
1638
- seq(
1639
- repeat($._declaration_modifiers),
1640
- alias('auto', $.storage_class_specifier),
1641
- repeat($._declaration_modifiers)
1733
+ optional($.ms_call_modifier),
1734
+ specifiers,
1735
+ ...(oldStyle ? [] : [optional($.ms_call_modifier)]),
1736
+ field('declarator', oldStyle ? alias($._old_style_declarator, $._declarator) : $._declarator),
1737
+ ...(oldStyle ? [repeat1($.declaration)] : []),
1738
+ field('body', $.compound_statement)
1739
+ );
1740
+ }
1741
+
1742
+ /**
1743
+ * @param {import('./types/treeSitterDsl').GrammarSymbols<'storage_class_specifier' | 'ms_call_modifier' | '_declaration_declarator' | 'gnu_asm_expression' | 'init_declarator' | 'type_qualifier' | 'identifier'>} $
1744
+ * @param {import('./types/treeSitterDsl').Rule} specifiers
1745
+ * @param {import('./types/treeSitterDsl').Rule} modifiers
1746
+ * @param {boolean} allowInferredType
1747
+ * @param {import('./types/treeSitterDsl').Rule} declarator
1748
+ */
1749
+ function declarationRule($, specifiers, modifiers, allowInferredType = true, declarator = $._declaration_declarator) {
1750
+ return choice(
1751
+ seq(
1752
+ prec.dynamic(
1753
+ 1,
1754
+ allowInferredType
1755
+ ? choice(
1756
+ specifiers,
1757
+ prec.dynamic(
1758
+ PREC.PAREN_DECLARATOR - 1,
1759
+ seq(repeat(modifiers), alias('auto', $.storage_class_specifier), repeat(modifiers))
1760
+ )
1642
1761
  )
1643
- )
1762
+ : specifiers
1763
+ ),
1764
+ commaSep1(
1765
+ field(
1766
+ 'declarator',
1767
+ choice(seq(optional($.ms_call_modifier), declarator, optional($.gnu_asm_expression)), $.init_declarator)
1644
1768
  )
1645
- : specifiers,
1646
- commaSep1(
1647
- field(
1648
- 'declarator',
1649
- choice(seq(optional($.ms_call_modifier), declarator, optional($.gnu_asm_expression)), $.init_declarator)
1769
+ ),
1770
+ ';'
1771
+ ),
1772
+ ...(allowInferredType
1773
+ ? [seq(repeat1($.type_qualifier), commaSep1(field('declarator', choice($.identifier, $.init_declarator))), ';')]
1774
+ : [])
1775
+ );
1776
+ }
1777
+
1778
+ /**
1779
+ * @param {import('./types/treeSitterDsl').GrammarSymbols<'macro_specifier' | 'type_qualifier' | '_type_definition_type' | '_non_identifier_type_specifier' | '_type_definition_declarators' | 'attribute_specifier'>} $
1780
+ * @param {import('./types/treeSitterDsl').Rule} modifiers
1781
+ * @param {boolean} allowPrefixMarkers
1782
+ */
1783
+ function typeDefinitionRule($, modifiers, allowPrefixMarkers) {
1784
+ return seq(
1785
+ optional(prec.dynamic(1, '__extension__')),
1786
+ ...(allowPrefixMarkers ? [repeat($.macro_specifier)] : []),
1787
+ choice(
1788
+ seq(repeat($.type_qualifier), 'typedef', $._type_definition_type),
1789
+ seq(
1790
+ repeat(modifiers),
1791
+ field('type', $._non_identifier_type_specifier),
1792
+ repeat($.type_qualifier),
1793
+ 'typedef',
1794
+ repeat(choice($.type_qualifier, $._non_identifier_type_specifier))
1650
1795
  )
1651
1796
  ),
1797
+ $._type_definition_declarators,
1798
+ repeat($.attribute_specifier),
1652
1799
  ';'
1653
1800
  );
1654
1801
  }
@@ -1669,36 +1816,13 @@ function declarationDeclaratorRule($, functionDeclarator) {
1669
1816
  );
1670
1817
  }
1671
1818
 
1672
- /**
1673
- * @param {import('./types/treeSitterDsl').GrammarSymbols<'function_definition' | '_old_style_function_definition' | 'linkage_specification' | 'declaration' | '_macro_modified_declaration' | 'statement' | 'type_definition' | '_empty_declaration' | 'preproc_include' | 'preproc_def' | 'preproc_function_def' | 'preproc_call'>} $
1674
- * @param {boolean} header
1675
- */
1676
- function blockItemRule($, header) {
1677
- return choice(
1678
- $.function_definition,
1679
- alias($._old_style_function_definition, $.function_definition),
1680
- $.linkage_specification,
1681
- $.declaration,
1682
- ...(header ? [alias($._macro_modified_declaration, $.declaration)] : []),
1683
- $.statement,
1684
- $.type_definition,
1685
- $._empty_declaration,
1686
- header ? alias(sym('preproc_if_in_header'), sym('preproc_if')) : sym('preproc_if'),
1687
- header ? alias(sym('preproc_ifdef_in_header'), sym('preproc_ifdef')) : sym('preproc_ifdef'),
1688
- $.preproc_include,
1689
- $.preproc_def,
1690
- $.preproc_function_def,
1691
- $.preproc_call
1692
- );
1693
- }
1694
-
1695
1819
  /**
1696
1820
  * @param {import('./types/treeSitterDsl').GrammarSymbols<'identifier' | 'argument_list' | '_macro_attribute_argument_list'>} $
1697
- * @param {'leading' | 'post-type' | 'continuation' | 'post-type-continuation'} context
1821
+ * @param {'leading' | 'post-type' | 'continuation' | 'post-type-continuation' | 'trailing'} context
1698
1822
  */
1699
1823
  function macroAttributeRule($, context) {
1700
1824
  const guard =
1701
- context === 'continuation'
1825
+ context === 'continuation' || context === 'trailing'
1702
1826
  ? []
1703
1827
  : [
1704
1828
  sym(
@@ -1713,7 +1837,7 @@ function macroAttributeRule($, context) {
1713
1837
  -2,
1714
1838
  seq(
1715
1839
  ...guard,
1716
- field('name', $.identifier),
1840
+ field('name', context === 'trailing' ? alias(sym('_trailing_macro_attribute_name'), $.identifier) : $.identifier),
1717
1841
  field('arguments', alias($._macro_attribute_argument_list, $.argument_list))
1718
1842
  )
1719
1843
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-c",
3
- "version": "10.0.0",
3
+ "version": "12.0.0",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "incremental",
@@ -21,6 +21,7 @@
21
21
  "src/node-types.json",
22
22
  "tree-sitter.json",
23
23
  "*.wasm",
24
+ "src/grammarKeywords.cjs",
24
25
  "src/pragma.h",
25
26
  "src/identifier.h",
26
27
  "src/keywords.h",
@@ -18,6 +18,13 @@ typedef struct {
18
18
  bool declarator_name;
19
19
  } MacroPrototypeTail;
20
20
 
21
+ typedef struct {
22
+ TSSymbol pragma;
23
+ TSSymbol object_start;
24
+ bool allow_pragma;
25
+ bool allow_object_start;
26
+ } MacroAttributeSymbols;
27
+
21
28
  static bool function_macro_balanced(TSLexer *lexer, int32_t open, int32_t close, bool *literal);
22
29
  static bool function_macro_group(int32_t c, int32_t open, int32_t close, unsigned *depth, unsigned *nested);
23
30
  static void function_macro_note_literal(bool *literal, unsigned depth, unsigned nested, const MacroArgumentWord *word, int32_t c);
@@ -32,24 +39,100 @@ static bool function_macro_line_comment(TSLexer *lexer);
32
39
  static bool function_macro_block_comment(TSLexer *lexer);
33
40
  static void function_macro_argument_word(MacroArgumentWord *word, int32_t c);
34
41
 
42
+ static bool function_macro_trailing_argument_prefix(TSLexer *lexer);
43
+ static bool function_macro_object_prefix(TSLexer *lexer);
44
+ static bool function_macro_object_word(TSLexer *lexer, MacroName *name, bool escaped);
45
+ static bool function_macro_scalar_type_keyword(const char *word);
46
+ static bool function_macro_object_prefix_keyword(const char *word);
47
+ static bool function_macro_attribute_keyword(const char *word, bool trailing);
35
48
  static bool function_macro_keyword(const char *word);
36
49
  static bool function_macro_word_in(const char *word, const char *const *words, unsigned count);
37
50
 
38
- static bool scan_function_attribute_guard(TSLexer *lexer, bool prototype, bool require_literal, bool allow_pragma, TSSymbol pragma_symbol) {
51
+ static bool scan_function_attribute_guard(TSLexer *lexer, bool prototype, bool require_literal, bool trailing, const MacroAttributeSymbols *symbols) {
39
52
  char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
40
- MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true, .lookahead_only = true};
53
+ MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true, .lookahead_only = !trailing};
41
54
  if (!scan_macro_name(lexer, &name)) return false;
42
55
  if (name.ascii && strcmp(word, pragma_word) == 0) {
43
- lexer->result_symbol = pragma_symbol;
44
- return allow_pragma && scan_pragma_suffix(lexer);
56
+ lexer->result_symbol = symbols->pragma;
57
+ return symbols->allow_pragma && scan_pragma_suffix(lexer);
45
58
  }
46
- if (name.ascii && function_macro_keyword(word)) return false;
59
+ if (symbols->allow_object_start && name.ascii && function_macro_scalar_type_keyword(word)) {
60
+ lexer->result_symbol = symbols->object_start;
61
+ return function_macro_object_prefix(lexer);
62
+ }
63
+ if (lexer->result_symbol == symbols->object_start) return false;
64
+ if (name.ascii && function_macro_attribute_keyword(word, trailing)) return false;
47
65
  if (!scan_pragma_spacing(lexer) || lexer->lookahead != '(') return false;
66
+ if (trailing) return function_macro_trailing_argument_prefix(lexer);
48
67
  bool literal = false;
49
68
  return function_macro_balanced(lexer, '(', ')', &literal) && (!require_literal || literal) &&
50
69
  (!prototype || function_macro_declaration_continues(lexer, require_literal));
51
70
  }
52
71
 
72
+ static bool function_macro_trailing_argument_prefix(TSLexer *lexer) {
73
+ do {
74
+ lexer->advance(lexer, false);
75
+ if (!scan_pragma_spacing(lexer)) return false;
76
+ } while (lexer->lookahead == '(');
77
+ int32_t first = lexer->lookahead;
78
+ if (first == '.') {
79
+ lexer->advance(lexer, false);
80
+ first = lexer->lookahead;
81
+ return first >= '0' && first <= '9';
82
+ }
83
+ return first == ',' || first == '"' || first == '\'' || (first >= '0' && first <= '9');
84
+ }
85
+
86
+ static bool function_macro_object_prefix(TSLexer *lexer) {
87
+ bool escaped = false;
88
+ if (!function_macro_tail_spacing(lexer, &escaped)) return false;
89
+ while (!escaped && lexer->lookahead == '*') {
90
+ lexer->advance(lexer, false);
91
+ if (!function_macro_tail_spacing(lexer, &escaped)) return false;
92
+ }
93
+ char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
94
+ MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true};
95
+ if (!function_macro_object_word(lexer, &name, escaped) ||
96
+ (name.ascii && (function_macro_object_prefix_keyword(word) || strcmp(word, pragma_word) == 0))) return false;
97
+ escaped = false;
98
+ if (!function_macro_tail_spacing(lexer, &escaped)) return false;
99
+ name = (MacroName){.word = word, .capacity = sizeof(word) - 1, .ascii = true};
100
+ if (!function_macro_object_word(lexer, &name, escaped) || (name.ascii && function_macro_attribute_keyword(word, true))) return false;
101
+ return scan_pragma_spacing(lexer) && lexer->lookahead == '(' && function_macro_trailing_argument_prefix(lexer);
102
+ }
103
+
104
+ static bool function_macro_object_word(TSLexer *lexer, MacroName *name, bool escaped) {
105
+ memset(name->word, 0, name->capacity + 1);
106
+ if (escaped) {
107
+ name->ascii = false;
108
+ if (!scan_macro_name_ucn(lexer)) return false;
109
+ }
110
+ bool continuation = escaped;
111
+ for (;;) {
112
+ MacroNameCharacter c = scan_macro_name_character(lexer, continuation, name);
113
+ if (c == MACRO_NAME_END) return continuation;
114
+ if (c == MACRO_NAME_SPLICE && continuation) return scan_preproc_splices(lexer);
115
+ if (c != MACRO_NAME_CHARACTER) return false;
116
+ continuation = true;
117
+ }
118
+ }
119
+
120
+ static bool function_macro_scalar_type_keyword(const char *word) {
121
+ return function_macro_word_in(word, function_macro_scalar_type_keywords,
122
+ sizeof(function_macro_scalar_type_keywords) / sizeof(function_macro_scalar_type_keywords[0]));
123
+ }
124
+
125
+ static bool function_macro_object_prefix_keyword(const char *word) {
126
+ return function_macro_word_in(word, function_macro_object_prefix_keywords,
127
+ sizeof(function_macro_object_prefix_keywords) / sizeof(function_macro_object_prefix_keywords[0]));
128
+ }
129
+
130
+ static bool function_macro_attribute_keyword(const char *word, bool trailing) {
131
+ if (!trailing) return function_macro_keyword(word);
132
+ return strcmp(word, pragma_word) == 0 || function_macro_word_in(word, function_macro_group_modifiers,
133
+ sizeof(function_macro_group_modifiers) / sizeof(function_macro_group_modifiers[0]));
134
+ }
135
+
53
136
  static bool function_macro_balanced(TSLexer *lexer, int32_t open, int32_t close, bool *literal) {
54
137
  unsigned depth = 1;
55
138
  unsigned nested = 0;
@@ -0,0 +1,4 @@
1
+ // @ts-check
2
+
3
+ // oxlint-disable-next-line unicorn/prefer-module -- Tree-sitter loads grammar dependencies as CommonJS.
4
+ module.exports = { primitiveTypeKeywords: ['bool', 'char', 'int', 'float', 'double', 'void'] };
package/src/keywords.h CHANGED
@@ -190,6 +190,84 @@ static const char *const function_macro_type_keywords[] = {
190
190
  "unsigned",
191
191
  "void",
192
192
  };
193
+ static const char *const function_macro_scalar_type_keywords[] = {
194
+ "bool",
195
+ "char",
196
+ "char16_t",
197
+ "char32_t",
198
+ "char64_t",
199
+ "char8_t",
200
+ "charptr_t",
201
+ "double",
202
+ "float",
203
+ "int",
204
+ "int16_t",
205
+ "int32_t",
206
+ "int64_t",
207
+ "int8_t",
208
+ "intptr_t",
209
+ "max_align_t",
210
+ "nullptr_t",
211
+ "ptrdiff_t",
212
+ "size_t",
213
+ "ssize_t",
214
+ "uint16_t",
215
+ "uint32_t",
216
+ "uint64_t",
217
+ "uint8_t",
218
+ "uintptr_t",
219
+ "void",
220
+ };
221
+ static const char *const function_macro_object_prefix_keywords[] = {
222
+ "_Alignas",
223
+ "_Atomic",
224
+ "_Complex",
225
+ "_Imaginary",
226
+ "_Nonnull",
227
+ "_Noreturn",
228
+ "_Thread_local",
229
+ "__asm",
230
+ "__asm__",
231
+ "__attribute",
232
+ "__attribute__",
233
+ "__cdecl",
234
+ "__clrcall",
235
+ "__complex__",
236
+ "__declspec",
237
+ "__extension__",
238
+ "__fastcall",
239
+ "__forceinline",
240
+ "__inline",
241
+ "__inline__",
242
+ "__restrict__",
243
+ "__stdcall",
244
+ "__thiscall",
245
+ "__thread",
246
+ "__vectorcall",
247
+ "alignas",
248
+ "asm",
249
+ "auto",
250
+ "bool",
251
+ "char",
252
+ "const",
253
+ "constexpr",
254
+ "double",
255
+ "extern",
256
+ "float",
257
+ "inline",
258
+ "int",
259
+ "long",
260
+ "noreturn",
261
+ "register",
262
+ "restrict",
263
+ "short",
264
+ "signed",
265
+ "static",
266
+ "thread_local",
267
+ "unsigned",
268
+ "void",
269
+ "volatile",
270
+ };
193
271
  static const char *const function_macro_declaration_modifiers[] = {
194
272
  "_Alignas",
195
273
  "_Atomic",
@@ -1448,6 +1448,10 @@
1448
1448
  "type": "attribute_specifier",
1449
1449
  "named": true
1450
1450
  },
1451
+ {
1452
+ "type": "macro_specifier",
1453
+ "named": true
1454
+ },
1451
1455
  {
1452
1456
  "type": "ms_declspec_modifier",
1453
1457
  "named": true
@@ -1471,18 +1475,66 @@
1471
1475
  "multiple": true,
1472
1476
  "required": false,
1473
1477
  "types": [
1478
+ {
1479
+ "type": "attributed_statement",
1480
+ "named": true
1481
+ },
1482
+ {
1483
+ "type": "break_statement",
1484
+ "named": true
1485
+ },
1486
+ {
1487
+ "type": "case_statement",
1488
+ "named": true
1489
+ },
1490
+ {
1491
+ "type": "compound_statement",
1492
+ "named": true
1493
+ },
1494
+ {
1495
+ "type": "continue_statement",
1496
+ "named": true
1497
+ },
1474
1498
  {
1475
1499
  "type": "declaration",
1476
1500
  "named": true
1477
1501
  },
1502
+ {
1503
+ "type": "do_statement",
1504
+ "named": true
1505
+ },
1506
+ {
1507
+ "type": "expression_statement",
1508
+ "named": true
1509
+ },
1510
+ {
1511
+ "type": "for_statement",
1512
+ "named": true
1513
+ },
1478
1514
  {
1479
1515
  "type": "function_definition",
1480
1516
  "named": true
1481
1517
  },
1518
+ {
1519
+ "type": "goto_statement",
1520
+ "named": true
1521
+ },
1522
+ {
1523
+ "type": "if_statement",
1524
+ "named": true
1525
+ },
1526
+ {
1527
+ "type": "labeled_statement",
1528
+ "named": true
1529
+ },
1482
1530
  {
1483
1531
  "type": "linkage_specification",
1484
1532
  "named": true
1485
1533
  },
1534
+ {
1535
+ "type": "macro_specifier",
1536
+ "named": true
1537
+ },
1486
1538
  {
1487
1539
  "type": "ms_declspec_modifier",
1488
1540
  "named": true
@@ -1512,7 +1564,11 @@
1512
1564
  "named": true
1513
1565
  },
1514
1566
  {
1515
- "type": "statement",
1567
+ "type": "return_statement",
1568
+ "named": true
1569
+ },
1570
+ {
1571
+ "type": "switch_statement",
1516
1572
  "named": true
1517
1573
  },
1518
1574
  {
@@ -1522,6 +1578,10 @@
1522
1578
  {
1523
1579
  "type": "type_specifier",
1524
1580
  "named": true
1581
+ },
1582
+ {
1583
+ "type": "while_statement",
1584
+ "named": true
1525
1585
  }
1526
1586
  ]
1527
1587
  }
@@ -1757,6 +1817,10 @@
1757
1817
  "type": "bitfield_clause",
1758
1818
  "named": true
1759
1819
  },
1820
+ {
1821
+ "type": "macro_specifier",
1822
+ "named": true
1823
+ },
1760
1824
  {
1761
1825
  "type": "ms_declspec_modifier",
1762
1826
  "named": true
@@ -1784,6 +1848,10 @@
1784
1848
  "type": "field_declaration",
1785
1849
  "named": true
1786
1850
  },
1851
+ {
1852
+ "type": "macro_specifier",
1853
+ "named": true
1854
+ },
1787
1855
  {
1788
1856
  "type": "preproc_call",
1789
1857
  "named": true
@@ -2031,6 +2099,10 @@
2031
2099
  "type": "declaration",
2032
2100
  "named": true
2033
2101
  },
2102
+ {
2103
+ "type": "macro_specifier",
2104
+ "named": true
2105
+ },
2034
2106
  {
2035
2107
  "type": "ms_call_modifier",
2036
2108
  "named": true
@@ -2527,6 +2599,22 @@
2527
2599
  }
2528
2600
  }
2529
2601
  },
2602
+ {
2603
+ "type": "macro_specifier",
2604
+ "named": true,
2605
+ "fields": {
2606
+ "name": {
2607
+ "multiple": false,
2608
+ "required": true,
2609
+ "types": [
2610
+ {
2611
+ "type": "identifier",
2612
+ "named": true
2613
+ }
2614
+ ]
2615
+ }
2616
+ }
2617
+ },
2530
2618
  {
2531
2619
  "type": "macro_type_specifier",
2532
2620
  "named": true,
@@ -2705,6 +2793,10 @@
2705
2793
  "type": "attribute_specifier",
2706
2794
  "named": true
2707
2795
  },
2796
+ {
2797
+ "type": "macro_specifier",
2798
+ "named": true
2799
+ },
2708
2800
  {
2709
2801
  "type": "ms_declspec_modifier",
2710
2802
  "named": true
@@ -2862,6 +2954,10 @@
2862
2954
  "type": "attribute_specifier",
2863
2955
  "named": true
2864
2956
  },
2957
+ {
2958
+ "type": "macro_specifier",
2959
+ "named": true
2960
+ },
2865
2961
  {
2866
2962
  "type": "ms_based_modifier",
2867
2963
  "named": true
@@ -3103,6 +3199,10 @@
3103
3199
  "type": "linkage_specification",
3104
3200
  "named": true
3105
3201
  },
3202
+ {
3203
+ "type": "macro_specifier",
3204
+ "named": true
3205
+ },
3106
3206
  {
3107
3207
  "type": "ms_declspec_modifier",
3108
3208
  "named": true
@@ -3215,6 +3315,10 @@
3215
3315
  "type": "linkage_specification",
3216
3316
  "named": true
3217
3317
  },
3318
+ {
3319
+ "type": "macro_specifier",
3320
+ "named": true
3321
+ },
3218
3322
  {
3219
3323
  "type": "ms_declspec_modifier",
3220
3324
  "named": true
@@ -3298,6 +3402,10 @@
3298
3402
  "type": "linkage_specification",
3299
3403
  "named": true
3300
3404
  },
3405
+ {
3406
+ "type": "macro_specifier",
3407
+ "named": true
3408
+ },
3301
3409
  {
3302
3410
  "type": "ms_declspec_modifier",
3303
3411
  "named": true
@@ -3488,6 +3596,10 @@
3488
3596
  "type": "linkage_specification",
3489
3597
  "named": true
3490
3598
  },
3599
+ {
3600
+ "type": "macro_specifier",
3601
+ "named": true
3602
+ },
3491
3603
  {
3492
3604
  "type": "ms_declspec_modifier",
3493
3605
  "named": true
@@ -3600,6 +3712,10 @@
3600
3712
  "type": "linkage_specification",
3601
3713
  "named": true
3602
3714
  },
3715
+ {
3716
+ "type": "macro_specifier",
3717
+ "named": true
3718
+ },
3603
3719
  {
3604
3720
  "type": "ms_declspec_modifier",
3605
3721
  "named": true
@@ -4129,6 +4245,10 @@
4129
4245
  "type": "linkage_specification",
4130
4246
  "named": true
4131
4247
  },
4248
+ {
4249
+ "type": "macro_specifier",
4250
+ "named": true
4251
+ },
4132
4252
  {
4133
4253
  "type": "ms_declspec_modifier",
4134
4254
  "named": true
@@ -4229,6 +4349,10 @@
4229
4349
  "type": "enum_specifier",
4230
4350
  "named": true
4231
4351
  },
4352
+ {
4353
+ "type": "macro_specifier",
4354
+ "named": true
4355
+ },
4232
4356
  {
4233
4357
  "type": "ms_declspec_modifier",
4234
4358
  "named": true
package/src/scanner.c CHANGED
@@ -1,6 +1,6 @@
1
1
  #include "functionMacro.h"
2
2
 
3
- enum TokenType { PRAGMA_OPERATOR, PREPROC_ARG, PREPROC_NEWLINE, PREPROC_LPAREN, PREPROC_DIRECTIVE_ARG, PREPROC_FUNCTION_NAME, FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX };
3
+ enum TokenType { PRAGMA_OPERATOR, PREPROC_ARG, PREPROC_NEWLINE, PREPROC_LPAREN, PREPROC_DIRECTIVE_ARG, PREPROC_FUNCTION_NAME, FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX, TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START };
4
4
 
5
5
  static bool scan_preproc_tokens(TSLexer *lexer, const bool *valid_symbols);
6
6
 
@@ -30,13 +30,27 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
30
30
  lexer->result_symbol = PREPROC_FUNCTION_NAME;
31
31
  return scan_function_macro_name(lexer, valid_symbols[PRAGMA_OPERATOR], PRAGMA_OPERATOR);
32
32
  }
33
- bool leading = valid_symbols[FUNCTION_ATTRIBUTE_START];
34
- bool post_type = valid_symbols[POST_TYPE_FUNCTION_ATTRIBUTE_START];
35
- bool suffix = valid_symbols[POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX];
36
- if ((leading || post_type || suffix) && !valid_symbols[PREPROC_LPAREN] &&
37
- !valid_symbols[PREPROC_NEWLINE] && !valid_symbols[PREPROC_ARG] && !valid_symbols[PREPROC_DIRECTIVE_ARG]) {
38
- lexer->result_symbol = leading ? FUNCTION_ATTRIBUTE_START : post_type ? POST_TYPE_FUNCTION_ATTRIBUTE_START : POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX;
39
- return scan_function_attribute_guard(lexer, leading || post_type, !leading, valid_symbols[PRAGMA_OPERATOR], PRAGMA_OPERATOR);
33
+ if (valid_symbols[PREPROC_LPAREN] || valid_symbols[PREPROC_NEWLINE] ||
34
+ valid_symbols[PREPROC_ARG] || valid_symbols[PREPROC_DIRECTIVE_ARG]) {
35
+ return scan_preproc_tokens(lexer, valid_symbols);
36
+ }
37
+ static const TSSymbol attribute_tokens[] = {
38
+ FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX,
39
+ TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START,
40
+ };
41
+ const MacroAttributeSymbols symbols = {
42
+ .pragma = PRAGMA_OPERATOR,
43
+ .object_start = TRAILING_OBJECT_DECLARATION_START,
44
+ .allow_pragma = valid_symbols[PRAGMA_OPERATOR],
45
+ .allow_object_start = valid_symbols[TRAILING_OBJECT_DECLARATION_START],
46
+ };
47
+ for (unsigned i = 0; i < sizeof(attribute_tokens) / sizeof(attribute_tokens[0]); i++) {
48
+ TSSymbol token = attribute_tokens[i];
49
+ if (!valid_symbols[token]) continue;
50
+ lexer->result_symbol = token;
51
+ return scan_function_attribute_guard(lexer,
52
+ token == FUNCTION_ATTRIBUTE_START || token == POST_TYPE_FUNCTION_ATTRIBUTE_START,
53
+ token != FUNCTION_ATTRIBUTE_START, token == TRAILING_MACRO_ATTRIBUTE_NAME, &symbols);
40
54
  }
41
55
  return scan_preproc_tokens(lexer, valid_symbols);
42
56
  }
Binary file
package/tree-sitter.json CHANGED
@@ -16,7 +16,7 @@
16
16
  }
17
17
  ],
18
18
  "metadata": {
19
- "version": "10.0.0",
19
+ "version": "12.0.0",
20
20
  "license": "MIT",
21
21
  "description": "C grammar for tree-sitter",
22
22
  "authors": [