@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 +7 -4
- package/package.json +5 -3
- package/src/directiveBuffer.h +219 -0
- package/src/directiveScan.h +178 -56
- package/src/directiveUnits.h +365 -0
- package/src/emptyDirectives.h +106 -207
- package/src/scanner.c +22 -48
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +3 -1
package/src/emptyDirectives.h
CHANGED
|
@@ -1,40 +1,10 @@
|
|
|
1
|
-
// code-gauge-allow-file fileNcss -- One source-fact pass shares
|
|
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 "
|
|
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
|
-
// Whether the current segment recorded a null directive or a line-end barrier, which the reverse pass carries.
|
|
27
|
-
bool utf16, encoding_known, failed, carries;
|
|
28
|
-
} DirectiveBuffer;
|
|
29
|
-
|
|
30
|
-
// A position in a `DirectiveBuffer`; copies share the buffer, so a lookahead copy reads what the original will.
|
|
31
|
-
typedef struct {
|
|
32
|
-
DirectiveBuffer *buffer;
|
|
33
|
-
uint32_t index, byte, position_index, previous_byte, before_previous_byte;
|
|
34
|
-
// Whether a line splice with spaces before its newline was read, which the lexer's tokens do not take as a splice.
|
|
35
|
-
bool spaced_splice;
|
|
36
|
-
} DirectiveInput;
|
|
37
|
-
|
|
38
8
|
#define DIRECTIVE_FACT_PAGE_SHIFT 15
|
|
39
9
|
#define DIRECTIVE_FACT_PAGE_SIZE (1 << DIRECTIVE_FACT_PAGE_SHIFT)
|
|
40
10
|
|
|
@@ -45,7 +15,10 @@ typedef struct {
|
|
|
45
15
|
typedef struct {
|
|
46
16
|
uint8_t **starts, **ends, **comments, **line_ends, **directive_starts, **directive_ends;
|
|
47
17
|
uint32_t pages;
|
|
48
|
-
|
|
18
|
+
// `anchored` and `ready` belong to the scanner state: whether the token at the start of the input is out, and
|
|
19
|
+
// whether the facts, which `built` says exist, are in use. `pragmas` is the fact that the input read for them holds
|
|
20
|
+
// `pragma_word`, without which no scan has to look for a `_Pragma` operator.
|
|
21
|
+
bool built, anchored, ready, pragmas;
|
|
49
22
|
} EmptyDirectives;
|
|
50
23
|
|
|
51
24
|
typedef enum { DIRECTIVE_ORDINARY, DIRECTIVE_CONDITION, DIRECTIVE_DIAGNOSTIC, DIRECTIVE_REQUIRED, DIRECTIVE_UNDEF, DIRECTIVE_LINE, DIRECTIVE_PRAGMA, DIRECTIVE_HEADER } DirectiveKind;
|
|
@@ -73,7 +46,7 @@ static bool directive_segment_ends(const DirectiveInput *input);
|
|
|
73
46
|
static bool directive_short_line_comment(const DirectiveInput *input, uint32_t index, uint32_t limit);
|
|
74
47
|
static bool directive_line_clean(const DirectiveLine *line);
|
|
75
48
|
static void directive_shift(DirectiveBuffer *buffer, uint32_t start);
|
|
76
|
-
static
|
|
49
|
+
static void directive_finish(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveSpans *run, DirectiveSpans *tail);
|
|
77
50
|
static void directive_prefixes(EmptyDirectives *state, const DirectiveInput *input, const DirectiveSpans *prefix, DirectiveSpans *tail);
|
|
78
51
|
static bool directive_step_back(const DirectiveInput *input, uint32_t *index, uint32_t *position, uint32_t *byte, int32_t *c, uint32_t *width);
|
|
79
52
|
static void directive_carry(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveCarry *carry, int32_t c, uint32_t byte);
|
|
@@ -81,17 +54,6 @@ static void directive_carry_tail(DirectiveCarry *carry, DirectiveSpans *tail, ui
|
|
|
81
54
|
static void directive_spans_reverse(DirectiveSpans *spans);
|
|
82
55
|
static void directive_mark_spans(EmptyDirectives *state, DirectiveBuffer *buffer, const DirectiveSpans *spans);
|
|
83
56
|
static uint32_t directive_skip_inert(const EmptyDirectives *state, const DirectiveInput *input, uint32_t end, uint32_t position, uint32_t *byte);
|
|
84
|
-
static inline bool directive_has(const DirectiveInput *input, uint32_t index);
|
|
85
|
-
static bool directive_fill(DirectiveBuffer *buffer, uint32_t index);
|
|
86
|
-
static bool directive_append(DirectiveBuffer *buffer);
|
|
87
|
-
static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c);
|
|
88
|
-
static bool directive_reserve(DirectiveBuffer *buffer, unsigned width);
|
|
89
|
-
static bool directive_position(DirectiveBuffer *buffer, uint32_t index, uint32_t byte, uint32_t width);
|
|
90
|
-
static void directive_encode(DirectiveBuffer *buffer, uint32_t c);
|
|
91
|
-
static uint16_t directive_character(const DirectiveInput *input, uint32_t index);
|
|
92
|
-
static void directive_store(DirectiveBuffer *buffer, uint16_t c);
|
|
93
|
-
static void directive_release(DirectiveBuffer *buffer);
|
|
94
|
-
static unsigned directive_utf8_width(uint32_t c);
|
|
95
57
|
static void directive_set(EmptyDirectives *state, DirectiveBuffer *buffer, uint8_t ***facts, uint32_t byte);
|
|
96
58
|
static bool directive_bit(const EmptyDirectives *state, uint8_t *const *facts, uint32_t byte);
|
|
97
59
|
static bool directive_grow_directories(EmptyDirectives *state, uint32_t pages);
|
|
@@ -104,6 +66,8 @@ static void directive_code(EmptyDirectives *state, DirectiveInput *input, Direct
|
|
|
104
66
|
static bool directive_pragma_part(DirectiveLine *line, int32_t c);
|
|
105
67
|
static bool directive_neutral(const DirectiveLine *line);
|
|
106
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);
|
|
107
71
|
static bool directive_header_ready(DirectiveLine *line, int32_t c);
|
|
108
72
|
static bool directive_header(DirectiveInput *input, int32_t close);
|
|
109
73
|
static void directive_line_comment(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
|
|
@@ -121,7 +85,7 @@ static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t i
|
|
|
121
85
|
static int32_t directive_peek(const DirectiveInput *input);
|
|
122
86
|
static int32_t directive_plain(const DirectiveInput *input);
|
|
123
87
|
static void directive_skip_plain(DirectiveInput *input);
|
|
124
|
-
typedef enum { DIRECTIVE_RUN_WORD,
|
|
88
|
+
typedef enum { DIRECTIVE_RUN_WORD, DIRECTIVE_RUN_SPACE, DIRECTIVE_RUN_LINE_COMMENT, DIRECTIVE_RUN_BLOCK_COMMENT } DirectiveRun;
|
|
125
89
|
static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun run);
|
|
126
90
|
static uint32_t directive_run_end(const DirectiveBuffer *buffer, DirectiveRun run, uint32_t index, uint32_t end);
|
|
127
91
|
static bool directive_run_continues(DirectiveRun run, uint16_t c);
|
|
@@ -147,16 +111,19 @@ static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool na
|
|
|
147
111
|
static uint32_t directive_byte(const DirectiveInput *input);
|
|
148
112
|
static void directive_record(EmptyDirectives *state, DirectiveInput *input, const DirectiveLine *line, uint32_t end);
|
|
149
113
|
|
|
150
|
-
|
|
114
|
+
// Scans the zero-width token `symbol` that starts the directive context: once at the start of the input, where it
|
|
115
|
+
// anchors the tree, and, when comments lead the input, once more where the facts are built after them. Returns false
|
|
116
|
+
// while a comment is still ahead, having consumed characters.
|
|
117
|
+
static bool empty_directives_initialize(EmptyDirectives *state, TSLexer *lexer, TSSymbol symbol) {
|
|
151
118
|
TSLexerCheckpoint start;
|
|
152
119
|
lexer->get_checkpoint(lexer, &start);
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
state->ready =
|
|
120
|
+
lexer->result_symbol = symbol;
|
|
121
|
+
bool anchored = state->anchored;
|
|
122
|
+
state->anchored = true;
|
|
123
|
+
if (!state->built) state->built = empty_directives_build(state, lexer);
|
|
124
|
+
state->ready = state->built;
|
|
158
125
|
lexer->mark_end_at(lexer, &start);
|
|
159
|
-
return
|
|
126
|
+
return state->ready || !anchored;
|
|
160
127
|
}
|
|
161
128
|
|
|
162
129
|
static bool empty_directives_after_keyword(const EmptyDirectives *state, TSLexer *lexer) {
|
|
@@ -166,6 +133,25 @@ static bool empty_directives_after_keyword(const EmptyDirectives *state, TSLexer
|
|
|
166
133
|
return directive_bit(state, state->directive_ends, position.bytes);
|
|
167
134
|
}
|
|
168
135
|
|
|
136
|
+
// False only when the facts show that no `_Pragma` operator can follow any position.
|
|
137
|
+
static bool empty_directives_pragma_possible(const EmptyDirectives *state) {
|
|
138
|
+
return !state || !state->built || state->pragmas;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Whether none of the `count` tokens, at least eight, is valid but those whose byte in `extras` is 0xff.
|
|
142
|
+
static inline bool empty_directives_only_valid(const bool *valid_symbols, const uint8_t *extras, unsigned count) {
|
|
143
|
+
_Static_assert(sizeof(bool) == 1, "valid_symbols is read as bytes");
|
|
144
|
+
uint64_t others = 0;
|
|
145
|
+
for (unsigned i = 0; i < count; i += 8) {
|
|
146
|
+
unsigned offset = i + 8 > count ? count - 8 : i;
|
|
147
|
+
uint64_t valid, extra;
|
|
148
|
+
memcpy(&valid, valid_symbols + offset, sizeof valid);
|
|
149
|
+
memcpy(&extra, extras + offset, sizeof extra);
|
|
150
|
+
others |= valid & ~extra;
|
|
151
|
+
}
|
|
152
|
+
return !others;
|
|
153
|
+
}
|
|
154
|
+
|
|
169
155
|
static bool empty_directives_at_start(const EmptyDirectives *state, TSLexer *lexer) {
|
|
170
156
|
if (!state || !state->directive_starts) return false;
|
|
171
157
|
TSLexerPosition position;
|
|
@@ -224,6 +210,15 @@ static void empty_directives_consume_started(TSLexer *lexer, EmptyDirectives *st
|
|
|
224
210
|
// Ordinary code goes through `DirectiveScan` without being stored, and only directive segments, each from a directive's
|
|
225
211
|
// `#` to the end of the logical line where no directive state is left, are stored for the line-state pass and the
|
|
226
212
|
// reverse prefix pass. The facts are stored as `EmptyDirectives` describes.
|
|
213
|
+
//
|
|
214
|
+
// Returns false without building when a comment follows the lexer's position after spaces only, which leaves the build
|
|
215
|
+
// to the scan after the comment's token. That scan starts from the initial state where the lexer ends the comment: the
|
|
216
|
+
// state this pass has there too, unless the lexer ends the comment earlier, as at a backslash that spaces separate
|
|
217
|
+
// from its newline, and then what follows is read as the lexer's tokens take it and not as comment text.
|
|
218
|
+
//
|
|
219
|
+
// A build from the start of an input that begins with a block comment would read to the end of the input in a scan
|
|
220
|
+
// that consumes nothing, which the runtime takes for a scanner stuck in an unterminated comment: it then reads the
|
|
221
|
+
// whole input once more and guards every later scan.
|
|
227
222
|
static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
|
|
228
223
|
TSLexerPosition position;
|
|
229
224
|
lexer->get_position(lexer, &position);
|
|
@@ -231,7 +226,12 @@ static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
|
|
|
231
226
|
DirectiveInput input = {.buffer = &buffer, .byte = position.bytes};
|
|
232
227
|
// `run` holds the run before a segment's `#`, and `tail` the segment's trailing run.
|
|
233
228
|
DirectiveSpans run = {0}, tail = {0};
|
|
234
|
-
DirectiveScan scan = {.beginning = true, .run = &run};
|
|
229
|
+
DirectiveScan scan = {.beginning = true, .run = &run, .classes = buffer.classes, .pragma = &buffer.pragma};
|
|
230
|
+
if (directive_scan_leading_comment(&scan, lexer)) {
|
|
231
|
+
free(run.items);
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
directive_unit_classes(buffer.classes);
|
|
235
235
|
for (;;) {
|
|
236
236
|
if (!directive_scan_buffered(&scan, &input)) {
|
|
237
237
|
buffer.length = buffer.position_count = 0;
|
|
@@ -247,10 +247,8 @@ static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
|
|
|
247
247
|
if (!more || buffer.failed) break;
|
|
248
248
|
directive_scan_resume(&scan, &run, &tail);
|
|
249
249
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
free(tail.items);
|
|
253
|
-
return directive_finish(state, &buffer);
|
|
250
|
+
directive_finish(state, &buffer, &run, &tail);
|
|
251
|
+
return true;
|
|
254
252
|
}
|
|
255
253
|
|
|
256
254
|
// Restarts `scan` on ordinary code, continuing the run with a segment's trailing run, whose spans `tail` holds in byte
|
|
@@ -259,7 +257,7 @@ static void directive_scan_resume(DirectiveScan *scan, DirectiveSpans *run, Dire
|
|
|
259
257
|
DirectiveSpans before = *run;
|
|
260
258
|
*run = *tail;
|
|
261
259
|
*tail = before;
|
|
262
|
-
*scan = (DirectiveScan){.beginning = true, .run = run};
|
|
260
|
+
*scan = (DirectiveScan){.beginning = true, .run = run, .classes = scan->classes, .pragma = scan->pragma};
|
|
263
261
|
if (!run->count) return;
|
|
264
262
|
DirectiveSpan last = run->items[--run->count];
|
|
265
263
|
scan->run_spans = run->count;
|
|
@@ -379,11 +377,14 @@ static void directive_shift(DirectiveBuffer *buffer, uint32_t start) {
|
|
|
379
377
|
buffer->position_count = count;
|
|
380
378
|
}
|
|
381
379
|
|
|
382
|
-
|
|
380
|
+
// Without all the facts, none is kept, and a `_Pragma` operator may be anywhere.
|
|
381
|
+
static void directive_finish(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveSpans *run, DirectiveSpans *tail) {
|
|
382
|
+
bool failed = buffer->failed || run->failed || tail->failed;
|
|
383
|
+
free(run->items);
|
|
384
|
+
free(tail->items);
|
|
383
385
|
directive_release(buffer);
|
|
384
|
-
|
|
385
|
-
empty_directives_release(state);
|
|
386
|
-
return false;
|
|
386
|
+
state->pragmas = buffer->pragma || failed;
|
|
387
|
+
if (failed) empty_directives_release(state);
|
|
387
388
|
}
|
|
388
389
|
|
|
389
390
|
static void empty_directives_release(EmptyDirectives *state) {
|
|
@@ -497,128 +498,6 @@ static uint32_t directive_skip_inert(const EmptyDirectives *state, const Directi
|
|
|
497
498
|
return end;
|
|
498
499
|
}
|
|
499
500
|
|
|
500
|
-
// Reads characters from the lexer until `index` is stored; false at the end of the input.
|
|
501
|
-
static inline bool directive_has(const DirectiveInput *input, uint32_t index) {
|
|
502
|
-
return index < input->buffer->length || directive_fill(input->buffer, index);
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
// Reads ahead to the end of the line or `DIRECTIVE_FILL_UNITS` characters past `index`, since the pass reads each
|
|
506
|
-
// stored character several times and the characters it leaves behind go back to `DirectiveScan`.
|
|
507
|
-
static bool directive_fill(DirectiveBuffer *buffer, uint32_t index) {
|
|
508
|
-
uint32_t limit = index + DIRECTIVE_FILL_UNITS;
|
|
509
|
-
while (buffer->length < limit && !buffer->failed && !pragma_eof(buffer->lexer)) {
|
|
510
|
-
int32_t c = buffer->lexer->lookahead;
|
|
511
|
-
if (!directive_append(buffer)) buffer->failed = true;
|
|
512
|
-
else if (directive_newline(c) && buffer->length > index) break;
|
|
513
|
-
}
|
|
514
|
-
return index < buffer->length;
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
static bool directive_append(DirectiveBuffer *buffer) {
|
|
518
|
-
TSLexer *lexer = buffer->lexer;
|
|
519
|
-
uint32_t c = lexer->lookahead, index = buffer->length, start = buffer->fill_byte;
|
|
520
|
-
if (c < 0x80 && buffer->encoding_known && (index >> DIRECTIVE_PAGE_SHIFT) < buffer->page_count) {
|
|
521
|
-
return directive_append_ascii(buffer, (uint16_t)c);
|
|
522
|
-
}
|
|
523
|
-
unsigned width = directive_utf8_width(c), utf16_width = c > 0xffff && c <= 0x10ffff ? 4 : 2;
|
|
524
|
-
if (!directive_reserve(buffer, c > 0xffff && c <= 0x10ffff ? 2 : 1)) return false;
|
|
525
|
-
directive_encode(buffer, c);
|
|
526
|
-
TSLexerPosition end;
|
|
527
|
-
lexer->advance(lexer, false);
|
|
528
|
-
lexer->get_position(lexer, &end);
|
|
529
|
-
buffer->fill_byte = end.bytes;
|
|
530
|
-
uint32_t measured = end.bytes - start;
|
|
531
|
-
// A character stored before the encoding is known keeps its measured width, which a later choice cannot change.
|
|
532
|
-
bool known = buffer->encoding_known;
|
|
533
|
-
if (!known && c <= 0x10ffff && width != utf16_width && !lexer->is_at_included_range_start(lexer)) {
|
|
534
|
-
buffer->utf16 = measured == utf16_width;
|
|
535
|
-
buffer->encoding_known = true;
|
|
536
|
-
}
|
|
537
|
-
if (!known || c > 0x10ffff || measured != (buffer->utf16 ? utf16_width : width)) {
|
|
538
|
-
return directive_position(buffer, index, start, measured);
|
|
539
|
-
}
|
|
540
|
-
return true;
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
static bool directive_position(DirectiveBuffer *buffer, uint32_t index, uint32_t byte, uint32_t width) {
|
|
544
|
-
if (buffer->position_count == buffer->position_capacity) {
|
|
545
|
-
uint32_t capacity = buffer->position_capacity ? buffer->position_capacity * 2 : 16;
|
|
546
|
-
if (capacity < buffer->position_capacity || (size_t)capacity > SIZE_MAX / sizeof(*buffer->positions)) return false;
|
|
547
|
-
void *positions = realloc(buffer->positions, (size_t)capacity * sizeof(*buffer->positions));
|
|
548
|
-
if (!positions) return false;
|
|
549
|
-
buffer->positions = positions;
|
|
550
|
-
buffer->position_capacity = capacity;
|
|
551
|
-
}
|
|
552
|
-
buffer->positions[buffer->position_count++] = (DirectivePosition){index, byte, width};
|
|
553
|
-
return true;
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
// An ASCII character on a stored page, once the encoding is known, takes the expected width unless the lexer skipped to
|
|
557
|
-
// another included range after it.
|
|
558
|
-
static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c) {
|
|
559
|
-
TSLexer *lexer = buffer->lexer;
|
|
560
|
-
uint32_t index = buffer->length, start = buffer->fill_byte;
|
|
561
|
-
buffer->characters[index >> DIRECTIVE_PAGE_SHIFT][index & (DIRECTIVE_PAGE_SIZE - 1)] = c;
|
|
562
|
-
buffer->length = index + 1;
|
|
563
|
-
TSLexerPosition end;
|
|
564
|
-
lexer->advance(lexer, false);
|
|
565
|
-
lexer->get_position(lexer, &end);
|
|
566
|
-
buffer->fill_byte = end.bytes;
|
|
567
|
-
return end.bytes - start == (buffer->utf16 ? 2u : 1u) || directive_position(buffer, index, start, end.bytes - start);
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
static bool directive_reserve(DirectiveBuffer *buffer, unsigned width) {
|
|
571
|
-
if (buffer->length > UINT32_MAX - width) return false;
|
|
572
|
-
uint32_t last = (buffer->length + width - 1) >> DIRECTIVE_PAGE_SHIFT;
|
|
573
|
-
if (last >= buffer->page_capacity) {
|
|
574
|
-
uint32_t next = buffer->page_capacity ? buffer->page_capacity * 2 : 16;
|
|
575
|
-
if (next < buffer->page_capacity || (size_t)next > SIZE_MAX / sizeof(*buffer->characters)) return false;
|
|
576
|
-
void *characters = realloc(buffer->characters, (size_t)next * sizeof(*buffer->characters));
|
|
577
|
-
if (!characters) return false;
|
|
578
|
-
buffer->characters = characters;
|
|
579
|
-
buffer->page_capacity = next;
|
|
580
|
-
}
|
|
581
|
-
while (buffer->page_count <= last) {
|
|
582
|
-
uint16_t *page = malloc((size_t)DIRECTIVE_PAGE_SIZE * sizeof(*page));
|
|
583
|
-
if (!page) return false;
|
|
584
|
-
buffer->characters[buffer->page_count++] = page;
|
|
585
|
-
}
|
|
586
|
-
return true;
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
// Only the two units of a character above U+FFFF form a surrogate pair. A decoding error is stored as U+FFFD with the
|
|
590
|
-
// recorded position `directive_append` gives it, and a lone surrogate as U+E000, which `preproc_word` and `pragma_space`
|
|
591
|
-
// classify alike, so that neither joins a neighbouring lone surrogate into a character the lexer did not read.
|
|
592
|
-
static void directive_encode(DirectiveBuffer *buffer, uint32_t c) {
|
|
593
|
-
if (c > 0x10ffff) directive_store(buffer, 0xfffd);
|
|
594
|
-
else if (c >= 0xd800 && c <= 0xdfff) directive_store(buffer, 0xe000);
|
|
595
|
-
else if (c <= 0xffff) directive_store(buffer, c);
|
|
596
|
-
else {
|
|
597
|
-
c -= 0x10000;
|
|
598
|
-
directive_store(buffer, 0xd800 | (c >> 10));
|
|
599
|
-
directive_store(buffer, 0xdc00 | (c & 0x3ff));
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
static uint16_t directive_character(const DirectiveInput *input, uint32_t index) {
|
|
604
|
-
return input->buffer->characters[index >> DIRECTIVE_PAGE_SHIFT][index & (DIRECTIVE_PAGE_SIZE - 1)];
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
static void directive_store(DirectiveBuffer *buffer, uint16_t c) {
|
|
608
|
-
buffer->characters[buffer->length >> DIRECTIVE_PAGE_SHIFT][buffer->length & (DIRECTIVE_PAGE_SIZE - 1)] = c;
|
|
609
|
-
buffer->length++;
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
static void directive_release(DirectiveBuffer *buffer) {
|
|
613
|
-
for (uint32_t i = 0; i < buffer->page_count; i++) free(buffer->characters[i]);
|
|
614
|
-
free(buffer->characters);
|
|
615
|
-
free(buffer->positions);
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
static unsigned directive_utf8_width(uint32_t c) {
|
|
619
|
-
return c < 0x80 || c > 0x10ffff ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
|
|
620
|
-
}
|
|
621
|
-
|
|
622
501
|
static void directive_set(EmptyDirectives *state, DirectiveBuffer *buffer, uint8_t ***facts, uint32_t byte) {
|
|
623
502
|
if (buffer->failed) return;
|
|
624
503
|
uint32_t page = byte >> DIRECTIVE_FACT_PAGE_SHIFT;
|
|
@@ -712,29 +591,47 @@ static void directive_code(EmptyDirectives *state, DirectiveInput *input, Direct
|
|
|
712
591
|
directive_word_role(line, kind, name, pragma, operand, preproc_word(c, false));
|
|
713
592
|
}
|
|
714
593
|
|
|
715
|
-
// Outside a directive name, a line
|
|
716
|
-
//
|
|
594
|
+
// Outside a directive name, a line with no `_Pragma` or header operand in progress keeps its state through every token
|
|
595
|
+
// when it is of no directive kind, or of a kind that only waits for an operand it already has: its words need no
|
|
596
|
+
// classification.
|
|
717
597
|
static bool directive_neutral(const DirectiveLine *line) {
|
|
718
|
-
|
|
598
|
+
bool settled = line->kind == DIRECTIVE_ORDINARY || (line->operand && directive_requires_operand(line->kind));
|
|
599
|
+
return settled && !line->pending && !line->pragma_depth && !line->pragma_pending && !line->header;
|
|
719
600
|
}
|
|
720
601
|
|
|
721
|
-
// Also consumes the
|
|
602
|
+
// Also consumes the stored characters after the token that leave the line state as it is.
|
|
722
603
|
static void directive_neutral_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c) {
|
|
723
604
|
line->operand = true;
|
|
724
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;
|
|
725
622
|
for (;;) {
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
return;
|
|
730
|
-
}
|
|
731
|
-
} else if (preproc_word(c, false) || (c >= '0' && c <= '9')) {
|
|
732
|
-
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;
|
|
733
626
|
}
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
if (
|
|
737
|
-
|
|
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;
|
|
738
635
|
}
|
|
739
636
|
}
|
|
740
637
|
|
|
@@ -930,13 +827,18 @@ static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun r
|
|
|
930
827
|
index = directive_run_end(buffer, run, index, end);
|
|
931
828
|
if (index < end || index == start || end < buffer->length) break;
|
|
932
829
|
}
|
|
933
|
-
uint32_t length = index - input->index
|
|
934
|
-
if (
|
|
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;
|
|
935
838
|
input->before_previous_byte = length > 1 ? input->byte + (length - 2) * width : input->previous_byte;
|
|
936
839
|
input->previous_byte = input->byte + (length - 1) * width;
|
|
937
840
|
input->byte += length * width;
|
|
938
|
-
input->index
|
|
939
|
-
return length;
|
|
841
|
+
input->index += length;
|
|
940
842
|
}
|
|
941
843
|
|
|
942
844
|
// The index of the first stored character from `index` to `end` that ends the run, or `end`.
|
|
@@ -959,9 +861,6 @@ static bool directive_run_continues(DirectiveRun run, uint16_t c) {
|
|
|
959
861
|
switch (run) {
|
|
960
862
|
case DIRECTIVE_RUN_WORD:
|
|
961
863
|
return identifier;
|
|
962
|
-
case DIRECTIVE_RUN_CODE:
|
|
963
|
-
// The rest of a neutral line: characters that start no token.
|
|
964
|
-
return !identifier && c != '"' && c != '\'' && c != '/';
|
|
965
864
|
case DIRECTIVE_RUN_SPACE:
|
|
966
865
|
return directive_horizontal(c);
|
|
967
866
|
case DIRECTIVE_RUN_LINE_COMMENT:
|
package/src/scanner.c
CHANGED
|
@@ -7,14 +7,12 @@ typedef struct {
|
|
|
7
7
|
MacroProofMemo proof_memo;
|
|
8
8
|
} CScanner;
|
|
9
9
|
|
|
10
|
-
enum TokenType { PRAGMA_OPERATOR, PREPROC_ARG, PREPROC_NEWLINE, PREPROC_LPAREN, PREPROC_DIRECTIVE_ARG, PREPROC_FUNCTION_NAME, FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX, TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START, CONDITIONAL_MARKER_IFDEF, CONDITIONAL_MARKER_IFNDEF, LEADING_CONDITIONAL_MARKER_IFDEF, LEADING_CONDITIONAL_MARKER_IFNDEF, GNU_RAW_STRING_BODY, RAW_MACRO_TYPE_TAIL, BRACED_FUNCTION_ATTRIBUTE_START, BRACED_MACRO_TYPE_START, DIRECTIVE_CONTEXT, PREPROC_EMPTY, PREPROC_DIRECTIVE_START };
|
|
10
|
+
enum TokenType { PRAGMA_OPERATOR, PREPROC_ARG, PREPROC_NEWLINE, PREPROC_LPAREN, PREPROC_DIRECTIVE_ARG, PREPROC_FUNCTION_NAME, FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX, TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START, CONDITIONAL_MARKER_IFDEF, CONDITIONAL_MARKER_IFNDEF, LEADING_CONDITIONAL_MARKER_IFDEF, LEADING_CONDITIONAL_MARKER_IFNDEF, GNU_RAW_STRING_BODY, RAW_MACRO_TYPE_TAIL, BRACED_FUNCTION_ATTRIBUTE_START, BRACED_MACRO_TYPE_START, DIRECTIVE_CONTEXT, PREPROC_EMPTY, PREPROC_DIRECTIVE_START, TOKEN_COUNT };
|
|
11
11
|
|
|
12
12
|
static EmptyDirectives *scanner_directives(CScanner *scanner);
|
|
13
13
|
|
|
14
14
|
static bool only_extras_valid(const EmptyDirectives *state, const bool *valid_symbols);
|
|
15
15
|
static bool scan_extras(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
|
|
16
|
-
static bool scan_extras_pragma(EmptyDirectives *state, TSLexer *lexer, const TSLexerCheckpoint *start, bool skipped);
|
|
17
|
-
static uint32_t extras_start_byte(TSLexer *lexer, const TSLexerCheckpoint *start, bool skipped);
|
|
18
16
|
static bool scan_preproc_tokens(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
|
|
19
17
|
static bool scan_declaration_tokens(CScanner *scanner, TSLexer *lexer, const bool *valid_symbols);
|
|
20
18
|
static bool scan_directive_context(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols);
|
|
@@ -33,16 +31,20 @@ void tree_sitter_c_external_scanner_destroy(void *payload) {
|
|
|
33
31
|
free(scanner);
|
|
34
32
|
}
|
|
35
33
|
|
|
34
|
+
enum { SERIALIZED_READY = 1, SERIALIZED_ANCHORED = 2 };
|
|
35
|
+
|
|
36
36
|
unsigned tree_sitter_c_external_scanner_serialize(void *payload, char *buffer) {
|
|
37
37
|
const EmptyDirectives *state = scanner_directives(payload);
|
|
38
|
-
buffer[0] = state
|
|
38
|
+
buffer[0] = state ? (state->ready ? SERIALIZED_READY : 0) | (state->anchored ? SERIALIZED_ANCHORED : 0) : 0;
|
|
39
39
|
return 1;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
void tree_sitter_c_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {
|
|
43
43
|
CScanner *scanner = payload;
|
|
44
44
|
EmptyDirectives *state = scanner_directives(scanner);
|
|
45
|
-
if (state)
|
|
45
|
+
if (!state) return;
|
|
46
|
+
state->ready = length && (buffer[0] & SERIALIZED_READY);
|
|
47
|
+
state->anchored = length && (buffer[0] & SERIALIZED_ANCHORED);
|
|
46
48
|
}
|
|
47
49
|
|
|
48
50
|
// code-gauge-allow functionCognitiveComplexity -- External-token validity dispatches each lexing mode to its scanner.
|
|
@@ -50,6 +52,7 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
|
|
|
50
52
|
CScanner *scanner = payload;
|
|
51
53
|
EmptyDirectives *state = scanner_directives(scanner);
|
|
52
54
|
if (only_extras_valid(state, valid_symbols)) return scan_extras(state, lexer, valid_symbols);
|
|
55
|
+
if (state && !state->ready && valid_symbols[DIRECTIVE_CONTEXT]) return empty_directives_initialize(state, lexer, DIRECTIVE_CONTEXT);
|
|
53
56
|
if (scan_directive_context(state, lexer, valid_symbols)) return true;
|
|
54
57
|
if (empty_directives_at_line_end(state, lexer)) return false;
|
|
55
58
|
if (valid_symbols[GNU_RAW_STRING_BODY] && !valid_symbols[PREPROC_LPAREN]) {
|
|
@@ -68,8 +71,8 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
|
|
|
68
71
|
valid_symbols[PREPROC_ARG] || valid_symbols[PREPROC_DIRECTIVE_ARG]) {
|
|
69
72
|
return scan_preproc_tokens(state, lexer, valid_symbols);
|
|
70
73
|
}
|
|
71
|
-
bool
|
|
72
|
-
|
|
74
|
+
bool conditional = valid_symbols[LEADING_CONDITIONAL_MARKER_IFDEF] || valid_symbols[LEADING_CONDITIONAL_MARKER_IFNDEF] ||
|
|
75
|
+
valid_symbols[CONDITIONAL_MARKER_IFDEF] || valid_symbols[CONDITIONAL_MARKER_IFNDEF];
|
|
73
76
|
if (!conditional) return scan_declaration_tokens(scanner, lexer, valid_symbols);
|
|
74
77
|
while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
|
|
75
78
|
if (lexer->lookahead == '#') return scan_conditional_directive(state, lexer, valid_symbols);
|
|
@@ -77,51 +80,25 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
|
|
|
77
80
|
}
|
|
78
81
|
|
|
79
82
|
static bool only_extras_valid(const EmptyDirectives *state, const bool *valid_symbols) {
|
|
80
|
-
|
|
81
|
-
|
|
83
|
+
static const uint8_t extras[TOKEN_COUNT] = {[PRAGMA_OPERATOR] = 0xff, [DIRECTIVE_CONTEXT] = 0xff, [PREPROC_EMPTY] = 0xff};
|
|
84
|
+
return state && state->ready && empty_directives_only_valid(valid_symbols, extras, TOKEN_COUNT);
|
|
82
85
|
}
|
|
83
86
|
|
|
84
|
-
//
|
|
85
|
-
//
|
|
86
|
-
// exists) or `_Pragma` (when skipped spaces may start at `line_ends`), avoiding checkpoint/column work on other tokens.
|
|
87
|
+
// Fails without consuming a character unless a null directive starts here or the input holds `pragma_word`, since
|
|
88
|
+
// the lexer rereads from the start what a failed scan consumed.
|
|
87
89
|
static bool scan_extras(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (
|
|
90
|
+
if (valid_symbols[PREPROC_EMPTY] && empty_directives_consume(lexer, state, true)) {
|
|
91
|
+
lexer->mark_end(lexer);
|
|
92
|
+
lexer->result_symbol = PREPROC_EMPTY;
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
if (!valid_symbols[PRAGMA_OPERATOR] || !state->pragmas || empty_directives_at_line_end(state, lexer)) return false;
|
|
94
96
|
while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
|
|
95
|
-
int32_t c = lexer->lookahead;
|
|
96
|
-
if (c == '_') return allow_pragma && scan_extras_pragma(state, lexer, &start, skipped);
|
|
97
|
-
if (!allow_empty || (c != '#' && c != '\\')) return false;
|
|
98
|
-
if (!empty_directives_start_at(state, extras_start_byte(lexer, &start, skipped))) return false;
|
|
99
|
-
empty_directives_consume_started(lexer, state, true);
|
|
100
|
-
lexer->mark_end(lexer);
|
|
101
|
-
lexer->result_symbol = PREPROC_EMPTY;
|
|
102
|
-
return true;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
static bool scan_extras_pragma(EmptyDirectives *state, TSLexer *lexer, const TSLexerCheckpoint *start, bool skipped) {
|
|
106
97
|
if (!scan_pragma_word(lexer)) return false;
|
|
107
|
-
if (skipped && state->line_ends && empty_directives_line_end_at(state, extras_start_byte(lexer, start, true))) {
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
110
98
|
lexer->result_symbol = PRAGMA_OPERATOR;
|
|
111
99
|
return scan_pragma_suffix_end_with(lexer, true, &(PragmaTrivia){empty_directives_skip, state});
|
|
112
100
|
}
|
|
113
101
|
|
|
114
|
-
static uint32_t extras_start_byte(TSLexer *lexer, const TSLexerCheckpoint *start, bool skipped) {
|
|
115
|
-
TSLexerPosition position;
|
|
116
|
-
if (skipped) {
|
|
117
|
-
lexer->mark_end_at(lexer, start);
|
|
118
|
-
lexer->get_mark_end(lexer, &position);
|
|
119
|
-
} else {
|
|
120
|
-
lexer->get_position(lexer, &position);
|
|
121
|
-
}
|
|
122
|
-
return position.bytes;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
102
|
static bool scan_conditional_directive(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
|
|
126
103
|
if (!empty_directives_at_start(state, lexer)) return false;
|
|
127
104
|
const ConditionalMacroSymbols symbols = {
|
|
@@ -133,10 +110,6 @@ static bool scan_conditional_directive(EmptyDirectives *state, TSLexer *lexer, c
|
|
|
133
110
|
}
|
|
134
111
|
|
|
135
112
|
static bool scan_directive_context(EmptyDirectives *state, TSLexer *lexer, const bool *valid_symbols) {
|
|
136
|
-
if (state && !state->ready && valid_symbols[DIRECTIVE_CONTEXT]) {
|
|
137
|
-
lexer->result_symbol = DIRECTIVE_CONTEXT;
|
|
138
|
-
return empty_directives_initialize(state, lexer);
|
|
139
|
-
}
|
|
140
113
|
if (valid_symbols[PREPROC_DIRECTIVE_START] && empty_directives_after_keyword(state, lexer)) {
|
|
141
114
|
lexer->result_symbol = PREPROC_DIRECTIVE_START;
|
|
142
115
|
lexer->mark_end(lexer);
|
|
@@ -227,7 +200,8 @@ static bool scan_preproc_tokens(EmptyDirectives *state, TSLexer *lexer, const bo
|
|
|
227
200
|
return scan_preproc_arg(lexer, false, argument_mode);
|
|
228
201
|
}
|
|
229
202
|
lexer->result_symbol = PRAGMA_OPERATOR;
|
|
230
|
-
return valid_symbols[PRAGMA_OPERATOR] &&
|
|
203
|
+
return valid_symbols[PRAGMA_OPERATOR] && empty_directives_pragma_possible(state) &&
|
|
204
|
+
scan_pragma_with(lexer, &(PragmaTrivia){empty_directives_skip, state});
|
|
231
205
|
}
|
|
232
206
|
|
|
233
207
|
static EmptyDirectives *scanner_directives(CScanner *scanner) {
|
package/tree-sitter-c.wasm
CHANGED
|
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": "
|
|
30
|
+
"version": "43.0.0",
|
|
29
31
|
"license": "MIT",
|
|
30
32
|
"description": "C grammar for tree-sitter",
|
|
31
33
|
"authors": [
|