tree-sitter-postgres 1.2.0

Postgres grammar for tree-sitter
Documentation
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/**
 * External scanner for PL/pgSQL tree-sitter grammar.
 *
 * PL/pgSQL embeds PostgreSQL SQL in many different grammar contexts. The
 * important detail is that those contexts do not all use the same terminator:
 * IF conditions end at THEN, WHILE/FOR expressions end at LOOP, dynamic
 * EXECUTE expressions end at INTO/USING/;, and ordinary SQL statements end at
 * semicolon. PostgreSQL's real PL/pgSQL parser models this with calls like
 * read_sql_expression() and read_sql_construct() that receive context-specific
 * terminators.
 *
 * This scanner mirrors that model with several external tokens. The grammar
 * chooses the token for the current context, and the scanner consumes an opaque
 * SQL fragment until that token's delimiter set is reached. The fragment is
 * later parsed/highlighted by the PostgreSQL grammar through language
 * injection.
 */
#include "tree_sitter/parser.h"

#include <string.h>

enum TokenType {
  SQL_STATEMENT,
  SQL_UNTIL_SEMICOLON,
  SQL_UNTIL_THEN,
  SQL_UNTIL_WHEN,
  SQL_UNTIL_LOOP,
  SQL_UNTIL_ASSIGNMENT,
  SQL_UNTIL_RANGE,
  SQL_UNTIL_BY_OR_LOOP,
  SQL_UNTIL_INTO_USING_OR_SEMICOLON,
  SQL_UNTIL_USING_OR_SEMICOLON,
  SQL_UNTIL_USING_OR_LOOP,
  SQL_UNTIL_COMMA_OR_SEMICOLON,
  SQL_UNTIL_COMMA_USING_OR_SEMICOLON,
  SQL_UNTIL_COMMA_OR_LOOP,
  SQL_UNTIL_COMMA_OR_RPAREN,
  SQL_UNTIL_FROM_OR_INTO,
  SQL_UNTIL_SEMICOLON_GUARDED,
};

void *tree_sitter_plpgsql_external_scanner_create(void) { return NULL; }
void tree_sitter_plpgsql_external_scanner_destroy(void *payload) { (void)payload; }
unsigned tree_sitter_plpgsql_external_scanner_serialize(void *payload, char *buffer) { (void)payload; (void)buffer; return 0; }
void tree_sitter_plpgsql_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) { (void)payload; (void)buffer; (void)length; }

static void skip_whitespace(TSLexer *lexer) {
  while (lexer->lookahead == ' ' || lexer->lookahead == '\t' ||
         lexer->lookahead == '\n' || lexer->lookahead == '\r') {
    lexer->advance(lexer, true);
  }
}

/* Keep the scanner self-contained for WebAssembly builds. Zed and other
 * editor runtimes may not provide libc ctype imports to grammar Wasm modules. */
static bool is_ascii_alpha(int c) {
  return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}

static bool is_ascii_digit(int c) {
  return c >= '0' && c <= '9';
}

static bool is_ascii_alnum(int c) {
  return is_ascii_alpha(c) || is_ascii_digit(c);
}

static char ascii_tolower(int c) {
  return (c >= 'A' && c <= 'Z') ? (char)(c + ('a' - 'A')) : (char)c;
}

static bool is_tag_start_char(int c) {
  return is_ascii_alpha(c) || c == '_' || c >= 0x80;
}

static bool is_tag_char(int c) {
  return is_tag_start_char(c) || is_ascii_digit(c);
}

typedef struct {
  enum TokenType symbol;
  bool stop_semicolon;
  bool stop_then;
  bool stop_when;
  bool stop_loop;
  bool stop_into;
  bool stop_using;
  bool stop_comma;
  bool stop_assignment;
  bool stop_range;
  bool stop_by;
  bool stop_from;
  bool refuse_plpgsql_statement_start;
  bool loop_yields_loop_token;
  /* Refuse to start the fragment on words that must instead lex as PL/pgSQL
   * keywords in the current context: NEXT/QUERY/EXECUTE after RETURN and
   * EXECUTE after OPEN ... FOR (refuse_first_return_open_kw), or
   * EXECUTE/REVERSE after FOR ... IN (refuse_first_for_in_kw). */
  bool refuse_first_return_open_kw;
  bool refuse_first_for_in_kw;
} ScanMode;

static ScanMode mode_for(enum TokenType symbol) {
  ScanMode mode = {0};
  mode.symbol = symbol;

  switch (symbol) {
    case SQL_STATEMENT:
      mode.stop_semicolon = true;
      mode.refuse_plpgsql_statement_start = true;
      break;
    case SQL_UNTIL_SEMICOLON:
      mode.stop_semicolon = true;
      break;
    case SQL_UNTIL_THEN:
      mode.stop_semicolon = true;
      mode.stop_then = true;
      break;
    case SQL_UNTIL_WHEN:
      mode.stop_semicolon = true;
      mode.stop_when = true;
      break;
    case SQL_UNTIL_LOOP:
      mode.stop_semicolon = true;
      mode.stop_loop = true;
      break;
    case SQL_UNTIL_ASSIGNMENT:
      mode.stop_semicolon = true;
      mode.stop_assignment = true;
      break;
    case SQL_UNTIL_RANGE:
      mode.stop_semicolon = true;
      mode.stop_range = true;
      break;
    case SQL_UNTIL_BY_OR_LOOP:
      mode.stop_semicolon = true;
      mode.stop_by = true;
      mode.stop_loop = true;
      break;
    case SQL_UNTIL_INTO_USING_OR_SEMICOLON:
      mode.stop_semicolon = true;
      mode.stop_into = true;
      mode.stop_using = true;
      break;
    case SQL_UNTIL_USING_OR_SEMICOLON:
      mode.stop_semicolon = true;
      mode.stop_using = true;
      break;
    case SQL_UNTIL_USING_OR_LOOP:
      mode.stop_semicolon = true;
      mode.stop_using = true;
      mode.stop_loop = true;
      break;
    case SQL_UNTIL_COMMA_OR_SEMICOLON:
      mode.stop_semicolon = true;
      mode.stop_comma = true;
      break;
    case SQL_UNTIL_COMMA_USING_OR_SEMICOLON:
      mode.stop_semicolon = true;
      mode.stop_comma = true;
      mode.stop_using = true;
      break;
    case SQL_UNTIL_COMMA_OR_LOOP:
      mode.stop_semicolon = true;
      mode.stop_comma = true;
      mode.stop_loop = true;
      break;
    case SQL_UNTIL_COMMA_OR_RPAREN:
      mode.stop_semicolon = true;
      mode.stop_comma = true;
      break;
    case SQL_UNTIL_FROM_OR_INTO:
      mode.stop_semicolon = true;
      mode.stop_from = true;
      mode.stop_into = true;
      break;
    case SQL_UNTIL_SEMICOLON_GUARDED:
      mode.stop_semicolon = true;
      mode.refuse_first_return_open_kw = true;
      break;
  }

  return mode;
}

static bool is_plpgsql_statement_start(const char *word) {
  return strcmp(word, "begin") == 0 ||
         strcmp(word, "declare") == 0 ||
         strcmp(word, "end") == 0 ||
         strcmp(word, "exception") == 0 ||
         strcmp(word, "if") == 0 ||
         strcmp(word, "case") == 0 ||
         strcmp(word, "loop") == 0 ||
         strcmp(word, "while") == 0 ||
         strcmp(word, "for") == 0 ||
         strcmp(word, "foreach") == 0 ||
         strcmp(word, "return") == 0 ||
         strcmp(word, "raise") == 0 ||
         strcmp(word, "assert") == 0 ||
         strcmp(word, "execute") == 0 ||
         strcmp(word, "perform") == 0 ||
         strcmp(word, "call") == 0 ||
         strcmp(word, "do") == 0 ||
         strcmp(word, "get") == 0 ||
         strcmp(word, "open") == 0 ||
         strcmp(word, "fetch") == 0 ||
         strcmp(word, "move") == 0 ||
         strcmp(word, "close") == 0 ||
         strcmp(word, "null") == 0 ||
         strcmp(word, "exit") == 0 ||
         strcmp(word, "continue") == 0 ||
         strcmp(word, "commit") == 0 ||
         strcmp(word, "rollback") == 0 ||
         strcmp(word, "elsif") == 0 ||
         strcmp(word, "elseif") == 0 ||
         strcmp(word, "else") == 0 ||
         strcmp(word, "when") == 0;
}

static bool is_sql_statement_start(const char *word) {
  return strcmp(word, "select") == 0 ||
         strcmp(word, "insert") == 0 ||
         strcmp(word, "update") == 0 ||
         strcmp(word, "delete") == 0 ||
         strcmp(word, "merge") == 0 ||
         strcmp(word, "with") == 0 ||
         strcmp(word, "values") == 0 ||
         strcmp(word, "create") == 0 ||
         strcmp(word, "alter") == 0 ||
         strcmp(word, "drop") == 0 ||
         strcmp(word, "truncate") == 0 ||
         strcmp(word, "grant") == 0 ||
         strcmp(word, "revoke") == 0 ||
         strcmp(word, "analyze") == 0 ||
         strcmp(word, "analyse") == 0 ||
         strcmp(word, "explain") == 0 ||
         strcmp(word, "vacuum") == 0 ||
         strcmp(word, "lock") == 0 ||
         strcmp(word, "notify") == 0 ||
         strcmp(word, "listen") == 0 ||
         strcmp(word, "unlisten") == 0 ||
         strcmp(word, "refresh") == 0 ||
         strcmp(word, "reindex") == 0 ||
         strcmp(word, "copy") == 0 ||
         strcmp(word, "set") == 0 ||
         strcmp(word, "reset") == 0 ||
         strcmp(word, "show") == 0 ||
         strcmp(word, "discard") == 0 ||
         strcmp(word, "prepare") == 0 ||
         strcmp(word, "deallocate") == 0;
}

static bool is_keyword_terminator(const ScanMode *mode, const char *word, enum TokenType *result_symbol) {
  *result_symbol = mode->symbol;
  if (mode->stop_loop && strcmp(word, "loop") == 0) {
    if (mode->loop_yields_loop_token) *result_symbol = SQL_UNTIL_LOOP;
    return true;
  }
  return (mode->stop_then && strcmp(word, "then") == 0) ||
         (mode->stop_when && strcmp(word, "when") == 0) ||
         (mode->stop_into && strcmp(word, "into") == 0) ||
         (mode->stop_using && strcmp(word, "using") == 0) ||
         (mode->stop_by && strcmp(word, "by") == 0) ||
         (mode->stop_from && strcmp(word, "from") == 0);
}

static bool finish_token(TSLexer *lexer, enum TokenType symbol, bool has_content) {
  if (!has_content) return false;
  lexer->result_symbol = symbol;
  return true;
}

static bool consume_single_quoted_string(TSLexer *lexer) {
  lexer->advance(lexer, false);
  while (lexer->lookahead != 0) {
    if (lexer->lookahead == '\'') {
      lexer->advance(lexer, false);
      if (lexer->lookahead != '\'') return true;
      lexer->advance(lexer, false);
    } else {
      lexer->advance(lexer, false);
    }
  }
  return true;
}

static bool consume_double_quoted_identifier(TSLexer *lexer) {
  lexer->advance(lexer, false);
  while (lexer->lookahead != 0) {
    if (lexer->lookahead == '"') {
      lexer->advance(lexer, false);
      if (lexer->lookahead != '"') return true;
      lexer->advance(lexer, false);
    } else {
      lexer->advance(lexer, false);
    }
  }
  return true;
}

static bool consume_dollar_quoted_string(TSLexer *lexer) {
  lexer->advance(lexer, false); /* opening $ */

  char tag[64];
  int tag_len = 0;
  if (is_tag_start_char(lexer->lookahead)) {
    do {
      if (tag_len >= 63) return true;
      tag[tag_len++] = (char)lexer->lookahead;
      lexer->advance(lexer, false);
    } while (is_tag_char(lexer->lookahead));
  }

  if (lexer->lookahead != '$') {
    return true; /* Not actually a dollar quote; we consumed a SQL '$'. */
  }
  lexer->advance(lexer, false);

  while (lexer->lookahead != 0) {
    if (lexer->lookahead == '$') {
      lexer->advance(lexer, false);
      int i = 0;
      while (i < tag_len && lexer->lookahead == (unsigned char)tag[i]) {
        lexer->advance(lexer, false);
        i++;
      }
      if (i == tag_len && lexer->lookahead == '$') {
        lexer->advance(lexer, false);
        return true;
      }
      continue;
    }
    lexer->advance(lexer, false);
  }

  return true;
}

static bool scan_sql(TSLexer *lexer, ScanMode mode, const bool *valid_symbols) {
  (void)valid_symbols;

  skip_whitespace(lexer);

  if (lexer->lookahead == 0) return false;

  int depth = 0;
  bool has_content = false;
  bool saw_first_word = false;
  bool disable_assignment_stop = false;

  while (lexer->lookahead != 0) {
    if (depth == 0) {
      if (mode.stop_semicolon && lexer->lookahead == ';') {
        lexer->mark_end(lexer);
        return finish_token(lexer, mode.symbol, has_content);
      }

      if (mode.stop_comma && lexer->lookahead == ',') {
        lexer->mark_end(lexer);
        return finish_token(lexer, mode.symbol, has_content);
      }
    }

    if (lexer->lookahead == '(' || lexer->lookahead == '[') {
      depth++;
      lexer->advance(lexer, false);
      has_content = true;
      continue;
    }

    if (lexer->lookahead == ')' || lexer->lookahead == ']') {
      if (depth > 0) {
        depth--;
        lexer->advance(lexer, false);
        has_content = true;
        continue;
      }
      lexer->mark_end(lexer);
      return finish_token(lexer, mode.symbol, has_content);
    }

    if (lexer->lookahead == '\'') {
      consume_single_quoted_string(lexer);
      has_content = true;
      continue;
    }

    if (lexer->lookahead == '"') {
      consume_double_quoted_identifier(lexer);
      has_content = true;
      continue;
    }

    if (lexer->lookahead == '$') {
      consume_dollar_quoted_string(lexer);
      has_content = true;
      continue;
    }

    if (lexer->lookahead == '-') {
      lexer->advance(lexer, false);
      if (lexer->lookahead == '-') {
        if (!has_content) return false;
        while (lexer->lookahead != 0 && lexer->lookahead != '\n') {
          lexer->advance(lexer, false);
        }
        has_content = true;
        continue;
      }
      has_content = true;
      continue;
    }

    if (lexer->lookahead == '/') {
      lexer->advance(lexer, false);
      if (lexer->lookahead == '*') {
        if (!has_content) return false;
        lexer->advance(lexer, false);
        int comment_depth = 1;
        while (lexer->lookahead != 0 && comment_depth > 0) {
          if (lexer->lookahead == '/') {
            lexer->advance(lexer, false);
            if (lexer->lookahead == '*') {
              comment_depth++;
              lexer->advance(lexer, false);
            }
          } else if (lexer->lookahead == '*') {
            lexer->advance(lexer, false);
            if (lexer->lookahead == '/') {
              comment_depth--;
              lexer->advance(lexer, false);
            }
          } else {
            lexer->advance(lexer, false);
          }
        }
        has_content = true;
        continue;
      }
      has_content = true;
      continue;
    }

    if (depth == 0 && !has_content && lexer->lookahead == '<') {
      lexer->mark_end(lexer);
      lexer->advance(lexer, false);
      if (lexer->lookahead == '<') {
        return finish_token(lexer, mode.symbol, has_content);
      }
      has_content = true;
      continue;
    }

    if (depth == 0 && lexer->lookahead == ':') {
      lexer->mark_end(lexer);
      lexer->advance(lexer, false);
      if (lexer->lookahead == '=') {
        if (mode.stop_assignment && !disable_assignment_stop) {
          return finish_token(lexer, mode.symbol, has_content);
        }
        lexer->advance(lexer, false);
        has_content = true;
        continue;
      }
      if (lexer->lookahead == ':') {
        lexer->advance(lexer, false);
        has_content = true;
        continue;
      }
      has_content = true;
      continue;
    }

    if (depth == 0 && lexer->lookahead == '=') {
      lexer->mark_end(lexer);
      if (mode.stop_assignment && !disable_assignment_stop) {
        return finish_token(lexer, mode.symbol, has_content);
      }
      lexer->advance(lexer, false);
      has_content = true;
      continue;
    }

    if (depth == 0 && lexer->lookahead == '.') {
      lexer->mark_end(lexer);
      lexer->advance(lexer, false);
      if (lexer->lookahead == '.') {
        if (mode.stop_range) {
          return finish_token(lexer, mode.symbol, has_content);
        }
        lexer->advance(lexer, false);
        has_content = true;
        continue;
      }
      has_content = true;
      continue;
    }

    if (depth == 0 && is_ascii_alpha(lexer->lookahead)) {
      lexer->mark_end(lexer);

      char word[32];
      int len = 0;
      while (is_ascii_alnum(lexer->lookahead) || lexer->lookahead == '_') {
        if (len < 31) word[len++] = ascii_tolower(lexer->lookahead);
        lexer->advance(lexer, false);
      }
      word[len] = '\0';

      if (!saw_first_word) {
        saw_first_word = true;
        if (mode.refuse_plpgsql_statement_start && is_plpgsql_statement_start(word)) {
          return false;
        }
        if (mode.refuse_first_return_open_kw &&
            (strcmp(word, "next") == 0 ||
             strcmp(word, "query") == 0 ||
             strcmp(word, "execute") == 0)) {
          return false;
        }
        if (mode.refuse_first_for_in_kw &&
            (strcmp(word, "execute") == 0 ||
             strcmp(word, "reverse") == 0)) {
          return false;
        }
        if (mode.stop_assignment && is_sql_statement_start(word)) {
          disable_assignment_stop = true;
        }
      }

      enum TokenType result_symbol = mode.symbol;
      if (is_keyword_terminator(&mode, word, &result_symbol)) {
        return finish_token(lexer, result_symbol, has_content);
      }

      has_content = true;
      continue;
    }

    if (is_ascii_alpha(lexer->lookahead) || lexer->lookahead == '_' || lexer->lookahead >= 0x80) {
      while (is_ascii_alnum(lexer->lookahead) || lexer->lookahead == '_' ||
             lexer->lookahead == '$' || lexer->lookahead >= 0x80) {
        lexer->advance(lexer, false);
      }
      has_content = true;
      continue;
    }

    lexer->advance(lexer, false);
    has_content = true;
  }

  lexer->mark_end(lexer);
  return finish_token(lexer, mode.symbol, has_content);
}

static bool scan_assignment_or_statement(TSLexer *lexer, const bool *valid_symbols) {
  (void)valid_symbols;

  /* If we reach ';' before an assignment operator, emit SQL_STATEMENT instead.
   * If we reach ':=' or '=' first, emit SQL_UNTIL_ASSIGNMENT. */
  skip_whitespace(lexer);
  if (lexer->lookahead == 0) return false;

  int depth = 0;
  bool has_content = false;
  bool saw_first_word = false;
  bool disable_assignment_stop = false;

  while (lexer->lookahead != 0) {
    if (depth == 0 && lexer->lookahead == ';') {
      lexer->mark_end(lexer);
      if (!has_content) return false;
      lexer->result_symbol = SQL_STATEMENT;
      return true;
    }

    if (lexer->lookahead == '(' || lexer->lookahead == '[') {
      depth++;
      lexer->advance(lexer, false);
      has_content = true;
      continue;
    }

    if (lexer->lookahead == ')' || lexer->lookahead == ']') {
      if (depth > 0) {
        depth--;
        lexer->advance(lexer, false);
        has_content = true;
        continue;
      }
      lexer->mark_end(lexer);
      if (!has_content) return false;
      lexer->result_symbol = SQL_STATEMENT;
      return true;
    }

    if (lexer->lookahead == '\'') {
      consume_single_quoted_string(lexer);
      has_content = true;
      continue;
    }
    if (lexer->lookahead == '"') {
      consume_double_quoted_identifier(lexer);
      has_content = true;
      continue;
    }
    if (lexer->lookahead == '$') {
      consume_dollar_quoted_string(lexer);
      has_content = true;
      continue;
    }

    if (lexer->lookahead == '-') {
      lexer->advance(lexer, false);
      if (lexer->lookahead == '-') {
        if (!has_content) return false;
        while (lexer->lookahead != 0 && lexer->lookahead != '\n') lexer->advance(lexer, false);
        has_content = true;
        continue;
      }
      has_content = true;
      continue;
    }

    if (lexer->lookahead == '/') {
      lexer->advance(lexer, false);
      if (lexer->lookahead == '*') {
        if (!has_content) return false;
        lexer->advance(lexer, false);
        int comment_depth = 1;
        while (lexer->lookahead != 0 && comment_depth > 0) {
          if (lexer->lookahead == '/') {
            lexer->advance(lexer, false);
            if (lexer->lookahead == '*') { comment_depth++; lexer->advance(lexer, false); }
          } else if (lexer->lookahead == '*') {
            lexer->advance(lexer, false);
            if (lexer->lookahead == '/') { comment_depth--; lexer->advance(lexer, false); }
          } else {
            lexer->advance(lexer, false);
          }
        }
        has_content = true;
        continue;
      }
      has_content = true;
      continue;
    }

    if (depth == 0 && !has_content && lexer->lookahead == '<') {
      lexer->mark_end(lexer);
      lexer->advance(lexer, false);
      if (lexer->lookahead == '<') {
        if (!has_content) return false;
        lexer->result_symbol = SQL_STATEMENT;
        return true;
      }
      has_content = true;
      continue;
    }

    if (depth == 0 && lexer->lookahead == ':') {
      lexer->mark_end(lexer);
      lexer->advance(lexer, false);
      if (lexer->lookahead == '=') {
        if (!disable_assignment_stop) {
          return finish_token(lexer, SQL_UNTIL_ASSIGNMENT, has_content);
        }
        lexer->advance(lexer, false);
        has_content = true;
        continue;
      }
      if (lexer->lookahead == ':') lexer->advance(lexer, false);
      has_content = true;
      continue;
    }

    if (depth == 0 && lexer->lookahead == '=') {
      lexer->mark_end(lexer);
      if (!disable_assignment_stop) {
        return finish_token(lexer, SQL_UNTIL_ASSIGNMENT, has_content);
      }
      lexer->advance(lexer, false);
      has_content = true;
      continue;
    }

    if (depth == 0 && is_ascii_alpha(lexer->lookahead)) {
      lexer->mark_end(lexer);
      char word[32];
      int len = 0;
      while (is_ascii_alnum(lexer->lookahead) || lexer->lookahead == '_') {
        if (len < 31) word[len++] = ascii_tolower(lexer->lookahead);
        lexer->advance(lexer, false);
      }
      word[len] = '\0';

      if (!saw_first_word) {
        saw_first_word = true;
        if (is_plpgsql_statement_start(word)) return false;
        if (is_sql_statement_start(word)) disable_assignment_stop = true;
      }

      has_content = true;
      continue;
    }

    if (is_ascii_alpha(lexer->lookahead) || lexer->lookahead == '_' || lexer->lookahead >= 0x80) {
      while (is_ascii_alnum(lexer->lookahead) || lexer->lookahead == '_' ||
             lexer->lookahead == '$' || lexer->lookahead >= 0x80) {
        lexer->advance(lexer, false);
      }
      has_content = true;
      continue;
    }

    lexer->advance(lexer, false);
    has_content = true;
  }

  lexer->mark_end(lexer);
  if (!has_content) return false;
  lexer->result_symbol = SQL_STATEMENT;
  return true;
}

bool tree_sitter_plpgsql_external_scanner_scan(
  void *payload, TSLexer *lexer, const bool *valid_symbols
) {
  (void)payload;

  if (valid_symbols[SQL_UNTIL_ASSIGNMENT] && valid_symbols[SQL_STATEMENT]) {
    return scan_assignment_or_statement(lexer, valid_symbols);
  }

  /* FOR ... IN can begin either an integer range (expr .. expr) or a query
   * (query LOOP). Scan once and choose the token based on which delimiter is
   * encountered first. EXECUTE and REVERSE introduce the dynamic and reverse
   * forms, so they must lex as keywords rather than start the fragment. */
  if (valid_symbols[SQL_UNTIL_RANGE] && valid_symbols[SQL_UNTIL_LOOP]) {
    ScanMode mode = mode_for(SQL_UNTIL_RANGE);
    mode.stop_loop = true;
    mode.loop_yields_loop_token = true;
    mode.refuse_first_for_in_kw = true;
    return scan_sql(lexer, mode, valid_symbols);
  }

  for (int symbol = SQL_STATEMENT; symbol <= SQL_UNTIL_SEMICOLON_GUARDED; symbol++) {
    if (valid_symbols[symbol]) {
      return scan_sql(lexer, mode_for((enum TokenType)symbol), valid_symbols);
    }
  }

  return false;
}