@willbooster/tree-sitter-c 18.3.0 → 20.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 +80 -16
- package/grammar.js +583 -98
- package/package.json +4 -3
- package/queries/highlights.scm +1 -0
- package/src/conditionalMacro.h +2 -2
- package/src/functionMacro.h +285 -51
- package/src/gnuRawString.h +70 -0
- package/src/keywords.h +37 -0
- package/src/node-types.json +113 -24
- package/src/pragma.h +58 -7
- package/src/scanner.c +22 -5
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +3 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/tree-sitter-c",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "20.0.0",
|
|
4
4
|
"description": "C grammar for tree-sitter",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"c",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"src/keywords.h",
|
|
28
28
|
"src/functionMacro.h",
|
|
29
29
|
"src/conditionalMacro.h",
|
|
30
|
+
"src/gnuRawString.h",
|
|
30
31
|
"src/scanner.c"
|
|
31
32
|
],
|
|
32
33
|
"publishConfig": {
|
|
@@ -58,7 +59,7 @@
|
|
|
58
59
|
"@vitest/browser-playwright": "5.0.5",
|
|
59
60
|
"@willbooster/oxlint-config": "1.4.9",
|
|
60
61
|
"@willbooster/wb": "22.29.3",
|
|
61
|
-
"@willbooster/web-tree-sitter": "15.0.
|
|
62
|
+
"@willbooster/web-tree-sitter": "15.0.3",
|
|
62
63
|
"conventional-changelog-conventionalcommits": "10.4.1",
|
|
63
64
|
"lefthook": "2.2.2",
|
|
64
65
|
"oxlint": "1.88.0",
|
|
@@ -69,7 +70,7 @@
|
|
|
69
70
|
"wrangler": "4.151.1"
|
|
70
71
|
},
|
|
71
72
|
"peerDependencies": {
|
|
72
|
-
"@willbooster/web-tree-sitter": "^15.0.
|
|
73
|
+
"@willbooster/web-tree-sitter": "^15.0.3"
|
|
73
74
|
},
|
|
74
75
|
"peerDependenciesMeta": {
|
|
75
76
|
"@willbooster/web-tree-sitter": {
|
package/queries/highlights.scm
CHANGED
package/src/conditionalMacro.h
CHANGED
|
@@ -134,14 +134,14 @@ static bool conditional_macro_modifier_group(TSLexer *lexer, const char *word, b
|
|
|
134
134
|
sizeof(function_macro_group_modifiers) / sizeof(function_macro_group_modifiers[0]));
|
|
135
135
|
if (!grouped || lexer->lookahead != '(') return true;
|
|
136
136
|
bool literal = false;
|
|
137
|
-
return function_macro_balanced(lexer, '(', ')', &literal) && conditional_macro_skip_lines(lexer, escaped);
|
|
137
|
+
return function_macro_balanced(lexer, '(', ')', &literal, NULL) && conditional_macro_skip_lines(lexer, escaped);
|
|
138
138
|
}
|
|
139
139
|
|
|
140
140
|
static bool conditional_macro_attribute_declaration(TSLexer *lexer, bool *escaped) {
|
|
141
141
|
lexer->advance(lexer, false);
|
|
142
142
|
if (!conditional_macro_skip_lines(lexer, escaped) || lexer->lookahead != '[') return false;
|
|
143
143
|
bool literal = false;
|
|
144
|
-
if (!function_macro_balanced(lexer, '[', ']', &literal) ||
|
|
144
|
+
if (!function_macro_balanced(lexer, '[', ']', &literal, NULL) ||
|
|
145
145
|
!conditional_macro_skip_lines(lexer, escaped) || lexer->lookahead != ']') return false;
|
|
146
146
|
lexer->advance(lexer, false);
|
|
147
147
|
return conditional_macro_skip_lines(lexer, escaped);
|
package/src/functionMacro.h
CHANGED
|
@@ -8,14 +8,22 @@ typedef struct {
|
|
|
8
8
|
bool identifier;
|
|
9
9
|
bool number;
|
|
10
10
|
int32_t last;
|
|
11
|
+
GnuRawStringPrefix raw_prefix;
|
|
11
12
|
} MacroArgumentWord;
|
|
12
13
|
|
|
14
|
+
typedef enum {
|
|
15
|
+
MACRO_ARGUMENT_PLAIN,
|
|
16
|
+
MACRO_ARGUMENT_CONSUMED,
|
|
17
|
+
MACRO_ARGUMENT_INVALID,
|
|
18
|
+
} MacroArgumentToken;
|
|
19
|
+
|
|
13
20
|
typedef struct {
|
|
14
21
|
bool post_type;
|
|
15
22
|
bool declarator_group;
|
|
16
23
|
bool explicit_type;
|
|
17
24
|
bool modifier;
|
|
18
25
|
bool declarator_name;
|
|
26
|
+
bool unterminated_raw;
|
|
19
27
|
} MacroPrototypeTail;
|
|
20
28
|
|
|
21
29
|
typedef struct {
|
|
@@ -23,15 +31,29 @@ typedef struct {
|
|
|
23
31
|
TSSymbol object_start;
|
|
24
32
|
bool allow_pragma;
|
|
25
33
|
bool allow_object_start;
|
|
34
|
+
TSSymbol braced_start;
|
|
35
|
+
bool allow_braced_start;
|
|
26
36
|
} MacroAttributeSymbols;
|
|
27
37
|
|
|
28
|
-
|
|
29
|
-
|
|
38
|
+
typedef enum {
|
|
39
|
+
MACRO_GROUP_INVALID, MACRO_GROUP_BALANCED, MACRO_GROUP_BRACED,
|
|
40
|
+
} MacroGroup;
|
|
41
|
+
|
|
42
|
+
static MacroArgumentToken function_macro_argument_token(TSLexer *lexer, MacroArgumentWord *word, bool *unterminated_raw);
|
|
43
|
+
|
|
44
|
+
static bool function_macro_balanced(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw);
|
|
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);
|
|
30
51
|
static void function_macro_note_literal(bool *literal, unsigned depth, unsigned nested, const MacroArgumentWord *word, int32_t c);
|
|
31
52
|
static bool function_macro_tail_word(TSLexer *lexer, bool escaped, MacroPrototypeTail *tail);
|
|
32
|
-
static
|
|
33
|
-
static bool
|
|
34
|
-
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);
|
|
56
|
+
static bool function_macro_argument_quote(TSLexer *lexer, int32_t quote, bool raw, bool *unterminated_raw);
|
|
35
57
|
static bool function_macro_argument_escape(TSLexer *lexer, MacroArgumentWord *word);
|
|
36
58
|
static bool function_macro_argument_slash(TSLexer *lexer, bool allow_division);
|
|
37
59
|
static bool function_macro_tail_spacing(TSLexer *lexer, bool *escaped);
|
|
@@ -48,25 +70,172 @@ static bool function_macro_attribute_keyword(const char *word, bool trailing);
|
|
|
48
70
|
static bool function_macro_keyword(const char *word);
|
|
49
71
|
static bool function_macro_word_in(const char *word, const char *const *words, unsigned count);
|
|
50
72
|
|
|
51
|
-
static bool
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
73
|
+
static inline bool scan_raw_macro_type_tail(TSLexer *lexer) {
|
|
74
|
+
while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
|
|
75
|
+
if (lexer->lookahead == ')' || lexer->eof(lexer)) return false;
|
|
76
|
+
unsigned depth = 1;
|
|
77
|
+
unsigned nested = 0;
|
|
78
|
+
unsigned braces = 0;
|
|
79
|
+
bool balanced = true;
|
|
80
|
+
MacroArgumentWord word = {0};
|
|
81
|
+
while (!lexer->eof(lexer)) {
|
|
82
|
+
int32_t c = lexer->lookahead;
|
|
83
|
+
if (c == ')' && depth == 1) break;
|
|
84
|
+
if (function_macro_argument_token(lexer, &word, NULL) != MACRO_ARGUMENT_PLAIN) continue;
|
|
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++;
|
|
89
|
+
else if (c == ')') depth--;
|
|
90
|
+
function_macro_argument_word(&word, c);
|
|
91
|
+
lexer->advance(lexer, false);
|
|
92
|
+
}
|
|
93
|
+
lexer->mark_end(lexer);
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
|
|
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) {
|
|
56
100
|
lexer->result_symbol = symbols->pragma;
|
|
57
101
|
return symbols->allow_pragma && scan_pragma_suffix(lexer);
|
|
58
102
|
}
|
|
59
|
-
if (symbols->allow_object_start && name
|
|
103
|
+
if (symbols->allow_object_start && name->ascii && function_macro_scalar_type_keyword(name->word)) {
|
|
60
104
|
lexer->result_symbol = symbols->object_start;
|
|
61
105
|
return function_macro_object_prefix(lexer);
|
|
62
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;
|
|
63
119
|
if (lexer->result_symbol == symbols->object_start) return false;
|
|
64
120
|
if (name.ascii && function_macro_attribute_keyword(word, trailing)) return false;
|
|
65
121
|
if (!scan_pragma_spacing(lexer) || lexer->lookahead != '(') return false;
|
|
66
122
|
if (trailing) return function_macro_trailing_argument_prefix(lexer);
|
|
67
123
|
bool literal = false;
|
|
68
|
-
|
|
69
|
-
|
|
124
|
+
bool unterminated_raw = false;
|
|
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;
|
|
70
239
|
}
|
|
71
240
|
|
|
72
241
|
static bool function_macro_trailing_argument_prefix(TSLexer *lexer) {
|
|
@@ -80,6 +249,15 @@ static bool function_macro_trailing_argument_prefix(TSLexer *lexer) {
|
|
|
80
249
|
first = lexer->lookahead;
|
|
81
250
|
return first >= '0' && first <= '9';
|
|
82
251
|
}
|
|
252
|
+
if (first == 'R' || first == 'L' || first == 'u' || first == 'U') {
|
|
253
|
+
GnuRawStringPrefix prefix = {0};
|
|
254
|
+
while (lexer->lookahead != '"' && prefix.length < sizeof(prefix.value)) {
|
|
255
|
+
gnu_raw_string_prefix_character(&prefix, lexer->lookahead);
|
|
256
|
+
lexer->advance(lexer, false);
|
|
257
|
+
if (!scan_preproc_splices(lexer)) return false;
|
|
258
|
+
}
|
|
259
|
+
return lexer->lookahead == '"' && gnu_raw_string_prefix(&prefix);
|
|
260
|
+
}
|
|
83
261
|
return first == ',' || first == '"' || first == '\'' || (first >= '0' && first <= '9');
|
|
84
262
|
}
|
|
85
263
|
|
|
@@ -133,43 +311,61 @@ static bool function_macro_attribute_keyword(const char *word, bool trailing) {
|
|
|
133
311
|
sizeof(function_macro_group_modifiers) / sizeof(function_macro_group_modifiers[0]));
|
|
134
312
|
}
|
|
135
313
|
|
|
136
|
-
static bool function_macro_balanced(TSLexer *lexer, int32_t open, int32_t close, bool *literal) {
|
|
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) {
|
|
137
332
|
unsigned depth = 1;
|
|
138
333
|
unsigned nested = 0;
|
|
334
|
+
// A definition body's own brace permits its member semicolons.
|
|
335
|
+
unsigned braces = open == '{';
|
|
139
336
|
MacroArgumentWord word = {0};
|
|
140
|
-
lexer->advance(lexer, false);
|
|
141
337
|
while (depth) {
|
|
142
|
-
if (lexer->eof(lexer)) return
|
|
338
|
+
if (lexer->eof(lexer)) return MACRO_GROUP_INVALID;
|
|
143
339
|
int32_t c = lexer->lookahead;
|
|
144
|
-
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;
|
|
145
342
|
function_macro_note_literal(literal, depth, nested, &word, c);
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
if (c == '/') {
|
|
152
|
-
if (!function_macro_argument_slash(lexer, true)) return false;
|
|
153
|
-
word = (MacroArgumentWord){0};
|
|
154
|
-
continue;
|
|
155
|
-
}
|
|
156
|
-
if (c == '\\') {
|
|
157
|
-
if (!function_macro_argument_escape(lexer, &word)) return false;
|
|
158
|
-
continue;
|
|
159
|
-
}
|
|
160
|
-
if (c == '"' || (c == '\'' && !word.number)) {
|
|
161
|
-
if (!function_macro_argument_quote(lexer, c)) return false;
|
|
162
|
-
word = (MacroArgumentWord){0};
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
343
|
+
MacroArgumentToken token = function_macro_argument_token(lexer, &word, unterminated_raw);
|
|
344
|
+
if (token == MACRO_ARGUMENT_INVALID) return MACRO_GROUP_INVALID;
|
|
345
|
+
if (token == MACRO_ARGUMENT_CONSUMED) continue;
|
|
165
346
|
function_macro_argument_word(&word, c);
|
|
166
|
-
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;
|
|
167
348
|
lexer->advance(lexer, false);
|
|
168
349
|
}
|
|
169
|
-
return nested == 0;
|
|
350
|
+
return nested == 0 ? MACRO_GROUP_BALANCED : MACRO_GROUP_INVALID;
|
|
351
|
+
}
|
|
352
|
+
|
|
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;
|
|
170
364
|
}
|
|
171
365
|
|
|
172
|
-
static bool function_macro_group(int32_t c, int32_t open, int32_t close, unsigned *depth, unsigned *nested) {
|
|
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)--;
|
|
173
369
|
if (c == open) (*depth)++;
|
|
174
370
|
else if (c == close) (*depth)--;
|
|
175
371
|
else if (c > 0 && c < 128 && strchr("([{", c)) (*nested)++;
|
|
@@ -185,7 +381,7 @@ static void function_macro_note_literal(bool *literal, unsigned depth, unsigned
|
|
|
185
381
|
if (c == '"' || (c == '\'' && !word->number) || (c >= '0' && c <= '9' && !word->identifier)) *literal = true;
|
|
186
382
|
}
|
|
187
383
|
|
|
188
|
-
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) {
|
|
189
385
|
MacroPrototypeTail tail = {.post_type = post_type, .explicit_type = post_type};
|
|
190
386
|
for (;;) {
|
|
191
387
|
bool escaped = false;
|
|
@@ -198,26 +394,33 @@ static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type)
|
|
|
198
394
|
} else if (c <= 127 && strchr("{}#)]", c)) {
|
|
199
395
|
return false;
|
|
200
396
|
} else if (c == '(' || c == '[') {
|
|
201
|
-
|
|
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);
|
|
202
399
|
} else if (c == '"' || c == '\'') {
|
|
203
|
-
if (!function_macro_argument_quote(lexer, c)) return false;
|
|
204
|
-
} else {
|
|
205
|
-
|
|
206
|
-
lexer->advance(lexer, false);
|
|
207
|
-
tail.modifier = false;
|
|
400
|
+
if (!function_macro_argument_quote(lexer, c, false, NULL)) return false;
|
|
401
|
+
} else if (!function_macro_tail_punctuator(lexer, c, &tail)) {
|
|
402
|
+
return false;
|
|
208
403
|
}
|
|
209
404
|
}
|
|
210
405
|
}
|
|
211
406
|
|
|
212
|
-
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) {
|
|
213
415
|
bool literal = false;
|
|
214
|
-
|
|
416
|
+
MacroGroup group = function_macro_group_prefix(lexer, c, c == '(' ? ')' : ']', &literal, &tail->unterminated_raw, false);
|
|
417
|
+
if (group != MACRO_GROUP_BALANCED) return group;
|
|
215
418
|
if (c == '(' && !tail->modifier) {
|
|
216
419
|
if (literal && tail->post_type && !tail->declarator_group) tail->declarator_name = false;
|
|
217
420
|
else tail->declarator_group = true;
|
|
218
421
|
}
|
|
219
422
|
tail->modifier = false;
|
|
220
|
-
return
|
|
423
|
+
return MACRO_GROUP_BALANCED;
|
|
221
424
|
}
|
|
222
425
|
|
|
223
426
|
static bool function_macro_tail_spacing(TSLexer *lexer, bool *escaped) {
|
|
@@ -262,13 +465,39 @@ static bool function_macro_tail_word(TSLexer *lexer, bool escaped, MacroPrototyp
|
|
|
262
465
|
static bool function_macro_argument_escape(TSLexer *lexer, MacroArgumentWord *word) {
|
|
263
466
|
lexer->advance(lexer, false);
|
|
264
467
|
if (scan_preproc_newline(lexer, false)) return true;
|
|
468
|
+
gnu_raw_string_prefix_invalidate(&word->raw_prefix);
|
|
265
469
|
if (!scan_preproc_ucn(lexer, &word->number)) return false;
|
|
266
470
|
word->identifier = !word->number;
|
|
267
471
|
return true;
|
|
268
472
|
}
|
|
269
473
|
|
|
270
|
-
static
|
|
474
|
+
static MacroArgumentToken function_macro_argument_token(TSLexer *lexer, MacroArgumentWord *word, bool *unterminated_raw) {
|
|
475
|
+
int32_t c = lexer->lookahead;
|
|
476
|
+
if (pragma_space(c)) {
|
|
477
|
+
lexer->advance(lexer, false);
|
|
478
|
+
*word = (MacroArgumentWord){0};
|
|
479
|
+
} else if (c == '/') {
|
|
480
|
+
if (!function_macro_argument_slash(lexer, true)) return MACRO_ARGUMENT_INVALID;
|
|
481
|
+
*word = (MacroArgumentWord){0};
|
|
482
|
+
} else if (c == '\\') {
|
|
483
|
+
if (!function_macro_argument_escape(lexer, word)) return MACRO_ARGUMENT_INVALID;
|
|
484
|
+
} else if (c == '"' || (c == '\'' && !word->number)) {
|
|
485
|
+
if (!function_macro_argument_quote(lexer, c,
|
|
486
|
+
c == '"' && gnu_raw_string_prefix(&word->raw_prefix), unterminated_raw)) return MACRO_ARGUMENT_INVALID;
|
|
487
|
+
*word = (MacroArgumentWord){0};
|
|
488
|
+
} else {
|
|
489
|
+
return MACRO_ARGUMENT_PLAIN;
|
|
490
|
+
}
|
|
491
|
+
return MACRO_ARGUMENT_CONSUMED;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
static bool function_macro_argument_quote(TSLexer *lexer, int32_t quote, bool raw, bool *unterminated_raw) {
|
|
271
495
|
lexer->advance(lexer, false);
|
|
496
|
+
if (raw) {
|
|
497
|
+
bool terminated = scan_gnu_raw_string_end(lexer);
|
|
498
|
+
if (!terminated && unterminated_raw) *unterminated_raw = lexer->eof(lexer);
|
|
499
|
+
return terminated;
|
|
500
|
+
}
|
|
272
501
|
while (!lexer->eof(lexer)) {
|
|
273
502
|
int32_t c = lexer->lookahead;
|
|
274
503
|
lexer->advance(lexer, false);
|
|
@@ -328,7 +557,12 @@ static void function_macro_argument_word(MacroArgumentWord *word, int32_t c) {
|
|
|
328
557
|
if (!member && !digit && c != '.' && c != '\'' && !exponent_sign) *word = (MacroArgumentWord){0};
|
|
329
558
|
if (digit && !word->identifier) word->number = true;
|
|
330
559
|
if (member && !word->number) word->identifier = true;
|
|
331
|
-
if (word->number)
|
|
560
|
+
if (word->number) {
|
|
561
|
+
word->last = c;
|
|
562
|
+
gnu_raw_string_prefix_invalidate(&word->raw_prefix);
|
|
563
|
+
}
|
|
564
|
+
else if (member || digit || exponent_sign) gnu_raw_string_prefix_character(&word->raw_prefix, c);
|
|
565
|
+
else word->raw_prefix = (GnuRawStringPrefix){0};
|
|
332
566
|
}
|
|
333
567
|
|
|
334
568
|
static bool function_macro_keyword(const char *word) {
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
#ifndef TREE_SITTER_C_GNU_RAW_STRING_H_
|
|
2
|
+
#define TREE_SITTER_C_GNU_RAW_STRING_H_
|
|
3
|
+
|
|
4
|
+
#include "tree_sitter/parser.h"
|
|
5
|
+
#include <string.h>
|
|
6
|
+
|
|
7
|
+
typedef struct {
|
|
8
|
+
char value[4];
|
|
9
|
+
unsigned length;
|
|
10
|
+
} GnuRawStringPrefix;
|
|
11
|
+
|
|
12
|
+
static bool scan_gnu_raw_string_body(TSLexer *lexer);
|
|
13
|
+
static void gnu_raw_string_prefix_invalidate(GnuRawStringPrefix *prefix);
|
|
14
|
+
|
|
15
|
+
static inline bool scan_gnu_raw_string_token(TSLexer *lexer) {
|
|
16
|
+
if (!scan_gnu_raw_string_body(lexer)) return false;
|
|
17
|
+
lexer->mark_end(lexer);
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static bool scan_gnu_raw_string_end(TSLexer *lexer) {
|
|
22
|
+
if (!scan_gnu_raw_string_body(lexer) || lexer->lookahead != '"') return false;
|
|
23
|
+
lexer->advance(lexer, false);
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static void gnu_raw_string_prefix_character(GnuRawStringPrefix *prefix, int32_t c) {
|
|
28
|
+
if (prefix->length < sizeof(prefix->value) - 1 && c > 0 && c < 128) {
|
|
29
|
+
prefix->value[prefix->length++] = (char)c;
|
|
30
|
+
prefix->value[prefix->length] = 0;
|
|
31
|
+
} else {
|
|
32
|
+
gnu_raw_string_prefix_invalidate(prefix);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static void gnu_raw_string_prefix_invalidate(GnuRawStringPrefix *prefix) {
|
|
37
|
+
prefix->length = sizeof(prefix->value);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static bool gnu_raw_string_prefix(const GnuRawStringPrefix *prefix) {
|
|
41
|
+
return prefix->length < sizeof(prefix->value) &&
|
|
42
|
+
(strcmp(prefix->value, "R") == 0 || strcmp(prefix->value, "LR") == 0 ||
|
|
43
|
+
strcmp(prefix->value, "uR") == 0 || strcmp(prefix->value, "UR") == 0 ||
|
|
44
|
+
strcmp(prefix->value, "u8R") == 0);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
static bool scan_gnu_raw_string_body(TSLexer *lexer) {
|
|
48
|
+
char delimiter[16];
|
|
49
|
+
unsigned length = 0;
|
|
50
|
+
while (lexer->lookahead != '(') {
|
|
51
|
+
int32_t c = lexer->lookahead;
|
|
52
|
+
if (lexer->eof(lexer) || length == sizeof delimiter || c < 0x21 || c > 0x7e || c == ')' || c == '\\') return false;
|
|
53
|
+
delimiter[length++] = (char)c;
|
|
54
|
+
lexer->advance(lexer, false);
|
|
55
|
+
}
|
|
56
|
+
lexer->advance(lexer, false);
|
|
57
|
+
for (int delimiter_index = -1; !lexer->eof(lexer); lexer->advance(lexer, false)) {
|
|
58
|
+
if (delimiter_index >= 0 && (unsigned)delimiter_index == length && lexer->lookahead == '"') break;
|
|
59
|
+
if (delimiter_index >= 0 && (unsigned)delimiter_index < length &&
|
|
60
|
+
lexer->lookahead == delimiter[delimiter_index]) {
|
|
61
|
+
delimiter_index++;
|
|
62
|
+
} else {
|
|
63
|
+
delimiter_index = -1;
|
|
64
|
+
}
|
|
65
|
+
if (lexer->lookahead == ')') delimiter_index = 0;
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
#endif
|
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",
|