@willbooster/tree-sitter-c 11.0.0 → 13.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 +24 -2
- package/grammar.js +285 -129
- package/package.json +7 -6
- package/src/conditionalMacro.h +208 -0
- package/src/keywords.h +1 -0
- package/src/node-types.json +149 -1
- package/src/scanner.c +12 -2
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +1 -1
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[](https://github.com/WillBooster/tree-sitter-c/actions/workflows/test.yml)
|
|
6
6
|
[](https://github.com/WillBooster/tree-sitter-c/actions/workflows/test-rust.yml)
|
|
7
7
|
[](https://github.com/semantic-release/semantic-release)
|
|
8
|
-
[](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
|
|
9
9
|
[](https://crates.io/crates/willbooster-tree-sitter-c)
|
|
10
10
|
|
|
11
11
|
C grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
|
|
@@ -58,10 +58,16 @@ parser.setLanguage(await Language.load(c));
|
|
|
58
58
|
|
|
59
59
|
The package also ships `grammar.js`, scanner sources under `src/`, the queries in `queries/`,
|
|
60
60
|
and the node types in `src/node-types.json` for grammars extending C (such as C++).
|
|
61
|
-
The scanner sources include `scanner.c`, `pragma.h`, `identifier.h`, `keywords.h`,
|
|
61
|
+
The scanner sources include `scanner.c`, `pragma.h`, `identifier.h`, `keywords.h`, `functionMacro.h`, and
|
|
62
|
+
`conditionalMacro.h`.
|
|
62
63
|
|
|
63
64
|
Derived grammars must port the inherited external-token dispatch from `src/scanner.c`.
|
|
64
65
|
Keep preprocessor argument/newline handling ahead of annotation lookahead.
|
|
66
|
+
Port `_conditional_marker_ifdef`, `_conditional_marker_ifndef`, `_leading_conditional_marker_ifdef`, and
|
|
67
|
+
`_leading_conditional_marker_ifndef` with `scan_conditional_macro`. The leading pair checks a following type prefix;
|
|
68
|
+
when both contexts are valid, choose the leading role if its type proof succeeds, otherwise use the valid
|
|
69
|
+
post-type role. Each token consumes only its opening directive keyword; the directive body, closing directive and following prefix are lookahead. Keep this dispatch ahead of
|
|
70
|
+
the function-annotation guards and return immediately after a failed directive scan.
|
|
65
71
|
The hidden `_function_attribute_start`, `_post_type_function_attribute_start`, and
|
|
66
72
|
`_post_type_function_attribute_suffix` guards look ahead before the ordinary identifier token;
|
|
67
73
|
keep them outside the name field so identifier leaves and ordinary C lexing retain their shape.
|
|
@@ -87,6 +93,22 @@ The separate `_trailing_macro_modified_declaration` route uses public declaratio
|
|
|
87
93
|
`_trailing_macro_object_declarator` / `_trailing_macro_pointer_declarator`; adapt these header rules when
|
|
88
94
|
overriding declaration or declarator behavior.
|
|
89
95
|
|
|
96
|
+
Conditional declaration markers support an `#ifdef` or `#ifndef` block containing one marker identifier,
|
|
97
|
+
followed by `#endif` and a declaration prefix. Comments, blank lines and line splices in trivia are accepted.
|
|
98
|
+
Leading markers require a builtin type, or a type alias followed by another identifier or `*`; declaration
|
|
99
|
+
modifiers alone do not establish a type. Known grouped modifiers, pragma extras and bracket attributes are
|
|
100
|
+
checked once before the type. Groups with unquoted braces, semicolons or directives need separate support.
|
|
101
|
+
EOF, another directive and adjacent marker blocks retain ordinary preprocessor parsing. The scanner does not infer whether a conditional identifier expands to a type or annotation.
|
|
102
|
+
|
|
103
|
+
Normal and old-style pointer declarators and typedef type prefixes also accept conditional markers. Post-type
|
|
104
|
+
markers use `_conditional_declaration`, `_conditional_function_definition`, and
|
|
105
|
+
`_conditional_old_style_function_definition`; adapt these header rules when overriding declarations or function
|
|
106
|
+
definitions. The conditional remains a direct `preproc_ifdef` child of its annotated declaration or pointer,
|
|
107
|
+
with its physical directive range. Tag-only declarations retain their top-level tag node without inventing a
|
|
108
|
+
declarator. The marker identifier has the ordinary `macro_specifier.name` field. A containing function or field starts at its first annotation.
|
|
109
|
+
Other conditional forms, multiple-marker bodies, adjacent marker blocks, implicit-int prefixes and body-bearing
|
|
110
|
+
modifier groups need separate annotation support.
|
|
111
|
+
|
|
90
112
|
Trailing object annotations support one complete `primitive_type` token, plain pointer stars and an identifier,
|
|
91
113
|
for example `int *value ATTR((1));`. The first argument token after initial parentheses must be numeric,
|
|
92
114
|
a string/character quote or a comma. Comments and whitespace may separate the prefix tokens; the remaining
|
package/grammar.js
CHANGED
|
@@ -71,11 +71,28 @@ module.exports = Object.assign(
|
|
|
71
71
|
name: 'c',
|
|
72
72
|
|
|
73
73
|
conflicts: ($) => [
|
|
74
|
-
[$.
|
|
74
|
+
[$._declaration_modifiers, $._empty_declaration],
|
|
75
|
+
[$._statement_declaration, $._statement_declaration_modifiers],
|
|
76
|
+
[$.declaration, $._declaration_modifiers],
|
|
77
|
+
[$._statement_declaration, $._statement_type_definition, $._statement_declaration_modifiers],
|
|
78
|
+
[$.declaration, $.type_definition, $._macro_declaration_specifiers, $._declaration_modifiers],
|
|
79
|
+
[$.type_definition, $.type_specifier],
|
|
80
|
+
[$.type_definition, $._macro_declaration_specifiers],
|
|
81
|
+
[$._macro_declaration_specifiers, $._declaration_modifiers, $.attributed_statement, $.case_statement],
|
|
82
|
+
[$._macro_declaration_specifiers, $.type_specifier, $.expression, $.macro_specifier],
|
|
83
|
+
[$._macro_declaration_specifiers, $.type_specifier, $.macro_specifier],
|
|
84
|
+
[$.macro_specifier, $._type_declarator],
|
|
85
|
+
[$.macro_specifier, $._field_declarator],
|
|
86
|
+
[$.macro_specifier, $._declarator],
|
|
87
|
+
[$.macro_specifier, $._declarator, $.attributed_declarator, $.type_specifier],
|
|
88
|
+
[$.macro_specifier, $._declarator, $.attributed_declarator],
|
|
89
|
+
[$.macro_specifier, $.sized_type_specifier],
|
|
90
|
+
[$.macro_specifier, $.type_specifier, $.sized_type_specifier],
|
|
91
|
+
[$.macro_specifier, $.expression],
|
|
92
|
+
[$.macro_specifier, $.type_specifier],
|
|
75
93
|
[$._macro_declaration_specifiers, $.type_definition, $.type_specifier],
|
|
76
94
|
[$._macro_declaration_specifiers, $.type_specifier, $.expression],
|
|
77
95
|
[$._macro_declaration_specifiers, $.type_specifier],
|
|
78
|
-
[$._macro_declaration_specifiers, $.type_definition, $._declaration_modifiers],
|
|
79
96
|
[$._macro_declaration_specifiers, $._declaration_modifiers, $._empty_declaration],
|
|
80
97
|
[$._macro_declaration_specifiers, $._declaration_modifiers],
|
|
81
98
|
[$.preproc_include_argument],
|
|
@@ -85,20 +102,25 @@ module.exports = Object.assign(
|
|
|
85
102
|
[$.comma_expression, $._initializer_element],
|
|
86
103
|
[$.comma_expression, $.initializer_pair],
|
|
87
104
|
[$._block_item, $._initializer_directive],
|
|
88
|
-
[sym('
|
|
89
|
-
[sym('
|
|
90
|
-
[sym('
|
|
91
|
-
[sym('
|
|
92
|
-
[sym('
|
|
105
|
+
[sym('preproc_if_in_statement'), sym('preproc_if_in_initializer_list')],
|
|
106
|
+
[sym('preproc_ifdef_in_statement'), sym('preproc_ifdef_in_initializer_list')],
|
|
107
|
+
[sym('preproc_else_in_statement'), sym('preproc_else_in_initializer_list')],
|
|
108
|
+
[sym('preproc_elif_in_statement'), sym('preproc_elif_in_initializer_list')],
|
|
109
|
+
[sym('preproc_elifdef_in_statement'), sym('preproc_elifdef_in_initializer_list')],
|
|
93
110
|
|
|
94
111
|
[$.type_specifier, $.expression, $.va_arg_expression, $.macro_type_specifier],
|
|
95
112
|
[$.va_arg_expression, $.expression],
|
|
96
113
|
[$.type_definition, $._declaration_modifiers],
|
|
97
|
-
[$.
|
|
114
|
+
[$._statement_type_definition, $._statement_declaration_modifiers],
|
|
115
|
+
[$.type_definition, $._declaration_modifiers, $._empty_declaration],
|
|
116
|
+
[$._statement_type_definition, $.type_specifier],
|
|
98
117
|
[$.type_definition, $.type_qualifier, $.extension_expression],
|
|
118
|
+
[$._statement_type_definition, $.type_qualifier, $.extension_expression],
|
|
99
119
|
[$.type_definition, $.type_qualifier],
|
|
100
|
-
[$.
|
|
120
|
+
[$._statement_type_definition, $.type_qualifier],
|
|
121
|
+
[$._statement_declaration_modifiers, $._statement_empty_declaration],
|
|
101
122
|
[$.declaration, $.storage_class_specifier],
|
|
123
|
+
[$._statement_declaration, $.storage_class_specifier],
|
|
102
124
|
[$.sized_type_specifier, $.enum_specifier],
|
|
103
125
|
[$.type_specifier, $._declarator],
|
|
104
126
|
[$.type_specifier, $._declarator, $.macro_type_specifier],
|
|
@@ -118,8 +140,8 @@ module.exports = Object.assign(
|
|
|
118
140
|
[$.attributed_statement],
|
|
119
141
|
[$.case_statement],
|
|
120
142
|
[$._single_attributed_statement],
|
|
121
|
-
[$.
|
|
122
|
-
[$.
|
|
143
|
+
[$._statement_declaration_modifiers, $._case_body_attributed_statement, $._case_label_attributes],
|
|
144
|
+
[$._statement_declaration_modifiers, $.attributed_statement, $.case_statement],
|
|
123
145
|
[$.enum_specifier],
|
|
124
146
|
[sym('preproc_if_in_enumerator_list'), sym('preproc_if_in_enumerator_list_no_comma')],
|
|
125
147
|
[sym('preproc_ifdef_in_enumerator_list'), sym('preproc_ifdef_in_enumerator_list_no_comma')],
|
|
@@ -130,6 +152,7 @@ module.exports = Object.assign(
|
|
|
130
152
|
[$.parameter_list, $._old_style_parameter_list],
|
|
131
153
|
[$.function_declarator, $._function_declaration_declarator],
|
|
132
154
|
[$.pointer_declarator],
|
|
155
|
+
[$.pointer_declarator, $.abstract_pointer_declarator],
|
|
133
156
|
[$.pointer_field_declarator],
|
|
134
157
|
[$.pointer_type_declarator],
|
|
135
158
|
[$.abstract_pointer_declarator],
|
|
@@ -146,6 +169,7 @@ module.exports = Object.assign(
|
|
|
146
169
|
[$.attributed_field_declarator, $._field_declarator],
|
|
147
170
|
[$.attributed_type_declarator],
|
|
148
171
|
[$.attributed_type_declarator, $._type_declarator],
|
|
172
|
+
[$._declaration_modifiers, $._field_declaration_list_item],
|
|
149
173
|
[$._declaration_specifiers],
|
|
150
174
|
[$._compound_literal_storage_class_specifier, $.type_specifier],
|
|
151
175
|
[$._compound_literal_constexpr_specifier, $.type_qualifier],
|
|
@@ -163,12 +187,17 @@ module.exports = Object.assign(
|
|
|
163
187
|
sym('_post_type_function_attribute_suffix'),
|
|
164
188
|
sym('_trailing_macro_attribute_name'),
|
|
165
189
|
sym('_trailing_object_declaration_start'),
|
|
190
|
+
sym('_conditional_marker_ifdef'),
|
|
191
|
+
sym('_conditional_marker_ifndef'),
|
|
192
|
+
sym('_leading_conditional_marker_ifdef'),
|
|
193
|
+
sym('_leading_conditional_marker_ifndef'),
|
|
166
194
|
],
|
|
167
195
|
|
|
168
196
|
extras: ($) => [$.pragma_operator, /\s|\\(?:\r\n?|\n\r?)/, $.comment],
|
|
169
197
|
|
|
170
198
|
inline: ($) => [
|
|
171
199
|
$._non_identifier_type_specifier,
|
|
200
|
+
$._non_macro_declaration_modifiers,
|
|
172
201
|
$._unattributed_declarator,
|
|
173
202
|
$._unattributed_field_declarator,
|
|
174
203
|
$._unattributed_type_declarator,
|
|
@@ -207,10 +236,13 @@ module.exports = Object.assign(
|
|
|
207
236
|
choice(
|
|
208
237
|
$.function_definition,
|
|
209
238
|
alias($._old_style_function_definition, $.function_definition),
|
|
239
|
+
alias(sym('_conditional_function_definition'), $.function_definition),
|
|
240
|
+
alias(sym('_conditional_old_style_function_definition'), $.function_definition),
|
|
210
241
|
$.linkage_specification,
|
|
211
242
|
$.declaration,
|
|
212
243
|
alias(sym('_macro_modified_declaration'), $.declaration),
|
|
213
244
|
alias(sym('_trailing_macro_modified_declaration'), $.declaration),
|
|
245
|
+
alias(sym('_conditional_declaration'), $.declaration),
|
|
214
246
|
$._top_level_statement,
|
|
215
247
|
$.attributed_statement,
|
|
216
248
|
$.type_definition,
|
|
@@ -223,9 +255,44 @@ module.exports = Object.assign(
|
|
|
223
255
|
$.preproc_call
|
|
224
256
|
),
|
|
225
257
|
|
|
226
|
-
_block_item: ($) =>
|
|
258
|
+
_block_item: ($) =>
|
|
259
|
+
choice(
|
|
260
|
+
alias($._statement_function_definition, $.function_definition),
|
|
261
|
+
alias($._statement_old_style_function_definition, $.function_definition),
|
|
262
|
+
$.linkage_specification,
|
|
263
|
+
alias($._statement_declaration, $.declaration),
|
|
264
|
+
$.statement,
|
|
265
|
+
alias($._statement_type_definition, $.type_definition),
|
|
266
|
+
$._statement_empty_declaration,
|
|
267
|
+
alias(sym('preproc_if_in_statement'), sym('preproc_if')),
|
|
268
|
+
alias(sym('preproc_ifdef_in_statement'), sym('preproc_ifdef')),
|
|
269
|
+
$.preproc_include,
|
|
270
|
+
$.preproc_def,
|
|
271
|
+
$.preproc_function_def,
|
|
272
|
+
$.preproc_call
|
|
273
|
+
),
|
|
227
274
|
|
|
228
|
-
_header_preproc_item: ($) =>
|
|
275
|
+
_header_preproc_item: ($) =>
|
|
276
|
+
choice(
|
|
277
|
+
$.function_definition,
|
|
278
|
+
alias($._old_style_function_definition, $.function_definition),
|
|
279
|
+
alias(sym('_conditional_function_definition'), $.function_definition),
|
|
280
|
+
alias(sym('_conditional_old_style_function_definition'), $.function_definition),
|
|
281
|
+
$.linkage_specification,
|
|
282
|
+
$.declaration,
|
|
283
|
+
alias(sym('_macro_modified_declaration'), $.declaration),
|
|
284
|
+
alias(sym('_trailing_macro_modified_declaration'), $.declaration),
|
|
285
|
+
alias(sym('_conditional_declaration'), $.declaration),
|
|
286
|
+
$.statement,
|
|
287
|
+
$.type_definition,
|
|
288
|
+
$._empty_declaration,
|
|
289
|
+
alias(sym('preproc_if_in_header'), sym('preproc_if')),
|
|
290
|
+
alias(sym('preproc_ifdef_in_header'), sym('preproc_ifdef')),
|
|
291
|
+
$.preproc_include,
|
|
292
|
+
$.preproc_def,
|
|
293
|
+
$.preproc_function_def,
|
|
294
|
+
$.preproc_call
|
|
295
|
+
),
|
|
229
296
|
|
|
230
297
|
// Derived grammars that replace externals still need this lexical fallback. Comment-rich spacing makes the
|
|
231
298
|
// lexer too large for the pinned Node Wasm optimizer; the external scanner handles those forms.
|
|
@@ -318,8 +385,8 @@ module.exports = Object.assign(
|
|
|
318
385
|
),
|
|
319
386
|
|
|
320
387
|
...preprocIf('_in_header', () => sym('_header_preproc_item')),
|
|
321
|
-
|
|
322
388
|
...preprocIf('', () => sym('_block_item')),
|
|
389
|
+
...preprocIf('_in_statement', () => sym('_block_item')),
|
|
323
390
|
...preprocIf('_in_single_case', () => sym('_single_case_body'), 0, true),
|
|
324
391
|
...preprocIf('_in_field_declaration_list', () => sym('_field_declaration_list_item')),
|
|
325
392
|
...preprocIf('_in_enumerator_list', () => choice(seq(sym('enumerator'), ','), sym('_enumerator_directives'))),
|
|
@@ -392,23 +459,11 @@ module.exports = Object.assign(
|
|
|
392
459
|
);
|
|
393
460
|
},
|
|
394
461
|
|
|
395
|
-
function_definition: ($) =>
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
field('declarator', $._declarator),
|
|
401
|
-
field('body', $.compound_statement)
|
|
402
|
-
),
|
|
403
|
-
|
|
404
|
-
_old_style_function_definition: ($) =>
|
|
405
|
-
seq(
|
|
406
|
-
optional($.ms_call_modifier),
|
|
407
|
-
$._declaration_specifiers,
|
|
408
|
-
field('declarator', alias($._old_style_declarator, $._declarator)),
|
|
409
|
-
repeat1($.declaration),
|
|
410
|
-
field('body', $.compound_statement)
|
|
411
|
-
),
|
|
462
|
+
function_definition: ($) => functionDefinitionRule($, $._declaration_specifiers, false),
|
|
463
|
+
_statement_function_definition: ($) => functionDefinitionRule($, $._statement_declaration_specifiers, false),
|
|
464
|
+
_old_style_function_definition: ($) => functionDefinitionRule($, $._declaration_specifiers, true),
|
|
465
|
+
_statement_old_style_function_definition: ($) =>
|
|
466
|
+
functionDefinitionRule($, $._statement_declaration_specifiers, true),
|
|
412
467
|
|
|
413
468
|
_old_style_declarator: ($) =>
|
|
414
469
|
choice(
|
|
@@ -426,16 +481,45 @@ module.exports = Object.assign(
|
|
|
426
481
|
'*',
|
|
427
482
|
repeat($.attribute_declaration),
|
|
428
483
|
repeat($.ms_pointer_modifier),
|
|
429
|
-
repeat(
|
|
484
|
+
repeat(
|
|
485
|
+
choice($.type_qualifier, $.attribute_specifier, $.macro_specifier, $._conditional_macro_specifier)
|
|
486
|
+
),
|
|
430
487
|
field('declarator', alias($._old_style_declarator, $._declarator))
|
|
431
488
|
)
|
|
432
489
|
)
|
|
433
490
|
),
|
|
434
491
|
|
|
435
|
-
|
|
492
|
+
_conditional_function_definition: ($) =>
|
|
493
|
+
functionDefinitionRule($, sym('_conditional_function_specifiers'), false),
|
|
494
|
+
_conditional_old_style_function_definition: ($) =>
|
|
495
|
+
functionDefinitionRule($, sym('_conditional_function_specifiers'), true),
|
|
496
|
+
_conditional_function_specifiers: ($) =>
|
|
497
|
+
seq(
|
|
498
|
+
repeat($._declaration_modifiers),
|
|
499
|
+
field('type', $.type_specifier),
|
|
500
|
+
repeat($._non_macro_declaration_modifiers),
|
|
501
|
+
$._conditional_macro_specifier,
|
|
502
|
+
repeat(choice($._non_macro_declaration_modifiers, $._conditional_macro_specifier))
|
|
503
|
+
),
|
|
504
|
+
|
|
505
|
+
declaration: ($) => declarationRule($, $._declaration_specifiers, $._declaration_modifiers),
|
|
506
|
+
_statement_declaration: ($) =>
|
|
507
|
+
declarationRule($, $._statement_declaration_specifiers, $._statement_declaration_modifiers),
|
|
508
|
+
|
|
509
|
+
type_definition: ($) => typeDefinitionRule($, $._declaration_modifiers, true),
|
|
510
|
+
_statement_type_definition: ($) => typeDefinitionRule($, $._statement_declaration_modifiers, false),
|
|
436
511
|
|
|
437
512
|
_macro_modified_declaration: ($) =>
|
|
438
|
-
declarationRule(
|
|
513
|
+
declarationRule(
|
|
514
|
+
$,
|
|
515
|
+
$._macro_declaration_specifiers,
|
|
516
|
+
$._non_macro_declaration_modifiers,
|
|
517
|
+
false,
|
|
518
|
+
$._macro_declaration_declarator
|
|
519
|
+
),
|
|
520
|
+
|
|
521
|
+
_conditional_declaration: ($) =>
|
|
522
|
+
declarationRule($, sym('_conditional_function_specifiers'), $._non_macro_declaration_modifiers, false),
|
|
439
523
|
|
|
440
524
|
_trailing_macro_modified_declaration: ($) =>
|
|
441
525
|
seq(
|
|
@@ -452,52 +536,47 @@ module.exports = Object.assign(
|
|
|
452
536
|
_trailing_macro_pointer_declarator: ($) => pointerDeclaratorRule($, $._trailing_macro_object_declarator),
|
|
453
537
|
|
|
454
538
|
_macro_declaration_specifiers: ($) => {
|
|
455
|
-
const modifier = choice(
|
|
539
|
+
const modifier = choice(
|
|
540
|
+
$._non_macro_declaration_modifiers,
|
|
541
|
+
alias($._macro_attribute_suffix, $.attribute_specifier)
|
|
542
|
+
);
|
|
456
543
|
const postModifier = choice(
|
|
457
|
-
$.
|
|
544
|
+
$._non_macro_declaration_modifiers,
|
|
458
545
|
alias($._post_type_macro_attribute_suffix, $.attribute_specifier)
|
|
459
546
|
);
|
|
460
547
|
return choice(
|
|
461
548
|
seq(
|
|
462
549
|
choice($.storage_class_specifier, $.type_qualifier, $.ms_declspec_modifier),
|
|
463
|
-
repeat($.
|
|
550
|
+
repeat($._non_macro_declaration_modifiers),
|
|
464
551
|
alias($._macro_attribute_specifier, $.attribute_specifier),
|
|
465
552
|
repeat(modifier),
|
|
466
553
|
field('type', $._non_identifier_type_specifier),
|
|
467
554
|
repeat(postModifier)
|
|
468
555
|
),
|
|
469
556
|
seq(
|
|
470
|
-
repeat($.
|
|
557
|
+
repeat($._non_macro_declaration_modifiers),
|
|
471
558
|
field('type', choice($._non_identifier_type_specifier, $._type_identifier)),
|
|
472
|
-
repeat($.
|
|
559
|
+
repeat($._non_macro_declaration_modifiers),
|
|
473
560
|
alias($._post_type_macro_attribute_specifier, $.attribute_specifier),
|
|
474
561
|
repeat(postModifier)
|
|
475
562
|
)
|
|
476
563
|
);
|
|
477
564
|
},
|
|
478
565
|
|
|
479
|
-
|
|
566
|
+
_type_definition_type: ($) =>
|
|
480
567
|
seq(
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
seq(
|
|
485
|
-
repeat($._declaration_modifiers),
|
|
486
|
-
field('type', $._non_identifier_type_specifier),
|
|
487
|
-
repeat($.type_qualifier),
|
|
488
|
-
'typedef',
|
|
489
|
-
repeat(choice($.type_qualifier, $._non_identifier_type_specifier))
|
|
490
|
-
)
|
|
491
|
-
),
|
|
492
|
-
$._type_definition_declarators,
|
|
493
|
-
repeat($.attribute_specifier),
|
|
494
|
-
';'
|
|
568
|
+
repeat(choice($.type_qualifier, $.macro_specifier, $._leading_conditional_macro_specifier)),
|
|
569
|
+
field('type', $.type_specifier),
|
|
570
|
+
repeat($.type_qualifier)
|
|
495
571
|
),
|
|
496
|
-
_type_definition_type: ($) =>
|
|
497
|
-
seq(repeat($.type_qualifier), field('type', $.type_specifier), repeat($.type_qualifier)),
|
|
498
572
|
_type_definition_declarators: ($) => commaSep1(field('declarator', $._type_declarator)),
|
|
499
573
|
|
|
500
574
|
_declaration_modifiers: ($) =>
|
|
575
|
+
choice($._non_macro_declaration_modifiers, $.macro_specifier, $._leading_conditional_macro_specifier),
|
|
576
|
+
|
|
577
|
+
_statement_declaration_modifiers: ($) => $._non_macro_declaration_modifiers,
|
|
578
|
+
|
|
579
|
+
_non_macro_declaration_modifiers: ($) =>
|
|
501
580
|
choice(
|
|
502
581
|
$.storage_class_specifier,
|
|
503
582
|
$.type_qualifier,
|
|
@@ -507,7 +586,18 @@ module.exports = Object.assign(
|
|
|
507
586
|
),
|
|
508
587
|
|
|
509
588
|
_declaration_specifiers: ($) =>
|
|
510
|
-
seq(
|
|
589
|
+
seq(
|
|
590
|
+
repeat($._declaration_modifiers),
|
|
591
|
+
field('type', $.type_specifier),
|
|
592
|
+
repeat($._non_macro_declaration_modifiers)
|
|
593
|
+
),
|
|
594
|
+
|
|
595
|
+
_statement_declaration_specifiers: ($) =>
|
|
596
|
+
seq(
|
|
597
|
+
repeat($._statement_declaration_modifiers),
|
|
598
|
+
field('type', $.type_specifier),
|
|
599
|
+
repeat($._statement_declaration_modifiers)
|
|
600
|
+
),
|
|
511
601
|
|
|
512
602
|
linkage_specification: ($) =>
|
|
513
603
|
seq(
|
|
@@ -563,7 +653,7 @@ module.exports = Object.assign(
|
|
|
563
653
|
$.ms_pointer_size_modifier
|
|
564
654
|
),
|
|
565
655
|
|
|
566
|
-
declaration_list: ($) => seq('{', repeat($.
|
|
656
|
+
declaration_list: ($) => seq('{', repeat($._top_level_item), '}'),
|
|
567
657
|
|
|
568
658
|
_declarator: ($) => choice($.attributed_declarator, $._unattributed_declarator),
|
|
569
659
|
|
|
@@ -640,7 +730,9 @@ module.exports = Object.assign(
|
|
|
640
730
|
'*',
|
|
641
731
|
repeat($.attribute_declaration),
|
|
642
732
|
repeat($.ms_pointer_modifier),
|
|
643
|
-
repeat(
|
|
733
|
+
repeat(
|
|
734
|
+
choice($.type_qualifier, $.attribute_specifier, $.macro_specifier, $._conditional_macro_specifier)
|
|
735
|
+
),
|
|
644
736
|
field('declarator', $._field_declarator)
|
|
645
737
|
)
|
|
646
738
|
)
|
|
@@ -655,7 +747,9 @@ module.exports = Object.assign(
|
|
|
655
747
|
'*',
|
|
656
748
|
repeat($.attribute_declaration),
|
|
657
749
|
repeat($.ms_pointer_modifier),
|
|
658
|
-
repeat(
|
|
750
|
+
repeat(
|
|
751
|
+
choice($.type_qualifier, $.attribute_specifier, $.macro_specifier, $._conditional_macro_specifier)
|
|
752
|
+
),
|
|
659
753
|
field('declarator', $._type_declarator)
|
|
660
754
|
)
|
|
661
755
|
)
|
|
@@ -825,7 +919,7 @@ module.exports = Object.assign(
|
|
|
825
919
|
bit_int_specifier: ($) => seq('_BitInt', '(', $.expression, ')'),
|
|
826
920
|
|
|
827
921
|
sized_type_specifier: ($) =>
|
|
828
|
-
sizedTypeSpecifier(optional(choice(prec.dynamic(-
|
|
922
|
+
sizedTypeSpecifier(optional(choice(prec.dynamic(-3, $._type_identifier), $.primitive_type))),
|
|
829
923
|
|
|
830
924
|
_sized_bit_int_specifier: ($) => sizedTypeSpecifier($.bit_int_specifier),
|
|
831
925
|
|
|
@@ -937,6 +1031,7 @@ module.exports = Object.assign(
|
|
|
937
1031
|
_field_declaration_list_item: ($) =>
|
|
938
1032
|
choice(
|
|
939
1033
|
$.field_declaration,
|
|
1034
|
+
prec.dynamic(-1, $.macro_specifier),
|
|
940
1035
|
$.preproc_def,
|
|
941
1036
|
$.preproc_function_def,
|
|
942
1037
|
$.preproc_call,
|
|
@@ -1010,7 +1105,12 @@ module.exports = Object.assign(
|
|
|
1010
1105
|
$.goto_statement
|
|
1011
1106
|
),
|
|
1012
1107
|
|
|
1013
|
-
labeled_statement: ($) =>
|
|
1108
|
+
labeled_statement: ($) =>
|
|
1109
|
+
seq(
|
|
1110
|
+
field('label', $._statement_identifier),
|
|
1111
|
+
':',
|
|
1112
|
+
choice(alias($._statement_declaration, $.declaration), $.statement)
|
|
1113
|
+
),
|
|
1014
1114
|
|
|
1015
1115
|
// This is missing binary expressions, others were kept so that macro code can be parsed better and code examples
|
|
1016
1116
|
_top_level_expression_statement: ($) => seq(optional($._expression_not_binary), ';'),
|
|
@@ -1075,14 +1175,18 @@ module.exports = Object.assign(
|
|
|
1075
1175
|
1,
|
|
1076
1176
|
choice(
|
|
1077
1177
|
$._single_statement,
|
|
1078
|
-
$.declaration,
|
|
1079
|
-
$.type_definition,
|
|
1178
|
+
alias($._statement_declaration, $.declaration),
|
|
1179
|
+
alias($._statement_type_definition, $.type_definition),
|
|
1080
1180
|
alias(sym('preproc_if_in_single_case'), sym('preproc_if')),
|
|
1081
1181
|
alias(sym('preproc_ifdef_in_single_case'), sym('preproc_ifdef'))
|
|
1082
1182
|
)
|
|
1083
1183
|
),
|
|
1084
1184
|
_single_labeled_statement: ($) =>
|
|
1085
|
-
seq(
|
|
1185
|
+
seq(
|
|
1186
|
+
field('label', $._statement_identifier),
|
|
1187
|
+
':',
|
|
1188
|
+
choice($._single_statement, alias($._statement_declaration, $.declaration))
|
|
1189
|
+
),
|
|
1086
1190
|
_single_attributed_statement: ($) =>
|
|
1087
1191
|
seq(
|
|
1088
1192
|
repeat1($.attribute_declaration),
|
|
@@ -1105,7 +1209,14 @@ module.exports = Object.assign(
|
|
|
1105
1209
|
),
|
|
1106
1210
|
|
|
1107
1211
|
_case_statement_item: ($) =>
|
|
1108
|
-
prec.dynamic(
|
|
1212
|
+
prec.dynamic(
|
|
1213
|
+
1,
|
|
1214
|
+
choice(
|
|
1215
|
+
alias($._case_body_statement, $.statement),
|
|
1216
|
+
alias($._statement_declaration, $.declaration),
|
|
1217
|
+
alias($._statement_type_definition, $.type_definition)
|
|
1218
|
+
)
|
|
1219
|
+
),
|
|
1109
1220
|
|
|
1110
1221
|
_case_body_statement: ($) =>
|
|
1111
1222
|
choice(
|
|
@@ -1118,7 +1229,7 @@ module.exports = Object.assign(
|
|
|
1118
1229
|
seq(
|
|
1119
1230
|
field('label', $._statement_identifier),
|
|
1120
1231
|
':',
|
|
1121
|
-
choice($.declaration, alias($._case_body_statement, $.statement))
|
|
1232
|
+
choice(alias($._statement_declaration, $.declaration), alias($._case_body_statement, $.statement))
|
|
1122
1233
|
),
|
|
1123
1234
|
_case_body_attributed_statement: ($) =>
|
|
1124
1235
|
prec(1, seq(repeat1($.attribute_declaration), alias($._case_body_unattributed_statement, $.statement))),
|
|
@@ -1147,7 +1258,7 @@ module.exports = Object.assign(
|
|
|
1147
1258
|
_for_statement_body: ($) =>
|
|
1148
1259
|
seq(
|
|
1149
1260
|
choice(
|
|
1150
|
-
field('initializer', $.declaration),
|
|
1261
|
+
field('initializer', alias($._statement_declaration, $.declaration)),
|
|
1151
1262
|
seq(field('initializer', optional(choice($.expression, $.comma_expression))), ';')
|
|
1152
1263
|
),
|
|
1153
1264
|
field('condition', optional(choice($.expression, $.comma_expression))),
|
|
@@ -1603,7 +1714,23 @@ module.exports = Object.assign(
|
|
|
1603
1714
|
_field_identifier: ($) => alias(choice($.identifier, VA_ARG_KEYWORDS), sym('field_identifier')),
|
|
1604
1715
|
_statement_identifier: ($) => alias(choice($.identifier, VA_ARG_KEYWORDS), sym('statement_identifier')),
|
|
1605
1716
|
|
|
1606
|
-
_empty_declaration: ($) =>
|
|
1717
|
+
_empty_declaration: ($) =>
|
|
1718
|
+
seq(
|
|
1719
|
+
repeat(choice($.ms_declspec_modifier, $.macro_specifier, $._leading_conditional_macro_specifier)),
|
|
1720
|
+
$.type_specifier,
|
|
1721
|
+
';'
|
|
1722
|
+
),
|
|
1723
|
+
_statement_empty_declaration: ($) => seq(repeat($.ms_declspec_modifier), $.type_specifier, ';'),
|
|
1724
|
+
|
|
1725
|
+
macro_specifier: ($) => prec.dynamic(-2, field('name', $.identifier)),
|
|
1726
|
+
|
|
1727
|
+
_conditional_macro_specifier: () => alias(sym('_marker_preproc_ifdef'), sym('preproc_ifdef')),
|
|
1728
|
+
_leading_conditional_macro_specifier: () => alias(sym('_leading_marker_preproc_ifdef'), sym('preproc_ifdef')),
|
|
1729
|
+
|
|
1730
|
+
_marker_preproc_ifdef: ($) =>
|
|
1731
|
+
conditionalMacroRule($, sym('_conditional_marker_ifdef'), sym('_conditional_marker_ifndef')),
|
|
1732
|
+
_leading_marker_preproc_ifdef: ($) =>
|
|
1733
|
+
conditionalMacroRule($, sym('_leading_conditional_marker_ifdef'), sym('_leading_conditional_marker_ifndef')),
|
|
1607
1734
|
|
|
1608
1735
|
macro_type_specifier: ($) =>
|
|
1609
1736
|
prec.dynamic(
|
|
@@ -1626,53 +1753,110 @@ module.exports = Object.assign(
|
|
|
1626
1753
|
);
|
|
1627
1754
|
|
|
1628
1755
|
/**
|
|
1629
|
-
* @param {import('./types/treeSitterDsl').GrammarSymbols<'
|
|
1756
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'identifier' | 'macro_specifier'>} $
|
|
1757
|
+
* @param {import('./types/treeSitterDsl').Rule} ifdef
|
|
1758
|
+
* @param {import('./types/treeSitterDsl').Rule} ifndef
|
|
1759
|
+
*/
|
|
1760
|
+
function conditionalMacroRule($, ifdef, ifndef) {
|
|
1761
|
+
return seq(
|
|
1762
|
+
choice(alias(ifdef, '#ifdef'), alias(ifndef, '#ifndef')),
|
|
1763
|
+
field('name', $.identifier),
|
|
1764
|
+
$.macro_specifier,
|
|
1765
|
+
preprocessor('endif')
|
|
1766
|
+
);
|
|
1767
|
+
}
|
|
1768
|
+
|
|
1769
|
+
/**
|
|
1770
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'ms_based_modifier' | 'ms_call_modifier' | 'attribute_declaration' | 'ms_pointer_modifier' | 'type_qualifier' | 'attribute_specifier' | 'macro_specifier' | '_conditional_macro_specifier'>} $
|
|
1630
1771
|
* @param {import('./types/treeSitterDsl').Rule} declarator
|
|
1631
1772
|
*/
|
|
1632
1773
|
function pointerDeclaratorRule($, declarator) {
|
|
1633
|
-
return prec.
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
repeat(choice($.type_qualifier, $.attribute_specifier)),
|
|
1643
|
-
field('declarator', declarator)
|
|
1644
|
-
)
|
|
1774
|
+
return prec.right(
|
|
1775
|
+
seq(
|
|
1776
|
+
optional($.ms_based_modifier),
|
|
1777
|
+
optional(prec.dynamic(2, $.ms_call_modifier)),
|
|
1778
|
+
'*',
|
|
1779
|
+
repeat($.attribute_declaration),
|
|
1780
|
+
repeat($.ms_pointer_modifier),
|
|
1781
|
+
repeat(choice($.type_qualifier, $.attribute_specifier, $.macro_specifier, $._conditional_macro_specifier)),
|
|
1782
|
+
field('declarator', declarator)
|
|
1645
1783
|
)
|
|
1646
1784
|
);
|
|
1647
1785
|
}
|
|
1648
1786
|
|
|
1649
1787
|
/**
|
|
1650
|
-
* @param {import('./types/treeSitterDsl').GrammarSymbols<'
|
|
1788
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'ms_call_modifier' | '_old_style_declarator' | '_declarator' | 'declaration' | 'compound_statement'>} $
|
|
1651
1789
|
* @param {import('./types/treeSitterDsl').Rule} specifiers
|
|
1652
|
-
* @param {boolean}
|
|
1653
|
-
* @param {import('./types/treeSitterDsl').Rule} declarator
|
|
1790
|
+
* @param {boolean} oldStyle
|
|
1654
1791
|
*/
|
|
1655
|
-
function
|
|
1792
|
+
function functionDefinitionRule($, specifiers, oldStyle) {
|
|
1656
1793
|
return seq(
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1794
|
+
optional($.ms_call_modifier),
|
|
1795
|
+
specifiers,
|
|
1796
|
+
...(oldStyle ? [] : [optional($.ms_call_modifier)]),
|
|
1797
|
+
field('declarator', oldStyle ? alias($._old_style_declarator, $._declarator) : $._declarator),
|
|
1798
|
+
...(oldStyle ? [repeat1($.declaration)] : []),
|
|
1799
|
+
field('body', $.compound_statement)
|
|
1800
|
+
);
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1803
|
+
/**
|
|
1804
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'storage_class_specifier' | 'ms_call_modifier' | '_declaration_declarator' | 'gnu_asm_expression' | 'init_declarator' | 'type_qualifier' | 'identifier'>} $
|
|
1805
|
+
* @param {import('./types/treeSitterDsl').Rule} specifiers
|
|
1806
|
+
* @param {import('./types/treeSitterDsl').Rule} modifiers
|
|
1807
|
+
* @param {boolean} allowInferredType
|
|
1808
|
+
* @param {import('./types/treeSitterDsl').Rule} declarator
|
|
1809
|
+
*/
|
|
1810
|
+
function declarationRule($, specifiers, modifiers, allowInferredType = true, declarator = $._declaration_declarator) {
|
|
1811
|
+
return choice(
|
|
1812
|
+
seq(
|
|
1813
|
+
prec.dynamic(
|
|
1814
|
+
1,
|
|
1815
|
+
allowInferredType
|
|
1816
|
+
? choice(
|
|
1817
|
+
specifiers,
|
|
1818
|
+
prec.dynamic(
|
|
1819
|
+
PREC.PAREN_DECLARATOR - 1,
|
|
1820
|
+
seq(repeat(modifiers), alias('auto', $.storage_class_specifier), repeat(modifiers))
|
|
1821
|
+
)
|
|
1666
1822
|
)
|
|
1667
|
-
|
|
1823
|
+
: specifiers
|
|
1824
|
+
),
|
|
1825
|
+
commaSep1(
|
|
1826
|
+
field(
|
|
1827
|
+
'declarator',
|
|
1828
|
+
choice(seq(optional($.ms_call_modifier), declarator, optional($.gnu_asm_expression)), $.init_declarator)
|
|
1668
1829
|
)
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1830
|
+
),
|
|
1831
|
+
';'
|
|
1832
|
+
),
|
|
1833
|
+
...(allowInferredType
|
|
1834
|
+
? [seq(repeat1($.type_qualifier), commaSep1(field('declarator', choice($.identifier, $.init_declarator))), ';')]
|
|
1835
|
+
: [])
|
|
1836
|
+
);
|
|
1837
|
+
}
|
|
1838
|
+
|
|
1839
|
+
/**
|
|
1840
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'macro_specifier' | '_leading_conditional_macro_specifier' | 'type_qualifier' | '_type_definition_type' | '_non_identifier_type_specifier' | '_type_definition_declarators' | 'attribute_specifier'>} $
|
|
1841
|
+
* @param {import('./types/treeSitterDsl').Rule} modifiers
|
|
1842
|
+
* @param {boolean} allowPrefixMarkers
|
|
1843
|
+
*/
|
|
1844
|
+
function typeDefinitionRule($, modifiers, allowPrefixMarkers) {
|
|
1845
|
+
return seq(
|
|
1846
|
+
optional(prec.dynamic(1, '__extension__')),
|
|
1847
|
+
...(allowPrefixMarkers ? [repeat(choice($.macro_specifier, $._leading_conditional_macro_specifier))] : []),
|
|
1848
|
+
choice(
|
|
1849
|
+
seq(repeat($.type_qualifier), 'typedef', $._type_definition_type),
|
|
1850
|
+
seq(
|
|
1851
|
+
repeat(modifiers),
|
|
1852
|
+
field('type', $._non_identifier_type_specifier),
|
|
1853
|
+
repeat($.type_qualifier),
|
|
1854
|
+
'typedef',
|
|
1855
|
+
repeat(choice($.type_qualifier, $._non_identifier_type_specifier))
|
|
1674
1856
|
)
|
|
1675
1857
|
),
|
|
1858
|
+
$._type_definition_declarators,
|
|
1859
|
+
repeat($.attribute_specifier),
|
|
1676
1860
|
';'
|
|
1677
1861
|
);
|
|
1678
1862
|
}
|
|
@@ -1693,34 +1877,6 @@ function declarationDeclaratorRule($, functionDeclarator) {
|
|
|
1693
1877
|
);
|
|
1694
1878
|
}
|
|
1695
1879
|
|
|
1696
|
-
/**
|
|
1697
|
-
* @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'>} $
|
|
1698
|
-
* @param {boolean} header
|
|
1699
|
-
*/
|
|
1700
|
-
function blockItemRule($, header) {
|
|
1701
|
-
return choice(
|
|
1702
|
-
$.function_definition,
|
|
1703
|
-
alias($._old_style_function_definition, $.function_definition),
|
|
1704
|
-
$.linkage_specification,
|
|
1705
|
-
$.declaration,
|
|
1706
|
-
...(header
|
|
1707
|
-
? [
|
|
1708
|
-
alias($._macro_modified_declaration, $.declaration),
|
|
1709
|
-
alias(sym('_trailing_macro_modified_declaration'), $.declaration),
|
|
1710
|
-
]
|
|
1711
|
-
: []),
|
|
1712
|
-
$.statement,
|
|
1713
|
-
$.type_definition,
|
|
1714
|
-
$._empty_declaration,
|
|
1715
|
-
header ? alias(sym('preproc_if_in_header'), sym('preproc_if')) : sym('preproc_if'),
|
|
1716
|
-
header ? alias(sym('preproc_ifdef_in_header'), sym('preproc_ifdef')) : sym('preproc_ifdef'),
|
|
1717
|
-
$.preproc_include,
|
|
1718
|
-
$.preproc_def,
|
|
1719
|
-
$.preproc_function_def,
|
|
1720
|
-
$.preproc_call
|
|
1721
|
-
);
|
|
1722
|
-
}
|
|
1723
|
-
|
|
1724
1880
|
/**
|
|
1725
1881
|
* @param {import('./types/treeSitterDsl').GrammarSymbols<'identifier' | 'argument_list' | '_macro_attribute_argument_list'>} $
|
|
1726
1882
|
* @param {'leading' | 'post-type' | 'continuation' | 'post-type-continuation' | 'trailing'} context
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/tree-sitter-c",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "13.0.0",
|
|
4
4
|
"description": "C grammar for tree-sitter",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"incremental",
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"src/identifier.h",
|
|
27
27
|
"src/keywords.h",
|
|
28
28
|
"src/functionMacro.h",
|
|
29
|
+
"src/conditionalMacro.h",
|
|
29
30
|
"src/scanner.c"
|
|
30
31
|
],
|
|
31
32
|
"scripts": {
|
|
@@ -49,16 +50,16 @@
|
|
|
49
50
|
},
|
|
50
51
|
"devDependencies": {
|
|
51
52
|
"@tsconfig/bun": "1.0.11",
|
|
52
|
-
"@types/bun": "1.4.
|
|
53
|
+
"@types/bun": "1.4.3",
|
|
53
54
|
"@vitest/browser-playwright": "5.0.3",
|
|
54
55
|
"@willbooster/oxfmt-config": "1.2.2",
|
|
55
56
|
"@willbooster/oxlint-config": "1.4.8",
|
|
56
|
-
"@willbooster/wb": "22.26.
|
|
57
|
+
"@willbooster/wb": "22.26.2",
|
|
57
58
|
"@willbooster/web-tree-sitter": "7.0.5",
|
|
58
59
|
"conventional-changelog-conventionalcommits": "10.4.1",
|
|
59
|
-
"lefthook": "2.2.
|
|
60
|
-
"oxfmt": "0.
|
|
61
|
-
"oxlint": "1.
|
|
60
|
+
"lefthook": "2.2.2",
|
|
61
|
+
"oxfmt": "0.73.0",
|
|
62
|
+
"oxlint": "1.88.0",
|
|
62
63
|
"oxlint-tsgolint": "7.0.2004",
|
|
63
64
|
"playwright": "1.64.0",
|
|
64
65
|
"semantic-release": "25.0.9",
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
#ifndef TREE_SITTER_C_CONDITIONAL_MACRO_H_
|
|
2
|
+
#define TREE_SITTER_C_CONDITIONAL_MACRO_H_
|
|
3
|
+
|
|
4
|
+
#include "functionMacro.h"
|
|
5
|
+
|
|
6
|
+
static bool conditional_macro_body(TSLexer *lexer, bool *escaped);
|
|
7
|
+
static bool conditional_macro_word(TSLexer *lexer, MacroName *name, bool escaped);
|
|
8
|
+
static bool conditional_macro_spacing(TSLexer *lexer, bool *separated, bool *escaped);
|
|
9
|
+
static bool conditional_macro_next_line(TSLexer *lexer, bool *escaped);
|
|
10
|
+
static bool conditional_macro_directive(TSLexer *lexer, MacroName *name);
|
|
11
|
+
static bool conditional_macro_marker_line(TSLexer *lexer, bool *escaped);
|
|
12
|
+
static bool conditional_macro_closing(TSLexer *lexer);
|
|
13
|
+
static bool conditional_macro_type_prefix(TSLexer *lexer, bool escaped);
|
|
14
|
+
static bool conditional_macro_skip_lines(TSLexer *lexer, bool *escaped);
|
|
15
|
+
static bool conditional_macro_modifier_group(TSLexer *lexer, const char *word, bool *escaped);
|
|
16
|
+
static bool conditional_macro_attribute_declaration(TSLexer *lexer, bool *escaped);
|
|
17
|
+
|
|
18
|
+
typedef struct {
|
|
19
|
+
TSSymbol ifdef, ifndef, leading_ifdef, leading_ifndef;
|
|
20
|
+
} ConditionalMacroSymbols;
|
|
21
|
+
|
|
22
|
+
static bool scan_conditional_macro(TSLexer *lexer, const ConditionalMacroSymbols *symbols, const bool *valid_symbols) {
|
|
23
|
+
char directive[7] = {0};
|
|
24
|
+
MacroName command = {.word = directive, .capacity = sizeof(directive) - 1, .ascii = true};
|
|
25
|
+
if (!conditional_macro_directive(lexer, &command) || !command.ascii) return false;
|
|
26
|
+
bool ifdef = strcmp(directive, "ifdef") == 0;
|
|
27
|
+
if (!ifdef && strcmp(directive, "ifndef") != 0) return false;
|
|
28
|
+
TSSymbol leading_symbol = ifdef ? symbols->leading_ifdef : symbols->leading_ifndef;
|
|
29
|
+
TSSymbol generic_symbol = ifdef ? symbols->ifdef : symbols->ifndef;
|
|
30
|
+
bool leading = valid_symbols[leading_symbol];
|
|
31
|
+
bool generic = valid_symbols[generic_symbol];
|
|
32
|
+
if (!leading && !generic) return false;
|
|
33
|
+
lexer->mark_end(lexer);
|
|
34
|
+
|
|
35
|
+
bool escaped = false;
|
|
36
|
+
if (!conditional_macro_body(lexer, &escaped)) return false;
|
|
37
|
+
if (leading && conditional_macro_type_prefix(lexer, escaped)) {
|
|
38
|
+
lexer->result_symbol = leading_symbol;
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
lexer->result_symbol = generic_symbol;
|
|
42
|
+
return generic;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static bool conditional_macro_body(TSLexer *lexer, bool *escaped) {
|
|
46
|
+
bool separated = false;
|
|
47
|
+
MacroName condition = {.ascii = true};
|
|
48
|
+
if (!conditional_macro_spacing(lexer, &separated, escaped) || !separated ||
|
|
49
|
+
!conditional_macro_word(lexer, &condition, *escaped) || !conditional_macro_next_line(lexer, escaped)) return false;
|
|
50
|
+
if (!conditional_macro_marker_line(lexer, escaped) || !conditional_macro_closing(lexer)) return false;
|
|
51
|
+
return conditional_macro_skip_lines(lexer, escaped) && !lexer->eof(lexer) && lexer->lookahead != '#';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static bool conditional_macro_closing(TSLexer *lexer) {
|
|
55
|
+
char directive[7] = {0};
|
|
56
|
+
MacroName command = {.word = directive, .capacity = sizeof(directive) - 1, .ascii = true};
|
|
57
|
+
if (!conditional_macro_directive(lexer, &command) || !command.ascii || strcmp(directive, "endif") != 0)
|
|
58
|
+
return false;
|
|
59
|
+
bool separated = false;
|
|
60
|
+
bool escaped = false;
|
|
61
|
+
if (!conditional_macro_spacing(lexer, &separated, &escaped)) return false;
|
|
62
|
+
return !escaped && (lexer->eof(lexer) || lexer->lookahead == '\n' || lexer->lookahead == '\r');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
typedef enum {
|
|
67
|
+
CONDITIONAL_TYPE, CONDITIONAL_MODIFIER, CONDITIONAL_TYPE_MODIFIER, CONDITIONAL_ALIAS, CONDITIONAL_RESERVED,
|
|
68
|
+
CONDITIONAL_INVALID,
|
|
69
|
+
} ConditionalMacroPrefix;
|
|
70
|
+
|
|
71
|
+
static ConditionalMacroPrefix conditional_macro_prefix_kind(const MacroName *name, const char *word);
|
|
72
|
+
|
|
73
|
+
static ConditionalMacroPrefix conditional_macro_prefix(TSLexer *lexer, bool *escaped);
|
|
74
|
+
|
|
75
|
+
static bool conditional_macro_type_prefix(TSLexer *lexer, bool escaped) {
|
|
76
|
+
ConditionalMacroPrefix kind;
|
|
77
|
+
do {
|
|
78
|
+
kind = conditional_macro_prefix(lexer, &escaped);
|
|
79
|
+
} while (kind == CONDITIONAL_MODIFIER);
|
|
80
|
+
if (kind == CONDITIONAL_TYPE) return true;
|
|
81
|
+
if (kind != CONDITIONAL_ALIAS) return false;
|
|
82
|
+
if (lexer->lookahead == '*') return true;
|
|
83
|
+
MacroName declarator = {.ascii = true};
|
|
84
|
+
return conditional_macro_word(lexer, &declarator, escaped);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
static ConditionalMacroPrefix conditional_macro_prefix(TSLexer *lexer, bool *escaped) {
|
|
88
|
+
if (lexer->lookahead == '[') {
|
|
89
|
+
return conditional_macro_attribute_declaration(lexer, escaped) ? CONDITIONAL_MODIFIER : CONDITIONAL_INVALID;
|
|
90
|
+
}
|
|
91
|
+
char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
|
|
92
|
+
MacroName type = {.word = word, .capacity = sizeof(word) - 1, .ascii = true};
|
|
93
|
+
if (!conditional_macro_word(lexer, &type, *escaped)) return CONDITIONAL_INVALID;
|
|
94
|
+
ConditionalMacroPrefix kind = conditional_macro_prefix_kind(&type, word);
|
|
95
|
+
if (kind == CONDITIONAL_TYPE) return kind;
|
|
96
|
+
if (!conditional_macro_skip_lines(lexer, escaped)) return CONDITIONAL_INVALID;
|
|
97
|
+
if (kind == CONDITIONAL_TYPE_MODIFIER && lexer->lookahead == '(') return CONDITIONAL_TYPE;
|
|
98
|
+
if (kind == CONDITIONAL_MODIFIER || kind == CONDITIONAL_TYPE_MODIFIER) {
|
|
99
|
+
return conditional_macro_modifier_group(lexer, word, escaped) ? CONDITIONAL_MODIFIER : CONDITIONAL_INVALID;
|
|
100
|
+
}
|
|
101
|
+
return kind;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
static ConditionalMacroPrefix conditional_macro_prefix_kind(const MacroName *name, const char *word) {
|
|
105
|
+
if (name->ascii && strcmp(word, pragma_word) == 0) return CONDITIONAL_MODIFIER;
|
|
106
|
+
if (!name->ascii || !function_macro_keyword(word)) return CONDITIONAL_ALIAS;
|
|
107
|
+
bool type = function_macro_word_in(word, function_macro_type_keywords,
|
|
108
|
+
sizeof(function_macro_type_keywords) / sizeof(function_macro_type_keywords[0]));
|
|
109
|
+
bool modifier = (!type && function_macro_object_prefix_keyword(word)) ||
|
|
110
|
+
function_macro_word_in(word, function_macro_declaration_modifiers,
|
|
111
|
+
sizeof(function_macro_declaration_modifiers) / sizeof(function_macro_declaration_modifiers[0]));
|
|
112
|
+
if (type) return modifier ? CONDITIONAL_TYPE_MODIFIER : CONDITIONAL_TYPE;
|
|
113
|
+
return modifier ? CONDITIONAL_MODIFIER : CONDITIONAL_RESERVED;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
static bool conditional_macro_modifier_group(TSLexer *lexer, const char *word, bool *escaped) {
|
|
117
|
+
bool grouped = strcmp(word, pragma_word) == 0 || function_macro_word_in(word, function_macro_group_modifiers,
|
|
118
|
+
sizeof(function_macro_group_modifiers) / sizeof(function_macro_group_modifiers[0]));
|
|
119
|
+
if (!grouped || lexer->lookahead != '(') return true;
|
|
120
|
+
bool literal = false;
|
|
121
|
+
return function_macro_balanced(lexer, '(', ')', &literal) && conditional_macro_skip_lines(lexer, escaped);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
static bool conditional_macro_attribute_declaration(TSLexer *lexer, bool *escaped) {
|
|
125
|
+
lexer->advance(lexer, false);
|
|
126
|
+
if (!conditional_macro_skip_lines(lexer, escaped) || lexer->lookahead != '[') return false;
|
|
127
|
+
bool literal = false;
|
|
128
|
+
if (!function_macro_balanced(lexer, '[', ']', &literal) ||
|
|
129
|
+
!conditional_macro_skip_lines(lexer, escaped) || lexer->lookahead != ']') return false;
|
|
130
|
+
lexer->advance(lexer, false);
|
|
131
|
+
return conditional_macro_skip_lines(lexer, escaped);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
static bool conditional_macro_skip_lines(TSLexer *lexer, bool *escaped) {
|
|
135
|
+
bool separated = false;
|
|
136
|
+
for (;;) {
|
|
137
|
+
*escaped = false;
|
|
138
|
+
if (!conditional_macro_spacing(lexer, &separated, escaped)) return false;
|
|
139
|
+
if (*escaped || !scan_preproc_newline(lexer, false)) return true;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
static bool conditional_macro_marker_line(TSLexer *lexer, bool *escaped) {
|
|
144
|
+
char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
|
|
145
|
+
MacroName marker = {.word = word, .capacity = sizeof(word) - 1, .ascii = true};
|
|
146
|
+
if (!conditional_macro_word(lexer, &marker, *escaped) ||
|
|
147
|
+
(marker.ascii && function_macro_keyword(word))) return false;
|
|
148
|
+
return conditional_macro_next_line(lexer, escaped);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static bool conditional_macro_directive(TSLexer *lexer, MacroName *name) {
|
|
152
|
+
if (lexer->lookahead != '#') return false;
|
|
153
|
+
lexer->advance(lexer, false);
|
|
154
|
+
while (lexer->lookahead == ' ' || lexer->lookahead == '\t') lexer->advance(lexer, false);
|
|
155
|
+
return conditional_macro_word(lexer, name, false);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static bool conditional_macro_next_line(TSLexer *lexer, bool *escaped) {
|
|
159
|
+
bool separated = false;
|
|
160
|
+
*escaped = false;
|
|
161
|
+
if (!conditional_macro_spacing(lexer, &separated, escaped) || *escaped ||
|
|
162
|
+
!scan_preproc_newline(lexer, false)) return false;
|
|
163
|
+
for (;;) {
|
|
164
|
+
if (!conditional_macro_spacing(lexer, &separated, escaped) || lexer->eof(lexer)) return false;
|
|
165
|
+
if (*escaped || !scan_preproc_newline(lexer, false)) return true;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
static bool conditional_macro_spacing(TSLexer *lexer, bool *separated, bool *escaped) {
|
|
170
|
+
while (lexer->lookahead != '\n' && lexer->lookahead != '\r') {
|
|
171
|
+
int32_t c = lexer->lookahead;
|
|
172
|
+
if (pragma_space(c)) {
|
|
173
|
+
*separated = true;
|
|
174
|
+
lexer->advance(lexer, false);
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (c == '/') {
|
|
178
|
+
*separated = true;
|
|
179
|
+
if (!function_macro_argument_slash(lexer, false)) return false;
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
if (c != '\\') return true;
|
|
183
|
+
lexer->advance(lexer, false);
|
|
184
|
+
if (scan_preproc_newline(lexer, false)) continue;
|
|
185
|
+
*escaped = lexer->lookahead == 'u' || lexer->lookahead == 'U';
|
|
186
|
+
return *escaped;
|
|
187
|
+
}
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
static bool conditional_macro_word(TSLexer *lexer, MacroName *name, bool escaped) {
|
|
192
|
+
bool has_name = escaped;
|
|
193
|
+
if (escaped) {
|
|
194
|
+
name->ascii = false;
|
|
195
|
+
if (!scan_macro_name_ucn(lexer)) return false;
|
|
196
|
+
}
|
|
197
|
+
for (;;) {
|
|
198
|
+
MacroNameCharacter character = scan_macro_name_character(lexer, has_name, name);
|
|
199
|
+
if (character == MACRO_NAME_END) return has_name;
|
|
200
|
+
if (character == MACRO_NAME_SPLICE) {
|
|
201
|
+
return has_name && lexer->lookahead != '\\' && !preproc_word(lexer->lookahead, true);
|
|
202
|
+
}
|
|
203
|
+
if (character != MACRO_NAME_CHARACTER) return false;
|
|
204
|
+
has_name = true;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
#endif
|
package/src/keywords.h
CHANGED
package/src/node-types.json
CHANGED
|
@@ -1448,10 +1448,18 @@
|
|
|
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
|
|
1454
1458
|
},
|
|
1459
|
+
{
|
|
1460
|
+
"type": "preproc_ifdef",
|
|
1461
|
+
"named": true
|
|
1462
|
+
},
|
|
1455
1463
|
{
|
|
1456
1464
|
"type": "storage_class_specifier",
|
|
1457
1465
|
"named": true
|
|
@@ -1471,18 +1479,66 @@
|
|
|
1471
1479
|
"multiple": true,
|
|
1472
1480
|
"required": false,
|
|
1473
1481
|
"types": [
|
|
1482
|
+
{
|
|
1483
|
+
"type": "attributed_statement",
|
|
1484
|
+
"named": true
|
|
1485
|
+
},
|
|
1486
|
+
{
|
|
1487
|
+
"type": "break_statement",
|
|
1488
|
+
"named": true
|
|
1489
|
+
},
|
|
1490
|
+
{
|
|
1491
|
+
"type": "case_statement",
|
|
1492
|
+
"named": true
|
|
1493
|
+
},
|
|
1494
|
+
{
|
|
1495
|
+
"type": "compound_statement",
|
|
1496
|
+
"named": true
|
|
1497
|
+
},
|
|
1498
|
+
{
|
|
1499
|
+
"type": "continue_statement",
|
|
1500
|
+
"named": true
|
|
1501
|
+
},
|
|
1474
1502
|
{
|
|
1475
1503
|
"type": "declaration",
|
|
1476
1504
|
"named": true
|
|
1477
1505
|
},
|
|
1506
|
+
{
|
|
1507
|
+
"type": "do_statement",
|
|
1508
|
+
"named": true
|
|
1509
|
+
},
|
|
1510
|
+
{
|
|
1511
|
+
"type": "expression_statement",
|
|
1512
|
+
"named": true
|
|
1513
|
+
},
|
|
1514
|
+
{
|
|
1515
|
+
"type": "for_statement",
|
|
1516
|
+
"named": true
|
|
1517
|
+
},
|
|
1478
1518
|
{
|
|
1479
1519
|
"type": "function_definition",
|
|
1480
1520
|
"named": true
|
|
1481
1521
|
},
|
|
1522
|
+
{
|
|
1523
|
+
"type": "goto_statement",
|
|
1524
|
+
"named": true
|
|
1525
|
+
},
|
|
1526
|
+
{
|
|
1527
|
+
"type": "if_statement",
|
|
1528
|
+
"named": true
|
|
1529
|
+
},
|
|
1530
|
+
{
|
|
1531
|
+
"type": "labeled_statement",
|
|
1532
|
+
"named": true
|
|
1533
|
+
},
|
|
1482
1534
|
{
|
|
1483
1535
|
"type": "linkage_specification",
|
|
1484
1536
|
"named": true
|
|
1485
1537
|
},
|
|
1538
|
+
{
|
|
1539
|
+
"type": "macro_specifier",
|
|
1540
|
+
"named": true
|
|
1541
|
+
},
|
|
1486
1542
|
{
|
|
1487
1543
|
"type": "ms_declspec_modifier",
|
|
1488
1544
|
"named": true
|
|
@@ -1512,7 +1568,11 @@
|
|
|
1512
1568
|
"named": true
|
|
1513
1569
|
},
|
|
1514
1570
|
{
|
|
1515
|
-
"type": "
|
|
1571
|
+
"type": "return_statement",
|
|
1572
|
+
"named": true
|
|
1573
|
+
},
|
|
1574
|
+
{
|
|
1575
|
+
"type": "switch_statement",
|
|
1516
1576
|
"named": true
|
|
1517
1577
|
},
|
|
1518
1578
|
{
|
|
@@ -1522,6 +1582,10 @@
|
|
|
1522
1582
|
{
|
|
1523
1583
|
"type": "type_specifier",
|
|
1524
1584
|
"named": true
|
|
1585
|
+
},
|
|
1586
|
+
{
|
|
1587
|
+
"type": "while_statement",
|
|
1588
|
+
"named": true
|
|
1525
1589
|
}
|
|
1526
1590
|
]
|
|
1527
1591
|
}
|
|
@@ -1757,10 +1821,18 @@
|
|
|
1757
1821
|
"type": "bitfield_clause",
|
|
1758
1822
|
"named": true
|
|
1759
1823
|
},
|
|
1824
|
+
{
|
|
1825
|
+
"type": "macro_specifier",
|
|
1826
|
+
"named": true
|
|
1827
|
+
},
|
|
1760
1828
|
{
|
|
1761
1829
|
"type": "ms_declspec_modifier",
|
|
1762
1830
|
"named": true
|
|
1763
1831
|
},
|
|
1832
|
+
{
|
|
1833
|
+
"type": "preproc_ifdef",
|
|
1834
|
+
"named": true
|
|
1835
|
+
},
|
|
1764
1836
|
{
|
|
1765
1837
|
"type": "storage_class_specifier",
|
|
1766
1838
|
"named": true
|
|
@@ -1784,6 +1856,10 @@
|
|
|
1784
1856
|
"type": "field_declaration",
|
|
1785
1857
|
"named": true
|
|
1786
1858
|
},
|
|
1859
|
+
{
|
|
1860
|
+
"type": "macro_specifier",
|
|
1861
|
+
"named": true
|
|
1862
|
+
},
|
|
1787
1863
|
{
|
|
1788
1864
|
"type": "preproc_call",
|
|
1789
1865
|
"named": true
|
|
@@ -2031,6 +2107,10 @@
|
|
|
2031
2107
|
"type": "declaration",
|
|
2032
2108
|
"named": true
|
|
2033
2109
|
},
|
|
2110
|
+
{
|
|
2111
|
+
"type": "macro_specifier",
|
|
2112
|
+
"named": true
|
|
2113
|
+
},
|
|
2034
2114
|
{
|
|
2035
2115
|
"type": "ms_call_modifier",
|
|
2036
2116
|
"named": true
|
|
@@ -2039,6 +2119,10 @@
|
|
|
2039
2119
|
"type": "ms_declspec_modifier",
|
|
2040
2120
|
"named": true
|
|
2041
2121
|
},
|
|
2122
|
+
{
|
|
2123
|
+
"type": "preproc_ifdef",
|
|
2124
|
+
"named": true
|
|
2125
|
+
},
|
|
2042
2126
|
{
|
|
2043
2127
|
"type": "storage_class_specifier",
|
|
2044
2128
|
"named": true
|
|
@@ -2527,6 +2611,22 @@
|
|
|
2527
2611
|
}
|
|
2528
2612
|
}
|
|
2529
2613
|
},
|
|
2614
|
+
{
|
|
2615
|
+
"type": "macro_specifier",
|
|
2616
|
+
"named": true,
|
|
2617
|
+
"fields": {
|
|
2618
|
+
"name": {
|
|
2619
|
+
"multiple": false,
|
|
2620
|
+
"required": true,
|
|
2621
|
+
"types": [
|
|
2622
|
+
{
|
|
2623
|
+
"type": "identifier",
|
|
2624
|
+
"named": true
|
|
2625
|
+
}
|
|
2626
|
+
]
|
|
2627
|
+
}
|
|
2628
|
+
}
|
|
2629
|
+
},
|
|
2530
2630
|
{
|
|
2531
2631
|
"type": "macro_type_specifier",
|
|
2532
2632
|
"named": true,
|
|
@@ -2705,10 +2805,18 @@
|
|
|
2705
2805
|
"type": "attribute_specifier",
|
|
2706
2806
|
"named": true
|
|
2707
2807
|
},
|
|
2808
|
+
{
|
|
2809
|
+
"type": "macro_specifier",
|
|
2810
|
+
"named": true
|
|
2811
|
+
},
|
|
2708
2812
|
{
|
|
2709
2813
|
"type": "ms_declspec_modifier",
|
|
2710
2814
|
"named": true
|
|
2711
2815
|
},
|
|
2816
|
+
{
|
|
2817
|
+
"type": "preproc_ifdef",
|
|
2818
|
+
"named": true
|
|
2819
|
+
},
|
|
2712
2820
|
{
|
|
2713
2821
|
"type": "storage_class_specifier",
|
|
2714
2822
|
"named": true
|
|
@@ -2862,6 +2970,10 @@
|
|
|
2862
2970
|
"type": "attribute_specifier",
|
|
2863
2971
|
"named": true
|
|
2864
2972
|
},
|
|
2973
|
+
{
|
|
2974
|
+
"type": "macro_specifier",
|
|
2975
|
+
"named": true
|
|
2976
|
+
},
|
|
2865
2977
|
{
|
|
2866
2978
|
"type": "ms_based_modifier",
|
|
2867
2979
|
"named": true
|
|
@@ -2874,6 +2986,10 @@
|
|
|
2874
2986
|
"type": "ms_pointer_modifier",
|
|
2875
2987
|
"named": true
|
|
2876
2988
|
},
|
|
2989
|
+
{
|
|
2990
|
+
"type": "preproc_ifdef",
|
|
2991
|
+
"named": true
|
|
2992
|
+
},
|
|
2877
2993
|
{
|
|
2878
2994
|
"type": "type_qualifier",
|
|
2879
2995
|
"named": true
|
|
@@ -3103,6 +3219,10 @@
|
|
|
3103
3219
|
"type": "linkage_specification",
|
|
3104
3220
|
"named": true
|
|
3105
3221
|
},
|
|
3222
|
+
{
|
|
3223
|
+
"type": "macro_specifier",
|
|
3224
|
+
"named": true
|
|
3225
|
+
},
|
|
3106
3226
|
{
|
|
3107
3227
|
"type": "ms_declspec_modifier",
|
|
3108
3228
|
"named": true
|
|
@@ -3215,6 +3335,10 @@
|
|
|
3215
3335
|
"type": "linkage_specification",
|
|
3216
3336
|
"named": true
|
|
3217
3337
|
},
|
|
3338
|
+
{
|
|
3339
|
+
"type": "macro_specifier",
|
|
3340
|
+
"named": true
|
|
3341
|
+
},
|
|
3218
3342
|
{
|
|
3219
3343
|
"type": "ms_declspec_modifier",
|
|
3220
3344
|
"named": true
|
|
@@ -3298,6 +3422,10 @@
|
|
|
3298
3422
|
"type": "linkage_specification",
|
|
3299
3423
|
"named": true
|
|
3300
3424
|
},
|
|
3425
|
+
{
|
|
3426
|
+
"type": "macro_specifier",
|
|
3427
|
+
"named": true
|
|
3428
|
+
},
|
|
3301
3429
|
{
|
|
3302
3430
|
"type": "ms_declspec_modifier",
|
|
3303
3431
|
"named": true
|
|
@@ -3488,6 +3616,10 @@
|
|
|
3488
3616
|
"type": "linkage_specification",
|
|
3489
3617
|
"named": true
|
|
3490
3618
|
},
|
|
3619
|
+
{
|
|
3620
|
+
"type": "macro_specifier",
|
|
3621
|
+
"named": true
|
|
3622
|
+
},
|
|
3491
3623
|
{
|
|
3492
3624
|
"type": "ms_declspec_modifier",
|
|
3493
3625
|
"named": true
|
|
@@ -3600,6 +3732,10 @@
|
|
|
3600
3732
|
"type": "linkage_specification",
|
|
3601
3733
|
"named": true
|
|
3602
3734
|
},
|
|
3735
|
+
{
|
|
3736
|
+
"type": "macro_specifier",
|
|
3737
|
+
"named": true
|
|
3738
|
+
},
|
|
3603
3739
|
{
|
|
3604
3740
|
"type": "ms_declspec_modifier",
|
|
3605
3741
|
"named": true
|
|
@@ -4129,6 +4265,10 @@
|
|
|
4129
4265
|
"type": "linkage_specification",
|
|
4130
4266
|
"named": true
|
|
4131
4267
|
},
|
|
4268
|
+
{
|
|
4269
|
+
"type": "macro_specifier",
|
|
4270
|
+
"named": true
|
|
4271
|
+
},
|
|
4132
4272
|
{
|
|
4133
4273
|
"type": "ms_declspec_modifier",
|
|
4134
4274
|
"named": true
|
|
@@ -4229,10 +4369,18 @@
|
|
|
4229
4369
|
"type": "enum_specifier",
|
|
4230
4370
|
"named": true
|
|
4231
4371
|
},
|
|
4372
|
+
{
|
|
4373
|
+
"type": "macro_specifier",
|
|
4374
|
+
"named": true
|
|
4375
|
+
},
|
|
4232
4376
|
{
|
|
4233
4377
|
"type": "ms_declspec_modifier",
|
|
4234
4378
|
"named": true
|
|
4235
4379
|
},
|
|
4380
|
+
{
|
|
4381
|
+
"type": "preproc_ifdef",
|
|
4382
|
+
"named": true
|
|
4383
|
+
},
|
|
4236
4384
|
{
|
|
4237
4385
|
"type": "primitive_type",
|
|
4238
4386
|
"named": true
|
package/src/scanner.c
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
#include "
|
|
1
|
+
#include "conditionalMacro.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, TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START };
|
|
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, CONDITIONAL_MARKER_IFDEF, CONDITIONAL_MARKER_IFNDEF, LEADING_CONDITIONAL_MARKER_IFDEF, LEADING_CONDITIONAL_MARKER_IFNDEF };
|
|
4
4
|
|
|
5
5
|
static bool scan_preproc_tokens(TSLexer *lexer, const bool *valid_symbols);
|
|
6
6
|
|
|
@@ -34,6 +34,16 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
|
|
|
34
34
|
valid_symbols[PREPROC_ARG] || valid_symbols[PREPROC_DIRECTIVE_ARG]) {
|
|
35
35
|
return scan_preproc_tokens(lexer, valid_symbols);
|
|
36
36
|
}
|
|
37
|
+
bool leading = valid_symbols[LEADING_CONDITIONAL_MARKER_IFDEF] || valid_symbols[LEADING_CONDITIONAL_MARKER_IFNDEF];
|
|
38
|
+
bool conditional = leading || valid_symbols[CONDITIONAL_MARKER_IFDEF] || valid_symbols[CONDITIONAL_MARKER_IFNDEF];
|
|
39
|
+
if (conditional) {
|
|
40
|
+
while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
|
|
41
|
+
const ConditionalMacroSymbols symbols = {
|
|
42
|
+
.ifdef = CONDITIONAL_MARKER_IFDEF, .ifndef = CONDITIONAL_MARKER_IFNDEF,
|
|
43
|
+
.leading_ifdef = LEADING_CONDITIONAL_MARKER_IFDEF, .leading_ifndef = LEADING_CONDITIONAL_MARKER_IFNDEF,
|
|
44
|
+
};
|
|
45
|
+
if (lexer->lookahead == '#') return scan_conditional_macro(lexer, &symbols, valid_symbols);
|
|
46
|
+
}
|
|
37
47
|
static const TSSymbol attribute_tokens[] = {
|
|
38
48
|
FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX,
|
|
39
49
|
TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START,
|
package/tree-sitter-c.wasm
CHANGED
|
Binary file
|