forked from mtoohey31/tree-sitter-koka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.c
428 lines (371 loc) · 11.6 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
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <tree_sitter/parser.h>
enum TokenType {
OpenBrace,
CloseBrace,
Semi,
RawString,
EndContinuationSignal
};
struct scanner {
int close_braces_to_insert;
bool insert_open_brace;
int semis_to_insert;
bool no_final_semi_insert;
bool eof_semi_inserted;
bool push_layout_stack_after_open_brace;
size_t stack_len;
size_t stack_cap;
int *stack;
};
void scanner_reset(struct scanner *scanner) {
scanner->close_braces_to_insert = 0;
scanner->insert_open_brace = false;
scanner->semis_to_insert = 0;
scanner->no_final_semi_insert = false;
scanner->eof_semi_inserted = false;
scanner->push_layout_stack_after_open_brace = false;
scanner->stack_len = 0;
scanner->stack_cap = 0;
scanner->stack = NULL;
}
void scanner_push_indent(struct scanner *scanner, int indent_length) {
if (scanner->stack_len == scanner->stack_cap) {
// Full, so grow.
size_t new_stack_cap = scanner->stack_cap == 0 ? 8 : scanner->stack_cap * 2;
scanner->stack = realloc(scanner->stack, sizeof(int) * new_stack_cap);
assert(scanner->stack);
scanner->stack_cap = new_stack_cap;
}
scanner->stack[scanner->stack_len++] = indent_length;
}
int scanner_pop_indent(struct scanner *scanner) {
if (scanner->stack_len == 0) {
return -1;
}
return scanner->stack[--scanner->stack_len];
}
// Mixing tabs and spaces appears to be legal, and tabs seem to take up a visual
// width of 8 based on lexer.l. This seems dangerous, might want to look into
// this...
#define TABWIDTH 8
static inline void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
static inline void skip(TSLexer *lexer) { lexer->advance(lexer, true); }
static inline bool in_range(char c, char min, char max) {
return min <= c && c <= max;
}
static inline bool resolve_maybe_start_cont(TSLexer *lexer) {
switch (lexer->lookahead) {
case '>': // Excluding ">>", ">|<"
advance(lexer);
switch (lexer->lookahead) {
case '>': // ">>"
return false;
case '|':
advance(lexer);
return lexer->lookahead != '<'; // ">|<"
default:
return true;
}
case '<': // Also "<-", excluding "<<"
advance(lexer);
return lexer->lookahead != '<';
case 't': // For "then"
case 'e': // For "else" and "elif"
break;
default:
return false;
}
char word[4];
for (size_t i = 0; i < 4; i++) {
word[i] = lexer->lookahead;
advance(lexer);
}
if (strncmp(word, "then", 4) != 0 && strncmp(word, "else", 4) != 0 &&
strncmp(word, "elif", 4) != 0) {
// Then the starting characters don't match one of the possible start
// continuation characters, so return.
return false;
}
return !(in_range(lexer->lookahead, 'a', 'z') ||
in_range(lexer->lookahead, 'A', 'Z') ||
in_range(lexer->lookahead, '0', '9') || lexer->lookahead == '\'');
}
void *tree_sitter_koka_external_scanner_create() {
struct scanner *scanner = malloc(sizeof(struct scanner));
assert(scanner);
scanner_reset(scanner);
return scanner;
}
void tree_sitter_koka_external_scanner_destroy(void *payload) {
struct scanner *scanner = payload;
free(scanner->stack);
free(scanner);
}
unsigned tree_sitter_koka_external_scanner_serialize(void *payload,
char *buffer) {
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
_Static_assert(sizeof(struct scanner) <=
TREE_SITTER_SERIALIZATION_BUFFER_SIZE,
"serialization size is too small");
#else
assert(sizeof(struct scanner) <= TREE_SITTER_SERIALIZATION_BUFFER_SIZE &&
"serialization size is too small");
#endif
// This serializes the stack pointer too, but that's fine as long as we don't
// use it when deserializing, and desirable so we don't have to think about
// struct layout rules.
struct scanner *scanner = payload;
memcpy(buffer, scanner, sizeof(struct scanner));
memcpy(buffer + sizeof(struct scanner), scanner->stack,
sizeof(int) * scanner->stack_len);
return sizeof(struct scanner) + sizeof(int) * scanner->stack_len;
}
void tree_sitter_koka_external_scanner_deserialize(void *payload,
const char *buffer,
unsigned length) {
struct scanner *scanner = payload;
free(scanner->stack);
scanner_reset(scanner);
if (length == 0) {
return;
}
assert(length >= sizeof(struct scanner) && "invalid length");
memcpy(scanner, buffer, sizeof(struct scanner));
assert(scanner->stack_len ==
(length - sizeof(struct scanner)) / sizeof(int) &&
"invalid length");
scanner->stack_cap = scanner->stack_len;
if (scanner->stack_len == 0) {
scanner->stack = NULL;
return;
}
scanner->stack = malloc(scanner->stack_len * sizeof(int));
assert(scanner->stack);
memcpy(scanner->stack, buffer + sizeof(struct scanner),
scanner->stack_len * sizeof(int));
}
bool tree_sitter_koka_external_scanner_scan(void *payload, TSLexer *lexer,
const bool *valid_symbols) {
struct scanner *scanner = payload;
if (scanner->close_braces_to_insert >= scanner->semis_to_insert &&
scanner->close_braces_to_insert > 0) {
scanner->close_braces_to_insert--;
if (scanner->semis_to_insert == 1 && scanner->no_final_semi_insert) {
scanner->semis_to_insert = 0;
scanner->no_final_semi_insert = false;
}
lexer->result_symbol = CloseBrace;
return true;
}
if (scanner->semis_to_insert > 0) {
scanner->semis_to_insert--;
lexer->result_symbol = Semi;
return true;
}
if (scanner->insert_open_brace) {
scanner->insert_open_brace = false;
if (valid_symbols[OpenBrace])
lexer->result_symbol = OpenBrace;
return true;
}
lexer->mark_end(lexer);
// ' ', '\t', '\r', '\n'
bool found_eol = false;
int indent_length = 0;
while (true) {
switch (lexer->lookahead) {
case ' ':
indent_length++;
break;
case '\t':
indent_length += TABWIDTH;
break;
case '\r':
indent_length = 0;
break;
case '\n':
found_eol = true;
indent_length = 0;
break;
default:
goto AFTER_WHITESPACE;
}
skip(lexer);
}
AFTER_WHITESPACE:
if (scanner->push_layout_stack_after_open_brace) {
scanner_push_indent(scanner,
found_eol ? indent_length : lexer->get_column(lexer));
scanner->push_layout_stack_after_open_brace = false;
}
bool is_start_cont = false;
bool maybe_start_cont = false;
switch (lexer->lookahead) {
case '$':
case '%':
case '&':
case '*':
case '+':
case '@':
case '\\':
case '^':
case '?':
case '.':
case '=':
case ')':
case ']':
case '{':
case '}':
case ':': // Also ":="
case '-': // Also "->"
case '|': // Also "||"
// On this branch, we're sure that the token we encountered is a start
// continuation token.
is_start_cont = true;
break;
case '>': // Excluding ">>", ">|<"
case '<': // Also "<-", excluding "<<"
case 't': // For "then"
case 'e': // For "else" and "elif"
// On this branch, we're not sure, but it might be. Note that the lookahead
// token possibilities here are disjoint with those in the switch at the
// end, which is good because it means we can advance the lookahead to
// figure out what's going on, then skip the switch at the bottom because we
// know it wouldn't have produced anything.
maybe_start_cont = true;
break;
default:
// On this branch, we know the token can't be a start continuation token.
break;
};
if (found_eol) {
int prev_indent_length =
scanner->stack_len != 0 ? scanner->stack[scanner->stack_len - 1] : 0;
if (prev_indent_length < indent_length && valid_symbols[OpenBrace] &&
!valid_symbols[EndContinuationSignal] && !is_start_cont &&
(!maybe_start_cont || !resolve_maybe_start_cont(lexer))) {
assert(indent_length > prev_indent_length);
scanner_push_indent(scanner, indent_length);
lexer->result_symbol = OpenBrace;
return true;
} else if (prev_indent_length == indent_length && valid_symbols[Semi] &&
!valid_symbols[EndContinuationSignal] && !is_start_cont) {
lexer->result_symbol = Semi;
lexer->mark_end(lexer);
return !maybe_start_cont || !resolve_maybe_start_cont(lexer);
} else if (prev_indent_length > indent_length && valid_symbols[Semi] &&
lexer->lookahead != '}') {
lexer->mark_end(lexer);
while (scanner->stack_len != 0 &&
scanner->stack[scanner->stack_len - 1] > indent_length) {
scanner->close_braces_to_insert++;
scanner->semis_to_insert++;
scanner_pop_indent(scanner);
}
if (is_start_cont ||
(maybe_start_cont && resolve_maybe_start_cont(lexer))) {
scanner->no_final_semi_insert = true;
}
lexer->result_symbol = Semi;
prev_indent_length =
scanner->stack_len != 0 ? scanner->stack[scanner->stack_len - 1] : 0;
if (prev_indent_length < indent_length) {
scanner->insert_open_brace = true;
scanner_push_indent(scanner, indent_length);
scanner->no_final_semi_insert = true;
}
return true;
}
}
if (lexer->eof(lexer) && !scanner->eof_semi_inserted) {
scanner->eof_semi_inserted = true;
lexer->result_symbol = Semi;
return true;
}
if (maybe_start_cont) {
// See the comment on the branch of the switch that sets this.
return false;
}
switch (lexer->lookahead) {
case '{':
if (!valid_symbols[OpenBrace]) {
break;
}
lexer->result_symbol = OpenBrace;
advance(lexer);
lexer->mark_end(lexer);
assert(!scanner->push_layout_stack_after_open_brace &&
"encountered '{' before layout stack push for previous '{' was "
"handled");
scanner->push_layout_stack_after_open_brace = true;
return true;
case '}':
if (!valid_symbols[CloseBrace]) {
break;
}
advance(lexer);
lexer->mark_end(lexer);
indent_length = found_eol ? indent_length : lexer->get_column(lexer);
// do ... while ensures we pop at least one. We don't have to check
// scanner->stack_len != 0 before the first loop because that is guaranteed
// if valid_symbols[CloseBrace].
do {
scanner->close_braces_to_insert++;
scanner->semis_to_insert++;
scanner_pop_indent(scanner);
} while (scanner->stack_len != 0 &&
scanner->stack[scanner->stack_len - 1] > indent_length);
scanner->no_final_semi_insert = true;
lexer->result_symbol = Semi;
return true;
case ';':
if (!valid_symbols[Semi]) {
break;
}
lexer->result_symbol = Semi;
advance(lexer);
lexer->mark_end(lexer);
return true;
case 'r':
if (!valid_symbols[RawString]) {
break;
}
advance(lexer);
int pound_count = 0;
while (lexer->lookahead == '#') {
pound_count++;
advance(lexer);
}
if (lexer->lookahead != '"') {
return false;
}
while (!lexer->eof(lexer)) {
advance(lexer);
if (lexer->lookahead == '"') {
bool too_few_pounds = false;
for (int pounds_remaining = pound_count; pounds_remaining > 0;
pounds_remaining--) {
advance(lexer);
if (lexer->lookahead != '#') {
too_few_pounds = true;
break;
}
}
if (!too_few_pounds) {
break;
}
}
}
if (lexer->eof(lexer)) {
break;
}
lexer->result_symbol = RawString;
advance(lexer);
lexer->mark_end(lexer);
return true;
}
return false;
}