@willbooster/tree-sitter-c 12.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 +71 -10
- package/package.json +7 -6
- package/src/conditionalMacro.h +208 -0
- package/src/keywords.h +1 -0
- package/src/node-types.json +24 -0
- 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,6 +71,7 @@ module.exports = Object.assign(
|
|
|
71
71
|
name: 'c',
|
|
72
72
|
|
|
73
73
|
conflicts: ($) => [
|
|
74
|
+
[$._declaration_modifiers, $._empty_declaration],
|
|
74
75
|
[$._statement_declaration, $._statement_declaration_modifiers],
|
|
75
76
|
[$.declaration, $._declaration_modifiers],
|
|
76
77
|
[$._statement_declaration, $._statement_type_definition, $._statement_declaration_modifiers],
|
|
@@ -186,6 +187,10 @@ module.exports = Object.assign(
|
|
|
186
187
|
sym('_post_type_function_attribute_suffix'),
|
|
187
188
|
sym('_trailing_macro_attribute_name'),
|
|
188
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'),
|
|
189
194
|
],
|
|
190
195
|
|
|
191
196
|
extras: ($) => [$.pragma_operator, /\s|\\(?:\r\n?|\n\r?)/, $.comment],
|
|
@@ -231,10 +236,13 @@ module.exports = Object.assign(
|
|
|
231
236
|
choice(
|
|
232
237
|
$.function_definition,
|
|
233
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),
|
|
234
241
|
$.linkage_specification,
|
|
235
242
|
$.declaration,
|
|
236
243
|
alias(sym('_macro_modified_declaration'), $.declaration),
|
|
237
244
|
alias(sym('_trailing_macro_modified_declaration'), $.declaration),
|
|
245
|
+
alias(sym('_conditional_declaration'), $.declaration),
|
|
238
246
|
$._top_level_statement,
|
|
239
247
|
$.attributed_statement,
|
|
240
248
|
$.type_definition,
|
|
@@ -268,10 +276,13 @@ module.exports = Object.assign(
|
|
|
268
276
|
choice(
|
|
269
277
|
$.function_definition,
|
|
270
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),
|
|
271
281
|
$.linkage_specification,
|
|
272
282
|
$.declaration,
|
|
273
283
|
alias(sym('_macro_modified_declaration'), $.declaration),
|
|
274
284
|
alias(sym('_trailing_macro_modified_declaration'), $.declaration),
|
|
285
|
+
alias(sym('_conditional_declaration'), $.declaration),
|
|
275
286
|
$.statement,
|
|
276
287
|
$.type_definition,
|
|
277
288
|
$._empty_declaration,
|
|
@@ -470,12 +481,27 @@ module.exports = Object.assign(
|
|
|
470
481
|
'*',
|
|
471
482
|
repeat($.attribute_declaration),
|
|
472
483
|
repeat($.ms_pointer_modifier),
|
|
473
|
-
repeat(
|
|
484
|
+
repeat(
|
|
485
|
+
choice($.type_qualifier, $.attribute_specifier, $.macro_specifier, $._conditional_macro_specifier)
|
|
486
|
+
),
|
|
474
487
|
field('declarator', alias($._old_style_declarator, $._declarator))
|
|
475
488
|
)
|
|
476
489
|
)
|
|
477
490
|
),
|
|
478
491
|
|
|
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
|
+
|
|
479
505
|
declaration: ($) => declarationRule($, $._declaration_specifiers, $._declaration_modifiers),
|
|
480
506
|
_statement_declaration: ($) =>
|
|
481
507
|
declarationRule($, $._statement_declaration_specifiers, $._statement_declaration_modifiers),
|
|
@@ -492,6 +518,9 @@ module.exports = Object.assign(
|
|
|
492
518
|
$._macro_declaration_declarator
|
|
493
519
|
),
|
|
494
520
|
|
|
521
|
+
_conditional_declaration: ($) =>
|
|
522
|
+
declarationRule($, sym('_conditional_function_specifiers'), $._non_macro_declaration_modifiers, false),
|
|
523
|
+
|
|
495
524
|
_trailing_macro_modified_declaration: ($) =>
|
|
496
525
|
seq(
|
|
497
526
|
sym('_trailing_object_declaration_start'),
|
|
@@ -536,13 +565,14 @@ module.exports = Object.assign(
|
|
|
536
565
|
|
|
537
566
|
_type_definition_type: ($) =>
|
|
538
567
|
seq(
|
|
539
|
-
repeat(choice($.type_qualifier, $.macro_specifier)),
|
|
568
|
+
repeat(choice($.type_qualifier, $.macro_specifier, $._leading_conditional_macro_specifier)),
|
|
540
569
|
field('type', $.type_specifier),
|
|
541
570
|
repeat($.type_qualifier)
|
|
542
571
|
),
|
|
543
572
|
_type_definition_declarators: ($) => commaSep1(field('declarator', $._type_declarator)),
|
|
544
573
|
|
|
545
|
-
_declaration_modifiers: ($) =>
|
|
574
|
+
_declaration_modifiers: ($) =>
|
|
575
|
+
choice($._non_macro_declaration_modifiers, $.macro_specifier, $._leading_conditional_macro_specifier),
|
|
546
576
|
|
|
547
577
|
_statement_declaration_modifiers: ($) => $._non_macro_declaration_modifiers,
|
|
548
578
|
|
|
@@ -700,7 +730,9 @@ module.exports = Object.assign(
|
|
|
700
730
|
'*',
|
|
701
731
|
repeat($.attribute_declaration),
|
|
702
732
|
repeat($.ms_pointer_modifier),
|
|
703
|
-
repeat(
|
|
733
|
+
repeat(
|
|
734
|
+
choice($.type_qualifier, $.attribute_specifier, $.macro_specifier, $._conditional_macro_specifier)
|
|
735
|
+
),
|
|
704
736
|
field('declarator', $._field_declarator)
|
|
705
737
|
)
|
|
706
738
|
)
|
|
@@ -715,7 +747,9 @@ module.exports = Object.assign(
|
|
|
715
747
|
'*',
|
|
716
748
|
repeat($.attribute_declaration),
|
|
717
749
|
repeat($.ms_pointer_modifier),
|
|
718
|
-
repeat(
|
|
750
|
+
repeat(
|
|
751
|
+
choice($.type_qualifier, $.attribute_specifier, $.macro_specifier, $._conditional_macro_specifier)
|
|
752
|
+
),
|
|
719
753
|
field('declarator', $._type_declarator)
|
|
720
754
|
)
|
|
721
755
|
)
|
|
@@ -1680,11 +1714,24 @@ module.exports = Object.assign(
|
|
|
1680
1714
|
_field_identifier: ($) => alias(choice($.identifier, VA_ARG_KEYWORDS), sym('field_identifier')),
|
|
1681
1715
|
_statement_identifier: ($) => alias(choice($.identifier, VA_ARG_KEYWORDS), sym('statement_identifier')),
|
|
1682
1716
|
|
|
1683
|
-
_empty_declaration: ($) =>
|
|
1717
|
+
_empty_declaration: ($) =>
|
|
1718
|
+
seq(
|
|
1719
|
+
repeat(choice($.ms_declspec_modifier, $.macro_specifier, $._leading_conditional_macro_specifier)),
|
|
1720
|
+
$.type_specifier,
|
|
1721
|
+
';'
|
|
1722
|
+
),
|
|
1684
1723
|
_statement_empty_declaration: ($) => seq(repeat($.ms_declspec_modifier), $.type_specifier, ';'),
|
|
1685
1724
|
|
|
1686
1725
|
macro_specifier: ($) => prec.dynamic(-2, field('name', $.identifier)),
|
|
1687
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')),
|
|
1734
|
+
|
|
1688
1735
|
macro_type_specifier: ($) =>
|
|
1689
1736
|
prec.dynamic(
|
|
1690
1737
|
-1,
|
|
@@ -1706,7 +1753,21 @@ module.exports = Object.assign(
|
|
|
1706
1753
|
);
|
|
1707
1754
|
|
|
1708
1755
|
/**
|
|
1709
|
-
* @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'>} $
|
|
1710
1771
|
* @param {import('./types/treeSitterDsl').Rule} declarator
|
|
1711
1772
|
*/
|
|
1712
1773
|
function pointerDeclaratorRule($, declarator) {
|
|
@@ -1717,7 +1778,7 @@ function pointerDeclaratorRule($, declarator) {
|
|
|
1717
1778
|
'*',
|
|
1718
1779
|
repeat($.attribute_declaration),
|
|
1719
1780
|
repeat($.ms_pointer_modifier),
|
|
1720
|
-
repeat(choice($.type_qualifier, $.attribute_specifier, $.macro_specifier)),
|
|
1781
|
+
repeat(choice($.type_qualifier, $.attribute_specifier, $.macro_specifier, $._conditional_macro_specifier)),
|
|
1721
1782
|
field('declarator', declarator)
|
|
1722
1783
|
)
|
|
1723
1784
|
);
|
|
@@ -1776,14 +1837,14 @@ function declarationRule($, specifiers, modifiers, allowInferredType = true, dec
|
|
|
1776
1837
|
}
|
|
1777
1838
|
|
|
1778
1839
|
/**
|
|
1779
|
-
* @param {import('./types/treeSitterDsl').GrammarSymbols<'macro_specifier' | 'type_qualifier' | '_type_definition_type' | '_non_identifier_type_specifier' | '_type_definition_declarators' | 'attribute_specifier'>} $
|
|
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'>} $
|
|
1780
1841
|
* @param {import('./types/treeSitterDsl').Rule} modifiers
|
|
1781
1842
|
* @param {boolean} allowPrefixMarkers
|
|
1782
1843
|
*/
|
|
1783
1844
|
function typeDefinitionRule($, modifiers, allowPrefixMarkers) {
|
|
1784
1845
|
return seq(
|
|
1785
1846
|
optional(prec.dynamic(1, '__extension__')),
|
|
1786
|
-
...(allowPrefixMarkers ? [repeat($.macro_specifier)] : []),
|
|
1847
|
+
...(allowPrefixMarkers ? [repeat(choice($.macro_specifier, $._leading_conditional_macro_specifier))] : []),
|
|
1787
1848
|
choice(
|
|
1788
1849
|
seq(repeat($.type_qualifier), 'typedef', $._type_definition_type),
|
|
1789
1850
|
seq(
|
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
|
@@ -1456,6 +1456,10 @@
|
|
|
1456
1456
|
"type": "ms_declspec_modifier",
|
|
1457
1457
|
"named": true
|
|
1458
1458
|
},
|
|
1459
|
+
{
|
|
1460
|
+
"type": "preproc_ifdef",
|
|
1461
|
+
"named": true
|
|
1462
|
+
},
|
|
1459
1463
|
{
|
|
1460
1464
|
"type": "storage_class_specifier",
|
|
1461
1465
|
"named": true
|
|
@@ -1825,6 +1829,10 @@
|
|
|
1825
1829
|
"type": "ms_declspec_modifier",
|
|
1826
1830
|
"named": true
|
|
1827
1831
|
},
|
|
1832
|
+
{
|
|
1833
|
+
"type": "preproc_ifdef",
|
|
1834
|
+
"named": true
|
|
1835
|
+
},
|
|
1828
1836
|
{
|
|
1829
1837
|
"type": "storage_class_specifier",
|
|
1830
1838
|
"named": true
|
|
@@ -2111,6 +2119,10 @@
|
|
|
2111
2119
|
"type": "ms_declspec_modifier",
|
|
2112
2120
|
"named": true
|
|
2113
2121
|
},
|
|
2122
|
+
{
|
|
2123
|
+
"type": "preproc_ifdef",
|
|
2124
|
+
"named": true
|
|
2125
|
+
},
|
|
2114
2126
|
{
|
|
2115
2127
|
"type": "storage_class_specifier",
|
|
2116
2128
|
"named": true
|
|
@@ -2801,6 +2813,10 @@
|
|
|
2801
2813
|
"type": "ms_declspec_modifier",
|
|
2802
2814
|
"named": true
|
|
2803
2815
|
},
|
|
2816
|
+
{
|
|
2817
|
+
"type": "preproc_ifdef",
|
|
2818
|
+
"named": true
|
|
2819
|
+
},
|
|
2804
2820
|
{
|
|
2805
2821
|
"type": "storage_class_specifier",
|
|
2806
2822
|
"named": true
|
|
@@ -2970,6 +2986,10 @@
|
|
|
2970
2986
|
"type": "ms_pointer_modifier",
|
|
2971
2987
|
"named": true
|
|
2972
2988
|
},
|
|
2989
|
+
{
|
|
2990
|
+
"type": "preproc_ifdef",
|
|
2991
|
+
"named": true
|
|
2992
|
+
},
|
|
2973
2993
|
{
|
|
2974
2994
|
"type": "type_qualifier",
|
|
2975
2995
|
"named": true
|
|
@@ -4357,6 +4377,10 @@
|
|
|
4357
4377
|
"type": "ms_declspec_modifier",
|
|
4358
4378
|
"named": true
|
|
4359
4379
|
},
|
|
4380
|
+
{
|
|
4381
|
+
"type": "preproc_ifdef",
|
|
4382
|
+
"named": true
|
|
4383
|
+
},
|
|
4360
4384
|
{
|
|
4361
4385
|
"type": "primitive_type",
|
|
4362
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
|