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