@willbooster/tree-sitter-c 34.0.1 → 34.0.2

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 CHANGED
@@ -74,7 +74,10 @@ Append `_preproc_directive_start` in its inherited slot as a zero-width guard af
74
74
  including keywords in overridden directive rules. It is not an extra. Retain the phase-two splice handling in
75
75
  `preprocessorToken` when overriding keyword spelling.
76
76
  Port the scanner lifecycle functions together with `EmptyDirectives` from `emptyDirectives.h`; the source-boundary
77
- facts belong to one full parse and are freed by scanner destruction. Run `scan_directive_context` first,
77
+ facts belong to one full parse and are freed by scanner destruction. When the scanner is ready and no external token
78
+ other than `pragma_operator`, `_directive_context` and `preproc_empty` is valid, dispatch to `scan_extras` first;
79
+ `only_extras_valid` checks the inherited slots only, so extend it to every external token the derived grammar adds,
80
+ or the fast path drops those tokens. Otherwise run `scan_directive_context` first,
78
81
  emitting `_preproc_directive_start` only at `empty_directives_after_keyword` before null-directive consumption,
79
82
  then reject external scans at `empty_directives_at_line_end` before the other token dispatch. Pass `empty_directives_skip` and its state through `ConditionalMacroSymbols.skip_empty` and
80
83
  `empty_context` so declaration lookahead ignores null directives without moving the opening token's range.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-c",
3
- "version": "34.0.1",
3
+ "version": "34.0.2",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "c",
@@ -1,4 +1,4 @@
1
- // code-gauge-allow-file fileNcss 695 -- One source-fact pass shares compact character storage, directive lexing and physical-position conversion.
1
+ // code-gauge-allow-file fileNcss 706 -- One source-fact pass shares compact character storage, directive lexing and physical-position conversion.
2
2
  #ifndef TREE_SITTER_C_EMPTY_DIRECTIVES_H_
3
3
  #define TREE_SITTER_C_EMPTY_DIRECTIVES_H_
4
4
 
@@ -59,6 +59,9 @@ static void directive_line_comment(EmptyDirectives *state, DirectiveInput *input
59
59
  static uint32_t directive_splice_end(const DirectiveInput *input);
60
60
 
61
61
  static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer);
62
+ static bool empty_directives_line_end_at(const EmptyDirectives *state, uint32_t byte);
63
+ static bool empty_directives_start_at(const EmptyDirectives *state, uint32_t byte);
64
+ static void empty_directives_consume_started(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix);
62
65
  static void empty_directives_find(EmptyDirectives *state, DirectiveInput *input);
63
66
  static int32_t directive_next(DirectiveInput *input);
64
67
  static int32_t directive_physical_next(DirectiveInput *input);
@@ -120,10 +123,14 @@ static bool empty_directives_at_start(const EmptyDirectives *state, TSLexer *lex
120
123
  }
121
124
 
122
125
  static bool empty_directives_at_line_end(const EmptyDirectives *state, TSLexer *lexer) {
123
- if (!state || !state->line_ends) return false;
126
+ if (!state || !state->line_ends || !(directive_horizontal(lexer->lookahead) || directive_newline(lexer->lookahead))) return false;
124
127
  TSLexerPosition position;
125
128
  lexer->get_position(lexer, &position);
126
- return position.bytes < state->bytes && (state->line_ends[position.bytes / 8] & (1u << (position.bytes % 8)));
129
+ return empty_directives_line_end_at(state, position.bytes);
130
+ }
131
+
132
+ static bool empty_directives_line_end_at(const EmptyDirectives *state, uint32_t byte) {
133
+ return state->line_ends && byte < state->bytes && (state->line_ends[byte / 8] & (1u << (byte % 8)));
127
134
  }
128
135
 
129
136
  static bool empty_directives_consume(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix);
@@ -132,18 +139,32 @@ static bool empty_directives_skip(TSLexer *lexer, void *payload) {
132
139
  return empty_directives_consume(lexer, payload, false);
133
140
  }
134
141
 
142
+ // A null directive, including its prefix, starts only at `#`, `\\` or a space, so other characters need no position query,
143
+ // which would make the runtime allocate the token on the heap.
135
144
  static bool empty_directives_consume(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix) {
136
- if (!state || !state->starts) return false;
145
+ int32_t c = lexer->lookahead;
146
+ if (!state || !(c == '#' || c == '\\' || pragma_space(c))) return false;
137
147
  TSLexerPosition position;
138
148
  lexer->get_position(lexer, &position);
139
- if (position.bytes >= state->bytes || !(state->starts[position.bytes / 8] & (1u << (position.bytes % 8)))) return false;
149
+ if (!empty_directives_start_at(state, position.bytes)) return false;
150
+ empty_directives_consume_started(lexer, state, skip_prefix);
151
+ return true;
152
+ }
153
+
154
+ static bool empty_directives_start_at(const EmptyDirectives *state, uint32_t byte) {
155
+ return state->starts && byte < state->bytes && (state->starts[byte / 8] & (1u << (byte % 8)));
156
+ }
157
+
158
+ // Consumes the rest of a null directive whose start was confirmed at a position the lexer has not left except over
159
+ // prefix characters.
160
+ static void empty_directives_consume_started(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix) {
161
+ TSLexerPosition position;
140
162
  while (lexer->lookahead != '#' && !lexer->eof(lexer)) lexer->advance(lexer, skip_prefix);
141
163
  do {
142
164
  lexer->advance(lexer, false);
143
165
  lexer->get_position(lexer, &position);
144
166
  } while (!lexer->eof(lexer) && position.bytes < state->bytes &&
145
167
  !(state->ends[position.bytes / 8] & (1u << (position.bytes % 8))));
146
- return true;
147
168
  }
148
169
 
149
170
  // code-gauge-allow functionNcss 33 -- The source-fact buffers are allocated and released together so initialization fails atomically.
@@ -1,4 +1,4 @@
1
- // code-gauge-allow-file fileNcss 567 -- Annotation and argument scanners share lexical helpers and keyword tables.
1
+ // code-gauge-allow-file fileNcss 575 -- 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
 
@@ -165,14 +165,16 @@ typedef enum {
165
165
  MACRO_TYPE_PREFIX_KNOWN_TYPE, MACRO_TYPE_PREFIX_BALANCED_TYPE, MACRO_TYPE_PREFIX_TAGGED_TYPE,
166
166
  } MacroTypePrefix;
167
167
 
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);
169
-
170
168
  typedef struct {
171
169
  bool marker, known_type, tag, body, started;
172
170
  } MacroTypeState;
173
171
 
174
172
  typedef enum { MACRO_TYPE_CONTINUE, MACRO_TYPE_ACCEPT, MACRO_TYPE_REJECT } MacroTypeResult;
175
173
 
174
+ static MacroTypePrefix function_macro_type_prefix_item(TSLexer *lexer, const MacroName *name, const char *const *reserved_words, unsigned reserved_count, const MacroTypeState *state, const PragmaTrivia *trivia);
175
+ static MacroTypePrefix function_macro_known_type_item(TSLexer *lexer, const MacroName *name, const PragmaTrivia *trivia);
176
+ static MacroTypePrefix function_macro_macro_type_item(TSLexer *lexer, const MacroTypeState *state, const PragmaTrivia *trivia);
177
+
176
178
  static MacroTypeResult function_macro_type_transition(MacroTypeState *state, MacroTypePrefix kind);
177
179
  static MacroTypeResult function_macro_type_group(TSLexer *lexer, MacroTypeState *state, const PragmaTrivia *trivia);
178
180
  static MacroTypeResult function_macro_type_word(TSLexer *lexer, const MacroAttributeSymbols *symbols,
@@ -228,7 +230,7 @@ static MacroTypeResult function_macro_type_word(TSLexer *lexer, const MacroAttri
228
230
  if (handled) return accepted ? MACRO_TYPE_ACCEPT : MACRO_TYPE_REJECT;
229
231
  }
230
232
  state->started = true;
231
- MacroTypePrefix kind = function_macro_type_prefix_item(lexer, &name, reserved_words, reserved_count, !state->known_type, &symbols->trivia);
233
+ MacroTypePrefix kind = function_macro_type_prefix_item(lexer, &name, reserved_words, reserved_count, state, &symbols->trivia);
232
234
  return function_macro_type_transition(state, kind);
233
235
  }
234
236
 
@@ -257,8 +259,8 @@ static MacroTypeResult function_macro_type_transition(MacroTypeState *state, Mac
257
259
  return MACRO_TYPE_REJECT;
258
260
  }
259
261
 
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) {
262
+ // code-gauge-allow functionCognitiveComplexity 20 -- 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, const MacroTypeState *state, const PragmaTrivia *trivia) {
262
264
  if (name->ascii && strcmp(name->word, pragma_word) == 0) {
263
265
  return scan_pragma_suffix_end_with(lexer, false, trivia) && scan_pragma_spacing_with(lexer, trivia)
264
266
  ? MACRO_TYPE_PREFIX_MODIFIER : MACRO_TYPE_PREFIX_INVALID;
@@ -268,24 +270,33 @@ static MacroTypePrefix function_macro_type_prefix_item(TSLexer *lexer, const Mac
268
270
  return lexer->lookahead == '(' && function_macro_modifier_balanced(lexer, '(', ')', trivia) && scan_pragma_spacing_with(lexer, trivia)
269
271
  ? MACRO_TYPE_PREFIX_MODIFIER : MACRO_TYPE_PREFIX_INVALID;
270
272
  }
273
+ // Before a marker, function_macro_type_transition rejects every type, so its parentheses need not be scanned.
271
274
  if (name->ascii && function_macro_word_in(name->word, function_macro_type_keywords,
272
275
  sizeof(function_macro_type_keywords) / sizeof(function_macro_type_keywords[0]))) {
273
- if (lexer->lookahead == '(' && (!function_macro_modifier_balanced(lexer, '(', ')', trivia) || !scan_pragma_spacing_with(lexer, trivia))) return MACRO_TYPE_PREFIX_INVALID;
274
- if (strcmp(name->word, "struct") == 0 || strcmp(name->word, "union") == 0 || strcmp(name->word, "enum") == 0) return MACRO_TYPE_PREFIX_TAGGED_TYPE;
275
- return MACRO_TYPE_PREFIX_KNOWN_TYPE;
276
+ return state->marker ? function_macro_known_type_item(lexer, name, trivia) : MACRO_TYPE_PREFIX_INVALID;
276
277
  }
277
278
  if (name->ascii && function_macro_word_in(name->word, function_macro_type_prefix_modifiers,
278
279
  sizeof(function_macro_type_prefix_modifiers) / sizeof(function_macro_type_prefix_modifiers[0]))) return MACRO_TYPE_PREFIX_MODIFIER;
279
280
  if (function_macro_type_prefix_keyword(name, reserved_words, reserved_count)) return MACRO_TYPE_PREFIX_INVALID;
280
281
  if (lexer->lookahead == '(') {
281
- MacroGroup group = function_macro_group_prefix(lexer, '(', ')', NULL, NULL, type_argument, trivia);
282
- if (group == MACRO_GROUP_BRACED) return MACRO_TYPE_PREFIX_TYPE;
283
- if (group == MACRO_GROUP_BALANCED && scan_pragma_spacing_with(lexer, trivia)) return MACRO_TYPE_PREFIX_BALANCED_TYPE;
284
- return MACRO_TYPE_PREFIX_INVALID;
282
+ return state->marker ? function_macro_macro_type_item(lexer, state, trivia) : MACRO_TYPE_PREFIX_INVALID;
285
283
  }
286
284
  return MACRO_TYPE_PREFIX_MARKER;
287
285
  }
288
286
 
287
+ static MacroTypePrefix function_macro_known_type_item(TSLexer *lexer, const MacroName *name, const PragmaTrivia *trivia) {
288
+ if (lexer->lookahead == '(' && (!function_macro_modifier_balanced(lexer, '(', ')', trivia) || !scan_pragma_spacing_with(lexer, trivia))) return MACRO_TYPE_PREFIX_INVALID;
289
+ if (strcmp(name->word, "struct") == 0 || strcmp(name->word, "union") == 0 || strcmp(name->word, "enum") == 0) return MACRO_TYPE_PREFIX_TAGGED_TYPE;
290
+ return MACRO_TYPE_PREFIX_KNOWN_TYPE;
291
+ }
292
+
293
+ static MacroTypePrefix function_macro_macro_type_item(TSLexer *lexer, const MacroTypeState *state, const PragmaTrivia *trivia) {
294
+ MacroGroup group = function_macro_group_prefix(lexer, '(', ')', NULL, NULL, !state->known_type, trivia);
295
+ if (group == MACRO_GROUP_BRACED) return MACRO_TYPE_PREFIX_TYPE;
296
+ if (group == MACRO_GROUP_BALANCED && scan_pragma_spacing_with(lexer, trivia)) return MACRO_TYPE_PREFIX_BALANCED_TYPE;
297
+ return MACRO_TYPE_PREFIX_INVALID;
298
+ }
299
+
289
300
  // code-gauge-allow functionCognitiveComplexity 16 -- Literal prefixes inspect one consumed first argument token without restarting the lexer.
290
301
  static bool function_macro_trailing_argument_prefix(TSLexer *lexer, const PragmaTrivia *trivia) {
291
302
  do {
@@ -604,7 +615,8 @@ static bool function_macro_word_in(const char *word, const char *const *words, u
604
615
  unsigned end = count;
605
616
  while (start < end) {
606
617
  unsigned middle = start + (end - start) / 2;
607
- int order = strcmp(word, words[middle]);
618
+ int order = (unsigned char)word[0] - (unsigned char)words[middle][0];
619
+ if (!order && word[0]) order = strcmp(word + 1, words[middle] + 1);
608
620
  if (order == 0) return true;
609
621
  if (order < 0) end = middle;
610
622
  else start = middle + 1;
package/src/scanner.c CHANGED
@@ -11,6 +11,8 @@ enum TokenType { PRAGMA_OPERATOR, PREPROC_ARG, PREPROC_NEWLINE, PREPROC_LPAREN,
11
11
 
12
12
  static EmptyDirectives *scanner_directives(CScanner *scanner);
13
13
 
14
+ 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);
14
16
  static bool scan_preproc_tokens(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
15
17
  static bool scan_declaration_tokens(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols);
16
18
  static bool scan_directive_context(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
@@ -45,9 +47,11 @@ void tree_sitter_c_external_scanner_deserialize(void *payload, const char *buffe
45
47
  if (state) state->ready = length && buffer[0];
46
48
  }
47
49
 
50
+ // code-gauge-allow functionCognitiveComplexity 16 -- External-token validity dispatches each lexing mode to its scanner.
48
51
  bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
49
52
  CScanner *scanner = payload;
50
53
  EmptyDirectives *state = scanner_directives(scanner);
54
+ if (only_extras_valid(state, valid_symbols)) return scan_extras(state, lexer, valid_symbols);
51
55
  if (scan_directive_context(state, lexer, valid_symbols)) return true;
52
56
  if (empty_directives_at_line_end(state, lexer)) return false;
53
57
  if (valid_symbols[GNU_RAW_STRING_BODY] && !valid_symbols[PREPROC_LPAREN]) {
@@ -74,6 +78,38 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
74
78
  return scan_declaration_tokens(scanner, lexer, valid_symbols);
75
79
  }
76
80
 
81
+ static bool only_extras_valid(const EmptyDirectives *state, const bool *valid_symbols) {
82
+ if (!state || !state->ready) return false;
83
+ for (unsigned i = PREPROC_ARG; i < DIRECTIVE_CONTEXT; i++) {
84
+ if (valid_symbols[i]) return false;
85
+ }
86
+ return !valid_symbols[PREPROC_DIRECTIVE_START];
87
+ }
88
+
89
+ // Decides as the general path does in states where only extras are valid, but queries the position only once the first
90
+ // non-space character shows that a null directive or `_Pragma` may follow, since any position query makes the runtime
91
+ // allocate the token on the heap. A null directive's prefix holds only spaces and splices, and line ends are recorded
92
+ // only at spaces and newlines. The skipped spaces moved the lexer, so the start position is read back through the mark.
93
+ static bool scan_extras(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
94
+ TSLexerCheckpoint start;
95
+ lexer->get_checkpoint(lexer, &start);
96
+ while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
97
+ int32_t c = lexer->lookahead;
98
+ if (c != '#' && c != '\\' && c != '_') return false;
99
+ TSLexerPosition position;
100
+ lexer->mark_end_at(lexer, &start);
101
+ lexer->get_mark_end(lexer, &position);
102
+ if (valid_symbols[PREPROC_EMPTY] && empty_directives_start_at(state, position.bytes)) {
103
+ empty_directives_consume_started(lexer, state, true);
104
+ lexer->mark_end(lexer);
105
+ lexer->result_symbol = PREPROC_EMPTY;
106
+ return true;
107
+ }
108
+ if (!valid_symbols[PRAGMA_OPERATOR] || empty_directives_line_end_at(state, position.bytes)) return false;
109
+ lexer->result_symbol = PRAGMA_OPERATOR;
110
+ return scan_pragma_with(lexer, &(PragmaTrivia){empty_directives_skip, state});
111
+ }
112
+
77
113
  static bool scan_conditional_directive(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
78
114
  if (!empty_directives_at_start(state, lexer)) return false;
79
115
  const ConditionalMacroSymbols symbols = {
Binary file
package/tree-sitter.json CHANGED
@@ -24,7 +24,7 @@
24
24
  }
25
25
  ],
26
26
  "metadata": {
27
- "version": "34.0.1",
27
+ "version": "34.0.2",
28
28
  "license": "MIT",
29
29
  "description": "C grammar for tree-sitter",
30
30
  "authors": [