@willbooster/tree-sitter-c 40.0.5 → 42.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-c",
3
- "version": "40.0.5",
3
+ "version": "42.0.0",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "c",
@@ -31,6 +31,12 @@ typedef struct {
31
31
  DirectiveScanMode mode;
32
32
  GnuRawStringPrefix raw;
33
33
  DirectiveSpans *run;
34
+ // Set when the characters read hold `pragma_word`, also in a comment or a literal: a scan starts inside a literal
35
+ // after an escape sequence, and the lexer ends some comments earlier than this pass does.
36
+ bool *pragma;
37
+ // The characters read last, the latest in the low byte and zero for one that is not ASCII. Code outside words is
38
+ // left out, so a word's first character restarts them.
39
+ uint64_t recent;
34
40
  int32_t quote, previous, pair;
35
41
  uint32_t spaces, run_start, run_end, run_spans;
36
42
  unsigned delimiter_length, closing_length, closing_end;
@@ -38,13 +44,19 @@ typedef struct {
38
44
  bool beginning, number, escape, star, splice, in_run;
39
45
  } DirectiveScan;
40
46
 
41
- static inline bool directive_scan_plain(DirectiveScan *scan, TSLexer *lexer);
42
- static inline bool directive_scan_plain_word(DirectiveScan *scan, TSLexer *lexer, int32_t c);
43
- static inline bool directive_scan_plain_code(DirectiveScan *scan, TSLexer *lexer, int32_t c);
44
- static inline bool directive_scan_plain_text(DirectiveScan *scan, TSLexer *lexer, int32_t c);
47
+ static bool directive_scan_leading_comment(DirectiveScan *scan, TSLexer *lexer);
48
+ static bool directive_scan_feed(DirectiveScan *scan, int32_t c);
49
+ static inline bool directive_scan_plain_mode(DirectiveScan *scan, TSLexer *lexer);
50
+ static inline bool directive_scan_plain_word(DirectiveScan *scan, TSLexer *lexer);
51
+ static inline void directive_scan_word_characters(DirectiveScan *scan, uint64_t recent, uint32_t count);
52
+ static inline bool directive_pragma_word(uint64_t recent);
53
+ static inline uint64_t directive_scan_note(DirectiveScan *scan, uint64_t recent, int32_t c);
54
+ static inline bool directive_scan_plain_code(DirectiveScan *scan, TSLexer *lexer);
55
+ static inline bool directive_scan_plain_block_comment(DirectiveScan *scan, TSLexer *lexer);
56
+ static inline void directive_scan_plain_text(DirectiveScan *scan, TSLexer *lexer, int32_t end, bool raw);
45
57
  static inline void directive_scan_lexer_run(DirectiveScan *scan, TSLexer *lexer, bool member);
46
58
  static inline bool directive_scan_ascii_word(int32_t c);
47
- static inline bool directive_scan_ascii_special(int32_t c);
59
+ static inline bool directive_scan_ascii_plain(int32_t c);
48
60
  static bool directive_scan_pending(DirectiveScan *scan, int32_t c);
49
61
  static bool directive_scan_splice(DirectiveScan *scan, int32_t c);
50
62
  static bool directive_scan_logical(DirectiveScan *scan, int32_t c);
@@ -62,71 +74,137 @@ static void directive_scan_close_run(DirectiveScan *scan, uint32_t limit, bool s
62
74
  static void directive_scan_push(DirectiveScan *scan, uint32_t start, uint32_t end);
63
75
  static inline void directive_scan_extend(DirectiveScan *scan, uint32_t start, bool skipped);
64
76
 
77
+ // Feeds the spaces at the lexer's position, and a `/` after them with the character that follows it. Returns true when
78
+ // they open a comment. It reads no further than that one `/`, so that the scans after a postponed build, which start
79
+ // where this one's tokens end, do not read the same characters again and again.
80
+ static bool directive_scan_leading_comment(DirectiveScan *scan, TSLexer *lexer) {
81
+ for (bool slash = false;;) {
82
+ if (scan->mode == SCAN_LINE_COMMENT || scan->mode == SCAN_BLOCK_COMMENT) return true;
83
+ int32_t c = lexer->lookahead;
84
+ bool space = !slash && scan->mode == SCAN_CODE && !scan->splice && (pragma_space(c) || c == '/');
85
+ if ((!space && scan->mode != SCAN_SLASH) || pragma_eof(lexer)) return false;
86
+ slash = slash || c == '/';
87
+ directive_scan_feed(scan, c);
88
+ directive_scan_lexer_run(scan, lexer, pragma_space(c) || c == '\\');
89
+ lexer->advance(lexer, false);
90
+ }
91
+ }
92
+
65
93
  // Consumes the lexer's ASCII characters that need no physical-layer work and change no state but the line beginning,
66
94
  // the current word, the end of a comment and the run of spaces, which is most of the input, and stops at a character
67
- // that `directive_scan_feed` has to handle.
68
- static void directive_scan_plain_run(DirectiveScan *target, TSLexer *lexer) {
69
- if (target->pair || target->splice) return;
70
- // The lexer's calls cannot reach this copy, so it can stay in registers.
71
- DirectiveScan copy = *target;
72
- while (directive_scan_plain(&copy, lexer)) lexer->advance(lexer, false);
73
- *target = copy;
95
+ // that `directive_scan_feed` has to handle. Each mode has a loop of its own, which keeps what it reads in registers
96
+ // across the lexer's calls.
97
+ static void directive_scan_plain_run(DirectiveScan *scan, TSLexer *lexer) {
98
+ if (scan->splice) return;
99
+ // Only another newline can belong to the newline before it.
100
+ if (scan->pair && directive_newline(lexer->lookahead)) return;
101
+ scan->pair = 0;
102
+ while (directive_scan_plain_mode(scan, lexer)) {}
74
103
  }
75
104
 
76
- static inline bool directive_scan_plain(DirectiveScan *scan, TSLexer *lexer) {
77
- int32_t c = lexer->lookahead;
78
- if (c <= 0 || c >= 0x80) return false;
105
+ // Consumes the plain characters of the current mode. Returns true when those of another mode may follow.
106
+ static inline bool directive_scan_plain_mode(DirectiveScan *scan, TSLexer *lexer) {
79
107
  switch (scan->mode) {
80
108
  case SCAN_WORD:
81
- return directive_scan_plain_word(scan, lexer, c);
109
+ return directive_scan_plain_word(scan, lexer);
82
110
  case SCAN_CODE:
83
- return directive_scan_plain_code(scan, lexer, c);
84
- case SCAN_LINE_COMMENT:
85
- return directive_scan_plain_text(scan, lexer, c);
111
+ return directive_scan_plain_code(scan, lexer);
86
112
  case SCAN_BLOCK_COMMENT:
87
- if (!directive_scan_plain_text(scan, lexer, c)) return false;
88
- if (scan->star && c == '/') scan->mode = SCAN_CODE;
89
- scan->star = c == '*';
90
- return true;
113
+ return directive_scan_plain_block_comment(scan, lexer);
114
+ case SCAN_LINE_COMMENT:
115
+ directive_scan_plain_text(scan, lexer, 0, false);
116
+ return false;
91
117
  case SCAN_QUOTE:
92
- return !scan->escape && c != scan->quote && directive_scan_plain_text(scan, lexer, c);
118
+ if (!scan->escape) directive_scan_plain_text(scan, lexer, scan->quote, false);
119
+ return false;
93
120
  case SCAN_RAW_BODY:
94
- if (c == '"' || !directive_scan_plain_text(scan, lexer, c)) return false;
95
- directive_scan_raw_body(scan, c);
96
- return true;
121
+ directive_scan_plain_text(scan, lexer, '"', true);
122
+ return false;
97
123
  default:
98
124
  return false;
99
125
  }
100
126
  }
101
127
 
102
- static inline bool directive_scan_plain_word(DirectiveScan *scan, TSLexer *lexer, int32_t c) {
103
- if (!directive_scan_ascii_word(c)) {
104
- // A line splice may continue the word, and the others may continue a number or open a raw string.
105
- if (directive_scan_ascii_special(c) || (scan->number && (c == '.' || c == '+' || c == '-'))) return false;
106
- scan->mode = SCAN_CODE;
107
- return directive_scan_plain_code(scan, lexer, c);
128
+ // Consumes the rest of the word. Returns true when code follows it.
129
+ static inline bool directive_scan_plain_word(DirectiveScan *scan, TSLexer *lexer) {
130
+ int32_t c = lexer->lookahead;
131
+ uint64_t recent = scan->recent;
132
+ uint32_t count = 0;
133
+ for (; directive_scan_ascii_word(c); c = lexer->lookahead) {
134
+ recent = recent << 8 | (uint64_t)c;
135
+ count++;
136
+ lexer->advance(lexer, false);
108
137
  }
109
- scan->in_run = false;
110
- scan->previous = c;
111
- if (scan->raw.length < sizeof scan->raw.value) gnu_raw_string_prefix_character(&scan->raw, c);
138
+ if (count) directive_scan_word_characters(scan, recent, count);
139
+ // A line splice may continue the word, and the others may continue a number or open a raw string.
140
+ if (!directive_scan_ascii_plain(c) || (scan->number && (c == '.' || c == '+' || c == '-'))) return false;
141
+ scan->mode = SCAN_CODE;
112
142
  return true;
113
143
  }
114
144
 
115
- static inline bool directive_scan_plain_code(DirectiveScan *scan, TSLexer *lexer, int32_t c) {
116
- if (directive_scan_ascii_special(c)) return false;
117
- bool space = directive_horizontal(c);
118
- directive_scan_lexer_run(scan, lexer, space);
119
- if (space) return true;
120
- scan->beginning = false;
121
- if (directive_scan_ascii_word(c)) directive_scan_word_start(scan, c);
122
- return true;
145
+ // Adds the word's next `count` characters, the last ones of `recent`, as `directive_scan_word` does one at a time: a
146
+ // prefix that would exceed three characters is no raw string prefix whatever they are.
147
+ static inline void directive_scan_word_characters(DirectiveScan *scan, uint64_t recent, uint32_t count) {
148
+ scan->in_run = false;
149
+ scan->previous = (int32_t)(recent & 0xff);
150
+ scan->recent = recent;
151
+ if (directive_pragma_word(recent)) *scan->pragma = true;
152
+ if (scan->raw.length >= sizeof scan->raw.value) return;
153
+ if (count > sizeof scan->raw.value - 1 - scan->raw.length) {
154
+ gnu_raw_string_prefix_invalidate(&scan->raw);
155
+ return;
156
+ }
157
+ while (count--) gnu_raw_string_prefix_character(&scan->raw, (int32_t)(recent >> (8 * count) & 0xff));
123
158
  }
124
159
 
125
- // The body of a comment or a literal.
126
- static inline bool directive_scan_plain_text(DirectiveScan *scan, TSLexer *lexer, int32_t c) {
127
- if (c == '\n' || c == '\r' || c == '\\') return false;
128
- directive_scan_lexer_run(scan, lexer, directive_horizontal(c));
129
- return true;
160
+ // Consumes code up to a word. Returns true when a word follows, whose first character is consumed.
161
+ static inline bool directive_scan_plain_code(DirectiveScan *scan, TSLexer *lexer) {
162
+ for (;;) {
163
+ int32_t c = lexer->lookahead;
164
+ if (!directive_scan_ascii_plain(c)) return false;
165
+ bool space = directive_horizontal(c);
166
+ directive_scan_lexer_run(scan, lexer, space);
167
+ lexer->advance(lexer, false);
168
+ if (space) continue;
169
+ scan->beginning = false;
170
+ if (!directive_scan_ascii_word(c)) continue;
171
+ directive_scan_word_start(scan, c);
172
+ if (scan->mode == SCAN_WORD) return true;
173
+ }
174
+ }
175
+
176
+ // Consumes the comment's body. Returns true when the comment ends.
177
+ static inline bool directive_scan_plain_block_comment(DirectiveScan *scan, TSLexer *lexer) {
178
+ bool star = scan->star, closed = false;
179
+ uint64_t recent = scan->recent;
180
+ while (!closed) {
181
+ int32_t c = lexer->lookahead;
182
+ if (c <= 0 || c >= 0x80 || c == '\n' || c == '\r' || c == '\\') break;
183
+ directive_scan_lexer_run(scan, lexer, directive_horizontal(c));
184
+ recent = directive_scan_note(scan, recent, c);
185
+ lexer->advance(lexer, false);
186
+ closed = star && c == '/';
187
+ star = c == '*';
188
+ }
189
+ scan->recent = recent;
190
+ scan->star = star;
191
+ if (closed) scan->mode = SCAN_CODE;
192
+ return closed;
193
+ }
194
+
195
+ // Consumes the body of a line comment or a literal up to `end`, the closing search of a `raw` string seeing each
196
+ // character.
197
+ static inline void directive_scan_plain_text(DirectiveScan *scan, TSLexer *lexer, int32_t end, bool raw) {
198
+ uint64_t recent = scan->recent;
199
+ for (;;) {
200
+ int32_t c = lexer->lookahead;
201
+ if (c <= 0 || c >= 0x80 || c == '\n' || c == '\r' || c == '\\' || c == end) break;
202
+ directive_scan_lexer_run(scan, lexer, directive_horizontal(c));
203
+ if (raw) directive_scan_raw_body(scan, c);
204
+ recent = directive_scan_note(scan, recent, c);
205
+ lexer->advance(lexer, false);
206
+ }
207
+ scan->recent = recent;
130
208
  }
131
209
 
132
210
  // Extends or ends the run of spaces and backslashes with the lookahead, as `directive_scan_run` does. A span read from
@@ -167,16 +245,16 @@ static void directive_scan_push(DirectiveScan *scan, uint32_t start, uint32_t en
167
245
  }
168
246
 
169
247
  static inline bool directive_scan_ascii_word(int32_t c) {
170
- return c != '\\' && ((preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1);
248
+ return (uint32_t)c < 0x80 && c != '\\' && ((preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1);
171
249
  }
172
250
 
173
- // NUL, newlines, `\`, `/`, quotes and `#`, which code leaves to `directive_scan_feed`.
174
- static inline bool directive_scan_ascii_special(int32_t c) {
251
+ // ASCII but NUL, newlines, `\`, `/`, quotes and `#`, which code leaves to `directive_scan_feed`.
252
+ static inline bool directive_scan_ascii_plain(int32_t c) {
175
253
  static const uint64_t special[] = {
176
254
  1ull | (1ull << '\n') | (1ull << '\r') | (1ull << '"') | (1ull << '#') | (1ull << '\'') | (1ull << '/'),
177
255
  1ull << ('\\' - 64),
178
256
  };
179
- return (special[c / 64] >> (c % 64)) & 1;
257
+ return (uint32_t)c < 0x80 && !((special[c / 64] >> (c % 64)) & 1);
180
258
  }
181
259
 
182
260
  // Extends or ends the run with the stored character `c` from `start` to `end`.
@@ -193,6 +271,7 @@ static void directive_scan_run(DirectiveScan *scan, int32_t c, uint32_t start, u
193
271
  // `directive_encode`) and returns true when `c` starts a directive, which the caller leaves unconsumed.
194
272
  static bool directive_scan_feed(DirectiveScan *scan, int32_t c) {
195
273
  if ((uint32_t)c > 0x10ffff) c = 0xfffd;
274
+ scan->recent = directive_scan_note(scan, scan->recent, c);
196
275
  if (scan->mode == SCAN_RAW_BODY) {
197
276
  directive_scan_raw_body(scan, c);
198
277
  return false;
@@ -311,6 +390,7 @@ static void directive_scan_word_start(DirectiveScan *scan, int32_t c) {
311
390
  scan->mode = SCAN_WORD;
312
391
  scan->number = c >= '0' && c <= '9';
313
392
  scan->previous = c;
393
+ scan->recent = c < 0x80 ? (uint64_t)c : 0;
314
394
  scan->raw = (GnuRawStringPrefix){0};
315
395
  gnu_raw_string_prefix_character(&scan->raw, c);
316
396
  }
@@ -370,6 +450,20 @@ static void directive_scan_raw_body(DirectiveScan *scan, int32_t c) {
370
450
  scan->mode = SCAN_CODE;
371
451
  }
372
452
 
453
+ // Adds `c` to the characters read last and returns them.
454
+ static inline uint64_t directive_scan_note(DirectiveScan *scan, uint64_t recent, int32_t c) {
455
+ recent = recent << 8 | ((uint32_t)c < 0x80 ? (uint64_t)c : 0);
456
+ if (directive_pragma_word(recent)) *scan->pragma = true;
457
+ return recent;
458
+ }
459
+
460
+ // Whether the characters in `recent`, the last one in the low byte, end in `pragma_word`.
461
+ static inline bool directive_pragma_word(uint64_t recent) {
462
+ uint64_t word = 0;
463
+ for (unsigned i = 0; i < sizeof pragma_word - 1; i++) word = word << 8 | (uint8_t)pragma_word[i];
464
+ return (recent & ((1ull << 8 * (sizeof pragma_word - 1)) - 1)) == word;
465
+ }
466
+
373
467
  static bool directive_horizontal(int32_t c) {
374
468
  return c == ' ' || c == '\t' || c == '\v' || c == '\f';
375
469
  }
@@ -23,14 +23,19 @@ typedef struct {
23
23
  // The lexer's byte position, which follows the last stored character.
24
24
  uint32_t fill_byte;
25
25
  uint32_t conditional_depth;
26
- // Whether the current segment recorded a null directive or a line-end barrier, which the reverse pass carries.
27
- bool utf16, encoding_known, failed, carries;
26
+ // The characters stored last, as `DirectiveScan` keeps those it reads.
27
+ uint64_t recent;
28
+ // Whether the current segment recorded a null directive or a line-end barrier, which the reverse pass carries, and
29
+ // whether the characters stored or scanned hold `pragma_word`.
30
+ bool utf16, encoding_known, failed, carries, pragma;
28
31
  } DirectiveBuffer;
29
32
 
30
33
  // A position in a `DirectiveBuffer`; copies share the buffer, so a lookahead copy reads what the original will.
31
34
  typedef struct {
32
35
  DirectiveBuffer *buffer;
33
36
  uint32_t index, byte, position_index, previous_byte, before_previous_byte;
37
+ // Whether a line splice with spaces before its newline was read, which the lexer's tokens do not take as a splice.
38
+ bool spaced_splice;
34
39
  } DirectiveInput;
35
40
 
36
41
  #define DIRECTIVE_FACT_PAGE_SHIFT 15
@@ -41,9 +46,12 @@ typedef struct {
41
46
  // holds no fact; source without facts, such as an excluded range, costs one pointer per page of each directory.
42
47
  // `empty_directives_release` frees them.
43
48
  typedef struct {
44
- uint8_t **starts, **ends, **line_ends, **directive_starts, **directive_ends;
49
+ uint8_t **starts, **ends, **comments, **line_ends, **directive_starts, **directive_ends;
45
50
  uint32_t pages;
46
- bool built, ready;
51
+ // `anchored` and `ready` belong to the scanner state: whether the token at the start of the input is out, and
52
+ // whether the facts, which `built` says exist, are in use. `pragmas` is the fact that the input read for them holds
53
+ // `pragma_word`, without which no scan has to look for a `_Pragma` operator.
54
+ bool built, anchored, ready, pragmas;
47
55
  } EmptyDirectives;
48
56
 
49
57
  typedef enum { DIRECTIVE_ORDINARY, DIRECTIVE_CONDITION, DIRECTIVE_DIAGNOSTIC, DIRECTIVE_REQUIRED, DIRECTIVE_UNDEF, DIRECTIVE_LINE, DIRECTIVE_PRAGMA, DIRECTIVE_HEADER } DirectiveKind;
@@ -58,7 +66,8 @@ typedef struct {
58
66
  bool beginning, pending, operand, pragma_pending, pragma_newline, pragma_name;
59
67
  unsigned pragma_depth, header;
60
68
  DirectiveKind kind;
61
- uint32_t start, required_end;
69
+ // `comment_start` is the first comment after a pending `#`, or 0.
70
+ uint32_t start, comment_start, required_end;
62
71
  } DirectiveLine;
63
72
 
64
73
  static bool directive_scan_buffered(DirectiveScan *scan, DirectiveInput *input);
@@ -70,7 +79,7 @@ static bool directive_segment_ends(const DirectiveInput *input);
70
79
  static bool directive_short_line_comment(const DirectiveInput *input, uint32_t index, uint32_t limit);
71
80
  static bool directive_line_clean(const DirectiveLine *line);
72
81
  static void directive_shift(DirectiveBuffer *buffer, uint32_t start);
73
- static bool directive_finish(EmptyDirectives *state, DirectiveBuffer *buffer);
82
+ static void directive_finish(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveSpans *run, DirectiveSpans *tail);
74
83
  static void directive_prefixes(EmptyDirectives *state, const DirectiveInput *input, const DirectiveSpans *prefix, DirectiveSpans *tail);
75
84
  static bool directive_step_back(const DirectiveInput *input, uint32_t *index, uint32_t *position, uint32_t *byte, int32_t *c, uint32_t *width);
76
85
  static void directive_carry(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveCarry *carry, int32_t c, uint32_t byte);
@@ -82,6 +91,7 @@ static inline bool directive_has(const DirectiveInput *input, uint32_t index);
82
91
  static bool directive_fill(DirectiveBuffer *buffer, uint32_t index);
83
92
  static bool directive_append(DirectiveBuffer *buffer);
84
93
  static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c);
94
+ static inline void directive_note(DirectiveBuffer *buffer, uint32_t c);
85
95
  static bool directive_reserve(DirectiveBuffer *buffer, unsigned width);
86
96
  static bool directive_position(DirectiveBuffer *buffer, uint32_t index, uint32_t byte, uint32_t width);
87
97
  static void directive_encode(DirectiveBuffer *buffer, uint32_t c);
@@ -104,6 +114,7 @@ static void directive_neutral_code(EmptyDirectives *state, DirectiveInput *input
104
114
  static bool directive_header_ready(DirectiveLine *line, int32_t c);
105
115
  static bool directive_header(DirectiveInput *input, int32_t close);
106
116
  static void directive_line_comment(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
117
+ static bool directive_skip_splice(DirectiveInput *input);
107
118
  static uint32_t directive_splice_end(const DirectiveInput *input);
108
119
 
109
120
  static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer);
@@ -141,18 +152,21 @@ static DirectiveKind directive_word_kind(const DirectiveWord *word);
141
152
  static void directive_word_character(DirectiveWord *word, int32_t c);
142
153
  static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool name, bool pragma, bool had_operand, bool identifier);
143
154
  static uint32_t directive_byte(const DirectiveInput *input);
144
- static void directive_record(EmptyDirectives *state, DirectiveInput *input, uint32_t start, uint32_t end);
155
+ static void directive_record(EmptyDirectives *state, DirectiveInput *input, const DirectiveLine *line, uint32_t end);
145
156
 
146
- static bool empty_directives_initialize(EmptyDirectives *state, TSLexer *lexer) {
157
+ // Scans the zero-width token `symbol` that starts the directive context: once at the start of the input, where it
158
+ // anchors the tree, and, when comments lead the input, once more where the facts are built after them. Returns false
159
+ // while a comment is still ahead, having consumed characters.
160
+ static bool empty_directives_initialize(EmptyDirectives *state, TSLexer *lexer, TSSymbol symbol) {
147
161
  TSLexerCheckpoint start;
148
162
  lexer->get_checkpoint(lexer, &start);
149
- if (!state->built) {
150
- state->built = true;
151
- empty_directives_build(state, lexer);
152
- }
153
- state->ready = true;
163
+ lexer->result_symbol = symbol;
164
+ bool anchored = state->anchored;
165
+ state->anchored = true;
166
+ if (!state->built) state->built = empty_directives_build(state, lexer);
167
+ state->ready = state->built;
154
168
  lexer->mark_end_at(lexer, &start);
155
- return true;
169
+ return state->ready || !anchored;
156
170
  }
157
171
 
158
172
  static bool empty_directives_after_keyword(const EmptyDirectives *state, TSLexer *lexer) {
@@ -162,6 +176,25 @@ static bool empty_directives_after_keyword(const EmptyDirectives *state, TSLexer
162
176
  return directive_bit(state, state->directive_ends, position.bytes);
163
177
  }
164
178
 
179
+ // False only when the facts show that no `_Pragma` operator can follow any position.
180
+ static bool empty_directives_pragma_possible(const EmptyDirectives *state) {
181
+ return !state || !state->built || state->pragmas;
182
+ }
183
+
184
+ // Whether none of the `count` tokens, at least eight, is valid but those whose byte in `extras` is 0xff.
185
+ static inline bool empty_directives_only_valid(const bool *valid_symbols, const uint8_t *extras, unsigned count) {
186
+ _Static_assert(sizeof(bool) == 1, "valid_symbols is read as bytes");
187
+ uint64_t others = 0;
188
+ for (unsigned i = 0; i < count; i += 8) {
189
+ unsigned offset = i + 8 > count ? count - 8 : i;
190
+ uint64_t valid, extra;
191
+ memcpy(&valid, valid_symbols + offset, sizeof valid);
192
+ memcpy(&extra, extras + offset, sizeof extra);
193
+ others |= valid & ~extra;
194
+ }
195
+ return !others;
196
+ }
197
+
165
198
  static bool empty_directives_at_start(const EmptyDirectives *state, TSLexer *lexer) {
166
199
  if (!state || !state->directive_starts) return false;
167
200
  TSLexerPosition position;
@@ -202,19 +235,33 @@ static bool empty_directives_start_at(const EmptyDirectives *state, uint32_t byt
202
235
  }
203
236
 
204
237
  // Consumes the rest of a null directive whose start was confirmed at a position the lexer has not left except over
205
- // prefix characters.
238
+ // prefix characters: as a token (`skip_prefix`) up to its first recorded end, which `directive_record` places before
239
+ // the comments or after the line, and otherwise the whole logical line, since some scanner lookahead dispatches on
240
+ // the character after the skip without reading comments.
206
241
  static void empty_directives_consume_started(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix) {
207
242
  TSLexerPosition position;
208
243
  while (lexer->lookahead != '#' && !pragma_eof(lexer)) lexer->advance(lexer, skip_prefix);
209
- do {
210
- lexer->advance(lexer, false);
211
- lexer->get_position(lexer, &position);
212
- } while (!pragma_eof(lexer) && !directive_bit(state, state->ends, position.bytes));
244
+ for (bool whole = !skip_prefix;; whole = false) {
245
+ do {
246
+ lexer->advance(lexer, false);
247
+ lexer->get_position(lexer, &position);
248
+ } while (!pragma_eof(lexer) && !directive_bit(state, state->ends, position.bytes));
249
+ if (!whole || pragma_eof(lexer) || !directive_bit(state, state->comments, position.bytes)) return;
250
+ }
213
251
  }
214
252
 
215
253
  // Ordinary code goes through `DirectiveScan` without being stored, and only directive segments, each from a directive's
216
254
  // `#` to the end of the logical line where no directive state is left, are stored for the line-state pass and the
217
255
  // reverse prefix pass. The facts are stored as `EmptyDirectives` describes.
256
+ //
257
+ // Returns false without building when a comment follows the lexer's position after spaces only, which leaves the build
258
+ // to the scan after the comment's token. That scan starts from the initial state where the lexer ends the comment: the
259
+ // state this pass has there too, unless the lexer ends the comment earlier, as at a backslash that spaces separate
260
+ // from its newline, and then what follows is read as the lexer's tokens take it and not as comment text.
261
+ //
262
+ // A build from the start of an input that begins with a block comment would read to the end of the input in a scan
263
+ // that consumes nothing, which the runtime takes for a scanner stuck in an unterminated comment: it then reads the
264
+ // whole input once more and guards every later scan.
218
265
  static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
219
266
  TSLexerPosition position;
220
267
  lexer->get_position(lexer, &position);
@@ -222,7 +269,11 @@ static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
222
269
  DirectiveInput input = {.buffer = &buffer, .byte = position.bytes};
223
270
  // `run` holds the run before a segment's `#`, and `tail` the segment's trailing run.
224
271
  DirectiveSpans run = {0}, tail = {0};
225
- DirectiveScan scan = {.beginning = true, .run = &run};
272
+ DirectiveScan scan = {.beginning = true, .run = &run, .pragma = &buffer.pragma};
273
+ if (directive_scan_leading_comment(&scan, lexer)) {
274
+ free(run.items);
275
+ return false;
276
+ }
226
277
  for (;;) {
227
278
  if (!directive_scan_buffered(&scan, &input)) {
228
279
  buffer.length = buffer.position_count = 0;
@@ -238,10 +289,8 @@ static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
238
289
  if (!more || buffer.failed) break;
239
290
  directive_scan_resume(&scan, &run, &tail);
240
291
  }
241
- if (run.failed || tail.failed) buffer.failed = true;
242
- free(run.items);
243
- free(tail.items);
244
- return directive_finish(state, &buffer);
292
+ directive_finish(state, &buffer, &run, &tail);
293
+ return true;
245
294
  }
246
295
 
247
296
  // Restarts `scan` on ordinary code, continuing the run with a segment's trailing run, whose spans `tail` holds in byte
@@ -250,7 +299,7 @@ static void directive_scan_resume(DirectiveScan *scan, DirectiveSpans *run, Dire
250
299
  DirectiveSpans before = *run;
251
300
  *run = *tail;
252
301
  *tail = before;
253
- *scan = (DirectiveScan){.beginning = true, .run = run};
302
+ *scan = (DirectiveScan){.beginning = true, .run = run, .pragma = scan->pragma};
254
303
  if (!run->count) return;
255
304
  DirectiveSpan last = run->items[--run->count];
256
305
  scan->run_spans = run->count;
@@ -316,7 +365,7 @@ static bool directive_segment(EmptyDirectives *state, DirectiveInput *input) {
316
365
  if (directive_line_clean(&line) && directive_segment_ends(input)) return true;
317
366
  }
318
367
  directive_complete_required(state, input, &line);
319
- if (line.pending) directive_record(state, input, line.start, input->buffer->fill_byte);
368
+ if (line.pending) directive_record(state, input, &line, input->buffer->fill_byte);
320
369
  return false;
321
370
  }
322
371
 
@@ -370,21 +419,24 @@ static void directive_shift(DirectiveBuffer *buffer, uint32_t start) {
370
419
  buffer->position_count = count;
371
420
  }
372
421
 
373
- static bool directive_finish(EmptyDirectives *state, DirectiveBuffer *buffer) {
422
+ // Without all the facts, none is kept, and a `_Pragma` operator may be anywhere.
423
+ static void directive_finish(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveSpans *run, DirectiveSpans *tail) {
424
+ bool failed = buffer->failed || run->failed || tail->failed;
425
+ free(run->items);
426
+ free(tail->items);
374
427
  directive_release(buffer);
375
- if (!buffer->failed) return true;
376
- empty_directives_release(state);
377
- return false;
428
+ state->pragmas = buffer->pragma || failed;
429
+ if (failed) empty_directives_release(state);
378
430
  }
379
431
 
380
432
  static void empty_directives_release(EmptyDirectives *state) {
381
- uint8_t **directories[] = {state->starts, state->ends, state->line_ends, state->directive_starts, state->directive_ends};
433
+ uint8_t **directories[] = {state->starts, state->ends, state->comments, state->line_ends, state->directive_starts, state->directive_ends};
382
434
  for (unsigned i = 0; i < sizeof directories / sizeof *directories; i++) {
383
435
  if (!directories[i]) continue;
384
436
  for (uint32_t page = 0; page < state->pages; page++) free(directories[i][page]);
385
437
  free(directories[i]);
386
438
  }
387
- state->starts = state->ends = state->line_ends = state->directive_starts = state->directive_ends = NULL;
439
+ state->starts = state->ends = state->comments = state->line_ends = state->directive_starts = state->directive_ends = NULL;
388
440
  state->pages = 0;
389
441
  }
390
442
 
@@ -514,6 +566,7 @@ static bool directive_append(DirectiveBuffer *buffer) {
514
566
  unsigned width = directive_utf8_width(c), utf16_width = c > 0xffff && c <= 0x10ffff ? 4 : 2;
515
567
  if (!directive_reserve(buffer, c > 0xffff && c <= 0x10ffff ? 2 : 1)) return false;
516
568
  directive_encode(buffer, c);
569
+ directive_note(buffer, c);
517
570
  TSLexerPosition end;
518
571
  lexer->advance(lexer, false);
519
572
  lexer->get_position(lexer, &end);
@@ -551,6 +604,7 @@ static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c) {
551
604
  uint32_t index = buffer->length, start = buffer->fill_byte;
552
605
  buffer->characters[index >> DIRECTIVE_PAGE_SHIFT][index & (DIRECTIVE_PAGE_SIZE - 1)] = c;
553
606
  buffer->length = index + 1;
607
+ directive_note(buffer, c);
554
608
  TSLexerPosition end;
555
609
  lexer->advance(lexer, false);
556
610
  lexer->get_position(lexer, &end);
@@ -558,6 +612,11 @@ static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c) {
558
612
  return end.bytes - start == (buffer->utf16 ? 2u : 1u) || directive_position(buffer, index, start, end.bytes - start);
559
613
  }
560
614
 
615
+ static inline void directive_note(DirectiveBuffer *buffer, uint32_t c) {
616
+ buffer->recent = buffer->recent << 8 | (c < 0x80 ? c : 0);
617
+ if (directive_pragma_word(buffer->recent)) buffer->pragma = true;
618
+ }
619
+
561
620
  static bool directive_reserve(DirectiveBuffer *buffer, unsigned width) {
562
621
  if (buffer->length > UINT32_MAX - width) return false;
563
622
  uint32_t last = (buffer->length + width - 1) >> DIRECTIVE_PAGE_SHIFT;
@@ -636,7 +695,7 @@ static bool directive_bit(const EmptyDirectives *state, uint8_t *const *facts, u
636
695
  }
637
696
 
638
697
  static bool directive_grow_directories(EmptyDirectives *state, uint32_t pages) {
639
- uint8_t ***directories[] = {&state->starts, &state->ends, &state->line_ends, &state->directive_starts, &state->directive_ends};
698
+ uint8_t ***directories[] = {&state->starts, &state->ends, &state->comments, &state->line_ends, &state->directive_starts, &state->directive_ends};
640
699
  if ((size_t)pages > SIZE_MAX / sizeof(uint8_t *)) return false;
641
700
  for (unsigned i = 0; i < sizeof directories / sizeof *directories; i++) {
642
701
  if (!*directories[i]) continue;
@@ -655,6 +714,10 @@ static void directive_line_character(EmptyDirectives *state, DirectiveInput *inp
655
714
  return;
656
715
  }
657
716
  if (c == '/' && (directive_peek(input) == '/' || directive_peek(input) == '*')) {
717
+ if (line->pending && !line->comment_start) {
718
+ line->comment_start = input->previous_byte;
719
+ input->spaced_splice = false;
720
+ }
658
721
  directive_line_comment(state, input, line);
659
722
  return;
660
723
  }
@@ -665,6 +728,7 @@ static void directive_line_character(EmptyDirectives *state, DirectiveInput *inp
665
728
  line->kind = DIRECTIVE_ORDINARY;
666
729
  line->operand = false;
667
730
  line->start = input->previous_byte;
731
+ line->comment_start = 0;
668
732
  directive_set(state, input->buffer, &state->directive_starts, line->start);
669
733
  line->beginning = false;
670
734
  line->pending = true;
@@ -698,10 +762,12 @@ static void directive_code(EmptyDirectives *state, DirectiveInput *input, Direct
698
762
  directive_word_role(line, kind, name, pragma, operand, preproc_word(c, false));
699
763
  }
700
764
 
701
- // Outside a directive name, a line of no directive kind with no `_Pragma` or header operand in progress keeps that
702
- // state through every token, so its words need no classification.
765
+ // Outside a directive name, a line with no `_Pragma` or header operand in progress keeps its state through every token
766
+ // when it is of no directive kind, or of a kind that only waits for an operand it already has: its words need no
767
+ // classification.
703
768
  static bool directive_neutral(const DirectiveLine *line) {
704
- return !line->pending && line->kind == DIRECTIVE_ORDINARY && !line->pragma_depth && !line->pragma_pending && !line->header;
769
+ bool settled = line->kind == DIRECTIVE_ORDINARY || (line->operand && directive_requires_operand(line->kind));
770
+ return settled && !line->pending && !line->pragma_depth && !line->pragma_pending && !line->header;
705
771
  }
706
772
 
707
773
  // Also consumes the following characters that leave the line state as it is, which is every plain character but `/`.
@@ -787,7 +853,7 @@ static void directive_line_comment(EmptyDirectives *state, DirectiveInput *input
787
853
 
788
854
  static void directive_line_end(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line) {
789
855
  directive_complete_required(state, input, line);
790
- if (line->pending) directive_record(state, input, line->start, directive_byte(input));
856
+ if (line->pending) directive_record(state, input, line, directive_byte(input));
791
857
  line->beginning = true;
792
858
  line->pending = false;
793
859
  bool required = line->kind == DIRECTIVE_CONDITION ||
@@ -834,11 +900,7 @@ static int32_t directive_next(DirectiveInput *input) {
834
900
  }
835
901
  while (directive_has(input, input->index)) {
836
902
  int32_t c = directive_physical_next(input);
837
- uint32_t after = c == '\\' ? directive_splice_end(input) : input->index;
838
- if (after != input->index) {
839
- while (input->index < after) directive_physical_next(input);
840
- continue;
841
- }
903
+ if (c == '\\' && directive_skip_splice(input)) continue;
842
904
  if (directive_newline(c)) {
843
905
  if (directive_has(input, input->index) && directive_newline(directive_character(input, input->index)) &&
844
906
  directive_character(input, input->index) != c) directive_physical_next(input);
@@ -879,6 +941,15 @@ static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t i
879
941
  return input->buffer->utf16 ? 2 : directive_utf8_width(unit);
880
942
  }
881
943
 
944
+ // Consumes the rest of a line splice after its `\\`; false when none follows.
945
+ static bool directive_skip_splice(DirectiveInput *input) {
946
+ uint32_t after = directive_splice_end(input);
947
+ if (after == input->index) return false;
948
+ if (directive_horizontal(directive_character(input, input->index))) input->spaced_splice = true;
949
+ while (input->index < after) directive_physical_next(input);
950
+ return true;
951
+ }
952
+
882
953
  static uint32_t directive_splice_end(const DirectiveInput *input) {
883
954
  uint32_t after = input->index;
884
955
  while (directive_has(input, after) && directive_horizontal(directive_character(input, after))) after++;
@@ -1131,10 +1202,15 @@ static uint32_t directive_byte(const DirectiveInput *input) {
1131
1202
  return input->byte;
1132
1203
  }
1133
1204
 
1134
- static void directive_record(EmptyDirectives *state, DirectiveInput *input, uint32_t start, uint32_t end) {
1205
+ // A null directive with comments has two ends: before its first comment, marked in `comments` too, and after its line.
1206
+ // It keeps its comments when a spaced splice follows their start, where the lexer would read them differently.
1207
+ static void directive_record(EmptyDirectives *state, DirectiveInput *input, const DirectiveLine *line, uint32_t end) {
1135
1208
  input->buffer->carries = true;
1136
- directive_set(state, input->buffer, &state->starts, start);
1209
+ directive_set(state, input->buffer, &state->starts, line->start);
1137
1210
  directive_set(state, input->buffer, &state->ends, end);
1211
+ if (!line->comment_start || input->spaced_splice) return;
1212
+ directive_set(state, input->buffer, &state->ends, line->comment_start);
1213
+ directive_set(state, input->buffer, &state->comments, line->comment_start);
1138
1214
  }
1139
1215
 
1140
1216
  #endif
package/src/scanner.c CHANGED
@@ -7,14 +7,12 @@ 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 };
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 };
11
11
 
12
12
  static EmptyDirectives *scanner_directives(CScanner *scanner);
13
13
 
14
14
  static bool only_extras_valid(const EmptyDirectives *state, const bool *valid_symbols);
15
15
  static bool scan_extras(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
16
- static bool scan_extras_pragma(EmptyDirectives *state, TSLexer *lexer, const TSLexerCheckpoint *start, bool skipped);
17
- static uint32_t extras_start_byte(TSLexer *lexer, const TSLexerCheckpoint *start, bool skipped);
18
16
  static bool scan_preproc_tokens(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
19
17
  static bool scan_declaration_tokens(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols);
20
18
  static bool scan_directive_context(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
@@ -33,16 +31,20 @@ void tree_sitter_c_external_scanner_destroy(void *payload) {
33
31
  free(scanner);
34
32
  }
35
33
 
34
+ enum { SERIALIZED_READY = 1, SERIALIZED_ANCHORED = 2 };
35
+
36
36
  unsigned tree_sitter_c_external_scanner_serialize(void *payload, char *buffer) {
37
37
  const EmptyDirectives *state = scanner_directives(payload);
38
- buffer[0] = state && state->ready;
38
+ buffer[0] = state ? (state->ready ? SERIALIZED_READY : 0) | (state->anchored ? SERIALIZED_ANCHORED : 0) : 0;
39
39
  return 1;
40
40
  }
41
41
 
42
42
  void tree_sitter_c_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {
43
43
  CScanner *scanner = payload;
44
44
  EmptyDirectives *state = scanner_directives(scanner);
45
- if (state) state->ready = length && buffer[0];
45
+ if (!state) return;
46
+ state->ready = length && (buffer[0] & SERIALIZED_READY);
47
+ state->anchored = length && (buffer[0] & SERIALIZED_ANCHORED);
46
48
  }
47
49
 
48
50
  // code-gauge-allow functionCognitiveComplexity -- External-token validity dispatches each lexing mode to its scanner.
@@ -50,6 +52,7 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
50
52
  CScanner *scanner = payload;
51
53
  EmptyDirectives *state = scanner_directives(scanner);
52
54
  if (only_extras_valid(state, valid_symbols)) return scan_extras(state, lexer, valid_symbols);
55
+ if (state && !state->ready && valid_symbols[DIRECTIVE_CONTEXT]) return empty_directives_initialize(state, lexer, DIRECTIVE_CONTEXT);
53
56
  if (scan_directive_context(state, lexer, valid_symbols)) return true;
54
57
  if (empty_directives_at_line_end(state, lexer)) return false;
55
58
  if (valid_symbols[GNU_RAW_STRING_BODY] && !valid_symbols[PREPROC_LPAREN]) {
@@ -68,8 +71,8 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
68
71
  valid_symbols[PREPROC_ARG] || valid_symbols[PREPROC_DIRECTIVE_ARG]) {
69
72
  return scan_preproc_tokens(state, lexer, valid_symbols);
70
73
  }
71
- bool leading = valid_symbols[LEADING_CONDITIONAL_MARKER_IFDEF] || valid_symbols[LEADING_CONDITIONAL_MARKER_IFNDEF];
72
- bool conditional = leading || valid_symbols[CONDITIONAL_MARKER_IFDEF] || valid_symbols[CONDITIONAL_MARKER_IFNDEF];
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];
73
76
  if (!conditional) return scan_declaration_tokens(scanner, lexer, valid_symbols);
74
77
  while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
75
78
  if (lexer->lookahead == '#') return scan_conditional_directive(state, lexer, valid_symbols);
@@ -77,51 +80,25 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
77
80
  }
78
81
 
79
82
  static bool only_extras_valid(const EmptyDirectives *state, const bool *valid_symbols) {
80
- return state && state->ready && !valid_symbols[PREPROC_DIRECTIVE_START] &&
81
- !memchr(valid_symbols + PREPROC_ARG, true, (DIRECTIVE_CONTEXT - PREPROC_ARG) * sizeof(*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);
82
85
  }
83
86
 
84
- // `starts` marks only `#`, `\`, and spaces/splices before a null directive's `#`, and `line_ends` marks only spaces and
85
- // newlines, so `extras_start_byte` restores the pre-space checkpoint via `mark_end_at` only after `#`/`\` (when `starts`
86
- // exists) or `_Pragma` (when skipped spaces may start at `line_ends`), avoiding checkpoint/column work on other tokens.
87
+ // Fails without consuming a character unless a null directive starts here or the input holds `pragma_word`, since
88
+ // the lexer rereads from the start what a failed scan consumed.
87
89
  static bool scan_extras(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
88
- bool allow_empty = valid_symbols[PREPROC_EMPTY] && state->starts;
89
- bool allow_pragma = valid_symbols[PRAGMA_OPERATOR];
90
- if (!allow_empty && !allow_pragma) return false;
91
- bool skipped = pragma_space(lexer->lookahead);
92
- TSLexerCheckpoint start = {0};
93
- if (skipped && (allow_empty || state->line_ends)) lexer->get_checkpoint(lexer, &start);
90
+ if (valid_symbols[PREPROC_EMPTY] && empty_directives_consume(lexer, state, true)) {
91
+ lexer->mark_end(lexer);
92
+ lexer->result_symbol = PREPROC_EMPTY;
93
+ return true;
94
+ }
95
+ if (!valid_symbols[PRAGMA_OPERATOR] || !state->pragmas || empty_directives_at_line_end(state, lexer)) return false;
94
96
  while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
95
- int32_t c = lexer->lookahead;
96
- if (c == '_') return allow_pragma && scan_extras_pragma(state, lexer, &start, skipped);
97
- if (!allow_empty || (c != '#' && c != '\\')) return false;
98
- if (!empty_directives_start_at(state, extras_start_byte(lexer, &start, skipped))) return false;
99
- empty_directives_consume_started(lexer, state, true);
100
- lexer->mark_end(lexer);
101
- lexer->result_symbol = PREPROC_EMPTY;
102
- return true;
103
- }
104
-
105
- static bool scan_extras_pragma(EmptyDirectives *state, TSLexer *lexer, const TSLexerCheckpoint *start, bool skipped) {
106
97
  if (!scan_pragma_word(lexer)) return false;
107
- if (skipped && state->line_ends && empty_directives_line_end_at(state, extras_start_byte(lexer, start, true))) {
108
- return false;
109
- }
110
98
  lexer->result_symbol = PRAGMA_OPERATOR;
111
99
  return scan_pragma_suffix_end_with(lexer, true, &(PragmaTrivia){empty_directives_skip, state});
112
100
  }
113
101
 
114
- static uint32_t extras_start_byte(TSLexer *lexer, const TSLexerCheckpoint *start, bool skipped) {
115
- TSLexerPosition position;
116
- if (skipped) {
117
- lexer->mark_end_at(lexer, start);
118
- lexer->get_mark_end(lexer, &position);
119
- } else {
120
- lexer->get_position(lexer, &position);
121
- }
122
- return position.bytes;
123
- }
124
-
125
102
  static bool scan_conditional_directive(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
126
103
  if (!empty_directives_at_start(state, lexer)) return false;
127
104
  const ConditionalMacroSymbols symbols = {
@@ -133,10 +110,6 @@ static bool scan_conditional_directive(EmptyDirectives *state, TSLexer *lexer, c
133
110
  }
134
111
 
135
112
  static bool scan_directive_context(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
136
- if (state && !state->ready && valid_symbols[DIRECTIVE_CONTEXT]) {
137
- lexer->result_symbol = DIRECTIVE_CONTEXT;
138
- return empty_directives_initialize(state, lexer);
139
- }
140
113
  if (valid_symbols[PREPROC_DIRECTIVE_START] && empty_directives_after_keyword(state, lexer)) {
141
114
  lexer->result_symbol = PREPROC_DIRECTIVE_START;
142
115
  lexer->mark_end(lexer);
@@ -227,7 +200,8 @@ static bool scan_preproc_tokens(EmptyDirectives *state, TSLexer *lexer, const bo
227
200
  return scan_preproc_arg(lexer, false, argument_mode);
228
201
  }
229
202
  lexer->result_symbol = PRAGMA_OPERATOR;
230
- return valid_symbols[PRAGMA_OPERATOR] && scan_pragma_with(lexer, &(PragmaTrivia){empty_directives_skip, state});
203
+ return valid_symbols[PRAGMA_OPERATOR] && empty_directives_pragma_possible(state) &&
204
+ scan_pragma_with(lexer, &(PragmaTrivia){empty_directives_skip, state});
231
205
  }
232
206
 
233
207
  static EmptyDirectives *scanner_directives(CScanner *scanner) {
Binary file
package/tree-sitter.json CHANGED
@@ -25,7 +25,7 @@
25
25
  }
26
26
  ],
27
27
  "metadata": {
28
- "version": "40.0.5",
28
+ "version": "42.0.0",
29
29
  "license": "MIT",
30
30
  "description": "C grammar for tree-sitter",
31
31
  "authors": [