@willbooster/tree-sitter-c 43.0.0 → 45.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/src/scanner.c CHANGED
@@ -7,16 +7,82 @@ typedef struct {
7
7
  MacroProofMemo proof_memo;
8
8
  } CScanner;
9
9
 
10
- enum TokenType { PRAGMA_OPERATOR, PREPROC_ARG, PREPROC_NEWLINE, PREPROC_LPAREN, PREPROC_DIRECTIVE_ARG, PREPROC_FUNCTION_NAME, FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX, TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START, CONDITIONAL_MARKER_IFDEF, CONDITIONAL_MARKER_IFNDEF, LEADING_CONDITIONAL_MARKER_IFDEF, LEADING_CONDITIONAL_MARKER_IFNDEF, GNU_RAW_STRING_BODY, RAW_MACRO_TYPE_TAIL, BRACED_FUNCTION_ATTRIBUTE_START, BRACED_MACRO_TYPE_START, DIRECTIVE_CONTEXT, PREPROC_EMPTY, PREPROC_DIRECTIVE_START, TOKEN_COUNT };
10
+ enum TokenType {
11
+ PRAGMA_OPERATOR,
12
+ PREPROC_ARG,
13
+ PREPROC_NEWLINE,
14
+ PREPROC_LPAREN,
15
+ PREPROC_DIRECTIVE_ARG,
16
+ PREPROC_FUNCTION_NAME,
17
+ FUNCTION_ATTRIBUTE_START,
18
+ POST_TYPE_FUNCTION_ATTRIBUTE_START,
19
+ POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX,
20
+ TRAILING_MACRO_ATTRIBUTE_NAME,
21
+ TRAILING_OBJECT_DECLARATION_START,
22
+ CONDITIONAL_MARKER_IFDEF,
23
+ CONDITIONAL_MARKER_IFNDEF,
24
+ LEADING_CONDITIONAL_MARKER_IFDEF,
25
+ LEADING_CONDITIONAL_MARKER_IFNDEF,
26
+ GNU_RAW_STRING_BODY,
27
+ RAW_MACRO_TYPE_TAIL,
28
+ BRACED_FUNCTION_ATTRIBUTE_START,
29
+ BRACED_MACRO_TYPE_START,
30
+ DIRECTIVE_CONTEXT,
31
+ PREPROC_EMPTY,
32
+ PREPROC_INCLUDE,
33
+ PREPROC_DEFINE,
34
+ PREPROC_IF,
35
+ PREPROC_IFDEF,
36
+ PREPROC_IFNDEF,
37
+ PREPROC_ELSE,
38
+ PREPROC_ELIF,
39
+ PREPROC_ELIFDEF,
40
+ PREPROC_ELIFNDEF,
41
+ PREPROC_ENDIF,
42
+ PREPROC_PRAGMA,
43
+ PREPROC_DIRECTIVE,
44
+ TOKEN_COUNT
45
+ };
11
46
 
12
- static EmptyDirectives *scanner_directives(CScanner *scanner);
47
+ static const PreprocDirectiveSymbols directive_symbols = {
48
+ .include = PREPROC_INCLUDE,
49
+ .define = PREPROC_DEFINE,
50
+ .preproc_if = PREPROC_IF,
51
+ .ifdef = PREPROC_IFDEF,
52
+ .ifndef = PREPROC_IFNDEF,
53
+ .preproc_else = PREPROC_ELSE,
54
+ .elif = PREPROC_ELIF,
55
+ .elifdef = PREPROC_ELIFDEF,
56
+ .elifndef = PREPROC_ELIFNDEF,
57
+ .endif = PREPROC_ENDIF,
58
+ .pragma = PREPROC_PRAGMA,
59
+ .directive = PREPROC_DIRECTIVE,
60
+ };
61
+
62
+ #if defined(__GNUC__) || defined(__clang__)
63
+ #define SCANNER_NOINLINE __attribute__((noinline))
64
+ #else
65
+ #define SCANNER_NOINLINE
66
+ #endif
13
67
 
68
+ static inline bool can_start_spaced_c_token(uint32_t c, const EmptyDirectives *state, const bool *valid_symbols);
69
+ static SCANNER_NOINLINE bool scan_c_token_spaced_fast(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols);
70
+ static SCANNER_NOINLINE bool scan_c_token_slow(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols);
71
+ static EmptyDirectives *scanner_directives(CScanner *scanner);
72
+ static bool in_error_recovery(const bool *valid_symbols);
73
+ static inline bool any_preproc_body_valid(const bool *valid_symbols);
74
+ static inline bool any_attribute_valid(const bool *valid_symbols);
14
75
  static bool only_extras_valid(const EmptyDirectives *state, const bool *valid_symbols);
15
- static bool scan_extras(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
76
+ static bool scan_extras(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols);
77
+ static bool scan_empty_directive_extra(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols, bool error_recovery);
78
+ static bool scan_error_recovery_tokens(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
79
+ static bool scan_active_tokens(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols);
80
+ static bool scan_non_declaration_tokens(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
81
+ static bool scan_hash_directive(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols, bool error_recovery);
82
+ static bool scan_declaration_tokens(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols, bool consumed_backslash);
16
83
  static bool scan_preproc_tokens(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
17
- static bool scan_declaration_tokens(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols);
18
- static bool scan_directive_context(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
19
- static bool scan_conditional_directive(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
84
+ static bool is_c_space(int32_t c);
85
+ static bool starts_macro_scan(int32_t c, bool consumed_backslash);
20
86
 
21
87
  void *tree_sitter_c_external_scanner_create(void) {
22
88
  return calloc(1, sizeof(CScanner));
@@ -47,77 +113,140 @@ void tree_sitter_c_external_scanner_deserialize(void *payload, const char *buffe
47
113
  state->anchored = length && (buffer[0] & SERIALIZED_ANCHORED);
48
114
  }
49
115
 
50
- // code-gauge-allow functionCognitiveComplexity -- External-token validity dispatches each lexing mode to its scanner.
51
116
  bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
52
117
  CScanner *scanner = payload;
53
118
  EmptyDirectives *state = scanner_directives(scanner);
54
- if (only_extras_valid(state, valid_symbols)) return scan_extras(state, lexer, valid_symbols);
119
+ int32_t look = lexer->lookahead;
120
+ if (look > 0 && look < 0x80 && look != '\\' && state && state->ready && !state->starts && !state->failed &&
121
+ !any_preproc_body_valid(valid_symbols)) {
122
+ if (look == ' ' || (uint32_t)(look - '\t') < 5) {
123
+ return scan_c_token_spaced_fast(scanner, lexer, valid_symbols);
124
+ }
125
+ if (!can_start_spaced_c_token((uint32_t)look, state, valid_symbols)) return false;
126
+ }
127
+ return scan_c_token_slow(scanner, lexer, valid_symbols);
128
+ }
129
+
130
+ static inline bool can_start_spaced_c_token(uint32_t c, const EmptyDirectives *state, const bool *valid_symbols) {
131
+ if (c == '#') return state->directive_starts.count > 0;
132
+ if (c == '[') return valid_symbols[BRACED_MACRO_TYPE_START];
133
+ if (!((preproc_identifier_start_ascii[c / 64] >> (c % 64)) & 1)) return false;
134
+ return any_attribute_valid(valid_symbols) || valid_symbols[BRACED_MACRO_TYPE_START] ||
135
+ (valid_symbols[TRAILING_OBJECT_DECLARATION_START] && peek_object_starter((uint16_t)c)) ||
136
+ (c == '_' && valid_symbols[PRAGMA_OPERATOR] && state->pragmas);
137
+ }
138
+
139
+ static SCANNER_NOINLINE bool scan_c_token_spaced_fast(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
140
+ int32_t peeked = peek_after_ascii_spaces(lexer);
141
+ if (peeked > 0 && !can_start_spaced_c_token((uint32_t)peeked, &scanner->directives, valid_symbols)) return false;
142
+ return scan_c_token_slow(scanner, lexer, valid_symbols);
143
+ }
144
+
145
+ static SCANNER_NOINLINE bool scan_c_token_slow(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
146
+ EmptyDirectives *state = scanner_directives(scanner);
147
+ if (only_extras_valid(state, valid_symbols)) return scan_extras(scanner, lexer, valid_symbols);
55
148
  if (state && !state->ready && valid_symbols[DIRECTIVE_CONTEXT]) return empty_directives_initialize(state, lexer, DIRECTIVE_CONTEXT);
56
- if (scan_directive_context(state, lexer, valid_symbols)) return true;
149
+ bool error_recovery = in_error_recovery(valid_symbols);
150
+ if (scan_empty_directive_extra(state, lexer, valid_symbols, error_recovery)) return true;
151
+ return error_recovery
152
+ ? scan_error_recovery_tokens(state, lexer, valid_symbols)
153
+ : scan_active_tokens(scanner, lexer, valid_symbols);
154
+ }
155
+
156
+ static bool scan_error_recovery_tokens(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
157
+ bool consumed_backslash = false;
158
+ skip_leading_spaces_and_splices(lexer, is_c_space, NULL, &consumed_backslash);
159
+ if (!consumed_backslash && lexer->lookahead == '#') return scan_hash_directive(state, lexer, valid_symbols, true);
160
+ lexer->result_symbol = PRAGMA_OPERATOR;
161
+ return !consumed_backslash && valid_symbols[PRAGMA_OPERATOR] && empty_directives_pragma_possible(state) &&
162
+ scan_pragma_with(lexer, &(PragmaTrivia){empty_directives_skip, state});
163
+ }
164
+
165
+ static bool scan_active_tokens(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
166
+ EmptyDirectives *state = scanner_directives(scanner);
57
167
  if (empty_directives_at_line_end(state, lexer)) return false;
58
- if (valid_symbols[GNU_RAW_STRING_BODY] && !valid_symbols[PREPROC_LPAREN]) {
59
- lexer->result_symbol = GNU_RAW_STRING_BODY;
60
- return scan_gnu_raw_string_token(lexer);
61
- }
62
- if (valid_symbols[RAW_MACRO_TYPE_TAIL] && !valid_symbols[PREPROC_LPAREN]) {
63
- lexer->result_symbol = RAW_MACRO_TYPE_TAIL;
64
- return scan_raw_macro_type_tail(lexer, &(PragmaTrivia){empty_directives_skip, state});
65
- }
66
- if (valid_symbols[PREPROC_FUNCTION_NAME] && !valid_symbols[PREPROC_LPAREN]) {
67
- lexer->result_symbol = PREPROC_FUNCTION_NAME;
68
- return scan_function_macro_name(lexer, valid_symbols[PRAGMA_OPERATOR], PRAGMA_OPERATOR);
69
- }
70
- if (valid_symbols[PREPROC_LPAREN] || valid_symbols[PREPROC_NEWLINE] ||
71
- valid_symbols[PREPROC_ARG] || valid_symbols[PREPROC_DIRECTIVE_ARG]) {
72
- return scan_preproc_tokens(state, lexer, valid_symbols);
168
+ if (any_preproc_body_valid(valid_symbols)) return scan_non_declaration_tokens(state, lexer, valid_symbols);
169
+ int32_t peeked = peek_after_ascii_spaces(lexer);
170
+ if (peeked >= 0) {
171
+ bool ident = starts_macro_scan(peeked, false) &&
172
+ (any_attribute_valid(valid_symbols) || valid_symbols[BRACED_MACRO_TYPE_START] ||
173
+ (valid_symbols[TRAILING_OBJECT_DECLARATION_START] && peek_object_starter((uint16_t)peeked)) ||
174
+ (peeked == '_' && valid_symbols[PRAGMA_OPERATOR] && empty_directives_pragma_possible(state)));
175
+ if (!ident && !(peeked == '#' && empty_directives_has_directives(state)) &&
176
+ !(peeked == '[' && valid_symbols[BRACED_MACRO_TYPE_START])) {
177
+ return false;
178
+ }
73
179
  }
74
- bool conditional = valid_symbols[LEADING_CONDITIONAL_MARKER_IFDEF] || valid_symbols[LEADING_CONDITIONAL_MARKER_IFNDEF] ||
75
- valid_symbols[CONDITIONAL_MARKER_IFDEF] || valid_symbols[CONDITIONAL_MARKER_IFNDEF];
76
- if (!conditional) return scan_declaration_tokens(scanner, lexer, valid_symbols);
77
- while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
78
- if (lexer->lookahead == '#') return scan_conditional_directive(state, lexer, valid_symbols);
79
- return scan_declaration_tokens(scanner, lexer, valid_symbols);
180
+ bool consumed_backslash = false;
181
+ skip_leading_spaces_and_splices(lexer, is_c_space, NULL, &consumed_backslash);
182
+ if (!consumed_backslash && lexer->lookahead == '#') return scan_hash_directive(state, lexer, valid_symbols, false);
183
+ return scan_declaration_tokens(scanner, lexer, valid_symbols, consumed_backslash);
184
+ }
185
+
186
+ static inline bool any_preproc_body_valid(const bool *valid_symbols) {
187
+ _Static_assert(PREPROC_FUNCTION_NAME - PREPROC_ARG == 4 && RAW_MACRO_TYPE_TAIL - GNU_RAW_STRING_BODY == 1,
188
+ "preproc body tokens are contiguous");
189
+ uint64_t head = 0;
190
+ uint16_t raw = 0;
191
+ memcpy(&head, valid_symbols + PREPROC_ARG, 5);
192
+ memcpy(&raw, valid_symbols + GNU_RAW_STRING_BODY, 2);
193
+ return (head | raw) != 0;
194
+ }
195
+
196
+ static inline bool any_attribute_valid(const bool *valid_symbols) {
197
+ _Static_assert(TRAILING_MACRO_ATTRIBUTE_NAME - FUNCTION_ATTRIBUTE_START == 3,
198
+ "attribute tokens are contiguous");
199
+ uint32_t bits = 0;
200
+ memcpy(&bits, valid_symbols + FUNCTION_ATTRIBUTE_START, 4);
201
+ return bits != 0 || valid_symbols[BRACED_FUNCTION_ATTRIBUTE_START];
202
+ }
203
+
204
+ static bool in_error_recovery(const bool *valid_symbols) {
205
+ return valid_symbols[GNU_RAW_STRING_BODY] && valid_symbols[RAW_MACRO_TYPE_TAIL];
80
206
  }
81
207
 
82
208
  static bool only_extras_valid(const EmptyDirectives *state, const bool *valid_symbols) {
83
- static const uint8_t extras[TOKEN_COUNT] = {[PRAGMA_OPERATOR] = 0xff, [DIRECTIVE_CONTEXT] = 0xff, [PREPROC_EMPTY] = 0xff};
84
- return state && state->ready && empty_directives_only_valid(valid_symbols, extras, TOKEN_COUNT);
209
+ static const uint8_t extras[24] = {
210
+ [PRAGMA_OPERATOR] = 0xff,
211
+ [TRAILING_OBJECT_DECLARATION_START] = 0xff,
212
+ [CONDITIONAL_MARKER_IFDEF] = 0xff, [CONDITIONAL_MARKER_IFNDEF] = 0xff,
213
+ [LEADING_CONDITIONAL_MARKER_IFDEF] = 0xff, [LEADING_CONDITIONAL_MARKER_IFNDEF] = 0xff,
214
+ [DIRECTIVE_CONTEXT] = 0xff, [PREPROC_EMPTY] = 0xff,
215
+ [PREPROC_INCLUDE] = 0xff, [PREPROC_DEFINE] = 0xff, [PREPROC_IF] = 0xff,
216
+ };
217
+ _Static_assert(DIRECTIVE_CONTEXT == 19 && TOKEN_COUNT == 33, "trailing tokens are all extras");
218
+ return state && state->ready && empty_directives_only_valid(valid_symbols, extras, sizeof extras);
85
219
  }
86
220
 
87
- // Fails without consuming a character unless a null directive starts here or the input holds `pragma_word`, since
221
+ // Fails without consuming a character unless a directive, pragma, or object-declaration guard can start here, since
88
222
  // the lexer rereads from the start what a failed scan consumed.
89
- static bool scan_extras(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
223
+ static bool scan_extras(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
224
+ EmptyDirectives *state = scanner_directives(scanner);
90
225
  if (valid_symbols[PREPROC_EMPTY] && empty_directives_consume(lexer, state, true)) {
91
226
  lexer->mark_end(lexer);
92
227
  lexer->result_symbol = PREPROC_EMPTY;
93
228
  return true;
94
229
  }
95
- if (!valid_symbols[PRAGMA_OPERATOR] || !state->pragmas || empty_directives_at_line_end(state, lexer)) return false;
96
- while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
97
- if (!scan_pragma_word(lexer)) return false;
98
- lexer->result_symbol = PRAGMA_OPERATOR;
99
- return scan_pragma_suffix_end_with(lexer, true, &(PragmaTrivia){empty_directives_skip, state});
100
- }
101
-
102
- static bool scan_conditional_directive(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
103
- if (!empty_directives_at_start(state, lexer)) return false;
104
- const ConditionalMacroSymbols symbols = {
105
- .ifdef = CONDITIONAL_MARKER_IFDEF, .ifndef = CONDITIONAL_MARKER_IFNDEF,
106
- .leading_ifdef = LEADING_CONDITIONAL_MARKER_IFDEF, .leading_ifndef = LEADING_CONDITIONAL_MARKER_IFNDEF,
107
- .skip_empty = empty_directives_skip, .empty_context = state,
108
- };
109
- return scan_conditional_macro(lexer, &symbols, valid_symbols);
230
+ bool allow_directive = empty_directives_has_directives(state);
231
+ bool allow_object = valid_symbols[TRAILING_OBJECT_DECLARATION_START];
232
+ bool allow_pragma = valid_symbols[PRAGMA_OPERATOR] && state->pragmas;
233
+ PeekExtrasKind kind = peek_extras_kind(lexer, allow_directive, allow_object, allow_pragma);
234
+ if (kind == PEEK_EXTRAS_NONE) return false;
235
+ bool at_line_end = kind != PEEK_EXTRAS_DIRECTIVE && empty_directives_at_line_end(state, lexer);
236
+ if (kind == PEEK_EXTRAS_DECLARATION && at_line_end) return false;
237
+ bool consumed_backslash = false;
238
+ skip_leading_spaces_and_splices(lexer, is_c_space, NULL, &consumed_backslash);
239
+ if (!consumed_backslash && lexer->lookahead == '#') {
240
+ return allow_directive && scan_hash_directive(state, lexer, valid_symbols, false);
241
+ }
242
+ if ((!allow_object && !allow_pragma) || at_line_end) return false;
243
+ return scan_declaration_tokens(scanner, lexer, valid_symbols, consumed_backslash);
110
244
  }
111
245
 
112
- static bool scan_directive_context(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
113
- if (valid_symbols[PREPROC_DIRECTIVE_START] && empty_directives_after_keyword(state, lexer)) {
114
- lexer->result_symbol = PREPROC_DIRECTIVE_START;
115
- lexer->mark_end(lexer);
116
- return true;
117
- }
118
- bool directive_body = valid_symbols[PREPROC_NEWLINE] || valid_symbols[PREPROC_LPAREN] ||
119
- valid_symbols[PREPROC_ARG] || valid_symbols[PREPROC_DIRECTIVE_ARG] || valid_symbols[GNU_RAW_STRING_BODY];
120
- bool macro_name = valid_symbols[PREPROC_FUNCTION_NAME] && !valid_symbols[PREPROC_LPAREN];
246
+ static bool scan_empty_directive_extra(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols, bool error_recovery) {
247
+ bool directive_body = !error_recovery && (valid_symbols[PREPROC_NEWLINE] || valid_symbols[PREPROC_LPAREN] ||
248
+ valid_symbols[PREPROC_ARG] || valid_symbols[PREPROC_DIRECTIVE_ARG] || valid_symbols[GNU_RAW_STRING_BODY]);
249
+ bool macro_name = !error_recovery && valid_symbols[PREPROC_FUNCTION_NAME];
121
250
  if (valid_symbols[PREPROC_EMPTY] && !macro_name && (!directive_body || lexer->lookahead == '#') &&
122
251
  empty_directives_consume(lexer, state, true)) {
123
252
  lexer->mark_end(lexer);
@@ -127,8 +256,56 @@ static bool scan_directive_context(EmptyDirectives *state, TSLexer *lexer, const
127
256
  return false;
128
257
  }
129
258
 
130
- static bool scan_declaration_tokens(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
259
+ static bool scan_non_declaration_tokens(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
260
+ if (valid_symbols[GNU_RAW_STRING_BODY]) {
261
+ lexer->result_symbol = GNU_RAW_STRING_BODY;
262
+ return scan_gnu_raw_string_token(lexer);
263
+ }
264
+ if (valid_symbols[RAW_MACRO_TYPE_TAIL]) {
265
+ lexer->result_symbol = RAW_MACRO_TYPE_TAIL;
266
+ return scan_raw_macro_type_tail(lexer, &(PragmaTrivia){empty_directives_skip, state});
267
+ }
268
+ if (valid_symbols[PREPROC_FUNCTION_NAME]) {
269
+ lexer->result_symbol = PREPROC_FUNCTION_NAME;
270
+ return scan_function_macro_name(lexer, valid_symbols[PRAGMA_OPERATOR], PRAGMA_OPERATOR);
271
+ }
272
+ return scan_preproc_tokens(state, lexer, valid_symbols);
273
+ }
274
+
275
+ static bool scan_hash_directive(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols, bool error_recovery) {
276
+ char word[10] = {0};
277
+ uint32_t keyword_end = 0;
278
+ bool at_start = empty_directives_at_start(state, lexer);
279
+ if ((!at_start && !error_recovery) || !empty_directives_scan_word(lexer, word, sizeof word, &keyword_end)) return false;
280
+ if (!at_start || !empty_directives_has_keyword_end(state, keyword_end)) {
281
+ if (!error_recovery) return false;
282
+ lexer->result_symbol = GNU_RAW_STRING_BODY;
283
+ return true;
284
+ }
285
+ bool ifdef = strcmp(word, "ifdef") == 0;
286
+ bool conditional = !error_recovery && (valid_symbols[LEADING_CONDITIONAL_MARKER_IFDEF] ||
287
+ valid_symbols[LEADING_CONDITIONAL_MARKER_IFNDEF] || valid_symbols[CONDITIONAL_MARKER_IFDEF] ||
288
+ valid_symbols[CONDITIONAL_MARKER_IFNDEF]);
289
+ if (conditional && (ifdef || strcmp(word, "ifndef") == 0)) {
290
+ TSLexerCheckpoint end;
291
+ lexer->get_mark_end_checkpoint(lexer, &end);
292
+ const ConditionalMacroSymbols symbols = {
293
+ .ifdef = CONDITIONAL_MARKER_IFDEF, .ifndef = CONDITIONAL_MARKER_IFNDEF,
294
+ .leading_ifdef = LEADING_CONDITIONAL_MARKER_IFDEF, .leading_ifndef = LEADING_CONDITIONAL_MARKER_IFNDEF,
295
+ .skip_empty = empty_directives_skip, .empty_context = state,
296
+ };
297
+ if (scan_conditional_macro_after_keyword(lexer, ifdef, &symbols, valid_symbols)) return true;
298
+ lexer->mark_end_at(lexer, &end);
299
+ }
300
+ return empty_directives_select_symbol(word, &directive_symbols, valid_symbols, &lexer->result_symbol);
301
+ }
302
+
303
+ static bool scan_declaration_tokens(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols, bool consumed_backslash) {
131
304
  EmptyDirectives *state = scanner_directives(scanner);
305
+ int32_t c = lexer->lookahead;
306
+ bool can_start_braced = valid_symbols[BRACED_MACRO_TYPE_START] &&
307
+ ((!consumed_backslash && c == '[') || starts_macro_scan(c, consumed_backslash));
308
+ if (!can_start_braced && !starts_macro_scan(c, consumed_backslash)) return false;
132
309
  static const TSSymbol attribute_tokens[] = {
133
310
  FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX, POST_TYPE_FUNCTION_ATTRIBUTE_START,
134
311
  TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START, BRACED_FUNCTION_ATTRIBUTE_START,
@@ -143,18 +320,19 @@ static bool scan_declaration_tokens(CScanner *scanner, TSLexer *lexer, const boo
143
320
  .braced_start = BRACED_FUNCTION_ATTRIBUTE_START,
144
321
  .allow_braced_start = valid_symbols[BRACED_FUNCTION_ATTRIBUTE_START],
145
322
  };
146
- if (valid_symbols[BRACED_MACRO_TYPE_START]) {
323
+ if (can_start_braced) {
147
324
  lexer->result_symbol = BRACED_MACRO_TYPE_START;
148
- return scan_braced_macro_type_start(lexer, &symbols, NULL, 0);
325
+ return scan_braced_macro_type_start_from(lexer, consumed_backslash, &symbols, NULL, 0);
149
326
  }
150
327
  for (unsigned i = 0; i < sizeof(attribute_tokens) / sizeof(attribute_tokens[0]); i++) {
151
328
  TSSymbol token = attribute_tokens[i];
152
329
  if (!valid_symbols[token]) continue;
153
330
  lexer->result_symbol = token;
154
- return scan_function_attribute_guard(lexer,
331
+ return scan_function_attribute_guard_from(lexer,
155
332
  token == FUNCTION_ATTRIBUTE_START || token == POST_TYPE_FUNCTION_ATTRIBUTE_START,
156
- token != FUNCTION_ATTRIBUTE_START, token == TRAILING_MACRO_ATTRIBUTE_NAME, &symbols);
333
+ token != FUNCTION_ATTRIBUTE_START, token == TRAILING_MACRO_ATTRIBUTE_NAME, consumed_backslash, &symbols);
157
334
  }
335
+ if (consumed_backslash) return false;
158
336
  return scan_preproc_tokens(state, lexer, valid_symbols);
159
337
  }
160
338
 
@@ -204,6 +382,17 @@ static bool scan_preproc_tokens(EmptyDirectives *state, TSLexer *lexer, const bo
204
382
  scan_pragma_with(lexer, &(PragmaTrivia){empty_directives_skip, state});
205
383
  }
206
384
 
385
+ static bool is_c_space(int32_t c) {
386
+ return c == ' ' || (uint32_t)(c - '\t') <= '\r' - '\t';
387
+ }
388
+
389
+ static bool starts_macro_scan(int32_t c, bool consumed_backslash) {
390
+ if (consumed_backslash) return c == 'u' || c == 'U';
391
+ if (c < 0) return false;
392
+ if (c < 0x80) return c == '\\' || ((preproc_identifier_start_ascii[c / 64] >> (c % 64)) & 1);
393
+ return preproc_identifier_character(c, false);
394
+ }
395
+
207
396
  static EmptyDirectives *scanner_directives(CScanner *scanner) {
208
397
  return scanner ? &scanner->directives : NULL;
209
398
  }
Binary file
package/tree-sitter.json CHANGED
@@ -18,6 +18,7 @@
18
18
  "src/functionMacro.h",
19
19
  "src/macroProof.h",
20
20
  "src/conditionalMacro.h",
21
+ "src/directiveFacts.h",
21
22
  "src/emptyDirectives.h",
22
23
  "src/directiveScan.h",
23
24
  "src/directiveUnits.h",
@@ -27,7 +28,7 @@
27
28
  }
28
29
  ],
29
30
  "metadata": {
30
- "version": "43.0.0",
31
+ "version": "45.0.0",
31
32
  "license": "MIT",
32
33
  "description": "C grammar for tree-sitter",
33
34
  "authors": [
@@ -166,6 +166,11 @@ export interface Grammar<
166
166
  previous: SymbolRule<string>[],
167
167
  ) => SymbolRule<string>[];
168
168
 
169
+ mergeableExternals?: (
170
+ $: GrammarSymbols<RuleName | BaseGrammarRuleName>,
171
+ previous: SymbolRule<string>[],
172
+ ) => SymbolRule<string>[];
173
+
169
174
  /**
170
175
  * The name of a token that will match keywords for the purpose of the
171
176
  * keyword extraction optimization.
@@ -212,6 +217,7 @@ export type GrammarSchema<RuleName extends string> = {
212
217
  extras: Rule[];
213
218
  inline: string[];
214
219
  supertypes: string[];
220
+ mergeable_externals?: string[];
215
221
  word: string | undefined;
216
222
  reserved: Record<string, Rule[]>;
217
223
  };