Debian Codegraph @

Xtree-house/bindings/vendor/src/lexer.c

raw
1 #include "./length.h"
2 #include "./lexer.h"
3 #include "./unicode.h"
4
5 #include "tree_sitter/api.h"
6
7 #include <stdarg.h>
8 #include <stdio.h>
9
10 #define LOG(message, character) \
11 if (self->logger.log) { \
12 snprintf( \
13 self->debug_buffer, \
14 TREE_SITTER_SERIALIZATION_BUFFER_SIZE, \
15 32 <= character && character < 127 ? \
16 message " character:'%c'" : \
17 message " character:%d", \
18 character \
19 ); \
20 self->logger.log( \
21 self->logger.payload, \
22 TSLogTypeLex, \
23 self->debug_buffer \
24 ); \
25 }
26
27 static const int32_t BYTE_ORDER_MARK = 0xFEFF;
28
29 static const TSRange DEFAULT_RANGE = {
30 .start_point = {
31 .row = 0,
32 .column = 0,
33 },
34 .end_point = {
35 .row = UINT32_MAX,
36 .column = UINT32_MAX,
37 },
38 .start_byte = 0,
39 .end_byte = UINT32_MAX
40 };
41
42 /**
43 * Sets the column data to the given value and marks it valid.
44 * @param self The lexer state.
45 * @param val The new value of the column data.
46 */
47 static void ts_lexer__set_column_data(Lexer *self, uint32_t val) {
48 self->column_data.valid = true;
49 self->column_data.value = val;
50 }
51
52 /**
53 * Increments the value of the column data; no-op if invalid.
54 * @param self The lexer state.
55 */
56 static void ts_lexer__increment_column_data(Lexer *self) {
57 if (self->column_data.valid) {
58 self->column_data.value++;
59 }
60 }
61
62 /**
63 * Marks the column data as invalid.
64 * @param self The lexer state.
65 */
66 static void ts_lexer__invalidate_column_data(Lexer *self) {
67 self->column_data.valid = false;
68 self->column_data.value = 0;
69 }
70
71 // Check if the lexer has reached EOF. This state is stored
72 // by setting the lexer's `current_included_range_index` such that
73 // it has consumed all of its available ranges.
74 static bool ts_lexer__eof(const TSLexer *_self) {
75 Lexer *self = (Lexer *)_self;
76 return self->current_included_range_index == self->included_range_count;
77 }
78
79 // Clear the currently stored chunk of source code, because the lexer's
80 // position has changed.
81 static void ts_lexer__clear_chunk(Lexer *self) {
82 self->chunk = NULL;
83 self->chunk_size = 0;
84 self->chunk_start = 0;
85 }
86
87 // Call the lexer's input callback to obtain a new chunk of source code
88 // for the current position.
89 static void ts_lexer__get_chunk(Lexer *self) {
90 self->chunk_start = self->current_position.bytes;
91 self->chunk = self->input.read(
92 self->input.payload,
93 self->current_position.bytes,
94 self->current_position.extent,
95 &self->chunk_size
96 );
97 if (!self->chunk_size) {
98 self->current_included_range_index = self->included_range_count;
99 self->chunk = NULL;
100 }
101 }
102
103 // Decode the next unicode character in the current chunk of source code.
104 // This assumes that the lexer has already retrieved a chunk of source
105 // code that spans the current position.
106 static void ts_lexer__get_lookahead(Lexer *self) {
107 uint32_t position_in_chunk = self->current_position.bytes - self->chunk_start;
108 uint32_t size = self->chunk_size - position_in_chunk;
109
110 if (size == 0) {
111 self->lookahead_size = 1;
112 self->data.lookahead = '\0';
113 return;
114 }
115
116 const uint8_t *chunk = (const uint8_t *)self->chunk + position_in_chunk;
117 TSDecodeFunction decode =
118 self->input.encoding == TSInputEncodingUTF8 ? ts_decode_utf8 :
119 self->input.encoding == TSInputEncodingUTF16LE ? ts_decode_utf16_le :
120 self->input.encoding == TSInputEncodingUTF16BE ? ts_decode_utf16_be : self->input.decode;
121
122 self->lookahead_size = decode(chunk, size, &self->data.lookahead);
123
124 // If this chunk ended in the middle of a multi-byte character,
125 // try again with a fresh chunk.
126 if (self->data.lookahead == TS_DECODE_ERROR && size < 4) {
127 ts_lexer__get_chunk(self);
128 chunk = (const uint8_t *)self->chunk;
129 size = self->chunk_size;
130 self->lookahead_size = decode(chunk, size, &self->data.lookahead);
131 }
132
133 if (self->data.lookahead == TS_DECODE_ERROR) {
134 self->lookahead_size = 1;
135 }
136 }
137
138 static void ts_lexer_goto(Lexer *self, Length position) {
139 if (position.bytes != self->current_position.bytes) {
140 ts_lexer__invalidate_column_data(self);
141 }
142
143 self->current_position = position;
144
145 // Move to the first valid position at or after the given position.
146 bool found_included_range = false;
147 for (unsigned i = 0; i < self->included_range_count; i++) {
148 TSRange *included_range = &self->included_ranges[i];
149 if (
150 included_range->end_byte > self->current_position.bytes &&
151 included_range->end_byte > included_range->start_byte
152 ) {
153 if (included_range->start_byte >= self->current_position.bytes) {
154 self->current_position = (Length) {
155 .bytes = included_range->start_byte,
156 .extent = included_range->start_point,
157 };
158 }
159
160 self->current_included_range_index = i;
161 found_included_range = true;
162 break;
163 }
164 }
165
166 if (found_included_range) {
167 // If the current position is outside of the current chunk of text,
168 // then clear out the current chunk of text.
169 if (self->chunk && (
170 self->current_position.bytes < self->chunk_start ||
171 self->current_position.bytes >= self->chunk_start + self->chunk_size
172 )) {
173 ts_lexer__clear_chunk(self);
174 }
175
176 self->lookahead_size = 0;
177 self->data.lookahead = '\0';
178 }
179
180 // If the given position is beyond any of included ranges, move to the EOF
181 // state - past the end of the included ranges.
182 else {
183 self->current_included_range_index = self->included_range_count;
184 TSRange *last_included_range = &self->included_ranges[self->included_range_count - 1];
185 self->current_position = (Length) {
186 .bytes = last_included_range->end_byte,
187 .extent = last_included_range->end_point,
188 };
189 ts_lexer__clear_chunk(self);
190 self->lookahead_size = 1;
191 self->data.lookahead = '\0';
192 }
193 }
194
195 /**
196 * Actually advances the lexer. Does not log anything.
197 * @param self The lexer state.
198 * @param skip Whether to mark the consumed codepoint as whitespace.
199 */
200 static void ts_lexer__do_advance(Lexer *self, bool skip) {
201 if (self->lookahead_size) {
202 if (self->data.lookahead == '\n') {
203 self->current_position.extent.row++;
204 self->current_position.extent.column = 0;
205 ts_lexer__set_column_data(self, 0);
206 } else {
207 bool is_bom = self->current_position.bytes == 0 &&
208 self->data.lookahead == BYTE_ORDER_MARK;
209 if (!is_bom) ts_lexer__increment_column_data(self);
210 self->current_position.extent.column += self->lookahead_size;
211 }
212 self->current_position.bytes += self->lookahead_size;
213 }
214
215 const TSRange *current_range = &self->included_ranges[self->current_included_range_index];
216 while (
217 self->current_position.bytes >= current_range->end_byte ||
218 current_range->end_byte == current_range->start_byte
219 ) {
220 if (self->current_included_range_index < self->included_range_count) {
221 self->current_included_range_index++;
222 }
223 if (self->current_included_range_index < self->included_range_count) {
224 current_range++;
225 self->current_position = (Length) {
226 current_range->start_byte,
227 current_range->start_point,
228 };
229 } else {
230 current_range = NULL;
231 break;
232 }
233 }
234
235 if (skip) self->token_start_position = self->current_position;
236
237 if (current_range) {
238 if (
239 self->current_position.bytes < self->chunk_start ||
240 self->current_position.bytes >= self->chunk_start + self->chunk_size
241 ) {
242 ts_lexer__get_chunk(self);
243 }
244 ts_lexer__get_lookahead(self);
245 } else {
246 ts_lexer__clear_chunk(self);
247 self->data.lookahead = '\0';
248 self->lookahead_size = 1;
249 }
250 }
251
252 // Advance to the next character in the source code, retrieving a new
253 // chunk of source code if needed.
254 static void ts_lexer__advance(TSLexer *_self, bool skip) {
255 Lexer *self = (Lexer *)_self;
256 if (!self->chunk) return;
257
258 if (skip) {
259 LOG("skip", self->data.lookahead)
260 } else {
261 LOG("consume", self->data.lookahead)
262 }
263
264 ts_lexer__do_advance(self, skip);
265 }
266
267 // Mark that a token match has completed. This can be called multiple
268 // times if a longer match is found later.
269 static void ts_lexer__mark_end(TSLexer *_self) {
270 Lexer *self = (Lexer *)_self;
271 if (!ts_lexer__eof(&self->data)) {
272 // If the lexer is right at the beginning of included range,
273 // then the token should be considered to end at the *end* of the
274 // previous included range, rather than here.
275 TSRange *current_included_range = &self->included_ranges[
276 self->current_included_range_index
277 ];
278 if (
279 self->current_included_range_index > 0 &&
280 self->current_position.bytes == current_included_range->start_byte
281 ) {
282 TSRange *previous_included_range = current_included_range - 1;
283 self->token_end_position = (Length) {
284 previous_included_range->end_byte,
285 previous_included_range->end_point,
286 };
287 return;
288 }
289 }
290 self->token_end_position = self->current_position;
291 }
292
293 static uint32_t ts_lexer__get_column(TSLexer *_self) {
294 Lexer *self = (Lexer *)_self;
295
296 self->did_get_column = true;
297
298 if (!self->column_data.valid) {
299 // Record current position
300 uint32_t goal_byte = self->current_position.bytes;
301
302 // Back up to the beginning of the line
303 Length start_of_col = {
304 self->current_position.bytes - self->current_position.extent.column,
305 {self->current_position.extent.row, 0},
306 };
307 ts_lexer_goto(self, start_of_col);
308 ts_lexer__set_column_data(self, 0);
309 ts_lexer__get_chunk(self);
310
311 if (!ts_lexer__eof(_self)) {
312 ts_lexer__get_lookahead(self);
313
314 // Advance to the recorded position
315 while (self->current_position.bytes < goal_byte && !ts_lexer__eof(_self) && self->chunk) {
316 ts_lexer__do_advance(self, false);
317 if (ts_lexer__eof(_self)) break;
318 }
319 }
320 }
321
322 return self->column_data.value;
323 }
324
325 // Is the lexer at a boundary between two disjoint included ranges of
326 // source code? This is exposed as an API because some languages' external
327 // scanners need to perform custom actions at these boundaries.
328 static bool ts_lexer__is_at_included_range_start(const TSLexer *_self) {
329 const Lexer *self = (const Lexer *)_self;
330 if (self->current_included_range_index < self->included_range_count) {
331 TSRange *current_range = &self->included_ranges[self->current_included_range_index];
332 return self->current_position.bytes == current_range->start_byte;
333 } else {
334 return false;
335 }
336 }
337
338 static void ts_lexer__log(const TSLexer *_self, const char *fmt, ...) {
339 Lexer *self = (Lexer *)_self;
340 va_list args;
341 va_start(args, fmt);
342 if (self->logger.log) {
343 vsnprintf(self->debug_buffer, TREE_SITTER_SERIALIZATION_BUFFER_SIZE, fmt, args);
344 self->logger.log(self->logger.payload, TSLogTypeLex, self->debug_buffer);
345 }
346 va_end(args);
347 }
348
349 void ts_lexer_init(Lexer *self) {
350 *self = (Lexer) {
351 .data = {
352 // The lexer's methods are stored as struct fields so that generated
353 // parsers can call them without needing to be linked against this
354 // library.
355 .advance = ts_lexer__advance,
356 .mark_end = ts_lexer__mark_end,
357 .get_column = ts_lexer__get_column,
358 .is_at_included_range_start = ts_lexer__is_at_included_range_start,
359 .eof = ts_lexer__eof,
360 .log = ts_lexer__log,
361 .lookahead = 0,
362 .result_symbol = 0,
363 },
364 .chunk = NULL,
365 .chunk_size = 0,
366 .chunk_start = 0,
367 .current_position = {0, {0, 0}},
368 .logger = {
369 .payload = NULL,
370 .log = NULL
371 },
372 .included_ranges = NULL,
373 .included_range_count = 0,
374 .current_included_range_index = 0,
375 .did_get_column = false,
376 .column_data = {
377 .valid = false,
378 .value = 0
379 }
380 };
381 ts_lexer_set_included_ranges(self, NULL, 0);
382 }
383
384 void ts_lexer_delete(Lexer *self) {
385 ts_free(self->included_ranges);
386 }
387
388 void ts_lexer_set_input(Lexer *self, TSInput input) {
389 self->input = input;
390 ts_lexer__clear_chunk(self);
391 ts_lexer_goto(self, self->current_position);
392 }
393
394 // Move the lexer to the given position. This doesn't do any work
395 // if the parser is already at the given position.
396 void ts_lexer_reset(Lexer *self, Length position) {
397 if (position.bytes != self->current_position.bytes) {
398 ts_lexer_goto(self, position);
399 }
400 }
401
402 void ts_lexer_start(Lexer *self) {
403 self->token_start_position = self->current_position;
404 self->token_end_position = LENGTH_UNDEFINED;
405 self->data.result_symbol = 0;
406 self->did_get_column = false;
407 if (!ts_lexer__eof(&self->data)) {
408 if (!self->chunk_size) ts_lexer__get_chunk(self);
409 if (!self->lookahead_size) ts_lexer__get_lookahead(self);
410 if (self->current_position.bytes == 0) {
411 if (self->data.lookahead == BYTE_ORDER_MARK) {
412 ts_lexer__advance(&self->data, true);
413 }
414 ts_lexer__set_column_data(self, 0);
415 }
416 }
417 }
418
419 void ts_lexer_finish(Lexer *self, uint32_t *lookahead_end_byte) {
420 if (length_is_undefined(self->token_end_position)) {
421 ts_lexer__mark_end(&self->data);
422 }
423
424 // If the token ended at an included range boundary, then its end position
425 // will have been reset to the end of the preceding range. Reset the start
426 // position to match.
427 if (self->token_end_position.bytes < self->token_start_position.bytes) {
428 self->token_start_position = self->token_end_position;
429 }
430
431 uint32_t current_lookahead_end_byte = self->current_position.bytes + 1;
432
433 // In order to determine that a byte sequence is invalid UTF8 or UTF16,
434 // the character decoding algorithm may have looked at the following byte.
435 // Therefore, the next byte *after* the current (invalid) character
436 // affects the interpretation of the current character.
437 if (self->data.lookahead == TS_DECODE_ERROR) {
438 current_lookahead_end_byte += 4; // the maximum number of bytes read to identify an invalid code point
439 }
440
441 if (current_lookahead_end_byte > *lookahead_end_byte) {
442 *lookahead_end_byte = current_lookahead_end_byte;
443 }
444 }
445
446 void ts_lexer_mark_end(Lexer *self) {
447 ts_lexer__mark_end(&self->data);
448 }
449
450 bool ts_lexer_set_included_ranges(
451 Lexer *self,
452 const TSRange *ranges,
453 uint32_t count
454 ) {
455 if (count == 0 || !ranges) {
456 ranges = &DEFAULT_RANGE;
457 count = 1;
458 } else {
459 uint32_t previous_byte = 0;
460 for (unsigned i = 0; i < count; i++) {
461 const TSRange *range = &ranges[i];
462 if (
463 range->start_byte < previous_byte ||
464 range->end_byte < range->start_byte
465 ) return false;
466 previous_byte = range->end_byte;
467 }
468 }
469
470 size_t size = count * sizeof(TSRange);
471 self->included_ranges = ts_realloc(self->included_ranges, size);
472 memcpy(self->included_ranges, ranges, size);
473 self->included_range_count = count;
474 ts_lexer_goto(self, self->current_position);
475 return true;
476 }
477
478 TSRange *ts_lexer_included_ranges(const Lexer *self, uint32_t *count) {
479 *count = self->included_range_count;
480 return self->included_ranges;
481 }
482
483 #undef LOG