@willbooster/tree-sitter-c 19.0.0 → 21.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 +84 -20
- package/grammar.js +527 -81
- package/package.json +7 -8
- package/src/functionMacro.h +209 -29
- package/src/keywords.h +37 -0
- package/src/node-types.json +60 -22
- package/src/pragma.h +8 -2
- package/src/scanner.c +9 -3
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +1 -3
- package/queries/highlights.scm +0 -122
- package/queries/tags.scm +0 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/tree-sitter-c",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "21.0.0",
|
|
4
4
|
"description": "C grammar for tree-sitter",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"c",
|
|
@@ -17,7 +17,6 @@
|
|
|
17
17
|
"files": [
|
|
18
18
|
"grammar.js",
|
|
19
19
|
"types/*",
|
|
20
|
-
"queries/*.scm",
|
|
21
20
|
"src/node-types.json",
|
|
22
21
|
"tree-sitter.json",
|
|
23
22
|
"*.wasm",
|
|
@@ -55,22 +54,22 @@
|
|
|
55
54
|
},
|
|
56
55
|
"devDependencies": {
|
|
57
56
|
"@tsconfig/bun": "1.0.11",
|
|
58
|
-
"@types/bun": "1.4.
|
|
57
|
+
"@types/bun": "1.4.4",
|
|
59
58
|
"@vitest/browser-playwright": "5.0.5",
|
|
60
59
|
"@willbooster/oxlint-config": "1.4.9",
|
|
61
|
-
"@willbooster/wb": "22.
|
|
62
|
-
"@willbooster/web-tree-sitter": "15.0.
|
|
60
|
+
"@willbooster/wb": "22.32.3",
|
|
61
|
+
"@willbooster/web-tree-sitter": "15.0.3",
|
|
63
62
|
"conventional-changelog-conventionalcommits": "10.4.1",
|
|
64
63
|
"lefthook": "2.2.2",
|
|
65
|
-
"oxlint": "1.
|
|
66
|
-
"oxlint-tsgolint": "7.0.
|
|
64
|
+
"oxlint": "1.89.0",
|
|
65
|
+
"oxlint-tsgolint": "7.0.2005",
|
|
67
66
|
"playwright": "1.64.1",
|
|
68
67
|
"semantic-release": "25.0.9",
|
|
69
68
|
"vitest": "5.0.5",
|
|
70
69
|
"wrangler": "4.151.1"
|
|
71
70
|
},
|
|
72
71
|
"peerDependencies": {
|
|
73
|
-
"@willbooster/web-tree-sitter": "^15.0.
|
|
72
|
+
"@willbooster/web-tree-sitter": "^15.0.3"
|
|
74
73
|
},
|
|
75
74
|
"peerDependenciesMeta": {
|
|
76
75
|
"@willbooster/web-tree-sitter": {
|
package/src/functionMacro.h
CHANGED
|
@@ -31,16 +31,28 @@ typedef struct {
|
|
|
31
31
|
TSSymbol object_start;
|
|
32
32
|
bool allow_pragma;
|
|
33
33
|
bool allow_object_start;
|
|
34
|
+
TSSymbol braced_start;
|
|
35
|
+
bool allow_braced_start;
|
|
34
36
|
} MacroAttributeSymbols;
|
|
35
37
|
|
|
38
|
+
typedef enum {
|
|
39
|
+
MACRO_GROUP_INVALID, MACRO_GROUP_BALANCED, MACRO_GROUP_BRACED,
|
|
40
|
+
} MacroGroup;
|
|
41
|
+
|
|
36
42
|
static MacroArgumentToken function_macro_argument_token(TSLexer *lexer, MacroArgumentWord *word, bool *unterminated_raw);
|
|
37
43
|
|
|
38
44
|
static bool function_macro_balanced(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw);
|
|
39
|
-
static bool
|
|
45
|
+
static bool function_macro_modifier_balanced(TSLexer *lexer, int32_t open, int32_t close);
|
|
46
|
+
static MacroGroup function_macro_group_prefix(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw, bool type_argument);
|
|
47
|
+
static MacroGroup function_macro_group_contents(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw, bool type_argument, bool modifier);
|
|
48
|
+
static bool function_macro_group_separator(int32_t c, unsigned depth, unsigned nested, bool type_argument, unsigned braces);
|
|
49
|
+
static bool function_macro_type_prefix_keyword(const MacroName *name, const char *const *reserved_words, unsigned reserved_count);
|
|
50
|
+
static bool function_macro_group(int32_t c, int32_t open, int32_t close, unsigned *depth, unsigned *nested, unsigned *braces);
|
|
40
51
|
static void function_macro_note_literal(bool *literal, unsigned depth, unsigned nested, const MacroArgumentWord *word, int32_t c);
|
|
41
52
|
static bool function_macro_tail_word(TSLexer *lexer, bool escaped, MacroPrototypeTail *tail);
|
|
42
|
-
static
|
|
43
|
-
static bool
|
|
53
|
+
static MacroGroup function_macro_tail_group(TSLexer *lexer, int32_t c, MacroPrototypeTail *tail);
|
|
54
|
+
static bool function_macro_tail_punctuator(TSLexer *lexer, int32_t c, MacroPrototypeTail *tail);
|
|
55
|
+
static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type, bool allow_braced);
|
|
44
56
|
static bool function_macro_argument_quote(TSLexer *lexer, int32_t quote, bool raw, bool *unterminated_raw);
|
|
45
57
|
static bool function_macro_argument_escape(TSLexer *lexer, MacroArgumentWord *word);
|
|
46
58
|
static bool function_macro_argument_slash(TSLexer *lexer, bool allow_division);
|
|
@@ -62,12 +74,18 @@ static inline bool scan_raw_macro_type_tail(TSLexer *lexer) {
|
|
|
62
74
|
while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
|
|
63
75
|
if (lexer->lookahead == ')' || lexer->eof(lexer)) return false;
|
|
64
76
|
unsigned depth = 1;
|
|
77
|
+
unsigned nested = 0;
|
|
78
|
+
unsigned braces = 0;
|
|
79
|
+
bool balanced = true;
|
|
65
80
|
MacroArgumentWord word = {0};
|
|
66
81
|
while (!lexer->eof(lexer)) {
|
|
67
82
|
int32_t c = lexer->lookahead;
|
|
68
83
|
if (c == ')' && depth == 1) break;
|
|
69
84
|
if (function_macro_argument_token(lexer, &word, NULL) != MACRO_ARGUMENT_PLAIN) continue;
|
|
70
|
-
|
|
85
|
+
// Like the attribute guard, leave a balanced group's first brace and later arguments to the grammar.
|
|
86
|
+
if (c == '{' && balanced) return false;
|
|
87
|
+
if (balanced) balanced = function_macro_group(c, '(', ')', &depth, &nested, &braces);
|
|
88
|
+
else if (c == '(') depth++;
|
|
71
89
|
else if (c == ')') depth--;
|
|
72
90
|
function_macro_argument_word(&word, c);
|
|
73
91
|
lexer->advance(lexer, false);
|
|
@@ -76,27 +94,148 @@ static inline bool scan_raw_macro_type_tail(TSLexer *lexer) {
|
|
|
76
94
|
return true;
|
|
77
95
|
}
|
|
78
96
|
|
|
79
|
-
static bool
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (!scan_macro_name(lexer, &name)) return false;
|
|
83
|
-
if (name.ascii && strcmp(word, pragma_word) == 0) {
|
|
97
|
+
static bool function_macro_first_word(TSLexer *lexer, const MacroName *name, const MacroAttributeSymbols *symbols, bool *handled) {
|
|
98
|
+
*handled = true;
|
|
99
|
+
if (name->ascii && strcmp(name->word, pragma_word) == 0) {
|
|
84
100
|
lexer->result_symbol = symbols->pragma;
|
|
85
101
|
return symbols->allow_pragma && scan_pragma_suffix(lexer);
|
|
86
102
|
}
|
|
87
|
-
if (symbols->allow_object_start && name
|
|
103
|
+
if (symbols->allow_object_start && name->ascii && function_macro_scalar_type_keyword(name->word)) {
|
|
88
104
|
lexer->result_symbol = symbols->object_start;
|
|
89
105
|
return function_macro_object_prefix(lexer);
|
|
90
106
|
}
|
|
107
|
+
*handled = false;
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
static bool scan_function_attribute_guard(TSLexer *lexer, bool prototype, bool require_literal, bool trailing, const MacroAttributeSymbols *symbols) {
|
|
112
|
+
bool braced_only = symbols->allow_braced_start && lexer->result_symbol == symbols->braced_start;
|
|
113
|
+
char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
|
|
114
|
+
MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true, .lookahead_only = !trailing};
|
|
115
|
+
if (!scan_macro_name(lexer, &name)) return false;
|
|
116
|
+
bool handled;
|
|
117
|
+
bool accepted = function_macro_first_word(lexer, &name, symbols, &handled);
|
|
118
|
+
if (handled) return accepted;
|
|
91
119
|
if (lexer->result_symbol == symbols->object_start) return false;
|
|
92
120
|
if (name.ascii && function_macro_attribute_keyword(word, trailing)) return false;
|
|
93
121
|
if (!scan_pragma_spacing(lexer) || lexer->lookahead != '(') return false;
|
|
94
122
|
if (trailing) return function_macro_trailing_argument_prefix(lexer);
|
|
95
123
|
bool literal = false;
|
|
96
124
|
bool unterminated_raw = false;
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
125
|
+
MacroGroup group = function_macro_group_prefix(lexer, '(', ')', &literal, &unterminated_raw, false);
|
|
126
|
+
// The grammar closes braced arguments without repeated lookahead through an incomplete body.
|
|
127
|
+
if (group == MACRO_GROUP_BRACED && symbols->allow_braced_start) {
|
|
128
|
+
lexer->result_symbol = symbols->braced_start;
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
if (braced_only) return false;
|
|
132
|
+
if (unterminated_raw) return true;
|
|
133
|
+
if (group != MACRO_GROUP_BALANCED) return false;
|
|
134
|
+
return (!require_literal || literal) && (!prototype || function_macro_declaration_continues(lexer, require_literal, symbols->allow_braced_start));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
static bool function_macro_bracket_attribute(TSLexer *lexer) {
|
|
138
|
+
lexer->advance(lexer, false);
|
|
139
|
+
if (!scan_pragma_spacing(lexer) || lexer->lookahead != '[') return false;
|
|
140
|
+
if (!function_macro_modifier_balanced(lexer, '[', ']') ||
|
|
141
|
+
!scan_pragma_spacing(lexer) || lexer->lookahead != ']') return false;
|
|
142
|
+
lexer->advance(lexer, false);
|
|
143
|
+
return scan_pragma_spacing(lexer);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
static bool function_macro_type_modifier_word(const MacroName *name) {
|
|
147
|
+
return name->ascii && function_macro_word_in(name->word, function_macro_group_modifiers,
|
|
148
|
+
sizeof(function_macro_group_modifiers) / sizeof(function_macro_group_modifiers[0])) &&
|
|
149
|
+
!function_macro_word_in(name->word, function_macro_type_keywords,
|
|
150
|
+
sizeof(function_macro_type_keywords) / sizeof(function_macro_type_keywords[0]));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
typedef enum {
|
|
154
|
+
MACRO_TYPE_PREFIX_INVALID, MACRO_TYPE_PREFIX_MODIFIER, MACRO_TYPE_PREFIX_MARKER, MACRO_TYPE_PREFIX_TYPE,
|
|
155
|
+
MACRO_TYPE_PREFIX_KNOWN_TYPE, MACRO_TYPE_PREFIX_BALANCED_TYPE, MACRO_TYPE_PREFIX_TAGGED_TYPE,
|
|
156
|
+
} MacroTypePrefix;
|
|
157
|
+
|
|
158
|
+
static MacroTypePrefix function_macro_type_prefix_item(TSLexer *lexer, const MacroName *name, const char *const *reserved_words, unsigned reserved_count, bool type_argument);
|
|
159
|
+
|
|
160
|
+
static bool scan_braced_macro_type_start(TSLexer *lexer, const MacroAttributeSymbols *symbols, const char *const *reserved_words, unsigned reserved_count) {
|
|
161
|
+
bool marker = false;
|
|
162
|
+
bool known_type = false;
|
|
163
|
+
bool tag = false;
|
|
164
|
+
bool body = false;
|
|
165
|
+
bool started = false;
|
|
166
|
+
for (;;) {
|
|
167
|
+
while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
|
|
168
|
+
if (lexer->lookahead == '[') {
|
|
169
|
+
if (!started) lexer->mark_end(lexer);
|
|
170
|
+
started = true;
|
|
171
|
+
if (!function_macro_bracket_attribute(lexer)) return false;
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
if (lexer->lookahead == '{') {
|
|
175
|
+
if (!body || !function_macro_modifier_balanced(lexer, '{', '}')) return false;
|
|
176
|
+
body = tag = false;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
|
|
180
|
+
MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true, .lookahead_only = true, .preserve_end = started};
|
|
181
|
+
if (!scan_macro_name(lexer, &name)) return false;
|
|
182
|
+
if (!started) {
|
|
183
|
+
bool handled;
|
|
184
|
+
bool accepted = function_macro_first_word(lexer, &name, symbols, &handled);
|
|
185
|
+
if (handled) return accepted;
|
|
186
|
+
}
|
|
187
|
+
started = true;
|
|
188
|
+
MacroTypePrefix kind = function_macro_type_prefix_item(lexer, &name, reserved_words, reserved_count, !known_type);
|
|
189
|
+
switch (kind) {
|
|
190
|
+
case MACRO_TYPE_PREFIX_INVALID:
|
|
191
|
+
return false;
|
|
192
|
+
case MACRO_TYPE_PREFIX_TYPE:
|
|
193
|
+
return marker;
|
|
194
|
+
case MACRO_TYPE_PREFIX_MODIFIER:
|
|
195
|
+
continue;
|
|
196
|
+
case MACRO_TYPE_PREFIX_MARKER:
|
|
197
|
+
if (known_type && !tag) return false;
|
|
198
|
+
marker |= !known_type;
|
|
199
|
+
body = tag;
|
|
200
|
+
tag = false;
|
|
201
|
+
break;
|
|
202
|
+
case MACRO_TYPE_PREFIX_KNOWN_TYPE:
|
|
203
|
+
case MACRO_TYPE_PREFIX_BALANCED_TYPE:
|
|
204
|
+
case MACRO_TYPE_PREFIX_TAGGED_TYPE:
|
|
205
|
+
if (!marker || (known_type && kind == MACRO_TYPE_PREFIX_BALANCED_TYPE)) return false;
|
|
206
|
+
known_type = true;
|
|
207
|
+
tag = body = kind == MACRO_TYPE_PREFIX_TAGGED_TYPE;
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
static MacroTypePrefix function_macro_type_prefix_item(TSLexer *lexer, const MacroName *name, const char *const *reserved_words, unsigned reserved_count, bool type_argument) {
|
|
214
|
+
if (name->ascii && strcmp(name->word, pragma_word) == 0) {
|
|
215
|
+
return scan_pragma_suffix_end(lexer, false) && scan_pragma_spacing(lexer)
|
|
216
|
+
? MACRO_TYPE_PREFIX_MODIFIER : MACRO_TYPE_PREFIX_INVALID;
|
|
217
|
+
}
|
|
218
|
+
if (!scan_pragma_spacing(lexer)) return MACRO_TYPE_PREFIX_INVALID;
|
|
219
|
+
if (function_macro_type_modifier_word(name)) {
|
|
220
|
+
return lexer->lookahead == '(' && function_macro_modifier_balanced(lexer, '(', ')') && scan_pragma_spacing(lexer)
|
|
221
|
+
? MACRO_TYPE_PREFIX_MODIFIER : MACRO_TYPE_PREFIX_INVALID;
|
|
222
|
+
}
|
|
223
|
+
if (name->ascii && function_macro_word_in(name->word, function_macro_type_keywords,
|
|
224
|
+
sizeof(function_macro_type_keywords) / sizeof(function_macro_type_keywords[0]))) {
|
|
225
|
+
if (lexer->lookahead == '(' && (!function_macro_modifier_balanced(lexer, '(', ')') || !scan_pragma_spacing(lexer))) return MACRO_TYPE_PREFIX_INVALID;
|
|
226
|
+
if (strcmp(name->word, "struct") == 0 || strcmp(name->word, "union") == 0 || strcmp(name->word, "enum") == 0) return MACRO_TYPE_PREFIX_TAGGED_TYPE;
|
|
227
|
+
return MACRO_TYPE_PREFIX_KNOWN_TYPE;
|
|
228
|
+
}
|
|
229
|
+
if (name->ascii && function_macro_word_in(name->word, function_macro_type_prefix_modifiers,
|
|
230
|
+
sizeof(function_macro_type_prefix_modifiers) / sizeof(function_macro_type_prefix_modifiers[0]))) return MACRO_TYPE_PREFIX_MODIFIER;
|
|
231
|
+
if (function_macro_type_prefix_keyword(name, reserved_words, reserved_count)) return MACRO_TYPE_PREFIX_INVALID;
|
|
232
|
+
if (lexer->lookahead == '(') {
|
|
233
|
+
MacroGroup group = function_macro_group_prefix(lexer, '(', ')', NULL, NULL, type_argument);
|
|
234
|
+
if (group == MACRO_GROUP_BRACED) return MACRO_TYPE_PREFIX_TYPE;
|
|
235
|
+
if (group == MACRO_GROUP_BALANCED && scan_pragma_spacing(lexer)) return MACRO_TYPE_PREFIX_BALANCED_TYPE;
|
|
236
|
+
return MACRO_TYPE_PREFIX_INVALID;
|
|
237
|
+
}
|
|
238
|
+
return MACRO_TYPE_PREFIX_MARKER;
|
|
100
239
|
}
|
|
101
240
|
|
|
102
241
|
static bool function_macro_trailing_argument_prefix(TSLexer *lexer) {
|
|
@@ -173,26 +312,60 @@ static bool function_macro_attribute_keyword(const char *word, bool trailing) {
|
|
|
173
312
|
}
|
|
174
313
|
|
|
175
314
|
static bool function_macro_balanced(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw) {
|
|
315
|
+
return function_macro_group_prefix(lexer, open, close, literal, unterminated_raw, false) == MACRO_GROUP_BALANCED;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
static bool function_macro_modifier_balanced(TSLexer *lexer, int32_t open, int32_t close) {
|
|
319
|
+
lexer->advance(lexer, false);
|
|
320
|
+
return function_macro_group_contents(lexer, open, close, NULL, NULL, false, true) == MACRO_GROUP_BALANCED;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
static MacroGroup function_macro_group_prefix(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw, bool type_argument) {
|
|
324
|
+
lexer->advance(lexer, false);
|
|
325
|
+
if (type_argument && (!scan_pragma_spacing(lexer) || (!preproc_word(lexer->lookahead, false) && lexer->lookahead != '\\'))) {
|
|
326
|
+
return MACRO_GROUP_INVALID;
|
|
327
|
+
}
|
|
328
|
+
return function_macro_group_contents(lexer, open, close, literal, unterminated_raw, type_argument, false);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
static MacroGroup function_macro_group_contents(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw, bool type_argument, bool modifier) {
|
|
176
332
|
unsigned depth = 1;
|
|
177
333
|
unsigned nested = 0;
|
|
334
|
+
// A definition body's own brace permits its member semicolons.
|
|
335
|
+
unsigned braces = open == '{';
|
|
178
336
|
MacroArgumentWord word = {0};
|
|
179
|
-
lexer->advance(lexer, false);
|
|
180
337
|
while (depth) {
|
|
181
|
-
if (lexer->eof(lexer)) return
|
|
338
|
+
if (lexer->eof(lexer)) return MACRO_GROUP_INVALID;
|
|
182
339
|
int32_t c = lexer->lookahead;
|
|
183
|
-
if (c == '
|
|
340
|
+
if (c == '{' && !modifier) return MACRO_GROUP_BRACED;
|
|
341
|
+
if (function_macro_group_separator(c, depth, nested, type_argument, braces)) return MACRO_GROUP_INVALID;
|
|
184
342
|
function_macro_note_literal(literal, depth, nested, &word, c);
|
|
185
343
|
MacroArgumentToken token = function_macro_argument_token(lexer, &word, unterminated_raw);
|
|
186
|
-
if (token == MACRO_ARGUMENT_INVALID) return
|
|
344
|
+
if (token == MACRO_ARGUMENT_INVALID) return MACRO_GROUP_INVALID;
|
|
187
345
|
if (token == MACRO_ARGUMENT_CONSUMED) continue;
|
|
188
346
|
function_macro_argument_word(&word, c);
|
|
189
|
-
if (!function_macro_group(c, open, close, &depth, &nested)) return
|
|
347
|
+
if (!function_macro_group(c, open, close, &depth, &nested, &braces)) return MACRO_GROUP_INVALID;
|
|
190
348
|
lexer->advance(lexer, false);
|
|
191
349
|
}
|
|
192
|
-
return nested == 0;
|
|
350
|
+
return nested == 0 ? MACRO_GROUP_BALANCED : MACRO_GROUP_INVALID;
|
|
193
351
|
}
|
|
194
352
|
|
|
195
|
-
static bool
|
|
353
|
+
static bool function_macro_group_separator(int32_t c, unsigned depth, unsigned nested, bool type_argument, unsigned braces) {
|
|
354
|
+
return ((c == ';' || c == '#') && !braces) || (type_argument && c == ',' && depth == 1 && !nested);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
static bool function_macro_type_prefix_keyword(const MacroName *name, const char *const *reserved_words, unsigned reserved_count) {
|
|
358
|
+
if (!name->ascii) return false;
|
|
359
|
+
if (function_macro_keyword(name->word)) return true;
|
|
360
|
+
for (unsigned i = 0; i < reserved_count; i++) {
|
|
361
|
+
if (strcmp(name->word, reserved_words[i]) == 0) return true;
|
|
362
|
+
}
|
|
363
|
+
return false;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
static bool function_macro_group(int32_t c, int32_t open, int32_t close, unsigned *depth, unsigned *nested, unsigned *braces) {
|
|
367
|
+
if (c == '{') (*braces)++;
|
|
368
|
+
else if (c == '}' && *braces) (*braces)--;
|
|
196
369
|
if (c == open) (*depth)++;
|
|
197
370
|
else if (c == close) (*depth)--;
|
|
198
371
|
else if (c > 0 && c < 128 && strchr("([{", c)) (*nested)++;
|
|
@@ -208,7 +381,7 @@ static void function_macro_note_literal(bool *literal, unsigned depth, unsigned
|
|
|
208
381
|
if (c == '"' || (c == '\'' && !word->number) || (c >= '0' && c <= '9' && !word->identifier)) *literal = true;
|
|
209
382
|
}
|
|
210
383
|
|
|
211
|
-
static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type) {
|
|
384
|
+
static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type, bool allow_braced) {
|
|
212
385
|
MacroPrototypeTail tail = {.post_type = post_type, .explicit_type = post_type};
|
|
213
386
|
for (;;) {
|
|
214
387
|
bool escaped = false;
|
|
@@ -221,26 +394,33 @@ static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type)
|
|
|
221
394
|
} else if (c <= 127 && strchr("{}#)]", c)) {
|
|
222
395
|
return false;
|
|
223
396
|
} else if (c == '(' || c == '[') {
|
|
224
|
-
|
|
397
|
+
MacroGroup group = function_macro_tail_group(lexer, c, &tail);
|
|
398
|
+
if (group != MACRO_GROUP_BALANCED) return tail.unterminated_raw || (allow_braced && group == MACRO_GROUP_BRACED);
|
|
225
399
|
} else if (c == '"' || c == '\'') {
|
|
226
400
|
if (!function_macro_argument_quote(lexer, c, false, NULL)) return false;
|
|
227
|
-
} else {
|
|
228
|
-
|
|
229
|
-
lexer->advance(lexer, false);
|
|
230
|
-
tail.modifier = false;
|
|
401
|
+
} else if (!function_macro_tail_punctuator(lexer, c, &tail)) {
|
|
402
|
+
return false;
|
|
231
403
|
}
|
|
232
404
|
}
|
|
233
405
|
}
|
|
234
406
|
|
|
235
|
-
static bool
|
|
407
|
+
static bool function_macro_tail_punctuator(TSLexer *lexer, int32_t c, MacroPrototypeTail *tail) {
|
|
408
|
+
if (c == '*' && tail->post_type && tail->declarator_name && !tail->declarator_group) return false;
|
|
409
|
+
lexer->advance(lexer, false);
|
|
410
|
+
tail->modifier = false;
|
|
411
|
+
return true;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
static MacroGroup function_macro_tail_group(TSLexer *lexer, int32_t c, MacroPrototypeTail *tail) {
|
|
236
415
|
bool literal = false;
|
|
237
|
-
|
|
416
|
+
MacroGroup group = function_macro_group_prefix(lexer, c, c == '(' ? ')' : ']', &literal, &tail->unterminated_raw, false);
|
|
417
|
+
if (group != MACRO_GROUP_BALANCED) return group;
|
|
238
418
|
if (c == '(' && !tail->modifier) {
|
|
239
419
|
if (literal && tail->post_type && !tail->declarator_group) tail->declarator_name = false;
|
|
240
420
|
else tail->declarator_group = true;
|
|
241
421
|
}
|
|
242
422
|
tail->modifier = false;
|
|
243
|
-
return
|
|
423
|
+
return MACRO_GROUP_BALANCED;
|
|
244
424
|
}
|
|
245
425
|
|
|
246
426
|
static bool function_macro_tail_spacing(TSLexer *lexer, bool *escaped) {
|
package/src/keywords.h
CHANGED
|
@@ -270,6 +270,43 @@ static const char *const function_macro_object_prefix_keywords[] = {
|
|
|
270
270
|
"void",
|
|
271
271
|
"volatile",
|
|
272
272
|
};
|
|
273
|
+
static const char *const function_macro_type_prefix_modifiers[] = {
|
|
274
|
+
"_Alignas",
|
|
275
|
+
"_Atomic",
|
|
276
|
+
"_Nonnull",
|
|
277
|
+
"_Noreturn",
|
|
278
|
+
"_Thread_local",
|
|
279
|
+
"__asm",
|
|
280
|
+
"__asm__",
|
|
281
|
+
"__attribute",
|
|
282
|
+
"__attribute__",
|
|
283
|
+
"__cdecl",
|
|
284
|
+
"__clrcall",
|
|
285
|
+
"__declspec",
|
|
286
|
+
"__extension__",
|
|
287
|
+
"__fastcall",
|
|
288
|
+
"__forceinline",
|
|
289
|
+
"__inline",
|
|
290
|
+
"__inline__",
|
|
291
|
+
"__restrict__",
|
|
292
|
+
"__stdcall",
|
|
293
|
+
"__thiscall",
|
|
294
|
+
"__thread",
|
|
295
|
+
"__vectorcall",
|
|
296
|
+
"alignas",
|
|
297
|
+
"asm",
|
|
298
|
+
"auto",
|
|
299
|
+
"const",
|
|
300
|
+
"constexpr",
|
|
301
|
+
"extern",
|
|
302
|
+
"inline",
|
|
303
|
+
"noreturn",
|
|
304
|
+
"register",
|
|
305
|
+
"restrict",
|
|
306
|
+
"static",
|
|
307
|
+
"thread_local",
|
|
308
|
+
"volatile",
|
|
309
|
+
};
|
|
273
310
|
static const char *const function_macro_declaration_modifiers[] = {
|
|
274
311
|
"_Alignas",
|
|
275
312
|
"_Atomic",
|
package/src/node-types.json
CHANGED
|
@@ -554,6 +554,10 @@
|
|
|
554
554
|
"type": "initializer_pair",
|
|
555
555
|
"named": true
|
|
556
556
|
},
|
|
557
|
+
{
|
|
558
|
+
"type": "parameter_declaration",
|
|
559
|
+
"named": true
|
|
560
|
+
},
|
|
557
561
|
{
|
|
558
562
|
"type": "preproc_arg",
|
|
559
563
|
"named": true
|
|
@@ -1419,25 +1423,13 @@
|
|
|
1419
1423
|
"required": false,
|
|
1420
1424
|
"types": [
|
|
1421
1425
|
{
|
|
1422
|
-
"type": "
|
|
1423
|
-
"named": true
|
|
1424
|
-
},
|
|
1425
|
-
{
|
|
1426
|
-
"type": "attributed_declarator",
|
|
1427
|
-
"named": true
|
|
1428
|
-
},
|
|
1429
|
-
{
|
|
1430
|
-
"type": "function_declarator",
|
|
1426
|
+
"type": "_declarator",
|
|
1431
1427
|
"named": true
|
|
1432
1428
|
},
|
|
1433
1429
|
{
|
|
1434
1430
|
"type": "gnu_asm_expression",
|
|
1435
1431
|
"named": true
|
|
1436
1432
|
},
|
|
1437
|
-
{
|
|
1438
|
-
"type": "identifier",
|
|
1439
|
-
"named": true
|
|
1440
|
-
},
|
|
1441
1433
|
{
|
|
1442
1434
|
"type": "init_declarator",
|
|
1443
1435
|
"named": true
|
|
@@ -1445,14 +1437,6 @@
|
|
|
1445
1437
|
{
|
|
1446
1438
|
"type": "ms_call_modifier",
|
|
1447
1439
|
"named": true
|
|
1448
|
-
},
|
|
1449
|
-
{
|
|
1450
|
-
"type": "parenthesized_declarator",
|
|
1451
|
-
"named": true
|
|
1452
|
-
},
|
|
1453
|
-
{
|
|
1454
|
-
"type": "pointer_declarator",
|
|
1455
|
-
"named": true
|
|
1456
1440
|
}
|
|
1457
1441
|
]
|
|
1458
1442
|
},
|
|
@@ -2744,6 +2728,32 @@
|
|
|
2744
2728
|
}
|
|
2745
2729
|
]
|
|
2746
2730
|
}
|
|
2731
|
+
},
|
|
2732
|
+
"children": {
|
|
2733
|
+
"multiple": true,
|
|
2734
|
+
"required": false,
|
|
2735
|
+
"types": [
|
|
2736
|
+
{
|
|
2737
|
+
"type": "compound_statement",
|
|
2738
|
+
"named": true
|
|
2739
|
+
},
|
|
2740
|
+
{
|
|
2741
|
+
"type": "expression",
|
|
2742
|
+
"named": true
|
|
2743
|
+
},
|
|
2744
|
+
{
|
|
2745
|
+
"type": "initializer_list",
|
|
2746
|
+
"named": true
|
|
2747
|
+
},
|
|
2748
|
+
{
|
|
2749
|
+
"type": "initializer_pair",
|
|
2750
|
+
"named": true
|
|
2751
|
+
},
|
|
2752
|
+
{
|
|
2753
|
+
"type": "type_descriptor",
|
|
2754
|
+
"named": true
|
|
2755
|
+
}
|
|
2756
|
+
]
|
|
2747
2757
|
}
|
|
2748
2758
|
},
|
|
2749
2759
|
{
|
|
@@ -2934,13 +2944,25 @@
|
|
|
2934
2944
|
"named": true
|
|
2935
2945
|
},
|
|
2936
2946
|
{
|
|
2937
|
-
"type": "
|
|
2947
|
+
"type": "expression",
|
|
2948
|
+
"named": true
|
|
2949
|
+
},
|
|
2950
|
+
{
|
|
2951
|
+
"type": "initializer_list",
|
|
2952
|
+
"named": true
|
|
2953
|
+
},
|
|
2954
|
+
{
|
|
2955
|
+
"type": "initializer_pair",
|
|
2938
2956
|
"named": true
|
|
2939
2957
|
},
|
|
2940
2958
|
{
|
|
2941
2959
|
"type": "parameter_declaration",
|
|
2942
2960
|
"named": true
|
|
2943
2961
|
},
|
|
2962
|
+
{
|
|
2963
|
+
"type": "type_descriptor",
|
|
2964
|
+
"named": true
|
|
2965
|
+
},
|
|
2944
2966
|
{
|
|
2945
2967
|
"type": "variadic_parameter",
|
|
2946
2968
|
"named": true
|
|
@@ -3002,6 +3024,18 @@
|
|
|
3002
3024
|
"type": "expression",
|
|
3003
3025
|
"named": true
|
|
3004
3026
|
},
|
|
3027
|
+
{
|
|
3028
|
+
"type": "initializer_list",
|
|
3029
|
+
"named": true
|
|
3030
|
+
},
|
|
3031
|
+
{
|
|
3032
|
+
"type": "initializer_pair",
|
|
3033
|
+
"named": true
|
|
3034
|
+
},
|
|
3035
|
+
{
|
|
3036
|
+
"type": "parameter_declaration",
|
|
3037
|
+
"named": true
|
|
3038
|
+
},
|
|
3005
3039
|
{
|
|
3006
3040
|
"type": "preproc_arg",
|
|
3007
3041
|
"named": true
|
|
@@ -3009,6 +3043,10 @@
|
|
|
3009
3043
|
{
|
|
3010
3044
|
"type": "preproc_defined",
|
|
3011
3045
|
"named": true
|
|
3046
|
+
},
|
|
3047
|
+
{
|
|
3048
|
+
"type": "type_descriptor",
|
|
3049
|
+
"named": true
|
|
3012
3050
|
}
|
|
3013
3051
|
]
|
|
3014
3052
|
}
|
package/src/pragma.h
CHANGED
|
@@ -14,6 +14,7 @@ typedef struct {
|
|
|
14
14
|
unsigned length;
|
|
15
15
|
bool ascii;
|
|
16
16
|
bool lookahead_only;
|
|
17
|
+
bool preserve_end;
|
|
17
18
|
} MacroName;
|
|
18
19
|
|
|
19
20
|
typedef enum {
|
|
@@ -37,6 +38,7 @@ static bool scan_macro_name_ucn(TSLexer *lexer);
|
|
|
37
38
|
static bool scan_pragma_argument(TSLexer *lexer);
|
|
38
39
|
static bool scan_pragma_spacing(TSLexer *lexer);
|
|
39
40
|
static bool scan_pragma_suffix(TSLexer *lexer);
|
|
41
|
+
static bool scan_pragma_suffix_end(TSLexer *lexer, bool mark_end);
|
|
40
42
|
static bool pragma_space(int32_t c);
|
|
41
43
|
static bool scan_pragma_word(TSLexer *lexer);
|
|
42
44
|
static bool scan_preproc_newline(TSLexer *lexer, bool skip);
|
|
@@ -55,13 +57,17 @@ static bool scan_pragma(TSLexer *lexer) {
|
|
|
55
57
|
}
|
|
56
58
|
|
|
57
59
|
static bool scan_pragma_suffix(TSLexer *lexer) {
|
|
60
|
+
return scan_pragma_suffix_end(lexer, true);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static bool scan_pragma_suffix_end(TSLexer *lexer, bool mark_end) {
|
|
58
64
|
if (!scan_pragma_spacing(lexer) || lexer->lookahead != '(') return false;
|
|
59
65
|
lexer->advance(lexer, false);
|
|
60
66
|
if (!scan_pragma_spacing(lexer)) return false;
|
|
61
67
|
if (!scan_pragma_argument(lexer)) return false;
|
|
62
68
|
if (!scan_pragma_spacing(lexer) || lexer->lookahead != ')') return false;
|
|
63
69
|
lexer->advance(lexer, false);
|
|
64
|
-
lexer->mark_end(lexer);
|
|
70
|
+
if (mark_end) lexer->mark_end(lexer);
|
|
65
71
|
return true;
|
|
66
72
|
}
|
|
67
73
|
|
|
@@ -414,7 +420,7 @@ static bool scan_function_macro_name(TSLexer *lexer, bool allow_pragma, TSSymbol
|
|
|
414
420
|
static bool scan_macro_name(TSLexer *lexer, MacroName *name) {
|
|
415
421
|
bool has_name = false;
|
|
416
422
|
for (;;) {
|
|
417
|
-
if (!has_name) scan_macro_name_start(lexer, name->lookahead_only);
|
|
423
|
+
if (!has_name) scan_macro_name_start(lexer, name->lookahead_only && !name->preserve_end);
|
|
418
424
|
MacroNameCharacter character = scan_macro_name_character(lexer, has_name, name);
|
|
419
425
|
if (character == MACRO_NAME_INVALID) return false;
|
|
420
426
|
if (character == MACRO_NAME_END) return has_name;
|
package/src/scanner.c
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#include "conditionalMacro.h"
|
|
2
2
|
#include "gnuRawString.h"
|
|
3
3
|
|
|
4
|
-
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, GNU_RAW_STRING_BODY, RAW_MACRO_TYPE_TAIL };
|
|
4
|
+
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, GNU_RAW_STRING_BODY, RAW_MACRO_TYPE_TAIL, BRACED_FUNCTION_ATTRIBUTE_START, BRACED_MACRO_TYPE_START };
|
|
5
5
|
|
|
6
6
|
static bool scan_preproc_tokens(TSLexer *lexer, const bool *valid_symbols);
|
|
7
7
|
|
|
@@ -54,15 +54,21 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
|
|
|
54
54
|
if (lexer->lookahead == '#') return scan_conditional_macro(lexer, &symbols, valid_symbols);
|
|
55
55
|
}
|
|
56
56
|
static const TSSymbol attribute_tokens[] = {
|
|
57
|
-
FUNCTION_ATTRIBUTE_START,
|
|
58
|
-
TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START,
|
|
57
|
+
FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX, POST_TYPE_FUNCTION_ATTRIBUTE_START,
|
|
58
|
+
TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START, BRACED_FUNCTION_ATTRIBUTE_START,
|
|
59
59
|
};
|
|
60
60
|
const MacroAttributeSymbols symbols = {
|
|
61
61
|
.pragma = PRAGMA_OPERATOR,
|
|
62
62
|
.object_start = TRAILING_OBJECT_DECLARATION_START,
|
|
63
63
|
.allow_pragma = valid_symbols[PRAGMA_OPERATOR],
|
|
64
64
|
.allow_object_start = valid_symbols[TRAILING_OBJECT_DECLARATION_START],
|
|
65
|
+
.braced_start = BRACED_FUNCTION_ATTRIBUTE_START,
|
|
66
|
+
.allow_braced_start = valid_symbols[BRACED_FUNCTION_ATTRIBUTE_START],
|
|
65
67
|
};
|
|
68
|
+
if (valid_symbols[BRACED_MACRO_TYPE_START]) {
|
|
69
|
+
lexer->result_symbol = BRACED_MACRO_TYPE_START;
|
|
70
|
+
return scan_braced_macro_type_start(lexer, &symbols, NULL, 0);
|
|
71
|
+
}
|
|
66
72
|
for (unsigned i = 0; i < sizeof(attribute_tokens) / sizeof(attribute_tokens[0]); i++) {
|
|
67
73
|
TSSymbol token = attribute_tokens[i];
|
|
68
74
|
if (!valid_symbols[token]) continue;
|
package/tree-sitter-c.wasm
CHANGED
|
Binary file
|
package/tree-sitter.json
CHANGED
|
@@ -10,8 +10,6 @@
|
|
|
10
10
|
"c",
|
|
11
11
|
"h"
|
|
12
12
|
],
|
|
13
|
-
"highlights": "queries/highlights.scm",
|
|
14
|
-
"tags": "queries/tags.scm",
|
|
15
13
|
"injection-regex": "^(c|h)$",
|
|
16
14
|
"external-files": [
|
|
17
15
|
"src/pragma.h",
|
|
@@ -24,7 +22,7 @@
|
|
|
24
22
|
}
|
|
25
23
|
],
|
|
26
24
|
"metadata": {
|
|
27
|
-
"version": "
|
|
25
|
+
"version": "21.0.0",
|
|
28
26
|
"license": "MIT",
|
|
29
27
|
"description": "C grammar for tree-sitter",
|
|
30
28
|
"authors": [
|