@willbooster/tree-sitter-c 42.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": "42.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,17 @@ 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;
34
38
  // Set when the characters read hold `pragma_word`, also in a comment or a literal: a scan starts inside a literal
35
39
  // after an escape sequence, and the lexer ends some comments earlier than this pass does.
36
40
  bool *pragma;
@@ -46,6 +50,7 @@ typedef struct {
46
50
 
47
51
  static bool directive_scan_leading_comment(DirectiveScan *scan, TSLexer *lexer);
48
52
  static bool directive_scan_feed(DirectiveScan *scan, int32_t c);
53
+ static bool directive_scan_view(DirectiveScan *scan, TSLexer *lexer);
49
54
  static inline bool directive_scan_plain_mode(DirectiveScan *scan, TSLexer *lexer);
50
55
  static inline bool directive_scan_plain_word(DirectiveScan *scan, TSLexer *lexer);
51
56
  static inline void directive_scan_word_characters(DirectiveScan *scan, uint64_t recent, uint32_t count);
@@ -74,6 +79,8 @@ static void directive_scan_close_run(DirectiveScan *scan, uint32_t limit, bool s
74
79
  static void directive_scan_push(DirectiveScan *scan, uint32_t start, uint32_t end);
75
80
  static inline void directive_scan_extend(DirectiveScan *scan, uint32_t start, bool skipped);
76
81
 
82
+ #include "directiveUnits.h"
83
+
77
84
  // Feeds the spaces at the lexer's position, and a `/` after them with the character that follows it. Returns true when
78
85
  // they open a comment. It reads no further than that one `/`, so that the scans after a postponed build, which start
79
86
  // where this one's tokens end, do not read the same characters again and again.
@@ -99,9 +106,30 @@ static void directive_scan_plain_run(DirectiveScan *scan, TSLexer *lexer) {
99
106
  // Only another newline can belong to the newline before it.
100
107
  if (scan->pair && directive_newline(lexer->lookahead)) return;
101
108
  scan->pair = 0;
109
+ while (directive_scan_view(scan, lexer)) {}
102
110
  while (directive_scan_plain_mode(scan, lexer)) {}
103
111
  }
104
112
 
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
+
105
133
  // Consumes the plain characters of the current mode. Returns true when those of another mode may follow.
106
134
  static inline bool directive_scan_plain_mode(DirectiveScan *scan, TSLexer *lexer) {
107
135
  switch (scan->mode) {
@@ -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
@@ -1,43 +1,10 @@
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.
1
+ // code-gauge-allow-file fileNcss -- One source-fact pass shares 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 "directiveScan.h"
5
+ #include "directiveBuffer.h"
6
6
  #include <stdlib.h>
7
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
- } DirectiveBuffer;
32
-
33
- // A position in a `DirectiveBuffer`; copies share the buffer, so a lookahead copy reads what the original will.
34
- typedef struct {
35
- DirectiveBuffer *buffer;
36
- uint32_t index, byte, position_index, previous_byte, before_previous_byte;
37
- // Whether a line splice with spaces before its newline was read, which the lexer's tokens do not take as a splice.
38
- bool spaced_splice;
39
- } DirectiveInput;
40
-
41
8
  #define DIRECTIVE_FACT_PAGE_SHIFT 15
42
9
  #define DIRECTIVE_FACT_PAGE_SIZE (1 << DIRECTIVE_FACT_PAGE_SHIFT)
43
10
 
@@ -87,18 +54,6 @@ static void directive_carry_tail(DirectiveCarry *carry, DirectiveSpans *tail, ui
87
54
  static void directive_spans_reverse(DirectiveSpans *spans);
88
55
  static void directive_mark_spans(EmptyDirectives *state, DirectiveBuffer *buffer, const DirectiveSpans *spans);
89
56
  static uint32_t directive_skip_inert(const EmptyDirectives *state, const DirectiveInput *input, uint32_t end, uint32_t position, uint32_t *byte);
90
- static inline bool directive_has(const DirectiveInput *input, uint32_t index);
91
- static bool directive_fill(DirectiveBuffer *buffer, uint32_t index);
92
- static bool directive_append(DirectiveBuffer *buffer);
93
- static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c);
94
- static inline void directive_note(DirectiveBuffer *buffer, uint32_t c);
95
- static bool directive_reserve(DirectiveBuffer *buffer, unsigned width);
96
- static bool directive_position(DirectiveBuffer *buffer, uint32_t index, uint32_t byte, uint32_t width);
97
- static void directive_encode(DirectiveBuffer *buffer, uint32_t c);
98
- static uint16_t directive_character(const DirectiveInput *input, uint32_t index);
99
- static void directive_store(DirectiveBuffer *buffer, uint16_t c);
100
- static void directive_release(DirectiveBuffer *buffer);
101
- static unsigned directive_utf8_width(uint32_t c);
102
57
  static void directive_set(EmptyDirectives *state, DirectiveBuffer *buffer, uint8_t ***facts, uint32_t byte);
103
58
  static bool directive_bit(const EmptyDirectives *state, uint8_t *const *facts, uint32_t byte);
104
59
  static bool directive_grow_directories(EmptyDirectives *state, uint32_t pages);
@@ -111,6 +66,8 @@ static void directive_code(EmptyDirectives *state, DirectiveInput *input, Direct
111
66
  static bool directive_pragma_part(DirectiveLine *line, int32_t c);
112
67
  static bool directive_neutral(const DirectiveLine *line);
113
68
  static void directive_neutral_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
69
+ static void directive_neutral_units(DirectiveInput *input);
70
+ static void directive_advance_plain(DirectiveInput *input, uint32_t length);
114
71
  static bool directive_header_ready(DirectiveLine *line, int32_t c);
115
72
  static bool directive_header(DirectiveInput *input, int32_t close);
116
73
  static void directive_line_comment(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
@@ -128,7 +85,7 @@ static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t i
128
85
  static int32_t directive_peek(const DirectiveInput *input);
129
86
  static int32_t directive_plain(const DirectiveInput *input);
130
87
  static void directive_skip_plain(DirectiveInput *input);
131
- typedef enum { DIRECTIVE_RUN_WORD, DIRECTIVE_RUN_CODE, DIRECTIVE_RUN_SPACE, DIRECTIVE_RUN_LINE_COMMENT, DIRECTIVE_RUN_BLOCK_COMMENT } DirectiveRun;
88
+ typedef enum { DIRECTIVE_RUN_WORD, DIRECTIVE_RUN_SPACE, DIRECTIVE_RUN_LINE_COMMENT, DIRECTIVE_RUN_BLOCK_COMMENT } DirectiveRun;
132
89
  static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun run);
133
90
  static uint32_t directive_run_end(const DirectiveBuffer *buffer, DirectiveRun run, uint32_t index, uint32_t end);
134
91
  static bool directive_run_continues(DirectiveRun run, uint16_t c);
@@ -269,11 +226,12 @@ static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
269
226
  DirectiveInput input = {.buffer = &buffer, .byte = position.bytes};
270
227
  // `run` holds the run before a segment's `#`, and `tail` the segment's trailing run.
271
228
  DirectiveSpans run = {0}, tail = {0};
272
- DirectiveScan scan = {.beginning = true, .run = &run, .pragma = &buffer.pragma};
229
+ DirectiveScan scan = {.beginning = true, .run = &run, .classes = buffer.classes, .pragma = &buffer.pragma};
273
230
  if (directive_scan_leading_comment(&scan, lexer)) {
274
231
  free(run.items);
275
232
  return false;
276
233
  }
234
+ directive_unit_classes(buffer.classes);
277
235
  for (;;) {
278
236
  if (!directive_scan_buffered(&scan, &input)) {
279
237
  buffer.length = buffer.position_count = 0;
@@ -299,7 +257,7 @@ static void directive_scan_resume(DirectiveScan *scan, DirectiveSpans *run, Dire
299
257
  DirectiveSpans before = *run;
300
258
  *run = *tail;
301
259
  *tail = before;
302
- *scan = (DirectiveScan){.beginning = true, .run = run, .pragma = scan->pragma};
260
+ *scan = (DirectiveScan){.beginning = true, .run = run, .classes = scan->classes, .pragma = scan->pragma};
303
261
  if (!run->count) return;
304
262
  DirectiveSpan last = run->items[--run->count];
305
263
  scan->run_spans = run->count;
@@ -540,135 +498,6 @@ static uint32_t directive_skip_inert(const EmptyDirectives *state, const Directi
540
498
  return end;
541
499
  }
542
500
 
543
- // Reads characters from the lexer until `index` is stored; false at the end of the input.
544
- static inline bool directive_has(const DirectiveInput *input, uint32_t index) {
545
- return index < input->buffer->length || directive_fill(input->buffer, index);
546
- }
547
-
548
- // Reads ahead to the end of the line or `DIRECTIVE_FILL_UNITS` characters past `index`, since the pass reads each
549
- // stored character several times and the characters it leaves behind go back to `DirectiveScan`.
550
- static bool directive_fill(DirectiveBuffer *buffer, uint32_t index) {
551
- uint32_t limit = index + DIRECTIVE_FILL_UNITS;
552
- while (buffer->length < limit && !buffer->failed && !pragma_eof(buffer->lexer)) {
553
- int32_t c = buffer->lexer->lookahead;
554
- if (!directive_append(buffer)) buffer->failed = true;
555
- else if (directive_newline(c) && buffer->length > index) break;
556
- }
557
- return index < buffer->length;
558
- }
559
-
560
- static bool directive_append(DirectiveBuffer *buffer) {
561
- TSLexer *lexer = buffer->lexer;
562
- uint32_t c = lexer->lookahead, index = buffer->length, start = buffer->fill_byte;
563
- if (c < 0x80 && buffer->encoding_known && (index >> DIRECTIVE_PAGE_SHIFT) < buffer->page_count) {
564
- return directive_append_ascii(buffer, (uint16_t)c);
565
- }
566
- unsigned width = directive_utf8_width(c), utf16_width = c > 0xffff && c <= 0x10ffff ? 4 : 2;
567
- if (!directive_reserve(buffer, c > 0xffff && c <= 0x10ffff ? 2 : 1)) return false;
568
- directive_encode(buffer, c);
569
- directive_note(buffer, c);
570
- TSLexerPosition end;
571
- lexer->advance(lexer, false);
572
- lexer->get_position(lexer, &end);
573
- buffer->fill_byte = end.bytes;
574
- uint32_t measured = end.bytes - start;
575
- // A character stored before the encoding is known keeps its measured width, which a later choice cannot change.
576
- bool known = buffer->encoding_known;
577
- if (!known && c <= 0x10ffff && width != utf16_width && !lexer->is_at_included_range_start(lexer)) {
578
- buffer->utf16 = measured == utf16_width;
579
- buffer->encoding_known = true;
580
- }
581
- if (!known || c > 0x10ffff || measured != (buffer->utf16 ? utf16_width : width)) {
582
- return directive_position(buffer, index, start, measured);
583
- }
584
- return true;
585
- }
586
-
587
- static bool directive_position(DirectiveBuffer *buffer, uint32_t index, uint32_t byte, uint32_t width) {
588
- if (buffer->position_count == buffer->position_capacity) {
589
- uint32_t capacity = buffer->position_capacity ? buffer->position_capacity * 2 : 16;
590
- if (capacity < buffer->position_capacity || (size_t)capacity > SIZE_MAX / sizeof(*buffer->positions)) return false;
591
- void *positions = realloc(buffer->positions, (size_t)capacity * sizeof(*buffer->positions));
592
- if (!positions) return false;
593
- buffer->positions = positions;
594
- buffer->position_capacity = capacity;
595
- }
596
- buffer->positions[buffer->position_count++] = (DirectivePosition){index, byte, width};
597
- return true;
598
- }
599
-
600
- // An ASCII character on a stored page, once the encoding is known, takes the expected width unless the lexer skipped to
601
- // another included range after it.
602
- static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c) {
603
- TSLexer *lexer = buffer->lexer;
604
- uint32_t index = buffer->length, start = buffer->fill_byte;
605
- buffer->characters[index >> DIRECTIVE_PAGE_SHIFT][index & (DIRECTIVE_PAGE_SIZE - 1)] = c;
606
- buffer->length = index + 1;
607
- directive_note(buffer, c);
608
- TSLexerPosition end;
609
- lexer->advance(lexer, false);
610
- lexer->get_position(lexer, &end);
611
- buffer->fill_byte = end.bytes;
612
- return end.bytes - start == (buffer->utf16 ? 2u : 1u) || directive_position(buffer, index, start, end.bytes - start);
613
- }
614
-
615
- static inline void directive_note(DirectiveBuffer *buffer, uint32_t c) {
616
- buffer->recent = buffer->recent << 8 | (c < 0x80 ? c : 0);
617
- if (directive_pragma_word(buffer->recent)) buffer->pragma = true;
618
- }
619
-
620
- static bool directive_reserve(DirectiveBuffer *buffer, unsigned width) {
621
- if (buffer->length > UINT32_MAX - width) return false;
622
- uint32_t last = (buffer->length + width - 1) >> DIRECTIVE_PAGE_SHIFT;
623
- if (last >= buffer->page_capacity) {
624
- uint32_t next = buffer->page_capacity ? buffer->page_capacity * 2 : 16;
625
- if (next < buffer->page_capacity || (size_t)next > SIZE_MAX / sizeof(*buffer->characters)) return false;
626
- void *characters = realloc(buffer->characters, (size_t)next * sizeof(*buffer->characters));
627
- if (!characters) return false;
628
- buffer->characters = characters;
629
- buffer->page_capacity = next;
630
- }
631
- while (buffer->page_count <= last) {
632
- uint16_t *page = malloc((size_t)DIRECTIVE_PAGE_SIZE * sizeof(*page));
633
- if (!page) return false;
634
- buffer->characters[buffer->page_count++] = page;
635
- }
636
- return true;
637
- }
638
-
639
- // Only the two units of a character above U+FFFF form a surrogate pair. A decoding error is stored as U+FFFD with the
640
- // recorded position `directive_append` gives it, and a lone surrogate as U+E000, which `preproc_word` and `pragma_space`
641
- // classify alike, so that neither joins a neighbouring lone surrogate into a character the lexer did not read.
642
- static void directive_encode(DirectiveBuffer *buffer, uint32_t c) {
643
- if (c > 0x10ffff) directive_store(buffer, 0xfffd);
644
- else if (c >= 0xd800 && c <= 0xdfff) directive_store(buffer, 0xe000);
645
- else if (c <= 0xffff) directive_store(buffer, c);
646
- else {
647
- c -= 0x10000;
648
- directive_store(buffer, 0xd800 | (c >> 10));
649
- directive_store(buffer, 0xdc00 | (c & 0x3ff));
650
- }
651
- }
652
-
653
- static uint16_t directive_character(const DirectiveInput *input, uint32_t index) {
654
- return input->buffer->characters[index >> DIRECTIVE_PAGE_SHIFT][index & (DIRECTIVE_PAGE_SIZE - 1)];
655
- }
656
-
657
- static void directive_store(DirectiveBuffer *buffer, uint16_t c) {
658
- buffer->characters[buffer->length >> DIRECTIVE_PAGE_SHIFT][buffer->length & (DIRECTIVE_PAGE_SIZE - 1)] = c;
659
- buffer->length++;
660
- }
661
-
662
- static void directive_release(DirectiveBuffer *buffer) {
663
- for (uint32_t i = 0; i < buffer->page_count; i++) free(buffer->characters[i]);
664
- free(buffer->characters);
665
- free(buffer->positions);
666
- }
667
-
668
- static unsigned directive_utf8_width(uint32_t c) {
669
- return c < 0x80 || c > 0x10ffff ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
670
- }
671
-
672
501
  static void directive_set(EmptyDirectives *state, DirectiveBuffer *buffer, uint8_t ***facts, uint32_t byte) {
673
502
  if (buffer->failed) return;
674
503
  uint32_t page = byte >> DIRECTIVE_FACT_PAGE_SHIFT;
@@ -770,23 +599,39 @@ static bool directive_neutral(const DirectiveLine *line) {
770
599
  return settled && !line->pending && !line->pragma_depth && !line->pragma_pending && !line->header;
771
600
  }
772
601
 
773
- // Also consumes the following characters that leave the line state as it is, which is every plain character but `/`.
602
+ // Also consumes the stored characters after the token that leave the line state as it is.
774
603
  static void directive_neutral_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c) {
775
604
  line->operand = true;
776
605
  line->beginning = false;
606
+ if (c == '"' || c == '\'') {
607
+ if (directive_quoted(input, c)) {
608
+ directive_line_end(state, input, line);
609
+ return;
610
+ }
611
+ } else if (preproc_word(c, false) || (c >= '0' && c <= '9')) {
612
+ directive_word(input, c, true, false);
613
+ }
614
+ directive_neutral_units(input);
615
+ }
616
+
617
+ // Consumes the tokens stored after `input` up to the line feed or to the first one that needs more than its units, as
618
+ // `directive_line_character` would on a neutral line: `directive_scan_units` reads a line with no directive state, and
619
+ // a neutral line keeps its state through every token.
620
+ static void directive_neutral_units(DirectiveInput *input) {
621
+ const DirectiveBuffer *buffer = input->buffer;
777
622
  for (;;) {
778
- if (c == '"' || c == '\'') {
779
- if (directive_quoted(input, c)) {
780
- directive_line_end(state, input, line);
781
- return;
782
- }
783
- } else if (preproc_word(c, false) || (c >= '0' && c <= '9')) {
784
- directive_word(input, c, true, false);
623
+ uint32_t index = input->index, end = buffer->length;
624
+ if (input->position_index < buffer->position_count && buffer->positions[input->position_index].index < end) {
625
+ end = buffer->positions[input->position_index].index;
785
626
  }
786
- directive_plain_run(input, DIRECTIVE_RUN_CODE);
787
- c = directive_plain(input);
788
- if (c < 0 || c == '/') return;
789
- directive_skip_plain(input);
627
+ uint32_t page_end = (index | (DIRECTIVE_PAGE_SIZE - 1)) + 1;
628
+ if (page_end < end) end = page_end;
629
+ if (index >= end) return;
630
+ DirectiveScan scan = {.classes = buffer->classes};
631
+ const uint16_t *units = buffer->characters[index >> DIRECTIVE_PAGE_SHIFT] + (index & (DIRECTIVE_PAGE_SIZE - 1));
632
+ uint32_t length = directive_scan_units(&scan, (const char *)units, end - index, 2, 0, false).consumed;
633
+ if (length) directive_advance_plain(input, length);
634
+ if (length < end - index) return;
790
635
  }
791
636
  }
792
637
 
@@ -982,13 +827,18 @@ static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun r
982
827
  index = directive_run_end(buffer, run, index, end);
983
828
  if (index < end || index == start || end < buffer->length) break;
984
829
  }
985
- uint32_t length = index - input->index, width = buffer->utf16 ? 2 : 1;
986
- if (!length) return 0;
830
+ uint32_t length = index - input->index;
831
+ if (length) directive_advance_plain(input, length);
832
+ return length;
833
+ }
834
+
835
+ // Moves `input` over `length` plain characters, at least one.
836
+ static void directive_advance_plain(DirectiveInput *input, uint32_t length) {
837
+ uint32_t width = input->buffer->utf16 ? 2 : 1;
987
838
  input->before_previous_byte = length > 1 ? input->byte + (length - 2) * width : input->previous_byte;
988
839
  input->previous_byte = input->byte + (length - 1) * width;
989
840
  input->byte += length * width;
990
- input->index = index;
991
- return length;
841
+ input->index += length;
992
842
  }
993
843
 
994
844
  // The index of the first stored character from `index` to `end` that ends the run, or `end`.
@@ -1011,9 +861,6 @@ static bool directive_run_continues(DirectiveRun run, uint16_t c) {
1011
861
  switch (run) {
1012
862
  case DIRECTIVE_RUN_WORD:
1013
863
  return identifier;
1014
- case DIRECTIVE_RUN_CODE:
1015
- // The rest of a neutral line: characters that start no token.
1016
- return !identifier && c != '"' && c != '\'' && c != '/';
1017
864
  case DIRECTIVE_RUN_SPACE:
1018
865
  return directive_horizontal(c);
1019
866
  case DIRECTIVE_RUN_LINE_COMMENT:
Binary file
package/tree-sitter.json CHANGED
@@ -20,12 +20,14 @@
20
20
  "src/conditionalMacro.h",
21
21
  "src/emptyDirectives.h",
22
22
  "src/directiveScan.h",
23
+ "src/directiveUnits.h",
24
+ "src/directiveBuffer.h",
23
25
  "src/gnuRawString.h"
24
26
  ]
25
27
  }
26
28
  ],
27
29
  "metadata": {
28
- "version": "42.0.0",
30
+ "version": "43.0.0",
29
31
  "license": "MIT",
30
32
  "description": "C grammar for tree-sitter",
31
33
  "authors": [