@willbooster/tree-sitter-c 41.0.0 → 43.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.
@@ -0,0 +1,365 @@
1
+ // The line-state pass of `directiveScan.h` over code units read from memory instead of characters fed one at a time.
2
+ // Included by `directiveScan.h`, whose `DirectiveScan` it reads and leaves as the character path would.
3
+ #ifndef TREE_SITTER_C_DIRECTIVE_UNITS_H_
4
+ #define TREE_SITTER_C_DIRECTIVE_UNITS_H_
5
+
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 };
10
+ #define UNIT_PRAGMA (UNIT_UNDERSCORE | UNIT_P)
11
+ #define UNIT_CLASS_COUNT 128
12
+
13
+ typedef struct {
14
+ uint32_t consumed, line_feeds, tail;
15
+ } DirectiveUnits;
16
+
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.
19
+ typedef struct {
20
+ const char *units;
21
+ const uint8_t *classes;
22
+ uint32_t count, i, start, line_feeds, line, quote;
23
+ int32_t previous;
24
+ DirectiveScanMode mode;
25
+ bool lines, beginning, star, number, pragma, fresh;
26
+ uint8_t seen;
27
+ } DirectiveUnitPass;
28
+
29
+ #if defined(__GNUC__) || defined(__clang__)
30
+ #define DIRECTIVE_INLINE __attribute__((always_inline)) static inline
31
+ #else
32
+ #define DIRECTIVE_INLINE static inline
33
+ #endif
34
+
35
+ DIRECTIVE_INLINE bool directive_units_code(DirectiveUnitPass *pass, unsigned size);
36
+ DIRECTIVE_INLINE bool directive_units_code_token(DirectiveUnitPass *pass, unsigned size, uint32_t c);
37
+ DIRECTIVE_INLINE bool directive_units_word(DirectiveUnitPass *pass, unsigned size);
38
+ DIRECTIVE_INLINE bool directive_units_line_comment(DirectiveUnitPass *pass, unsigned size);
39
+ DIRECTIVE_INLINE bool directive_units_block_comment(DirectiveUnitPass *pass, unsigned size);
40
+ DIRECTIVE_INLINE bool directive_units_quoted(DirectiveUnitPass *pass, unsigned size);
41
+ DIRECTIVE_INLINE void directive_units_open(DirectiveUnitPass *pass, DirectiveScanMode mode, uint32_t length);
42
+ DIRECTIVE_INLINE bool directive_units_close(DirectiveUnitPass *pass, unsigned size);
43
+ DIRECTIVE_INLINE bool directive_units_line_feed(DirectiveUnitPass *pass, unsigned size);
44
+ DIRECTIVE_INLINE bool directive_units_escape(const DirectiveUnitPass *pass, unsigned size);
45
+ DIRECTIVE_INLINE void directive_units_note_pragma(DirectiveUnitPass *pass, unsigned size);
46
+ DIRECTIVE_INLINE uint32_t directive_unit(const char *units, unsigned size, uint32_t index);
47
+ static void directive_scan_units_end(DirectiveScan *scan, const char *units, unsigned size, uint32_t from, uint32_t end, uint32_t byte);
48
+ static void directive_scan_units_word(DirectiveScan *scan, const char *units, unsigned size, uint32_t first, uint32_t end);
49
+ static bool directive_units_pragma(const char *units, unsigned size, uint32_t start, uint32_t end);
50
+
51
+ // Runs the line-state pass over `count` units of `size` bytes, the first of which is at `byte` of the input, as
52
+ // `directive_scan_feed` does one character at a time. It stops before the first unit that needs the physical layer (a
53
+ // backslash that may start a line splice, a carriage return, a character that is not ASCII), that starts a directive or
54
+ // may open a raw string, or that the units read do not tell apart. Without `lines`, it also stops before each line
55
+ // feed, leaves `scan` as it is, and consumes no part of a word, comment or literal that it does not finish.
56
+ // code-gauge-allow functionNcss -- One pass copies the scan state in, dispatches on its mode, and copies it back.
57
+ DIRECTIVE_INLINE DirectiveUnits directive_scan_units(DirectiveScan *scan, const char *units, uint32_t count, unsigned size, uint32_t byte, bool lines) {
58
+ DirectiveUnitPass pass = {
59
+ .units = units, .classes = scan->classes, .count = count, .quote = (uint32_t)scan->quote, .previous = scan->previous,
60
+ .mode = scan->mode, .lines = lines, .beginning = scan->beginning, .star = scan->star, .number = scan->number,
61
+ };
62
+ for (bool more = true; more;) {
63
+ switch (pass.mode) {
64
+ case SCAN_WORD:
65
+ more = directive_units_word(&pass, size);
66
+ break;
67
+ case SCAN_LINE_COMMENT:
68
+ more = directive_units_line_comment(&pass, size);
69
+ break;
70
+ case SCAN_BLOCK_COMMENT:
71
+ more = directive_units_block_comment(&pass, size);
72
+ break;
73
+ case SCAN_QUOTE:
74
+ more = directive_units_quoted(&pass, size);
75
+ break;
76
+ default:
77
+ more = directive_units_code(&pass, size);
78
+ }
79
+ }
80
+ if (!lines) return (DirectiveUnits){pass.mode == SCAN_CODE ? pass.i : pass.start, 0, 0};
81
+ if (!pass.i) return (DirectiveUnits){0};
82
+ if (pass.mode != SCAN_CODE) directive_units_note_pragma(&pass, size);
83
+ if (pass.pragma) *scan->pragma = true;
84
+ scan->mode = pass.mode;
85
+ scan->beginning = pass.beginning;
86
+ scan->star = pass.star;
87
+ scan->number = pass.number;
88
+ scan->quote = (int32_t)pass.quote;
89
+ scan->escape = false;
90
+ directive_scan_units_end(scan, units, size, pass.fresh ? pass.start : UINT32_MAX, pass.i, byte);
91
+ return (DirectiveUnits){pass.i, pass.line_feeds, pass.i - pass.line};
92
+ }
93
+
94
+ // Each of these reads the units of one mode and returns true when those of `pass->mode` follow.
95
+
96
+ 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++;
103
+ }
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
+ }
113
+
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;
121
+ }
122
+ 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;
134
+ }
135
+ pass->i++;
136
+ } else if (c == '#' && !pass->beginning) {
137
+ pass->i++;
138
+ } else {
139
+ return false;
140
+ }
141
+ pass->beginning = false;
142
+ return true;
143
+ }
144
+
145
+ // code-gauge-allow functionCognitiveComplexity -- A number continues over signs and separators that end any other word.
146
+ DIRECTIVE_INLINE bool directive_units_word(DirectiveUnitPass *pass, unsigned size) {
147
+ uint32_t c = 0;
148
+ uint8_t class;
149
+ 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
+ }
155
+ if (pass->i >= pass->count || c >= UNIT_CLASS_COUNT || c == '\\') return false;
156
+ if (!pass->number) break;
157
+ int32_t previous = pass->i > (pass->fresh ? pass->start : 0)
158
+ ? (int32_t)directive_unit(pass->units, size, pass->i - 1)
159
+ : pass->previous;
160
+ bool exponent_sign = (c == '+' || c == '-') &&
161
+ (previous == 'e' || previous == 'E' || previous == 'p' || previous == 'P');
162
+ if (!(c == '.' || c == '\'' || exponent_sign)) break;
163
+ pass->i++;
164
+ }
165
+ // The word may be a raw string prefix, which no word of more characters is.
166
+ if (c == '"' && (!pass->fresh || pass->i - pass->start < sizeof ((GnuRawStringPrefix *)0)->value)) return false;
167
+ return directive_units_close(pass, size);
168
+ }
169
+
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
+ DIRECTIVE_INLINE bool directive_units_line_comment(DirectiveUnitPass *pass, unsigned size) {
172
+ 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)) {
181
+ pass->i++;
182
+ } else if (c == '\n' && !pass->lines) {
183
+ // The comment is read, and the line feed ends the line for the caller.
184
+ pass->mode = SCAN_CODE;
185
+ return false;
186
+ } else if (c == '\n' && directive_units_line_feed(pass, size)) {
187
+ pass->beginning = true;
188
+ return directive_units_close(pass, size);
189
+ } else {
190
+ return false;
191
+ }
192
+ }
193
+ }
194
+
195
+ // 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
+ DIRECTIVE_INLINE bool directive_units_block_comment(DirectiveUnitPass *pass, unsigned size) {
198
+ 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];
203
+ bool star = pass->star;
204
+ pass->star = c == '*';
205
+ if (class & UNIT_BLOCK) {
206
+ pass->seen |= class;
207
+ pass->i++;
208
+ } else if (c == '*') {
209
+ pass->i++;
210
+ } else if (c == '/') {
211
+ pass->i++;
212
+ if (star) return directive_units_close(pass, size);
213
+ } else if (c == '\\' && directive_units_escape(pass, size)) {
214
+ pass->i++;
215
+ } else if (c != '\n' || !directive_units_line_feed(pass, size)) {
216
+ pass->star = star;
217
+ return false;
218
+ }
219
+ }
220
+ }
221
+
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
+ DIRECTIVE_INLINE bool directive_units_quoted(DirectiveUnitPass *pass, unsigned size) {
224
+ 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) {
233
+ pass->i++;
234
+ return directive_units_close(pass, size);
235
+ } else if (c == '"' || c == '\'') {
236
+ pass->i++;
237
+ } else if (c == '\\' && directive_units_escape(pass, size)) {
238
+ pass->seen |= pass->classes[directive_unit(pass->units, size, pass->i + 1)];
239
+ pass->i += 2;
240
+ } else {
241
+ return false;
242
+ }
243
+ }
244
+ }
245
+
246
+ // 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) {
248
+ pass->mode = mode;
249
+ pass->start = pass->i;
250
+ pass->fresh = true;
251
+ pass->seen = 0;
252
+ pass->i += length;
253
+ }
254
+
255
+ // Ends the word, comment or literal being read; code follows it.
256
+ DIRECTIVE_INLINE bool directive_units_close(DirectiveUnitPass *pass, unsigned size) {
257
+ directive_units_note_pragma(pass, size);
258
+ pass->mode = SCAN_CODE;
259
+ return true;
260
+ }
261
+
262
+ // Reads the line feed at `pass->i` unless the pass stops at line feeds, or a carriage return may belong to it.
263
+ DIRECTIVE_INLINE bool directive_units_line_feed(DirectiveUnitPass *pass, unsigned size) {
264
+ if (!pass->lines || pass->i + 1 >= pass->count || directive_unit(pass->units, size, pass->i + 1) == '\r') return false;
265
+ pass->line_feeds++;
266
+ pass->line = ++pass->i;
267
+ return true;
268
+ }
269
+
270
+ // Whether the backslash at `pass->i` starts no line splice and the unit after it is a plain character.
271
+ DIRECTIVE_INLINE bool directive_units_escape(const DirectiveUnitPass *pass, unsigned size) {
272
+ if (pass->i + 1 >= pass->count) return false;
273
+ uint32_t next = directive_unit(pass->units, size, pass->i + 1);
274
+ return next < UNIT_CLASS_COUNT && (pass->classes[next] & (UNIT_LINE | UNIT_SPACE)) == UNIT_LINE;
275
+ }
276
+
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;
280
+ }
281
+
282
+ // The unit at `index` of units of `size` bytes in the host's byte order.
283
+ DIRECTIVE_INLINE uint32_t directive_unit(const char *units, unsigned size, uint32_t index) {
284
+ if (size == 1) return (uint8_t)units[index];
285
+ uint16_t unit;
286
+ memcpy(&unit, units + 2 * (size_t)index, sizeof unit);
287
+ return unit;
288
+ }
289
+
290
+ // Leaves in `scan` what the character path keeps of the `end` units read: the last characters of the unfinished word,
291
+ // comment or literal, which starts at `from` or, when that is `UINT32_MAX`, before the units, and the run of spaces
292
+ // and backslashes that the units end with.
293
+ // code-gauge-allow functionCognitiveComplexity -- The characters kept and the run of spaces each depend on where their text starts among the units.
294
+ static void directive_scan_units_end(DirectiveScan *scan, const char *units, unsigned size, uint32_t from, uint32_t end, uint32_t byte) {
295
+ if (scan->mode == SCAN_CODE) {
296
+ scan->recent = 0;
297
+ } 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
+ uint64_t recent = from == UINT32_MAX ? scan->recent : 0;
300
+ uint32_t first = from == UINT32_MAX ? 0 : from;
301
+ for (uint32_t k = end - first > 8 ? end - 8 : first; k < end; k++) recent = recent << 8 | directive_unit(units, size, k);
302
+ scan->recent = recent;
303
+ if (from != UINT32_MAX && scan->mode == SCAN_WORD) scan->raw = (GnuRawStringPrefix){0};
304
+ if (scan->mode == SCAN_WORD && end > first) directive_scan_units_word(scan, units, size, first, end);
305
+ }
306
+ uint32_t run = end;
307
+ while (run > 0) {
308
+ uint32_t c = directive_unit(units, size, run - 1);
309
+ if (!directive_horizontal((int32_t)c) && c != '\n' && c != '\\') break;
310
+ run--;
311
+ }
312
+ bool continued = run == 0 && scan->in_run;
313
+ scan->in_run = run < end;
314
+ if (!scan->in_run) return;
315
+ if (!continued) {
316
+ scan->run_spans = 0;
317
+ scan->run_start = byte + run * size;
318
+ }
319
+ scan->run_end = byte + (end - 1) * size + 1;
320
+ }
321
+
322
+ // Adds the word's characters from `first` to before `end`, as `directive_scan_word_characters` does.
323
+ static void directive_scan_units_word(DirectiveScan *scan, const char *units, unsigned size, uint32_t first, uint32_t end) {
324
+ scan->previous = (int32_t)directive_unit(units, size, end - 1);
325
+ if (scan->raw.length >= sizeof scan->raw.value) return;
326
+ if (end - first > sizeof scan->raw.value - 1 - scan->raw.length) {
327
+ gnu_raw_string_prefix_invalidate(&scan->raw);
328
+ return;
329
+ }
330
+ for (uint32_t k = first; k < end; k++) gnu_raw_string_prefix_character(&scan->raw, (int32_t)directive_unit(units, size, k));
331
+ }
332
+
333
+ // Whether the ASCII units from `start` to before `end` hold `pragma_word`.
334
+ static bool directive_units_pragma(const char *units, unsigned size, uint32_t start, uint32_t end) {
335
+ uint32_t length = sizeof pragma_word - 1;
336
+ for (uint32_t i = start; i + length <= end; i++) {
337
+ uint32_t matched = 0;
338
+ while (matched < length && directive_unit(units, size, i + matched) == (uint8_t)pragma_word[matched]) matched++;
339
+ if (matched == length) return true;
340
+ }
341
+ return false;
342
+ }
343
+
344
+ // Whether the characters in `recent` end with a start of `pragma_word` that the next ones may complete.
345
+ static bool directive_pragma_pending(uint64_t recent) {
346
+ uint64_t word = 0;
347
+ for (unsigned length = 1; length < sizeof pragma_word - 1; length++) {
348
+ word = word << 8 | (uint8_t)pragma_word[length - 1];
349
+ if ((recent & ((1ull << 8 * length) - 1)) == word) return true;
350
+ }
351
+ return false;
352
+ }
353
+
354
+ // code-gauge-allow functionCognitiveComplexity -- Each class is one condition on the character.
355
+ static void directive_unit_classes(uint8_t *classes) {
356
+ for (int32_t c = 0; c < UNIT_CLASS_COUNT; c++) {
357
+ 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;
359
+ uint8_t letter = c == '_' ? UNIT_UNDERSCORE : c == 'P' ? UNIT_P : 0;
360
+ uint8_t block = text && c != '*' && c != '/' ? UNIT_BLOCK : 0, quote = text && c != '"' && c != '\'' ? UNIT_QUOTE : 0;
361
+ classes[c] = (uint8_t)(code | letter | (text ? UNIT_LINE : 0) | block | quote);
362
+ }
363
+ }
364
+
365
+ #endif