This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathscanner.c
508 lines (435 loc) · 14.2 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
#include <tree_sitter/parser.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
enum TokenType {
AUTOMATIC_SEPARATOR,
BRACED_INTERPOLATION_OPENING,
UNBRACED_INTERPOLATION_OPENING,
INTERPOLATION_CLOSING,
C_STRING_OPENING, // = 4
RAW_STRING_OPENING, // = 5
STRING_OPENING, // = 6
STRING_CONTENT,
STRING_CLOSING,
COMMENT,
NONE
};
enum StringType {
SINGLE_QUOTE = NONE + 1, // = 9 + 1 + 1 = 11
DOUBLE_QUOTE = NONE + 4, // = 9 + 1 + 4 = 14
};
enum StringTokenType {
C_SINGLE_QUOTE_OPENING = C_STRING_OPENING + SINGLE_QUOTE, // 5 + 11 = 16
C_DOUBLE_QUOTE_OPENING = C_STRING_OPENING + DOUBLE_QUOTE, // 5 + 14 = 19
RAW_SINGLE_QUOTE_OPENING = RAW_STRING_OPENING + SINGLE_QUOTE, // 4 + 11 = 15
RAW_DOUBLE_QUOTE_OPENING = RAW_STRING_OPENING + DOUBLE_QUOTE, // 4 + 14 = 18
SINGLE_QUOTE_OPENING = STRING_OPENING + SINGLE_QUOTE, // 6 + 11 = 17
DOUBLE_QUOTE_OPENING = STRING_OPENING + DOUBLE_QUOTE // 6 + 14 = 20
};
bool is_type_single_quote(uint8_t type) {
uint8_t orig_type = type - SINGLE_QUOTE;
return orig_type >= C_STRING_OPENING && orig_type <= STRING_OPENING;
}
bool is_type_double_quote(uint8_t type) {
uint8_t orig_type = type - DOUBLE_QUOTE;
return orig_type >= C_STRING_OPENING && orig_type <= STRING_OPENING;
}
bool is_type_string(uint8_t type) {
return is_type_single_quote(type) || is_type_double_quote(type);
}
uint8_t get_final_string_type(uint8_t type) {
if (is_type_single_quote(type)) {
return type - SINGLE_QUOTE;
} else if (is_type_double_quote(type)) {
return type - DOUBLE_QUOTE;
} else {
return type;
}
}
char expected_end_char(uint8_t type) {
if (is_type_single_quote(type)) {
return '\'';
} else if (is_type_double_quote(type)) {
return '"';
} else if (type == BRACED_INTERPOLATION_OPENING) {
return '}';
} else {
return '\0';
}
}
// Stack
typedef struct {
int top;
int init_size;
uint8_t *contents;
} Stack;
Stack *new_stack(int init_size) {
Stack *stack = malloc(sizeof(Stack));
stack->top = -1;
stack->init_size = init_size;
stack->contents = malloc(sizeof(uint8_t) * (init_size + 1));
return stack;
}
void stack_push(Stack *stack, uint8_t content) {
if (stack->top < stack->init_size) {
stack->contents[++stack->top] = content;
}
}
uint8_t stack_top(Stack *stack) {
if (stack->top >= 0) {
return stack->contents[stack->top];
}
// if stack is at -1;
return NONE;
}
uint8_t stack_pop(Stack *stack) {
if (stack->top >= 0) {
uint8_t current_top = stack_top(stack);
stack->contents[stack->top--] = NONE;
return current_top;
}
// if stack is at -1;
return NONE;
}
bool stack_empty(Stack *stack) {
return stack->top == -1;
}
void stack_serialize(Stack *stack, char *buffer, unsigned *n) {
int size = stack->top + 1;
unsigned i = *n;
buffer[i++] = stack->top;
buffer[i++] = stack->init_size;
if (size > 0) {
memcpy(&buffer[i], stack->contents, size);
i += size;
}
}
void stack_deserialize(Stack *stack, const char *buffer, unsigned *n, unsigned len) {
if (len == 0) return;
memset(stack->contents, 0, stack->init_size * sizeof(*stack->contents));
stack->top = buffer[(*n)++];
stack->init_size = buffer[(*n)++];
int size = stack->top + 1;
if (size > 0) {
memcpy(stack->contents, &buffer[*n], size);
(*n) += size;
}
}
// Utils
void tsv_advance(TSLexer *lexer) {
lexer->advance(lexer, false);
}
void ts_skip(TSLexer *lexer) {
lexer->advance(lexer, true);
}
bool is_separatable(char c) {
return c == '\r' || c == '\n' || c == '\t';
}
// Scanner
typedef struct {
bool initialized;
Stack *tokens;
} Scanner;
void push_type(Scanner *scanner, uint8_t token_type) {
stack_push(scanner->tokens, token_type);
}
bool scan_interpolation_opening(Scanner *scanner, TSLexer *lexer) {
if (lexer->lookahead != '$') {
return false;
}
tsv_advance(lexer);
uint8_t got_top = stack_top(scanner->tokens);
if (is_type_string(got_top) && lexer->lookahead == expected_end_char(got_top)) {
return false;
}
bool is_valid = false;
bool is_braced = false;
if (lexer->lookahead == '{') {
tsv_advance(lexer);
is_valid = true;
is_braced = true;
} else if (isalpha(lexer->lookahead)) {
// hopefully resolves issues regarding '$r' or '$c'
is_valid = true;
} else {
switch (lexer->lookahead) {
// just marked it here
case '$':
case '%':
case '(':
case '\\':
is_valid = false;
}
}
if (is_valid) {
lexer->mark_end(lexer);
if (is_braced) {
lexer->result_symbol = BRACED_INTERPOLATION_OPENING;
push_type(scanner, lexer->result_symbol);
} else {
lexer->result_symbol = UNBRACED_INTERPOLATION_OPENING;
}
}
return is_valid;
}
bool scan_interpolation_closing(Scanner *scanner, TSLexer *lexer) {
uint8_t got_top = stack_pop(scanner->tokens);
bool has_braced_closing = got_top == BRACED_INTERPOLATION_OPENING && lexer->lookahead == expected_end_char(got_top);
if (has_braced_closing || got_top == UNBRACED_INTERPOLATION_OPENING) {
if (has_braced_closing) {
tsv_advance(lexer);
}
lexer->result_symbol = INTERPOLATION_CLOSING;
return true;
}
return false;
}
bool scan_automatic_separator(Scanner *scanner, TSLexer *lexer) {
bool is_newline = false;
bool has_whitespace = false;
int tab_count = 0;
while (is_separatable(lexer->lookahead)) {
if (!has_whitespace) {
has_whitespace = true;
}
if (lexer->lookahead == '\r') {
tsv_advance(lexer);
lexer->mark_end(lexer);
}
if (!is_newline && lexer->lookahead == '\n') {
is_newline = true;
} else if (lexer->lookahead == '\t') {
tab_count++;
}
tsv_advance(lexer);
lexer->mark_end(lexer);
}
// true if tab count is 1 or below, false if above 1
bool needs_to_be_separated = tab_count <= 1;
// for multi-level blocks. not a good code. should be improved later.
if (has_whitespace) {
char got_char = lexer->lookahead;
switch (got_char) {
case '|':
case '&':
tsv_advance(lexer);
if (lexer->lookahead == got_char || !isalpha(lexer->lookahead)) {
needs_to_be_separated = false;
} else {
needs_to_be_separated = true;
}
break;
case '*':
case '_':
case '\'':
case '"':
needs_to_be_separated = true;
break;
case '/':
tsv_advance(lexer);
if (lexer->lookahead == got_char || lexer->lookahead == '*') {
needs_to_be_separated = true;
} else {
needs_to_be_separated = false;
}
default:
if (isalpha(lexer->lookahead)) {
needs_to_be_separated = true;
}
break;
}
}
if (is_newline && needs_to_be_separated) {
lexer->result_symbol = AUTOMATIC_SEPARATOR;
return true;
}
return false;
}
bool scan_string_opening(Scanner *scanner, TSLexer *lexer, bool is_quote, bool is_c, bool is_raw) {
if (is_raw && lexer->lookahead == 'r') {
lexer->result_symbol = RAW_STRING_OPENING;
tsv_advance(lexer);
} else if (is_c && lexer->lookahead == 'c') {
lexer->result_symbol = C_STRING_OPENING;
tsv_advance(lexer);
} else if (is_quote && (lexer->lookahead == '\'' || lexer->lookahead == '"')) {
lexer->result_symbol = STRING_OPENING;
} else {
return false;
}
if (lexer->lookahead == '\'' || lexer->lookahead == '"') {
uint8_t string_type = lexer->lookahead == '\'' ? SINGLE_QUOTE : DOUBLE_QUOTE;
tsv_advance(lexer);
lexer->mark_end(lexer);
push_type(scanner, lexer->result_symbol + string_type);
return true;
}
return false;
}
bool scan_string_content(Scanner *scanner, TSLexer *lexer) {
uint8_t got_top = stack_top(scanner->tokens);
if (stack_empty(scanner->tokens) || !is_type_string(got_top)) {
return false;
}
lexer->result_symbol = STRING_CONTENT;
bool is_raw = get_final_string_type(got_top) == RAW_STRING_OPENING;
bool has_content = false;
char quote_to_skip = expected_end_char(got_top);
for (; ; has_content = true) {
lexer->mark_end(lexer);
if (lexer->lookahead == '\0' || lexer->lookahead == quote_to_skip) {
return has_content;
}
if (!is_raw && (lexer->lookahead == '\\' || lexer->lookahead == '$')) {
return has_content;
}
tsv_advance(lexer);
}
return has_content;
}
bool scan_string_closing(Scanner *scanner, TSLexer *lexer) {
uint8_t got_top = stack_pop(scanner->tokens);
if (is_type_string(got_top) && lexer->lookahead == expected_end_char(got_top)) {
tsv_advance(lexer);
lexer->result_symbol = STRING_CLOSING;
return true;
}
return false;
}
bool scan_comment(Scanner *scanner, TSLexer *lexer) {
uint8_t got_top = stack_top(scanner->tokens);
if (is_type_string(got_top) || lexer->lookahead != '/') {
return false;
}
tsv_advance(lexer);
if (lexer->lookahead != '/' && lexer->lookahead != '*') {
return false;
}
bool is_multiline = lexer->lookahead == '*';
int nested_multiline_count = 0;
tsv_advance(lexer);
while (true) {
lexer->mark_end(lexer);
if (is_multiline) {
if (lexer->lookahead == '/') {
// Handles the "nested" comments (e.g. /* /* comment */ */)
tsv_advance(lexer);
if (lexer->lookahead == '*') {
tsv_advance(lexer);
lexer->mark_end(lexer);
nested_multiline_count++;
}
continue;
} else if (lexer->lookahead == '*') {
tsv_advance(lexer);
if (lexer->lookahead == '/') {
tsv_advance(lexer);
lexer->mark_end(lexer);
if (nested_multiline_count == 0) {
break;
} else {
nested_multiline_count--;
}
}
// do mark_end first before advancing
continue;
}
} else if (!is_multiline && (lexer->lookahead == '\r' || lexer->lookahead == '\n')) {
break;
}
if (lexer->lookahead == '\0') {
break;
}
tsv_advance(lexer);
}
lexer->result_symbol = COMMENT;
return true;
}
// Tree-Sitter external scanner functions
void *tree_sitter_v_external_scanner_create() {
Scanner *scanner = malloc(sizeof(Scanner));
scanner->initialized = true;
scanner->tokens = new_stack(10);
return scanner;
}
void tree_sitter_v_external_scanner_destroy(void *p) {
Scanner *scanner = (Scanner*) p;
if (scanner->tokens->top + 1 > 0) {
free(scanner->tokens->contents);
}
free(scanner->tokens);
free(scanner);
}
unsigned tree_sitter_v_external_scanner_serialize(void *p, char *buffer) {
unsigned i = 0;
Scanner *scanner = (Scanner*) p;
stack_serialize(scanner->tokens, buffer, &i);
return i;
}
void tree_sitter_v_external_scanner_deserialize(void *p, const char *buffer, unsigned n) {
Scanner *scanner = (Scanner*) p;
if (n > 0){
unsigned i = 0;
scanner->initialized = true;
stack_deserialize(scanner->tokens, buffer, &i, n);
} else {
scanner->initialized = false;
}
}
bool tree_sitter_v_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
if (lexer->lookahead == 0) {
// tsv_advance(lexer);
return false;
}
Scanner *scanner = (Scanner*) payload;
bool is_stack_empty = stack_empty(scanner->tokens);
uint8_t top = stack_top(scanner->tokens);
if (is_separatable(lexer->lookahead) && valid_symbols[AUTOMATIC_SEPARATOR] && is_stack_empty) {
return scan_automatic_separator(scanner, lexer);
} else if (is_stack_empty || top == BRACED_INTERPOLATION_OPENING) {
while (lexer->lookahead == ' ' || is_separatable(lexer->lookahead)) {
// skip only if whitespace
lexer->advance(lexer, true);
}
}
if (!is_type_string(top) && lexer->lookahead == '/' && valid_symbols[COMMENT]) {
return scan_comment(scanner, lexer);
}
if (
(
top == BRACED_INTERPOLATION_OPENING
|| top == UNBRACED_INTERPOLATION_OPENING
|| is_stack_empty
) && (
valid_symbols[C_STRING_OPENING]
|| valid_symbols[RAW_STRING_OPENING]
|| valid_symbols[STRING_OPENING]
)
) {
return scan_string_opening(
scanner,
lexer,
valid_symbols[STRING_OPENING],
valid_symbols[C_STRING_OPENING],
valid_symbols[RAW_STRING_OPENING]
);
} else {
while (isspace(lexer->lookahead)) {
tsv_advance(lexer);
}
if (valid_symbols[STRING_CLOSING] || valid_symbols[STRING_CONTENT] || valid_symbols[BRACED_INTERPOLATION_OPENING] || valid_symbols[UNBRACED_INTERPOLATION_OPENING] || valid_symbols[INTERPOLATION_CLOSING]) {
if (lexer->lookahead == expected_end_char(top)) {
if (valid_symbols[STRING_CLOSING]) {
return scan_string_closing(scanner, lexer);
} else if (valid_symbols[INTERPOLATION_CLOSING]) {
return scan_interpolation_closing(scanner, lexer);
}
} else if (lexer->lookahead == '$' && (valid_symbols[BRACED_INTERPOLATION_OPENING] || valid_symbols[UNBRACED_INTERPOLATION_OPENING])) {
return scan_interpolation_opening(scanner, lexer);
}
return scan_string_content(scanner, lexer);
}
}
return false;
}