@constellationdev/cli 1.3.0 → 1.3.2

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.
@@ -0,0 +1,856 @@
1
+ #include "tree_sitter/parser.h"
2
+ #include <string.h>
3
+ #include <wctype.h>
4
+
5
+ #define TOKEN_COUNT 28
6
+
7
+ enum TokenType {
8
+ BLOCK_COMMENT,
9
+ RAW_STR_PART,
10
+ RAW_STR_CONTINUING_INDICATOR,
11
+ RAW_STR_END_PART,
12
+ IMPLICIT_SEMI,
13
+ EXPLICIT_SEMI,
14
+ ARROW_OPERATOR,
15
+ DOT_OPERATOR,
16
+ CONJUNCTION_OPERATOR,
17
+ DISJUNCTION_OPERATOR,
18
+ NIL_COALESCING_OPERATOR,
19
+ EQUAL_SIGN,
20
+ EQ_EQ,
21
+ PLUS_THEN_WS,
22
+ MINUS_THEN_WS,
23
+ BANG,
24
+ THROWS_KEYWORD,
25
+ RETHROWS_KEYWORD,
26
+ DEFAULT_KEYWORD,
27
+ WHERE_KEYWORD,
28
+ ELSE_KEYWORD,
29
+ CATCH_KEYWORD,
30
+ AS_KEYWORD,
31
+ AS_QUEST,
32
+ AS_BANG,
33
+ ASYNC_KEYWORD,
34
+ CUSTOM_OPERATOR,
35
+ FAKE_TRY_BANG
36
+ };
37
+
38
+ #define OPERATOR_COUNT 20
39
+
40
+ const char* OPERATORS[OPERATOR_COUNT] = {
41
+ "->",
42
+ ".",
43
+ "&&",
44
+ "||",
45
+ "??",
46
+ "=",
47
+ "==",
48
+ "+",
49
+ "-",
50
+ "!",
51
+ "throws",
52
+ "rethrows",
53
+ "default",
54
+ "where",
55
+ "else",
56
+ "catch",
57
+ "as",
58
+ "as?",
59
+ "as!",
60
+ "async"
61
+ };
62
+
63
+ enum IllegalTerminatorGroup {
64
+ ALPHANUMERIC,
65
+ OPERATOR_SYMBOLS,
66
+ OPERATOR_OR_DOT,
67
+ NON_WHITESPACE
68
+ };
69
+
70
+ const enum IllegalTerminatorGroup OP_ILLEGAL_TERMINATORS[OPERATOR_COUNT] = {
71
+ OPERATOR_SYMBOLS, // ->
72
+ OPERATOR_OR_DOT, // .
73
+ OPERATOR_SYMBOLS, // &&
74
+ OPERATOR_SYMBOLS, // ||
75
+ OPERATOR_SYMBOLS, // ??
76
+ OPERATOR_SYMBOLS, // =
77
+ OPERATOR_SYMBOLS, // ==
78
+ NON_WHITESPACE, // +
79
+ NON_WHITESPACE, // -
80
+ OPERATOR_SYMBOLS, // !
81
+ ALPHANUMERIC, // throws
82
+ ALPHANUMERIC, // rethrows
83
+ ALPHANUMERIC, // default
84
+ ALPHANUMERIC, // where
85
+ ALPHANUMERIC, // else
86
+ ALPHANUMERIC, // catch
87
+ ALPHANUMERIC, // as
88
+ OPERATOR_SYMBOLS, // as?
89
+ OPERATOR_SYMBOLS, // as!
90
+ ALPHANUMERIC // async
91
+ };
92
+
93
+ const enum TokenType OP_SYMBOLS[OPERATOR_COUNT] = {
94
+ ARROW_OPERATOR,
95
+ DOT_OPERATOR,
96
+ CONJUNCTION_OPERATOR,
97
+ DISJUNCTION_OPERATOR,
98
+ NIL_COALESCING_OPERATOR,
99
+ EQUAL_SIGN,
100
+ EQ_EQ,
101
+ PLUS_THEN_WS,
102
+ MINUS_THEN_WS,
103
+ BANG,
104
+ THROWS_KEYWORD,
105
+ RETHROWS_KEYWORD,
106
+ DEFAULT_KEYWORD,
107
+ WHERE_KEYWORD,
108
+ ELSE_KEYWORD,
109
+ CATCH_KEYWORD,
110
+ AS_KEYWORD,
111
+ AS_QUEST,
112
+ AS_BANG,
113
+ ASYNC_KEYWORD
114
+ };
115
+
116
+ const uint64_t OP_SYMBOL_SUPPRESSOR[OPERATOR_COUNT] = {
117
+ 0, // ARROW_OPERATOR,
118
+ 0, // DOT_OPERATOR,
119
+ 0, // CONJUNCTION_OPERATOR,
120
+ 0, // DISJUNCTION_OPERATOR,
121
+ 0, // NIL_COALESCING_OPERATOR,
122
+ 0, // EQUAL_SIGN,
123
+ 0, // EQ_EQ,
124
+ 0, // PLUS_THEN_WS,
125
+ 0, // MINUS_THEN_WS,
126
+ 1 << FAKE_TRY_BANG, // BANG,
127
+ 0, // THROWS_KEYWORD,
128
+ 0, // RETHROWS_KEYWORD,
129
+ 0, // DEFAULT_KEYWORD,
130
+ 0, // WHERE_KEYWORD,
131
+ 0, // ELSE_KEYWORD,
132
+ 0, // CATCH_KEYWORD,
133
+ 0, // AS_KEYWORD,
134
+ 0, // AS_QUEST,
135
+ 0, // AS_BANG,
136
+ 0, // ASYNC_KEYWORD
137
+ };
138
+
139
+ #define RESERVED_OP_COUNT 31
140
+
141
+ const char* RESERVED_OPS[RESERVED_OP_COUNT] = {
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
+ bool is_cross_semi_token(enum TokenType op) {
176
+ switch(op) {
177
+ case ARROW_OPERATOR:
178
+ case DOT_OPERATOR:
179
+ case CONJUNCTION_OPERATOR:
180
+ case DISJUNCTION_OPERATOR:
181
+ case NIL_COALESCING_OPERATOR:
182
+ case EQUAL_SIGN:
183
+ case EQ_EQ:
184
+ case PLUS_THEN_WS:
185
+ case MINUS_THEN_WS:
186
+ case THROWS_KEYWORD:
187
+ case RETHROWS_KEYWORD:
188
+ case DEFAULT_KEYWORD:
189
+ case WHERE_KEYWORD:
190
+ case ELSE_KEYWORD:
191
+ case CATCH_KEYWORD:
192
+ case AS_KEYWORD:
193
+ case AS_QUEST:
194
+ case AS_BANG:
195
+ case ASYNC_KEYWORD:
196
+ case CUSTOM_OPERATOR:
197
+ return true;
198
+ case BANG:
199
+ default:
200
+ return false;
201
+ }
202
+ }
203
+
204
+ #define NON_CONSUMING_CROSS_SEMI_CHAR_COUNT 3
205
+ const uint32_t NON_CONSUMING_CROSS_SEMI_CHARS[NON_CONSUMING_CROSS_SEMI_CHAR_COUNT] = { '?', ':', '{' };
206
+
207
+ /**
208
+ * All possible results of having performed some sort of parsing.
209
+ *
210
+ * A parser can return a result along two dimensions:
211
+ * 1. Should the scanner continue trying to find another result?
212
+ * 2. Was some result produced by this parsing attempt?
213
+ *
214
+ * These are flattened into a single enum together. When the function returns one of the `TOKEN_FOUND` cases, it
215
+ * will always populate its `symbol_result` field. When it returns one of the `STOP_PARSING` cases, callers should
216
+ * immediately return (with the value, if there is one).
217
+ */
218
+ enum ParseDirective {
219
+ CONTINUE_PARSING_NOTHING_FOUND,
220
+ CONTINUE_PARSING_TOKEN_FOUND,
221
+ CONTINUE_PARSING_SLASH_CONSUMED,
222
+ STOP_PARSING_NOTHING_FOUND,
223
+ STOP_PARSING_TOKEN_FOUND,
224
+ STOP_PARSING_END_OF_FILE
225
+ };
226
+
227
+ struct ScannerState {
228
+ uint32_t ongoing_raw_str_hash_count;
229
+ };
230
+
231
+ void *tree_sitter_swift_external_scanner_create() {
232
+ return calloc(0, sizeof(struct ScannerState));
233
+ }
234
+
235
+ void tree_sitter_swift_external_scanner_destroy(void *payload) {
236
+ free(payload);
237
+ }
238
+
239
+ void tree_sitter_swift_external_scanner_reset(void *payload) {
240
+ struct ScannerState *state = (struct ScannerState *)payload;
241
+ state->ongoing_raw_str_hash_count = 0;
242
+ }
243
+
244
+ unsigned tree_sitter_swift_external_scanner_serialize(void *payload, char *buffer) {
245
+ struct ScannerState *state = (struct ScannerState *)payload;
246
+ uint32_t hash_count = state->ongoing_raw_str_hash_count;
247
+ buffer[0] = (hash_count >> 24) & 0xff;
248
+ buffer[1] = (hash_count >> 16) & 0xff;
249
+ buffer[2] = (hash_count >> 8) & 0xff;
250
+ buffer[3] = (hash_count) & 0xff;
251
+ return 4;
252
+ }
253
+
254
+ void tree_sitter_swift_external_scanner_deserialize(
255
+ void *payload,
256
+ const char *buffer,
257
+ unsigned length
258
+ ) {
259
+ if (length < 4) {
260
+ return;
261
+ }
262
+
263
+ uint32_t hash_count = (
264
+ (((uint32_t) buffer[0]) << 24) |
265
+ (((uint32_t) buffer[1]) << 16) |
266
+ (((uint32_t) buffer[2]) << 8) |
267
+ (((uint32_t) buffer[3]))
268
+ );
269
+ struct ScannerState *state = (struct ScannerState *)payload;
270
+ state->ongoing_raw_str_hash_count = hash_count;
271
+ }
272
+
273
+ static void advance(TSLexer *lexer) {
274
+ lexer->advance(lexer, false);
275
+ }
276
+
277
+ static bool should_treat_as_wspace(int32_t character) {
278
+ return iswspace(character) || (((int32_t) ';') == character);
279
+ }
280
+
281
+ static int32_t encountered_op_count(bool *encountered_operator) {
282
+ int32_t encountered = 0;
283
+ for (int op_idx = 0; op_idx < OPERATOR_COUNT; op_idx++) {
284
+ if (encountered_operator[op_idx]) {
285
+ encountered++;
286
+ }
287
+ }
288
+
289
+ return encountered;
290
+ }
291
+
292
+ static bool any_reserved_ops(uint8_t *encountered_reserved_ops) {
293
+ for (int op_idx = 0; op_idx < RESERVED_OP_COUNT; op_idx++) {
294
+ if (encountered_reserved_ops[op_idx] == 2) {
295
+ return true;
296
+ }
297
+ }
298
+
299
+ return false;
300
+ }
301
+
302
+ static bool is_legal_custom_operator(
303
+ int32_t char_idx,
304
+ int32_t first_char,
305
+ int32_t cur_char
306
+ ) {
307
+ bool is_first_char = !char_idx;
308
+ switch (cur_char) {
309
+ case '=':
310
+ case '-':
311
+ case '+':
312
+ case '!':
313
+ case '%':
314
+ case '<':
315
+ case '>':
316
+ case '&':
317
+ case '|':
318
+ case '^':
319
+ case '?':
320
+ case '~':
321
+ return true;
322
+ case '.':
323
+ // Grammar allows `.` for any operator that starts with `.`
324
+ return is_first_char || first_char == '.';
325
+ case '*':
326
+ case '/':
327
+ // Not listed in the grammar, but `/*` and `//` can't be the start of an operator since they start comments
328
+ return char_idx != 1 || first_char != '/';
329
+ default:
330
+ if (
331
+ (cur_char >= 0x00A1 && cur_char <= 0x00A7) ||
332
+ (cur_char == 0x00A9) ||
333
+ (cur_char == 0x00AB) ||
334
+ (cur_char == 0x00AC) ||
335
+ (cur_char == 0x00AE) ||
336
+ (cur_char >= 0x00B0 && cur_char <= 0x00B1) ||
337
+ (cur_char == 0x00B6) ||
338
+ (cur_char == 0x00BB) ||
339
+ (cur_char == 0x00BF) ||
340
+ (cur_char == 0x00D7) ||
341
+ (cur_char == 0x00F7) ||
342
+ (cur_char >= 0x2016 && cur_char <= 0x2017) ||
343
+ (cur_char >= 0x2020 && cur_char <= 0x2027) ||
344
+ (cur_char >= 0x2030 && cur_char <= 0x203E) ||
345
+ (cur_char >= 0x2041 && cur_char <= 0x2053) ||
346
+ (cur_char >= 0x2055 && cur_char <= 0x205E) ||
347
+ (cur_char >= 0x2190 && cur_char <= 0x23FF) ||
348
+ (cur_char >= 0x2500 && cur_char <= 0x2775) ||
349
+ (cur_char >= 0x2794 && cur_char <= 0x2BFF) ||
350
+ (cur_char >= 0x2E00 && cur_char <= 0x2E7F) ||
351
+ (cur_char >= 0x3001 && cur_char <= 0x3003) ||
352
+ (cur_char >= 0x3008 && cur_char <= 0x3020) ||
353
+ (cur_char == 0x3030)
354
+ ) {
355
+ return true;
356
+ } else if (
357
+ (cur_char >= 0x0300 && cur_char <= 0x036f) ||
358
+ (cur_char >= 0x1DC0 && cur_char <= 0x1DFF) ||
359
+ (cur_char >= 0x20D0 && cur_char <= 0x20FF) ||
360
+ (cur_char >= 0xFE00 && cur_char <= 0xFE0F) ||
361
+ (cur_char >= 0xFE20 && cur_char <= 0xFE2F) ||
362
+ (cur_char >= 0xE0100 && cur_char <= 0xE01EF)
363
+ ) {
364
+ return !is_first_char;
365
+ } else {
366
+ return false;
367
+ }
368
+ }
369
+ }
370
+
371
+ static bool eat_operators(
372
+ TSLexer *lexer,
373
+ const bool *valid_symbols,
374
+ bool mark_end,
375
+ const int32_t prior_char,
376
+ enum TokenType *symbol_result
377
+ ) {
378
+ bool possible_operators[OPERATOR_COUNT];
379
+ uint8_t reserved_operators[RESERVED_OP_COUNT];
380
+ for (int op_idx = 0; op_idx < OPERATOR_COUNT; op_idx++) {
381
+ possible_operators[op_idx] = valid_symbols[OP_SYMBOLS[op_idx]] && (!prior_char || OPERATORS[op_idx][0] == prior_char);
382
+ }
383
+ for (int op_idx = 0; op_idx < RESERVED_OP_COUNT; op_idx++) {
384
+ reserved_operators[op_idx] = !prior_char || RESERVED_OPS[op_idx][0] == prior_char;
385
+ }
386
+
387
+ bool possible_custom_operator = valid_symbols[CUSTOM_OPERATOR];
388
+ int32_t first_char = prior_char ? prior_char : lexer->lookahead;
389
+ int32_t last_examined_char = first_char;
390
+
391
+ int32_t str_idx = prior_char ? 1 : 0;
392
+ int32_t full_match = -1;
393
+ while(true) {
394
+ for (int op_idx = 0; op_idx < OPERATOR_COUNT; op_idx++) {
395
+ if (!possible_operators[op_idx]) {
396
+ continue;
397
+ }
398
+
399
+ if (OPERATORS[op_idx][str_idx] == '\0') {
400
+ // Make sure that the operator is allowed to have the next character as its lookahead.
401
+ enum IllegalTerminatorGroup illegal_terminators = OP_ILLEGAL_TERMINATORS[op_idx];
402
+ switch (lexer->lookahead) {
403
+ // See "Operators":
404
+ // https://docs.swift.org/swift-book/ReferenceManual/LexicalStructure.html#ID418
405
+ case '/':
406
+ case '=':
407
+ case '-':
408
+ case '+':
409
+ case '!':
410
+ case '*':
411
+ case '%':
412
+ case '<':
413
+ case '>':
414
+ case '&':
415
+ case '|':
416
+ case '^':
417
+ case '?':
418
+ case '~':
419
+ if (illegal_terminators == OPERATOR_SYMBOLS) {
420
+ break;
421
+ } // Otherwise, intentionally fall through to the OPERATOR_OR_DOT case
422
+ // fall through
423
+ case '.':
424
+ if (illegal_terminators == OPERATOR_OR_DOT) {
425
+ break;
426
+ } // Otherwise, fall through to DEFAULT which checks its groups directly
427
+ // fall through
428
+ default:
429
+ if (iswalnum(lexer->lookahead) && illegal_terminators == ALPHANUMERIC) {
430
+ break;
431
+ }
432
+
433
+ if (!iswspace(lexer->lookahead) && illegal_terminators == NON_WHITESPACE) {
434
+ break;
435
+ }
436
+
437
+ full_match = op_idx;
438
+ if (mark_end) {
439
+ lexer->mark_end(lexer);
440
+ }
441
+ }
442
+
443
+ possible_operators[op_idx] = false;
444
+ continue;
445
+ }
446
+
447
+ if (OPERATORS[op_idx][str_idx] != lexer->lookahead) {
448
+ possible_operators[op_idx] = false;
449
+ continue;
450
+ }
451
+ }
452
+
453
+ for (int op_idx = 0; op_idx < RESERVED_OP_COUNT; op_idx++) {
454
+ if (!reserved_operators[op_idx]) {
455
+ continue;
456
+ }
457
+
458
+ if (RESERVED_OPS[op_idx][str_idx] == '\0') {
459
+ reserved_operators[op_idx] = 0;
460
+ continue;
461
+ }
462
+
463
+ if (RESERVED_OPS[op_idx][str_idx] != lexer->lookahead) {
464
+ reserved_operators[op_idx] = 0;
465
+ continue;
466
+ }
467
+
468
+ if (RESERVED_OPS[op_idx][str_idx + 1] == '\0') {
469
+ reserved_operators[op_idx] = 2;
470
+ continue;
471
+ }
472
+ }
473
+
474
+ possible_custom_operator = possible_custom_operator && is_legal_custom_operator(
475
+ str_idx,
476
+ first_char,
477
+ lexer->lookahead
478
+ );
479
+
480
+ uint32_t encountered_ops = encountered_op_count(possible_operators);
481
+ if (encountered_ops == 0) {
482
+ if (!possible_custom_operator) {
483
+ break;
484
+ } else if (mark_end && full_match == -1) {
485
+ lexer->mark_end(lexer);
486
+ }
487
+ }
488
+
489
+ last_examined_char = lexer->lookahead;
490
+ lexer->advance(lexer, false);
491
+ str_idx += 1;
492
+
493
+ if (encountered_ops == 0 && !is_legal_custom_operator(
494
+ str_idx,
495
+ first_char,
496
+ lexer->lookahead
497
+ )) {
498
+ break;
499
+ }
500
+ }
501
+
502
+ if (full_match != -1) {
503
+ // We have a match -- first see if that match has a symbol that suppresses it. For example, in `try!`, we do not
504
+ // want to emit the `!` as a symbol in our scanner, because we want the parser to have the chance to parse it as
505
+ // an immediate token.
506
+ uint64_t suppressing_symbols = OP_SYMBOL_SUPPRESSOR[full_match];
507
+ if (suppressing_symbols) {
508
+ for (uint64_t suppressor = 0; suppressor < TOKEN_COUNT; suppressor++) {
509
+ if (!(suppressing_symbols & 1 << suppressor)) {
510
+ continue;
511
+ }
512
+
513
+ // The suppressing symbol is valid in this position, so skip it.
514
+ if (valid_symbols[suppressor]) {
515
+ return false;
516
+ }
517
+ }
518
+ }
519
+ *symbol_result = OP_SYMBOLS[full_match];
520
+ return true;
521
+ }
522
+
523
+ if (possible_custom_operator && !any_reserved_ops(reserved_operators)) {
524
+ if ((last_examined_char != '<' || iswspace(lexer->lookahead)) && mark_end) {
525
+ lexer->mark_end(lexer);
526
+ }
527
+ *symbol_result = CUSTOM_OPERATOR;
528
+ return true;
529
+ }
530
+
531
+ return false;
532
+ }
533
+
534
+ static enum ParseDirective eat_comment(
535
+ TSLexer *lexer,
536
+ const bool *valid_symbols,
537
+ bool mark_end,
538
+ enum TokenType *symbol_result
539
+ ) {
540
+ if (lexer->lookahead != '/') {
541
+ return CONTINUE_PARSING_NOTHING_FOUND;
542
+ }
543
+
544
+ advance(lexer);
545
+
546
+ if (lexer->lookahead != '*') {
547
+ return CONTINUE_PARSING_SLASH_CONSUMED;
548
+ }
549
+
550
+ advance(lexer);
551
+
552
+ bool after_star = false;
553
+ unsigned nesting_depth = 1;
554
+ for (;;) {
555
+ switch (lexer->lookahead) {
556
+ case '\0':
557
+ return STOP_PARSING_END_OF_FILE;
558
+ case '*':
559
+ advance(lexer);
560
+ after_star = true;
561
+ break;
562
+ case '/':
563
+ if (after_star) {
564
+ advance(lexer);
565
+ after_star = false;
566
+ nesting_depth--;
567
+ if (nesting_depth == 0) {
568
+ if (mark_end) {
569
+ lexer->mark_end(lexer);
570
+ }
571
+ *symbol_result = BLOCK_COMMENT;
572
+ return STOP_PARSING_TOKEN_FOUND;
573
+ }
574
+ } else {
575
+ advance(lexer);
576
+ after_star = false;
577
+ if (lexer->lookahead == '*') {
578
+ nesting_depth++;
579
+ advance(lexer);
580
+ }
581
+ }
582
+ break;
583
+ default:
584
+ advance(lexer);
585
+ after_star = false;
586
+ break;
587
+ }
588
+ }
589
+ }
590
+
591
+ static enum ParseDirective eat_whitespace(
592
+ TSLexer *lexer,
593
+ const bool *valid_symbols,
594
+ enum TokenType *symbol_result
595
+ ) {
596
+ enum ParseDirective ws_directive = CONTINUE_PARSING_NOTHING_FOUND;
597
+ bool semi_is_valid = valid_symbols[IMPLICIT_SEMI] && valid_symbols[EXPLICIT_SEMI];
598
+ uint32_t lookahead;
599
+ while (should_treat_as_wspace(lookahead = lexer->lookahead)) {
600
+ if (lookahead == ';') {
601
+ if (semi_is_valid) {
602
+ ws_directive = STOP_PARSING_TOKEN_FOUND;
603
+ lexer->advance(lexer, false);
604
+ }
605
+
606
+ break;
607
+ }
608
+
609
+ lexer->advance(lexer, true);
610
+
611
+ lexer->mark_end(lexer);
612
+
613
+ if (ws_directive == CONTINUE_PARSING_NOTHING_FOUND && (lookahead == '\n' || lookahead == '\r')) {
614
+ ws_directive = CONTINUE_PARSING_TOKEN_FOUND;
615
+ }
616
+ }
617
+
618
+ enum ParseDirective any_comment = CONTINUE_PARSING_NOTHING_FOUND;
619
+ if (ws_directive == CONTINUE_PARSING_TOKEN_FOUND && lookahead == '/') {
620
+ bool has_seen_single_comment = false;
621
+ while (lexer->lookahead == '/') {
622
+ // It's possible that this is a comment - start an exploratory mission to find out, and if it is, look for what
623
+ // comes after it. We care about what comes after it for the purpose of suppressing the newline.
624
+
625
+ enum TokenType multiline_comment_result;
626
+ any_comment = eat_comment(lexer, valid_symbols, /* mark_end */ false, &multiline_comment_result);
627
+ if (any_comment == STOP_PARSING_TOKEN_FOUND) {
628
+ // This is a multiline comment. This scanner should be parsing those, so we might want to bail out and
629
+ // emit it instead. However, we only want to do that if we haven't advanced through a _single_ line
630
+ // comment on the way - otherwise that will get lumped into this.
631
+ if (!has_seen_single_comment) {
632
+ lexer->mark_end(lexer);
633
+ *symbol_result = multiline_comment_result;
634
+ return STOP_PARSING_TOKEN_FOUND;
635
+ }
636
+ } else if (any_comment == STOP_PARSING_END_OF_FILE) {
637
+ return STOP_PARSING_END_OF_FILE;
638
+ } else if (any_comment == CONTINUE_PARSING_SLASH_CONSUMED) {
639
+ // We accidentally ate a slash -- we should actually bail out, say we saw nothing, and let the next pass
640
+ // take it from after the newline.
641
+ return CONTINUE_PARSING_SLASH_CONSUMED;
642
+ } else if (lexer->lookahead == '/') {
643
+ // There wasn't a multiline comment, which we know means that the comment parser ate its `/` and then
644
+ // bailed out. If it had seen anything comment-like after that first `/` it would have continued going
645
+ // and eventually had a well-formed comment or an EOF. Thus, if we're currently looking at a `/`, it's
646
+ // the second one of those and it means we have a single-line comment.
647
+ has_seen_single_comment = true;
648
+ while (lexer->lookahead != '\n' && lexer->lookahead != '\0') {
649
+ lexer->advance(lexer, true);
650
+ }
651
+ } else if (iswspace(lexer->lookahead)) {
652
+ // We didn't see any type of comment - in fact, we saw an operator that we don't normally treat as an
653
+ // operator. Still, this is a reason to stop parsing.
654
+ return STOP_PARSING_NOTHING_FOUND;
655
+ }
656
+
657
+ // If we skipped through some comment, we're at whitespace now, so advance.
658
+ while(iswspace(lexer->lookahead)) {
659
+ any_comment = CONTINUE_PARSING_NOTHING_FOUND; // We're advancing, so clear out the comment
660
+ lexer->advance(lexer, true);
661
+ }
662
+ }
663
+
664
+ enum TokenType operator_result;
665
+ bool saw_operator = eat_operators(
666
+ lexer,
667
+ valid_symbols,
668
+ /* mark_end */ false,
669
+ '\0',
670
+ &operator_result
671
+ );
672
+ if (saw_operator) {
673
+ // The operator we saw should suppress the newline, so bail out.
674
+ return STOP_PARSING_NOTHING_FOUND;
675
+ } else {
676
+ // Promote the implicit newline to an explicit one so we don't check for operators again.
677
+ *symbol_result = IMPLICIT_SEMI;
678
+ ws_directive = STOP_PARSING_TOKEN_FOUND;
679
+ }
680
+ }
681
+
682
+ // Let's consume operators that can live after a "semicolon" style newline. Before we do that, though, we want to
683
+ // check for a set of characters that we do not consume, but that still suppress the semi.
684
+ if (ws_directive == CONTINUE_PARSING_TOKEN_FOUND) {
685
+ for (int i = 0; i < NON_CONSUMING_CROSS_SEMI_CHAR_COUNT; i++) {
686
+ if (NON_CONSUMING_CROSS_SEMI_CHARS[i] == lookahead) {
687
+ return CONTINUE_PARSING_NOTHING_FOUND;
688
+ }
689
+ }
690
+ }
691
+
692
+ if (semi_is_valid && ws_directive != CONTINUE_PARSING_NOTHING_FOUND) {
693
+ *symbol_result = lookahead == ';' ? EXPLICIT_SEMI : IMPLICIT_SEMI;
694
+ return ws_directive;
695
+ }
696
+
697
+ return CONTINUE_PARSING_NOTHING_FOUND;
698
+ }
699
+
700
+ static bool eat_raw_str_part(
701
+ struct ScannerState *state,
702
+ TSLexer *lexer,
703
+ const bool *valid_symbols,
704
+ enum TokenType *symbol_result
705
+ ) {
706
+ uint32_t hash_count = state->ongoing_raw_str_hash_count;
707
+ if (!valid_symbols[RAW_STR_PART]) {
708
+ return false;
709
+ } else if (hash_count == 0) {
710
+ // If this is a raw_str_part, it's the first one - look for hashes
711
+ while (lexer->lookahead == '#') {
712
+ hash_count += 1;
713
+ advance(lexer);
714
+ }
715
+
716
+ if (hash_count == 0) {
717
+ return false;
718
+ }
719
+
720
+ if (lexer->lookahead == '"') {
721
+ advance(lexer);
722
+ } else {
723
+ return false;
724
+ }
725
+
726
+ } else if (valid_symbols[RAW_STR_CONTINUING_INDICATOR]) {
727
+ // This is the end of an interpolation - now it's another raw_str_part. This is a synthetic
728
+ // marker to tell us that the grammar just consumed a `(` symbol to close a raw
729
+ // interpolation (since we don't want to fire on every `(` in existence). We don't have
730
+ // anything to do except continue.
731
+ } else {
732
+ return false;
733
+ }
734
+
735
+ // We're in a state where anything other than `hash_count` hash symbols in a row should be eaten
736
+ // and is part of a string.
737
+ // The last character _before_ the hashes will tell us what happens next.
738
+ // Matters are also complicated by the fact that we don't want to consume every character we
739
+ // visit; if we see a `\#(`, for instance, with the appropriate number of hash symbols, we want
740
+ // to end our parsing _before_ that sequence. This allows highlighting tools to treat that as a
741
+ // separate token.
742
+ while (lexer->lookahead != '\0') {
743
+ uint8_t last_char = '\0';
744
+ lexer->mark_end(lexer); // We always want to parse thru the start of the string so far
745
+ // Advance through anything that isn't a hash symbol, because we want to count those.
746
+ while (lexer->lookahead != '#' && lexer->lookahead != '\0') {
747
+ last_char = lexer->lookahead;
748
+ advance(lexer);
749
+ if (last_char != '\\' || lexer->lookahead == '\\') {
750
+ // Mark a new end, but only if we didn't just advance past a `\` symbol, since we
751
+ // don't want to consume that. Exception: if this is a `\` that happens _right
752
+ // after_ another `\`, we for some reason _do_ want to consume that, because
753
+ // apparently that is parsed as a literal `\` followed by something escaped.
754
+ lexer->mark_end(lexer);
755
+ }
756
+ }
757
+
758
+ // We hit at least one hash - count them and see if they match.
759
+ uint32_t current_hash_count = 0;
760
+ while (lexer->lookahead == '#' && current_hash_count < hash_count) {
761
+ current_hash_count += 1;
762
+ advance(lexer);
763
+ }
764
+
765
+ // If we saw exactly the right number of hashes, one of three things is true:
766
+ // 1. We're trying to interpolate into this string.
767
+ // 2. The string just ended.
768
+ // 3. This was just some hash characters doing nothing important.
769
+ if (current_hash_count == hash_count) {
770
+ if (last_char == '\\' && lexer->lookahead == '(') {
771
+ // Interpolation case! Don't consume those chars; they get saved for grammar.js.
772
+ *symbol_result = RAW_STR_PART;
773
+ state->ongoing_raw_str_hash_count = hash_count;
774
+ return true;
775
+ } else if (last_char == '"') {
776
+ // The string is finished! Mark the end here, on the very last hash symbol.
777
+ lexer->mark_end(lexer);
778
+ *symbol_result = RAW_STR_END_PART;
779
+ state->ongoing_raw_str_hash_count = 0;
780
+ return true;
781
+ }
782
+ // Nothing special happened - let the string continue.
783
+ }
784
+ }
785
+
786
+ return false;
787
+ }
788
+
789
+ bool tree_sitter_swift_external_scanner_scan(
790
+ void *payload,
791
+ TSLexer *lexer,
792
+ const bool *valid_symbols
793
+ ) {
794
+ // Figure out our scanner state
795
+ struct ScannerState *state = (struct ScannerState *)payload;
796
+
797
+ // Consume any whitespace at the start.
798
+ enum TokenType ws_result;
799
+ enum ParseDirective ws_directive = eat_whitespace(lexer, valid_symbols, &ws_result);
800
+ if (ws_directive == STOP_PARSING_TOKEN_FOUND) {
801
+ lexer->result_symbol = ws_result;
802
+ return true;
803
+ }
804
+
805
+ if (ws_directive == STOP_PARSING_NOTHING_FOUND || ws_directive == STOP_PARSING_END_OF_FILE) {
806
+ return false;
807
+ }
808
+
809
+ bool has_ws_result = (ws_directive == CONTINUE_PARSING_TOKEN_FOUND);
810
+
811
+ // Now consume comments (before custom operators so that those aren't treated as comments)
812
+ enum TokenType comment_result;
813
+ enum ParseDirective comment = ws_directive == CONTINUE_PARSING_SLASH_CONSUMED ? ws_directive : eat_comment(lexer, valid_symbols, /* mark_end */ true, &comment_result);
814
+ if (comment == STOP_PARSING_TOKEN_FOUND) {
815
+ lexer->mark_end(lexer);
816
+ lexer->result_symbol = comment_result;
817
+ return true;
818
+ }
819
+
820
+ if (comment == STOP_PARSING_END_OF_FILE) {
821
+ return false;
822
+ }
823
+ // Now consume any operators that might cause our whitespace to be suppressed.
824
+ enum TokenType operator_result;
825
+ bool saw_operator = eat_operators(
826
+ lexer,
827
+ valid_symbols,
828
+ /* mark_end */ !has_ws_result,
829
+ comment == CONTINUE_PARSING_SLASH_CONSUMED ? '/' : '\0',
830
+ &operator_result
831
+ );
832
+
833
+ if (saw_operator && (!has_ws_result || is_cross_semi_token(operator_result))) {
834
+ lexer->result_symbol = operator_result;
835
+ if (has_ws_result) lexer->mark_end(lexer);
836
+ return true;
837
+ }
838
+
839
+ if (has_ws_result) {
840
+ // Don't `mark_end`, since we may have advanced through some operators.
841
+ lexer->result_symbol = ws_result;
842
+ return true;
843
+ }
844
+
845
+ // NOTE: this will consume any `#` characters it sees, even if it does not find a result. Keep
846
+ // it at the end so that it doesn't interfere with special literals or selectors!
847
+ enum TokenType raw_str_result;
848
+ bool saw_raw_str_part = eat_raw_str_part(state, lexer, valid_symbols, &raw_str_result);
849
+ if (saw_raw_str_part) {
850
+ lexer->result_symbol = raw_str_result;
851
+ return true;
852
+ }
853
+
854
+ return false;
855
+ }
856
+