@willbooster/tree-sitter-c 24.0.0 → 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 +20 -7
- package/grammar.js +9 -1
- package/package.json +3 -1
- package/src/conditionalMacro.h +23 -19
- package/src/emptyDirectives.h +439 -0
- package/src/functionMacro.h +159 -100
- package/src/macroProof.h +100 -0
- package/src/node-types.json +5 -0
- package/src/pragma.h +33 -5
- package/src/scanner.c +73 -23
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +3 -1
package/src/functionMacro.h
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
// code-gauge-allow-file fileNcss 593 -- Annotation and argument scanners share lexical helpers and keyword tables.
|
|
1
2
|
#ifndef TREE_SITTER_C_FUNCTION_MACRO_H_
|
|
2
3
|
#define TREE_SITTER_C_FUNCTION_MACRO_H_
|
|
3
4
|
|
|
4
5
|
#include "pragma.h"
|
|
5
6
|
#include "keywords.h"
|
|
7
|
+
#include "macroProof.h"
|
|
6
8
|
|
|
7
9
|
typedef struct {
|
|
8
10
|
bool identifier;
|
|
@@ -27,6 +29,8 @@ typedef struct {
|
|
|
27
29
|
} MacroPrototypeTail;
|
|
28
30
|
|
|
29
31
|
typedef struct {
|
|
32
|
+
PragmaTrivia trivia;
|
|
33
|
+
MacroProofMemo *proof_memo;
|
|
30
34
|
TSSymbol pragma;
|
|
31
35
|
TSSymbol object_start;
|
|
32
36
|
bool allow_pragma;
|
|
@@ -41,28 +45,28 @@ typedef enum {
|
|
|
41
45
|
|
|
42
46
|
static MacroArgumentToken function_macro_argument_token(TSLexer *lexer, MacroArgumentWord *word, bool *unterminated_raw);
|
|
43
47
|
|
|
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_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);
|
|
48
52
|
static bool function_macro_group_separator(int32_t c, unsigned depth, unsigned nested, bool type_argument, unsigned braces);
|
|
49
53
|
static bool function_macro_type_prefix_keyword(const MacroName *name, const char *const *reserved_words, unsigned reserved_count);
|
|
50
54
|
static bool function_macro_group(int32_t c, int32_t open, int32_t close, unsigned *depth, unsigned *nested, unsigned *braces);
|
|
51
55
|
static void function_macro_note_literal(bool *literal, unsigned depth, unsigned nested, const MacroArgumentWord *word, int32_t c);
|
|
52
56
|
static bool function_macro_tail_word(TSLexer *lexer, bool escaped, MacroPrototypeTail *tail);
|
|
53
|
-
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);
|
|
54
58
|
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);
|
|
59
|
+
static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type, bool allow_braced, const PragmaTrivia *trivia);
|
|
56
60
|
static bool function_macro_argument_quote(TSLexer *lexer, int32_t quote, bool raw, bool *unterminated_raw);
|
|
57
61
|
static bool function_macro_argument_escape(TSLexer *lexer, MacroArgumentWord *word);
|
|
58
62
|
static bool function_macro_argument_slash(TSLexer *lexer, bool allow_division);
|
|
59
|
-
static bool function_macro_tail_spacing(TSLexer *lexer, bool *escaped);
|
|
63
|
+
static bool function_macro_tail_spacing(TSLexer *lexer, bool *escaped, const PragmaTrivia *trivia);
|
|
60
64
|
static bool function_macro_line_comment(TSLexer *lexer);
|
|
61
65
|
static bool function_macro_block_comment(TSLexer *lexer);
|
|
62
66
|
static void function_macro_argument_word(MacroArgumentWord *word, int32_t c);
|
|
63
67
|
|
|
64
|
-
static bool function_macro_trailing_argument_prefix(TSLexer *lexer);
|
|
65
|
-
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);
|
|
66
70
|
static bool function_macro_object_word(TSLexer *lexer, MacroName *name, bool escaped);
|
|
67
71
|
static bool function_macro_scalar_type_keyword(const char *word);
|
|
68
72
|
static bool function_macro_object_prefix_keyword(const char *word);
|
|
@@ -70,7 +74,9 @@ static bool function_macro_attribute_keyword(const char *word, bool trailing);
|
|
|
70
74
|
static bool function_macro_keyword(const char *word);
|
|
71
75
|
static bool function_macro_word_in(const char *word, const char *const *words, unsigned count);
|
|
72
76
|
|
|
73
|
-
|
|
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) {
|
|
74
80
|
while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
|
|
75
81
|
if (lexer->lookahead == ')' || lexer->eof(lexer)) return false;
|
|
76
82
|
unsigned depth = 1;
|
|
@@ -79,10 +85,13 @@ static inline bool scan_raw_macro_type_tail(TSLexer *lexer) {
|
|
|
79
85
|
bool balanced = true;
|
|
80
86
|
MacroArgumentWord word = {0};
|
|
81
87
|
while (!lexer->eof(lexer)) {
|
|
88
|
+
if (scan_pragma_empty(lexer, trivia)) {
|
|
89
|
+
word = (MacroArgumentWord){0};
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
82
92
|
int32_t c = lexer->lookahead;
|
|
83
93
|
if (c == ')' && depth == 1) break;
|
|
84
94
|
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
95
|
if (c == '{' && balanced) return false;
|
|
87
96
|
if (balanced) balanced = function_macro_group(c, '(', ')', &depth, &nested, &braces);
|
|
88
97
|
else if (c == '(') depth++;
|
|
@@ -95,20 +104,24 @@ static inline bool scan_raw_macro_type_tail(TSLexer *lexer) {
|
|
|
95
104
|
}
|
|
96
105
|
|
|
97
106
|
static bool function_macro_first_word(TSLexer *lexer, const MacroName *name, const MacroAttributeSymbols *symbols, bool *handled) {
|
|
107
|
+
const PragmaTrivia *trivia = &symbols->trivia;
|
|
98
108
|
*handled = true;
|
|
99
109
|
if (name->ascii && strcmp(name->word, pragma_word) == 0) {
|
|
100
110
|
lexer->result_symbol = symbols->pragma;
|
|
101
|
-
return symbols->allow_pragma &&
|
|
111
|
+
return symbols->allow_pragma && scan_pragma_suffix_end_with(lexer, true, trivia);
|
|
102
112
|
}
|
|
103
113
|
if (symbols->allow_object_start && name->ascii && function_macro_scalar_type_keyword(name->word)) {
|
|
104
114
|
lexer->result_symbol = symbols->object_start;
|
|
105
|
-
return function_macro_object_prefix(lexer);
|
|
115
|
+
return function_macro_object_prefix(lexer, trivia);
|
|
106
116
|
}
|
|
107
117
|
*handled = false;
|
|
108
118
|
return false;
|
|
109
119
|
}
|
|
110
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.
|
|
111
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;
|
|
112
125
|
bool braced_only = symbols->allow_braced_start && lexer->result_symbol == symbols->braced_start;
|
|
113
126
|
char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
|
|
114
127
|
MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true, .lookahead_only = !trailing};
|
|
@@ -118,12 +131,11 @@ static bool scan_function_attribute_guard(TSLexer *lexer, bool prototype, bool r
|
|
|
118
131
|
if (handled) return accepted;
|
|
119
132
|
if (lexer->result_symbol == symbols->object_start) return false;
|
|
120
133
|
if (name.ascii && function_macro_attribute_keyword(word, trailing)) return false;
|
|
121
|
-
if (!
|
|
122
|
-
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);
|
|
123
136
|
bool literal = false;
|
|
124
137
|
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.
|
|
138
|
+
MacroGroup group = function_macro_group_prefix(lexer, '(', ')', &literal, &unterminated_raw, false, trivia);
|
|
127
139
|
if (group == MACRO_GROUP_BRACED && symbols->allow_braced_start) {
|
|
128
140
|
lexer->result_symbol = symbols->braced_start;
|
|
129
141
|
return true;
|
|
@@ -131,16 +143,16 @@ static bool scan_function_attribute_guard(TSLexer *lexer, bool prototype, bool r
|
|
|
131
143
|
if (braced_only) return false;
|
|
132
144
|
if (unterminated_raw) return true;
|
|
133
145
|
if (group != MACRO_GROUP_BALANCED) return false;
|
|
134
|
-
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));
|
|
135
147
|
}
|
|
136
148
|
|
|
137
|
-
static bool function_macro_bracket_attribute(TSLexer *lexer) {
|
|
149
|
+
static bool function_macro_bracket_attribute(TSLexer *lexer, const PragmaTrivia *trivia) {
|
|
138
150
|
lexer->advance(lexer, false);
|
|
139
|
-
if (!
|
|
140
|
-
if (!function_macro_modifier_balanced(lexer, '[', ']') ||
|
|
141
|
-
!
|
|
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;
|
|
142
154
|
lexer->advance(lexer, false);
|
|
143
|
-
return
|
|
155
|
+
return scan_pragma_spacing_with(lexer, trivia);
|
|
144
156
|
}
|
|
145
157
|
|
|
146
158
|
static bool function_macro_type_modifier_word(const MacroName *name) {
|
|
@@ -155,74 +167,112 @@ typedef enum {
|
|
|
155
167
|
MACRO_TYPE_PREFIX_KNOWN_TYPE, MACRO_TYPE_PREFIX_BALANCED_TYPE, MACRO_TYPE_PREFIX_TAGGED_TYPE,
|
|
156
168
|
} MacroTypePrefix;
|
|
157
169
|
|
|
158
|
-
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);
|
|
171
|
+
|
|
172
|
+
typedef struct {
|
|
173
|
+
bool marker, known_type, tag, body, started;
|
|
174
|
+
} MacroTypeState;
|
|
175
|
+
|
|
176
|
+
typedef enum { MACRO_TYPE_CONTINUE, MACRO_TYPE_ACCEPT, MACRO_TYPE_REJECT } MacroTypeResult;
|
|
177
|
+
|
|
178
|
+
static MacroTypeResult function_macro_type_transition(MacroTypeState *state, MacroTypePrefix kind);
|
|
179
|
+
static MacroTypeResult function_macro_type_group(TSLexer *lexer, MacroTypeState *state, const PragmaTrivia *trivia);
|
|
180
|
+
static MacroTypeResult function_macro_type_word(TSLexer *lexer, const MacroAttributeSymbols *symbols,
|
|
181
|
+
const char *const *reserved_words, unsigned reserved_count, MacroTypeState *state);
|
|
182
|
+
|
|
183
|
+
static bool function_macro_prove_braced_type(TSLexer *lexer, const MacroAttributeSymbols *symbols,
|
|
184
|
+
const char *const *reserved_words, unsigned reserved_count, MacroProofMemo *memo);
|
|
159
185
|
|
|
160
186
|
static bool scan_braced_macro_type_start(TSLexer *lexer, const MacroAttributeSymbols *symbols, const char *const *reserved_words, unsigned reserved_count) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
bool
|
|
164
|
-
|
|
165
|
-
|
|
187
|
+
MacroProofMemo *memo = symbols->proof_memo;
|
|
188
|
+
if (!macro_proof_begin(memo, reserved_words, reserved_count, &symbols->trivia)) memo = NULL;
|
|
189
|
+
bool accepted = function_macro_prove_braced_type(lexer, symbols, reserved_words, reserved_count, memo);
|
|
190
|
+
macro_proof_finish(memo, accepted);
|
|
191
|
+
return accepted;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
static bool function_macro_prove_braced_type(TSLexer *lexer, const MacroAttributeSymbols *symbols,
|
|
195
|
+
const char *const *reserved_words, unsigned reserved_count, MacroProofMemo *memo) {
|
|
196
|
+
const PragmaTrivia *trivia = &symbols->trivia;
|
|
197
|
+
MacroTypeState state = {0};
|
|
166
198
|
for (;;) {
|
|
167
199
|
while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
if (!
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
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
|
-
}
|
|
200
|
+
uint8_t key = state.marker | (state.known_type << 1) | (state.tag << 2) | (state.body << 3);
|
|
201
|
+
unsigned cached = state.started ? macro_proof_step(memo, lexer, key) : 0;
|
|
202
|
+
if (cached) return cached == 2;
|
|
203
|
+
if (scan_pragma_empty(lexer, trivia)) continue;
|
|
204
|
+
MacroTypeResult result = lexer->lookahead == '[' || lexer->lookahead == '{'
|
|
205
|
+
? function_macro_type_group(lexer, &state, trivia)
|
|
206
|
+
: function_macro_type_word(lexer, symbols, reserved_words, reserved_count, &state);
|
|
207
|
+
if (result != MACRO_TYPE_CONTINUE) return result == MACRO_TYPE_ACCEPT;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
static MacroTypeResult function_macro_type_group(TSLexer *lexer, MacroTypeState *state, const PragmaTrivia *trivia) {
|
|
212
|
+
if (lexer->lookahead == '[') {
|
|
213
|
+
if (!state->started) lexer->mark_end(lexer);
|
|
214
|
+
state->started = true;
|
|
215
|
+
return function_macro_bracket_attribute(lexer, trivia) ? MACRO_TYPE_CONTINUE : MACRO_TYPE_REJECT;
|
|
216
|
+
}
|
|
217
|
+
if (!state->body || !function_macro_modifier_balanced(lexer, '{', '}', trivia)) return MACRO_TYPE_REJECT;
|
|
218
|
+
state->body = state->tag = false;
|
|
219
|
+
return MACRO_TYPE_CONTINUE;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
static MacroTypeResult function_macro_type_word(TSLexer *lexer, const MacroAttributeSymbols *symbols,
|
|
223
|
+
const char *const *reserved_words, unsigned reserved_count, MacroTypeState *state) {
|
|
224
|
+
char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
|
|
225
|
+
MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true, .lookahead_only = true, .preserve_end = state->started};
|
|
226
|
+
if (!scan_macro_name(lexer, &name)) return MACRO_TYPE_REJECT;
|
|
227
|
+
if (!state->started) {
|
|
228
|
+
bool handled;
|
|
229
|
+
bool accepted = function_macro_first_word(lexer, &name, symbols, &handled);
|
|
230
|
+
if (handled) return accepted ? MACRO_TYPE_ACCEPT : MACRO_TYPE_REJECT;
|
|
210
231
|
}
|
|
232
|
+
state->started = true;
|
|
233
|
+
MacroTypePrefix kind = function_macro_type_prefix_item(lexer, &name, reserved_words, reserved_count, !state->known_type, &symbols->trivia);
|
|
234
|
+
return function_macro_type_transition(state, kind);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
static MacroTypeResult function_macro_type_transition(MacroTypeState *state, MacroTypePrefix kind) {
|
|
238
|
+
switch (kind) {
|
|
239
|
+
case MACRO_TYPE_PREFIX_INVALID:
|
|
240
|
+
return MACRO_TYPE_REJECT;
|
|
241
|
+
case MACRO_TYPE_PREFIX_TYPE:
|
|
242
|
+
return state->marker ? MACRO_TYPE_ACCEPT : MACRO_TYPE_REJECT;
|
|
243
|
+
case MACRO_TYPE_PREFIX_MODIFIER:
|
|
244
|
+
return MACRO_TYPE_CONTINUE;
|
|
245
|
+
case MACRO_TYPE_PREFIX_MARKER:
|
|
246
|
+
if (state->known_type && !state->tag) return MACRO_TYPE_REJECT;
|
|
247
|
+
state->marker |= !state->known_type;
|
|
248
|
+
state->body = state->tag;
|
|
249
|
+
state->tag = false;
|
|
250
|
+
return MACRO_TYPE_CONTINUE;
|
|
251
|
+
case MACRO_TYPE_PREFIX_KNOWN_TYPE:
|
|
252
|
+
case MACRO_TYPE_PREFIX_BALANCED_TYPE:
|
|
253
|
+
case MACRO_TYPE_PREFIX_TAGGED_TYPE:
|
|
254
|
+
if (!state->marker || (state->known_type && kind == MACRO_TYPE_PREFIX_BALANCED_TYPE)) return MACRO_TYPE_REJECT;
|
|
255
|
+
state->known_type = true;
|
|
256
|
+
state->tag = state->body = kind == MACRO_TYPE_PREFIX_TAGGED_TYPE;
|
|
257
|
+
return MACRO_TYPE_CONTINUE;
|
|
258
|
+
}
|
|
259
|
+
return MACRO_TYPE_REJECT;
|
|
211
260
|
}
|
|
212
261
|
|
|
213
|
-
|
|
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) {
|
|
214
264
|
if (name->ascii && strcmp(name->word, pragma_word) == 0) {
|
|
215
|
-
return
|
|
265
|
+
return scan_pragma_suffix_end_with(lexer, false, trivia) && scan_pragma_spacing_with(lexer, trivia)
|
|
216
266
|
? MACRO_TYPE_PREFIX_MODIFIER : MACRO_TYPE_PREFIX_INVALID;
|
|
217
267
|
}
|
|
218
|
-
if (!
|
|
268
|
+
if (!scan_pragma_spacing_with(lexer, trivia)) return MACRO_TYPE_PREFIX_INVALID;
|
|
219
269
|
if (function_macro_type_modifier_word(name)) {
|
|
220
|
-
return lexer->lookahead == '(' && function_macro_modifier_balanced(lexer, '(', ')') &&
|
|
270
|
+
return lexer->lookahead == '(' && function_macro_modifier_balanced(lexer, '(', ')', trivia) && scan_pragma_spacing_with(lexer, trivia)
|
|
221
271
|
? MACRO_TYPE_PREFIX_MODIFIER : MACRO_TYPE_PREFIX_INVALID;
|
|
222
272
|
}
|
|
223
273
|
if (name->ascii && function_macro_word_in(name->word, function_macro_type_keywords,
|
|
224
274
|
sizeof(function_macro_type_keywords) / sizeof(function_macro_type_keywords[0]))) {
|
|
225
|
-
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;
|
|
226
276
|
if (strcmp(name->word, "struct") == 0 || strcmp(name->word, "union") == 0 || strcmp(name->word, "enum") == 0) return MACRO_TYPE_PREFIX_TAGGED_TYPE;
|
|
227
277
|
return MACRO_TYPE_PREFIX_KNOWN_TYPE;
|
|
228
278
|
}
|
|
@@ -230,18 +280,19 @@ static MacroTypePrefix function_macro_type_prefix_item(TSLexer *lexer, const Mac
|
|
|
230
280
|
sizeof(function_macro_type_prefix_modifiers) / sizeof(function_macro_type_prefix_modifiers[0]))) return MACRO_TYPE_PREFIX_MODIFIER;
|
|
231
281
|
if (function_macro_type_prefix_keyword(name, reserved_words, reserved_count)) return MACRO_TYPE_PREFIX_INVALID;
|
|
232
282
|
if (lexer->lookahead == '(') {
|
|
233
|
-
MacroGroup group = function_macro_group_prefix(lexer, '(', ')', NULL, NULL, type_argument);
|
|
283
|
+
MacroGroup group = function_macro_group_prefix(lexer, '(', ')', NULL, NULL, type_argument, trivia);
|
|
234
284
|
if (group == MACRO_GROUP_BRACED) return MACRO_TYPE_PREFIX_TYPE;
|
|
235
|
-
if (group == MACRO_GROUP_BALANCED &&
|
|
285
|
+
if (group == MACRO_GROUP_BALANCED && scan_pragma_spacing_with(lexer, trivia)) return MACRO_TYPE_PREFIX_BALANCED_TYPE;
|
|
236
286
|
return MACRO_TYPE_PREFIX_INVALID;
|
|
237
287
|
}
|
|
238
288
|
return MACRO_TYPE_PREFIX_MARKER;
|
|
239
289
|
}
|
|
240
290
|
|
|
241
|
-
|
|
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) {
|
|
242
293
|
do {
|
|
243
294
|
lexer->advance(lexer, false);
|
|
244
|
-
if (!
|
|
295
|
+
if (!scan_pragma_spacing_with(lexer, trivia)) return false;
|
|
245
296
|
} while (lexer->lookahead == '(');
|
|
246
297
|
int32_t first = lexer->lookahead;
|
|
247
298
|
if (first == '.') {
|
|
@@ -261,22 +312,22 @@ static bool function_macro_trailing_argument_prefix(TSLexer *lexer) {
|
|
|
261
312
|
return first == ',' || first == '"' || first == '\'' || (first >= '0' && first <= '9');
|
|
262
313
|
}
|
|
263
314
|
|
|
264
|
-
static bool function_macro_object_prefix(TSLexer *lexer) {
|
|
315
|
+
static bool function_macro_object_prefix(TSLexer *lexer, const PragmaTrivia *trivia) {
|
|
265
316
|
bool escaped = false;
|
|
266
|
-
if (!function_macro_tail_spacing(lexer, &escaped)) return false;
|
|
317
|
+
if (!function_macro_tail_spacing(lexer, &escaped, trivia)) return false;
|
|
267
318
|
while (!escaped && lexer->lookahead == '*') {
|
|
268
319
|
lexer->advance(lexer, false);
|
|
269
|
-
if (!function_macro_tail_spacing(lexer, &escaped)) return false;
|
|
320
|
+
if (!function_macro_tail_spacing(lexer, &escaped, trivia)) return false;
|
|
270
321
|
}
|
|
271
322
|
char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
|
|
272
323
|
MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true};
|
|
273
324
|
if (!function_macro_object_word(lexer, &name, escaped) ||
|
|
274
325
|
(name.ascii && (function_macro_object_prefix_keyword(word) || strcmp(word, pragma_word) == 0))) return false;
|
|
275
326
|
escaped = false;
|
|
276
|
-
if (!function_macro_tail_spacing(lexer, &escaped)) return false;
|
|
327
|
+
if (!function_macro_tail_spacing(lexer, &escaped, trivia)) return false;
|
|
277
328
|
name = (MacroName){.word = word, .capacity = sizeof(word) - 1, .ascii = true};
|
|
278
329
|
if (!function_macro_object_word(lexer, &name, escaped) || (name.ascii && function_macro_attribute_keyword(word, true))) return false;
|
|
279
|
-
return
|
|
330
|
+
return scan_pragma_spacing_with(lexer, trivia) && lexer->lookahead == '(' && function_macro_trailing_argument_prefix(lexer, trivia);
|
|
280
331
|
}
|
|
281
332
|
|
|
282
333
|
static bool function_macro_object_word(TSLexer *lexer, MacroName *name, bool escaped) {
|
|
@@ -311,30 +362,36 @@ static bool function_macro_attribute_keyword(const char *word, bool trailing) {
|
|
|
311
362
|
sizeof(function_macro_group_modifiers) / sizeof(function_macro_group_modifiers[0]));
|
|
312
363
|
}
|
|
313
364
|
|
|
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;
|
|
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;
|
|
316
367
|
}
|
|
317
368
|
|
|
318
|
-
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) {
|
|
319
370
|
lexer->advance(lexer, false);
|
|
320
|
-
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;
|
|
321
372
|
}
|
|
322
373
|
|
|
323
|
-
|
|
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) {
|
|
324
376
|
lexer->advance(lexer, false);
|
|
325
|
-
if (type_argument && (!
|
|
377
|
+
if (type_argument && (!scan_pragma_spacing_with(lexer, trivia) || (!preproc_word(lexer->lookahead, false) && lexer->lookahead != '\\'))) {
|
|
326
378
|
return MACRO_GROUP_INVALID;
|
|
327
379
|
}
|
|
328
|
-
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);
|
|
329
381
|
}
|
|
330
382
|
|
|
331
|
-
|
|
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) {
|
|
332
386
|
unsigned depth = 1;
|
|
333
387
|
unsigned nested = 0;
|
|
334
|
-
// A definition body's own brace permits its member semicolons.
|
|
335
388
|
unsigned braces = open == '{';
|
|
336
389
|
MacroArgumentWord word = {0};
|
|
337
390
|
while (depth) {
|
|
391
|
+
if (scan_pragma_empty(lexer, trivia)) {
|
|
392
|
+
word = (MacroArgumentWord){0};
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
338
395
|
if (lexer->eof(lexer)) return MACRO_GROUP_INVALID;
|
|
339
396
|
int32_t c = lexer->lookahead;
|
|
340
397
|
if (c == '{' && !modifier) return MACRO_GROUP_BRACED;
|
|
@@ -381,11 +438,12 @@ static void function_macro_note_literal(bool *literal, unsigned depth, unsigned
|
|
|
381
438
|
if (c == '"' || (c == '\'' && !word->number) || (c >= '0' && c <= '9' && !word->identifier)) *literal = true;
|
|
382
439
|
}
|
|
383
440
|
|
|
384
|
-
|
|
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) {
|
|
385
443
|
MacroPrototypeTail tail = {.post_type = post_type, .explicit_type = post_type};
|
|
386
444
|
for (;;) {
|
|
387
445
|
bool escaped = false;
|
|
388
|
-
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;
|
|
389
447
|
int32_t c = lexer->lookahead;
|
|
390
448
|
if (escaped || preproc_word(c, false)) {
|
|
391
449
|
if (!function_macro_tail_word(lexer, escaped, &tail)) return false;
|
|
@@ -394,7 +452,7 @@ static bool function_macro_declaration_continues(TSLexer *lexer, bool post_type,
|
|
|
394
452
|
} else if (c <= 127 && strchr("{}#)]", c)) {
|
|
395
453
|
return false;
|
|
396
454
|
} else if (c == '(' || c == '[') {
|
|
397
|
-
MacroGroup group = function_macro_tail_group(lexer, c, &tail);
|
|
455
|
+
MacroGroup group = function_macro_tail_group(lexer, c, &tail, trivia);
|
|
398
456
|
if (group != MACRO_GROUP_BALANCED) return tail.unterminated_raw || (allow_braced && group == MACRO_GROUP_BRACED);
|
|
399
457
|
} else if (c == '"' || c == '\'') {
|
|
400
458
|
if (!function_macro_argument_quote(lexer, c, false, NULL)) return false;
|
|
@@ -411,9 +469,9 @@ static bool function_macro_tail_punctuator(TSLexer *lexer, int32_t c, MacroProto
|
|
|
411
469
|
return true;
|
|
412
470
|
}
|
|
413
471
|
|
|
414
|
-
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) {
|
|
415
473
|
bool literal = false;
|
|
416
|
-
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);
|
|
417
475
|
if (group != MACRO_GROUP_BALANCED) return group;
|
|
418
476
|
if (c == '(' && !tail->modifier) {
|
|
419
477
|
if (literal && tail->post_type && !tail->declarator_group) tail->declarator_name = false;
|
|
@@ -423,8 +481,9 @@ static MacroGroup function_macro_tail_group(TSLexer *lexer, int32_t c, MacroProt
|
|
|
423
481
|
return MACRO_GROUP_BALANCED;
|
|
424
482
|
}
|
|
425
483
|
|
|
426
|
-
static bool function_macro_tail_spacing(TSLexer *lexer, bool *escaped) {
|
|
484
|
+
static bool function_macro_tail_spacing(TSLexer *lexer, bool *escaped, const PragmaTrivia *trivia) {
|
|
427
485
|
for (;;) {
|
|
486
|
+
if (scan_pragma_empty(lexer, trivia)) continue;
|
|
428
487
|
int32_t c = lexer->lookahead;
|
|
429
488
|
if (pragma_space(c)) {
|
|
430
489
|
lexer->advance(lexer, false);
|
package/src/macroProof.h
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
#ifndef TREE_SITTER_C_MACRO_PROOF_H_
|
|
2
|
+
#define TREE_SITTER_C_MACRO_PROOF_H_
|
|
3
|
+
|
|
4
|
+
#include "pragma.h"
|
|
5
|
+
#include <stdlib.h>
|
|
6
|
+
|
|
7
|
+
typedef struct { void *children[256]; } MacroProofNode;
|
|
8
|
+
typedef struct { uint32_t values[256]; } MacroProofPage;
|
|
9
|
+
typedef struct { uint32_t byte; uint8_t state; } MacroProofKey;
|
|
10
|
+
typedef struct {
|
|
11
|
+
MacroProofNode root;
|
|
12
|
+
MacroProofKey *trace;
|
|
13
|
+
uint32_t length, capacity;
|
|
14
|
+
const char *const *reserved_words;
|
|
15
|
+
unsigned reserved_count;
|
|
16
|
+
bool (*skip_empty)(TSLexer *, void *);
|
|
17
|
+
void *empty_context;
|
|
18
|
+
bool bound;
|
|
19
|
+
} MacroProofMemo;
|
|
20
|
+
|
|
21
|
+
static MacroProofPage *macro_proof_page(MacroProofMemo *memo, uint32_t byte, bool create);
|
|
22
|
+
static void macro_proof_append(MacroProofMemo *memo, MacroProofKey key);
|
|
23
|
+
static void macro_proof_free_node(MacroProofNode *node, unsigned level);
|
|
24
|
+
|
|
25
|
+
static bool macro_proof_begin(MacroProofMemo *memo, const char *const *reserved_words, unsigned reserved_count, const PragmaTrivia *trivia) {
|
|
26
|
+
if (!memo) return false;
|
|
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;
|
|
29
|
+
memo->bound = true;
|
|
30
|
+
memo->reserved_words = reserved_words;
|
|
31
|
+
memo->reserved_count = reserved_count;
|
|
32
|
+
memo->skip_empty = trivia->skip_empty;
|
|
33
|
+
memo->empty_context = trivia->empty_context;
|
|
34
|
+
memo->length = 0;
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static unsigned macro_proof_step(MacroProofMemo *memo, TSLexer *lexer, uint8_t state) {
|
|
39
|
+
if (!memo) return 0;
|
|
40
|
+
TSLexerPosition position;
|
|
41
|
+
lexer->get_position(lexer, &position);
|
|
42
|
+
MacroProofPage *page = macro_proof_page(memo, position.bytes, false);
|
|
43
|
+
unsigned result = page ? (page->values[position.bytes & 255] >> (2 * state)) & 3 : 0;
|
|
44
|
+
if (!result) macro_proof_append(memo, (MacroProofKey){position.bytes, state});
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static void macro_proof_finish(MacroProofMemo *memo, bool accepted) {
|
|
49
|
+
if (!memo) return;
|
|
50
|
+
for (uint32_t i = 0; i < memo->length; i++) {
|
|
51
|
+
MacroProofKey key = memo->trace[i];
|
|
52
|
+
MacroProofPage *page = macro_proof_page(memo, key.byte, true);
|
|
53
|
+
if (!page) continue;
|
|
54
|
+
unsigned shift = 2 * key.state;
|
|
55
|
+
uint32_t *value = &page->values[key.byte & 255];
|
|
56
|
+
*value = (*value & ~(3u << shift)) | ((accepted ? 2u : 1u) << shift);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static void macro_proof_destroy(MacroProofMemo *memo) {
|
|
61
|
+
if (!memo) return;
|
|
62
|
+
macro_proof_free_node(&memo->root, 3);
|
|
63
|
+
free(memo->trace);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
static MacroProofPage *macro_proof_page(MacroProofMemo *memo, uint32_t byte, bool create) {
|
|
67
|
+
MacroProofNode *node = &memo->root;
|
|
68
|
+
for (unsigned level = 3; level > 0; level--) {
|
|
69
|
+
unsigned index = (byte >> (8 * level)) & 255;
|
|
70
|
+
if (!node->children[index] && create) {
|
|
71
|
+
node->children[index] = calloc(1, level == 1 ? sizeof(MacroProofPage) : sizeof(MacroProofNode));
|
|
72
|
+
}
|
|
73
|
+
if (!node->children[index]) return NULL;
|
|
74
|
+
if (level == 1) return (MacroProofPage *)node->children[index];
|
|
75
|
+
node = (MacroProofNode *)node->children[index];
|
|
76
|
+
}
|
|
77
|
+
return NULL;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
static void macro_proof_append(MacroProofMemo *memo, MacroProofKey key) {
|
|
81
|
+
if (memo->length == memo->capacity) {
|
|
82
|
+
uint32_t next = memo->capacity ? memo->capacity * 2 : 64;
|
|
83
|
+
if (next < memo->capacity || (size_t)next > SIZE_MAX / sizeof(*memo->trace)) return;
|
|
84
|
+
void *trace = realloc(memo->trace, (size_t)next * sizeof(*memo->trace));
|
|
85
|
+
if (!trace) return;
|
|
86
|
+
memo->trace = (MacroProofKey *)trace;
|
|
87
|
+
memo->capacity = next;
|
|
88
|
+
}
|
|
89
|
+
memo->trace[memo->length++] = key;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static void macro_proof_free_node(MacroProofNode *node, unsigned level) {
|
|
93
|
+
for (unsigned i = 0; i < 256; i++) {
|
|
94
|
+
if (!node->children[i]) continue;
|
|
95
|
+
if (level > 1) macro_proof_free_node((MacroProofNode *)node->children[i], level - 1);
|
|
96
|
+
free(node->children[i]);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
#endif
|
package/src/node-types.json
CHANGED