@willbooster/tree-sitter-c 40.0.3 → 40.0.5

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,35 +1,59 @@
1
- // code-gauge-allow-file fileNcss 735 -- One source-fact pass shares compact character storage, directive lexing and physical-position conversion.
1
+ // code-gauge-allow-file fileNcss -- One source-fact pass shares on-demand directive-segment storage, directive lexing, physical-position conversion, prefix spans and fact bitsets.
2
2
  #ifndef TREE_SITTER_C_EMPTY_DIRECTIVES_H_
3
3
  #define TREE_SITTER_C_EMPTY_DIRECTIVES_H_
4
4
 
5
- #include "pragma.h"
5
+ #include "directiveScan.h"
6
6
  #include <stdlib.h>
7
7
 
8
8
  #define DIRECTIVE_PAGE_SHIFT 12
9
9
  #define DIRECTIVE_PAGE_SIZE (1 << DIRECTIVE_PAGE_SHIFT)
10
+ #define DIRECTIVE_SEGMENT_UNITS (DIRECTIVE_PAGE_SIZE * 16)
11
+ #define DIRECTIVE_FILL_UNITS 256
10
12
 
11
13
  typedef struct {
12
14
  uint32_t index, byte, width;
13
15
  } DirectivePosition;
14
16
 
17
+ // The characters of one directive segment, read from the lexer as the line-state pass needs them.
15
18
  typedef struct {
16
19
  uint16_t **characters;
17
20
  DirectivePosition *positions;
18
- uint32_t position_count, position_capacity, position_index, previous_byte, before_previous_byte;
19
- uint8_t *prefix_barriers;
20
- uint32_t length, index, byte, end, page_count;
21
+ TSLexer *lexer;
22
+ uint32_t length, page_count, page_capacity, position_count, position_capacity;
23
+ // The lexer's byte position, which follows the last stored character.
24
+ uint32_t fill_byte;
21
25
  uint32_t conditional_depth;
22
- bool utf16, encoding_known, has_hash, has_directives, has_directive_ends, has_empty, has_barriers;
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;
28
+ } DirectiveBuffer;
29
+
30
+ // A position in a `DirectiveBuffer`; copies share the buffer, so a lookahead copy reads what the original will.
31
+ typedef struct {
32
+ DirectiveBuffer *buffer;
33
+ uint32_t index, byte, position_index, previous_byte, before_previous_byte;
23
34
  } DirectiveInput;
24
35
 
36
+ #define DIRECTIVE_FACT_PAGE_SHIFT 15
37
+ #define DIRECTIVE_FACT_PAGE_SIZE (1 << DIRECTIVE_FACT_PAGE_SHIFT)
38
+
39
+ // Each kind of fact is a directory of `pages` pointers, one per `DIRECTIVE_FACT_PAGE_SIZE` source bytes up to and
40
+ // possibly past the furthest fact, to bitset pages allocated only where a fact is set, so a missing directory or page
41
+ // holds no fact; source without facts, such as an excluded range, costs one pointer per page of each directory.
42
+ // `empty_directives_release` frees them.
25
43
  typedef struct {
26
- uint8_t *starts, *ends, *line_ends, *directive_starts, *directive_ends;
27
- uint32_t bytes;
44
+ uint8_t **starts, **ends, **line_ends, **directive_starts, **directive_ends;
45
+ uint32_t pages;
28
46
  bool built, ready;
29
47
  } EmptyDirectives;
30
48
 
31
49
  typedef enum { DIRECTIVE_ORDINARY, DIRECTIVE_CONDITION, DIRECTIVE_DIAGNOSTIC, DIRECTIVE_REQUIRED, DIRECTIVE_UNDEF, DIRECTIVE_LINE, DIRECTIVE_PRAGMA, DIRECTIVE_HEADER } DirectiveKind;
32
50
 
51
+ // The reverse pass's state; the trailing run's open span covers the bytes from `tail_start` to before `tail_end`.
52
+ typedef struct {
53
+ bool prefix, line_end, tail, tail_open;
54
+ uint32_t tail_start, tail_end;
55
+ } DirectiveCarry;
56
+
33
57
  typedef struct {
34
58
  bool beginning, pending, operand, pragma_pending, pragma_newline, pragma_name;
35
59
  unsigned pragma_depth, header;
@@ -37,17 +61,38 @@ typedef struct {
37
61
  uint32_t start, required_end;
38
62
  } DirectiveLine;
39
63
 
40
- static void directive_prefixes(EmptyDirectives *state, DirectiveInput *input);
41
- static uint32_t directive_skip_inert(const DirectiveInput *input, uint32_t end, uint32_t position, uint32_t *byte);
42
- static bool directive_append(DirectiveInput *input, uint32_t *capacity, uint32_t *byte, TSLexer *lexer);
43
- static bool directive_reserve(DirectiveInput *input, uint32_t *capacity, unsigned width);
44
- static bool directive_position(DirectiveInput *input, uint32_t index, uint32_t byte, uint32_t width);
45
- static void directive_encode(DirectiveInput *input, uint32_t c);
64
+ static bool directive_scan_buffered(DirectiveScan *scan, DirectiveInput *input);
65
+ static bool directive_scan_lexer(DirectiveScan *scan, DirectiveBuffer *buffer);
66
+ static void directive_scan_resume(DirectiveScan *scan, DirectiveSpans *run, DirectiveSpans *tail);
67
+ static void directive_scan_handoff(DirectiveScan *scan, uint32_t hash, bool skipped);
68
+ static bool directive_segment(EmptyDirectives *state, DirectiveInput *input);
69
+ static bool directive_segment_ends(const DirectiveInput *input);
70
+ static bool directive_short_line_comment(const DirectiveInput *input, uint32_t index, uint32_t limit);
71
+ static bool directive_line_clean(const DirectiveLine *line);
72
+ static void directive_shift(DirectiveBuffer *buffer, uint32_t start);
73
+ static bool directive_finish(EmptyDirectives *state, DirectiveBuffer *buffer);
74
+ static void directive_prefixes(EmptyDirectives *state, const DirectiveInput *input, const DirectiveSpans *prefix, DirectiveSpans *tail);
75
+ static bool directive_step_back(const DirectiveInput *input, uint32_t *index, uint32_t *position, uint32_t *byte, int32_t *c, uint32_t *width);
76
+ static void directive_carry(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveCarry *carry, int32_t c, uint32_t byte);
77
+ static void directive_carry_tail(DirectiveCarry *carry, DirectiveSpans *tail, uint32_t byte, uint32_t width);
78
+ static void directive_spans_reverse(DirectiveSpans *spans);
79
+ static void directive_mark_spans(EmptyDirectives *state, DirectiveBuffer *buffer, const DirectiveSpans *spans);
80
+ static uint32_t directive_skip_inert(const EmptyDirectives *state, const DirectiveInput *input, uint32_t end, uint32_t position, uint32_t *byte);
81
+ static inline bool directive_has(const DirectiveInput *input, uint32_t index);
82
+ static bool directive_fill(DirectiveBuffer *buffer, uint32_t index);
83
+ static bool directive_append(DirectiveBuffer *buffer);
84
+ static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c);
85
+ static bool directive_reserve(DirectiveBuffer *buffer, unsigned width);
86
+ static bool directive_position(DirectiveBuffer *buffer, uint32_t index, uint32_t byte, uint32_t width);
87
+ static void directive_encode(DirectiveBuffer *buffer, uint32_t c);
46
88
  static uint16_t directive_character(const DirectiveInput *input, uint32_t index);
47
- static void directive_store(DirectiveInput *input, uint16_t c);
48
- static void directive_release(DirectiveInput *input);
89
+ static void directive_store(DirectiveBuffer *buffer, uint16_t c);
90
+ static void directive_release(DirectiveBuffer *buffer);
49
91
  static unsigned directive_utf8_width(uint32_t c);
50
- static void directive_barrier(DirectiveInput *input);
92
+ static void directive_set(EmptyDirectives *state, DirectiveBuffer *buffer, uint8_t ***facts, uint32_t byte);
93
+ static bool directive_bit(const EmptyDirectives *state, uint8_t *const *facts, uint32_t byte);
94
+ static bool directive_grow_directories(EmptyDirectives *state, uint32_t pages);
95
+ static void directive_barrier(EmptyDirectives *state, DirectiveInput *input);
51
96
  static void directive_line_end(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
52
97
  static void directive_complete_required(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
53
98
  static bool directive_requires_operand(DirectiveKind kind);
@@ -62,11 +107,10 @@ static void directive_line_comment(EmptyDirectives *state, DirectiveInput *input
62
107
  static uint32_t directive_splice_end(const DirectiveInput *input);
63
108
 
64
109
  static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer);
65
- static void empty_directives_prune(EmptyDirectives *state, DirectiveInput *input);
110
+ static void empty_directives_release(EmptyDirectives *state);
66
111
  static bool empty_directives_line_end_at(const EmptyDirectives *state, uint32_t byte);
67
112
  static bool empty_directives_start_at(const EmptyDirectives *state, uint32_t byte);
68
113
  static void empty_directives_consume_started(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix);
69
- static void empty_directives_find(EmptyDirectives *state, DirectiveInput *input);
70
114
  static int32_t directive_next(DirectiveInput *input);
71
115
  static int32_t directive_physical_next(DirectiveInput *input);
72
116
  static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t index, int32_t *c);
@@ -75,6 +119,7 @@ static int32_t directive_plain(const DirectiveInput *input);
75
119
  static void directive_skip_plain(DirectiveInput *input);
76
120
  typedef enum { DIRECTIVE_RUN_WORD, DIRECTIVE_RUN_CODE, DIRECTIVE_RUN_SPACE, DIRECTIVE_RUN_LINE_COMMENT, DIRECTIVE_RUN_BLOCK_COMMENT } DirectiveRun;
77
121
  static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun run);
122
+ static uint32_t directive_run_end(const DirectiveBuffer *buffer, DirectiveRun run, uint32_t index, uint32_t end);
78
123
  static bool directive_run_continues(DirectiveRun run, uint16_t c);
79
124
  static void directive_pragma_name(DirectiveLine *line, const DirectiveInput *input);
80
125
  static bool directive_pragma_operator(const DirectiveInput *input);
@@ -95,8 +140,6 @@ static void directive_word_run(DirectiveInput *input, DirectiveWord *word, bool
95
140
  static DirectiveKind directive_word_kind(const DirectiveWord *word);
96
141
  static void directive_word_character(DirectiveWord *word, int32_t c);
97
142
  static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool name, bool pragma, bool had_operand, bool identifier);
98
- static bool directive_horizontal(int32_t c);
99
- static bool directive_newline(int32_t c);
100
143
  static uint32_t directive_byte(const DirectiveInput *input);
101
144
  static void directive_record(EmptyDirectives *state, DirectiveInput *input, uint32_t start, uint32_t end);
102
145
 
@@ -116,14 +159,14 @@ static bool empty_directives_after_keyword(const EmptyDirectives *state, TSLexer
116
159
  if (!state || !state->directive_ends) return false;
117
160
  TSLexerPosition position;
118
161
  lexer->get_position(lexer, &position);
119
- return position.bytes <= state->bytes && (state->directive_ends[position.bytes / 8] & (1u << (position.bytes % 8)));
162
+ return directive_bit(state, state->directive_ends, position.bytes);
120
163
  }
121
164
 
122
165
  static bool empty_directives_at_start(const EmptyDirectives *state, TSLexer *lexer) {
123
166
  if (!state || !state->directive_starts) return false;
124
167
  TSLexerPosition position;
125
168
  lexer->get_position(lexer, &position);
126
- return position.bytes < state->bytes && (state->directive_starts[position.bytes / 8] & (1u << (position.bytes % 8)));
169
+ return directive_bit(state, state->directive_starts, position.bytes);
127
170
  }
128
171
 
129
172
  static bool empty_directives_at_line_end(const EmptyDirectives *state, TSLexer *lexer) {
@@ -134,7 +177,7 @@ static bool empty_directives_at_line_end(const EmptyDirectives *state, TSLexer *
134
177
  }
135
178
 
136
179
  static bool empty_directives_line_end_at(const EmptyDirectives *state, uint32_t byte) {
137
- return state->line_ends && byte < state->bytes && (state->line_ends[byte / 8] & (1u << (byte % 8)));
180
+ return directive_bit(state, state->line_ends, byte);
138
181
  }
139
182
 
140
183
  static bool empty_directives_consume(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix);
@@ -155,7 +198,7 @@ static bool empty_directives_consume(TSLexer *lexer, EmptyDirectives *state, boo
155
198
  }
156
199
 
157
200
  static bool empty_directives_start_at(const EmptyDirectives *state, uint32_t byte) {
158
- return state->starts && byte < state->bytes && (state->starts[byte / 8] & (1u << (byte % 8)));
201
+ return directive_bit(state, state->starts, byte);
159
202
  }
160
203
 
161
204
  // Consumes the rest of a null directive whose start was confirmed at a position the lexer has not left except over
@@ -166,202 +209,444 @@ static void empty_directives_consume_started(TSLexer *lexer, EmptyDirectives *st
166
209
  do {
167
210
  lexer->advance(lexer, false);
168
211
  lexer->get_position(lexer, &position);
169
- } while (!pragma_eof(lexer) && position.bytes < state->bytes &&
170
- !(state->ends[position.bytes / 8] & (1u << (position.bytes % 8))));
212
+ } while (!pragma_eof(lexer) && !directive_bit(state, state->ends, position.bytes));
171
213
  }
172
214
 
173
- // code-gauge-allow functionNcss 35 -- The source-fact buffers are allocated and released together so initialization fails atomically.
215
+ // Ordinary code goes through `DirectiveScan` without being stored, and only directive segments, each from a directive's
216
+ // `#` to the end of the logical line where no directive state is left, are stored for the line-state pass and the
217
+ // reverse prefix pass. The facts are stored as `EmptyDirectives` describes.
174
218
  static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
175
- TSLexerPosition start;
176
- lexer->get_position(lexer, &start);
177
- DirectiveInput input = {.byte = start.bytes};
178
- uint32_t capacity = 0, byte = start.bytes;
179
- while (!pragma_eof(lexer)) {
180
- if (!directive_append(&input, &capacity, &byte, lexer)) goto failed;
181
- }
182
- input.end = byte;
183
- state->bytes = byte;
184
- if (!input.has_hash) {
185
- directive_release(&input);
186
- return true;
219
+ TSLexerPosition position;
220
+ lexer->get_position(lexer, &position);
221
+ DirectiveBuffer buffer = {.lexer = lexer, .fill_byte = position.bytes};
222
+ DirectiveInput input = {.buffer = &buffer, .byte = position.bytes};
223
+ // `run` holds the run before a segment's `#`, and `tail` the segment's trailing run.
224
+ DirectiveSpans run = {0}, tail = {0};
225
+ DirectiveScan scan = {.beginning = true, .run = &run};
226
+ for (;;) {
227
+ if (!directive_scan_buffered(&scan, &input)) {
228
+ buffer.length = buffer.position_count = 0;
229
+ if (!directive_scan_lexer(&scan, &buffer)) break;
230
+ input = (DirectiveInput){.buffer = &buffer, .byte = buffer.fill_byte};
231
+ }
232
+ directive_shift(&buffer, input.index);
233
+ buffer.carries = false;
234
+ uint32_t start = input.byte;
235
+ input = (DirectiveInput){.buffer = &buffer, .byte = start, .previous_byte = start, .before_previous_byte = start};
236
+ bool more = directive_segment(state, &input);
237
+ directive_prefixes(state, &input, &run, &tail);
238
+ if (!more || buffer.failed) break;
239
+ directive_scan_resume(&scan, &run, &tail);
240
+ }
241
+ if (run.failed || tail.failed) buffer.failed = true;
242
+ free(run.items);
243
+ free(tail.items);
244
+ return directive_finish(state, &buffer);
245
+ }
246
+
247
+ // Restarts `scan` on ordinary code, continuing the run with a segment's trailing run, whose spans `tail` holds in byte
248
+ // order; the lists swap so that `run` holds them.
249
+ static void directive_scan_resume(DirectiveScan *scan, DirectiveSpans *run, DirectiveSpans *tail) {
250
+ DirectiveSpans before = *run;
251
+ *run = *tail;
252
+ *tail = before;
253
+ *scan = (DirectiveScan){.beginning = true, .run = run};
254
+ if (!run->count) return;
255
+ DirectiveSpan last = run->items[--run->count];
256
+ scan->run_spans = run->count;
257
+ scan->in_run = true;
258
+ scan->run_start = last.start;
259
+ scan->run_end = last.end;
260
+ }
261
+
262
+ // Completes the run's spans before the directive's `#` at `hash`, which follows bytes left out when `skipped`.
263
+ static void directive_scan_handoff(DirectiveScan *scan, uint32_t hash, bool skipped) {
264
+ if (scan->in_run) directive_scan_close_run(scan, hash, skipped);
265
+ scan->run->count = scan->in_run ? scan->run_spans : 0;
266
+ }
267
+
268
+ // Feeds the characters a directive segment's lookahead stored past its end. Returns true at a directive's `#`, which
269
+ // is left at `input`.
270
+ static bool directive_scan_buffered(DirectiveScan *scan, DirectiveInput *input) {
271
+ while (input->index < input->buffer->length) {
272
+ DirectiveInput before = *input;
273
+ int32_t c = directive_physical_next(input);
274
+ if (directive_scan_feed(scan, c)) {
275
+ *input = before;
276
+ directive_scan_handoff(scan, before.byte, before.byte != scan->run_end);
277
+ return true;
278
+ }
279
+ directive_scan_run(scan, c, input->previous_byte, input->byte);
187
280
  }
188
- state->starts = calloc((size_t)state->bytes / 8 + 1, 1);
189
- state->ends = calloc((size_t)state->bytes / 8 + 1, 1);
190
- state->directive_starts = calloc((size_t)state->bytes / 8 + 1, 1);
191
- state->directive_ends = calloc((size_t)state->bytes / 8 + 1, 1);
192
- input.prefix_barriers = calloc((size_t)state->bytes / 8 + 1, 1);
193
- if (!state->starts || !state->ends || !state->directive_starts || !state->directive_ends || !input.prefix_barriers) goto failed;
194
- empty_directives_find(state, &input);
195
- if (input.has_empty || input.has_barriers) directive_prefixes(state, &input);
196
- empty_directives_prune(state, &input);
197
- directive_release(&input);
198
- return true;
199
- failed:
200
- free(input.prefix_barriers);
201
- directive_release(&input);
202
- free(state->starts);
203
- free(state->ends);
204
- free(state->directive_starts);
205
- free(state->directive_ends);
206
- state->starts = state->ends = state->directive_starts = state->directive_ends = NULL;
207
281
  return false;
208
282
  }
209
283
 
210
- static void empty_directives_prune(EmptyDirectives *state, DirectiveInput *input) {
211
- if (!input->has_empty) {
212
- free(state->starts);
213
- free(state->ends);
214
- state->starts = state->ends = NULL;
284
+ // Feeds the lexer's characters until a directive's `#`, which is left as the lookahead, or the end of the input.
285
+ static bool directive_scan_lexer(DirectiveScan *scan, DirectiveBuffer *buffer) {
286
+ TSLexer *lexer = buffer->lexer;
287
+ TSLexerPosition position;
288
+ for (;;) {
289
+ directive_scan_plain_run(scan, lexer);
290
+ if (pragma_eof(lexer)) break;
291
+ int32_t c = lexer->lookahead;
292
+ if (directive_scan_feed(scan, c)) {
293
+ lexer->get_position(lexer, &position);
294
+ buffer->fill_byte = position.bytes;
295
+ directive_scan_handoff(scan, position.bytes, lexer->is_at_included_range_start(lexer));
296
+ return true;
297
+ }
298
+ directive_scan_lexer_run(scan, lexer, pragma_space(c) || c == '\\');
299
+ lexer->advance(lexer, false);
215
300
  }
216
- if (!input->has_directives) {
217
- free(state->directive_starts);
218
- state->directive_starts = NULL;
301
+ lexer->get_position(lexer, &position);
302
+ buffer->fill_byte = position.bytes;
303
+ return false;
304
+ }
305
+
306
+ // Runs the line-state pass from a directive's `#` until a logical line ends with no directive state left and
307
+ // `directive_segment_ends` lets `DirectiveScan` take over again. Returns false at the end of the input.
308
+ static bool directive_segment(EmptyDirectives *state, DirectiveInput *input) {
309
+ DirectiveLine line = {.beginning = true};
310
+ for (;;) {
311
+ // Horizontal spaces never change the line state.
312
+ directive_plain_run(input, DIRECTIVE_RUN_SPACE);
313
+ int32_t c = directive_next(input);
314
+ if (c < 0) break;
315
+ directive_line_character(state, input, &line, c);
316
+ if (directive_line_clean(&line) && directive_segment_ends(input)) return true;
317
+ }
318
+ directive_complete_required(state, input, &line);
319
+ if (line.pending) directive_record(state, input, line.start, input->buffer->fill_byte);
320
+ return false;
321
+ }
322
+
323
+ // A segment goes on over a following line that, after its indentation, starts another directive, is blank, or is a
324
+ // line comment that ends within `DIRECTIVE_FILL_UNITS` characters, because handing each short line over costs more than
325
+ // the line-state pass on it, until the segment holds `DIRECTIVE_SEGMENT_UNITS` characters. Code or another comment,
326
+ // which may run on for any length, goes back to `DirectiveScan`.
327
+ static bool directive_segment_ends(const DirectiveInput *input) {
328
+ if (input->index >= DIRECTIVE_SEGMENT_UNITS) return true;
329
+ uint32_t index = input->index, limit = input->index + DIRECTIVE_FILL_UNITS;
330
+ while (index < limit && directive_has(input, index) && directive_horizontal(directive_character(input, index))) index++;
331
+ if (index >= limit || !directive_has(input, index)) return true;
332
+ uint16_t c = directive_character(input, index);
333
+ return c != '#' && !directive_newline(c) && !directive_short_line_comment(input, index, limit);
334
+ }
335
+
336
+ // A line splice would continue the comment, so the line holds no `\`.
337
+ static bool directive_short_line_comment(const DirectiveInput *input, uint32_t index, uint32_t limit) {
338
+ if (directive_character(input, index) != '/' || !directive_has(input, index + 1) ||
339
+ directive_character(input, index + 1) != '/') return false;
340
+ for (index += 2; index < limit && directive_has(input, index); index++) {
341
+ uint16_t c = directive_character(input, index);
342
+ if (c == '\\') return false;
343
+ if (directive_newline(c)) return true;
344
+ }
345
+ return false;
346
+ }
347
+
348
+ static bool directive_line_clean(const DirectiveLine *line) {
349
+ return line->beginning && !line->pending && !line->operand && !line->pragma_pending && !line->pragma_newline &&
350
+ !line->pragma_name && !line->pragma_depth && !line->header && line->kind == DIRECTIVE_ORDINARY && !line->required_end;
351
+ }
352
+
353
+ // Drops the characters before `start`, which `DirectiveScan` has consumed.
354
+ static void directive_shift(DirectiveBuffer *buffer, uint32_t start) {
355
+ if (!start) return;
356
+ uint32_t length = buffer->length - start;
357
+ for (uint32_t i = 0; i < length; i++) {
358
+ uint32_t from = start + i;
359
+ buffer->characters[i >> DIRECTIVE_PAGE_SHIFT][i & (DIRECTIVE_PAGE_SIZE - 1)] =
360
+ buffer->characters[from >> DIRECTIVE_PAGE_SHIFT][from & (DIRECTIVE_PAGE_SIZE - 1)];
219
361
  }
220
- if (!input->has_directive_ends) {
221
- free(state->directive_ends);
222
- state->directive_ends = NULL;
362
+ buffer->length = length;
363
+ uint32_t count = 0;
364
+ for (uint32_t i = 0; i < buffer->position_count; i++) {
365
+ DirectivePosition position = buffer->positions[i];
366
+ if (position.index < start) continue;
367
+ position.index -= start;
368
+ buffer->positions[count++] = position;
223
369
  }
224
- if (input->has_barriers) state->line_ends = input->prefix_barriers;
225
- else free(input->prefix_barriers);
370
+ buffer->position_count = count;
371
+ }
372
+
373
+ static bool directive_finish(EmptyDirectives *state, DirectiveBuffer *buffer) {
374
+ directive_release(buffer);
375
+ if (!buffer->failed) return true;
376
+ empty_directives_release(state);
377
+ return false;
226
378
  }
227
379
 
228
- // code-gauge-allow functionCognitiveComplexity 29 -- The reverse lexical pass applies sparse physical-position corrections while carrying null-prefix and line-end facts.
229
- static void directive_prefixes(EmptyDirectives *state, DirectiveInput *input) {
230
- bool prefix = false, line_end = false;
231
- uint32_t byte = input->end, position = input->position_count;
232
- for (uint32_t i = input->length; i > 0;) {
233
- if (!prefix && !line_end) i = directive_skip_inert(input, i, position, &byte);
380
+ static void empty_directives_release(EmptyDirectives *state) {
381
+ uint8_t **directories[] = {state->starts, state->ends, state->line_ends, state->directive_starts, state->directive_ends};
382
+ for (unsigned i = 0; i < sizeof directories / sizeof *directories; i++) {
383
+ if (!directories[i]) continue;
384
+ for (uint32_t page = 0; page < state->pages; page++) free(directories[i][page]);
385
+ free(directories[i]);
386
+ }
387
+ state->starts = state->ends = state->line_ends = state->directive_starts = state->directive_ends = NULL;
388
+ state->pages = 0;
389
+ }
390
+
391
+ // Marks the prefixes of the segment's null directives and the spaces before its directive line ends, reading the
392
+ // segment backwards from `input`, where its forward pass stopped. A prefix reaching the segment's `#` extends over the
393
+ // run before the segment, whose spans `prefix` holds. Stores in `tail`, in byte order, the spans of the segment's
394
+ // trailing run of spaces and backslashes after its last line end, over which a later null directive's prefix may extend.
395
+ static void directive_prefixes(EmptyDirectives *state, const DirectiveInput *input, const DirectiveSpans *prefix, DirectiveSpans *tail) {
396
+ DirectiveCarry carry = {.tail = true};
397
+ uint32_t byte = input->byte, position = input->position_index, width;
398
+ bool complete = true;
399
+ tail->count = 0;
400
+ for (uint32_t i = input->index; i > 0;) {
401
+ if (!carry.prefix && !carry.line_end && !carry.tail) i = directive_skip_inert(state, input, i, position, &byte);
234
402
  if (!i) break;
235
- i--;
236
- if (i && directive_character(input, i) >= 0xdc00 && directive_character(input, i) <= 0xdfff &&
237
- directive_character(input, i - 1) >= 0xd800 && directive_character(input, i - 1) <= 0xdbff) continue;
238
403
  int32_t c;
239
- uint32_t width = directive_expected_width(input, i, &c);
240
- if (position && input->positions[position - 1].index == i) byte = input->positions[--position].byte;
241
- else {
242
- if (width > byte) return;
243
- byte -= width;
244
- }
245
- if (byte > state->bytes) return;
246
- bool hash = c == '#' && (state->starts[byte / 8] & (1u << (byte % 8)));
247
- bool condition_end = input->prefix_barriers[byte / 8] & (1u << (byte % 8));
248
- line_end = condition_end || (line_end && directive_horizontal(c));
249
- if (line_end) input->prefix_barriers[byte / 8] |= 1u << (byte % 8);
250
- prefix = !condition_end && (hash || (prefix && (pragma_space(c) || c == '\\')));
251
- if (prefix) state->starts[byte / 8] |= 1u << (byte % 8);
404
+ complete = directive_step_back(input, &i, &position, &byte, &c, &width);
405
+ if (!complete) break;
406
+ directive_carry(state, input->buffer, &carry, c, byte);
407
+ if (carry.tail) directive_carry_tail(&carry, tail, byte, width);
408
+ else if (!input->buffer->carries) break;
409
+ }
410
+ if (carry.tail_open) directive_spans_push(tail, carry.tail_start, carry.tail_end);
411
+ directive_spans_reverse(tail);
412
+ if (complete && carry.prefix) directive_mark_spans(state, input->buffer, prefix);
413
+ }
414
+
415
+ static void directive_spans_reverse(DirectiveSpans *spans) {
416
+ for (uint32_t i = 0; i < spans->count / 2; i++) {
417
+ DirectiveSpan span = spans->items[i];
418
+ spans->items[i] = spans->items[spans->count - 1 - i];
419
+ spans->items[spans->count - 1 - i] = span;
252
420
  }
253
421
  }
254
422
 
423
+ static void directive_mark_spans(EmptyDirectives *state, DirectiveBuffer *buffer, const DirectiveSpans *spans) {
424
+ for (uint32_t i = 0; i < spans->count; i++) {
425
+ for (uint32_t b = spans->items[i].start; b < spans->items[i].end; b++) directive_set(state, buffer, &state->starts, b);
426
+ }
427
+ }
428
+
429
+ // Moves back to the character before `*index`, reading a surrogate pair as one character, and decodes it into `*c` and
430
+ // its byte width into `*width`; false when its width exceeds the bytes left.
431
+ static bool directive_step_back(const DirectiveInput *input, uint32_t *index, uint32_t *position, uint32_t *byte, int32_t *c, uint32_t *width) {
432
+ const DirectiveBuffer *buffer = input->buffer;
433
+ uint32_t i = --*index;
434
+ if (i && directive_character(input, i) >= 0xdc00 && directive_character(input, i) <= 0xdfff &&
435
+ directive_character(input, i - 1) >= 0xd800 && directive_character(input, i - 1) <= 0xdbff) i = --*index;
436
+ *width = directive_expected_width(input, i, c);
437
+ if (*position && buffer->positions[*position - 1].index == i) {
438
+ DirectivePosition recorded = buffer->positions[--*position];
439
+ *byte = recorded.byte;
440
+ *width = recorded.width;
441
+ return true;
442
+ }
443
+ if (*width > *byte) return false;
444
+ *byte -= *width;
445
+ return true;
446
+ }
447
+
448
+ // Carries null-directive prefixes back from their `#` and line ends back from their barriers over the character `c` at
449
+ // `byte`, and whether the trailing run continues.
450
+ static void directive_carry(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveCarry *carry, int32_t c, uint32_t byte) {
451
+ bool hash = c == '#' && directive_bit(state, state->starts, byte);
452
+ bool condition_end = directive_bit(state, state->line_ends, byte);
453
+ bool space = pragma_space(c) || c == '\\';
454
+ carry->line_end = condition_end || (carry->line_end && directive_horizontal(c));
455
+ if (carry->line_end) directive_set(state, buffer, &state->line_ends, byte);
456
+ carry->prefix = !condition_end && (hash || (carry->prefix && space));
457
+ if (carry->prefix) directive_set(state, buffer, &state->starts, byte);
458
+ carry->tail = carry->tail && !condition_end && space;
459
+ }
460
+
461
+ // Extends the trailing run back over the character at `byte`, starting a span where an included range left bytes out.
462
+ static void directive_carry_tail(DirectiveCarry *carry, DirectiveSpans *tail, uint32_t byte, uint32_t width) {
463
+ if (width > DIRECTIVE_CHARACTER_BYTES) width = DIRECTIVE_CHARACTER_BYTES;
464
+ if (carry->tail_open && byte + width == carry->tail_start) {
465
+ carry->tail_start = byte;
466
+ return;
467
+ }
468
+ if (carry->tail_open) directive_spans_push(tail, carry->tail_start, carry->tail_end);
469
+ carry->tail_start = byte;
470
+ carry->tail_end = byte + width;
471
+ carry->tail_open = true;
472
+ }
473
+
255
474
  // Steps back from the character before `end` over characters that cannot change the reverse pass's state while no
256
475
  // prefix or line end is carried: plain-width characters that are neither `#` nor barriers. Returns the new end.
257
- static uint32_t directive_skip_inert(const DirectiveInput *input, uint32_t end, uint32_t position, uint32_t *byte) {
258
- uint32_t stop = position ? input->positions[position - 1].index + 1 : 0, ascii_width = input->utf16 ? 2 : 1;
476
+ static uint32_t directive_skip_inert(const EmptyDirectives *state, const DirectiveInput *input, uint32_t end, uint32_t position, uint32_t *byte) {
477
+ const DirectiveBuffer *buffer = input->buffer;
478
+ uint32_t stop = position ? buffer->positions[position - 1].index + 1 : 0, ascii_width = buffer->utf16 ? 2 : 1;
259
479
  while (end > stop) {
260
480
  uint32_t c = directive_character(input, end - 1);
261
- uint32_t width = c < 0x80 ? ascii_width : input->utf16 && (c < 0xd800 || c > 0xdfff) ? 2 : 0;
481
+ uint32_t width = c < 0x80 ? ascii_width : buffer->utf16 && (c < 0xd800 || c > 0xdfff) ? 2 : 0;
262
482
  if (!width || c == '#' || width > *byte) break;
263
483
  uint32_t previous = *byte - width;
264
- if (input->prefix_barriers[previous / 8] & (1u << (previous % 8))) break;
484
+ if (directive_bit(state, state->line_ends, previous)) break;
265
485
  *byte = previous;
266
486
  end--;
267
487
  }
268
488
  return end;
269
489
  }
270
490
 
271
- // `*byte` is the lexer's position before the character and is advanced past it.
272
- static bool directive_append(DirectiveInput *input, uint32_t *capacity, uint32_t *byte, TSLexer *lexer) {
273
- uint32_t c = lexer->lookahead, index = input->length, start = *byte;
274
- if (c == '#') input->has_hash = true;
491
+ // Reads characters from the lexer until `index` is stored; false at the end of the input.
492
+ static inline bool directive_has(const DirectiveInput *input, uint32_t index) {
493
+ return index < input->buffer->length || directive_fill(input->buffer, index);
494
+ }
495
+
496
+ // Reads ahead to the end of the line or `DIRECTIVE_FILL_UNITS` characters past `index`, since the pass reads each
497
+ // stored character several times and the characters it leaves behind go back to `DirectiveScan`.
498
+ static bool directive_fill(DirectiveBuffer *buffer, uint32_t index) {
499
+ uint32_t limit = index + DIRECTIVE_FILL_UNITS;
500
+ while (buffer->length < limit && !buffer->failed && !pragma_eof(buffer->lexer)) {
501
+ int32_t c = buffer->lexer->lookahead;
502
+ if (!directive_append(buffer)) buffer->failed = true;
503
+ else if (directive_newline(c) && buffer->length > index) break;
504
+ }
505
+ return index < buffer->length;
506
+ }
507
+
508
+ static bool directive_append(DirectiveBuffer *buffer) {
509
+ TSLexer *lexer = buffer->lexer;
510
+ uint32_t c = lexer->lookahead, index = buffer->length, start = buffer->fill_byte;
511
+ if (c < 0x80 && buffer->encoding_known && (index >> DIRECTIVE_PAGE_SHIFT) < buffer->page_count) {
512
+ return directive_append_ascii(buffer, (uint16_t)c);
513
+ }
275
514
  unsigned width = directive_utf8_width(c), utf16_width = c > 0xffff && c <= 0x10ffff ? 4 : 2;
276
- if (!directive_reserve(input, capacity, c > 0xffff && c <= 0x10ffff ? 2 : 1)) return false;
277
- directive_encode(input, c);
515
+ if (!directive_reserve(buffer, c > 0xffff && c <= 0x10ffff ? 2 : 1)) return false;
516
+ directive_encode(buffer, c);
278
517
  TSLexerPosition end;
279
518
  lexer->advance(lexer, false);
280
519
  lexer->get_position(lexer, &end);
281
- *byte = end.bytes;
520
+ buffer->fill_byte = end.bytes;
282
521
  uint32_t measured = end.bytes - start;
283
- if (!input->encoding_known && c <= 0x10ffff && width != utf16_width && !lexer->is_at_included_range_start(lexer)) {
284
- input->utf16 = measured == utf16_width;
285
- input->encoding_known = true;
522
+ // A character stored before the encoding is known keeps its measured width, which a later choice cannot change.
523
+ bool known = buffer->encoding_known;
524
+ if (!known && c <= 0x10ffff && width != utf16_width && !lexer->is_at_included_range_start(lexer)) {
525
+ buffer->utf16 = measured == utf16_width;
526
+ buffer->encoding_known = true;
286
527
  }
287
- if (c > 0x10ffff || measured != (input->utf16 ? utf16_width : width)) {
288
- return directive_position(input, index, start, measured);
528
+ if (!known || c > 0x10ffff || measured != (buffer->utf16 ? utf16_width : width)) {
529
+ return directive_position(buffer, index, start, measured);
289
530
  }
290
531
  return true;
291
532
  }
292
533
 
293
- static bool directive_position(DirectiveInput *input, uint32_t index, uint32_t byte, uint32_t width) {
294
- if (input->position_count == input->position_capacity) {
295
- uint32_t capacity = input->position_capacity ? input->position_capacity * 2 : 16;
296
- if (capacity < input->position_capacity || (size_t)capacity > SIZE_MAX / sizeof(*input->positions)) return false;
297
- void *positions = realloc(input->positions, (size_t)capacity * sizeof(*input->positions));
534
+ static bool directive_position(DirectiveBuffer *buffer, uint32_t index, uint32_t byte, uint32_t width) {
535
+ if (buffer->position_count == buffer->position_capacity) {
536
+ uint32_t capacity = buffer->position_capacity ? buffer->position_capacity * 2 : 16;
537
+ if (capacity < buffer->position_capacity || (size_t)capacity > SIZE_MAX / sizeof(*buffer->positions)) return false;
538
+ void *positions = realloc(buffer->positions, (size_t)capacity * sizeof(*buffer->positions));
298
539
  if (!positions) return false;
299
- input->positions = positions;
300
- input->position_capacity = capacity;
540
+ buffer->positions = positions;
541
+ buffer->position_capacity = capacity;
301
542
  }
302
- input->positions[input->position_count++] = (DirectivePosition){index, byte, width};
543
+ buffer->positions[buffer->position_count++] = (DirectivePosition){index, byte, width};
303
544
  return true;
304
545
  }
305
546
 
306
- static bool directive_reserve(DirectiveInput *input, uint32_t *capacity, unsigned width) {
307
- if (input->length > UINT32_MAX - width) return false;
308
- uint32_t last = (input->length + width - 1) >> DIRECTIVE_PAGE_SHIFT;
309
- if (last >= *capacity) {
310
- uint32_t next = *capacity ? *capacity * 2 : 16;
311
- if (next < *capacity || (size_t)next > SIZE_MAX / sizeof(*input->characters)) return false;
312
- void *characters = realloc(input->characters, (size_t)next * sizeof(*input->characters));
547
+ // An ASCII character on a stored page, once the encoding is known, takes the expected width unless the lexer skipped to
548
+ // another included range after it.
549
+ static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c) {
550
+ TSLexer *lexer = buffer->lexer;
551
+ uint32_t index = buffer->length, start = buffer->fill_byte;
552
+ buffer->characters[index >> DIRECTIVE_PAGE_SHIFT][index & (DIRECTIVE_PAGE_SIZE - 1)] = c;
553
+ buffer->length = index + 1;
554
+ TSLexerPosition end;
555
+ lexer->advance(lexer, false);
556
+ lexer->get_position(lexer, &end);
557
+ buffer->fill_byte = end.bytes;
558
+ return end.bytes - start == (buffer->utf16 ? 2u : 1u) || directive_position(buffer, index, start, end.bytes - start);
559
+ }
560
+
561
+ static bool directive_reserve(DirectiveBuffer *buffer, unsigned width) {
562
+ if (buffer->length > UINT32_MAX - width) return false;
563
+ uint32_t last = (buffer->length + width - 1) >> DIRECTIVE_PAGE_SHIFT;
564
+ if (last >= buffer->page_capacity) {
565
+ uint32_t next = buffer->page_capacity ? buffer->page_capacity * 2 : 16;
566
+ if (next < buffer->page_capacity || (size_t)next > SIZE_MAX / sizeof(*buffer->characters)) return false;
567
+ void *characters = realloc(buffer->characters, (size_t)next * sizeof(*buffer->characters));
313
568
  if (!characters) return false;
314
- input->characters = characters;
315
- *capacity = next;
569
+ buffer->characters = characters;
570
+ buffer->page_capacity = next;
316
571
  }
317
- while (input->page_count <= last) {
572
+ while (buffer->page_count <= last) {
318
573
  uint16_t *page = malloc((size_t)DIRECTIVE_PAGE_SIZE * sizeof(*page));
319
574
  if (!page) return false;
320
- input->characters[input->page_count++] = page;
575
+ buffer->characters[buffer->page_count++] = page;
321
576
  }
322
577
  return true;
323
578
  }
324
579
 
325
- static void directive_encode(DirectiveInput *input, uint32_t c) {
326
- if (c > 0x10ffff) directive_store(input, 0xdfff);
327
- else if (c <= 0xffff) directive_store(input, c);
580
+ // Only the two units of a character above U+FFFF form a surrogate pair. A decoding error is stored as U+FFFD with the
581
+ // recorded position `directive_append` gives it, and a lone surrogate as U+E000, which `preproc_word` and `pragma_space`
582
+ // classify alike, so that neither joins a neighbouring lone surrogate into a character the lexer did not read.
583
+ static void directive_encode(DirectiveBuffer *buffer, uint32_t c) {
584
+ if (c > 0x10ffff) directive_store(buffer, 0xfffd);
585
+ else if (c >= 0xd800 && c <= 0xdfff) directive_store(buffer, 0xe000);
586
+ else if (c <= 0xffff) directive_store(buffer, c);
328
587
  else {
329
588
  c -= 0x10000;
330
- directive_store(input, 0xd800 | (c >> 10));
331
- directive_store(input, 0xdc00 | (c & 0x3ff));
589
+ directive_store(buffer, 0xd800 | (c >> 10));
590
+ directive_store(buffer, 0xdc00 | (c & 0x3ff));
332
591
  }
333
592
  }
334
593
 
335
594
  static uint16_t directive_character(const DirectiveInput *input, uint32_t index) {
336
- return input->characters[index >> DIRECTIVE_PAGE_SHIFT][index & (DIRECTIVE_PAGE_SIZE - 1)];
595
+ return input->buffer->characters[index >> DIRECTIVE_PAGE_SHIFT][index & (DIRECTIVE_PAGE_SIZE - 1)];
337
596
  }
338
597
 
339
- static void directive_store(DirectiveInput *input, uint16_t c) {
340
- input->characters[input->length >> DIRECTIVE_PAGE_SHIFT][input->length & (DIRECTIVE_PAGE_SIZE - 1)] = c;
341
- input->length++;
598
+ static void directive_store(DirectiveBuffer *buffer, uint16_t c) {
599
+ buffer->characters[buffer->length >> DIRECTIVE_PAGE_SHIFT][buffer->length & (DIRECTIVE_PAGE_SIZE - 1)] = c;
600
+ buffer->length++;
342
601
  }
343
602
 
344
- static void directive_release(DirectiveInput *input) {
345
- for (uint32_t i = 0; i < input->page_count; i++) free(input->characters[i]);
346
- free(input->characters);
347
- free(input->positions);
603
+ static void directive_release(DirectiveBuffer *buffer) {
604
+ for (uint32_t i = 0; i < buffer->page_count; i++) free(buffer->characters[i]);
605
+ free(buffer->characters);
606
+ free(buffer->positions);
348
607
  }
349
608
 
350
609
  static unsigned directive_utf8_width(uint32_t c) {
351
610
  return c < 0x80 || c > 0x10ffff ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
352
611
  }
353
612
 
354
- static void empty_directives_find(EmptyDirectives *state, DirectiveInput *input) {
355
- DirectiveLine line = {.beginning = true};
356
- while (input->index < input->length) {
357
- // Horizontal spaces never change the line state.
358
- directive_plain_run(input, DIRECTIVE_RUN_SPACE);
359
- int32_t c = directive_next(input);
360
- if (c < 0) break;
361
- directive_line_character(state, input, &line, c);
613
+ static void directive_set(EmptyDirectives *state, DirectiveBuffer *buffer, uint8_t ***facts, uint32_t byte) {
614
+ if (buffer->failed) return;
615
+ uint32_t page = byte >> DIRECTIVE_FACT_PAGE_SHIFT;
616
+ if (page >= state->pages && !directive_grow_directories(state, state->pages * 2 > page ? state->pages * 2 : page + 1)) {
617
+ buffer->failed = true;
618
+ return;
362
619
  }
363
- directive_complete_required(state, input, &line);
364
- if (line.pending) directive_record(state, input, line.start, input->end);
620
+ if (!*facts && !(*facts = calloc(state->pages, sizeof **facts))) {
621
+ buffer->failed = true;
622
+ return;
623
+ }
624
+ uint8_t **bits = &(*facts)[page];
625
+ if (!*bits && !(*bits = calloc(DIRECTIVE_FACT_PAGE_SIZE / 8, 1))) {
626
+ buffer->failed = true;
627
+ return;
628
+ }
629
+ uint32_t offset = byte & (DIRECTIVE_FACT_PAGE_SIZE - 1);
630
+ (*bits)[offset / 8] |= 1u << (offset % 8);
631
+ }
632
+
633
+ static bool directive_bit(const EmptyDirectives *state, uint8_t *const *facts, uint32_t byte) {
634
+ uint32_t page = byte >> DIRECTIVE_FACT_PAGE_SHIFT, offset = byte & (DIRECTIVE_FACT_PAGE_SIZE - 1);
635
+ return facts && page < state->pages && facts[page] && (facts[page][offset / 8] & (1u << (offset % 8)));
636
+ }
637
+
638
+ 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};
640
+ if ((size_t)pages > SIZE_MAX / sizeof(uint8_t *)) return false;
641
+ for (unsigned i = 0; i < sizeof directories / sizeof *directories; i++) {
642
+ if (!*directories[i]) continue;
643
+ uint8_t **directory = realloc(*directories[i], (size_t)pages * sizeof *directory);
644
+ if (!directory) return false;
645
+ memset(directory + state->pages, 0, (size_t)(pages - state->pages) * sizeof *directory);
646
+ *directories[i] = directory;
647
+ }
648
+ state->pages = pages;
649
+ return true;
365
650
  }
366
651
 
367
652
  static void directive_line_character(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c) {
@@ -380,8 +665,7 @@ static void directive_line_character(EmptyDirectives *state, DirectiveInput *inp
380
665
  line->kind = DIRECTIVE_ORDINARY;
381
666
  line->operand = false;
382
667
  line->start = input->previous_byte;
383
- state->directive_starts[line->start / 8] |= 1u << (line->start % 8);
384
- input->has_directives = true;
668
+ directive_set(state, input->buffer, &state->directive_starts, line->start);
385
669
  line->beginning = false;
386
670
  line->pending = true;
387
671
  return;
@@ -390,7 +674,7 @@ static void directive_line_character(EmptyDirectives *state, DirectiveInput *inp
390
674
  else directive_code(state, input, line, c);
391
675
  }
392
676
 
393
- // code-gauge-allow functionCognitiveComplexity 19 -- Header names, quoted tokens and distinct keyword boundaries update one logical directive's lexical state.
677
+ // code-gauge-allow functionCognitiveComplexity -- Header names, quoted tokens and distinct keyword boundaries update one logical directive's lexical state.
394
678
  static void directive_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c) {
395
679
  bool name = line->pending;
396
680
  bool header = directive_header_ready(line, c);
@@ -468,7 +752,7 @@ static bool directive_header_ready(DirectiveLine *line, int32_t c) {
468
752
  }
469
753
 
470
754
  static bool directive_header(DirectiveInput *input, int32_t close) {
471
- while (input->index < input->length) {
755
+ while (directive_has(input, input->index)) {
472
756
  int32_t c = directive_next(input);
473
757
  if (c == close) return false;
474
758
  if (c == '\n') return true;
@@ -508,7 +792,7 @@ static void directive_line_end(EmptyDirectives *state, DirectiveInput *input, Di
508
792
  line->pending = false;
509
793
  bool required = line->kind == DIRECTIVE_CONDITION ||
510
794
  (directive_requires_operand(line->kind) && !line->operand && !line->pragma_pending);
511
- if (required) directive_barrier(input);
795
+ if (required) directive_barrier(state, input);
512
796
  line->header = 0;
513
797
  if (line->pragma_pending) line->pragma_newline = true;
514
798
  if (!line->pragma_depth && !line->pragma_pending) {
@@ -522,9 +806,7 @@ static void directive_complete_required(EmptyDirectives *state, DirectiveInput *
522
806
  line->pragma_name = false;
523
807
  if (!line->required_end) return;
524
808
  if (line->operand || line->pragma_pending || pragma_name) {
525
- uint32_t end = line->required_end;
526
- state->directive_ends[end / 8] |= 1u << (end % 8);
527
- input->has_directive_ends = true;
809
+ directive_set(state, input->buffer, &state->directive_ends, line->required_end);
528
810
  }
529
811
  line->required_end = 0;
530
812
  }
@@ -533,16 +815,14 @@ static bool directive_requires_operand(DirectiveKind kind) {
533
815
  return kind == DIRECTIVE_REQUIRED || kind == DIRECTIVE_UNDEF || kind == DIRECTIVE_LINE || kind == DIRECTIVE_HEADER;
534
816
  }
535
817
 
536
- static void directive_barrier(DirectiveInput *input) {
818
+ static void directive_barrier(EmptyDirectives *state, DirectiveInput *input) {
537
819
  if (!input->index || !directive_newline(directive_character(input, input->index - 1))) return;
538
- uint32_t byte = input->previous_byte;
539
- input->prefix_barriers[byte / 8] |= 1u << (byte % 8);
540
- input->has_barriers = true;
820
+ input->buffer->carries = true;
821
+ directive_set(state, input->buffer, &state->line_ends, input->previous_byte);
541
822
  if (input->index < 2) return;
542
823
  int32_t previous = directive_character(input, input->index - 2);
543
824
  if (directive_newline(previous) && previous != directive_character(input, input->index - 1)) {
544
- byte = input->before_previous_byte;
545
- input->prefix_barriers[byte / 8] |= 1u << (byte % 8);
825
+ directive_set(state, input->buffer, &state->line_ends, input->before_previous_byte);
546
826
  }
547
827
  }
548
828
 
@@ -552,7 +832,7 @@ static int32_t directive_next(DirectiveInput *input) {
552
832
  directive_skip_plain(input);
553
833
  return plain;
554
834
  }
555
- while (input->index < input->length) {
835
+ while (directive_has(input, input->index)) {
556
836
  int32_t c = directive_physical_next(input);
557
837
  uint32_t after = c == '\\' ? directive_splice_end(input) : input->index;
558
838
  if (after != input->index) {
@@ -560,7 +840,7 @@ static int32_t directive_next(DirectiveInput *input) {
560
840
  continue;
561
841
  }
562
842
  if (directive_newline(c)) {
563
- if (input->index < input->length && directive_newline(directive_character(input, input->index)) &&
843
+ if (directive_has(input, input->index) && directive_newline(directive_character(input, input->index)) &&
564
844
  directive_character(input, input->index) != c) directive_physical_next(input);
565
845
  return '\n';
566
846
  }
@@ -570,14 +850,15 @@ static int32_t directive_next(DirectiveInput *input) {
570
850
  }
571
851
 
572
852
  static int32_t directive_physical_next(DirectiveInput *input) {
853
+ const DirectiveBuffer *buffer = input->buffer;
573
854
  uint32_t index = input->index;
574
855
  int32_t c;
575
856
  uint32_t width = directive_expected_width(input, index, &c);
576
857
  input->index += c > 0xffff ? 2 : 1;
577
858
  input->before_previous_byte = input->previous_byte;
578
859
  input->previous_byte = input->byte;
579
- if (input->position_index < input->position_count && input->positions[input->position_index].index == index) {
580
- DirectivePosition position = input->positions[input->position_index++];
860
+ if (input->position_index < buffer->position_count && buffer->positions[input->position_index].index == index) {
861
+ DirectivePosition position = buffer->positions[input->position_index++];
581
862
  input->previous_byte = position.byte;
582
863
  input->byte = position.byte + position.width;
583
864
  } else input->byte += width;
@@ -587,11 +868,7 @@ static int32_t directive_physical_next(DirectiveInput *input) {
587
868
  // Decodes the character at `index` into `*c` and returns its byte width when it has no recorded position.
588
869
  static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t index, int32_t *c) {
589
870
  uint32_t unit = directive_character(input, index);
590
- if (unit == 0xdfff) {
591
- *c = 0xfffd;
592
- return input->utf16 ? 2 : 1;
593
- }
594
- if (unit >= 0xd800 && unit <= 0xdbff && index + 1 < input->length) {
871
+ if (unit >= 0xd800 && unit <= 0xdbff && directive_has(input, index + 1)) {
595
872
  uint32_t low = directive_character(input, index + 1);
596
873
  if (low >= 0xdc00 && low <= 0xdfff) {
597
874
  *c = (int32_t)(0x10000 + ((unit - 0xd800) << 10) + (low - 0xdc00));
@@ -599,15 +876,15 @@ static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t i
599
876
  }
600
877
  }
601
878
  *c = (int32_t)unit;
602
- return input->utf16 ? 2 : directive_utf8_width(unit);
879
+ return input->buffer->utf16 ? 2 : directive_utf8_width(unit);
603
880
  }
604
881
 
605
882
  static uint32_t directive_splice_end(const DirectiveInput *input) {
606
883
  uint32_t after = input->index;
607
- while (after < input->length && directive_horizontal(directive_character(input, after))) after++;
608
- if (after == input->length || !directive_newline(directive_character(input, after))) return input->index;
884
+ while (directive_has(input, after) && directive_horizontal(directive_character(input, after))) after++;
885
+ if (!directive_has(input, after) || !directive_newline(directive_character(input, after))) return input->index;
609
886
  int32_t newline = directive_character(input, after++);
610
- if (after < input->length && directive_newline(directive_character(input, after)) &&
887
+ if (directive_has(input, after) && directive_newline(directive_character(input, after)) &&
611
888
  directive_character(input, after) != newline) after++;
612
889
  return after;
613
890
  }
@@ -621,16 +898,20 @@ static int32_t directive_peek(const DirectiveInput *input) {
621
898
 
622
899
  // Consumes the longest run of plain characters that `run` accepts and returns its length.
623
900
  static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun run) {
624
- uint32_t index = input->index, end = input->length;
625
- if (input->position_index < input->position_count && input->positions[input->position_index].index < end) {
626
- end = input->positions[input->position_index].index;
627
- }
628
- while (index < end) {
629
- uint16_t c = directive_character(input, index);
630
- if (c >= 0x80 || c == '\\' || directive_newline(c) || !directive_run_continues(run, c)) break;
631
- index++;
901
+ const DirectiveBuffer *buffer = input->buffer;
902
+ uint32_t index = input->index;
903
+ // Scans the stored characters before the next recorded position and reads more only at their end, because reading
904
+ // may store a character with a recorded position, which ends the run.
905
+ while (directive_has(input, index)) {
906
+ uint32_t end = buffer->length;
907
+ if (input->position_index < buffer->position_count && buffer->positions[input->position_index].index < end) {
908
+ end = buffer->positions[input->position_index].index;
909
+ }
910
+ uint32_t start = index;
911
+ index = directive_run_end(buffer, run, index, end);
912
+ if (index < end || index == start || end < buffer->length) break;
632
913
  }
633
- uint32_t length = index - input->index, width = input->utf16 ? 2 : 1;
914
+ uint32_t length = index - input->index, width = buffer->utf16 ? 2 : 1;
634
915
  if (!length) return 0;
635
916
  input->before_previous_byte = length > 1 ? input->byte + (length - 2) * width : input->previous_byte;
636
917
  input->previous_byte = input->byte + (length - 1) * width;
@@ -639,6 +920,20 @@ static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun r
639
920
  return length;
640
921
  }
641
922
 
923
+ // The index of the first stored character from `index` to `end` that ends the run, or `end`.
924
+ static uint32_t directive_run_end(const DirectiveBuffer *buffer, DirectiveRun run, uint32_t index, uint32_t end) {
925
+ while (index < end) {
926
+ const uint16_t *page = buffer->characters[index >> DIRECTIVE_PAGE_SHIFT];
927
+ uint32_t page_end = (index | (DIRECTIVE_PAGE_SIZE - 1)) + 1;
928
+ if (page_end > end) page_end = end;
929
+ for (; index < page_end; index++) {
930
+ uint16_t c = page[index & (DIRECTIVE_PAGE_SIZE - 1)];
931
+ if (c >= 0x80 || c == '\\' || directive_newline(c) || !directive_run_continues(run, c)) return index;
932
+ }
933
+ }
934
+ return end;
935
+ }
936
+
642
937
  // `c` is ASCII.
643
938
  static bool directive_run_continues(DirectiveRun run, uint16_t c) {
644
939
  bool identifier = (preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1;
@@ -662,16 +957,17 @@ static void directive_skip_plain(DirectiveInput *input) {
662
957
  input->index++;
663
958
  input->before_previous_byte = input->previous_byte;
664
959
  input->previous_byte = input->byte;
665
- input->byte += input->utf16 ? 2 : 1;
960
+ input->byte += input->buffer->utf16 ? 2 : 1;
666
961
  }
667
962
 
668
963
  // The next character when it is ASCII, cannot start a line splice or a newline, and has its expected width; otherwise -1.
669
964
  static int32_t directive_plain(const DirectiveInput *input) {
670
- if (input->index >= input->length) return -1;
965
+ if (!directive_has(input, input->index)) return -1;
671
966
  uint16_t c = directive_character(input, input->index);
672
967
  if (c >= 0x80 || c == '\\' || directive_newline(c)) return -1;
673
- bool positioned = input->position_index < input->position_count &&
674
- input->positions[input->position_index].index == input->index;
968
+ const DirectiveBuffer *buffer = input->buffer;
969
+ bool positioned = input->position_index < buffer->position_count &&
970
+ buffer->positions[input->position_index].index == input->index;
675
971
  return positioned ? -1 : c;
676
972
  }
677
973
 
@@ -708,7 +1004,7 @@ static bool directive_comment(DirectiveInput *input, bool *line) {
708
1004
  *line = directive_next(input) == '/';
709
1005
  int32_t previous = 0;
710
1006
  DirectiveRun run = *line ? DIRECTIVE_RUN_LINE_COMMENT : DIRECTIVE_RUN_BLOCK_COMMENT;
711
- while (input->index < input->length) {
1007
+ while (directive_has(input, input->index)) {
712
1008
  if (previous != '*') {
713
1009
  uint32_t length = directive_plain_run(input, run);
714
1010
  if (length) previous = directive_character(input, input->index - 1);
@@ -722,7 +1018,7 @@ static bool directive_comment(DirectiveInput *input, bool *line) {
722
1018
  }
723
1019
 
724
1020
  static bool directive_quoted(DirectiveInput *input, int32_t quote) {
725
- while (input->index < input->length) {
1021
+ while (directive_has(input, input->index)) {
726
1022
  int32_t c = directive_next(input);
727
1023
  if (c == quote) return false;
728
1024
  if (c == '\\') c = directive_next(input);
@@ -742,15 +1038,13 @@ static uint32_t directive_keyword_end(const DirectiveInput *input) {
742
1038
 
743
1039
  static DirectiveKind directive_keyword(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, const DirectiveWord *word, uint32_t end) {
744
1040
  DirectiveKind kind = directive_word_kind(word);
1041
+ DirectiveBuffer *buffer = input->buffer;
745
1042
  // Directives in a group that may be skipped need no operand, and the parser cannot tell which groups are skipped.
746
- if (directive_requires_operand(kind) && !input->conditional_depth) line->required_end = end;
747
- else {
748
- state->directive_ends[end / 8] |= 1u << (end % 8);
749
- input->has_directive_ends = true;
750
- }
1043
+ if (directive_requires_operand(kind) && !buffer->conditional_depth) line->required_end = end;
1044
+ else directive_set(state, buffer, &state->directive_ends, end);
751
1045
  if (word->length < sizeof word->directive) {
752
- if (strcmp(word->directive, "if") == 0 || strcmp(word->directive, "ifdef") == 0 || strcmp(word->directive, "ifndef") == 0) input->conditional_depth++;
753
- else if (strcmp(word->directive, "endif") == 0 && input->conditional_depth) input->conditional_depth--;
1046
+ if (strcmp(word->directive, "if") == 0 || strcmp(word->directive, "ifdef") == 0 || strcmp(word->directive, "ifndef") == 0) buffer->conditional_depth++;
1047
+ else if (strcmp(word->directive, "endif") == 0 && buffer->conditional_depth) buffer->conditional_depth--;
754
1048
  }
755
1049
  return kind;
756
1050
  }
@@ -818,13 +1112,13 @@ static bool directive_raw(DirectiveInput *input) {
818
1112
  if (length == 16 || c < 0x21 || c > 0x7e || c == ')' || c == '\\') return false;
819
1113
  delimiter[length++] = c;
820
1114
  }
821
- while (candidate.index < candidate.length) {
1115
+ while (directive_has(&candidate, candidate.index)) {
822
1116
  if (directive_physical_next(&candidate) != ')') continue;
823
1117
  DirectiveInput closing = candidate;
824
1118
  unsigned matched = 0;
825
- while (matched < length && closing.index < closing.length &&
1119
+ while (matched < length && directive_has(&closing, closing.index) &&
826
1120
  directive_physical_next(&closing) == delimiter[matched]) matched++;
827
- if (matched == length && closing.index < closing.length && directive_physical_next(&closing) == '"') {
1121
+ if (matched == length && directive_has(&closing, closing.index) && directive_physical_next(&closing) == '"') {
828
1122
  candidate = closing;
829
1123
  break;
830
1124
  }
@@ -833,22 +1127,14 @@ static bool directive_raw(DirectiveInput *input) {
833
1127
  return true;
834
1128
  }
835
1129
 
836
- static bool directive_horizontal(int32_t c) {
837
- return c == ' ' || c == '\t' || c == '\v' || c == '\f';
838
- }
839
-
840
- static bool directive_newline(int32_t c) {
841
- return c == '\n' || c == '\r';
842
- }
843
-
844
1130
  static uint32_t directive_byte(const DirectiveInput *input) {
845
1131
  return input->byte;
846
1132
  }
847
1133
 
848
1134
  static void directive_record(EmptyDirectives *state, DirectiveInput *input, uint32_t start, uint32_t end) {
849
- state->starts[start / 8] |= 1u << (start % 8);
850
- state->ends[end / 8] |= 1u << (end % 8);
851
- input->has_empty = true;
1135
+ input->buffer->carries = true;
1136
+ directive_set(state, input->buffer, &state->starts, start);
1137
+ directive_set(state, input->buffer, &state->ends, end);
852
1138
  }
853
1139
 
854
1140
  #endif