@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.
package/README.md CHANGED
@@ -24,7 +24,7 @@ the trees that error recovery produces may change; such changes are released as
24
24
  ## Usage
25
25
 
26
26
  The npm package ships `tree-sitter-c.wasm` for
27
- [@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version 23.0.0 or later within 23.x, which runs in Node.js, Bun, browsers, and Cloudflare Workers. The parser carries the ABI number of the WillBooster/tree-sitter generator (`0x5742000d`), so it loads only with that runtime; upstream tree-sitter runtimes reject it. In Node.js and Bun, load the grammar from its path:
27
+ [@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version 23.1.0 or later within 23.x, which runs in Node.js, Bun, browsers, and Cloudflare Workers. The parser carries the ABI number of the WillBooster/tree-sitter generator (`0x5742000e`), so it loads only with that runtime; upstream tree-sitter runtimes reject it. In Node.js and Bun, load the grammar from its path:
28
28
 
29
29
  ```js
30
30
  import { fileURLToPath } from 'node:url';
@@ -66,9 +66,12 @@ parser.setLanguage(await Language.load(c));
66
66
  The package also ships `grammar.js`, scanner sources under `src/`,
67
67
  and the node types in `src/node-types.json` for grammars extending C (such as C++).
68
68
  The scanner sources include `scanner.c`, `pragma.h`, `identifier.h`, `keywords.h`, `functionMacro.h`,
69
- `conditionalMacro.h`, `emptyDirectives.h`, `directiveScan.h`, `macroProof.h`, and `gnuRawString.h`.
69
+ `conditionalMacro.h`, `emptyDirectives.h`, `directiveBuffer.h`, `directiveScan.h`, `directiveUnits.h`, `macroProof.h`, and
70
+ `gnuRawString.h`.
70
71
 
71
72
  Derived grammars must port the inherited external-token dispatch from `src/scanner.c`.
73
+ The headers call `peek_units` and `advance_units` of `TSLexer`, so generate the derived parser with the CLI of
74
+ WillBooster/tree-sitter 23.1.0 or later, whose `src/tree_sitter/parser.h` declares them, and run it on that runtime.
72
75
  Append `_directive_context` and `preproc_empty` in their inherited external-token slots and retain them as extras.
73
76
  Append `_preproc_directive_start` in its inherited slot as a zero-width guard after every directive keyword token,
74
77
  including keywords in overridden directive rules. It is not an extra. Retain the phase-two splice handling in
@@ -211,10 +214,10 @@ arrays, parenthesized/function declarators, initializers and multiple declarator
211
214
 
212
215
  In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-c) and on
213
216
  [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
214
- fuzzed with, at 23.x (`Cargo.lock` records the exact version):
217
+ fuzzed with, at 23.1 or later within 23.x (`Cargo.lock` records the exact version):
215
218
 
216
219
  ```sh
217
- cargo add willbooster-tree-sitter@23 --rename tree-sitter
220
+ cargo add willbooster-tree-sitter@23.1 --rename tree-sitter
218
221
  cargo add willbooster-tree-sitter-c --rename tree-sitter-c
219
222
  ```
220
223
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-c",
3
- "version": "41.0.0",
3
+ "version": "43.0.0",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "c",
@@ -28,6 +28,8 @@
28
28
  "src/conditionalMacro.h",
29
29
  "src/emptyDirectives.h",
30
30
  "src/directiveScan.h",
31
+ "src/directiveUnits.h",
32
+ "src/directiveBuffer.h",
31
33
  "src/gnuRawString.h",
32
34
  "src/scanner.c"
33
35
  ],
@@ -60,7 +62,7 @@
60
62
  "@vitest/browser-playwright": "5.1.0",
61
63
  "@willbooster/oxlint-config": "1.4.9",
62
64
  "@willbooster/wb": "23.6.1",
63
- "@willbooster/web-tree-sitter": "23.0.2",
65
+ "@willbooster/web-tree-sitter": "23.1.0",
64
66
  "conventional-changelog-conventionalcommits": "10.4.1",
65
67
  "lefthook": "2.2.2",
66
68
  "oxlint": "1.93.0",
@@ -71,7 +73,7 @@
71
73
  "wrangler": "4.156.0"
72
74
  },
73
75
  "peerDependencies": {
74
- "@willbooster/web-tree-sitter": "^23.0.0"
76
+ "@willbooster/web-tree-sitter": "^23.1.0"
75
77
  },
76
78
  "peerDependenciesMeta": {
77
79
  "@willbooster/web-tree-sitter": {
@@ -0,0 +1,219 @@
1
+ // The characters of a directive segment, read from the lexer as the line-state pass of `emptyDirectives.h` needs them.
2
+ #ifndef TREE_SITTER_C_DIRECTIVE_BUFFER_H_
3
+ #define TREE_SITTER_C_DIRECTIVE_BUFFER_H_
4
+
5
+ #include "directiveScan.h"
6
+ #include <stdlib.h>
7
+
8
+ #define DIRECTIVE_PAGE_SHIFT 12
9
+ #define DIRECTIVE_PAGE_SIZE (1 << DIRECTIVE_PAGE_SHIFT)
10
+ #define DIRECTIVE_SEGMENT_UNITS (DIRECTIVE_PAGE_SIZE * 16)
11
+ #define DIRECTIVE_FILL_UNITS 256
12
+
13
+ typedef struct {
14
+ uint32_t index, byte, width;
15
+ } DirectivePosition;
16
+
17
+ // The characters of one directive segment, read from the lexer as the line-state pass needs them.
18
+ typedef struct {
19
+ uint16_t **characters;
20
+ DirectivePosition *positions;
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;
25
+ uint32_t conditional_depth;
26
+ // The characters stored last, as `DirectiveScan` keeps those it reads.
27
+ uint64_t recent;
28
+ // Whether the current segment recorded a null directive or a line-end barrier, which the reverse pass carries, and
29
+ // whether the characters stored or scanned hold `pragma_word`.
30
+ bool utf16, encoding_known, failed, carries, pragma;
31
+ uint8_t classes[UNIT_CLASS_COUNT];
32
+ } DirectiveBuffer;
33
+
34
+ // A position in a `DirectiveBuffer`; copies share the buffer, so a lookahead copy reads what the original will.
35
+ typedef struct {
36
+ DirectiveBuffer *buffer;
37
+ uint32_t index, byte, position_index, previous_byte, before_previous_byte;
38
+ // Whether a line splice with spaces before its newline was read, which the lexer's tokens do not take as a splice.
39
+ bool spaced_splice;
40
+ } DirectiveInput;
41
+
42
+ static inline bool directive_has(const DirectiveInput *input, uint32_t index);
43
+ static bool directive_fill(DirectiveBuffer *buffer, uint32_t index);
44
+ static uint32_t directive_fill_units(DirectiveBuffer *buffer, uint32_t limit);
45
+ static bool directive_append(DirectiveBuffer *buffer);
46
+ static bool directive_position(DirectiveBuffer *buffer, uint32_t index, uint32_t byte, uint32_t width);
47
+ static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c);
48
+ static inline void directive_note(DirectiveBuffer *buffer, uint32_t c);
49
+ static bool directive_reserve(DirectiveBuffer *buffer, unsigned width);
50
+ static void directive_encode(DirectiveBuffer *buffer, uint32_t c);
51
+ static uint16_t directive_character(const DirectiveInput *input, uint32_t index);
52
+ static void directive_store(DirectiveBuffer *buffer, uint16_t c);
53
+ static void directive_release(DirectiveBuffer *buffer);
54
+ static unsigned directive_utf8_width(uint32_t c);
55
+
56
+ // Reads characters from the lexer until `index` is stored; false at the end of the input.
57
+ static inline bool directive_has(const DirectiveInput *input, uint32_t index) {
58
+ return index < input->buffer->length || directive_fill(input->buffer, index);
59
+ }
60
+
61
+ // Reads ahead to the end of the line or `DIRECTIVE_FILL_UNITS` characters past `index`, since the pass reads each
62
+ // stored character several times and the characters it leaves behind go back to `DirectiveScan`.
63
+ static bool directive_fill(DirectiveBuffer *buffer, uint32_t index) {
64
+ uint32_t limit = index + DIRECTIVE_FILL_UNITS;
65
+ while (buffer->length < limit && !buffer->failed && !pragma_eof(buffer->lexer)) {
66
+ int32_t c = buffer->lexer->lookahead;
67
+ if (directive_fill_units(buffer, limit)) c = directive_character(&(DirectiveInput){.buffer = buffer}, buffer->length - 1);
68
+ else if (!directive_append(buffer)) buffer->failed = true;
69
+ if (!buffer->failed && directive_newline(c) && buffer->length > index) break;
70
+ }
71
+ return index < buffer->length;
72
+ }
73
+
74
+ // Stores the ASCII characters that the lexer hands out as units, up to the first newline, the end of the page or
75
+ // `limit` characters, as `directive_append` stores them one at a time, and returns how many. It stores none where a
76
+ // character may take another width than its encoding's (before the encoding is known, and at the start of an included
77
+ // range, whose first character follows bytes that are left out) or may complete a `pragma_word` stored before.
78
+ // code-gauge-allow functionCognitiveComplexity -- Each bound on the characters stored at once is a condition of its own.
79
+ // code-gauge-allow functionNcss -- Each bound on the characters stored at once is a condition of its own.
80
+ static uint32_t directive_fill_units(DirectiveBuffer *buffer, uint32_t limit) {
81
+ TSLexer *lexer = buffer->lexer;
82
+ uint32_t index = buffer->length, count, size;
83
+ if (!buffer->encoding_known || (index >> DIRECTIVE_PAGE_SHIFT) >= buffer->page_count || directive_pragma_pending(buffer->recent)) return 0;
84
+ const char *units = lexer->peek_units(lexer, &count, &size);
85
+ if (!count || size != (buffer->utf16 ? 2u : 1u) || lexer->is_at_included_range_start(lexer)) return 0;
86
+ uint32_t room = DIRECTIVE_PAGE_SIZE - (index & (DIRECTIVE_PAGE_SIZE - 1));
87
+ if (count > room) count = room;
88
+ if (count > limit - index) count = limit - index;
89
+ uint16_t *page = buffer->characters[index >> DIRECTIVE_PAGE_SHIFT] + (index & (DIRECTIVE_PAGE_SIZE - 1));
90
+ uint32_t length = 0, c = 0;
91
+ uint8_t seen = 0;
92
+ while (length < count && (c = directive_unit(units, size, length)) < UNIT_CLASS_COUNT) {
93
+ page[length++] = (uint16_t)c;
94
+ seen |= buffer->classes[c];
95
+ if (directive_newline((int32_t)c)) break;
96
+ }
97
+ if (!length) return 0;
98
+ if ((seen & UNIT_PRAGMA) == UNIT_PRAGMA && directive_units_pragma(units, size, 0, length)) buffer->pragma = true;
99
+ for (uint32_t i = length > 8 ? length - 8 : 0; i < length; i++) buffer->recent = buffer->recent << 8 | page[i];
100
+ buffer->length = index + length;
101
+ buffer->fill_byte += length * size;
102
+ bool line_feed = page[length - 1] == '\n';
103
+ lexer->advance_units(lexer, length, line_feed, line_feed ? 0 : length);
104
+ return length;
105
+ }
106
+
107
+ static bool directive_append(DirectiveBuffer *buffer) {
108
+ TSLexer *lexer = buffer->lexer;
109
+ uint32_t c = lexer->lookahead, index = buffer->length, start = buffer->fill_byte;
110
+ if (c < 0x80 && buffer->encoding_known && (index >> DIRECTIVE_PAGE_SHIFT) < buffer->page_count) {
111
+ return directive_append_ascii(buffer, (uint16_t)c);
112
+ }
113
+ unsigned width = directive_utf8_width(c), utf16_width = c > 0xffff && c <= 0x10ffff ? 4 : 2;
114
+ if (!directive_reserve(buffer, c > 0xffff && c <= 0x10ffff ? 2 : 1)) return false;
115
+ directive_encode(buffer, c);
116
+ directive_note(buffer, c);
117
+ TSLexerPosition end;
118
+ lexer->advance(lexer, false);
119
+ lexer->get_position(lexer, &end);
120
+ buffer->fill_byte = end.bytes;
121
+ uint32_t measured = end.bytes - start;
122
+ // A character stored before the encoding is known keeps its measured width, which a later choice cannot change.
123
+ bool known = buffer->encoding_known;
124
+ if (!known && c <= 0x10ffff && width != utf16_width && !lexer->is_at_included_range_start(lexer)) {
125
+ buffer->utf16 = measured == utf16_width;
126
+ buffer->encoding_known = true;
127
+ }
128
+ if (!known || c > 0x10ffff || measured != (buffer->utf16 ? utf16_width : width)) {
129
+ return directive_position(buffer, index, start, measured);
130
+ }
131
+ return true;
132
+ }
133
+
134
+ static bool directive_position(DirectiveBuffer *buffer, uint32_t index, uint32_t byte, uint32_t width) {
135
+ if (buffer->position_count == buffer->position_capacity) {
136
+ uint32_t capacity = buffer->position_capacity ? buffer->position_capacity * 2 : 16;
137
+ if (capacity < buffer->position_capacity || (size_t)capacity > SIZE_MAX / sizeof(*buffer->positions)) return false;
138
+ void *positions = realloc(buffer->positions, (size_t)capacity * sizeof(*buffer->positions));
139
+ if (!positions) return false;
140
+ buffer->positions = positions;
141
+ buffer->position_capacity = capacity;
142
+ }
143
+ buffer->positions[buffer->position_count++] = (DirectivePosition){index, byte, width};
144
+ return true;
145
+ }
146
+
147
+ // An ASCII character on a stored page, once the encoding is known, takes the expected width unless the lexer skipped to
148
+ // another included range after it.
149
+ static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c) {
150
+ TSLexer *lexer = buffer->lexer;
151
+ uint32_t index = buffer->length, start = buffer->fill_byte;
152
+ buffer->characters[index >> DIRECTIVE_PAGE_SHIFT][index & (DIRECTIVE_PAGE_SIZE - 1)] = c;
153
+ buffer->length = index + 1;
154
+ directive_note(buffer, c);
155
+ TSLexerPosition end;
156
+ lexer->advance(lexer, false);
157
+ lexer->get_position(lexer, &end);
158
+ buffer->fill_byte = end.bytes;
159
+ return end.bytes - start == (buffer->utf16 ? 2u : 1u) || directive_position(buffer, index, start, end.bytes - start);
160
+ }
161
+
162
+ static inline void directive_note(DirectiveBuffer *buffer, uint32_t c) {
163
+ buffer->recent = buffer->recent << 8 | (c < 0x80 ? c : 0);
164
+ if (directive_pragma_word(buffer->recent)) buffer->pragma = true;
165
+ }
166
+
167
+ static bool directive_reserve(DirectiveBuffer *buffer, unsigned width) {
168
+ if (buffer->length > UINT32_MAX - width) return false;
169
+ uint32_t last = (buffer->length + width - 1) >> DIRECTIVE_PAGE_SHIFT;
170
+ if (last >= buffer->page_capacity) {
171
+ uint32_t next = buffer->page_capacity ? buffer->page_capacity * 2 : 16;
172
+ if (next < buffer->page_capacity || (size_t)next > SIZE_MAX / sizeof(*buffer->characters)) return false;
173
+ void *characters = realloc(buffer->characters, (size_t)next * sizeof(*buffer->characters));
174
+ if (!characters) return false;
175
+ buffer->characters = characters;
176
+ buffer->page_capacity = next;
177
+ }
178
+ while (buffer->page_count <= last) {
179
+ uint16_t *page = malloc((size_t)DIRECTIVE_PAGE_SIZE * sizeof(*page));
180
+ if (!page) return false;
181
+ buffer->characters[buffer->page_count++] = page;
182
+ }
183
+ return true;
184
+ }
185
+
186
+ // Only the two units of a character above U+FFFF form a surrogate pair. A decoding error is stored as U+FFFD with the
187
+ // recorded position `directive_append` gives it, and a lone surrogate as U+E000, which `preproc_word` and `pragma_space`
188
+ // classify alike, so that neither joins a neighbouring lone surrogate into a character the lexer did not read.
189
+ static void directive_encode(DirectiveBuffer *buffer, uint32_t c) {
190
+ if (c > 0x10ffff) directive_store(buffer, 0xfffd);
191
+ else if (c >= 0xd800 && c <= 0xdfff) directive_store(buffer, 0xe000);
192
+ else if (c <= 0xffff) directive_store(buffer, c);
193
+ else {
194
+ c -= 0x10000;
195
+ directive_store(buffer, 0xd800 | (c >> 10));
196
+ directive_store(buffer, 0xdc00 | (c & 0x3ff));
197
+ }
198
+ }
199
+
200
+ static uint16_t directive_character(const DirectiveInput *input, uint32_t index) {
201
+ return input->buffer->characters[index >> DIRECTIVE_PAGE_SHIFT][index & (DIRECTIVE_PAGE_SIZE - 1)];
202
+ }
203
+
204
+ static void directive_store(DirectiveBuffer *buffer, uint16_t c) {
205
+ buffer->characters[buffer->length >> DIRECTIVE_PAGE_SHIFT][buffer->length & (DIRECTIVE_PAGE_SIZE - 1)] = c;
206
+ buffer->length++;
207
+ }
208
+
209
+ static void directive_release(DirectiveBuffer *buffer) {
210
+ for (uint32_t i = 0; i < buffer->page_count; i++) free(buffer->characters[i]);
211
+ free(buffer->characters);
212
+ free(buffer->positions);
213
+ }
214
+
215
+ static unsigned directive_utf8_width(uint32_t c) {
216
+ return c < 0x80 || c > 0x10ffff ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
217
+ }
218
+
219
+ #endif
@@ -24,13 +24,23 @@ typedef struct {
24
24
  typedef enum { SCAN_CODE, SCAN_SLASH, SCAN_LINE_COMMENT, SCAN_BLOCK_COMMENT, SCAN_QUOTE, SCAN_WORD, SCAN_RAW_DELIMITER, SCAN_RAW_BODY } DirectiveScanMode;
25
25
 
26
26
  // The line-state pass of `emptyDirectives.h` in the state it has on ordinary code, before the first directive and after
27
- // each directive line that ends with no directive state left, fed one physical character at a time so that the code is
28
- // never stored. It also tracks the run of spaces and backslashes before the next character, which a null directive's
29
- // prefix covers: the first `run_spans` spans in `run` and the current one, from `run_start` to before `run_end`.
27
+ // each directive line that ends with no directive state left. It is fed one physical character at a time
28
+ // (`directive_scan_feed`), from the lexer or from the characters that a directive segment's lookahead stored past its
29
+ // end, or over a stretch of the lexer's code units (`directive_scan_units`), which leaves it as those characters fed
30
+ // one by one would; no other ordinary code is stored. It also tracks the run of spaces and backslashes before the next
31
+ // character, which a null directive's prefix covers: the first `run_spans` spans in `run` and the current one, from
32
+ // `run_start` to before `run_end`.
30
33
  typedef struct {
31
34
  DirectiveScanMode mode;
32
35
  GnuRawStringPrefix raw;
33
36
  DirectiveSpans *run;
37
+ const uint8_t *classes;
38
+ // Set when the characters read hold `pragma_word`, also in a comment or a literal: a scan starts inside a literal
39
+ // after an escape sequence, and the lexer ends some comments earlier than this pass does.
40
+ bool *pragma;
41
+ // The characters read last, the latest in the low byte and zero for one that is not ASCII. Code outside words is
42
+ // left out, so a word's first character restarts them.
43
+ uint64_t recent;
34
44
  int32_t quote, previous, pair;
35
45
  uint32_t spaces, run_start, run_end, run_spans;
36
46
  unsigned delimiter_length, closing_length, closing_end;
@@ -38,13 +48,20 @@ typedef struct {
38
48
  bool beginning, number, escape, star, splice, in_run;
39
49
  } DirectiveScan;
40
50
 
41
- static inline bool directive_scan_plain(DirectiveScan *scan, TSLexer *lexer);
42
- static inline bool directive_scan_plain_word(DirectiveScan *scan, TSLexer *lexer, int32_t c);
43
- static inline bool directive_scan_plain_code(DirectiveScan *scan, TSLexer *lexer, int32_t c);
44
- static inline bool directive_scan_plain_text(DirectiveScan *scan, TSLexer *lexer, int32_t c);
51
+ static bool directive_scan_leading_comment(DirectiveScan *scan, TSLexer *lexer);
52
+ static bool directive_scan_feed(DirectiveScan *scan, int32_t c);
53
+ static bool directive_scan_view(DirectiveScan *scan, TSLexer *lexer);
54
+ static inline bool directive_scan_plain_mode(DirectiveScan *scan, TSLexer *lexer);
55
+ static inline bool directive_scan_plain_word(DirectiveScan *scan, TSLexer *lexer);
56
+ static inline void directive_scan_word_characters(DirectiveScan *scan, uint64_t recent, uint32_t count);
57
+ static inline bool directive_pragma_word(uint64_t recent);
58
+ static inline uint64_t directive_scan_note(DirectiveScan *scan, uint64_t recent, int32_t c);
59
+ static inline bool directive_scan_plain_code(DirectiveScan *scan, TSLexer *lexer);
60
+ static inline bool directive_scan_plain_block_comment(DirectiveScan *scan, TSLexer *lexer);
61
+ static inline void directive_scan_plain_text(DirectiveScan *scan, TSLexer *lexer, int32_t end, bool raw);
45
62
  static inline void directive_scan_lexer_run(DirectiveScan *scan, TSLexer *lexer, bool member);
46
63
  static inline bool directive_scan_ascii_word(int32_t c);
47
- static inline bool directive_scan_ascii_special(int32_t c);
64
+ static inline bool directive_scan_ascii_plain(int32_t c);
48
65
  static bool directive_scan_pending(DirectiveScan *scan, int32_t c);
49
66
  static bool directive_scan_splice(DirectiveScan *scan, int32_t c);
50
67
  static bool directive_scan_logical(DirectiveScan *scan, int32_t c);
@@ -62,71 +79,160 @@ static void directive_scan_close_run(DirectiveScan *scan, uint32_t limit, bool s
62
79
  static void directive_scan_push(DirectiveScan *scan, uint32_t start, uint32_t end);
63
80
  static inline void directive_scan_extend(DirectiveScan *scan, uint32_t start, bool skipped);
64
81
 
82
+ #include "directiveUnits.h"
83
+
84
+ // Feeds the spaces at the lexer's position, and a `/` after them with the character that follows it. Returns true when
85
+ // they open a comment. It reads no further than that one `/`, so that the scans after a postponed build, which start
86
+ // where this one's tokens end, do not read the same characters again and again.
87
+ static bool directive_scan_leading_comment(DirectiveScan *scan, TSLexer *lexer) {
88
+ for (bool slash = false;;) {
89
+ if (scan->mode == SCAN_LINE_COMMENT || scan->mode == SCAN_BLOCK_COMMENT) return true;
90
+ int32_t c = lexer->lookahead;
91
+ bool space = !slash && scan->mode == SCAN_CODE && !scan->splice && (pragma_space(c) || c == '/');
92
+ if ((!space && scan->mode != SCAN_SLASH) || pragma_eof(lexer)) return false;
93
+ slash = slash || c == '/';
94
+ directive_scan_feed(scan, c);
95
+ directive_scan_lexer_run(scan, lexer, pragma_space(c) || c == '\\');
96
+ lexer->advance(lexer, false);
97
+ }
98
+ }
99
+
65
100
  // Consumes the lexer's ASCII characters that need no physical-layer work and change no state but the line beginning,
66
101
  // the current word, the end of a comment and the run of spaces, which is most of the input, and stops at a character
67
- // that `directive_scan_feed` has to handle.
68
- static void directive_scan_plain_run(DirectiveScan *target, TSLexer *lexer) {
69
- if (target->pair || target->splice) return;
70
- // The lexer's calls cannot reach this copy, so it can stay in registers.
71
- DirectiveScan copy = *target;
72
- while (directive_scan_plain(&copy, lexer)) lexer->advance(lexer, false);
73
- *target = copy;
102
+ // that `directive_scan_feed` has to handle. Each mode has a loop of its own, which keeps what it reads in registers
103
+ // across the lexer's calls.
104
+ static void directive_scan_plain_run(DirectiveScan *scan, TSLexer *lexer) {
105
+ if (scan->splice) return;
106
+ // Only another newline can belong to the newline before it.
107
+ if (scan->pair && directive_newline(lexer->lookahead)) return;
108
+ scan->pair = 0;
109
+ while (directive_scan_view(scan, lexer)) {}
110
+ while (directive_scan_plain_mode(scan, lexer)) {}
74
111
  }
75
112
 
76
- static inline bool directive_scan_plain(DirectiveScan *scan, TSLexer *lexer) {
77
- int32_t c = lexer->lookahead;
78
- if (c <= 0 || c >= 0x80) return false;
113
+ // Reads ordinary code from the units the lexer hands out, a stretch at a time. Returns false where the character path
114
+ // has to go on: the units do not see a `pragma_word` that started before them, nor the bytes that an included range
115
+ // leaves out before its first character.
116
+ static bool directive_scan_view(DirectiveScan *scan, TSLexer *lexer) {
117
+ DirectiveScanMode mode = scan->mode;
118
+ bool readable = mode == SCAN_CODE || mode == SCAN_WORD || mode == SCAN_LINE_COMMENT || mode == SCAN_BLOCK_COMMENT ||
119
+ (mode == SCAN_QUOTE && !scan->escape);
120
+ if (!readable || (mode != SCAN_CODE && directive_pragma_pending(scan->recent))) return false;
121
+ uint32_t count, size;
122
+ const char *units = lexer->peek_units(lexer, &count, &size);
123
+ if (count < 2 || lexer->is_at_included_range_start(lexer)) return false;
124
+ TSLexerPosition position;
125
+ lexer->get_position(lexer, &position);
126
+ DirectiveUnits run = size == 1
127
+ ? directive_scan_units(scan, units, count, 1, position.bytes, true)
128
+ : directive_scan_units(scan, units, count, 2, position.bytes, true);
129
+ lexer->advance_units(lexer, run.consumed, run.line_feeds, run.tail);
130
+ return run.consumed == count;
131
+ }
132
+
133
+ // Consumes the plain characters of the current mode. Returns true when those of another mode may follow.
134
+ static inline bool directive_scan_plain_mode(DirectiveScan *scan, TSLexer *lexer) {
79
135
  switch (scan->mode) {
80
136
  case SCAN_WORD:
81
- return directive_scan_plain_word(scan, lexer, c);
137
+ return directive_scan_plain_word(scan, lexer);
82
138
  case SCAN_CODE:
83
- return directive_scan_plain_code(scan, lexer, c);
84
- case SCAN_LINE_COMMENT:
85
- return directive_scan_plain_text(scan, lexer, c);
139
+ return directive_scan_plain_code(scan, lexer);
86
140
  case SCAN_BLOCK_COMMENT:
87
- if (!directive_scan_plain_text(scan, lexer, c)) return false;
88
- if (scan->star && c == '/') scan->mode = SCAN_CODE;
89
- scan->star = c == '*';
90
- return true;
141
+ return directive_scan_plain_block_comment(scan, lexer);
142
+ case SCAN_LINE_COMMENT:
143
+ directive_scan_plain_text(scan, lexer, 0, false);
144
+ return false;
91
145
  case SCAN_QUOTE:
92
- return !scan->escape && c != scan->quote && directive_scan_plain_text(scan, lexer, c);
146
+ if (!scan->escape) directive_scan_plain_text(scan, lexer, scan->quote, false);
147
+ return false;
93
148
  case SCAN_RAW_BODY:
94
- if (c == '"' || !directive_scan_plain_text(scan, lexer, c)) return false;
95
- directive_scan_raw_body(scan, c);
96
- return true;
149
+ directive_scan_plain_text(scan, lexer, '"', true);
150
+ return false;
97
151
  default:
98
152
  return false;
99
153
  }
100
154
  }
101
155
 
102
- static inline bool directive_scan_plain_word(DirectiveScan *scan, TSLexer *lexer, int32_t c) {
103
- if (!directive_scan_ascii_word(c)) {
104
- // A line splice may continue the word, and the others may continue a number or open a raw string.
105
- if (directive_scan_ascii_special(c) || (scan->number && (c == '.' || c == '+' || c == '-'))) return false;
106
- scan->mode = SCAN_CODE;
107
- return directive_scan_plain_code(scan, lexer, c);
156
+ // Consumes the rest of the word. Returns true when code follows it.
157
+ static inline bool directive_scan_plain_word(DirectiveScan *scan, TSLexer *lexer) {
158
+ int32_t c = lexer->lookahead;
159
+ uint64_t recent = scan->recent;
160
+ uint32_t count = 0;
161
+ for (; directive_scan_ascii_word(c); c = lexer->lookahead) {
162
+ recent = recent << 8 | (uint64_t)c;
163
+ count++;
164
+ lexer->advance(lexer, false);
108
165
  }
109
- scan->in_run = false;
110
- scan->previous = c;
111
- if (scan->raw.length < sizeof scan->raw.value) gnu_raw_string_prefix_character(&scan->raw, c);
166
+ if (count) directive_scan_word_characters(scan, recent, count);
167
+ // A line splice may continue the word, and the others may continue a number or open a raw string.
168
+ if (!directive_scan_ascii_plain(c) || (scan->number && (c == '.' || c == '+' || c == '-'))) return false;
169
+ scan->mode = SCAN_CODE;
112
170
  return true;
113
171
  }
114
172
 
115
- static inline bool directive_scan_plain_code(DirectiveScan *scan, TSLexer *lexer, int32_t c) {
116
- if (directive_scan_ascii_special(c)) return false;
117
- bool space = directive_horizontal(c);
118
- directive_scan_lexer_run(scan, lexer, space);
119
- if (space) return true;
120
- scan->beginning = false;
121
- if (directive_scan_ascii_word(c)) directive_scan_word_start(scan, c);
122
- return true;
173
+ // Adds the word's next `count` characters, the last ones of `recent`, as `directive_scan_word` does one at a time: a
174
+ // prefix that would exceed three characters is no raw string prefix whatever they are.
175
+ static inline void directive_scan_word_characters(DirectiveScan *scan, uint64_t recent, uint32_t count) {
176
+ scan->in_run = false;
177
+ scan->previous = (int32_t)(recent & 0xff);
178
+ scan->recent = recent;
179
+ if (directive_pragma_word(recent)) *scan->pragma = true;
180
+ if (scan->raw.length >= sizeof scan->raw.value) return;
181
+ if (count > sizeof scan->raw.value - 1 - scan->raw.length) {
182
+ gnu_raw_string_prefix_invalidate(&scan->raw);
183
+ return;
184
+ }
185
+ while (count--) gnu_raw_string_prefix_character(&scan->raw, (int32_t)(recent >> (8 * count) & 0xff));
123
186
  }
124
187
 
125
- // The body of a comment or a literal.
126
- static inline bool directive_scan_plain_text(DirectiveScan *scan, TSLexer *lexer, int32_t c) {
127
- if (c == '\n' || c == '\r' || c == '\\') return false;
128
- directive_scan_lexer_run(scan, lexer, directive_horizontal(c));
129
- return true;
188
+ // Consumes code up to a word. Returns true when a word follows, whose first character is consumed.
189
+ static inline bool directive_scan_plain_code(DirectiveScan *scan, TSLexer *lexer) {
190
+ for (;;) {
191
+ int32_t c = lexer->lookahead;
192
+ if (!directive_scan_ascii_plain(c)) return false;
193
+ bool space = directive_horizontal(c);
194
+ directive_scan_lexer_run(scan, lexer, space);
195
+ lexer->advance(lexer, false);
196
+ if (space) continue;
197
+ scan->beginning = false;
198
+ if (!directive_scan_ascii_word(c)) continue;
199
+ directive_scan_word_start(scan, c);
200
+ if (scan->mode == SCAN_WORD) return true;
201
+ }
202
+ }
203
+
204
+ // Consumes the comment's body. Returns true when the comment ends.
205
+ static inline bool directive_scan_plain_block_comment(DirectiveScan *scan, TSLexer *lexer) {
206
+ bool star = scan->star, closed = false;
207
+ uint64_t recent = scan->recent;
208
+ while (!closed) {
209
+ int32_t c = lexer->lookahead;
210
+ if (c <= 0 || c >= 0x80 || c == '\n' || c == '\r' || c == '\\') break;
211
+ directive_scan_lexer_run(scan, lexer, directive_horizontal(c));
212
+ recent = directive_scan_note(scan, recent, c);
213
+ lexer->advance(lexer, false);
214
+ closed = star && c == '/';
215
+ star = c == '*';
216
+ }
217
+ scan->recent = recent;
218
+ scan->star = star;
219
+ if (closed) scan->mode = SCAN_CODE;
220
+ return closed;
221
+ }
222
+
223
+ // Consumes the body of a line comment or a literal up to `end`, the closing search of a `raw` string seeing each
224
+ // character.
225
+ static inline void directive_scan_plain_text(DirectiveScan *scan, TSLexer *lexer, int32_t end, bool raw) {
226
+ uint64_t recent = scan->recent;
227
+ for (;;) {
228
+ int32_t c = lexer->lookahead;
229
+ if (c <= 0 || c >= 0x80 || c == '\n' || c == '\r' || c == '\\' || c == end) break;
230
+ directive_scan_lexer_run(scan, lexer, directive_horizontal(c));
231
+ if (raw) directive_scan_raw_body(scan, c);
232
+ recent = directive_scan_note(scan, recent, c);
233
+ lexer->advance(lexer, false);
234
+ }
235
+ scan->recent = recent;
130
236
  }
131
237
 
132
238
  // Extends or ends the run of spaces and backslashes with the lookahead, as `directive_scan_run` does. A span read from
@@ -167,16 +273,16 @@ static void directive_scan_push(DirectiveScan *scan, uint32_t start, uint32_t en
167
273
  }
168
274
 
169
275
  static inline bool directive_scan_ascii_word(int32_t c) {
170
- return c != '\\' && ((preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1);
276
+ return (uint32_t)c < 0x80 && c != '\\' && ((preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1);
171
277
  }
172
278
 
173
- // NUL, newlines, `\`, `/`, quotes and `#`, which code leaves to `directive_scan_feed`.
174
- static inline bool directive_scan_ascii_special(int32_t c) {
279
+ // ASCII but NUL, newlines, `\`, `/`, quotes and `#`, which code leaves to `directive_scan_feed`.
280
+ static inline bool directive_scan_ascii_plain(int32_t c) {
175
281
  static const uint64_t special[] = {
176
282
  1ull | (1ull << '\n') | (1ull << '\r') | (1ull << '"') | (1ull << '#') | (1ull << '\'') | (1ull << '/'),
177
283
  1ull << ('\\' - 64),
178
284
  };
179
- return (special[c / 64] >> (c % 64)) & 1;
285
+ return (uint32_t)c < 0x80 && !((special[c / 64] >> (c % 64)) & 1);
180
286
  }
181
287
 
182
288
  // Extends or ends the run with the stored character `c` from `start` to `end`.
@@ -193,6 +299,7 @@ static void directive_scan_run(DirectiveScan *scan, int32_t c, uint32_t start, u
193
299
  // `directive_encode`) and returns true when `c` starts a directive, which the caller leaves unconsumed.
194
300
  static bool directive_scan_feed(DirectiveScan *scan, int32_t c) {
195
301
  if ((uint32_t)c > 0x10ffff) c = 0xfffd;
302
+ scan->recent = directive_scan_note(scan, scan->recent, c);
196
303
  if (scan->mode == SCAN_RAW_BODY) {
197
304
  directive_scan_raw_body(scan, c);
198
305
  return false;
@@ -311,6 +418,7 @@ static void directive_scan_word_start(DirectiveScan *scan, int32_t c) {
311
418
  scan->mode = SCAN_WORD;
312
419
  scan->number = c >= '0' && c <= '9';
313
420
  scan->previous = c;
421
+ scan->recent = c < 0x80 ? (uint64_t)c : 0;
314
422
  scan->raw = (GnuRawStringPrefix){0};
315
423
  gnu_raw_string_prefix_character(&scan->raw, c);
316
424
  }
@@ -370,6 +478,20 @@ static void directive_scan_raw_body(DirectiveScan *scan, int32_t c) {
370
478
  scan->mode = SCAN_CODE;
371
479
  }
372
480
 
481
+ // Adds `c` to the characters read last and returns them.
482
+ static inline uint64_t directive_scan_note(DirectiveScan *scan, uint64_t recent, int32_t c) {
483
+ recent = recent << 8 | ((uint32_t)c < 0x80 ? (uint64_t)c : 0);
484
+ if (directive_pragma_word(recent)) *scan->pragma = true;
485
+ return recent;
486
+ }
487
+
488
+ // Whether the characters in `recent`, the last one in the low byte, end in `pragma_word`.
489
+ static inline bool directive_pragma_word(uint64_t recent) {
490
+ uint64_t word = 0;
491
+ for (unsigned i = 0; i < sizeof pragma_word - 1; i++) word = word << 8 | (uint8_t)pragma_word[i];
492
+ return (recent & ((1ull << 8 * (sizeof pragma_word - 1)) - 1)) == word;
493
+ }
494
+
373
495
  static bool directive_horizontal(int32_t c) {
374
496
  return c == ' ' || c == '\t' || c == '\v' || c == '\f';
375
497
  }