@willbooster/tree-sitter-c 9.0.1 → 10.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 +22 -5
- package/grammar.js +167 -50
- package/package.json +3 -1
- package/src/functionMacro.h +269 -0
- package/src/identifier.h +1 -1
- package/src/keywords.h +220 -0
- package/src/node-types.json +27 -2
- package/src/pragma.h +77 -36
- package/src/scanner.c +16 -2
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +1 -1
package/README.md
CHANGED
|
@@ -56,12 +56,29 @@ 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` and the lookahead-only name-range handling in `pragma.h`.
|
|
69
|
+
First guards check the prototype tail; post-type continuation guards inspect only their own argument group.
|
|
70
|
+
Regenerate Unicode and keyword tables from the derived grammar's metadata.
|
|
71
|
+
The generation source is `script/generateIdentifierRanges.js` in this repository.
|
|
72
|
+
The `_preproc_function_name` token ends before splices used to determine adjacency, so the `_preproc_lparen`
|
|
73
|
+
scanner must skip those splices again to reach `(`.
|
|
74
|
+
|
|
75
|
+
Header annotation lookahead accepts argument/prototype groups without unquoted braces, semicolons or preprocessing directives.
|
|
76
|
+
Quoted punctuation remains literal content. GNU statement-expression, inline-tag, compound-literal
|
|
77
|
+
and directive-bearing annotation arguments need a separate grammar design to keep malformed-input recovery bounded.
|
|
78
|
+
|
|
79
|
+
Header annotation declarations use `_macro_modified_declaration`, `_macro_declaration_specifiers`,
|
|
80
|
+
`_macro_declaration_declarator`, and `_header_preproc_item`. Adapt these rules alongside public declaration/type
|
|
81
|
+
overrides when extending C. The annotation declarator uses `function_declarator` to retain trailing macro calls.
|
|
65
82
|
|
|
66
83
|
In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-c) and on
|
|
67
84
|
[willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
|
package/grammar.js
CHANGED
|
@@ -68,6 +68,13 @@ module.exports = Object.assign(
|
|
|
68
68
|
name: 'c',
|
|
69
69
|
|
|
70
70
|
conflicts: ($) => [
|
|
71
|
+
[$._macro_declaration_specifiers],
|
|
72
|
+
[$._macro_declaration_specifiers, $.type_definition, $.type_specifier],
|
|
73
|
+
[$._macro_declaration_specifiers, $.type_specifier, $.expression],
|
|
74
|
+
[$._macro_declaration_specifiers, $.type_specifier],
|
|
75
|
+
[$._macro_declaration_specifiers, $.type_definition, $._declaration_modifiers],
|
|
76
|
+
[$._macro_declaration_specifiers, $._declaration_modifiers, $._empty_declaration],
|
|
77
|
+
[$._macro_declaration_specifiers, $._declaration_modifiers],
|
|
71
78
|
[$.preproc_include_argument],
|
|
72
79
|
[$._preproc_include_token, $.preproc_argument_list],
|
|
73
80
|
[$._preproc_include_path, $.preproc_include_argument],
|
|
@@ -148,6 +155,9 @@ module.exports = Object.assign(
|
|
|
148
155
|
sym('_preproc_lparen'),
|
|
149
156
|
sym('_preproc_directive_arg'),
|
|
150
157
|
sym('_preproc_function_name'),
|
|
158
|
+
sym('_function_attribute_start'),
|
|
159
|
+
sym('_post_type_function_attribute_start'),
|
|
160
|
+
sym('_post_type_function_attribute_suffix'),
|
|
151
161
|
],
|
|
152
162
|
|
|
153
163
|
extras: ($) => [$.pragma_operator, /\s|\\(?:\r\n?|\n\r?)/, $.comment],
|
|
@@ -194,34 +204,22 @@ module.exports = Object.assign(
|
|
|
194
204
|
alias($._old_style_function_definition, $.function_definition),
|
|
195
205
|
$.linkage_specification,
|
|
196
206
|
$.declaration,
|
|
207
|
+
alias(sym('_macro_modified_declaration'), $.declaration),
|
|
197
208
|
$._top_level_statement,
|
|
198
209
|
$.attributed_statement,
|
|
199
210
|
$.type_definition,
|
|
200
211
|
$._empty_declaration,
|
|
201
|
-
sym('preproc_if'),
|
|
202
|
-
sym('preproc_ifdef'),
|
|
212
|
+
alias(sym('preproc_if_in_header'), sym('preproc_if')),
|
|
213
|
+
alias(sym('preproc_ifdef_in_header'), sym('preproc_ifdef')),
|
|
203
214
|
$.preproc_include,
|
|
204
215
|
$.preproc_def,
|
|
205
216
|
$.preproc_function_def,
|
|
206
217
|
$.preproc_call
|
|
207
218
|
),
|
|
208
219
|
|
|
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
|
-
),
|
|
220
|
+
_block_item: ($) => blockItemRule($, false),
|
|
221
|
+
|
|
222
|
+
_header_preproc_item: ($) => blockItemRule($, true),
|
|
225
223
|
|
|
226
224
|
// Derived grammars that replace externals still need this lexical fallback. Comment-rich spacing makes the
|
|
227
225
|
// lexer too large for the pinned Node Wasm optimizer; the external scanner handles those forms.
|
|
@@ -313,6 +311,8 @@ module.exports = Object.assign(
|
|
|
313
311
|
sym('_preproc_newline')
|
|
314
312
|
),
|
|
315
313
|
|
|
314
|
+
...preprocIf('_in_header', () => sym('_header_preproc_item')),
|
|
315
|
+
|
|
316
316
|
...preprocIf('', () => sym('_block_item')),
|
|
317
317
|
...preprocIf('_in_single_case', () => sym('_single_case_body'), 0, true),
|
|
318
318
|
...preprocIf('_in_field_declaration_list', () => sym('_field_declaration_list_item')),
|
|
@@ -426,30 +426,35 @@ module.exports = Object.assign(
|
|
|
426
426
|
)
|
|
427
427
|
),
|
|
428
428
|
|
|
429
|
-
declaration: ($) =>
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
)
|
|
449
|
-
)
|
|
429
|
+
declaration: ($) => declarationRule($, $._declaration_specifiers, true),
|
|
430
|
+
|
|
431
|
+
_macro_modified_declaration: ($) =>
|
|
432
|
+
declarationRule($, $._macro_declaration_specifiers, false, $._macro_declaration_declarator),
|
|
433
|
+
|
|
434
|
+
_macro_declaration_specifiers: ($) => {
|
|
435
|
+
const modifier = choice($._declaration_modifiers, alias($._macro_attribute_suffix, $.attribute_specifier));
|
|
436
|
+
const postModifier = choice(
|
|
437
|
+
$._declaration_modifiers,
|
|
438
|
+
alias($._post_type_macro_attribute_suffix, $.attribute_specifier)
|
|
439
|
+
);
|
|
440
|
+
return choice(
|
|
441
|
+
seq(
|
|
442
|
+
choice($.storage_class_specifier, $.type_qualifier, $.ms_declspec_modifier),
|
|
443
|
+
repeat($._declaration_modifiers),
|
|
444
|
+
alias($._macro_attribute_specifier, $.attribute_specifier),
|
|
445
|
+
repeat(modifier),
|
|
446
|
+
field('type', $._non_identifier_type_specifier),
|
|
447
|
+
repeat(postModifier)
|
|
450
448
|
),
|
|
451
|
-
|
|
452
|
-
|
|
449
|
+
seq(
|
|
450
|
+
repeat($._declaration_modifiers),
|
|
451
|
+
field('type', choice($._non_identifier_type_specifier, $._type_identifier)),
|
|
452
|
+
repeat($._declaration_modifiers),
|
|
453
|
+
alias($._post_type_macro_attribute_specifier, $.attribute_specifier),
|
|
454
|
+
repeat(postModifier)
|
|
455
|
+
)
|
|
456
|
+
);
|
|
457
|
+
},
|
|
453
458
|
|
|
454
459
|
type_definition: ($) =>
|
|
455
460
|
seq(
|
|
@@ -493,6 +498,19 @@ module.exports = Object.assign(
|
|
|
493
498
|
|
|
494
499
|
attribute_specifier: ($) => seq(choice('__attribute__', '__attribute'), '(', $.argument_list, ')'),
|
|
495
500
|
|
|
501
|
+
_macro_attribute_specifier: ($) => macroAttributeRule($, 'leading'),
|
|
502
|
+
|
|
503
|
+
_post_type_macro_attribute_specifier: ($) => macroAttributeRule($, 'post-type'),
|
|
504
|
+
|
|
505
|
+
_macro_attribute_suffix: ($) => macroAttributeRule($, 'continuation'),
|
|
506
|
+
|
|
507
|
+
_post_type_macro_attribute_suffix: ($) => macroAttributeRule($, 'post-type-continuation'),
|
|
508
|
+
|
|
509
|
+
_macro_attribute_argument_list: ($) => {
|
|
510
|
+
const argument = macroAttributeArgument($);
|
|
511
|
+
return seq('(', optional(argument), repeat(seq(',', optional(argument))), ')');
|
|
512
|
+
},
|
|
513
|
+
|
|
496
514
|
attribute: ($) =>
|
|
497
515
|
seq(optional(seq(field('prefix', $.identifier), '::')), field('name', $.identifier), optional($.argument_list)),
|
|
498
516
|
|
|
@@ -538,15 +556,9 @@ module.exports = Object.assign(
|
|
|
538
556
|
),
|
|
539
557
|
|
|
540
558
|
_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
|
-
),
|
|
559
|
+
declarationDeclaratorRule($, alias($._function_declaration_declarator, $.function_declarator)),
|
|
560
|
+
|
|
561
|
+
_macro_declaration_declarator: ($) => declarationDeclaratorRule($, $.function_declarator),
|
|
550
562
|
|
|
551
563
|
_field_declarator: ($) =>
|
|
552
564
|
choice(alias($.attributed_field_declarator, $.attributed_declarator), $._unattributed_field_declarator),
|
|
@@ -1601,12 +1613,117 @@ module.exports = Object.assign(
|
|
|
1601
1613
|
)
|
|
1602
1614
|
),
|
|
1603
1615
|
|
|
1616
|
+
_preproc_number: () =>
|
|
1617
|
+
/(\d|\.\d)([\p{XID_Continue}.]|[eEpP][+-]|'[_a-zA-Z0-9]|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8})*/u,
|
|
1618
|
+
|
|
1604
1619
|
comment: () => token(choice(LINE_COMMENT, seq('/*', /[^*]*\*+([^/*][^*]*\*+)*/, '/'))),
|
|
1605
1620
|
},
|
|
1606
1621
|
}),
|
|
1607
1622
|
{ PREC, preprocIf, preprocessor, commaSep, commaSep1 }
|
|
1608
1623
|
);
|
|
1609
1624
|
|
|
1625
|
+
/**
|
|
1626
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'storage_class_specifier' | 'ms_call_modifier' | '_declaration_declarator' | 'gnu_asm_expression' | 'init_declarator' | '_declaration_modifiers'>} $
|
|
1627
|
+
* @param {import('./types/treeSitterDsl').Rule} specifiers
|
|
1628
|
+
* @param {boolean} allowAuto
|
|
1629
|
+
* @param {import('./types/treeSitterDsl').Rule} declarator
|
|
1630
|
+
*/
|
|
1631
|
+
function declarationRule($, specifiers, allowAuto, declarator = $._declaration_declarator) {
|
|
1632
|
+
return seq(
|
|
1633
|
+
allowAuto
|
|
1634
|
+
? choice(
|
|
1635
|
+
specifiers,
|
|
1636
|
+
prec.dynamic(
|
|
1637
|
+
PREC.PAREN_DECLARATOR - 1,
|
|
1638
|
+
seq(
|
|
1639
|
+
repeat($._declaration_modifiers),
|
|
1640
|
+
alias('auto', $.storage_class_specifier),
|
|
1641
|
+
repeat($._declaration_modifiers)
|
|
1642
|
+
)
|
|
1643
|
+
)
|
|
1644
|
+
)
|
|
1645
|
+
: specifiers,
|
|
1646
|
+
commaSep1(
|
|
1647
|
+
field(
|
|
1648
|
+
'declarator',
|
|
1649
|
+
choice(seq(optional($.ms_call_modifier), declarator, optional($.gnu_asm_expression)), $.init_declarator)
|
|
1650
|
+
)
|
|
1651
|
+
),
|
|
1652
|
+
';'
|
|
1653
|
+
);
|
|
1654
|
+
}
|
|
1655
|
+
|
|
1656
|
+
/**
|
|
1657
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'attributed_declarator' | 'pointer_declarator' | 'array_declarator' | 'parenthesized_declarator' | 'identifier'>} $
|
|
1658
|
+
* @param {import('./types/treeSitterDsl').Rule} functionDeclarator
|
|
1659
|
+
*/
|
|
1660
|
+
function declarationDeclaratorRule($, functionDeclarator) {
|
|
1661
|
+
return choice(
|
|
1662
|
+
$.attributed_declarator,
|
|
1663
|
+
$.pointer_declarator,
|
|
1664
|
+
functionDeclarator,
|
|
1665
|
+
$.array_declarator,
|
|
1666
|
+
$.parenthesized_declarator,
|
|
1667
|
+
$.identifier,
|
|
1668
|
+
alias(VA_ARG_KEYWORDS, $.identifier)
|
|
1669
|
+
);
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
/**
|
|
1673
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'function_definition' | '_old_style_function_definition' | 'linkage_specification' | 'declaration' | '_macro_modified_declaration' | 'statement' | 'type_definition' | '_empty_declaration' | 'preproc_include' | 'preproc_def' | 'preproc_function_def' | 'preproc_call'>} $
|
|
1674
|
+
* @param {boolean} header
|
|
1675
|
+
*/
|
|
1676
|
+
function blockItemRule($, header) {
|
|
1677
|
+
return choice(
|
|
1678
|
+
$.function_definition,
|
|
1679
|
+
alias($._old_style_function_definition, $.function_definition),
|
|
1680
|
+
$.linkage_specification,
|
|
1681
|
+
$.declaration,
|
|
1682
|
+
...(header ? [alias($._macro_modified_declaration, $.declaration)] : []),
|
|
1683
|
+
$.statement,
|
|
1684
|
+
$.type_definition,
|
|
1685
|
+
$._empty_declaration,
|
|
1686
|
+
header ? alias(sym('preproc_if_in_header'), sym('preproc_if')) : sym('preproc_if'),
|
|
1687
|
+
header ? alias(sym('preproc_ifdef_in_header'), sym('preproc_ifdef')) : sym('preproc_ifdef'),
|
|
1688
|
+
$.preproc_include,
|
|
1689
|
+
$.preproc_def,
|
|
1690
|
+
$.preproc_function_def,
|
|
1691
|
+
$.preproc_call
|
|
1692
|
+
);
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
/**
|
|
1696
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'identifier' | 'argument_list' | '_macro_attribute_argument_list'>} $
|
|
1697
|
+
* @param {'leading' | 'post-type' | 'continuation' | 'post-type-continuation'} context
|
|
1698
|
+
*/
|
|
1699
|
+
function macroAttributeRule($, context) {
|
|
1700
|
+
const guard =
|
|
1701
|
+
context === 'continuation'
|
|
1702
|
+
? []
|
|
1703
|
+
: [
|
|
1704
|
+
sym(
|
|
1705
|
+
context === 'leading'
|
|
1706
|
+
? '_function_attribute_start'
|
|
1707
|
+
: context === 'post-type'
|
|
1708
|
+
? '_post_type_function_attribute_start'
|
|
1709
|
+
: '_post_type_function_attribute_suffix'
|
|
1710
|
+
),
|
|
1711
|
+
];
|
|
1712
|
+
return prec.dynamic(
|
|
1713
|
+
-2,
|
|
1714
|
+
seq(
|
|
1715
|
+
...guard,
|
|
1716
|
+
field('name', $.identifier),
|
|
1717
|
+
field('arguments', alias($._macro_attribute_argument_list, $.argument_list))
|
|
1718
|
+
)
|
|
1719
|
+
);
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
/** @param {import('./types/treeSitterDsl').GrammarSymbols<'expression' | 'type_descriptor' | '_preproc_number' | 'number_literal'>} $ */
|
|
1723
|
+
function macroAttributeArgument($) {
|
|
1724
|
+
return choice($.expression, prec.dynamic(-1, $.type_descriptor), alias($._preproc_number, $.number_literal));
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1610
1727
|
/**
|
|
1611
1728
|
* @param {import('./types/treeSitterDsl').RuleOrLiteral} type
|
|
1612
1729
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/tree-sitter-c",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "10.0.0",
|
|
4
4
|
"description": "C grammar for tree-sitter",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"incremental",
|
|
@@ -23,6 +23,8 @@
|
|
|
23
23
|
"*.wasm",
|
|
24
24
|
"src/pragma.h",
|
|
25
25
|
"src/identifier.h",
|
|
26
|
+
"src/keywords.h",
|
|
27
|
+
"src/functionMacro.h",
|
|
26
28
|
"src/scanner.c"
|
|
27
29
|
],
|
|
28
30
|
"scripts": {
|
|
@@ -0,0 +1,269 @@
|
|
|
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
|
+
static bool function_macro_balanced(TSLexer *lexer, int32_t open, int32_t close, bool *literal);
|
|
22
|
+
static bool function_macro_group(int32_t c, int32_t open, int32_t close, unsigned *depth, unsigned *nested);
|
|
23
|
+
static void function_macro_note_literal(bool *literal, unsigned depth, unsigned nested, const MacroArgumentWord *word, int32_t c);
|
|
24
|
+
static bool function_macro_tail_word(TSLexer *lexer, bool escaped, MacroPrototypeTail *tail);
|
|
25
|
+
static bool function_macro_tail_group(TSLexer *lexer, int32_t c, MacroPrototypeTail *tail);
|
|
26
|
+
static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type);
|
|
27
|
+
static bool function_macro_argument_quote(TSLexer *lexer, int32_t quote);
|
|
28
|
+
static bool function_macro_argument_escape(TSLexer *lexer, MacroArgumentWord *word);
|
|
29
|
+
static bool function_macro_argument_slash(TSLexer *lexer, bool allow_division);
|
|
30
|
+
static bool function_macro_tail_spacing(TSLexer *lexer, bool *escaped);
|
|
31
|
+
static bool function_macro_line_comment(TSLexer *lexer);
|
|
32
|
+
static bool function_macro_block_comment(TSLexer *lexer);
|
|
33
|
+
static void function_macro_argument_word(MacroArgumentWord *word, int32_t c);
|
|
34
|
+
|
|
35
|
+
static bool function_macro_keyword(const char *word);
|
|
36
|
+
static bool function_macro_word_in(const char *word, const char *const *words, unsigned count);
|
|
37
|
+
|
|
38
|
+
static bool scan_function_attribute_guard(TSLexer *lexer, bool prototype, bool require_literal, bool allow_pragma, TSSymbol pragma_symbol) {
|
|
39
|
+
char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
|
|
40
|
+
MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true, .lookahead_only = true};
|
|
41
|
+
if (!scan_macro_name(lexer, &name)) return false;
|
|
42
|
+
if (name.ascii && strcmp(word, pragma_word) == 0) {
|
|
43
|
+
lexer->result_symbol = pragma_symbol;
|
|
44
|
+
return allow_pragma && scan_pragma_suffix(lexer);
|
|
45
|
+
}
|
|
46
|
+
if (name.ascii && function_macro_keyword(word)) return false;
|
|
47
|
+
if (!scan_pragma_spacing(lexer) || lexer->lookahead != '(') return false;
|
|
48
|
+
bool literal = false;
|
|
49
|
+
return function_macro_balanced(lexer, '(', ')', &literal) && (!require_literal || literal) &&
|
|
50
|
+
(!prototype || function_macro_declaration_continues(lexer, require_literal));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
static bool function_macro_balanced(TSLexer *lexer, int32_t open, int32_t close, bool *literal) {
|
|
54
|
+
unsigned depth = 1;
|
|
55
|
+
unsigned nested = 0;
|
|
56
|
+
MacroArgumentWord word = {0};
|
|
57
|
+
lexer->advance(lexer, false);
|
|
58
|
+
while (depth) {
|
|
59
|
+
if (lexer->eof(lexer)) return false;
|
|
60
|
+
int32_t c = lexer->lookahead;
|
|
61
|
+
if (c == ';' || c == '{' || c == '#') return false;
|
|
62
|
+
function_macro_note_literal(literal, depth, nested, &word, c);
|
|
63
|
+
if (pragma_space(c)) {
|
|
64
|
+
lexer->advance(lexer, false);
|
|
65
|
+
word = (MacroArgumentWord){0};
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (c == '/') {
|
|
69
|
+
if (!function_macro_argument_slash(lexer, true)) return false;
|
|
70
|
+
word = (MacroArgumentWord){0};
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (c == '\\') {
|
|
74
|
+
if (!function_macro_argument_escape(lexer, &word)) return false;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (c == '"' || (c == '\'' && !word.number)) {
|
|
78
|
+
if (!function_macro_argument_quote(lexer, c)) return false;
|
|
79
|
+
word = (MacroArgumentWord){0};
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
function_macro_argument_word(&word, c);
|
|
83
|
+
if (!function_macro_group(c, open, close, &depth, &nested)) return false;
|
|
84
|
+
lexer->advance(lexer, false);
|
|
85
|
+
}
|
|
86
|
+
return nested == 0;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
static bool function_macro_group(int32_t c, int32_t open, int32_t close, unsigned *depth, unsigned *nested) {
|
|
90
|
+
if (c == open) (*depth)++;
|
|
91
|
+
else if (c == close) (*depth)--;
|
|
92
|
+
else if (c > 0 && c < 128 && strchr("([{", c)) (*nested)++;
|
|
93
|
+
else if (c > 0 && c < 128 && strchr(")]}", c)) {
|
|
94
|
+
if (!*nested) return false;
|
|
95
|
+
(*nested)--;
|
|
96
|
+
}
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
static void function_macro_note_literal(bool *literal, unsigned depth, unsigned nested, const MacroArgumentWord *word, int32_t c) {
|
|
101
|
+
if (!literal || depth != 1 || nested) return;
|
|
102
|
+
if (c == '"' || (c == '\'' && !word->number) || (c >= '0' && c <= '9' && !word->identifier)) *literal = true;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type) {
|
|
106
|
+
MacroPrototypeTail tail = {.post_type = post_type, .explicit_type = post_type};
|
|
107
|
+
for (;;) {
|
|
108
|
+
bool escaped = false;
|
|
109
|
+
if (!function_macro_tail_spacing(lexer, &escaped) || lexer->eof(lexer)) return false;
|
|
110
|
+
int32_t c = lexer->lookahead;
|
|
111
|
+
if (escaped || preproc_word(c, false)) {
|
|
112
|
+
if (!function_macro_tail_word(lexer, escaped, &tail)) return false;
|
|
113
|
+
} else if (c == ';') {
|
|
114
|
+
return tail.declarator_group && tail.explicit_type;
|
|
115
|
+
} else if (c <= 127 && strchr("{}#)]", c)) {
|
|
116
|
+
return false;
|
|
117
|
+
} else if (c == '(' || c == '[') {
|
|
118
|
+
if (!function_macro_tail_group(lexer, c, &tail)) return false;
|
|
119
|
+
} else if (c == '"' || c == '\'') {
|
|
120
|
+
if (!function_macro_argument_quote(lexer, c)) return false;
|
|
121
|
+
} else {
|
|
122
|
+
if (c == '*' && tail.post_type && tail.declarator_name && !tail.declarator_group) return false;
|
|
123
|
+
lexer->advance(lexer, false);
|
|
124
|
+
tail.modifier = false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
static bool function_macro_tail_group(TSLexer *lexer, int32_t c, MacroPrototypeTail *tail) {
|
|
130
|
+
bool literal = false;
|
|
131
|
+
if (!function_macro_balanced(lexer, c, c == '(' ? ')' : ']', &literal)) return false;
|
|
132
|
+
if (c == '(' && !tail->modifier) {
|
|
133
|
+
if (literal && tail->post_type && !tail->declarator_group) tail->declarator_name = false;
|
|
134
|
+
else tail->declarator_group = true;
|
|
135
|
+
}
|
|
136
|
+
tail->modifier = false;
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
static bool function_macro_tail_spacing(TSLexer *lexer, bool *escaped) {
|
|
141
|
+
for (;;) {
|
|
142
|
+
int32_t c = lexer->lookahead;
|
|
143
|
+
if (pragma_space(c)) {
|
|
144
|
+
lexer->advance(lexer, false);
|
|
145
|
+
} else if (c == '\\') {
|
|
146
|
+
lexer->advance(lexer, false);
|
|
147
|
+
if (scan_preproc_newline(lexer, false)) continue;
|
|
148
|
+
*escaped = lexer->lookahead == 'u' || lexer->lookahead == 'U';
|
|
149
|
+
return *escaped;
|
|
150
|
+
} else if (c == '/') {
|
|
151
|
+
if (!function_macro_argument_slash(lexer, false)) return false;
|
|
152
|
+
} else {
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static bool function_macro_tail_word(TSLexer *lexer, bool escaped, MacroPrototypeTail *tail) {
|
|
159
|
+
char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
|
|
160
|
+
MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = !escaped};
|
|
161
|
+
if (escaped && !scan_macro_name_ucn(lexer)) return false;
|
|
162
|
+
bool continuation = escaped;
|
|
163
|
+
while (scan_macro_name_character(lexer, continuation, &name) == MACRO_NAME_CHARACTER) continuation = true;
|
|
164
|
+
bool pragma = name.ascii && strcmp(word, pragma_word) == 0;
|
|
165
|
+
bool keyword = name.ascii && function_macro_keyword(word);
|
|
166
|
+
if (tail->post_type && !tail->declarator_group && !keyword && !pragma) {
|
|
167
|
+
if (tail->declarator_name) return false;
|
|
168
|
+
tail->declarator_name = true;
|
|
169
|
+
}
|
|
170
|
+
tail->explicit_type |= name.ascii && function_macro_word_in(word, function_macro_type_keywords,
|
|
171
|
+
sizeof(function_macro_type_keywords) / sizeof(function_macro_type_keywords[0]));
|
|
172
|
+
tail->modifier = pragma || (name.ascii && function_macro_word_in(word, function_macro_group_modifiers,
|
|
173
|
+
sizeof(function_macro_group_modifiers) / sizeof(function_macro_group_modifiers[0])));
|
|
174
|
+
return !tail->post_type || !keyword ||
|
|
175
|
+
function_macro_word_in(word, function_macro_declaration_modifiers,
|
|
176
|
+
sizeof(function_macro_declaration_modifiers) / sizeof(function_macro_declaration_modifiers[0]));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
static bool function_macro_argument_escape(TSLexer *lexer, MacroArgumentWord *word) {
|
|
180
|
+
lexer->advance(lexer, false);
|
|
181
|
+
if (scan_preproc_newline(lexer, false)) return true;
|
|
182
|
+
if (!scan_preproc_ucn(lexer, &word->number)) return false;
|
|
183
|
+
word->identifier = !word->number;
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
static bool function_macro_argument_quote(TSLexer *lexer, int32_t quote) {
|
|
188
|
+
lexer->advance(lexer, false);
|
|
189
|
+
while (!lexer->eof(lexer)) {
|
|
190
|
+
int32_t c = lexer->lookahead;
|
|
191
|
+
lexer->advance(lexer, false);
|
|
192
|
+
if (c == quote) return true;
|
|
193
|
+
if (c == '\n' || c == '\r') return false;
|
|
194
|
+
if (c != '\\') continue;
|
|
195
|
+
if (scan_preproc_newline(lexer, false)) continue;
|
|
196
|
+
if (!scan_preproc_splices(lexer)) continue;
|
|
197
|
+
if (lexer->eof(lexer)) return false;
|
|
198
|
+
lexer->advance(lexer, false);
|
|
199
|
+
}
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
static bool function_macro_argument_slash(TSLexer *lexer, bool allow_division) {
|
|
204
|
+
lexer->advance(lexer, false);
|
|
205
|
+
if (!scan_preproc_splices(lexer)) return allow_division;
|
|
206
|
+
if (lexer->lookahead == '/') return function_macro_line_comment(lexer);
|
|
207
|
+
if (lexer->lookahead == '*') return function_macro_block_comment(lexer);
|
|
208
|
+
return allow_division;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
static bool function_macro_line_comment(TSLexer *lexer) {
|
|
212
|
+
lexer->advance(lexer, false);
|
|
213
|
+
while (!lexer->eof(lexer) && lexer->lookahead != '\n' && lexer->lookahead != '\r') {
|
|
214
|
+
if (lexer->lookahead == '\\') {
|
|
215
|
+
do {
|
|
216
|
+
lexer->advance(lexer, false);
|
|
217
|
+
} while (lexer->lookahead == '\\');
|
|
218
|
+
if (scan_preproc_newline(lexer, false)) continue;
|
|
219
|
+
if (lexer->eof(lexer)) return false;
|
|
220
|
+
}
|
|
221
|
+
lexer->advance(lexer, false);
|
|
222
|
+
}
|
|
223
|
+
return true;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
static bool function_macro_block_comment(TSLexer *lexer) {
|
|
227
|
+
lexer->advance(lexer, false);
|
|
228
|
+
bool star = false;
|
|
229
|
+
while (!lexer->eof(lexer)) {
|
|
230
|
+
int32_t c = lexer->lookahead;
|
|
231
|
+
lexer->advance(lexer, false);
|
|
232
|
+
if (c == '\\' && scan_preproc_newline(lexer, false)) continue;
|
|
233
|
+
if (star && c == '/') return true;
|
|
234
|
+
star = c == '*';
|
|
235
|
+
}
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
static void function_macro_argument_word(MacroArgumentWord *word, int32_t c) {
|
|
240
|
+
bool member = preproc_word(c, word->identifier || word->number);
|
|
241
|
+
bool digit = c >= '0' && c <= '9';
|
|
242
|
+
bool exponent_sign = word->number && (c == '+' || c == '-') &&
|
|
243
|
+
(word->last == 'e' || word->last == 'E' || word->last == 'p' || word->last == 'P');
|
|
244
|
+
if (c == '.') word->identifier = false;
|
|
245
|
+
if (!member && !digit && c != '.' && c != '\'' && !exponent_sign) *word = (MacroArgumentWord){0};
|
|
246
|
+
if (digit && !word->identifier) word->number = true;
|
|
247
|
+
if (member && !word->number) word->identifier = true;
|
|
248
|
+
if (word->number) word->last = c;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
static bool function_macro_keyword(const char *word) {
|
|
252
|
+
return function_macro_word_in(word, function_macro_keywords,
|
|
253
|
+
sizeof(function_macro_keywords) / sizeof(function_macro_keywords[0]));
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
static bool function_macro_word_in(const char *word, const char *const *words, unsigned count) {
|
|
257
|
+
unsigned start = 0;
|
|
258
|
+
unsigned end = count;
|
|
259
|
+
while (start < end) {
|
|
260
|
+
unsigned middle = start + (end - start) / 2;
|
|
261
|
+
int order = strcmp(word, words[middle]);
|
|
262
|
+
if (order == 0) return true;
|
|
263
|
+
if (order < 0) end = middle;
|
|
264
|
+
else start = middle + 1;
|
|
265
|
+
}
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
#endif
|
package/src/identifier.h
CHANGED
package/src/keywords.h
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
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_declaration_modifiers[] = {
|
|
194
|
+
"_Alignas",
|
|
195
|
+
"_Atomic",
|
|
196
|
+
"_Nonnull",
|
|
197
|
+
"_Noreturn",
|
|
198
|
+
"__asm",
|
|
199
|
+
"__asm__",
|
|
200
|
+
"__attribute",
|
|
201
|
+
"__attribute__",
|
|
202
|
+
"__cdecl",
|
|
203
|
+
"__clrcall",
|
|
204
|
+
"__declspec",
|
|
205
|
+
"__extension__",
|
|
206
|
+
"__fastcall",
|
|
207
|
+
"__restrict__",
|
|
208
|
+
"__stdcall",
|
|
209
|
+
"__thiscall",
|
|
210
|
+
"__vectorcall",
|
|
211
|
+
"alignas",
|
|
212
|
+
"asm",
|
|
213
|
+
"const",
|
|
214
|
+
"constexpr",
|
|
215
|
+
"noreturn",
|
|
216
|
+
"restrict",
|
|
217
|
+
"volatile",
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
#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 };
|
|
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,18 @@ 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
|
+
bool leading = valid_symbols[FUNCTION_ATTRIBUTE_START];
|
|
34
|
+
bool post_type = valid_symbols[POST_TYPE_FUNCTION_ATTRIBUTE_START];
|
|
35
|
+
bool suffix = valid_symbols[POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX];
|
|
36
|
+
if ((leading || post_type || suffix) && !valid_symbols[PREPROC_LPAREN] &&
|
|
37
|
+
!valid_symbols[PREPROC_NEWLINE] && !valid_symbols[PREPROC_ARG] && !valid_symbols[PREPROC_DIRECTIVE_ARG]) {
|
|
38
|
+
lexer->result_symbol = leading ? FUNCTION_ATTRIBUTE_START : post_type ? POST_TYPE_FUNCTION_ATTRIBUTE_START : POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX;
|
|
39
|
+
return scan_function_attribute_guard(lexer, leading || post_type, !leading, valid_symbols[PRAGMA_OPERATOR], PRAGMA_OPERATOR);
|
|
40
|
+
}
|
|
41
|
+
return scan_preproc_tokens(lexer, valid_symbols);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static bool scan_preproc_tokens(TSLexer *lexer, const bool *valid_symbols) {
|
|
31
45
|
bool directive_text = !valid_symbols[PREPROC_ARG] && valid_symbols[PREPROC_DIRECTIVE_ARG];
|
|
32
46
|
TSSymbol argument_symbol = directive_text ? PREPROC_DIRECTIVE_ARG : PREPROC_ARG;
|
|
33
47
|
bool argument_valid = valid_symbols[PREPROC_ARG] || directive_text;
|
package/tree-sitter-c.wasm
CHANGED
|
Binary file
|