@willbooster/tree-sitter-c 24.0.1 → 25.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 +13 -6
- package/grammar.js +9 -1
- package/package.json +2 -1
- package/src/conditionalMacro.h +23 -19
- package/src/emptyDirectives.h +439 -0
- package/src/functionMacro.h +83 -59
- package/src/macroProof.h +8 -3
- package/src/node-types.json +5 -0
- package/src/pragma.h +33 -5
- package/src/scanner.c +66 -21
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +2 -1
package/src/functionMacro.h
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// code-gauge-allow-file fileNcss
|
|
1
|
+
// code-gauge-allow-file fileNcss 593 -- Annotation and argument scanners share lexical helpers and keyword tables.
|
|
2
2
|
#ifndef TREE_SITTER_C_FUNCTION_MACRO_H_
|
|
3
3
|
#define TREE_SITTER_C_FUNCTION_MACRO_H_
|
|
4
4
|
|
|
@@ -29,13 +29,14 @@ typedef struct {
|
|
|
29
29
|
} MacroPrototypeTail;
|
|
30
30
|
|
|
31
31
|
typedef struct {
|
|
32
|
+
PragmaTrivia trivia;
|
|
33
|
+
MacroProofMemo *proof_memo;
|
|
32
34
|
TSSymbol pragma;
|
|
33
35
|
TSSymbol object_start;
|
|
34
36
|
bool allow_pragma;
|
|
35
37
|
bool allow_object_start;
|
|
36
38
|
TSSymbol braced_start;
|
|
37
39
|
bool allow_braced_start;
|
|
38
|
-
MacroProofMemo *proof_memo;
|
|
39
40
|
} MacroAttributeSymbols;
|
|
40
41
|
|
|
41
42
|
typedef enum {
|
|
@@ -44,28 +45,28 @@ typedef enum {
|
|
|
44
45
|
|
|
45
46
|
static MacroArgumentToken function_macro_argument_token(TSLexer *lexer, MacroArgumentWord *word, bool *unterminated_raw);
|
|
46
47
|
|
|
47
|
-
static bool function_macro_balanced(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw);
|
|
48
|
-
static bool function_macro_modifier_balanced(TSLexer *lexer, int32_t open, int32_t close);
|
|
49
|
-
static MacroGroup function_macro_group_prefix(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw, bool type_argument);
|
|
50
|
-
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_balanced(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw, const PragmaTrivia *trivia);
|
|
49
|
+
static bool function_macro_modifier_balanced(TSLexer *lexer, int32_t open, int32_t close, const PragmaTrivia *trivia);
|
|
50
|
+
static MacroGroup function_macro_group_prefix(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw, bool type_argument, const PragmaTrivia *trivia);
|
|
51
|
+
static MacroGroup function_macro_group_contents(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw, bool type_argument, bool modifier, const PragmaTrivia *trivia);
|
|
51
52
|
static bool function_macro_group_separator(int32_t c, unsigned depth, unsigned nested, bool type_argument, unsigned braces);
|
|
52
53
|
static bool function_macro_type_prefix_keyword(const MacroName *name, const char *const *reserved_words, unsigned reserved_count);
|
|
53
54
|
static bool function_macro_group(int32_t c, int32_t open, int32_t close, unsigned *depth, unsigned *nested, unsigned *braces);
|
|
54
55
|
static void function_macro_note_literal(bool *literal, unsigned depth, unsigned nested, const MacroArgumentWord *word, int32_t c);
|
|
55
56
|
static bool function_macro_tail_word(TSLexer *lexer, bool escaped, MacroPrototypeTail *tail);
|
|
56
|
-
static MacroGroup function_macro_tail_group(TSLexer *lexer, int32_t c, MacroPrototypeTail *tail);
|
|
57
|
+
static MacroGroup function_macro_tail_group(TSLexer *lexer, int32_t c, MacroPrototypeTail *tail, const PragmaTrivia *trivia);
|
|
57
58
|
static bool function_macro_tail_punctuator(TSLexer *lexer, int32_t c, MacroPrototypeTail *tail);
|
|
58
|
-
static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type, bool allow_braced);
|
|
59
|
+
static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type, bool allow_braced, const PragmaTrivia *trivia);
|
|
59
60
|
static bool function_macro_argument_quote(TSLexer *lexer, int32_t quote, bool raw, bool *unterminated_raw);
|
|
60
61
|
static bool function_macro_argument_escape(TSLexer *lexer, MacroArgumentWord *word);
|
|
61
62
|
static bool function_macro_argument_slash(TSLexer *lexer, bool allow_division);
|
|
62
|
-
static bool function_macro_tail_spacing(TSLexer *lexer, bool *escaped);
|
|
63
|
+
static bool function_macro_tail_spacing(TSLexer *lexer, bool *escaped, const PragmaTrivia *trivia);
|
|
63
64
|
static bool function_macro_line_comment(TSLexer *lexer);
|
|
64
65
|
static bool function_macro_block_comment(TSLexer *lexer);
|
|
65
66
|
static void function_macro_argument_word(MacroArgumentWord *word, int32_t c);
|
|
66
67
|
|
|
67
|
-
static bool function_macro_trailing_argument_prefix(TSLexer *lexer);
|
|
68
|
-
static bool function_macro_object_prefix(TSLexer *lexer);
|
|
68
|
+
static bool function_macro_trailing_argument_prefix(TSLexer *lexer, const PragmaTrivia *trivia);
|
|
69
|
+
static bool function_macro_object_prefix(TSLexer *lexer, const PragmaTrivia *trivia);
|
|
69
70
|
static bool function_macro_object_word(TSLexer *lexer, MacroName *name, bool escaped);
|
|
70
71
|
static bool function_macro_scalar_type_keyword(const char *word);
|
|
71
72
|
static bool function_macro_object_prefix_keyword(const char *word);
|
|
@@ -73,7 +74,9 @@ static bool function_macro_attribute_keyword(const char *word, bool trailing);
|
|
|
73
74
|
static bool function_macro_keyword(const char *word);
|
|
74
75
|
static bool function_macro_word_in(const char *word, const char *const *words, unsigned count);
|
|
75
76
|
|
|
76
|
-
|
|
77
|
+
// code-gauge-allow functionCognitiveComplexity 18 -- Balanced delimiter recovery, trivia, and braced handoff share one consumed group.
|
|
78
|
+
// code-gauge-allow functionNcss 33 -- Balanced delimiter recovery, trivia, and braced handoff share one consumed group.
|
|
79
|
+
static inline bool scan_raw_macro_type_tail(TSLexer *lexer, const PragmaTrivia *trivia) {
|
|
77
80
|
while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
|
|
78
81
|
if (lexer->lookahead == ')' || lexer->eof(lexer)) return false;
|
|
79
82
|
unsigned depth = 1;
|
|
@@ -82,6 +85,10 @@ static inline bool scan_raw_macro_type_tail(TSLexer *lexer) {
|
|
|
82
85
|
bool balanced = true;
|
|
83
86
|
MacroArgumentWord word = {0};
|
|
84
87
|
while (!lexer->eof(lexer)) {
|
|
88
|
+
if (scan_pragma_empty(lexer, trivia)) {
|
|
89
|
+
word = (MacroArgumentWord){0};
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
85
92
|
int32_t c = lexer->lookahead;
|
|
86
93
|
if (c == ')' && depth == 1) break;
|
|
87
94
|
if (function_macro_argument_token(lexer, &word, NULL) != MACRO_ARGUMENT_PLAIN) continue;
|
|
@@ -97,20 +104,24 @@ static inline bool scan_raw_macro_type_tail(TSLexer *lexer) {
|
|
|
97
104
|
}
|
|
98
105
|
|
|
99
106
|
static bool function_macro_first_word(TSLexer *lexer, const MacroName *name, const MacroAttributeSymbols *symbols, bool *handled) {
|
|
107
|
+
const PragmaTrivia *trivia = &symbols->trivia;
|
|
100
108
|
*handled = true;
|
|
101
109
|
if (name->ascii && strcmp(name->word, pragma_word) == 0) {
|
|
102
110
|
lexer->result_symbol = symbols->pragma;
|
|
103
|
-
return symbols->allow_pragma &&
|
|
111
|
+
return symbols->allow_pragma && scan_pragma_suffix_end_with(lexer, true, trivia);
|
|
104
112
|
}
|
|
105
113
|
if (symbols->allow_object_start && name->ascii && function_macro_scalar_type_keyword(name->word)) {
|
|
106
114
|
lexer->result_symbol = symbols->object_start;
|
|
107
|
-
return function_macro_object_prefix(lexer);
|
|
115
|
+
return function_macro_object_prefix(lexer, trivia);
|
|
108
116
|
}
|
|
109
117
|
*handled = false;
|
|
110
118
|
return false;
|
|
111
119
|
}
|
|
112
120
|
|
|
121
|
+
// code-gauge-allow functionCognitiveComplexity 17 -- Attribute roles share first-word dispatch, token ranges, and group results.
|
|
122
|
+
// code-gauge-allow functionNcss 32 -- Attribute roles share first-word dispatch, token ranges, and group results.
|
|
113
123
|
static bool scan_function_attribute_guard(TSLexer *lexer, bool prototype, bool require_literal, bool trailing, const MacroAttributeSymbols *symbols) {
|
|
124
|
+
const PragmaTrivia *trivia = &symbols->trivia;
|
|
114
125
|
bool braced_only = symbols->allow_braced_start && lexer->result_symbol == symbols->braced_start;
|
|
115
126
|
char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
|
|
116
127
|
MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true, .lookahead_only = !trailing};
|
|
@@ -120,11 +131,11 @@ static bool scan_function_attribute_guard(TSLexer *lexer, bool prototype, bool r
|
|
|
120
131
|
if (handled) return accepted;
|
|
121
132
|
if (lexer->result_symbol == symbols->object_start) return false;
|
|
122
133
|
if (name.ascii && function_macro_attribute_keyword(word, trailing)) return false;
|
|
123
|
-
if (!
|
|
124
|
-
if (trailing) return function_macro_trailing_argument_prefix(lexer);
|
|
134
|
+
if (!scan_pragma_spacing_with(lexer, trivia) || lexer->lookahead != '(') return false;
|
|
135
|
+
if (trailing) return function_macro_trailing_argument_prefix(lexer, trivia);
|
|
125
136
|
bool literal = false;
|
|
126
137
|
bool unterminated_raw = false;
|
|
127
|
-
MacroGroup group = function_macro_group_prefix(lexer, '(', ')', &literal, &unterminated_raw, false);
|
|
138
|
+
MacroGroup group = function_macro_group_prefix(lexer, '(', ')', &literal, &unterminated_raw, false, trivia);
|
|
128
139
|
if (group == MACRO_GROUP_BRACED && symbols->allow_braced_start) {
|
|
129
140
|
lexer->result_symbol = symbols->braced_start;
|
|
130
141
|
return true;
|
|
@@ -132,16 +143,16 @@ static bool scan_function_attribute_guard(TSLexer *lexer, bool prototype, bool r
|
|
|
132
143
|
if (braced_only) return false;
|
|
133
144
|
if (unterminated_raw) return true;
|
|
134
145
|
if (group != MACRO_GROUP_BALANCED) return false;
|
|
135
|
-
return (!require_literal || literal) && (!prototype || function_macro_declaration_continues(lexer, require_literal, symbols->allow_braced_start));
|
|
146
|
+
return (!require_literal || literal) && (!prototype || function_macro_declaration_continues(lexer, require_literal, symbols->allow_braced_start, trivia));
|
|
136
147
|
}
|
|
137
148
|
|
|
138
|
-
static bool function_macro_bracket_attribute(TSLexer *lexer) {
|
|
149
|
+
static bool function_macro_bracket_attribute(TSLexer *lexer, const PragmaTrivia *trivia) {
|
|
139
150
|
lexer->advance(lexer, false);
|
|
140
|
-
if (!
|
|
141
|
-
if (!function_macro_modifier_balanced(lexer, '[', ']') ||
|
|
142
|
-
!
|
|
151
|
+
if (!scan_pragma_spacing_with(lexer, trivia) || lexer->lookahead != '[') return false;
|
|
152
|
+
if (!function_macro_modifier_balanced(lexer, '[', ']', trivia) ||
|
|
153
|
+
!scan_pragma_spacing_with(lexer, trivia) || lexer->lookahead != ']') return false;
|
|
143
154
|
lexer->advance(lexer, false);
|
|
144
|
-
return
|
|
155
|
+
return scan_pragma_spacing_with(lexer, trivia);
|
|
145
156
|
}
|
|
146
157
|
|
|
147
158
|
static bool function_macro_type_modifier_word(const MacroName *name) {
|
|
@@ -156,7 +167,7 @@ typedef enum {
|
|
|
156
167
|
MACRO_TYPE_PREFIX_KNOWN_TYPE, MACRO_TYPE_PREFIX_BALANCED_TYPE, MACRO_TYPE_PREFIX_TAGGED_TYPE,
|
|
157
168
|
} MacroTypePrefix;
|
|
158
169
|
|
|
159
|
-
static MacroTypePrefix function_macro_type_prefix_item(TSLexer *lexer, const MacroName *name, const char *const *reserved_words, unsigned reserved_count, bool type_argument);
|
|
170
|
+
static MacroTypePrefix function_macro_type_prefix_item(TSLexer *lexer, const MacroName *name, const char *const *reserved_words, unsigned reserved_count, bool type_argument, const PragmaTrivia *trivia);
|
|
160
171
|
|
|
161
172
|
typedef struct {
|
|
162
173
|
bool marker, known_type, tag, body, started;
|
|
@@ -165,7 +176,7 @@ typedef struct {
|
|
|
165
176
|
typedef enum { MACRO_TYPE_CONTINUE, MACRO_TYPE_ACCEPT, MACRO_TYPE_REJECT } MacroTypeResult;
|
|
166
177
|
|
|
167
178
|
static MacroTypeResult function_macro_type_transition(MacroTypeState *state, MacroTypePrefix kind);
|
|
168
|
-
static MacroTypeResult function_macro_type_group(TSLexer *lexer, MacroTypeState *state);
|
|
179
|
+
static MacroTypeResult function_macro_type_group(TSLexer *lexer, MacroTypeState *state, const PragmaTrivia *trivia);
|
|
169
180
|
static MacroTypeResult function_macro_type_word(TSLexer *lexer, const MacroAttributeSymbols *symbols,
|
|
170
181
|
const char *const *reserved_words, unsigned reserved_count, MacroTypeState *state);
|
|
171
182
|
|
|
@@ -174,7 +185,7 @@ static bool function_macro_prove_braced_type(TSLexer *lexer, const MacroAttribut
|
|
|
174
185
|
|
|
175
186
|
static bool scan_braced_macro_type_start(TSLexer *lexer, const MacroAttributeSymbols *symbols, const char *const *reserved_words, unsigned reserved_count) {
|
|
176
187
|
MacroProofMemo *memo = symbols->proof_memo;
|
|
177
|
-
if (!macro_proof_begin(memo, reserved_words, reserved_count)) memo = NULL;
|
|
188
|
+
if (!macro_proof_begin(memo, reserved_words, reserved_count, &symbols->trivia)) memo = NULL;
|
|
178
189
|
bool accepted = function_macro_prove_braced_type(lexer, symbols, reserved_words, reserved_count, memo);
|
|
179
190
|
macro_proof_finish(memo, accepted);
|
|
180
191
|
return accepted;
|
|
@@ -182,26 +193,28 @@ static bool scan_braced_macro_type_start(TSLexer *lexer, const MacroAttributeSym
|
|
|
182
193
|
|
|
183
194
|
static bool function_macro_prove_braced_type(TSLexer *lexer, const MacroAttributeSymbols *symbols,
|
|
184
195
|
const char *const *reserved_words, unsigned reserved_count, MacroProofMemo *memo) {
|
|
196
|
+
const PragmaTrivia *trivia = &symbols->trivia;
|
|
185
197
|
MacroTypeState state = {0};
|
|
186
198
|
for (;;) {
|
|
187
199
|
while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
|
|
188
200
|
uint8_t key = state.marker | (state.known_type << 1) | (state.tag << 2) | (state.body << 3);
|
|
189
201
|
unsigned cached = state.started ? macro_proof_step(memo, lexer, key) : 0;
|
|
190
202
|
if (cached) return cached == 2;
|
|
203
|
+
if (scan_pragma_empty(lexer, trivia)) continue;
|
|
191
204
|
MacroTypeResult result = lexer->lookahead == '[' || lexer->lookahead == '{'
|
|
192
|
-
? function_macro_type_group(lexer, &state)
|
|
205
|
+
? function_macro_type_group(lexer, &state, trivia)
|
|
193
206
|
: function_macro_type_word(lexer, symbols, reserved_words, reserved_count, &state);
|
|
194
207
|
if (result != MACRO_TYPE_CONTINUE) return result == MACRO_TYPE_ACCEPT;
|
|
195
208
|
}
|
|
196
209
|
}
|
|
197
210
|
|
|
198
|
-
static MacroTypeResult function_macro_type_group(TSLexer *lexer, MacroTypeState *state) {
|
|
211
|
+
static MacroTypeResult function_macro_type_group(TSLexer *lexer, MacroTypeState *state, const PragmaTrivia *trivia) {
|
|
199
212
|
if (lexer->lookahead == '[') {
|
|
200
213
|
if (!state->started) lexer->mark_end(lexer);
|
|
201
214
|
state->started = true;
|
|
202
|
-
return function_macro_bracket_attribute(lexer) ? MACRO_TYPE_CONTINUE : MACRO_TYPE_REJECT;
|
|
215
|
+
return function_macro_bracket_attribute(lexer, trivia) ? MACRO_TYPE_CONTINUE : MACRO_TYPE_REJECT;
|
|
203
216
|
}
|
|
204
|
-
if (!state->body || !function_macro_modifier_balanced(lexer, '{', '}')) return MACRO_TYPE_REJECT;
|
|
217
|
+
if (!state->body || !function_macro_modifier_balanced(lexer, '{', '}', trivia)) return MACRO_TYPE_REJECT;
|
|
205
218
|
state->body = state->tag = false;
|
|
206
219
|
return MACRO_TYPE_CONTINUE;
|
|
207
220
|
}
|
|
@@ -217,7 +230,7 @@ static MacroTypeResult function_macro_type_word(TSLexer *lexer, const MacroAttri
|
|
|
217
230
|
if (handled) return accepted ? MACRO_TYPE_ACCEPT : MACRO_TYPE_REJECT;
|
|
218
231
|
}
|
|
219
232
|
state->started = true;
|
|
220
|
-
MacroTypePrefix kind = function_macro_type_prefix_item(lexer, &name, reserved_words, reserved_count, !state->known_type);
|
|
233
|
+
MacroTypePrefix kind = function_macro_type_prefix_item(lexer, &name, reserved_words, reserved_count, !state->known_type, &symbols->trivia);
|
|
221
234
|
return function_macro_type_transition(state, kind);
|
|
222
235
|
}
|
|
223
236
|
|
|
@@ -246,19 +259,20 @@ static MacroTypeResult function_macro_type_transition(MacroTypeState *state, Mac
|
|
|
246
259
|
return MACRO_TYPE_REJECT;
|
|
247
260
|
}
|
|
248
261
|
|
|
249
|
-
|
|
262
|
+
// code-gauge-allow functionCognitiveComplexity 28 -- Keyword, modifier, and group alternatives classify one consumed type-prefix word.
|
|
263
|
+
static MacroTypePrefix function_macro_type_prefix_item(TSLexer *lexer, const MacroName *name, const char *const *reserved_words, unsigned reserved_count, bool type_argument, const PragmaTrivia *trivia) {
|
|
250
264
|
if (name->ascii && strcmp(name->word, pragma_word) == 0) {
|
|
251
|
-
return
|
|
265
|
+
return scan_pragma_suffix_end_with(lexer, false, trivia) && scan_pragma_spacing_with(lexer, trivia)
|
|
252
266
|
? MACRO_TYPE_PREFIX_MODIFIER : MACRO_TYPE_PREFIX_INVALID;
|
|
253
267
|
}
|
|
254
|
-
if (!
|
|
268
|
+
if (!scan_pragma_spacing_with(lexer, trivia)) return MACRO_TYPE_PREFIX_INVALID;
|
|
255
269
|
if (function_macro_type_modifier_word(name)) {
|
|
256
|
-
return lexer->lookahead == '(' && function_macro_modifier_balanced(lexer, '(', ')') &&
|
|
270
|
+
return lexer->lookahead == '(' && function_macro_modifier_balanced(lexer, '(', ')', trivia) && scan_pragma_spacing_with(lexer, trivia)
|
|
257
271
|
? MACRO_TYPE_PREFIX_MODIFIER : MACRO_TYPE_PREFIX_INVALID;
|
|
258
272
|
}
|
|
259
273
|
if (name->ascii && function_macro_word_in(name->word, function_macro_type_keywords,
|
|
260
274
|
sizeof(function_macro_type_keywords) / sizeof(function_macro_type_keywords[0]))) {
|
|
261
|
-
if (lexer->lookahead == '(' && (!function_macro_modifier_balanced(lexer, '(', ')') || !
|
|
275
|
+
if (lexer->lookahead == '(' && (!function_macro_modifier_balanced(lexer, '(', ')', trivia) || !scan_pragma_spacing_with(lexer, trivia))) return MACRO_TYPE_PREFIX_INVALID;
|
|
262
276
|
if (strcmp(name->word, "struct") == 0 || strcmp(name->word, "union") == 0 || strcmp(name->word, "enum") == 0) return MACRO_TYPE_PREFIX_TAGGED_TYPE;
|
|
263
277
|
return MACRO_TYPE_PREFIX_KNOWN_TYPE;
|
|
264
278
|
}
|
|
@@ -266,18 +280,19 @@ static MacroTypePrefix function_macro_type_prefix_item(TSLexer *lexer, const Mac
|
|
|
266
280
|
sizeof(function_macro_type_prefix_modifiers) / sizeof(function_macro_type_prefix_modifiers[0]))) return MACRO_TYPE_PREFIX_MODIFIER;
|
|
267
281
|
if (function_macro_type_prefix_keyword(name, reserved_words, reserved_count)) return MACRO_TYPE_PREFIX_INVALID;
|
|
268
282
|
if (lexer->lookahead == '(') {
|
|
269
|
-
MacroGroup group = function_macro_group_prefix(lexer, '(', ')', NULL, NULL, type_argument);
|
|
283
|
+
MacroGroup group = function_macro_group_prefix(lexer, '(', ')', NULL, NULL, type_argument, trivia);
|
|
270
284
|
if (group == MACRO_GROUP_BRACED) return MACRO_TYPE_PREFIX_TYPE;
|
|
271
|
-
if (group == MACRO_GROUP_BALANCED &&
|
|
285
|
+
if (group == MACRO_GROUP_BALANCED && scan_pragma_spacing_with(lexer, trivia)) return MACRO_TYPE_PREFIX_BALANCED_TYPE;
|
|
272
286
|
return MACRO_TYPE_PREFIX_INVALID;
|
|
273
287
|
}
|
|
274
288
|
return MACRO_TYPE_PREFIX_MARKER;
|
|
275
289
|
}
|
|
276
290
|
|
|
277
|
-
|
|
291
|
+
// code-gauge-allow functionCognitiveComplexity 16 -- Literal prefixes inspect one consumed first argument token without restarting the lexer.
|
|
292
|
+
static bool function_macro_trailing_argument_prefix(TSLexer *lexer, const PragmaTrivia *trivia) {
|
|
278
293
|
do {
|
|
279
294
|
lexer->advance(lexer, false);
|
|
280
|
-
if (!
|
|
295
|
+
if (!scan_pragma_spacing_with(lexer, trivia)) return false;
|
|
281
296
|
} while (lexer->lookahead == '(');
|
|
282
297
|
int32_t first = lexer->lookahead;
|
|
283
298
|
if (first == '.') {
|
|
@@ -297,22 +312,22 @@ static bool function_macro_trailing_argument_prefix(TSLexer *lexer) {
|
|
|
297
312
|
return first == ',' || first == '"' || first == '\'' || (first >= '0' && first <= '9');
|
|
298
313
|
}
|
|
299
314
|
|
|
300
|
-
static bool function_macro_object_prefix(TSLexer *lexer) {
|
|
315
|
+
static bool function_macro_object_prefix(TSLexer *lexer, const PragmaTrivia *trivia) {
|
|
301
316
|
bool escaped = false;
|
|
302
|
-
if (!function_macro_tail_spacing(lexer, &escaped)) return false;
|
|
317
|
+
if (!function_macro_tail_spacing(lexer, &escaped, trivia)) return false;
|
|
303
318
|
while (!escaped && lexer->lookahead == '*') {
|
|
304
319
|
lexer->advance(lexer, false);
|
|
305
|
-
if (!function_macro_tail_spacing(lexer, &escaped)) return false;
|
|
320
|
+
if (!function_macro_tail_spacing(lexer, &escaped, trivia)) return false;
|
|
306
321
|
}
|
|
307
322
|
char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
|
|
308
323
|
MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true};
|
|
309
324
|
if (!function_macro_object_word(lexer, &name, escaped) ||
|
|
310
325
|
(name.ascii && (function_macro_object_prefix_keyword(word) || strcmp(word, pragma_word) == 0))) return false;
|
|
311
326
|
escaped = false;
|
|
312
|
-
if (!function_macro_tail_spacing(lexer, &escaped)) return false;
|
|
327
|
+
if (!function_macro_tail_spacing(lexer, &escaped, trivia)) return false;
|
|
313
328
|
name = (MacroName){.word = word, .capacity = sizeof(word) - 1, .ascii = true};
|
|
314
329
|
if (!function_macro_object_word(lexer, &name, escaped) || (name.ascii && function_macro_attribute_keyword(word, true))) return false;
|
|
315
|
-
return
|
|
330
|
+
return scan_pragma_spacing_with(lexer, trivia) && lexer->lookahead == '(' && function_macro_trailing_argument_prefix(lexer, trivia);
|
|
316
331
|
}
|
|
317
332
|
|
|
318
333
|
static bool function_macro_object_word(TSLexer *lexer, MacroName *name, bool escaped) {
|
|
@@ -347,29 +362,36 @@ static bool function_macro_attribute_keyword(const char *word, bool trailing) {
|
|
|
347
362
|
sizeof(function_macro_group_modifiers) / sizeof(function_macro_group_modifiers[0]));
|
|
348
363
|
}
|
|
349
364
|
|
|
350
|
-
static bool function_macro_balanced(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw) {
|
|
351
|
-
return function_macro_group_prefix(lexer, open, close, literal, unterminated_raw, false) == MACRO_GROUP_BALANCED;
|
|
365
|
+
static bool function_macro_balanced(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw, const PragmaTrivia *trivia) {
|
|
366
|
+
return function_macro_group_prefix(lexer, open, close, literal, unterminated_raw, false, trivia) == MACRO_GROUP_BALANCED;
|
|
352
367
|
}
|
|
353
368
|
|
|
354
|
-
static bool function_macro_modifier_balanced(TSLexer *lexer, int32_t open, int32_t close) {
|
|
369
|
+
static bool function_macro_modifier_balanced(TSLexer *lexer, int32_t open, int32_t close, const PragmaTrivia *trivia) {
|
|
355
370
|
lexer->advance(lexer, false);
|
|
356
|
-
return function_macro_group_contents(lexer, open, close, NULL, NULL, false, true) == MACRO_GROUP_BALANCED;
|
|
371
|
+
return function_macro_group_contents(lexer, open, close, NULL, NULL, false, true, trivia) == MACRO_GROUP_BALANCED;
|
|
357
372
|
}
|
|
358
373
|
|
|
359
|
-
|
|
374
|
+
// code-gauge-allow functionParameterCount 7 -- One group proof uses its delimiter pair, optional outputs, grammar mode, and trivia context.
|
|
375
|
+
static MacroGroup function_macro_group_prefix(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw, bool type_argument, const PragmaTrivia *trivia) {
|
|
360
376
|
lexer->advance(lexer, false);
|
|
361
|
-
if (type_argument && (!
|
|
377
|
+
if (type_argument && (!scan_pragma_spacing_with(lexer, trivia) || (!preproc_word(lexer->lookahead, false) && lexer->lookahead != '\\'))) {
|
|
362
378
|
return MACRO_GROUP_INVALID;
|
|
363
379
|
}
|
|
364
|
-
return function_macro_group_contents(lexer, open, close, literal, unterminated_raw, type_argument, false);
|
|
380
|
+
return function_macro_group_contents(lexer, open, close, literal, unterminated_raw, type_argument, false, trivia);
|
|
365
381
|
}
|
|
366
382
|
|
|
367
|
-
|
|
383
|
+
// code-gauge-allow functionCognitiveComplexity 17 -- Trivia and delimiter alternatives update the same nesting and argument-word state.
|
|
384
|
+
// code-gauge-allow functionParameterCount 8 -- The shared group lexer uses its delimiter pair, optional outputs, two grammar modes, and trivia context.
|
|
385
|
+
static MacroGroup function_macro_group_contents(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw, bool type_argument, bool modifier, const PragmaTrivia *trivia) {
|
|
368
386
|
unsigned depth = 1;
|
|
369
387
|
unsigned nested = 0;
|
|
370
388
|
unsigned braces = open == '{';
|
|
371
389
|
MacroArgumentWord word = {0};
|
|
372
390
|
while (depth) {
|
|
391
|
+
if (scan_pragma_empty(lexer, trivia)) {
|
|
392
|
+
word = (MacroArgumentWord){0};
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
373
395
|
if (lexer->eof(lexer)) return MACRO_GROUP_INVALID;
|
|
374
396
|
int32_t c = lexer->lookahead;
|
|
375
397
|
if (c == '{' && !modifier) return MACRO_GROUP_BRACED;
|
|
@@ -416,11 +438,12 @@ static void function_macro_note_literal(bool *literal, unsigned depth, unsigned
|
|
|
416
438
|
if (c == '"' || (c == '\'' && !word->number) || (c >= '0' && c <= '9' && !word->identifier)) *literal = true;
|
|
417
439
|
}
|
|
418
440
|
|
|
419
|
-
|
|
441
|
+
// code-gauge-allow functionCognitiveComplexity 27 -- Words, groups, and punctuators advance the same declaration-tail state.
|
|
442
|
+
static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type, bool allow_braced, const PragmaTrivia *trivia) {
|
|
420
443
|
MacroPrototypeTail tail = {.post_type = post_type, .explicit_type = post_type};
|
|
421
444
|
for (;;) {
|
|
422
445
|
bool escaped = false;
|
|
423
|
-
if (!function_macro_tail_spacing(lexer, &escaped) || lexer->eof(lexer)) return false;
|
|
446
|
+
if (!function_macro_tail_spacing(lexer, &escaped, trivia) || lexer->eof(lexer)) return false;
|
|
424
447
|
int32_t c = lexer->lookahead;
|
|
425
448
|
if (escaped || preproc_word(c, false)) {
|
|
426
449
|
if (!function_macro_tail_word(lexer, escaped, &tail)) return false;
|
|
@@ -429,7 +452,7 @@ static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type,
|
|
|
429
452
|
} else if (c <= 127 && strchr("{}#)]", c)) {
|
|
430
453
|
return false;
|
|
431
454
|
} else if (c == '(' || c == '[') {
|
|
432
|
-
MacroGroup group = function_macro_tail_group(lexer, c, &tail);
|
|
455
|
+
MacroGroup group = function_macro_tail_group(lexer, c, &tail, trivia);
|
|
433
456
|
if (group != MACRO_GROUP_BALANCED) return tail.unterminated_raw || (allow_braced && group == MACRO_GROUP_BRACED);
|
|
434
457
|
} else if (c == '"' || c == '\'') {
|
|
435
458
|
if (!function_macro_argument_quote(lexer, c, false, NULL)) return false;
|
|
@@ -446,9 +469,9 @@ static bool function_macro_tail_punctuator(TSLexer *lexer, int32_t c, MacroProto
|
|
|
446
469
|
return true;
|
|
447
470
|
}
|
|
448
471
|
|
|
449
|
-
static MacroGroup function_macro_tail_group(TSLexer *lexer, int32_t c, MacroPrototypeTail *tail) {
|
|
472
|
+
static MacroGroup function_macro_tail_group(TSLexer *lexer, int32_t c, MacroPrototypeTail *tail, const PragmaTrivia *trivia) {
|
|
450
473
|
bool literal = false;
|
|
451
|
-
MacroGroup group = function_macro_group_prefix(lexer, c, c == '(' ? ')' : ']', &literal, &tail->unterminated_raw, false);
|
|
474
|
+
MacroGroup group = function_macro_group_prefix(lexer, c, c == '(' ? ')' : ']', &literal, &tail->unterminated_raw, false, trivia);
|
|
452
475
|
if (group != MACRO_GROUP_BALANCED) return group;
|
|
453
476
|
if (c == '(' && !tail->modifier) {
|
|
454
477
|
if (literal && tail->post_type && !tail->declarator_group) tail->declarator_name = false;
|
|
@@ -458,8 +481,9 @@ static MacroGroup function_macro_tail_group(TSLexer *lexer, int32_t c, MacroProt
|
|
|
458
481
|
return MACRO_GROUP_BALANCED;
|
|
459
482
|
}
|
|
460
483
|
|
|
461
|
-
static bool function_macro_tail_spacing(TSLexer *lexer, bool *escaped) {
|
|
484
|
+
static bool function_macro_tail_spacing(TSLexer *lexer, bool *escaped, const PragmaTrivia *trivia) {
|
|
462
485
|
for (;;) {
|
|
486
|
+
if (scan_pragma_empty(lexer, trivia)) continue;
|
|
463
487
|
int32_t c = lexer->lookahead;
|
|
464
488
|
if (pragma_space(c)) {
|
|
465
489
|
lexer->advance(lexer, false);
|
package/src/macroProof.h
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#ifndef TREE_SITTER_C_MACRO_PROOF_H_
|
|
2
2
|
#define TREE_SITTER_C_MACRO_PROOF_H_
|
|
3
3
|
|
|
4
|
-
#include "
|
|
4
|
+
#include "pragma.h"
|
|
5
5
|
#include <stdlib.h>
|
|
6
6
|
|
|
7
7
|
typedef struct { void *children[256]; } MacroProofNode;
|
|
@@ -13,6 +13,8 @@ typedef struct {
|
|
|
13
13
|
uint32_t length, capacity;
|
|
14
14
|
const char *const *reserved_words;
|
|
15
15
|
unsigned reserved_count;
|
|
16
|
+
bool (*skip_empty)(TSLexer *, void *);
|
|
17
|
+
void *empty_context;
|
|
16
18
|
bool bound;
|
|
17
19
|
} MacroProofMemo;
|
|
18
20
|
|
|
@@ -20,12 +22,15 @@ static MacroProofPage *macro_proof_page(MacroProofMemo *memo, uint32_t byte, boo
|
|
|
20
22
|
static void macro_proof_append(MacroProofMemo *memo, MacroProofKey key);
|
|
21
23
|
static void macro_proof_free_node(MacroProofNode *node, unsigned level);
|
|
22
24
|
|
|
23
|
-
static bool macro_proof_begin(MacroProofMemo *memo, const char *const *reserved_words, unsigned reserved_count) {
|
|
25
|
+
static bool macro_proof_begin(MacroProofMemo *memo, const char *const *reserved_words, unsigned reserved_count, const PragmaTrivia *trivia) {
|
|
24
26
|
if (!memo) return false;
|
|
25
|
-
if (memo->bound && (memo->reserved_words != reserved_words || memo->reserved_count != reserved_count
|
|
27
|
+
if (memo->bound && (memo->reserved_words != reserved_words || memo->reserved_count != reserved_count ||
|
|
28
|
+
memo->skip_empty != trivia->skip_empty || memo->empty_context != trivia->empty_context)) return false;
|
|
26
29
|
memo->bound = true;
|
|
27
30
|
memo->reserved_words = reserved_words;
|
|
28
31
|
memo->reserved_count = reserved_count;
|
|
32
|
+
memo->skip_empty = trivia->skip_empty;
|
|
33
|
+
memo->empty_context = trivia->empty_context;
|
|
29
34
|
memo->length = 0;
|
|
30
35
|
return true;
|
|
31
36
|
}
|
package/src/node-types.json
CHANGED
package/src/pragma.h
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// code-gauge-allow-file fileNcss 476 -- Preprocessor tokens and pragma operators share names, spacing, and literal readers.
|
|
1
2
|
#ifndef TREE_SITTER_C_PRAGMA_H_
|
|
2
3
|
#define TREE_SITTER_C_PRAGMA_H_
|
|
3
4
|
|
|
@@ -8,6 +9,11 @@
|
|
|
8
9
|
|
|
9
10
|
static const char pragma_word[] = "_Pragma";
|
|
10
11
|
|
|
12
|
+
typedef struct {
|
|
13
|
+
bool (*skip_empty)(TSLexer *, void *);
|
|
14
|
+
void *empty_context;
|
|
15
|
+
} PragmaTrivia;
|
|
16
|
+
|
|
11
17
|
typedef struct {
|
|
12
18
|
char *word;
|
|
13
19
|
unsigned capacity;
|
|
@@ -37,6 +43,9 @@ static bool scan_macro_name_ucn(TSLexer *lexer);
|
|
|
37
43
|
|
|
38
44
|
static bool scan_pragma_argument(TSLexer *lexer);
|
|
39
45
|
static bool scan_pragma_spacing(TSLexer *lexer);
|
|
46
|
+
static bool scan_pragma_spacing_with(TSLexer *lexer, const PragmaTrivia *trivia);
|
|
47
|
+
static bool scan_pragma_empty(TSLexer *lexer, const PragmaTrivia *trivia);
|
|
48
|
+
static bool scan_pragma_suffix_end_with(TSLexer *lexer, bool mark_end, const PragmaTrivia *trivia);
|
|
40
49
|
static bool scan_pragma_suffix(TSLexer *lexer);
|
|
41
50
|
static bool scan_pragma_suffix_end(TSLexer *lexer, bool mark_end);
|
|
42
51
|
static bool pragma_space(int32_t c);
|
|
@@ -48,12 +57,16 @@ static bool preproc_word(int32_t c, bool continuation);
|
|
|
48
57
|
static bool scan_preproc_quote(TSLexer *lexer, int32_t quote, bool escaped, bool directive_text);
|
|
49
58
|
static bool scan_preproc_raw_quote(TSLexer *lexer);
|
|
50
59
|
|
|
51
|
-
static bool
|
|
60
|
+
static bool scan_pragma_with(TSLexer *lexer, const PragmaTrivia *trivia) {
|
|
52
61
|
while (pragma_space(lexer->lookahead)) {
|
|
53
62
|
lexer->advance(lexer, true);
|
|
54
63
|
}
|
|
55
64
|
if (!scan_pragma_word(lexer)) return false;
|
|
56
|
-
return
|
|
65
|
+
return scan_pragma_suffix_end_with(lexer, true, trivia);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static bool scan_pragma(TSLexer *lexer) {
|
|
69
|
+
return scan_pragma_with(lexer, NULL);
|
|
57
70
|
}
|
|
58
71
|
|
|
59
72
|
static bool scan_pragma_suffix(TSLexer *lexer) {
|
|
@@ -61,11 +74,15 @@ static bool scan_pragma_suffix(TSLexer *lexer) {
|
|
|
61
74
|
}
|
|
62
75
|
|
|
63
76
|
static bool scan_pragma_suffix_end(TSLexer *lexer, bool mark_end) {
|
|
64
|
-
|
|
77
|
+
return scan_pragma_suffix_end_with(lexer, mark_end, NULL);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
static bool scan_pragma_suffix_end_with(TSLexer *lexer, bool mark_end, const PragmaTrivia *trivia) {
|
|
81
|
+
if (!scan_pragma_spacing_with(lexer, trivia) || lexer->lookahead != '(') return false;
|
|
65
82
|
lexer->advance(lexer, false);
|
|
66
|
-
if (!
|
|
83
|
+
if (!scan_pragma_spacing_with(lexer, trivia)) return false;
|
|
67
84
|
if (!scan_pragma_argument(lexer)) return false;
|
|
68
|
-
if (!
|
|
85
|
+
if (!scan_pragma_spacing_with(lexer, trivia) || lexer->lookahead != ')') return false;
|
|
69
86
|
lexer->advance(lexer, false);
|
|
70
87
|
if (mark_end) lexer->mark_end(lexer);
|
|
71
88
|
return true;
|
|
@@ -98,6 +115,17 @@ static bool scan_pragma_argument(TSLexer *lexer) {
|
|
|
98
115
|
return true;
|
|
99
116
|
}
|
|
100
117
|
|
|
118
|
+
static bool scan_pragma_spacing_with(TSLexer *lexer, const PragmaTrivia *trivia) {
|
|
119
|
+
for (;;) {
|
|
120
|
+
if (!scan_pragma_spacing(lexer)) return false;
|
|
121
|
+
if (!scan_pragma_empty(lexer, trivia)) return true;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static bool scan_pragma_empty(TSLexer *lexer, const PragmaTrivia *trivia) {
|
|
126
|
+
return trivia && trivia->skip_empty && trivia->skip_empty(lexer, trivia->empty_context);
|
|
127
|
+
}
|
|
128
|
+
|
|
101
129
|
static bool scan_pragma_spacing(TSLexer *lexer) {
|
|
102
130
|
for (;;) {
|
|
103
131
|
if (pragma_space(lexer->lookahead)) {
|