@willbooster/tree-sitter-c 9.0.1 → 11.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 +37 -6
- package/grammar.js +215 -69
- package/package.json +4 -1
- package/src/functionMacro.h +352 -0
- package/src/grammarKeywords.cjs +4 -0
- package/src/identifier.h +1 -1
- package/src/keywords.h +298 -0
- package/src/node-types.json +27 -2
- package/src/pragma.h +77 -36
- package/src/scanner.c +30 -2
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +1 -1
package/README.md
CHANGED
|
@@ -56,12 +56,43 @@ const parser = new Parser();
|
|
|
56
56
|
parser.setLanguage(await Language.load(c));
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
-
The package also ships `grammar.js`,
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
61
|
+
The scanner sources include `scanner.c`, `pragma.h`, `identifier.h`, `keywords.h`, and `functionMacro.h`.
|
|
62
|
+
|
|
63
|
+
Derived grammars must port the inherited external-token dispatch from `src/scanner.c`.
|
|
64
|
+
Keep preprocessor argument/newline handling ahead of annotation lookahead.
|
|
65
|
+
The hidden `_function_attribute_start`, `_post_type_function_attribute_start`, and
|
|
66
|
+
`_post_type_function_attribute_suffix` guards look ahead before the ordinary identifier token;
|
|
67
|
+
keep them outside the name field so identifier leaves and ordinary C lexing retain their shape.
|
|
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.
|
|
73
|
+
Regenerate Unicode and keyword tables from the derived grammar's metadata.
|
|
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`.
|
|
76
|
+
The `_preproc_function_name` token ends before splices used to determine adjacency, so the `_preproc_lparen`
|
|
77
|
+
scanner must skip those splices again to reach `(`.
|
|
78
|
+
|
|
79
|
+
Prototype annotation lookahead accepts argument/prototype groups without unquoted braces, semicolons or preprocessing directives.
|
|
80
|
+
Quoted punctuation remains literal content. GNU statement-expression, inline-tag, compound-literal
|
|
81
|
+
and directive-bearing annotation arguments need a separate grammar design to keep malformed-input recovery bounded.
|
|
82
|
+
|
|
83
|
+
Header annotation declarations use `_macro_modified_declaration`, `_macro_declaration_specifiers`,
|
|
84
|
+
`_macro_declaration_declarator`, and `_header_preproc_item`. Adapt these rules alongside public declaration/type
|
|
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.
|
|
65
96
|
|
|
66
97
|
In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-c) and on
|
|
67
98
|
[willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
|
|
@@ -70,7 +101,7 @@ fuzzed with, at version 7.0.5 or later. No other runtime loads the parser:
|
|
|
70
101
|
```toml
|
|
71
102
|
[dependencies]
|
|
72
103
|
tree-sitter = { package = "willbooster-tree-sitter", version = "7.0.5" }
|
|
73
|
-
tree-sitter-c = { package = "willbooster-tree-sitter-c", version = "
|
|
104
|
+
tree-sitter-c = { package = "willbooster-tree-sitter-c", version = "11" }
|
|
74
105
|
```
|
|
75
106
|
|
|
76
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,6 +71,13 @@ module.exports = Object.assign(
|
|
|
68
71
|
name: 'c',
|
|
69
72
|
|
|
70
73
|
conflicts: ($) => [
|
|
74
|
+
[$._macro_declaration_specifiers],
|
|
75
|
+
[$._macro_declaration_specifiers, $.type_definition, $.type_specifier],
|
|
76
|
+
[$._macro_declaration_specifiers, $.type_specifier, $.expression],
|
|
77
|
+
[$._macro_declaration_specifiers, $.type_specifier],
|
|
78
|
+
[$._macro_declaration_specifiers, $.type_definition, $._declaration_modifiers],
|
|
79
|
+
[$._macro_declaration_specifiers, $._declaration_modifiers, $._empty_declaration],
|
|
80
|
+
[$._macro_declaration_specifiers, $._declaration_modifiers],
|
|
71
81
|
[$.preproc_include_argument],
|
|
72
82
|
[$._preproc_include_token, $.preproc_argument_list],
|
|
73
83
|
[$._preproc_include_path, $.preproc_include_argument],
|
|
@@ -148,6 +158,11 @@ module.exports = Object.assign(
|
|
|
148
158
|
sym('_preproc_lparen'),
|
|
149
159
|
sym('_preproc_directive_arg'),
|
|
150
160
|
sym('_preproc_function_name'),
|
|
161
|
+
sym('_function_attribute_start'),
|
|
162
|
+
sym('_post_type_function_attribute_start'),
|
|
163
|
+
sym('_post_type_function_attribute_suffix'),
|
|
164
|
+
sym('_trailing_macro_attribute_name'),
|
|
165
|
+
sym('_trailing_object_declaration_start'),
|
|
151
166
|
],
|
|
152
167
|
|
|
153
168
|
extras: ($) => [$.pragma_operator, /\s|\\(?:\r\n?|\n\r?)/, $.comment],
|
|
@@ -194,34 +209,23 @@ module.exports = Object.assign(
|
|
|
194
209
|
alias($._old_style_function_definition, $.function_definition),
|
|
195
210
|
$.linkage_specification,
|
|
196
211
|
$.declaration,
|
|
212
|
+
alias(sym('_macro_modified_declaration'), $.declaration),
|
|
213
|
+
alias(sym('_trailing_macro_modified_declaration'), $.declaration),
|
|
197
214
|
$._top_level_statement,
|
|
198
215
|
$.attributed_statement,
|
|
199
216
|
$.type_definition,
|
|
200
217
|
$._empty_declaration,
|
|
201
|
-
sym('preproc_if'),
|
|
202
|
-
sym('preproc_ifdef'),
|
|
218
|
+
alias(sym('preproc_if_in_header'), sym('preproc_if')),
|
|
219
|
+
alias(sym('preproc_ifdef_in_header'), sym('preproc_ifdef')),
|
|
203
220
|
$.preproc_include,
|
|
204
221
|
$.preproc_def,
|
|
205
222
|
$.preproc_function_def,
|
|
206
223
|
$.preproc_call
|
|
207
224
|
),
|
|
208
225
|
|
|
209
|
-
_block_item: ($) =>
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
alias($._old_style_function_definition, $.function_definition),
|
|
213
|
-
$.linkage_specification,
|
|
214
|
-
$.declaration,
|
|
215
|
-
$.statement,
|
|
216
|
-
$.type_definition,
|
|
217
|
-
$._empty_declaration,
|
|
218
|
-
sym('preproc_if'),
|
|
219
|
-
sym('preproc_ifdef'),
|
|
220
|
-
$.preproc_include,
|
|
221
|
-
$.preproc_def,
|
|
222
|
-
$.preproc_function_def,
|
|
223
|
-
$.preproc_call
|
|
224
|
-
),
|
|
226
|
+
_block_item: ($) => blockItemRule($, false),
|
|
227
|
+
|
|
228
|
+
_header_preproc_item: ($) => blockItemRule($, true),
|
|
225
229
|
|
|
226
230
|
// Derived grammars that replace externals still need this lexical fallback. Comment-rich spacing makes the
|
|
227
231
|
// lexer too large for the pinned Node Wasm optimizer; the external scanner handles those forms.
|
|
@@ -313,6 +317,8 @@ module.exports = Object.assign(
|
|
|
313
317
|
sym('_preproc_newline')
|
|
314
318
|
),
|
|
315
319
|
|
|
320
|
+
...preprocIf('_in_header', () => sym('_header_preproc_item')),
|
|
321
|
+
|
|
316
322
|
...preprocIf('', () => sym('_block_item')),
|
|
317
323
|
...preprocIf('_in_single_case', () => sym('_single_case_body'), 0, true),
|
|
318
324
|
...preprocIf('_in_field_declaration_list', () => sym('_field_declaration_list_item')),
|
|
@@ -426,31 +432,50 @@ module.exports = Object.assign(
|
|
|
426
432
|
)
|
|
427
433
|
),
|
|
428
434
|
|
|
429
|
-
declaration: ($) =>
|
|
435
|
+
declaration: ($) => declarationRule($, $._declaration_specifiers, true),
|
|
436
|
+
|
|
437
|
+
_macro_modified_declaration: ($) =>
|
|
438
|
+
declarationRule($, $._macro_declaration_specifiers, false, $._macro_declaration_declarator),
|
|
439
|
+
|
|
440
|
+
_trailing_macro_modified_declaration: ($) =>
|
|
430
441
|
seq(
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
seq(
|
|
436
|
-
repeat($._declaration_modifiers),
|
|
437
|
-
alias('auto', $.storage_class_specifier),
|
|
438
|
-
repeat($._declaration_modifiers)
|
|
439
|
-
)
|
|
440
|
-
)
|
|
441
|
-
),
|
|
442
|
-
commaSep1(
|
|
443
|
-
field(
|
|
444
|
-
'declarator',
|
|
445
|
-
choice(
|
|
446
|
-
seq(optional($.ms_call_modifier), $._declaration_declarator, optional($.gnu_asm_expression)),
|
|
447
|
-
$.init_declarator
|
|
448
|
-
)
|
|
449
|
-
)
|
|
450
|
-
),
|
|
442
|
+
sym('_trailing_object_declaration_start'),
|
|
443
|
+
$._declaration_specifiers,
|
|
444
|
+
field('declarator', $._trailing_macro_object_declarator),
|
|
445
|
+
repeat1(alias($._trailing_macro_attribute_specifier, $.attribute_specifier)),
|
|
451
446
|
';'
|
|
452
447
|
),
|
|
453
448
|
|
|
449
|
+
_trailing_macro_object_declarator: ($) =>
|
|
450
|
+
choice($.identifier, alias($._trailing_macro_pointer_declarator, $.pointer_declarator)),
|
|
451
|
+
|
|
452
|
+
_trailing_macro_pointer_declarator: ($) => pointerDeclaratorRule($, $._trailing_macro_object_declarator),
|
|
453
|
+
|
|
454
|
+
_macro_declaration_specifiers: ($) => {
|
|
455
|
+
const modifier = choice($._declaration_modifiers, alias($._macro_attribute_suffix, $.attribute_specifier));
|
|
456
|
+
const postModifier = choice(
|
|
457
|
+
$._declaration_modifiers,
|
|
458
|
+
alias($._post_type_macro_attribute_suffix, $.attribute_specifier)
|
|
459
|
+
);
|
|
460
|
+
return choice(
|
|
461
|
+
seq(
|
|
462
|
+
choice($.storage_class_specifier, $.type_qualifier, $.ms_declspec_modifier),
|
|
463
|
+
repeat($._declaration_modifiers),
|
|
464
|
+
alias($._macro_attribute_specifier, $.attribute_specifier),
|
|
465
|
+
repeat(modifier),
|
|
466
|
+
field('type', $._non_identifier_type_specifier),
|
|
467
|
+
repeat(postModifier)
|
|
468
|
+
),
|
|
469
|
+
seq(
|
|
470
|
+
repeat($._declaration_modifiers),
|
|
471
|
+
field('type', choice($._non_identifier_type_specifier, $._type_identifier)),
|
|
472
|
+
repeat($._declaration_modifiers),
|
|
473
|
+
alias($._post_type_macro_attribute_specifier, $.attribute_specifier),
|
|
474
|
+
repeat(postModifier)
|
|
475
|
+
)
|
|
476
|
+
);
|
|
477
|
+
},
|
|
478
|
+
|
|
454
479
|
type_definition: ($) =>
|
|
455
480
|
seq(
|
|
456
481
|
optional(prec.dynamic(1, '__extension__')),
|
|
@@ -493,6 +518,21 @@ module.exports = Object.assign(
|
|
|
493
518
|
|
|
494
519
|
attribute_specifier: ($) => seq(choice('__attribute__', '__attribute'), '(', $.argument_list, ')'),
|
|
495
520
|
|
|
521
|
+
_macro_attribute_specifier: ($) => macroAttributeRule($, 'leading'),
|
|
522
|
+
|
|
523
|
+
_post_type_macro_attribute_specifier: ($) => macroAttributeRule($, 'post-type'),
|
|
524
|
+
|
|
525
|
+
_macro_attribute_suffix: ($) => macroAttributeRule($, 'continuation'),
|
|
526
|
+
|
|
527
|
+
_trailing_macro_attribute_specifier: ($) => macroAttributeRule($, 'trailing'),
|
|
528
|
+
|
|
529
|
+
_post_type_macro_attribute_suffix: ($) => macroAttributeRule($, 'post-type-continuation'),
|
|
530
|
+
|
|
531
|
+
_macro_attribute_argument_list: ($) => {
|
|
532
|
+
const argument = macroAttributeArgument($);
|
|
533
|
+
return seq('(', optional(argument), repeat(seq(',', optional(argument))), ')');
|
|
534
|
+
},
|
|
535
|
+
|
|
496
536
|
attribute: ($) =>
|
|
497
537
|
seq(optional(seq(field('prefix', $.identifier), '::')), field('name', $.identifier), optional($.argument_list)),
|
|
498
538
|
|
|
@@ -538,15 +578,9 @@ module.exports = Object.assign(
|
|
|
538
578
|
),
|
|
539
579
|
|
|
540
580
|
_declaration_declarator: ($) =>
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
alias($._function_declaration_declarator, $.function_declarator),
|
|
545
|
-
$.array_declarator,
|
|
546
|
-
$.parenthesized_declarator,
|
|
547
|
-
$.identifier,
|
|
548
|
-
alias(VA_ARG_KEYWORDS, $.identifier)
|
|
549
|
-
),
|
|
581
|
+
declarationDeclaratorRule($, alias($._function_declaration_declarator, $.function_declarator)),
|
|
582
|
+
|
|
583
|
+
_macro_declaration_declarator: ($) => declarationDeclaratorRule($, $.function_declarator),
|
|
550
584
|
|
|
551
585
|
_field_declarator: ($) =>
|
|
552
586
|
choice(alias($.attributed_field_declarator, $.attributed_declarator), $._unattributed_field_declarator),
|
|
@@ -595,21 +629,7 @@ module.exports = Object.assign(
|
|
|
595
629
|
attributed_field_declarator: ($) => seq($._unattributed_field_declarator, repeat1($.attribute_declaration)),
|
|
596
630
|
attributed_type_declarator: ($) => seq($._unattributed_type_declarator, repeat1($.attribute_declaration)),
|
|
597
631
|
|
|
598
|
-
pointer_declarator: ($) =>
|
|
599
|
-
prec.dynamic(
|
|
600
|
-
1,
|
|
601
|
-
prec.right(
|
|
602
|
-
seq(
|
|
603
|
-
optional($.ms_based_modifier),
|
|
604
|
-
optional(prec.dynamic(2, $.ms_call_modifier)),
|
|
605
|
-
'*',
|
|
606
|
-
repeat($.attribute_declaration),
|
|
607
|
-
repeat($.ms_pointer_modifier),
|
|
608
|
-
repeat(choice($.type_qualifier, $.attribute_specifier)),
|
|
609
|
-
field('declarator', $._declarator)
|
|
610
|
-
)
|
|
611
|
-
)
|
|
612
|
-
),
|
|
632
|
+
pointer_declarator: ($) => pointerDeclaratorRule($, $._declarator),
|
|
613
633
|
pointer_field_declarator: ($) =>
|
|
614
634
|
prec.dynamic(
|
|
615
635
|
1,
|
|
@@ -812,12 +832,7 @@ module.exports = Object.assign(
|
|
|
812
832
|
primitive_type: () =>
|
|
813
833
|
token(
|
|
814
834
|
choice(
|
|
815
|
-
|
|
816
|
-
'char',
|
|
817
|
-
'int',
|
|
818
|
-
'float',
|
|
819
|
-
'double',
|
|
820
|
-
'void',
|
|
835
|
+
...primitiveTypeKeywords,
|
|
821
836
|
'size_t',
|
|
822
837
|
'ssize_t',
|
|
823
838
|
'ptrdiff_t',
|
|
@@ -1601,12 +1616,143 @@ module.exports = Object.assign(
|
|
|
1601
1616
|
)
|
|
1602
1617
|
),
|
|
1603
1618
|
|
|
1619
|
+
_preproc_number: () =>
|
|
1620
|
+
/(\d|\.\d)([\p{XID_Continue}.]|[eEpP][+-]|'[_a-zA-Z0-9]|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8})*/u,
|
|
1621
|
+
|
|
1604
1622
|
comment: () => token(choice(LINE_COMMENT, seq('/*', /[^*]*\*+([^/*][^*]*\*+)*/, '/'))),
|
|
1605
1623
|
},
|
|
1606
1624
|
}),
|
|
1607
1625
|
{ PREC, preprocIf, preprocessor, commaSep, commaSep1 }
|
|
1608
1626
|
);
|
|
1609
1627
|
|
|
1628
|
+
/**
|
|
1629
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'ms_based_modifier' | 'ms_call_modifier' | 'attribute_declaration' | 'ms_pointer_modifier' | 'type_qualifier' | 'attribute_specifier'>} $
|
|
1630
|
+
* @param {import('./types/treeSitterDsl').Rule} declarator
|
|
1631
|
+
*/
|
|
1632
|
+
function pointerDeclaratorRule($, declarator) {
|
|
1633
|
+
return prec.dynamic(
|
|
1634
|
+
1,
|
|
1635
|
+
prec.right(
|
|
1636
|
+
seq(
|
|
1637
|
+
optional($.ms_based_modifier),
|
|
1638
|
+
optional(prec.dynamic(2, $.ms_call_modifier)),
|
|
1639
|
+
'*',
|
|
1640
|
+
repeat($.attribute_declaration),
|
|
1641
|
+
repeat($.ms_pointer_modifier),
|
|
1642
|
+
repeat(choice($.type_qualifier, $.attribute_specifier)),
|
|
1643
|
+
field('declarator', declarator)
|
|
1644
|
+
)
|
|
1645
|
+
)
|
|
1646
|
+
);
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
/**
|
|
1650
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'storage_class_specifier' | 'ms_call_modifier' | '_declaration_declarator' | 'gnu_asm_expression' | 'init_declarator' | '_declaration_modifiers'>} $
|
|
1651
|
+
* @param {import('./types/treeSitterDsl').Rule} specifiers
|
|
1652
|
+
* @param {boolean} allowAuto
|
|
1653
|
+
* @param {import('./types/treeSitterDsl').Rule} declarator
|
|
1654
|
+
*/
|
|
1655
|
+
function declarationRule($, specifiers, allowAuto, declarator = $._declaration_declarator) {
|
|
1656
|
+
return seq(
|
|
1657
|
+
allowAuto
|
|
1658
|
+
? choice(
|
|
1659
|
+
specifiers,
|
|
1660
|
+
prec.dynamic(
|
|
1661
|
+
PREC.PAREN_DECLARATOR - 1,
|
|
1662
|
+
seq(
|
|
1663
|
+
repeat($._declaration_modifiers),
|
|
1664
|
+
alias('auto', $.storage_class_specifier),
|
|
1665
|
+
repeat($._declaration_modifiers)
|
|
1666
|
+
)
|
|
1667
|
+
)
|
|
1668
|
+
)
|
|
1669
|
+
: specifiers,
|
|
1670
|
+
commaSep1(
|
|
1671
|
+
field(
|
|
1672
|
+
'declarator',
|
|
1673
|
+
choice(seq(optional($.ms_call_modifier), declarator, optional($.gnu_asm_expression)), $.init_declarator)
|
|
1674
|
+
)
|
|
1675
|
+
),
|
|
1676
|
+
';'
|
|
1677
|
+
);
|
|
1678
|
+
}
|
|
1679
|
+
|
|
1680
|
+
/**
|
|
1681
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'attributed_declarator' | 'pointer_declarator' | 'array_declarator' | 'parenthesized_declarator' | 'identifier'>} $
|
|
1682
|
+
* @param {import('./types/treeSitterDsl').Rule} functionDeclarator
|
|
1683
|
+
*/
|
|
1684
|
+
function declarationDeclaratorRule($, functionDeclarator) {
|
|
1685
|
+
return choice(
|
|
1686
|
+
$.attributed_declarator,
|
|
1687
|
+
$.pointer_declarator,
|
|
1688
|
+
functionDeclarator,
|
|
1689
|
+
$.array_declarator,
|
|
1690
|
+
$.parenthesized_declarator,
|
|
1691
|
+
$.identifier,
|
|
1692
|
+
alias(VA_ARG_KEYWORDS, $.identifier)
|
|
1693
|
+
);
|
|
1694
|
+
}
|
|
1695
|
+
|
|
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
|
+
/**
|
|
1725
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'identifier' | 'argument_list' | '_macro_attribute_argument_list'>} $
|
|
1726
|
+
* @param {'leading' | 'post-type' | 'continuation' | 'post-type-continuation' | 'trailing'} context
|
|
1727
|
+
*/
|
|
1728
|
+
function macroAttributeRule($, context) {
|
|
1729
|
+
const guard =
|
|
1730
|
+
context === 'continuation' || context === 'trailing'
|
|
1731
|
+
? []
|
|
1732
|
+
: [
|
|
1733
|
+
sym(
|
|
1734
|
+
context === 'leading'
|
|
1735
|
+
? '_function_attribute_start'
|
|
1736
|
+
: context === 'post-type'
|
|
1737
|
+
? '_post_type_function_attribute_start'
|
|
1738
|
+
: '_post_type_function_attribute_suffix'
|
|
1739
|
+
),
|
|
1740
|
+
];
|
|
1741
|
+
return prec.dynamic(
|
|
1742
|
+
-2,
|
|
1743
|
+
seq(
|
|
1744
|
+
...guard,
|
|
1745
|
+
field('name', context === 'trailing' ? alias(sym('_trailing_macro_attribute_name'), $.identifier) : $.identifier),
|
|
1746
|
+
field('arguments', alias($._macro_attribute_argument_list, $.argument_list))
|
|
1747
|
+
)
|
|
1748
|
+
);
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
/** @param {import('./types/treeSitterDsl').GrammarSymbols<'expression' | 'type_descriptor' | '_preproc_number' | 'number_literal'>} $ */
|
|
1752
|
+
function macroAttributeArgument($) {
|
|
1753
|
+
return choice($.expression, prec.dynamic(-1, $.type_descriptor), alias($._preproc_number, $.number_literal));
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1610
1756
|
/**
|
|
1611
1757
|
* @param {import('./types/treeSitterDsl').RuleOrLiteral} type
|
|
1612
1758
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/tree-sitter-c",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "11.0.0",
|
|
4
4
|
"description": "C grammar for tree-sitter",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"incremental",
|
|
@@ -21,8 +21,11 @@
|
|
|
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",
|
|
27
|
+
"src/keywords.h",
|
|
28
|
+
"src/functionMacro.h",
|
|
26
29
|
"src/scanner.c"
|
|
27
30
|
],
|
|
28
31
|
"scripts": {
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
#ifndef TREE_SITTER_C_FUNCTION_MACRO_H_
|
|
2
|
+
#define TREE_SITTER_C_FUNCTION_MACRO_H_
|
|
3
|
+
|
|
4
|
+
#include "pragma.h"
|
|
5
|
+
#include "keywords.h"
|
|
6
|
+
|
|
7
|
+
typedef struct {
|
|
8
|
+
bool identifier;
|
|
9
|
+
bool number;
|
|
10
|
+
int32_t last;
|
|
11
|
+
} MacroArgumentWord;
|
|
12
|
+
|
|
13
|
+
typedef struct {
|
|
14
|
+
bool post_type;
|
|
15
|
+
bool declarator_group;
|
|
16
|
+
bool explicit_type;
|
|
17
|
+
bool modifier;
|
|
18
|
+
bool declarator_name;
|
|
19
|
+
} MacroPrototypeTail;
|
|
20
|
+
|
|
21
|
+
typedef struct {
|
|
22
|
+
TSSymbol pragma;
|
|
23
|
+
TSSymbol object_start;
|
|
24
|
+
bool allow_pragma;
|
|
25
|
+
bool allow_object_start;
|
|
26
|
+
} MacroAttributeSymbols;
|
|
27
|
+
|
|
28
|
+
static bool function_macro_balanced(TSLexer *lexer, int32_t open, int32_t close, bool *literal);
|
|
29
|
+
static bool function_macro_group(int32_t c, int32_t open, int32_t close, unsigned *depth, unsigned *nested);
|
|
30
|
+
static void function_macro_note_literal(bool *literal, unsigned depth, unsigned nested, const MacroArgumentWord *word, int32_t c);
|
|
31
|
+
static bool function_macro_tail_word(TSLexer *lexer, bool escaped, MacroPrototypeTail *tail);
|
|
32
|
+
static bool function_macro_tail_group(TSLexer *lexer, int32_t c, MacroPrototypeTail *tail);
|
|
33
|
+
static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type);
|
|
34
|
+
static bool function_macro_argument_quote(TSLexer *lexer, int32_t quote);
|
|
35
|
+
static bool function_macro_argument_escape(TSLexer *lexer, MacroArgumentWord *word);
|
|
36
|
+
static bool function_macro_argument_slash(TSLexer *lexer, bool allow_division);
|
|
37
|
+
static bool function_macro_tail_spacing(TSLexer *lexer, bool *escaped);
|
|
38
|
+
static bool function_macro_line_comment(TSLexer *lexer);
|
|
39
|
+
static bool function_macro_block_comment(TSLexer *lexer);
|
|
40
|
+
static void function_macro_argument_word(MacroArgumentWord *word, int32_t c);
|
|
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);
|
|
48
|
+
static bool function_macro_keyword(const char *word);
|
|
49
|
+
static bool function_macro_word_in(const char *word, const char *const *words, unsigned count);
|
|
50
|
+
|
|
51
|
+
static bool scan_function_attribute_guard(TSLexer *lexer, bool prototype, bool require_literal, bool trailing, const MacroAttributeSymbols *symbols) {
|
|
52
|
+
char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
|
|
53
|
+
MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true, .lookahead_only = !trailing};
|
|
54
|
+
if (!scan_macro_name(lexer, &name)) return false;
|
|
55
|
+
if (name.ascii && strcmp(word, pragma_word) == 0) {
|
|
56
|
+
lexer->result_symbol = symbols->pragma;
|
|
57
|
+
return symbols->allow_pragma && scan_pragma_suffix(lexer);
|
|
58
|
+
}
|
|
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;
|
|
65
|
+
if (!scan_pragma_spacing(lexer) || lexer->lookahead != '(') return false;
|
|
66
|
+
if (trailing) return function_macro_trailing_argument_prefix(lexer);
|
|
67
|
+
bool literal = false;
|
|
68
|
+
return function_macro_balanced(lexer, '(', ')', &literal) && (!require_literal || literal) &&
|
|
69
|
+
(!prototype || function_macro_declaration_continues(lexer, require_literal));
|
|
70
|
+
}
|
|
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
|
+
|
|
136
|
+
static bool function_macro_balanced(TSLexer *lexer, int32_t open, int32_t close, bool *literal) {
|
|
137
|
+
unsigned depth = 1;
|
|
138
|
+
unsigned nested = 0;
|
|
139
|
+
MacroArgumentWord word = {0};
|
|
140
|
+
lexer->advance(lexer, false);
|
|
141
|
+
while (depth) {
|
|
142
|
+
if (lexer->eof(lexer)) return false;
|
|
143
|
+
int32_t c = lexer->lookahead;
|
|
144
|
+
if (c == ';' || c == '{' || c == '#') return false;
|
|
145
|
+
function_macro_note_literal(literal, depth, nested, &word, c);
|
|
146
|
+
if (pragma_space(c)) {
|
|
147
|
+
lexer->advance(lexer, false);
|
|
148
|
+
word = (MacroArgumentWord){0};
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (c == '/') {
|
|
152
|
+
if (!function_macro_argument_slash(lexer, true)) return false;
|
|
153
|
+
word = (MacroArgumentWord){0};
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (c == '\\') {
|
|
157
|
+
if (!function_macro_argument_escape(lexer, &word)) return false;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (c == '"' || (c == '\'' && !word.number)) {
|
|
161
|
+
if (!function_macro_argument_quote(lexer, c)) return false;
|
|
162
|
+
word = (MacroArgumentWord){0};
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
function_macro_argument_word(&word, c);
|
|
166
|
+
if (!function_macro_group(c, open, close, &depth, &nested)) return false;
|
|
167
|
+
lexer->advance(lexer, false);
|
|
168
|
+
}
|
|
169
|
+
return nested == 0;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
static bool function_macro_group(int32_t c, int32_t open, int32_t close, unsigned *depth, unsigned *nested) {
|
|
173
|
+
if (c == open) (*depth)++;
|
|
174
|
+
else if (c == close) (*depth)--;
|
|
175
|
+
else if (c > 0 && c < 128 && strchr("([{", c)) (*nested)++;
|
|
176
|
+
else if (c > 0 && c < 128 && strchr(")]}", c)) {
|
|
177
|
+
if (!*nested) return false;
|
|
178
|
+
(*nested)--;
|
|
179
|
+
}
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
static void function_macro_note_literal(bool *literal, unsigned depth, unsigned nested, const MacroArgumentWord *word, int32_t c) {
|
|
184
|
+
if (!literal || depth != 1 || nested) return;
|
|
185
|
+
if (c == '"' || (c == '\'' && !word->number) || (c >= '0' && c <= '9' && !word->identifier)) *literal = true;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type) {
|
|
189
|
+
MacroPrototypeTail tail = {.post_type = post_type, .explicit_type = post_type};
|
|
190
|
+
for (;;) {
|
|
191
|
+
bool escaped = false;
|
|
192
|
+
if (!function_macro_tail_spacing(lexer, &escaped) || lexer->eof(lexer)) return false;
|
|
193
|
+
int32_t c = lexer->lookahead;
|
|
194
|
+
if (escaped || preproc_word(c, false)) {
|
|
195
|
+
if (!function_macro_tail_word(lexer, escaped, &tail)) return false;
|
|
196
|
+
} else if (c == ';') {
|
|
197
|
+
return tail.declarator_group && tail.explicit_type;
|
|
198
|
+
} else if (c <= 127 && strchr("{}#)]", c)) {
|
|
199
|
+
return false;
|
|
200
|
+
} else if (c == '(' || c == '[') {
|
|
201
|
+
if (!function_macro_tail_group(lexer, c, &tail)) return false;
|
|
202
|
+
} else if (c == '"' || c == '\'') {
|
|
203
|
+
if (!function_macro_argument_quote(lexer, c)) return false;
|
|
204
|
+
} else {
|
|
205
|
+
if (c == '*' && tail.post_type && tail.declarator_name && !tail.declarator_group) return false;
|
|
206
|
+
lexer->advance(lexer, false);
|
|
207
|
+
tail.modifier = false;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
static bool function_macro_tail_group(TSLexer *lexer, int32_t c, MacroPrototypeTail *tail) {
|
|
213
|
+
bool literal = false;
|
|
214
|
+
if (!function_macro_balanced(lexer, c, c == '(' ? ')' : ']', &literal)) return false;
|
|
215
|
+
if (c == '(' && !tail->modifier) {
|
|
216
|
+
if (literal && tail->post_type && !tail->declarator_group) tail->declarator_name = false;
|
|
217
|
+
else tail->declarator_group = true;
|
|
218
|
+
}
|
|
219
|
+
tail->modifier = false;
|
|
220
|
+
return true;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
static bool function_macro_tail_spacing(TSLexer *lexer, bool *escaped) {
|
|
224
|
+
for (;;) {
|
|
225
|
+
int32_t c = lexer->lookahead;
|
|
226
|
+
if (pragma_space(c)) {
|
|
227
|
+
lexer->advance(lexer, false);
|
|
228
|
+
} else if (c == '\\') {
|
|
229
|
+
lexer->advance(lexer, false);
|
|
230
|
+
if (scan_preproc_newline(lexer, false)) continue;
|
|
231
|
+
*escaped = lexer->lookahead == 'u' || lexer->lookahead == 'U';
|
|
232
|
+
return *escaped;
|
|
233
|
+
} else if (c == '/') {
|
|
234
|
+
if (!function_macro_argument_slash(lexer, false)) return false;
|
|
235
|
+
} else {
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
static bool function_macro_tail_word(TSLexer *lexer, bool escaped, MacroPrototypeTail *tail) {
|
|
242
|
+
char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
|
|
243
|
+
MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = !escaped};
|
|
244
|
+
if (escaped && !scan_macro_name_ucn(lexer)) return false;
|
|
245
|
+
bool continuation = escaped;
|
|
246
|
+
while (scan_macro_name_character(lexer, continuation, &name) == MACRO_NAME_CHARACTER) continuation = true;
|
|
247
|
+
bool pragma = name.ascii && strcmp(word, pragma_word) == 0;
|
|
248
|
+
bool keyword = name.ascii && function_macro_keyword(word);
|
|
249
|
+
if (tail->post_type && !tail->declarator_group && !keyword && !pragma) {
|
|
250
|
+
if (tail->declarator_name) return false;
|
|
251
|
+
tail->declarator_name = true;
|
|
252
|
+
}
|
|
253
|
+
tail->explicit_type |= name.ascii && function_macro_word_in(word, function_macro_type_keywords,
|
|
254
|
+
sizeof(function_macro_type_keywords) / sizeof(function_macro_type_keywords[0]));
|
|
255
|
+
tail->modifier = pragma || (name.ascii && function_macro_word_in(word, function_macro_group_modifiers,
|
|
256
|
+
sizeof(function_macro_group_modifiers) / sizeof(function_macro_group_modifiers[0])));
|
|
257
|
+
return !tail->post_type || !keyword ||
|
|
258
|
+
function_macro_word_in(word, function_macro_declaration_modifiers,
|
|
259
|
+
sizeof(function_macro_declaration_modifiers) / sizeof(function_macro_declaration_modifiers[0]));
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
static bool function_macro_argument_escape(TSLexer *lexer, MacroArgumentWord *word) {
|
|
263
|
+
lexer->advance(lexer, false);
|
|
264
|
+
if (scan_preproc_newline(lexer, false)) return true;
|
|
265
|
+
if (!scan_preproc_ucn(lexer, &word->number)) return false;
|
|
266
|
+
word->identifier = !word->number;
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
static bool function_macro_argument_quote(TSLexer *lexer, int32_t quote) {
|
|
271
|
+
lexer->advance(lexer, false);
|
|
272
|
+
while (!lexer->eof(lexer)) {
|
|
273
|
+
int32_t c = lexer->lookahead;
|
|
274
|
+
lexer->advance(lexer, false);
|
|
275
|
+
if (c == quote) return true;
|
|
276
|
+
if (c == '\n' || c == '\r') return false;
|
|
277
|
+
if (c != '\\') continue;
|
|
278
|
+
if (scan_preproc_newline(lexer, false)) continue;
|
|
279
|
+
if (!scan_preproc_splices(lexer)) continue;
|
|
280
|
+
if (lexer->eof(lexer)) return false;
|
|
281
|
+
lexer->advance(lexer, false);
|
|
282
|
+
}
|
|
283
|
+
return false;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
static bool function_macro_argument_slash(TSLexer *lexer, bool allow_division) {
|
|
287
|
+
lexer->advance(lexer, false);
|
|
288
|
+
if (!scan_preproc_splices(lexer)) return allow_division;
|
|
289
|
+
if (lexer->lookahead == '/') return function_macro_line_comment(lexer);
|
|
290
|
+
if (lexer->lookahead == '*') return function_macro_block_comment(lexer);
|
|
291
|
+
return allow_division;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
static bool function_macro_line_comment(TSLexer *lexer) {
|
|
295
|
+
lexer->advance(lexer, false);
|
|
296
|
+
while (!lexer->eof(lexer) && lexer->lookahead != '\n' && lexer->lookahead != '\r') {
|
|
297
|
+
if (lexer->lookahead == '\\') {
|
|
298
|
+
do {
|
|
299
|
+
lexer->advance(lexer, false);
|
|
300
|
+
} while (lexer->lookahead == '\\');
|
|
301
|
+
if (scan_preproc_newline(lexer, false)) continue;
|
|
302
|
+
if (lexer->eof(lexer)) return false;
|
|
303
|
+
}
|
|
304
|
+
lexer->advance(lexer, false);
|
|
305
|
+
}
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
static bool function_macro_block_comment(TSLexer *lexer) {
|
|
310
|
+
lexer->advance(lexer, false);
|
|
311
|
+
bool star = false;
|
|
312
|
+
while (!lexer->eof(lexer)) {
|
|
313
|
+
int32_t c = lexer->lookahead;
|
|
314
|
+
lexer->advance(lexer, false);
|
|
315
|
+
if (c == '\\' && scan_preproc_newline(lexer, false)) continue;
|
|
316
|
+
if (star && c == '/') return true;
|
|
317
|
+
star = c == '*';
|
|
318
|
+
}
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
static void function_macro_argument_word(MacroArgumentWord *word, int32_t c) {
|
|
323
|
+
bool member = preproc_word(c, word->identifier || word->number);
|
|
324
|
+
bool digit = c >= '0' && c <= '9';
|
|
325
|
+
bool exponent_sign = word->number && (c == '+' || c == '-') &&
|
|
326
|
+
(word->last == 'e' || word->last == 'E' || word->last == 'p' || word->last == 'P');
|
|
327
|
+
if (c == '.') word->identifier = false;
|
|
328
|
+
if (!member && !digit && c != '.' && c != '\'' && !exponent_sign) *word = (MacroArgumentWord){0};
|
|
329
|
+
if (digit && !word->identifier) word->number = true;
|
|
330
|
+
if (member && !word->number) word->identifier = true;
|
|
331
|
+
if (word->number) word->last = c;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
static bool function_macro_keyword(const char *word) {
|
|
335
|
+
return function_macro_word_in(word, function_macro_keywords,
|
|
336
|
+
sizeof(function_macro_keywords) / sizeof(function_macro_keywords[0]));
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
static bool function_macro_word_in(const char *word, const char *const *words, unsigned count) {
|
|
340
|
+
unsigned start = 0;
|
|
341
|
+
unsigned end = count;
|
|
342
|
+
while (start < end) {
|
|
343
|
+
unsigned middle = start + (end - start) / 2;
|
|
344
|
+
int order = strcmp(word, words[middle]);
|
|
345
|
+
if (order == 0) return true;
|
|
346
|
+
if (order < 0) end = middle;
|
|
347
|
+
else start = middle + 1;
|
|
348
|
+
}
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
#endif
|
package/src/identifier.h
CHANGED
package/src/keywords.h
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
// Generated by script/generateIdentifierRanges.js from standalone grammar token words.
|
|
2
|
+
#ifndef TREE_SITTER_C_KEYWORDS_H_
|
|
3
|
+
#define TREE_SITTER_C_KEYWORDS_H_
|
|
4
|
+
|
|
5
|
+
#define FUNCTION_MACRO_KEYWORD_MAX_LENGTH 19
|
|
6
|
+
static const char *const function_macro_keywords[] = {
|
|
7
|
+
"NULL",
|
|
8
|
+
"_Alignas",
|
|
9
|
+
"_Alignof",
|
|
10
|
+
"_Atomic",
|
|
11
|
+
"_BitInt",
|
|
12
|
+
"_Complex",
|
|
13
|
+
"_Countof",
|
|
14
|
+
"_Generic",
|
|
15
|
+
"_Imaginary",
|
|
16
|
+
"_Nonnull",
|
|
17
|
+
"_Noreturn",
|
|
18
|
+
"_Thread_local",
|
|
19
|
+
"__alignof",
|
|
20
|
+
"__alignof__",
|
|
21
|
+
"__asm",
|
|
22
|
+
"__asm__",
|
|
23
|
+
"__attribute",
|
|
24
|
+
"__attribute__",
|
|
25
|
+
"__based",
|
|
26
|
+
"__builtin_available",
|
|
27
|
+
"__builtin_va_arg",
|
|
28
|
+
"__cdecl",
|
|
29
|
+
"__clrcall",
|
|
30
|
+
"__complex__",
|
|
31
|
+
"__declspec",
|
|
32
|
+
"__except",
|
|
33
|
+
"__extension__",
|
|
34
|
+
"__fastcall",
|
|
35
|
+
"__finally",
|
|
36
|
+
"__forceinline",
|
|
37
|
+
"__inline",
|
|
38
|
+
"__inline__",
|
|
39
|
+
"__leave",
|
|
40
|
+
"__ptr32",
|
|
41
|
+
"__ptr64",
|
|
42
|
+
"__restrict",
|
|
43
|
+
"__restrict__",
|
|
44
|
+
"__sptr",
|
|
45
|
+
"__stdcall",
|
|
46
|
+
"__thiscall",
|
|
47
|
+
"__thread",
|
|
48
|
+
"__try",
|
|
49
|
+
"__typeof",
|
|
50
|
+
"__typeof__",
|
|
51
|
+
"__typeof_unqual",
|
|
52
|
+
"__typeof_unqual__",
|
|
53
|
+
"__unaligned",
|
|
54
|
+
"__uptr",
|
|
55
|
+
"__vectorcall",
|
|
56
|
+
"__volatile__",
|
|
57
|
+
"_alignof",
|
|
58
|
+
"_unaligned",
|
|
59
|
+
"alignas",
|
|
60
|
+
"alignof",
|
|
61
|
+
"asm",
|
|
62
|
+
"auto",
|
|
63
|
+
"bool",
|
|
64
|
+
"break",
|
|
65
|
+
"case",
|
|
66
|
+
"char",
|
|
67
|
+
"char16_t",
|
|
68
|
+
"char32_t",
|
|
69
|
+
"char64_t",
|
|
70
|
+
"char8_t",
|
|
71
|
+
"charptr_t",
|
|
72
|
+
"const",
|
|
73
|
+
"constexpr",
|
|
74
|
+
"continue",
|
|
75
|
+
"default",
|
|
76
|
+
"defined",
|
|
77
|
+
"do",
|
|
78
|
+
"double",
|
|
79
|
+
"else",
|
|
80
|
+
"enum",
|
|
81
|
+
"extern",
|
|
82
|
+
"false",
|
|
83
|
+
"float",
|
|
84
|
+
"for",
|
|
85
|
+
"goto",
|
|
86
|
+
"if",
|
|
87
|
+
"inline",
|
|
88
|
+
"int",
|
|
89
|
+
"int16_t",
|
|
90
|
+
"int32_t",
|
|
91
|
+
"int64_t",
|
|
92
|
+
"int8_t",
|
|
93
|
+
"intptr_t",
|
|
94
|
+
"long",
|
|
95
|
+
"max_align_t",
|
|
96
|
+
"noreturn",
|
|
97
|
+
"nullptr",
|
|
98
|
+
"nullptr_t",
|
|
99
|
+
"offsetof",
|
|
100
|
+
"ptrdiff_t",
|
|
101
|
+
"register",
|
|
102
|
+
"restrict",
|
|
103
|
+
"return",
|
|
104
|
+
"short",
|
|
105
|
+
"signed",
|
|
106
|
+
"size_t",
|
|
107
|
+
"sizeof",
|
|
108
|
+
"ssize_t",
|
|
109
|
+
"static",
|
|
110
|
+
"struct",
|
|
111
|
+
"switch",
|
|
112
|
+
"thread_local",
|
|
113
|
+
"true",
|
|
114
|
+
"typedef",
|
|
115
|
+
"typeof",
|
|
116
|
+
"typeof_unqual",
|
|
117
|
+
"uint16_t",
|
|
118
|
+
"uint32_t",
|
|
119
|
+
"uint64_t",
|
|
120
|
+
"uint8_t",
|
|
121
|
+
"uintptr_t",
|
|
122
|
+
"union",
|
|
123
|
+
"unsigned",
|
|
124
|
+
"va_arg",
|
|
125
|
+
"void",
|
|
126
|
+
"volatile",
|
|
127
|
+
"while",
|
|
128
|
+
};
|
|
129
|
+
static const char *const function_macro_group_modifiers[] = {
|
|
130
|
+
"_Alignas",
|
|
131
|
+
"_Atomic",
|
|
132
|
+
"_BitInt",
|
|
133
|
+
"__asm",
|
|
134
|
+
"__asm__",
|
|
135
|
+
"__attribute",
|
|
136
|
+
"__attribute__",
|
|
137
|
+
"__declspec",
|
|
138
|
+
"__typeof",
|
|
139
|
+
"__typeof__",
|
|
140
|
+
"__typeof_unqual",
|
|
141
|
+
"__typeof_unqual__",
|
|
142
|
+
"alignas",
|
|
143
|
+
"asm",
|
|
144
|
+
"typeof",
|
|
145
|
+
"typeof_unqual",
|
|
146
|
+
};
|
|
147
|
+
static const char *const function_macro_type_keywords[] = {
|
|
148
|
+
"_Atomic",
|
|
149
|
+
"_BitInt",
|
|
150
|
+
"_Complex",
|
|
151
|
+
"_Imaginary",
|
|
152
|
+
"__complex__",
|
|
153
|
+
"__typeof",
|
|
154
|
+
"__typeof__",
|
|
155
|
+
"__typeof_unqual",
|
|
156
|
+
"__typeof_unqual__",
|
|
157
|
+
"bool",
|
|
158
|
+
"char",
|
|
159
|
+
"char16_t",
|
|
160
|
+
"char32_t",
|
|
161
|
+
"char64_t",
|
|
162
|
+
"char8_t",
|
|
163
|
+
"charptr_t",
|
|
164
|
+
"double",
|
|
165
|
+
"enum",
|
|
166
|
+
"float",
|
|
167
|
+
"int",
|
|
168
|
+
"int16_t",
|
|
169
|
+
"int32_t",
|
|
170
|
+
"int64_t",
|
|
171
|
+
"int8_t",
|
|
172
|
+
"intptr_t",
|
|
173
|
+
"long",
|
|
174
|
+
"max_align_t",
|
|
175
|
+
"nullptr_t",
|
|
176
|
+
"ptrdiff_t",
|
|
177
|
+
"short",
|
|
178
|
+
"signed",
|
|
179
|
+
"size_t",
|
|
180
|
+
"ssize_t",
|
|
181
|
+
"struct",
|
|
182
|
+
"typeof",
|
|
183
|
+
"typeof_unqual",
|
|
184
|
+
"uint16_t",
|
|
185
|
+
"uint32_t",
|
|
186
|
+
"uint64_t",
|
|
187
|
+
"uint8_t",
|
|
188
|
+
"uintptr_t",
|
|
189
|
+
"union",
|
|
190
|
+
"unsigned",
|
|
191
|
+
"void",
|
|
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
|
+
};
|
|
271
|
+
static const char *const function_macro_declaration_modifiers[] = {
|
|
272
|
+
"_Alignas",
|
|
273
|
+
"_Atomic",
|
|
274
|
+
"_Nonnull",
|
|
275
|
+
"_Noreturn",
|
|
276
|
+
"__asm",
|
|
277
|
+
"__asm__",
|
|
278
|
+
"__attribute",
|
|
279
|
+
"__attribute__",
|
|
280
|
+
"__cdecl",
|
|
281
|
+
"__clrcall",
|
|
282
|
+
"__declspec",
|
|
283
|
+
"__extension__",
|
|
284
|
+
"__fastcall",
|
|
285
|
+
"__restrict__",
|
|
286
|
+
"__stdcall",
|
|
287
|
+
"__thiscall",
|
|
288
|
+
"__vectorcall",
|
|
289
|
+
"alignas",
|
|
290
|
+
"asm",
|
|
291
|
+
"const",
|
|
292
|
+
"constexpr",
|
|
293
|
+
"noreturn",
|
|
294
|
+
"restrict",
|
|
295
|
+
"volatile",
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
#endif
|
package/src/node-types.json
CHANGED
|
@@ -546,6 +546,10 @@
|
|
|
546
546
|
{
|
|
547
547
|
"type": "system_lib_string",
|
|
548
548
|
"named": true
|
|
549
|
+
},
|
|
550
|
+
{
|
|
551
|
+
"type": "type_descriptor",
|
|
552
|
+
"named": true
|
|
549
553
|
}
|
|
550
554
|
]
|
|
551
555
|
}
|
|
@@ -764,10 +768,31 @@
|
|
|
764
768
|
{
|
|
765
769
|
"type": "attribute_specifier",
|
|
766
770
|
"named": true,
|
|
767
|
-
"fields": {
|
|
771
|
+
"fields": {
|
|
772
|
+
"arguments": {
|
|
773
|
+
"multiple": false,
|
|
774
|
+
"required": false,
|
|
775
|
+
"types": [
|
|
776
|
+
{
|
|
777
|
+
"type": "argument_list",
|
|
778
|
+
"named": true
|
|
779
|
+
}
|
|
780
|
+
]
|
|
781
|
+
},
|
|
782
|
+
"name": {
|
|
783
|
+
"multiple": false,
|
|
784
|
+
"required": false,
|
|
785
|
+
"types": [
|
|
786
|
+
{
|
|
787
|
+
"type": "identifier",
|
|
788
|
+
"named": true
|
|
789
|
+
}
|
|
790
|
+
]
|
|
791
|
+
}
|
|
792
|
+
},
|
|
768
793
|
"children": {
|
|
769
794
|
"multiple": false,
|
|
770
|
-
"required":
|
|
795
|
+
"required": false,
|
|
771
796
|
"types": [
|
|
772
797
|
{
|
|
773
798
|
"type": "argument_list",
|
package/src/pragma.h
CHANGED
|
@@ -3,9 +3,30 @@
|
|
|
3
3
|
|
|
4
4
|
#include "tree_sitter/parser.h"
|
|
5
5
|
#include "identifier.h"
|
|
6
|
+
#include <string.h>
|
|
6
7
|
|
|
7
8
|
static const char pragma_word[] = "_Pragma";
|
|
8
9
|
|
|
10
|
+
typedef struct {
|
|
11
|
+
char *word;
|
|
12
|
+
unsigned capacity;
|
|
13
|
+
unsigned length;
|
|
14
|
+
bool ascii;
|
|
15
|
+
bool lookahead_only;
|
|
16
|
+
} MacroName;
|
|
17
|
+
|
|
18
|
+
typedef enum {
|
|
19
|
+
MACRO_NAME_END,
|
|
20
|
+
MACRO_NAME_CHARACTER,
|
|
21
|
+
MACRO_NAME_SPLICE,
|
|
22
|
+
MACRO_NAME_INVALID,
|
|
23
|
+
} MacroNameCharacter;
|
|
24
|
+
|
|
25
|
+
static bool scan_macro_name(TSLexer *lexer, MacroName *name);
|
|
26
|
+
static void scan_macro_name_start(TSLexer *lexer, bool lookahead_only);
|
|
27
|
+
static MacroNameCharacter scan_macro_name_character(TSLexer *lexer, bool continuation, MacroName *name);
|
|
28
|
+
static bool scan_macro_name_ucn(TSLexer *lexer);
|
|
29
|
+
|
|
9
30
|
static bool scan_pragma_spacing(TSLexer *lexer);
|
|
10
31
|
static bool scan_pragma_suffix(TSLexer *lexer);
|
|
11
32
|
static bool pragma_space(int32_t c);
|
|
@@ -334,48 +355,68 @@ static bool pragma_space(int32_t c) {
|
|
|
334
355
|
}
|
|
335
356
|
|
|
336
357
|
static bool scan_function_macro_name(TSLexer *lexer, bool allow_pragma, TSSymbol pragma_symbol) {
|
|
358
|
+
char word[sizeof(pragma_word)] = {0};
|
|
359
|
+
MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true};
|
|
360
|
+
if (!scan_macro_name(lexer, &name)) return false;
|
|
361
|
+
bool adjacent = lexer->lookahead == '(';
|
|
362
|
+
if (allow_pragma && name.ascii && strcmp(word, pragma_word) == 0 && scan_pragma_suffix(lexer)) {
|
|
363
|
+
lexer->result_symbol = pragma_symbol;
|
|
364
|
+
return true;
|
|
365
|
+
}
|
|
366
|
+
return adjacent;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
static bool scan_macro_name(TSLexer *lexer, MacroName *name) {
|
|
337
370
|
bool has_name = false;
|
|
338
|
-
bool is_pragma = true;
|
|
339
|
-
unsigned pragma_length = 0;
|
|
340
371
|
for (;;) {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
if (
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
break;
|
|
349
|
-
}
|
|
350
|
-
is_pragma = false;
|
|
351
|
-
unsigned digits = lexer->lookahead == 'u' ? 4 : lexer->lookahead == 'U' ? 8 : 0;
|
|
352
|
-
if (!digits) return false;
|
|
353
|
-
lexer->advance(lexer, false);
|
|
354
|
-
for (unsigned i = 0; i < digits; i++) {
|
|
355
|
-
c = lexer->lookahead;
|
|
356
|
-
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) return false;
|
|
357
|
-
lexer->advance(lexer, false);
|
|
358
|
-
}
|
|
359
|
-
} else {
|
|
360
|
-
bool identifier = has_name
|
|
361
|
-
? set_contains(preproc_identifier_continue, sizeof(preproc_identifier_continue) / sizeof(TSCharacterRange), c)
|
|
362
|
-
: set_contains(preproc_identifier_start, sizeof(preproc_identifier_start) / sizeof(TSCharacterRange), c);
|
|
363
|
-
if (!identifier) break;
|
|
364
|
-
if (is_pragma) {
|
|
365
|
-
if (pragma_length < sizeof(pragma_word) - 1 && c == pragma_word[pragma_length]) pragma_length++;
|
|
366
|
-
else is_pragma = false;
|
|
367
|
-
}
|
|
368
|
-
lexer->advance(lexer, false);
|
|
372
|
+
if (!has_name) scan_macro_name_start(lexer, name->lookahead_only);
|
|
373
|
+
MacroNameCharacter character = scan_macro_name_character(lexer, has_name, name);
|
|
374
|
+
if (character == MACRO_NAME_INVALID) return false;
|
|
375
|
+
if (character == MACRO_NAME_END) return has_name;
|
|
376
|
+
if (character == MACRO_NAME_SPLICE) {
|
|
377
|
+
if (!has_name) continue;
|
|
378
|
+
return scan_preproc_splices(lexer);
|
|
369
379
|
}
|
|
370
380
|
has_name = true;
|
|
371
|
-
lexer->mark_end(lexer);
|
|
381
|
+
if (!name->lookahead_only) lexer->mark_end(lexer);
|
|
372
382
|
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
static void scan_macro_name_start(TSLexer *lexer, bool lookahead_only) {
|
|
386
|
+
while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
|
|
387
|
+
if (lookahead_only) lexer->mark_end(lexer);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
static MacroNameCharacter scan_macro_name_character(TSLexer *lexer, bool continuation, MacroName *name) {
|
|
391
|
+
int32_t c = lexer->lookahead;
|
|
392
|
+
if (c == '\\') {
|
|
393
|
+
lexer->advance(lexer, false);
|
|
394
|
+
if (scan_preproc_newline(lexer, !continuation)) return MACRO_NAME_SPLICE;
|
|
395
|
+
name->ascii = false;
|
|
396
|
+
return scan_macro_name_ucn(lexer) ? MACRO_NAME_CHARACTER : MACRO_NAME_INVALID;
|
|
377
397
|
}
|
|
378
|
-
|
|
398
|
+
bool accepted = continuation
|
|
399
|
+
? set_contains(preproc_identifier_continue, sizeof(preproc_identifier_continue) / sizeof(TSCharacterRange), c)
|
|
400
|
+
: set_contains(preproc_identifier_start, sizeof(preproc_identifier_start) / sizeof(TSCharacterRange), c);
|
|
401
|
+
if (!accepted) return MACRO_NAME_END;
|
|
402
|
+
if (name->ascii) {
|
|
403
|
+
if (c < 128 && name->length < name->capacity) name->word[name->length++] = (char)c;
|
|
404
|
+
else name->ascii = false;
|
|
405
|
+
}
|
|
406
|
+
lexer->advance(lexer, false);
|
|
407
|
+
return MACRO_NAME_CHARACTER;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
static bool scan_macro_name_ucn(TSLexer *lexer) {
|
|
411
|
+
unsigned digits = lexer->lookahead == 'u' ? 4 : lexer->lookahead == 'U' ? 8 : 0;
|
|
412
|
+
if (!digits) return false;
|
|
413
|
+
lexer->advance(lexer, false);
|
|
414
|
+
for (unsigned i = 0; i < digits; i++) {
|
|
415
|
+
int32_t c = lexer->lookahead;
|
|
416
|
+
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) return false;
|
|
417
|
+
lexer->advance(lexer, false);
|
|
418
|
+
}
|
|
419
|
+
return true;
|
|
379
420
|
}
|
|
380
421
|
|
|
381
422
|
#endif
|
package/src/scanner.c
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
#include "
|
|
1
|
+
#include "functionMacro.h"
|
|
2
2
|
|
|
3
|
-
enum TokenType { PRAGMA_OPERATOR, PREPROC_ARG, PREPROC_NEWLINE, PREPROC_LPAREN, PREPROC_DIRECTIVE_ARG, PREPROC_FUNCTION_NAME };
|
|
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
|
+
|
|
5
|
+
static bool scan_preproc_tokens(TSLexer *lexer, const bool *valid_symbols);
|
|
4
6
|
|
|
5
7
|
void *tree_sitter_c_external_scanner_create(void) {
|
|
6
8
|
return NULL;
|
|
@@ -28,6 +30,32 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
|
|
|
28
30
|
lexer->result_symbol = PREPROC_FUNCTION_NAME;
|
|
29
31
|
return scan_function_macro_name(lexer, valid_symbols[PRAGMA_OPERATOR], PRAGMA_OPERATOR);
|
|
30
32
|
}
|
|
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);
|
|
54
|
+
}
|
|
55
|
+
return scan_preproc_tokens(lexer, valid_symbols);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static bool scan_preproc_tokens(TSLexer *lexer, const bool *valid_symbols) {
|
|
31
59
|
bool directive_text = !valid_symbols[PREPROC_ARG] && valid_symbols[PREPROC_DIRECTIVE_ARG];
|
|
32
60
|
TSSymbol argument_symbol = directive_text ? PREPROC_DIRECTIVE_ARG : PREPROC_ARG;
|
|
33
61
|
bool argument_valid = valid_symbols[PREPROC_ARG] || directive_text;
|
package/tree-sitter-c.wasm
CHANGED
|
Binary file
|