@willbooster/tree-sitter-c 41.0.0 → 42.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/package.json +1 -1
- package/src/directiveScan.h +147 -53
- package/src/emptyDirectives.h +76 -24
- package/src/scanner.c +22 -48
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +1 -1
package/package.json
CHANGED
package/src/directiveScan.h
CHANGED
|
@@ -31,6 +31,12 @@ typedef struct {
|
|
|
31
31
|
DirectiveScanMode mode;
|
|
32
32
|
GnuRawStringPrefix raw;
|
|
33
33
|
DirectiveSpans *run;
|
|
34
|
+
// Set when the characters read hold `pragma_word`, also in a comment or a literal: a scan starts inside a literal
|
|
35
|
+
// after an escape sequence, and the lexer ends some comments earlier than this pass does.
|
|
36
|
+
bool *pragma;
|
|
37
|
+
// The characters read last, the latest in the low byte and zero for one that is not ASCII. Code outside words is
|
|
38
|
+
// left out, so a word's first character restarts them.
|
|
39
|
+
uint64_t recent;
|
|
34
40
|
int32_t quote, previous, pair;
|
|
35
41
|
uint32_t spaces, run_start, run_end, run_spans;
|
|
36
42
|
unsigned delimiter_length, closing_length, closing_end;
|
|
@@ -38,13 +44,19 @@ typedef struct {
|
|
|
38
44
|
bool beginning, number, escape, star, splice, in_run;
|
|
39
45
|
} DirectiveScan;
|
|
40
46
|
|
|
41
|
-
static
|
|
42
|
-
static
|
|
43
|
-
static inline bool
|
|
44
|
-
static inline bool
|
|
47
|
+
static bool directive_scan_leading_comment(DirectiveScan *scan, TSLexer *lexer);
|
|
48
|
+
static bool directive_scan_feed(DirectiveScan *scan, int32_t c);
|
|
49
|
+
static inline bool directive_scan_plain_mode(DirectiveScan *scan, TSLexer *lexer);
|
|
50
|
+
static inline bool directive_scan_plain_word(DirectiveScan *scan, TSLexer *lexer);
|
|
51
|
+
static inline void directive_scan_word_characters(DirectiveScan *scan, uint64_t recent, uint32_t count);
|
|
52
|
+
static inline bool directive_pragma_word(uint64_t recent);
|
|
53
|
+
static inline uint64_t directive_scan_note(DirectiveScan *scan, uint64_t recent, int32_t c);
|
|
54
|
+
static inline bool directive_scan_plain_code(DirectiveScan *scan, TSLexer *lexer);
|
|
55
|
+
static inline bool directive_scan_plain_block_comment(DirectiveScan *scan, TSLexer *lexer);
|
|
56
|
+
static inline void directive_scan_plain_text(DirectiveScan *scan, TSLexer *lexer, int32_t end, bool raw);
|
|
45
57
|
static inline void directive_scan_lexer_run(DirectiveScan *scan, TSLexer *lexer, bool member);
|
|
46
58
|
static inline bool directive_scan_ascii_word(int32_t c);
|
|
47
|
-
static inline bool
|
|
59
|
+
static inline bool directive_scan_ascii_plain(int32_t c);
|
|
48
60
|
static bool directive_scan_pending(DirectiveScan *scan, int32_t c);
|
|
49
61
|
static bool directive_scan_splice(DirectiveScan *scan, int32_t c);
|
|
50
62
|
static bool directive_scan_logical(DirectiveScan *scan, int32_t c);
|
|
@@ -62,71 +74,137 @@ static void directive_scan_close_run(DirectiveScan *scan, uint32_t limit, bool s
|
|
|
62
74
|
static void directive_scan_push(DirectiveScan *scan, uint32_t start, uint32_t end);
|
|
63
75
|
static inline void directive_scan_extend(DirectiveScan *scan, uint32_t start, bool skipped);
|
|
64
76
|
|
|
77
|
+
// Feeds the spaces at the lexer's position, and a `/` after them with the character that follows it. Returns true when
|
|
78
|
+
// they open a comment. It reads no further than that one `/`, so that the scans after a postponed build, which start
|
|
79
|
+
// where this one's tokens end, do not read the same characters again and again.
|
|
80
|
+
static bool directive_scan_leading_comment(DirectiveScan *scan, TSLexer *lexer) {
|
|
81
|
+
for (bool slash = false;;) {
|
|
82
|
+
if (scan->mode == SCAN_LINE_COMMENT || scan->mode == SCAN_BLOCK_COMMENT) return true;
|
|
83
|
+
int32_t c = lexer->lookahead;
|
|
84
|
+
bool space = !slash && scan->mode == SCAN_CODE && !scan->splice && (pragma_space(c) || c == '/');
|
|
85
|
+
if ((!space && scan->mode != SCAN_SLASH) || pragma_eof(lexer)) return false;
|
|
86
|
+
slash = slash || c == '/';
|
|
87
|
+
directive_scan_feed(scan, c);
|
|
88
|
+
directive_scan_lexer_run(scan, lexer, pragma_space(c) || c == '\\');
|
|
89
|
+
lexer->advance(lexer, false);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
65
93
|
// Consumes the lexer's ASCII characters that need no physical-layer work and change no state but the line beginning,
|
|
66
94
|
// the current word, the end of a comment and the run of spaces, which is most of the input, and stops at a character
|
|
67
|
-
// that `directive_scan_feed` has to handle.
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
95
|
+
// that `directive_scan_feed` has to handle. Each mode has a loop of its own, which keeps what it reads in registers
|
|
96
|
+
// across the lexer's calls.
|
|
97
|
+
static void directive_scan_plain_run(DirectiveScan *scan, TSLexer *lexer) {
|
|
98
|
+
if (scan->splice) return;
|
|
99
|
+
// Only another newline can belong to the newline before it.
|
|
100
|
+
if (scan->pair && directive_newline(lexer->lookahead)) return;
|
|
101
|
+
scan->pair = 0;
|
|
102
|
+
while (directive_scan_plain_mode(scan, lexer)) {}
|
|
74
103
|
}
|
|
75
104
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
if (c <= 0 || c >= 0x80) return false;
|
|
105
|
+
// Consumes the plain characters of the current mode. Returns true when those of another mode may follow.
|
|
106
|
+
static inline bool directive_scan_plain_mode(DirectiveScan *scan, TSLexer *lexer) {
|
|
79
107
|
switch (scan->mode) {
|
|
80
108
|
case SCAN_WORD:
|
|
81
|
-
return directive_scan_plain_word(scan, lexer
|
|
109
|
+
return directive_scan_plain_word(scan, lexer);
|
|
82
110
|
case SCAN_CODE:
|
|
83
|
-
return directive_scan_plain_code(scan, lexer
|
|
84
|
-
case SCAN_LINE_COMMENT:
|
|
85
|
-
return directive_scan_plain_text(scan, lexer, c);
|
|
111
|
+
return directive_scan_plain_code(scan, lexer);
|
|
86
112
|
case SCAN_BLOCK_COMMENT:
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
scan
|
|
90
|
-
return
|
|
113
|
+
return directive_scan_plain_block_comment(scan, lexer);
|
|
114
|
+
case SCAN_LINE_COMMENT:
|
|
115
|
+
directive_scan_plain_text(scan, lexer, 0, false);
|
|
116
|
+
return false;
|
|
91
117
|
case SCAN_QUOTE:
|
|
92
|
-
|
|
118
|
+
if (!scan->escape) directive_scan_plain_text(scan, lexer, scan->quote, false);
|
|
119
|
+
return false;
|
|
93
120
|
case SCAN_RAW_BODY:
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return true;
|
|
121
|
+
directive_scan_plain_text(scan, lexer, '"', true);
|
|
122
|
+
return false;
|
|
97
123
|
default:
|
|
98
124
|
return false;
|
|
99
125
|
}
|
|
100
126
|
}
|
|
101
127
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
128
|
+
// Consumes the rest of the word. Returns true when code follows it.
|
|
129
|
+
static inline bool directive_scan_plain_word(DirectiveScan *scan, TSLexer *lexer) {
|
|
130
|
+
int32_t c = lexer->lookahead;
|
|
131
|
+
uint64_t recent = scan->recent;
|
|
132
|
+
uint32_t count = 0;
|
|
133
|
+
for (; directive_scan_ascii_word(c); c = lexer->lookahead) {
|
|
134
|
+
recent = recent << 8 | (uint64_t)c;
|
|
135
|
+
count++;
|
|
136
|
+
lexer->advance(lexer, false);
|
|
108
137
|
}
|
|
109
|
-
scan
|
|
110
|
-
|
|
111
|
-
if (scan->
|
|
138
|
+
if (count) directive_scan_word_characters(scan, recent, count);
|
|
139
|
+
// A line splice may continue the word, and the others may continue a number or open a raw string.
|
|
140
|
+
if (!directive_scan_ascii_plain(c) || (scan->number && (c == '.' || c == '+' || c == '-'))) return false;
|
|
141
|
+
scan->mode = SCAN_CODE;
|
|
112
142
|
return true;
|
|
113
143
|
}
|
|
114
144
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
scan->
|
|
121
|
-
if (
|
|
122
|
-
return
|
|
145
|
+
// Adds the word's next `count` characters, the last ones of `recent`, as `directive_scan_word` does one at a time: a
|
|
146
|
+
// prefix that would exceed three characters is no raw string prefix whatever they are.
|
|
147
|
+
static inline void directive_scan_word_characters(DirectiveScan *scan, uint64_t recent, uint32_t count) {
|
|
148
|
+
scan->in_run = false;
|
|
149
|
+
scan->previous = (int32_t)(recent & 0xff);
|
|
150
|
+
scan->recent = recent;
|
|
151
|
+
if (directive_pragma_word(recent)) *scan->pragma = true;
|
|
152
|
+
if (scan->raw.length >= sizeof scan->raw.value) return;
|
|
153
|
+
if (count > sizeof scan->raw.value - 1 - scan->raw.length) {
|
|
154
|
+
gnu_raw_string_prefix_invalidate(&scan->raw);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
while (count--) gnu_raw_string_prefix_character(&scan->raw, (int32_t)(recent >> (8 * count) & 0xff));
|
|
123
158
|
}
|
|
124
159
|
|
|
125
|
-
//
|
|
126
|
-
static inline bool
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
160
|
+
// Consumes code up to a word. Returns true when a word follows, whose first character is consumed.
|
|
161
|
+
static inline bool directive_scan_plain_code(DirectiveScan *scan, TSLexer *lexer) {
|
|
162
|
+
for (;;) {
|
|
163
|
+
int32_t c = lexer->lookahead;
|
|
164
|
+
if (!directive_scan_ascii_plain(c)) return false;
|
|
165
|
+
bool space = directive_horizontal(c);
|
|
166
|
+
directive_scan_lexer_run(scan, lexer, space);
|
|
167
|
+
lexer->advance(lexer, false);
|
|
168
|
+
if (space) continue;
|
|
169
|
+
scan->beginning = false;
|
|
170
|
+
if (!directive_scan_ascii_word(c)) continue;
|
|
171
|
+
directive_scan_word_start(scan, c);
|
|
172
|
+
if (scan->mode == SCAN_WORD) return true;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Consumes the comment's body. Returns true when the comment ends.
|
|
177
|
+
static inline bool directive_scan_plain_block_comment(DirectiveScan *scan, TSLexer *lexer) {
|
|
178
|
+
bool star = scan->star, closed = false;
|
|
179
|
+
uint64_t recent = scan->recent;
|
|
180
|
+
while (!closed) {
|
|
181
|
+
int32_t c = lexer->lookahead;
|
|
182
|
+
if (c <= 0 || c >= 0x80 || c == '\n' || c == '\r' || c == '\\') break;
|
|
183
|
+
directive_scan_lexer_run(scan, lexer, directive_horizontal(c));
|
|
184
|
+
recent = directive_scan_note(scan, recent, c);
|
|
185
|
+
lexer->advance(lexer, false);
|
|
186
|
+
closed = star && c == '/';
|
|
187
|
+
star = c == '*';
|
|
188
|
+
}
|
|
189
|
+
scan->recent = recent;
|
|
190
|
+
scan->star = star;
|
|
191
|
+
if (closed) scan->mode = SCAN_CODE;
|
|
192
|
+
return closed;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Consumes the body of a line comment or a literal up to `end`, the closing search of a `raw` string seeing each
|
|
196
|
+
// character.
|
|
197
|
+
static inline void directive_scan_plain_text(DirectiveScan *scan, TSLexer *lexer, int32_t end, bool raw) {
|
|
198
|
+
uint64_t recent = scan->recent;
|
|
199
|
+
for (;;) {
|
|
200
|
+
int32_t c = lexer->lookahead;
|
|
201
|
+
if (c <= 0 || c >= 0x80 || c == '\n' || c == '\r' || c == '\\' || c == end) break;
|
|
202
|
+
directive_scan_lexer_run(scan, lexer, directive_horizontal(c));
|
|
203
|
+
if (raw) directive_scan_raw_body(scan, c);
|
|
204
|
+
recent = directive_scan_note(scan, recent, c);
|
|
205
|
+
lexer->advance(lexer, false);
|
|
206
|
+
}
|
|
207
|
+
scan->recent = recent;
|
|
130
208
|
}
|
|
131
209
|
|
|
132
210
|
// Extends or ends the run of spaces and backslashes with the lookahead, as `directive_scan_run` does. A span read from
|
|
@@ -167,16 +245,16 @@ static void directive_scan_push(DirectiveScan *scan, uint32_t start, uint32_t en
|
|
|
167
245
|
}
|
|
168
246
|
|
|
169
247
|
static inline bool directive_scan_ascii_word(int32_t c) {
|
|
170
|
-
return c != '\\' && ((preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1);
|
|
248
|
+
return (uint32_t)c < 0x80 && c != '\\' && ((preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1);
|
|
171
249
|
}
|
|
172
250
|
|
|
173
|
-
// NUL, newlines, `\`, `/`, quotes and `#`, which code leaves to `directive_scan_feed`.
|
|
174
|
-
static inline bool
|
|
251
|
+
// ASCII but NUL, newlines, `\`, `/`, quotes and `#`, which code leaves to `directive_scan_feed`.
|
|
252
|
+
static inline bool directive_scan_ascii_plain(int32_t c) {
|
|
175
253
|
static const uint64_t special[] = {
|
|
176
254
|
1ull | (1ull << '\n') | (1ull << '\r') | (1ull << '"') | (1ull << '#') | (1ull << '\'') | (1ull << '/'),
|
|
177
255
|
1ull << ('\\' - 64),
|
|
178
256
|
};
|
|
179
|
-
return (special[c / 64] >> (c % 64)) & 1;
|
|
257
|
+
return (uint32_t)c < 0x80 && !((special[c / 64] >> (c % 64)) & 1);
|
|
180
258
|
}
|
|
181
259
|
|
|
182
260
|
// Extends or ends the run with the stored character `c` from `start` to `end`.
|
|
@@ -193,6 +271,7 @@ static void directive_scan_run(DirectiveScan *scan, int32_t c, uint32_t start, u
|
|
|
193
271
|
// `directive_encode`) and returns true when `c` starts a directive, which the caller leaves unconsumed.
|
|
194
272
|
static bool directive_scan_feed(DirectiveScan *scan, int32_t c) {
|
|
195
273
|
if ((uint32_t)c > 0x10ffff) c = 0xfffd;
|
|
274
|
+
scan->recent = directive_scan_note(scan, scan->recent, c);
|
|
196
275
|
if (scan->mode == SCAN_RAW_BODY) {
|
|
197
276
|
directive_scan_raw_body(scan, c);
|
|
198
277
|
return false;
|
|
@@ -311,6 +390,7 @@ static void directive_scan_word_start(DirectiveScan *scan, int32_t c) {
|
|
|
311
390
|
scan->mode = SCAN_WORD;
|
|
312
391
|
scan->number = c >= '0' && c <= '9';
|
|
313
392
|
scan->previous = c;
|
|
393
|
+
scan->recent = c < 0x80 ? (uint64_t)c : 0;
|
|
314
394
|
scan->raw = (GnuRawStringPrefix){0};
|
|
315
395
|
gnu_raw_string_prefix_character(&scan->raw, c);
|
|
316
396
|
}
|
|
@@ -370,6 +450,20 @@ static void directive_scan_raw_body(DirectiveScan *scan, int32_t c) {
|
|
|
370
450
|
scan->mode = SCAN_CODE;
|
|
371
451
|
}
|
|
372
452
|
|
|
453
|
+
// Adds `c` to the characters read last and returns them.
|
|
454
|
+
static inline uint64_t directive_scan_note(DirectiveScan *scan, uint64_t recent, int32_t c) {
|
|
455
|
+
recent = recent << 8 | ((uint32_t)c < 0x80 ? (uint64_t)c : 0);
|
|
456
|
+
if (directive_pragma_word(recent)) *scan->pragma = true;
|
|
457
|
+
return recent;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// Whether the characters in `recent`, the last one in the low byte, end in `pragma_word`.
|
|
461
|
+
static inline bool directive_pragma_word(uint64_t recent) {
|
|
462
|
+
uint64_t word = 0;
|
|
463
|
+
for (unsigned i = 0; i < sizeof pragma_word - 1; i++) word = word << 8 | (uint8_t)pragma_word[i];
|
|
464
|
+
return (recent & ((1ull << 8 * (sizeof pragma_word - 1)) - 1)) == word;
|
|
465
|
+
}
|
|
466
|
+
|
|
373
467
|
static bool directive_horizontal(int32_t c) {
|
|
374
468
|
return c == ' ' || c == '\t' || c == '\v' || c == '\f';
|
|
375
469
|
}
|
package/src/emptyDirectives.h
CHANGED
|
@@ -23,8 +23,11 @@ typedef struct {
|
|
|
23
23
|
// The lexer's byte position, which follows the last stored character.
|
|
24
24
|
uint32_t fill_byte;
|
|
25
25
|
uint32_t conditional_depth;
|
|
26
|
-
//
|
|
27
|
-
|
|
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;
|
|
28
31
|
} DirectiveBuffer;
|
|
29
32
|
|
|
30
33
|
// A position in a `DirectiveBuffer`; copies share the buffer, so a lookahead copy reads what the original will.
|
|
@@ -45,7 +48,10 @@ typedef struct {
|
|
|
45
48
|
typedef struct {
|
|
46
49
|
uint8_t **starts, **ends, **comments, **line_ends, **directive_starts, **directive_ends;
|
|
47
50
|
uint32_t pages;
|
|
48
|
-
|
|
51
|
+
// `anchored` and `ready` belong to the scanner state: whether the token at the start of the input is out, and
|
|
52
|
+
// whether the facts, which `built` says exist, are in use. `pragmas` is the fact that the input read for them holds
|
|
53
|
+
// `pragma_word`, without which no scan has to look for a `_Pragma` operator.
|
|
54
|
+
bool built, anchored, ready, pragmas;
|
|
49
55
|
} EmptyDirectives;
|
|
50
56
|
|
|
51
57
|
typedef enum { DIRECTIVE_ORDINARY, DIRECTIVE_CONDITION, DIRECTIVE_DIAGNOSTIC, DIRECTIVE_REQUIRED, DIRECTIVE_UNDEF, DIRECTIVE_LINE, DIRECTIVE_PRAGMA, DIRECTIVE_HEADER } DirectiveKind;
|
|
@@ -73,7 +79,7 @@ static bool directive_segment_ends(const DirectiveInput *input);
|
|
|
73
79
|
static bool directive_short_line_comment(const DirectiveInput *input, uint32_t index, uint32_t limit);
|
|
74
80
|
static bool directive_line_clean(const DirectiveLine *line);
|
|
75
81
|
static void directive_shift(DirectiveBuffer *buffer, uint32_t start);
|
|
76
|
-
static
|
|
82
|
+
static void directive_finish(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveSpans *run, DirectiveSpans *tail);
|
|
77
83
|
static void directive_prefixes(EmptyDirectives *state, const DirectiveInput *input, const DirectiveSpans *prefix, DirectiveSpans *tail);
|
|
78
84
|
static bool directive_step_back(const DirectiveInput *input, uint32_t *index, uint32_t *position, uint32_t *byte, int32_t *c, uint32_t *width);
|
|
79
85
|
static void directive_carry(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveCarry *carry, int32_t c, uint32_t byte);
|
|
@@ -85,6 +91,7 @@ static inline bool directive_has(const DirectiveInput *input, uint32_t index);
|
|
|
85
91
|
static bool directive_fill(DirectiveBuffer *buffer, uint32_t index);
|
|
86
92
|
static bool directive_append(DirectiveBuffer *buffer);
|
|
87
93
|
static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c);
|
|
94
|
+
static inline void directive_note(DirectiveBuffer *buffer, uint32_t c);
|
|
88
95
|
static bool directive_reserve(DirectiveBuffer *buffer, unsigned width);
|
|
89
96
|
static bool directive_position(DirectiveBuffer *buffer, uint32_t index, uint32_t byte, uint32_t width);
|
|
90
97
|
static void directive_encode(DirectiveBuffer *buffer, uint32_t c);
|
|
@@ -147,16 +154,19 @@ static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool na
|
|
|
147
154
|
static uint32_t directive_byte(const DirectiveInput *input);
|
|
148
155
|
static void directive_record(EmptyDirectives *state, DirectiveInput *input, const DirectiveLine *line, uint32_t end);
|
|
149
156
|
|
|
150
|
-
|
|
157
|
+
// Scans the zero-width token `symbol` that starts the directive context: once at the start of the input, where it
|
|
158
|
+
// anchors the tree, and, when comments lead the input, once more where the facts are built after them. Returns false
|
|
159
|
+
// while a comment is still ahead, having consumed characters.
|
|
160
|
+
static bool empty_directives_initialize(EmptyDirectives *state, TSLexer *lexer, TSSymbol symbol) {
|
|
151
161
|
TSLexerCheckpoint start;
|
|
152
162
|
lexer->get_checkpoint(lexer, &start);
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
state->ready =
|
|
163
|
+
lexer->result_symbol = symbol;
|
|
164
|
+
bool anchored = state->anchored;
|
|
165
|
+
state->anchored = true;
|
|
166
|
+
if (!state->built) state->built = empty_directives_build(state, lexer);
|
|
167
|
+
state->ready = state->built;
|
|
158
168
|
lexer->mark_end_at(lexer, &start);
|
|
159
|
-
return
|
|
169
|
+
return state->ready || !anchored;
|
|
160
170
|
}
|
|
161
171
|
|
|
162
172
|
static bool empty_directives_after_keyword(const EmptyDirectives *state, TSLexer *lexer) {
|
|
@@ -166,6 +176,25 @@ static bool empty_directives_after_keyword(const EmptyDirectives *state, TSLexer
|
|
|
166
176
|
return directive_bit(state, state->directive_ends, position.bytes);
|
|
167
177
|
}
|
|
168
178
|
|
|
179
|
+
// False only when the facts show that no `_Pragma` operator can follow any position.
|
|
180
|
+
static bool empty_directives_pragma_possible(const EmptyDirectives *state) {
|
|
181
|
+
return !state || !state->built || state->pragmas;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Whether none of the `count` tokens, at least eight, is valid but those whose byte in `extras` is 0xff.
|
|
185
|
+
static inline bool empty_directives_only_valid(const bool *valid_symbols, const uint8_t *extras, unsigned count) {
|
|
186
|
+
_Static_assert(sizeof(bool) == 1, "valid_symbols is read as bytes");
|
|
187
|
+
uint64_t others = 0;
|
|
188
|
+
for (unsigned i = 0; i < count; i += 8) {
|
|
189
|
+
unsigned offset = i + 8 > count ? count - 8 : i;
|
|
190
|
+
uint64_t valid, extra;
|
|
191
|
+
memcpy(&valid, valid_symbols + offset, sizeof valid);
|
|
192
|
+
memcpy(&extra, extras + offset, sizeof extra);
|
|
193
|
+
others |= valid & ~extra;
|
|
194
|
+
}
|
|
195
|
+
return !others;
|
|
196
|
+
}
|
|
197
|
+
|
|
169
198
|
static bool empty_directives_at_start(const EmptyDirectives *state, TSLexer *lexer) {
|
|
170
199
|
if (!state || !state->directive_starts) return false;
|
|
171
200
|
TSLexerPosition position;
|
|
@@ -224,6 +253,15 @@ static void empty_directives_consume_started(TSLexer *lexer, EmptyDirectives *st
|
|
|
224
253
|
// Ordinary code goes through `DirectiveScan` without being stored, and only directive segments, each from a directive's
|
|
225
254
|
// `#` to the end of the logical line where no directive state is left, are stored for the line-state pass and the
|
|
226
255
|
// reverse prefix pass. The facts are stored as `EmptyDirectives` describes.
|
|
256
|
+
//
|
|
257
|
+
// Returns false without building when a comment follows the lexer's position after spaces only, which leaves the build
|
|
258
|
+
// to the scan after the comment's token. That scan starts from the initial state where the lexer ends the comment: the
|
|
259
|
+
// state this pass has there too, unless the lexer ends the comment earlier, as at a backslash that spaces separate
|
|
260
|
+
// from its newline, and then what follows is read as the lexer's tokens take it and not as comment text.
|
|
261
|
+
//
|
|
262
|
+
// 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
|
|
263
|
+
// that consumes nothing, which the runtime takes for a scanner stuck in an unterminated comment: it then reads the
|
|
264
|
+
// whole input once more and guards every later scan.
|
|
227
265
|
static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
|
|
228
266
|
TSLexerPosition position;
|
|
229
267
|
lexer->get_position(lexer, &position);
|
|
@@ -231,7 +269,11 @@ static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
|
|
|
231
269
|
DirectiveInput input = {.buffer = &buffer, .byte = position.bytes};
|
|
232
270
|
// `run` holds the run before a segment's `#`, and `tail` the segment's trailing run.
|
|
233
271
|
DirectiveSpans run = {0}, tail = {0};
|
|
234
|
-
DirectiveScan scan = {.beginning = true, .run = &run};
|
|
272
|
+
DirectiveScan scan = {.beginning = true, .run = &run, .pragma = &buffer.pragma};
|
|
273
|
+
if (directive_scan_leading_comment(&scan, lexer)) {
|
|
274
|
+
free(run.items);
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
235
277
|
for (;;) {
|
|
236
278
|
if (!directive_scan_buffered(&scan, &input)) {
|
|
237
279
|
buffer.length = buffer.position_count = 0;
|
|
@@ -247,10 +289,8 @@ static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
|
|
|
247
289
|
if (!more || buffer.failed) break;
|
|
248
290
|
directive_scan_resume(&scan, &run, &tail);
|
|
249
291
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
free(tail.items);
|
|
253
|
-
return directive_finish(state, &buffer);
|
|
292
|
+
directive_finish(state, &buffer, &run, &tail);
|
|
293
|
+
return true;
|
|
254
294
|
}
|
|
255
295
|
|
|
256
296
|
// Restarts `scan` on ordinary code, continuing the run with a segment's trailing run, whose spans `tail` holds in byte
|
|
@@ -259,7 +299,7 @@ static void directive_scan_resume(DirectiveScan *scan, DirectiveSpans *run, Dire
|
|
|
259
299
|
DirectiveSpans before = *run;
|
|
260
300
|
*run = *tail;
|
|
261
301
|
*tail = before;
|
|
262
|
-
*scan = (DirectiveScan){.beginning = true, .run = run};
|
|
302
|
+
*scan = (DirectiveScan){.beginning = true, .run = run, .pragma = scan->pragma};
|
|
263
303
|
if (!run->count) return;
|
|
264
304
|
DirectiveSpan last = run->items[--run->count];
|
|
265
305
|
scan->run_spans = run->count;
|
|
@@ -379,11 +419,14 @@ static void directive_shift(DirectiveBuffer *buffer, uint32_t start) {
|
|
|
379
419
|
buffer->position_count = count;
|
|
380
420
|
}
|
|
381
421
|
|
|
382
|
-
|
|
422
|
+
// Without all the facts, none is kept, and a `_Pragma` operator may be anywhere.
|
|
423
|
+
static void directive_finish(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveSpans *run, DirectiveSpans *tail) {
|
|
424
|
+
bool failed = buffer->failed || run->failed || tail->failed;
|
|
425
|
+
free(run->items);
|
|
426
|
+
free(tail->items);
|
|
383
427
|
directive_release(buffer);
|
|
384
|
-
|
|
385
|
-
empty_directives_release(state);
|
|
386
|
-
return false;
|
|
428
|
+
state->pragmas = buffer->pragma || failed;
|
|
429
|
+
if (failed) empty_directives_release(state);
|
|
387
430
|
}
|
|
388
431
|
|
|
389
432
|
static void empty_directives_release(EmptyDirectives *state) {
|
|
@@ -523,6 +566,7 @@ static bool directive_append(DirectiveBuffer *buffer) {
|
|
|
523
566
|
unsigned width = directive_utf8_width(c), utf16_width = c > 0xffff && c <= 0x10ffff ? 4 : 2;
|
|
524
567
|
if (!directive_reserve(buffer, c > 0xffff && c <= 0x10ffff ? 2 : 1)) return false;
|
|
525
568
|
directive_encode(buffer, c);
|
|
569
|
+
directive_note(buffer, c);
|
|
526
570
|
TSLexerPosition end;
|
|
527
571
|
lexer->advance(lexer, false);
|
|
528
572
|
lexer->get_position(lexer, &end);
|
|
@@ -560,6 +604,7 @@ static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c) {
|
|
|
560
604
|
uint32_t index = buffer->length, start = buffer->fill_byte;
|
|
561
605
|
buffer->characters[index >> DIRECTIVE_PAGE_SHIFT][index & (DIRECTIVE_PAGE_SIZE - 1)] = c;
|
|
562
606
|
buffer->length = index + 1;
|
|
607
|
+
directive_note(buffer, c);
|
|
563
608
|
TSLexerPosition end;
|
|
564
609
|
lexer->advance(lexer, false);
|
|
565
610
|
lexer->get_position(lexer, &end);
|
|
@@ -567,6 +612,11 @@ static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c) {
|
|
|
567
612
|
return end.bytes - start == (buffer->utf16 ? 2u : 1u) || directive_position(buffer, index, start, end.bytes - start);
|
|
568
613
|
}
|
|
569
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
|
+
|
|
570
620
|
static bool directive_reserve(DirectiveBuffer *buffer, unsigned width) {
|
|
571
621
|
if (buffer->length > UINT32_MAX - width) return false;
|
|
572
622
|
uint32_t last = (buffer->length + width - 1) >> DIRECTIVE_PAGE_SHIFT;
|
|
@@ -712,10 +762,12 @@ static void directive_code(EmptyDirectives *state, DirectiveInput *input, Direct
|
|
|
712
762
|
directive_word_role(line, kind, name, pragma, operand, preproc_word(c, false));
|
|
713
763
|
}
|
|
714
764
|
|
|
715
|
-
// Outside a directive name, a line
|
|
716
|
-
//
|
|
765
|
+
// Outside a directive name, a line with no `_Pragma` or header operand in progress keeps its state through every token
|
|
766
|
+
// when it is of no directive kind, or of a kind that only waits for an operand it already has: its words need no
|
|
767
|
+
// classification.
|
|
717
768
|
static bool directive_neutral(const DirectiveLine *line) {
|
|
718
|
-
|
|
769
|
+
bool settled = line->kind == DIRECTIVE_ORDINARY || (line->operand && directive_requires_operand(line->kind));
|
|
770
|
+
return settled && !line->pending && !line->pragma_depth && !line->pragma_pending && !line->header;
|
|
719
771
|
}
|
|
720
772
|
|
|
721
773
|
// Also consumes the following characters that leave the line state as it is, which is every plain character but `/`.
|
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
|