-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.c
527 lines (456 loc) · 14.5 KB
/
scanner.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#include "tree_sitter/parser.h"
#include <string.h>
// Enable debug logging and assertions by setting the following line to `#if 0`
#if 1
#define printf(...)
#define assert(...)
#else
#include <assert.h>
#include <stdio.h>
#endif
enum TokenType {
NEWLINE,
BLOCK_START,
BLOCK_CONTINUE,
BLOCK_END,
MAP_BLOCK_START,
INDENTED_LINE,
COMMENT,
STRING_START,
STRING_END,
RAW_STRING_START,
RAW_STRING_END,
INTERPOLATION_START,
INTERPOLATION_END,
END_OF_FILE,
ERROR_SENTINEL,
};
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define VEC_RESIZE(vec, _cap) \
void* tmp = realloc((vec).data, (_cap) * sizeof((vec).data[0])); \
assert(tmp != NULL); \
(vec).data = tmp; \
(vec).cap = (_cap);
#define VEC_GROW(vec, _cap) \
if ((vec).cap < (_cap)) { \
VEC_RESIZE((vec), (_cap)); \
}
#define VEC_PUSH(vec, el) \
if ((vec).cap == (vec).len) { \
VEC_RESIZE((vec), MAX(16, (vec).len * 2)); \
} \
(vec).data[(vec).len++] = (el);
#define VEC_POP(vec) (vec).len--;
#define VEC_SIZE(vec) (vec).len
#define VEC_BACK(vec) ((vec).data[(vec).len - 1])
#define VEC_FREE(vec) \
if ((vec).data != NULL) { \
free((vec).data); \
(vec).data = NULL; \
}
#define VEC_CLEAR(vec) (vec).len = 0;
static inline void advance(TSLexer* lexer) {
lexer->advance(lexer, false);
}
static inline void skip(TSLexer* lexer) {
lexer->advance(lexer, true);
}
typedef struct {
uint32_t len;
uint32_t cap;
uint16_t* data;
} IndentVec;
typedef struct {
uint32_t len;
uint32_t cap;
char* data;
} QuoteVec;
typedef struct {
// Keeping track of the block indent levels
IndentVec indents;
// Keeping track of the quote type used for the current string
QuoteVec quotes;
// If the previous token was a block start or end,
// then subsequent block ends and continues can be generated without newlines.
bool block_level_just_changed;
// We need to block comments from being accepted while a string is being parsed.
// interpolated strings are parsed from grammar.js, and without disabling comments,
// "#" would be parsed as a string containing a comment rather than simply a string.
// Additionally, we need a separate flag rather than checking whether or not the quotes
// is empty; during an interpolated expression we're within quotes, while parsing
// non-string expressions.
bool in_string;
uint8_t raw_string_hash_count;
} Scanner;
static void initialize_scanner(Scanner* scanner) {
VEC_CLEAR(scanner->indents);
VEC_CLEAR(scanner->quotes);
scanner->block_level_just_changed = false;
scanner->in_string = false;
scanner->raw_string_hash_count = 0;
}
static void skip_whitespace(TSLexer* lexer) {
printf("skipping whitespace\n");
while (true) {
uint32_t next = lexer->lookahead;
if (next != ' ' && next != '\t') {
printf("...stopping (%u (%c))\n", next, next);
break;
}
printf("...skipping (%u)\n", next);
skip(lexer);
}
}
static void skip_string(TSLexer* lexer, bool multiline) {
const char end = lexer->lookahead;
advance(lexer);
while (true) {
const char next = lexer->lookahead;
switch (next) {
case '\'':
case '"':
if (next == end) {
advance(lexer);
return;
}
skip_string(lexer, multiline);
break;
case '\n':
if (!multiline) {
return;
}
}
advance(lexer);
}
}
static bool line_starts_with_map_key(TSLexer* lexer) {
// This is called at the first non-whitespace character,
// skip forward to see if the line starts with a map key,
const uint16_t line_start = lexer->get_column(lexer);
while (true) {
switch (lexer->lookahead) {
case ':':
return lexer->get_column(lexer) > line_start;
case '\'':
case '"':
skip_string(lexer, false);
break;
case '{': // prevent keys in inline maps from being detected
case '\n':
case '\0':
return false;
default:
skip(lexer);
}
}
return false;
}
static void consume_multiline_comment(TSLexer* lexer) {
while (true) {
switch (lexer->lookahead) {
case '-':
advance(lexer);
if (lexer->lookahead == '#') {
advance(lexer);
return;
}
break;
case '\0':
return;
default:
advance(lexer);
}
}
}
static void consume_comment(TSLexer* lexer) {
assert(lexer->lookahead == '#');
advance(lexer);
if (lexer->lookahead == '-') {
advance(lexer);
consume_multiline_comment(lexer);
} else {
while (!lexer->eof(lexer)) {
switch (lexer->lookahead) {
case '\n':
return;
}
advance(lexer);
}
}
}
bool tree_sitter_koto_external_scanner_scan(
void* payload,
TSLexer* lexer,
const bool* valid_symbols) {
printf(
"scanner.scan: column: %u, lookahead: %i (%c)\n",
lexer->get_column(lexer),
lexer->lookahead,
(char)lexer->lookahead);
Scanner* scanner = (Scanner*)payload;
printf("scanner.scan valid symbols:");
for (int i = 0; i <= ERROR_SENTINEL; i++) {
printf(" %u", valid_symbols[i]);
}
printf("\n");
const bool error_recovery = valid_symbols[ERROR_SENTINEL];
// Mark the end before doing anything else to allow lookahead to work correctly
lexer->mark_end(lexer);
// Skip any initial whitespace
skip_whitespace(lexer);
char next = lexer->lookahead;
if (error_recovery) {
printf("scanner.scan: in error recovery\n");
}
// String start/end detection
if (valid_symbols[RAW_STRING_START] && !scanner->in_string && next == 'r') {
advance(lexer);
uint8_t hash_count = 0;
while (lexer->lookahead == '#') {
if (hash_count == 255) {
printf("scanner.scan: reached raw string hash limit\n");
return false;
}
hash_count++;
advance(lexer);
}
next = lexer->lookahead;
if (next == '"' || next == '\'') {
printf(">>>> raw string start\n");
advance(lexer);
VEC_PUSH(scanner->quotes, next);
scanner->in_string = true;
scanner->raw_string_hash_count = hash_count;
lexer->mark_end(lexer);
lexer->result_symbol = RAW_STRING_START;
return true;
} else {
printf("scanner.scan: rejected raw string start\n");
return false;
}
} else if (
valid_symbols[RAW_STRING_END] && scanner->in_string
&& next == VEC_BACK(scanner->quotes)) {
printf(">>>> raw string end\n");
advance(lexer);
uint8_t hash_count = 0;
while (lexer->lookahead == '#') {
if (hash_count == 255) {
break;
}
hash_count++;
advance(lexer);
}
if (hash_count != scanner->raw_string_hash_count) {
printf("scanner.scan: rejected raw string end\n");
return false;
}
VEC_POP(scanner->quotes);
scanner->in_string = false;
scanner->raw_string_hash_count = 0;
lexer->mark_end(lexer);
lexer->result_symbol = RAW_STRING_END;
return true;
} else if (
valid_symbols[STRING_START] && !scanner->in_string
&& (next == '"' || next == '\'')) {
printf(">>>> string start\n");
advance(lexer);
lexer->mark_end(lexer);
scanner->in_string = true;
VEC_PUSH(scanner->quotes, next);
lexer->result_symbol = STRING_START;
return true;
} else if (
valid_symbols[STRING_END] && scanner->in_string
&& next == VEC_BACK(scanner->quotes)) {
printf(">>>> string end\n");
advance(lexer);
lexer->mark_end(lexer);
VEC_POP(scanner->quotes);
scanner->in_string = false;
lexer->result_symbol = STRING_END;
return true;
} else if (valid_symbols[INTERPOLATION_START]) {
printf(">>>> interpolation start\n");
assert(scanner->in_string || error_recovery);
scanner->in_string = false;
lexer->result_symbol = INTERPOLATION_START;
return true;
} else if (valid_symbols[INTERPOLATION_END] && next == '}') {
printf(">>>> interpolation end\n");
assert(!scanner->in_string || error_recovery);
scanner->in_string = true;
lexer->result_symbol = INTERPOLATION_END;
return true;
}
bool newline = false;
// Consume newlines and starting indentation
while (true) {
// new_indent = lexer->get_column(lexer);
if (lexer->lookahead == '\r') {
skip(lexer);
}
if (lexer->lookahead == '\n') {
skip(lexer);
newline = true;
} else {
break;
}
skip_whitespace(lexer);
}
const uint16_t column = lexer->get_column(lexer);
const uint16_t block_indent
= VEC_SIZE(scanner->indents) > 0 ? VEC_BACK(scanner->indents) : 0;
const bool block_just_changed = scanner->block_level_just_changed;
scanner->block_level_just_changed = false;
const bool eof = lexer->eof(lexer);
printf(
"scanner.scan: column: %u, block_indent: %u num_indents: %u newline: %i eof: %i\n",
column,
block_indent,
scanner->indents.len,
newline,
eof);
// Initial block?
if (valid_symbols[BLOCK_START] && VEC_SIZE(scanner->indents) == 0) {
printf(">>>> initial block start: %u\n", column);
VEC_PUSH(scanner->indents, column);
scanner->block_level_just_changed = true;
lexer->result_symbol = BLOCK_START;
return true;
}
// Block end?
// This should be detected before the following call to mark_end so that the
// end of the block is placed at the last token in the block rather than at the start of
// the following line.
else if (
valid_symbols[BLOCK_END]
&& (eof || (newline || block_just_changed) && column < block_indent)) {
printf(">>>> block end: %u\n", column);
VEC_POP(scanner->indents);
scanner->block_level_just_changed = true;
lexer->result_symbol = BLOCK_END;
return true;
}
// Mark the current end, telling the lexer that the consumed tokens are significant.
lexer->mark_end(lexer);
next = lexer->lookahead;
// Comment?
if (valid_symbols[COMMENT] && !scanner->in_string && next == '#') {
consume_comment(lexer);
lexer->mark_end(lexer);
lexer->result_symbol = COMMENT;
return true;
}
// Map block start?
else if (
valid_symbols[MAP_BLOCK_START] && newline && column > block_indent
&& line_starts_with_map_key(lexer)) {
printf(">>>> map block start: %u\n", column);
VEC_PUSH(scanner->indents, column);
scanner->block_level_just_changed = true;
lexer->result_symbol = MAP_BLOCK_START;
return true;
}
// Block start?
else if (valid_symbols[BLOCK_START] && newline && column > block_indent) {
printf(">>>> block start: %u\n", column);
VEC_PUSH(scanner->indents, column);
scanner->block_level_just_changed = true;
lexer->result_symbol = BLOCK_START;
return true;
}
// Block continue?
else if (
valid_symbols[BLOCK_CONTINUE] && !eof && (newline || block_just_changed)
&& column == block_indent) {
printf(">>>> block continue: %u\n", column);
lexer->result_symbol = BLOCK_CONTINUE;
return true;
}
// Indented line?
else if (valid_symbols[INDENTED_LINE] && newline && column > block_indent) {
printf(">>>> indented line\n");
lexer->result_symbol = INDENTED_LINE;
return true;
}
// Newline?
else if (valid_symbols[NEWLINE] && newline) {
printf(">>>> newline\n");
lexer->result_symbol = NEWLINE;
return true;
}
// EOF?
else if (valid_symbols[END_OF_FILE] && eof) {
printf(">>>> eof\n");
lexer->result_symbol = END_OF_FILE;
return true;
}
printf("scanner.scan: rejected\n");
return false;
}
unsigned tree_sitter_koto_external_scanner_serialize(void* payload, char* buffer) {
printf("scanner.serialize: payload %p, buffer %p\n", payload, buffer);
char* write_ptr = buffer;
Scanner* scanner = (Scanner*)payload;
const uint32_t num_indents = scanner->indents.len;
*((uint32_t*)write_ptr) = num_indents;
write_ptr += sizeof(uint32_t);
const unsigned indents_size = num_indents * sizeof(scanner->indents.data[0]);
memcpy(write_ptr, scanner->indents.data, indents_size);
write_ptr += indents_size;
const uint32_t num_quotes = scanner->quotes.len;
*((uint32_t*)write_ptr) = num_quotes;
write_ptr += sizeof(uint32_t);
memcpy(write_ptr, scanner->quotes.data, num_quotes);
write_ptr += num_quotes;
*write_ptr++ = scanner->block_level_just_changed;
*write_ptr++ = scanner->in_string;
*write_ptr++ = scanner->raw_string_hash_count;
return write_ptr - buffer;
}
void tree_sitter_koto_external_scanner_deserialize(
void* payload,
const char* buffer,
unsigned length) {
printf(
"scanner.deserialize: payload %p, buffer %p, length %d\n", payload, buffer, length);
Scanner* scanner = (Scanner*)payload;
initialize_scanner(scanner);
if (length > 0) {
const char* read_ptr = buffer;
const uint32_t num_indents = *(uint32_t*)read_ptr;
read_ptr += sizeof(uint32_t);
VEC_CLEAR(scanner->indents);
for (int i = 0; i < num_indents; i++) {
const uint16_t indent = *(uint16_t*)read_ptr;
read_ptr += sizeof(uint16_t);
VEC_PUSH(scanner->indents, indent);
}
const uint32_t num_quotes = *(uint32_t*)read_ptr;
read_ptr += sizeof(uint32_t);
VEC_CLEAR(scanner->quotes);
for (int i = 0; i < num_quotes; i++) {
VEC_PUSH(scanner->quotes, *read_ptr++);
}
scanner->block_level_just_changed = *read_ptr++;
scanner->in_string = *read_ptr++;
scanner->raw_string_hash_count = *read_ptr++;
printf("scanner.deserialize: in_string %i\n", scanner->in_string);
}
}
void* tree_sitter_koto_external_scanner_create() {
printf("scanner.create\n");
Scanner* scanner = calloc(1, sizeof(Scanner));
initialize_scanner(scanner);
printf("scanner.create: payload: %p\n", scanner);
return scanner;
}
void tree_sitter_koto_external_scanner_destroy(void* payload) {
printf("scanner.destroy: payload: %p\n", payload);
Scanner* scanner = (Scanner*)payload;
VEC_FREE(scanner->indents);
VEC_FREE(scanner->quotes);
free(scanner);
}