@willbooster/tree-sitter-c 43.0.0 → 45.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,27 @@
1
+ // code-gauge-allow-file fileNcss -- Unit-level code, comment, quote, and SIMD scanners share `DirectiveUnitPass`.
1
2
  // The line-state pass of `directiveScan.h` over code units read from memory instead of characters fed one at a time.
2
3
  // Included by `directiveScan.h`, whose `DirectiveScan` it reads and leaves as the character path would.
3
4
  #ifndef TREE_SITTER_C_DIRECTIVE_UNITS_H_
4
5
  #define TREE_SITTER_C_DIRECTIVE_UNITS_H_
5
6
 
6
- // The classes of an ASCII character among the units of the input: the kind of code it is, whether it is `_` or `P`,
7
- // without both of which no text holds `pragma_word`, and whether it is plain text of a line comment, of a block comment
8
- // and of a literal.
9
- enum { UNIT_WORD = 1, UNIT_SPACE = 2, UNIT_PUNCTUATION = 4, UNIT_UNDERSCORE = 8, UNIT_P = 16, UNIT_LINE = 32, UNIT_BLOCK = 64, UNIT_QUOTE = 128 };
7
+ #if defined(__ARM_NEON) && defined(__aarch64__)
8
+ #include <arm_neon.h>
9
+ #define DIRECTIVE_USE_NEON 1
10
+ #endif
11
+
12
+ // The classes of an ASCII character among the units of the input: whether it is part of a word, horizontal space, or
13
+ // plain code (word or plain punctuation), whether it is `_` or `P`, without both of which no text holds `pragma_word`,
14
+ // and whether it is plain text of a line comment, of a block comment and of a literal.
15
+ enum {
16
+ UNIT_WORD = 1,
17
+ UNIT_SPACE = 2,
18
+ UNIT_CODE = 4,
19
+ UNIT_UNDERSCORE = 8,
20
+ UNIT_P = 16,
21
+ UNIT_LINE = 32,
22
+ UNIT_BLOCK = 64,
23
+ UNIT_QUOTE = 128,
24
+ };
10
25
  #define UNIT_PRAGMA (UNIT_UNDERSCORE | UNIT_P)
11
26
  #define UNIT_CLASS_COUNT 128
12
27
 
@@ -14,16 +29,20 @@ typedef struct {
14
29
  uint32_t consumed, line_feeds, tail;
15
30
  } DirectiveUnits;
16
31
 
17
- // The pass's state over `count` units of which `i` are read. The word, comment or literal being read starts at `start`
18
- // when `fresh`, and before the units otherwise; `seen` holds the classes of its characters.
32
+ // The pass's state over `count` units of which `i` are read. `floor` is where the current stretch of plain code began,
33
+ // and `pragma_from` where `seen` started accumulating character classes.
19
34
  typedef struct {
20
35
  const char *units;
21
36
  const uint8_t *classes;
22
- uint32_t count, i, start, line_feeds, line, quote;
37
+ uint32_t count, i, start, floor, pragma_from, line_feeds, line, quote;
23
38
  int32_t previous;
24
39
  DirectiveScanMode mode;
25
40
  bool lines, beginning, star, number, pragma, fresh;
26
41
  uint8_t seen;
42
+ #ifdef DIRECTIVE_USE_NEON
43
+ uint8x16x4_t table_lo, table_hi;
44
+ uint8x16_t seen_vec;
45
+ #endif
27
46
  } DirectiveUnitPass;
28
47
 
29
48
  #if defined(__GNUC__) || defined(__clang__)
@@ -33,16 +52,26 @@ typedef struct {
33
52
  #endif
34
53
 
35
54
  DIRECTIVE_INLINE bool directive_units_code(DirectiveUnitPass *pass, unsigned size);
55
+ DIRECTIVE_INLINE bool directive_units_code_beginning(DirectiveUnitPass *pass, unsigned size);
36
56
  DIRECTIVE_INLINE bool directive_units_code_token(DirectiveUnitPass *pass, unsigned size, uint32_t c);
57
+ DIRECTIVE_INLINE bool directive_units_slash(DirectiveUnitPass *pass, unsigned size);
58
+ DIRECTIVE_INLINE bool directive_units_word_quote(DirectiveUnitPass *pass, unsigned size, uint32_t c);
59
+ DIRECTIVE_INLINE void directive_units_finish_code(DirectiveUnitPass *pass, unsigned size);
60
+ static uint32_t directive_units_word_start(const DirectiveUnitPass *pass, unsigned size, uint32_t end);
37
61
  DIRECTIVE_INLINE bool directive_units_word(DirectiveUnitPass *pass, unsigned size);
38
62
  DIRECTIVE_INLINE bool directive_units_line_comment(DirectiveUnitPass *pass, unsigned size);
39
63
  DIRECTIVE_INLINE bool directive_units_block_comment(DirectiveUnitPass *pass, unsigned size);
40
64
  DIRECTIVE_INLINE bool directive_units_quoted(DirectiveUnitPass *pass, unsigned size);
41
- DIRECTIVE_INLINE void directive_units_open(DirectiveUnitPass *pass, DirectiveScanMode mode, uint32_t length);
65
+ DIRECTIVE_INLINE uint32_t directive_units_advance_mask(DirectiveUnitPass *pass, unsigned size, uint8_t mask);
66
+ #ifdef DIRECTIVE_USE_NEON
67
+ DIRECTIVE_INLINE void directive_units_neon_advance(DirectiveUnitPass *pass, unsigned size, uint8_t mask);
68
+ DIRECTIVE_INLINE bool directive_units_neon_load(const char *units, unsigned size, uint32_t i, uint8x16_t *chars);
69
+ #endif
70
+ DIRECTIVE_INLINE void directive_units_open(DirectiveUnitPass *pass, unsigned size, DirectiveScanMode mode, uint32_t length);
42
71
  DIRECTIVE_INLINE bool directive_units_close(DirectiveUnitPass *pass, unsigned size);
43
72
  DIRECTIVE_INLINE bool directive_units_line_feed(DirectiveUnitPass *pass, unsigned size);
44
73
  DIRECTIVE_INLINE bool directive_units_escape(const DirectiveUnitPass *pass, unsigned size);
45
- DIRECTIVE_INLINE void directive_units_note_pragma(DirectiveUnitPass *pass, unsigned size);
74
+ DIRECTIVE_INLINE void directive_units_flush_pragma(DirectiveUnitPass *pass, unsigned size);
46
75
  DIRECTIVE_INLINE uint32_t directive_unit(const char *units, unsigned size, uint32_t index);
47
76
  static void directive_scan_units_end(DirectiveScan *scan, const char *units, unsigned size, uint32_t from, uint32_t end, uint32_t byte);
48
77
  static void directive_scan_units_word(DirectiveScan *scan, const char *units, unsigned size, uint32_t first, uint32_t end);
@@ -58,6 +87,9 @@ DIRECTIVE_INLINE DirectiveUnits directive_scan_units(DirectiveScan *scan, const
58
87
  DirectiveUnitPass pass = {
59
88
  .units = units, .classes = scan->classes, .count = count, .quote = (uint32_t)scan->quote, .previous = scan->previous,
60
89
  .mode = scan->mode, .lines = lines, .beginning = scan->beginning, .star = scan->star, .number = scan->number,
90
+ #ifdef DIRECTIVE_USE_NEON
91
+ .table_lo = vld1q_u8_x4(scan->classes), .table_hi = vld1q_u8_x4(scan->classes + 64), .seen_vec = vdupq_n_u8(0),
92
+ #endif
61
93
  };
62
94
  for (bool more = true; more;) {
63
95
  switch (pass.mode) {
@@ -77,9 +109,10 @@ DIRECTIVE_INLINE DirectiveUnits directive_scan_units(DirectiveScan *scan, const
77
109
  more = directive_units_code(&pass, size);
78
110
  }
79
111
  }
112
+ directive_units_finish_code(&pass, size);
80
113
  if (!lines) return (DirectiveUnits){pass.mode == SCAN_CODE ? pass.i : pass.start, 0, 0};
81
114
  if (!pass.i) return (DirectiveUnits){0};
82
- if (pass.mode != SCAN_CODE) directive_units_note_pragma(&pass, size);
115
+ directive_units_flush_pragma(&pass, size);
83
116
  if (pass.pragma) *scan->pragma = true;
84
117
  scan->mode = pass.mode;
85
118
  scan->beginning = pass.beginning;
@@ -91,67 +124,162 @@ DIRECTIVE_INLINE DirectiveUnits directive_scan_units(DirectiveScan *scan, const
91
124
  return (DirectiveUnits){pass.i, pass.line_feeds, pass.i - pass.line};
92
125
  }
93
126
 
94
- // Each of these reads the units of one mode and returns true when those of `pass->mode` follow.
95
-
127
+ // Reads plain code, words and line breaks in one mode, opening another mode only at comments, quotes, raw-string
128
+ // candidates and digit separators.
96
129
  DIRECTIVE_INLINE bool directive_units_code(DirectiveUnitPass *pass, unsigned size) {
97
- uint32_t c = 0;
98
- uint8_t class = 0, plain = 0;
99
- while (pass->i < pass->count && (c = directive_unit(pass->units, size, pass->i)) < UNIT_CLASS_COUNT &&
100
- ((class = pass->classes[c]) & (UNIT_SPACE | UNIT_PUNCTUATION))) {
101
- plain |= class;
102
- pass->i++;
130
+ for (;;) {
131
+ if (pass->beginning && !directive_units_code_beginning(pass, size)) {
132
+ return pass->mode != SCAN_CODE;
133
+ }
134
+ uint32_t c = directive_units_advance_mask(pass, size, UNIT_CODE | UNIT_SPACE);
135
+ if (pass->i >= pass->count || c >= UNIT_CLASS_COUNT) return false;
136
+ if (c == '\n' && directive_units_line_feed(pass, size)) {
137
+ pass->beginning = true;
138
+ continue;
139
+ }
140
+ if (c == '#') {
141
+ pass->i++;
142
+ continue;
143
+ }
144
+ return directive_units_code_token(pass, size, c);
103
145
  }
104
- if (plain & UNIT_PUNCTUATION) pass->beginning = false;
105
- if (pass->i >= pass->count || c >= UNIT_CLASS_COUNT) return false;
106
- if (!(class & UNIT_WORD)) return directive_units_code_token(pass, size, c);
107
- pass->beginning = false;
108
- pass->number = c >= '0' && c <= '9';
109
- directive_units_open(pass, SCAN_WORD, 1);
110
- pass->seen = class;
111
- return true;
112
146
  }
113
147
 
114
- // Reads `c`, which is neither plain code nor part of a word.
115
- // code-gauge-allow functionCognitiveComplexity -- Each character that opens a line, a literal or a comment changes the mode in its own way.
116
- DIRECTIVE_INLINE bool directive_units_code_token(DirectiveUnitPass *pass, unsigned size, uint32_t c) {
117
- if (c == '\n') {
118
- if (!directive_units_line_feed(pass, size)) return false;
119
- pass->beginning = true;
120
- return true;
148
+ // Advances from the beginning of a line to its first non-space unit; returns true once the line holds plain code.
149
+ DIRECTIVE_INLINE bool directive_units_code_beginning(DirectiveUnitPass *pass, unsigned size) {
150
+ for (;;) {
151
+ uint32_t c = 0;
152
+ while (pass->i < pass->count && (c = directive_unit(pass->units, size, pass->i)) < UNIT_CLASS_COUNT &&
153
+ (pass->classes[c] & UNIT_SPACE)) {
154
+ pass->i++;
155
+ }
156
+ if (pass->i >= pass->count || c >= UNIT_CLASS_COUNT) return false;
157
+ uint8_t class = pass->classes[c];
158
+ if (class & UNIT_CODE) {
159
+ pass->beginning = false;
160
+ pass->seen |= class;
161
+ pass->i++;
162
+ return true;
163
+ }
164
+ if (c != '\n' || !directive_units_line_feed(pass, size)) {
165
+ return directive_units_code_token(pass, size, c) && pass->mode == SCAN_CODE;
166
+ }
121
167
  }
168
+ }
169
+
170
+ // Reads `c`, which is neither plain code nor a space.
171
+ DIRECTIVE_INLINE bool directive_units_code_token(DirectiveUnitPass *pass, unsigned size, uint32_t c) {
122
172
  if (c == '"' || c == '\'') {
123
- pass->quote = c;
124
- directive_units_open(pass, SCAN_QUOTE, 1);
125
- } else if (c == '/') {
126
- if (pass->i + 1 >= pass->count) return false;
127
- uint32_t next = directive_unit(pass->units, size, pass->i + 1);
128
- // A line splice may separate the `/` from what makes it a comment.
129
- if (next == '\\') return false;
130
- if (next == '/' || next == '*') {
131
- pass->star = false;
132
- directive_units_open(pass, next == '/' ? SCAN_LINE_COMMENT : SCAN_BLOCK_COMMENT, 2);
133
- return true;
173
+ if (!directive_units_word_quote(pass, size, c)) {
174
+ pass->quote = c;
175
+ directive_units_open(pass, size, SCAN_QUOTE, 1);
134
176
  }
135
- pass->i++;
136
- } else if (c == '#' && !pass->beginning) {
137
- pass->i++;
138
- } else {
139
- return false;
177
+ pass->beginning = false;
178
+ return true;
179
+ }
180
+ if (c == '/') return directive_units_slash(pass, size);
181
+ if (c != '#' || pass->beginning) return false;
182
+ pass->i++;
183
+ pass->floor = pass->i;
184
+ return true;
185
+ }
186
+
187
+ DIRECTIVE_INLINE bool directive_units_slash(DirectiveUnitPass *pass, unsigned size) {
188
+ if (pass->i + 1 >= pass->count) return false;
189
+ uint32_t next = directive_unit(pass->units, size, pass->i + 1);
190
+ if (next == '\\') return false;
191
+ if (next == '/' || next == '*') {
192
+ pass->star = false;
193
+ directive_units_open(pass, size, next == '/' ? SCAN_LINE_COMMENT : SCAN_BLOCK_COMMENT, 2);
194
+ return true;
140
195
  }
141
196
  pass->beginning = false;
197
+ pass->i++;
198
+ pass->floor = pass->i;
199
+ return true;
200
+ }
201
+
202
+ // Reopens the word before `"` when it may be a raw-string prefix, or the number before `'` so `'` is read as a
203
+ // separator instead of a character literal.
204
+ DIRECTIVE_INLINE bool directive_units_word_quote(DirectiveUnitPass *pass, unsigned size, uint32_t c) {
205
+ if (pass->i <= pass->floor) return false;
206
+ uint32_t prev = directive_unit(pass->units, size, pass->i - 1);
207
+ if (c == '"' && prev != 'R') return false;
208
+ if (c == '\'' && !(pass->classes[prev] & UNIT_WORD) && prev != '.') return false;
209
+ uint32_t start = directive_units_word_start(pass, size, pass->i);
210
+ if (start >= pass->i) return false;
211
+ uint32_t first = directive_unit(pass->units, size, start);
212
+ bool number = first >= '0' && first <= '9';
213
+ if (c == '\'' && !number) return false;
214
+ if (c == '"' && pass->i - start >= sizeof ((GnuRawStringPrefix *)0)->value) return false;
215
+ pass->i = start;
216
+ pass->number = number;
217
+ directive_units_open(pass, size, SCAN_WORD, 1);
142
218
  return true;
143
219
  }
144
220
 
221
+ // Reopens an unfinished word at the stop of `SCAN_CODE` when the next character may continue it.
222
+ DIRECTIVE_INLINE void directive_units_finish_code(DirectiveUnitPass *pass, unsigned size) {
223
+ if (pass->mode != SCAN_CODE || pass->i <= pass->floor) return;
224
+ if (pass->i < pass->count) {
225
+ uint32_t next = directive_unit(pass->units, size, pass->i);
226
+ if (next < UNIT_CLASS_COUNT && next != '\\') return;
227
+ }
228
+ uint32_t prev = directive_unit(pass->units, size, pass->i - 1);
229
+ if (!(pass->classes[prev] & UNIT_WORD) && prev != '.' && prev != '+' && prev != '-') return;
230
+ uint32_t start = directive_units_word_start(pass, size, pass->i);
231
+ if (start >= pass->i) return;
232
+ uint32_t first = directive_unit(pass->units, size, start);
233
+ pass->mode = SCAN_WORD;
234
+ pass->start = start;
235
+ pass->fresh = true;
236
+ pass->number = first >= '0' && first <= '9';
237
+ }
238
+
239
+ // Finds the start of the unfinished word or preprocessing number in `[pass->floor .. end]`, or `end` when none is open.
240
+ // code-gauge-allow functionCognitiveComplexity -- Backward candidate bound and forward pp-number replay share one helper.
241
+ static uint32_t directive_units_word_start(const DirectiveUnitPass *pass, unsigned size, uint32_t end) {
242
+ uint32_t k = end;
243
+ while (k > pass->floor) {
244
+ uint32_t u = directive_unit(pass->units, size, k - 1);
245
+ if ((pass->classes[u] & UNIT_WORD) || u == '.') {
246
+ k--;
247
+ continue;
248
+ }
249
+ if ((u == '+' || u == '-') && k - 1 > pass->floor) {
250
+ uint32_t p = directive_unit(pass->units, size, k - 2);
251
+ if (p == 'e' || p == 'E' || p == 'p' || p == 'P') {
252
+ k -= 2;
253
+ continue;
254
+ }
255
+ }
256
+ break;
257
+ }
258
+ uint32_t word_start = end, prev = 0;
259
+ bool in_word = false, number = false;
260
+ for (uint32_t j = k; j < end; j++) {
261
+ uint32_t u = directive_unit(pass->units, size, j);
262
+ bool word = (pass->classes[u] & UNIT_WORD) != 0;
263
+ bool exponent = number && (u == '+' || u == '-') && (prev == 'e' || prev == 'E' || prev == 'p' || prev == 'P');
264
+ if (!in_word) {
265
+ if (word) {
266
+ in_word = true;
267
+ number = u >= '0' && u <= '9';
268
+ word_start = j;
269
+ }
270
+ } else if (!word && !(number && (u == '.' || exponent))) {
271
+ in_word = false;
272
+ }
273
+ prev = u;
274
+ }
275
+ return in_word ? word_start : end;
276
+ }
277
+
145
278
  // code-gauge-allow functionCognitiveComplexity -- A number continues over signs and separators that end any other word.
146
279
  DIRECTIVE_INLINE bool directive_units_word(DirectiveUnitPass *pass, unsigned size) {
147
280
  uint32_t c = 0;
148
- uint8_t class;
149
281
  for (;;) {
150
- while (pass->i < pass->count && (c = directive_unit(pass->units, size, pass->i)) < UNIT_CLASS_COUNT &&
151
- ((class = pass->classes[c]) & UNIT_WORD)) {
152
- pass->seen |= class;
153
- pass->i++;
154
- }
282
+ c = directive_units_advance_mask(pass, size, UNIT_WORD);
155
283
  if (pass->i >= pass->count || c >= UNIT_CLASS_COUNT || c == '\\') return false;
156
284
  if (!pass->number) break;
157
285
  int32_t previous = pass->i > (pass->fresh ? pass->start : 0)
@@ -162,26 +290,19 @@ DIRECTIVE_INLINE bool directive_units_word(DirectiveUnitPass *pass, unsigned siz
162
290
  if (!(c == '.' || c == '\'' || exponent_sign)) break;
163
291
  pass->i++;
164
292
  }
165
- // The word may be a raw string prefix, which no word of more characters is.
166
293
  if (c == '"' && (!pass->fresh || pass->i - pass->start < sizeof ((GnuRawStringPrefix *)0)->value)) return false;
167
294
  return directive_units_close(pass, size);
168
295
  }
169
296
 
170
- // code-gauge-allow duplicateLines -- The loops over a comment's and a literal's text share their shape, and each ends its text differently.
171
297
  DIRECTIVE_INLINE bool directive_units_line_comment(DirectiveUnitPass *pass, unsigned size) {
172
298
  for (;;) {
173
- if (pass->i >= pass->count) return false;
174
- uint32_t c = directive_unit(pass->units, size, pass->i);
175
- if (c >= UNIT_CLASS_COUNT) return false;
176
- uint8_t class = pass->classes[c];
177
- if (class & UNIT_LINE) {
178
- pass->seen |= class;
179
- pass->i++;
180
- } else if (c == '\\' && directive_units_escape(pass, size)) {
299
+ uint32_t c = directive_units_advance_mask(pass, size, UNIT_LINE);
300
+ if (pass->i >= pass->count || c >= UNIT_CLASS_COUNT) return false;
301
+ if (c == '\\' && directive_units_escape(pass, size)) {
181
302
  pass->i++;
182
303
  } else if (c == '\n' && !pass->lines) {
183
- // The comment is read, and the line feed ends the line for the caller.
184
304
  pass->mode = SCAN_CODE;
305
+ pass->floor = pass->i;
185
306
  return false;
186
307
  } else if (c == '\n' && directive_units_line_feed(pass, size)) {
187
308
  pass->beginning = true;
@@ -193,19 +314,15 @@ DIRECTIVE_INLINE bool directive_units_line_comment(DirectiveUnitPass *pass, unsi
193
314
  }
194
315
 
195
316
  // code-gauge-allow functionCognitiveComplexity -- The comment's end depends on the character before each `/`.
196
- // code-gauge-allow duplicateLines -- The loops over a comment's and a literal's text share their shape, and each ends its text differently.
197
317
  DIRECTIVE_INLINE bool directive_units_block_comment(DirectiveUnitPass *pass, unsigned size) {
198
318
  for (;;) {
199
- if (pass->i >= pass->count) return false;
200
- uint32_t c = directive_unit(pass->units, size, pass->i);
201
- if (c >= UNIT_CLASS_COUNT) return false;
202
- uint8_t class = pass->classes[c];
319
+ uint32_t before = pass->i;
320
+ uint32_t c = directive_units_advance_mask(pass, size, UNIT_BLOCK);
321
+ if (pass->i > before) pass->star = false;
322
+ if (pass->i >= pass->count || c >= UNIT_CLASS_COUNT) return false;
203
323
  bool star = pass->star;
204
324
  pass->star = c == '*';
205
- if (class & UNIT_BLOCK) {
206
- pass->seen |= class;
207
- pass->i++;
208
- } else if (c == '*') {
325
+ if (c == '*') {
209
326
  pass->i++;
210
327
  } else if (c == '/') {
211
328
  pass->i++;
@@ -219,20 +336,15 @@ DIRECTIVE_INLINE bool directive_units_block_comment(DirectiveUnitPass *pass, uns
219
336
  }
220
337
  }
221
338
 
222
- // code-gauge-allow duplicateLines -- The loops over a comment's and a literal's text share their shape, and each ends its text differently.
223
339
  DIRECTIVE_INLINE bool directive_units_quoted(DirectiveUnitPass *pass, unsigned size) {
224
340
  for (;;) {
225
- if (pass->i >= pass->count) return false;
226
- uint32_t c = directive_unit(pass->units, size, pass->i);
227
- if (c >= UNIT_CLASS_COUNT) return false;
228
- uint8_t class = pass->classes[c];
229
- if (class & UNIT_QUOTE) {
230
- pass->seen |= class;
231
- pass->i++;
232
- } else if (c == pass->quote) {
341
+ uint32_t c = directive_units_advance_mask(pass, size, UNIT_QUOTE);
342
+ if (pass->i >= pass->count || c >= UNIT_CLASS_COUNT) return false;
343
+ if (c == pass->quote) {
233
344
  pass->i++;
234
345
  return directive_units_close(pass, size);
235
- } else if (c == '"' || c == '\'') {
346
+ }
347
+ if (c == '"' || c == '\'') {
236
348
  pass->i++;
237
349
  } else if (c == '\\' && directive_units_escape(pass, size)) {
238
350
  pass->seen |= pass->classes[directive_unit(pass->units, size, pass->i + 1)];
@@ -243,27 +355,83 @@ DIRECTIVE_INLINE bool directive_units_quoted(DirectiveUnitPass *pass, unsigned s
243
355
  }
244
356
  }
245
357
 
358
+ #ifdef DIRECTIVE_USE_NEON
359
+ DIRECTIVE_INLINE void directive_units_neon_advance(DirectiveUnitPass *pass, unsigned size, uint8_t mask) {
360
+ uint8x16_t mask_vec = vdupq_n_u8(mask);
361
+ uint8x16_t hi_xor = vdupq_n_u8(0x40);
362
+ uint8x16_t nl_vec = vdupq_n_u8('\n');
363
+ bool cross_lines = pass->lines && (mask == (UNIT_CODE | UNIT_SPACE) || mask == UNIT_BLOCK);
364
+ uint8_t tail_req = mask == (UNIT_CODE | UNIT_SPACE) ? UNIT_CODE : UNIT_BLOCK;
365
+ uint8x16_t chars;
366
+ while (pass->count - pass->i >= 16 && directive_units_neon_load(pass->units, size, pass->i, &chars)) {
367
+ uint8x16_t cls = vorrq_u8(vqtbl4q_u8(pass->table_lo, chars), vqtbl4q_u8(pass->table_hi, veorq_u8(chars, hi_xor)));
368
+ if (vminvq_u8(vandq_u8(cls, mask_vec)) == 0) {
369
+ if (!cross_lines || !(vgetq_lane_u8(cls, 15) & tail_req)) break;
370
+ uint8x16_t lf = vceqq_u8(chars, nl_vec);
371
+ if (vminvq_u8(vorrq_u8(vandq_u8(cls, mask_vec), lf)) == 0) break;
372
+ pass->line_feeds += vaddvq_u8(vshrq_n_u8(lf, 7));
373
+ uint64_t nibbles = vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(lf), 4)), 0);
374
+ pass->line = pass->i + 16u - ((uint32_t)__builtin_clzll(nibbles) >> 2);
375
+ pass->floor = pass->line;
376
+ }
377
+ pass->seen_vec = vorrq_u8(pass->seen_vec, cls);
378
+ pass->i += 16;
379
+ }
380
+ }
381
+
382
+ DIRECTIVE_INLINE bool directive_units_neon_load(const char *units, unsigned size, uint32_t i, uint8x16_t *chars) {
383
+ if (size == 1) {
384
+ *chars = vld1q_u8((const uint8_t *)units + i);
385
+ return vmaxvq_u8(*chars) < 0x80;
386
+ }
387
+ const uint8_t *ptr = (const uint8_t *)units + 2 * (size_t)i;
388
+ uint16x8_t lo = vreinterpretq_u16_u8(vld1q_u8(ptr));
389
+ uint16x8_t hi = vreinterpretq_u16_u8(vld1q_u8(ptr + 16));
390
+ if (vmaxvq_u16(vorrq_u16(lo, hi)) >= 0x80) return false;
391
+ *chars = vcombine_u8(vmovn_u16(lo), vmovn_u16(hi));
392
+ return true;
393
+ }
394
+ #endif
395
+
396
+ DIRECTIVE_INLINE uint32_t directive_units_advance_mask(DirectiveUnitPass *pass, unsigned size, uint8_t mask) {
397
+ #ifdef DIRECTIVE_USE_NEON
398
+ if (pass->count - pass->i >= 16) directive_units_neon_advance(pass, size, mask);
399
+ #endif
400
+ uint32_t c = 0;
401
+ uint8_t cls = 0;
402
+ while (pass->i < pass->count && (c = directive_unit(pass->units, size, pass->i)) < UNIT_CLASS_COUNT &&
403
+ ((cls = pass->classes[c]) & mask)) {
404
+ pass->seen |= cls;
405
+ pass->i++;
406
+ }
407
+ return c;
408
+ }
409
+
246
410
  // Starts a word, comment or literal whose first `length` units are read with this.
247
- DIRECTIVE_INLINE void directive_units_open(DirectiveUnitPass *pass, DirectiveScanMode mode, uint32_t length) {
411
+ DIRECTIVE_INLINE void directive_units_open(DirectiveUnitPass *pass, unsigned size, DirectiveScanMode mode, uint32_t length) {
412
+ directive_units_flush_pragma(pass, size);
248
413
  pass->mode = mode;
249
414
  pass->start = pass->i;
250
415
  pass->fresh = true;
251
- pass->seen = 0;
252
416
  pass->i += length;
253
417
  }
254
418
 
255
419
  // Ends the word, comment or literal being read; code follows it.
256
420
  DIRECTIVE_INLINE bool directive_units_close(DirectiveUnitPass *pass, unsigned size) {
257
- directive_units_note_pragma(pass, size);
421
+ directive_units_flush_pragma(pass, size);
258
422
  pass->mode = SCAN_CODE;
423
+ pass->floor = pass->i;
259
424
  return true;
260
425
  }
261
426
 
262
427
  // Reads the line feed at `pass->i` unless the pass stops at line feeds, or a carriage return may belong to it.
263
428
  DIRECTIVE_INLINE bool directive_units_line_feed(DirectiveUnitPass *pass, unsigned size) {
264
429
  if (!pass->lines || pass->i + 1 >= pass->count || directive_unit(pass->units, size, pass->i + 1) == '\r') return false;
430
+ directive_units_flush_pragma(pass, size);
265
431
  pass->line_feeds++;
266
432
  pass->line = ++pass->i;
433
+ pass->floor = pass->i;
434
+ pass->pragma_from = pass->i;
267
435
  return true;
268
436
  }
269
437
 
@@ -274,9 +442,18 @@ DIRECTIVE_INLINE bool directive_units_escape(const DirectiveUnitPass *pass, unsi
274
442
  return next < UNIT_CLASS_COUNT && (pass->classes[next] & (UNIT_LINE | UNIT_SPACE)) == UNIT_LINE;
275
443
  }
276
444
 
277
- DIRECTIVE_INLINE void directive_units_note_pragma(DirectiveUnitPass *pass, unsigned size) {
278
- if ((pass->seen & UNIT_PRAGMA) != UNIT_PRAGMA) return;
279
- if (directive_units_pragma(pass->units, size, pass->fresh ? pass->start : 0, pass->i)) pass->pragma = true;
445
+ DIRECTIVE_INLINE void directive_units_flush_pragma(DirectiveUnitPass *pass, unsigned size) {
446
+ #ifdef DIRECTIVE_USE_NEON
447
+ if (vmaxvq_u8(vandq_u8(pass->seen_vec, vdupq_n_u8(UNIT_UNDERSCORE)))) pass->seen |= UNIT_UNDERSCORE;
448
+ if (vmaxvq_u8(vandq_u8(pass->seen_vec, vdupq_n_u8(UNIT_P)))) pass->seen |= UNIT_P;
449
+ pass->seen_vec = vdupq_n_u8(0);
450
+ #endif
451
+ if ((pass->seen & UNIT_PRAGMA) == UNIT_PRAGMA &&
452
+ directive_units_pragma(pass->units, size, pass->pragma_from, pass->i)) {
453
+ pass->pragma = true;
454
+ }
455
+ pass->seen = 0;
456
+ pass->pragma_from = pass->i;
280
457
  }
281
458
 
282
459
  // The unit at `index` of units of `size` bytes in the host's byte order.
@@ -295,7 +472,6 @@ static void directive_scan_units_end(DirectiveScan *scan, const char *units, uns
295
472
  if (scan->mode == SCAN_CODE) {
296
473
  scan->recent = 0;
297
474
  } else {
298
- // A comment's or a literal's opener is no part of `pragma_word`, so what was read before it does not matter.
299
475
  uint64_t recent = from == UINT32_MAX ? scan->recent : 0;
300
476
  uint32_t first = from == UINT32_MAX ? 0 : from;
301
477
  for (uint32_t k = end - first > 8 ? end - 8 : first; k < end; k++) recent = recent << 8 | directive_unit(units, size, k);
@@ -334,7 +510,8 @@ static void directive_scan_units_word(DirectiveScan *scan, const char *units, un
334
510
  static bool directive_units_pragma(const char *units, unsigned size, uint32_t start, uint32_t end) {
335
511
  uint32_t length = sizeof pragma_word - 1;
336
512
  for (uint32_t i = start; i + length <= end; i++) {
337
- uint32_t matched = 0;
513
+ if (directive_unit(units, size, i) != '_' || directive_unit(units, size, i + 1) != 'P') continue;
514
+ uint32_t matched = 2;
338
515
  while (matched < length && directive_unit(units, size, i + matched) == (uint8_t)pragma_word[matched]) matched++;
339
516
  if (matched == length) return true;
340
517
  }
@@ -355,7 +532,9 @@ static bool directive_pragma_pending(uint64_t recent) {
355
532
  static void directive_unit_classes(uint8_t *classes) {
356
533
  for (int32_t c = 0; c < UNIT_CLASS_COUNT; c++) {
357
534
  bool text = c != 0 && !directive_newline(c) && c != '\\';
358
- uint8_t code = directive_scan_ascii_word(c) ? UNIT_WORD : directive_horizontal(c) ? UNIT_SPACE : directive_scan_ascii_plain(c) ? UNIT_PUNCTUATION : 0;
535
+ bool word = directive_scan_ascii_word(c);
536
+ bool space = directive_horizontal(c);
537
+ uint8_t code = (word ? UNIT_WORD | UNIT_CODE : 0) | (space ? UNIT_SPACE : directive_scan_ascii_plain(c) ? UNIT_CODE : 0);
359
538
  uint8_t letter = c == '_' ? UNIT_UNDERSCORE : c == 'P' ? UNIT_P : 0;
360
539
  uint8_t block = text && c != '*' && c != '/' ? UNIT_BLOCK : 0, quote = text && c != '"' && c != '\'' ? UNIT_QUOTE : 0;
361
540
  classes[c] = (uint8_t)(code | letter | (text ? UNIT_LINE : 0) | block | quote);