surrealql-language-server 0.5.0

Language Server Protocol implementation for SurrealQL
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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
//! Closed-vocabulary statement-head slots.
//!
//! A SurrealQL statement opens with a run of keywords whose legal
//! continuations are a small, closed set. `INFO FOR ` accepts exactly nine
//! words; `USE ` accepts four. Today the completion handler offers the whole
//! catalogue in every one of those positions (`crate::core::server`, the
//! unguarded call to `MergedSemanticModel::completion_items`), so `INFO FOR `
//! returns ~375 items of which nine are legal.
//!
//! This module is the vocabulary those positions need. It is a flat table of
//! literal word prefixes, looked up by exact length with a wildcard for a
//! name the user chose. Deliberately *not* a parser and deliberately not a
//! model of clause order:
//!
//! * **No clause spine.** `SELECT`'s clause order is strict, but classifying a
//!   position inside it requires knowing whether the last word was a clause
//!   keyword or a field that happens to be spelled `order` — SurrealQL accepts
//!   `SELECT order FROM t` (`parse_ident` admits keyword-like tokens). Guessing
//!   there hides legal completions in `WHERE … AND `, which is the busiest
//!   position in the language. Those positions keep today's behaviour.
//! * **Only closed sets.** A rule exists only where the legal continuation is
//!   a finite keyword set. Where an expression is legal — `DEFAULT `, a name
//!   the user invents, `INFO FOR USER ` (the engine parses that name as a full
//!   expression) — the answer is [`SlotYield::Expression`], which means "keep
//!   today's list". An empty list would be a wrong answer, not silence.
//!
//! That makes the table subtractive-only: it can never fire unless a literal
//! keyword prefix matches, so no position that works today can regress.
//!
//! Every vocabulary below is transcribed from the SurrealDB parser, not from
//! the documentation site. The citations are paths under
//! `surrealdb/core/src/syn/parser/`.

/// Matches exactly one word of any spelling — a table, index, or user name the
/// author chose. Not a valid SurrealQL keyword, so it cannot collide with one.
pub const ANY: &str = "*";

/// What the completion list may offer at one statement-head slot.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SlotYield {
    /// Exactly these keywords. Nothing else is legal here.
    Keywords(&'static [&'static str]),
    /// These keywords, plus the table names the model knows. Used where the
    /// slot accepts either (`DEFINE EVENT e ON ` takes `TABLE` or a table).
    KeywordsAndTables(&'static [&'static str]),
    /// Table names only.
    Tables,
    /// The names of the `DEFINE ANALYZER`s the model knows. Used only where the
    /// name must already exist.
    Analyzers,
    /// The legal set is open, or we have not modelled it. Callers must keep
    /// the behaviour they have today and offer their full list.
    Expression,
}

/// `ROOT | NAMESPACE | NS | DATABASE | DB` — the `<base>` of an `ON` clause
/// (`stmt/parts.rs:444-454`; `NS`/`DB` lex to the same tokens as the long
/// spellings, `lexer/keywords.rs:120,208`).
const BASE: &[&str] = &["ROOT", "NAMESPACE", "NS", "DATABASE", "DB"];

/// The six `INFO FOR` targets, with both spellings of the three that have one
/// (`stmt/mod.rs:417-476`). `SC` and `SCOPE` are *not* here: SurrealDB 3.x
/// dropped both, although the pinned tree-sitter grammar still offers them.
const INFO_TARGETS: &[&str] = &[
    "ROOT",
    "NAMESPACE",
    "NS",
    "DATABASE",
    "DB",
    "TABLE",
    "TB",
    "USER",
    "INDEX",
];

/// The 16 `DEFINE` sub-forms (`stmt/define.rs:51-78`). `TOKEN`, `SCOPE` and
/// `MODEL` are gone in 3.x — the words still lex, but no parser arm accepts
/// them.
const DEFINE_FORMS: &[&str] = &[
    "NAMESPACE",
    "DATABASE",
    "FUNCTION",
    "USER",
    "PARAM",
    "TABLE",
    "API",
    "EVENT",
    "FIELD",
    "INDEX",
    "ANALYZER",
    "ACCESS",
    "CONFIG",
    "BUCKET",
    "SEQUENCE",
    "MODULE",
];

/// The 16 `REMOVE` sub-forms (`stmt/remove.rs:25-320`).
const REMOVE_FORMS: &[&str] = &[
    "NAMESPACE",
    "DATABASE",
    "TABLE",
    "FUNCTION",
    "MODULE",
    "ACCESS",
    "USER",
    "PARAM",
    "EVENT",
    "FIELD",
    "INDEX",
    "ANALYZER",
    "SEQUENCE",
    "API",
    "BUCKET",
    "CONFIG",
];

/// The 17 `ALTER` sub-forms (`stmt/alter.rs:26-46`).
const ALTER_FORMS: &[&str] = &[
    "SYSTEM",
    "NAMESPACE",
    "DATABASE",
    "TABLE",
    "EVENT",
    "INDEX",
    "FIELD",
    "PARAM",
    "SEQUENCE",
    "BUCKET",
    "ANALYZER",
    "FUNCTION",
    "USER",
    "ACCESS",
    "CONFIG",
    "API",
    "MODULE",
];

/// `DEFINE TABLE <name> ` (`stmt/define.rs:663-731`). The view clause opens
/// with `AS`, not `VIEW`; `DEFINE TABLE t VIEW …` is a parse error.
const DEFINE_TABLE_CLAUSES: &[&str] = &[
    "COMMENT",
    "DROP",
    "TYPE",
    "SCHEMALESS",
    "SCHEMAFULL",
    "PERMISSIONS",
    "CHANGEFEED",
    "AS",
    "GRAPHQL_ALIAS",
    "GRAPHQL_DEPRECATED",
];

/// `DEFINE FIELD <name> ON <table> ` (`stmt/define.rs:954-1033`).
const DEFINE_FIELD_CLAUSES: &[&str] = &[
    "TYPE",
    "FLEXIBLE",
    "READONLY",
    "VALUE",
    "ASSERT",
    "DEFAULT",
    "PERMISSIONS",
    "COMMENT",
    "REFERENCE",
    "COMPUTED",
    "GRAPHQL_ALIAS",
    "GRAPHQL_DEPRECATED",
];

/// `DEFINE EVENT <name> ON <table> ` (`stmt/define.rs:846-920`). `RETRY` and
/// `MAXDEPTH` are only legal after `ASYNC`; offering them one slot early is
/// harmless, because the alternative is offering the whole catalogue.
const DEFINE_EVENT_CLAUSES: &[&str] = &["WHEN", "THEN", "COMMENT", "ASYNC", "RETRY", "MAXDEPTH"];

/// `DEFINE INDEX <name> ON <table> ` (`stmt/define.rs:1051-1338`). The index
/// kinds are `Idx Uniq Hnsw DiskAnn FullText Count` (`sql/index.rs:11-24`) —
/// `MTREE` and `SEARCH` are gone in 3.x.
const DEFINE_INDEX_CLAUSES: &[&str] = &[
    "FIELDS",
    "COLUMNS",
    "UNIQUE",
    "COUNT",
    "FULLTEXT",
    "HNSW",
    "DISKANN",
    "CONCURRENTLY",
    "COMMENT",
];

/// `DEFINE TABLE t TYPE ` (`stmt/define.rs:671-687`).
const TABLE_TYPES: &[&str] = &["NORMAL", "RELATION", "ANY"];

/// `PERMISSIONS ` (`stmt/parts.rs:324-353`).
const PERMISSIONS_HEADS: &[&str] = &["NONE", "FULL", "FOR"];

/// Keywords this table offers that the pinned tree-sitter grammar cannot lex.
///
/// The engine accepts every one of them, so hiding them would deny the author
/// a legal clause. Offering them means the grammar reports a syntax error on a
/// clause the server itself suggested. That is a pre-existing grammar gap, not
/// one this table introduces — recorded here so a grammar bump can shorten the
/// list, and asserted by `offers_outside_the_grammar_are_declared`.
pub const OFFERS_THE_GRAMMAR_CANNOT_PARSE: &[&str] = &[
    "GRAPHQL_ALIAS",
    "GRAPHQL_DEPRECATED",
    "SYSTEM",
    "DISKANN",
    "RETRY",
    "MAXDEPTH",
];

struct HeadRule {
    /// The words that must already be typed, in order. [`ANY`] matches one
    /// word of any spelling.
    prefix: &'static [&'static str],
    /// What the slot directly after `prefix` offers.
    yields: SlotYield,
}

/// Every modelled statement-head slot.
///
/// Order is irrelevant: [`head_slot`] matches on length and breaks ties by
/// [`specificity`], so the table stays readable rather than sorted.
const HEAD_RULES: &[HeadRule] = &[
    // ── INFO FOR ── stmt/mod.rs:417-476
    HeadRule {
        prefix: &["INFO"],
        yields: SlotYield::Keywords(&["FOR"]),
    },
    HeadRule {
        prefix: &["INFO", "FOR"],
        yields: SlotYield::Keywords(INFO_TARGETS),
    },
    HeadRule {
        prefix: &["INFO", "FOR", "TABLE"],
        yields: SlotYield::Tables,
    },
    HeadRule {
        prefix: &["INFO", "FOR", "TB"],
        yields: SlotYield::Tables,
    },
    // `VERSION` then `STRUCTURE`, in that order. The three database-level
    // targets take both; `USER` and `INDEX` take only `STRUCTURE`.
    HeadRule {
        prefix: &["INFO", "FOR", "ROOT"],
        yields: SlotYield::Keywords(&["VERSION", "STRUCTURE"]),
    },
    HeadRule {
        prefix: &["INFO", "FOR", "NAMESPACE"],
        yields: SlotYield::Keywords(&["VERSION", "STRUCTURE"]),
    },
    HeadRule {
        prefix: &["INFO", "FOR", "NS"],
        yields: SlotYield::Keywords(&["VERSION", "STRUCTURE"]),
    },
    HeadRule {
        prefix: &["INFO", "FOR", "DATABASE"],
        yields: SlotYield::Keywords(&["VERSION", "STRUCTURE"]),
    },
    HeadRule {
        prefix: &["INFO", "FOR", "DB"],
        yields: SlotYield::Keywords(&["VERSION", "STRUCTURE"]),
    },
    HeadRule {
        prefix: &["INFO", "FOR", "TABLE", ANY],
        yields: SlotYield::Keywords(&["VERSION", "STRUCTURE"]),
    },
    HeadRule {
        prefix: &["INFO", "FOR", "TB", ANY],
        yields: SlotYield::Keywords(&["VERSION", "STRUCTURE"]),
    },
    HeadRule {
        prefix: &["INFO", "FOR", "INDEX", ANY],
        yields: SlotYield::Keywords(&["ON"]),
    },
    HeadRule {
        prefix: &["INFO", "FOR", "INDEX", ANY, "ON"],
        yields: SlotYield::KeywordsAndTables(&["TABLE"]),
    },
    HeadRule {
        prefix: &["INFO", "FOR", "USER", ANY],
        yields: SlotYield::Keywords(&["ON", "STRUCTURE"]),
    },
    HeadRule {
        prefix: &["INFO", "FOR", "USER", ANY, "ON"],
        yields: SlotYield::Keywords(BASE),
    },
    // ── USE ── stmt/mod.rs:367-393
    HeadRule {
        prefix: &["USE"],
        yields: SlotYield::Keywords(&["NAMESPACE", "NS", "DATABASE", "DB", "DEFAULT"]),
    },
    HeadRule {
        prefix: &["USE", "NAMESPACE", ANY],
        yields: SlotYield::Keywords(&["DATABASE", "DB"]),
    },
    HeadRule {
        prefix: &["USE", "NS", ANY],
        yields: SlotYield::Keywords(&["DATABASE", "DB"]),
    },
    // ── SHOW CHANGES ── stmt/mod.rs:617-651
    HeadRule {
        prefix: &["SHOW"],
        yields: SlotYield::Keywords(&["CHANGES"]),
    },
    HeadRule {
        prefix: &["SHOW", "CHANGES"],
        yields: SlotYield::Keywords(&["FOR"]),
    },
    HeadRule {
        prefix: &["SHOW", "CHANGES", "FOR"],
        yields: SlotYield::Keywords(&["TABLE", "DATABASE"]),
    },
    HeadRule {
        prefix: &["SHOW", "CHANGES", "FOR", "TABLE"],
        yields: SlotYield::Tables,
    },
    HeadRule {
        prefix: &["SHOW", "CHANGES", "FOR", "TABLE", ANY],
        yields: SlotYield::Keywords(&["SINCE"]),
    },
    HeadRule {
        prefix: &["SHOW", "CHANGES", "FOR", "DATABASE"],
        yields: SlotYield::Keywords(&["SINCE"]),
    },
    // ── REBUILD ── stmt/mod.rs:545-570
    HeadRule {
        prefix: &["REBUILD"],
        yields: SlotYield::Keywords(&["INDEX"]),
    },
    HeadRule {
        prefix: &["REBUILD", "INDEX"],
        yields: SlotYield::Keywords(&["IF"]),
    },
    HeadRule {
        prefix: &["REBUILD", "INDEX", ANY],
        yields: SlotYield::Keywords(&["ON"]),
    },
    HeadRule {
        prefix: &["REBUILD", "INDEX", ANY, "ON"],
        yields: SlotYield::KeywordsAndTables(&["TABLE"]),
    },
    // ── DEFINE ── stmt/define.rs:51-78
    HeadRule {
        prefix: &["DEFINE"],
        yields: SlotYield::Keywords(DEFINE_FORMS),
    },
    // `[IF NOT EXISTS] | [OVERWRITE]` follows the sub-form keyword, before the
    // name (define.rs:81-108). `DEFINE TABLE ` also usefully offers existing
    // tables, because `OVERWRITE` targets one.
    HeadRule {
        prefix: &["DEFINE", "TABLE"],
        yields: SlotYield::KeywordsAndTables(&["IF", "OVERWRITE"]),
    },
    HeadRule {
        prefix: &["DEFINE", "TABLE", ANY],
        yields: SlotYield::Keywords(DEFINE_TABLE_CLAUSES),
    },
    HeadRule {
        prefix: &["DEFINE", "TABLE", ANY, "TYPE"],
        yields: SlotYield::Keywords(TABLE_TYPES),
    },
    HeadRule {
        prefix: &["DEFINE", "TABLE", ANY, "PERMISSIONS"],
        yields: SlotYield::Keywords(PERMISSIONS_HEADS),
    },
    HeadRule {
        prefix: &["DEFINE", "FIELD"],
        yields: SlotYield::Keywords(&["IF", "OVERWRITE"]),
    },
    HeadRule {
        prefix: &["DEFINE", "FIELD", ANY],
        yields: SlotYield::Keywords(&["ON"]),
    },
    HeadRule {
        prefix: &["DEFINE", "FIELD", ANY, "ON"],
        yields: SlotYield::KeywordsAndTables(&["TABLE"]),
    },
    HeadRule {
        prefix: &["DEFINE", "FIELD", ANY, "ON", ANY],
        yields: SlotYield::Keywords(DEFINE_FIELD_CLAUSES),
    },
    HeadRule {
        prefix: &["DEFINE", "FIELD", ANY, "ON", "TABLE", ANY],
        yields: SlotYield::Keywords(DEFINE_FIELD_CLAUSES),
    },
    HeadRule {
        prefix: &["DEFINE", "EVENT"],
        yields: SlotYield::Keywords(&["IF", "OVERWRITE"]),
    },
    HeadRule {
        prefix: &["DEFINE", "EVENT", ANY],
        yields: SlotYield::Keywords(&["ON"]),
    },
    HeadRule {
        prefix: &["DEFINE", "EVENT", ANY, "ON"],
        yields: SlotYield::KeywordsAndTables(&["TABLE"]),
    },
    HeadRule {
        prefix: &["DEFINE", "EVENT", ANY, "ON", ANY],
        yields: SlotYield::Keywords(DEFINE_EVENT_CLAUSES),
    },
    HeadRule {
        prefix: &["DEFINE", "EVENT", ANY, "ON", "TABLE", ANY],
        yields: SlotYield::Keywords(DEFINE_EVENT_CLAUSES),
    },
    HeadRule {
        prefix: &["DEFINE", "INDEX"],
        yields: SlotYield::Keywords(&["IF", "OVERWRITE"]),
    },
    HeadRule {
        prefix: &["DEFINE", "INDEX", ANY],
        yields: SlotYield::Keywords(&["ON"]),
    },
    HeadRule {
        prefix: &["DEFINE", "INDEX", ANY, "ON"],
        yields: SlotYield::KeywordsAndTables(&["TABLE"]),
    },
    HeadRule {
        prefix: &["DEFINE", "INDEX", ANY, "ON", ANY],
        yields: SlotYield::Keywords(DEFINE_INDEX_CLAUSES),
    },
    HeadRule {
        prefix: &["DEFINE", "INDEX", ANY, "ON", "TABLE", ANY],
        yields: SlotYield::Keywords(DEFINE_INDEX_CLAUSES),
    },
    HeadRule {
        prefix: &["DEFINE", "USER"],
        yields: SlotYield::Keywords(&["IF", "OVERWRITE"]),
    },
    HeadRule {
        prefix: &["DEFINE", "USER", ANY],
        yields: SlotYield::Keywords(&["ON"]),
    },
    HeadRule {
        prefix: &["DEFINE", "USER", ANY, "ON"],
        yields: SlotYield::Keywords(BASE),
    },
    HeadRule {
        prefix: &["DEFINE", "ACCESS"],
        yields: SlotYield::Keywords(&["IF", "OVERWRITE"]),
    },
    HeadRule {
        prefix: &["DEFINE", "ACCESS", ANY],
        yields: SlotYield::Keywords(&["ON"]),
    },
    HeadRule {
        prefix: &["DEFINE", "ACCESS", ANY, "ON"],
        yields: SlotYield::Keywords(BASE),
    },
    HeadRule {
        prefix: &["DEFINE", "CONFIG"],
        yields: SlotYield::Keywords(&["IF", "OVERWRITE", "API", "GRAPHQL", "DEFAULT"]),
    },
    // ── REMOVE ── stmt/remove.rs:25-320
    HeadRule {
        prefix: &["REMOVE"],
        yields: SlotYield::Keywords(REMOVE_FORMS),
    },
    // `AND EXPUNGE` precedes `IF EXISTS`, and only NAMESPACE/DATABASE/TABLE
    // accept it (remove.rs:25-45).
    HeadRule {
        prefix: &["REMOVE", "NAMESPACE"],
        yields: SlotYield::Keywords(&["AND", "IF"]),
    },
    HeadRule {
        prefix: &["REMOVE", "DATABASE"],
        yields: SlotYield::Keywords(&["AND", "IF"]),
    },
    HeadRule {
        prefix: &["REMOVE", "TABLE"],
        yields: SlotYield::KeywordsAndTables(&["AND", "IF"]),
    },
    HeadRule {
        prefix: &["REMOVE", "NAMESPACE", "AND"],
        yields: SlotYield::Keywords(&["EXPUNGE"]),
    },
    HeadRule {
        prefix: &["REMOVE", "DATABASE", "AND"],
        yields: SlotYield::Keywords(&["EXPUNGE"]),
    },
    HeadRule {
        prefix: &["REMOVE", "TABLE", "AND"],
        yields: SlotYield::Keywords(&["EXPUNGE"]),
    },
    HeadRule {
        prefix: &["REMOVE", "FIELD", ANY],
        yields: SlotYield::Keywords(&["ON"]),
    },
    HeadRule {
        prefix: &["REMOVE", "FIELD", ANY, "ON"],
        yields: SlotYield::KeywordsAndTables(&["TABLE"]),
    },
    HeadRule {
        prefix: &["REMOVE", "EVENT", ANY],
        yields: SlotYield::Keywords(&["ON"]),
    },
    HeadRule {
        prefix: &["REMOVE", "EVENT", ANY, "ON"],
        yields: SlotYield::KeywordsAndTables(&["TABLE"]),
    },
    HeadRule {
        prefix: &["REMOVE", "INDEX", ANY],
        yields: SlotYield::Keywords(&["ON"]),
    },
    HeadRule {
        prefix: &["REMOVE", "INDEX", ANY, "ON"],
        yields: SlotYield::KeywordsAndTables(&["TABLE"]),
    },
    HeadRule {
        prefix: &["REMOVE", "USER", ANY],
        yields: SlotYield::Keywords(&["ON"]),
    },
    HeadRule {
        prefix: &["REMOVE", "USER", ANY, "ON"],
        yields: SlotYield::Keywords(BASE),
    },
    HeadRule {
        prefix: &["REMOVE", "ACCESS", ANY],
        yields: SlotYield::Keywords(&["ON"]),
    },
    HeadRule {
        prefix: &["REMOVE", "ACCESS", ANY, "ON"],
        yields: SlotYield::Keywords(BASE),
    },
    HeadRule {
        prefix: &["REMOVE", "ANALYZER"],
        yields: SlotYield::Analyzers,
    },
    HeadRule {
        prefix: &["ALTER", "ANALYZER"],
        yields: SlotYield::Analyzers,
    },
    HeadRule {
        prefix: &["REMOVE", "CONFIG"],
        yields: SlotYield::Keywords(&["IF", "GRAPHQL", "API", "DEFAULT"]),
    },
    // ── ALTER ── stmt/alter.rs:26-46
    HeadRule {
        prefix: &["ALTER"],
        yields: SlotYield::Keywords(ALTER_FORMS),
    },
    HeadRule {
        prefix: &["ALTER", "TABLE"],
        yields: SlotYield::KeywordsAndTables(&["IF"]),
    },
    HeadRule {
        prefix: &["ALTER", "FIELD", ANY],
        yields: SlotYield::Keywords(&["ON"]),
    },
    HeadRule {
        prefix: &["ALTER", "FIELD", ANY, "ON"],
        yields: SlotYield::KeywordsAndTables(&["TABLE"]),
    },
    HeadRule {
        prefix: &["ALTER", "EVENT", ANY],
        yields: SlotYield::Keywords(&["ON"]),
    },
    HeadRule {
        prefix: &["ALTER", "EVENT", ANY, "ON"],
        yields: SlotYield::KeywordsAndTables(&["TABLE"]),
    },
    HeadRule {
        prefix: &["ALTER", "INDEX", ANY],
        yields: SlotYield::Keywords(&["ON"]),
    },
    HeadRule {
        prefix: &["ALTER", "INDEX", ANY, "ON"],
        yields: SlotYield::KeywordsAndTables(&["TABLE"]),
    },
    HeadRule {
        prefix: &["ALTER", "USER", ANY],
        yields: SlotYield::Keywords(&["ON"]),
    },
    HeadRule {
        prefix: &["ALTER", "USER", ANY, "ON"],
        yields: SlotYield::Keywords(BASE),
    },
    HeadRule {
        prefix: &["ALTER", "ACCESS", ANY],
        yields: SlotYield::Keywords(&["ON"]),
    },
    HeadRule {
        prefix: &["ALTER", "ACCESS", ANY, "ON"],
        yields: SlotYield::Keywords(BASE),
    },
    // ── ACCESS ── stmt/mod.rs:129-271
    HeadRule {
        prefix: &["ACCESS", ANY],
        yields: SlotYield::Keywords(&["ON", "GRANT", "SHOW", "REVOKE", "PURGE"]),
    },
    HeadRule {
        prefix: &["ACCESS", ANY, "ON"],
        yields: SlotYield::Keywords(BASE),
    },
    HeadRule {
        prefix: &["ACCESS", ANY, "GRANT"],
        yields: SlotYield::Keywords(&["FOR"]),
    },
    HeadRule {
        prefix: &["ACCESS", ANY, "GRANT", "FOR"],
        yields: SlotYield::Keywords(&["USER", "RECORD"]),
    },
    HeadRule {
        prefix: &["ACCESS", ANY, "SHOW"],
        yields: SlotYield::Keywords(&["ALL", "GRANT", "WHERE"]),
    },
    HeadRule {
        prefix: &["ACCESS", ANY, "REVOKE"],
        yields: SlotYield::Keywords(&["ALL", "GRANT", "WHERE"]),
    },
    HeadRule {
        prefix: &["ACCESS", ANY, "PURGE"],
        yields: SlotYield::Keywords(&["EXPIRED", "REVOKED"]),
    },
    // ── shared: `IF` opens `IF NOT EXISTS` in every DEFINE/REMOVE/ALTER head,
    //    and `IF EXISTS` in REMOVE/ALTER/REBUILD. Both spellings are legal
    //    depending on the form, so offer both words.
    HeadRule {
        prefix: &["DEFINE", ANY, "IF"],
        yields: SlotYield::Keywords(&["NOT", "EXISTS"]),
    },
    HeadRule {
        prefix: &["DEFINE", ANY, "IF", "NOT"],
        yields: SlotYield::Keywords(&["EXISTS"]),
    },
    HeadRule {
        prefix: &["REMOVE", ANY, "IF"],
        yields: SlotYield::Keywords(&["EXISTS"]),
    },
    HeadRule {
        prefix: &["ALTER", ANY, "IF"],
        yields: SlotYield::Keywords(&["EXISTS"]),
    },
    HeadRule {
        prefix: &["REBUILD", "INDEX", "IF"],
        yields: SlotYield::Keywords(&["EXISTS"]),
    },
];

/// Slots identified by the *last* words typed rather than by the whole
/// statement.
///
/// The head table matches a statement from its first word, which cannot reach a
/// clause deep inside a long statement:
/// `DEFINE INDEX i ON person FIELDS name FULLTEXT ANALYZER ` is nine words in,
/// and enumerating every path to it is hopeless.
///
/// Kept to slots where a two-word tail is unambiguous on its own. `FULLTEXT
/// ANALYZER` appears in exactly one construct, so matching it needs no context.
const TAIL_RULES: &[(&[&str], SlotYield)] = &[(&["FULLTEXT", "ANALYZER"], SlotYield::Analyzers)];

/// The vocabulary legal directly after `words`.
///
/// `words` holds the statement's words up to the cursor, in order, already
/// split on whitespace and with the statement terminator removed. Matching is
/// case-insensitive, because SurrealQL keywords are.
///
/// Returns [`SlotYield::Expression`] when no rule matches, which every caller
/// must read as "offer what you offer today".
pub fn head_slot(words: &[&str]) -> SlotYield {
    let mut best: Option<(u32, SlotYield)> = None;
    for rule in HEAD_RULES {
        if !matches_prefix(rule.prefix, words) {
            continue;
        }
        let score = specificity(rule.prefix);
        if best.is_none_or(|(highest, _)| score > highest) {
            best = Some((score, rule.yields));
        }
    }
    if let Some((_, yields)) = best {
        return yields;
    }

    // No head rule matched. Try the tails.
    for (tail, yields) in TAIL_RULES {
        if words.len() >= tail.len() && matches_prefix(tail, &words[words.len() - tail.len()..]) {
            return *yields;
        }
    }

    SlotYield::Expression
}

/// How specific a prefix is, weighted so that the slot nearest the cursor
/// decides.
///
/// Two rules can match with the same number of wildcards. `DEFINE TABLE IF `
/// matches both `["DEFINE", "TABLE", ANY]` (reading `IF` as a table name) and
/// `["DEFINE", ANY, "IF"]` (reading it as the head of `IF NOT EXISTS`). The
/// second is right: a table actually called `IF` needs backticks, and the
/// prologue is the far commoner intent. Generalised, the word immediately
/// before the cursor is the strongest signal, so bit `i` carries position `i`
/// and a plain comparison weighs the last position most.
fn specificity(prefix: &[&str]) -> u32 {
    debug_assert!(prefix.len() <= 32, "a rule prefix must fit the score mask");
    prefix
        .iter()
        .enumerate()
        .filter(|(_, word)| **word != ANY)
        .fold(0, |mask, (index, _)| mask | 1 << index)
}

fn matches_prefix(prefix: &[&str], words: &[&str]) -> bool {
    prefix.len() == words.len()
        && prefix
            .iter()
            .zip(words)
            .all(|(expected, actual)| *expected == ANY || expected.eq_ignore_ascii_case(actual))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::grammar::KEYWORDS;

    fn keywords(words: &[&str]) -> Vec<&'static str> {
        match head_slot(words) {
            SlotYield::Keywords(list) | SlotYield::KeywordsAndTables(list) => list.to_vec(),
            other => panic!("expected a keyword slot for {words:?}, got {other:?}"),
        }
    }

    #[test]
    fn info_for_offers_exactly_the_nine_engine_targets() {
        // The reported bug: this position returned ~375 items.
        assert_eq!(
            keywords(&["INFO", "FOR"]),
            vec![
                "ROOT",
                "NAMESPACE",
                "NS",
                "DATABASE",
                "DB",
                "TABLE",
                "TB",
                "USER",
                "INDEX"
            ]
        );
    }

    #[test]
    fn info_for_does_not_offer_the_scope_targets_surrealdb_dropped() {
        let offered = keywords(&["INFO", "FOR"]);
        assert!(
            !offered.contains(&"SC") && !offered.contains(&"SCOPE"),
            "SurrealDB 3.x has no INFO FOR SCOPE arm (stmt/mod.rs:417-476), got {offered:?}"
        );
    }

    #[test]
    fn info_alone_offers_only_for() {
        assert_eq!(keywords(&["INFO"]), vec!["FOR"]);
    }

    #[test]
    fn info_for_table_offers_tables() {
        assert_eq!(head_slot(&["INFO", "FOR", "TABLE"]), SlotYield::Tables);
        assert_eq!(head_slot(&["INFO", "FOR", "TB"]), SlotYield::Tables);
    }

    #[test]
    fn matching_ignores_keyword_case() {
        assert_eq!(head_slot(&["info", "for"]), head_slot(&["INFO", "FOR"]));
        assert_eq!(head_slot(&["Info", "For", "Table"]), SlotYield::Tables);
    }

    #[test]
    fn a_spelled_word_beats_a_wildcard_at_the_same_slot() {
        // `["DEFINE", ANY, "IF"]` and `["REBUILD","INDEX","IF"]` both have
        // length 3; the wildcard rule must not shadow a literal one.
        assert_eq!(keywords(&["REBUILD", "INDEX", "IF"]), vec!["EXISTS"]);
    }

    #[test]
    fn the_slot_nearest_the_cursor_breaks_a_wildcard_tie() {
        // `DEFINE TABLE IF ` matches `["DEFINE","TABLE",ANY]` and
        // `["DEFINE",ANY,"IF"]` with one wildcard each. `IF` opens the
        // `IF NOT EXISTS` prologue; a table named `IF` needs backticks.
        assert_eq!(keywords(&["DEFINE", "TABLE", "IF"]), vec!["NOT", "EXISTS"]);
        // The same tie the other way round: a real name still reaches the bag.
        assert!(keywords(&["DEFINE", "TABLE", "person"]).contains(&"SCHEMAFULL"));
    }

    #[test]
    fn use_offers_the_four_scope_keywords_and_default() {
        assert_eq!(
            keywords(&["USE"]),
            vec!["NAMESPACE", "NS", "DATABASE", "DB", "DEFAULT"]
        );
    }

    #[test]
    fn define_offers_the_sixteen_sub_forms() {
        let offered = keywords(&["DEFINE"]);
        assert_eq!(offered.len(), 16, "got {offered:?}");
        assert!(offered.contains(&"SEQUENCE") && offered.contains(&"MODULE"));
    }

    #[test]
    fn define_does_not_offer_the_sub_forms_surrealdb_removed() {
        let offered = keywords(&["DEFINE"]);
        for gone in ["TOKEN", "SCOPE", "MODEL"] {
            assert!(!offered.contains(&gone), "3.x has no DEFINE {gone} arm");
        }
    }

    #[test]
    fn alter_offers_the_seventeen_sub_forms() {
        assert_eq!(keywords(&["ALTER"]).len(), 17);
    }

    #[test]
    fn remove_offers_the_sixteen_sub_forms() {
        assert_eq!(keywords(&["REMOVE"]).len(), 16);
    }

    #[test]
    fn define_table_clause_bag_uses_as_not_view() {
        let offered = keywords(&["DEFINE", "TABLE", "person"]);
        assert!(offered.contains(&"AS"), "the view clause opens with AS");
        assert!(
            !offered.contains(&"VIEW"),
            "DEFINE TABLE t VIEW … is a parse error (define.rs:710-722)"
        );
    }

    #[test]
    fn define_table_type_offers_only_the_three_table_types() {
        assert_eq!(
            keywords(&["DEFINE", "TABLE", "person", "TYPE"]),
            vec!["NORMAL", "RELATION", "ANY"]
        );
    }

    #[test]
    fn remove_namespace_offers_and_before_if() {
        // `REMOVE NAMESPACE [AND EXPUNGE] [IF EXISTS] <name>` — AND comes
        // first (remove.rs:25-45).
        assert_eq!(keywords(&["REMOVE", "NAMESPACE"]), vec!["AND", "IF"]);
        assert_eq!(keywords(&["REMOVE", "NAMESPACE", "AND"]), vec!["EXPUNGE"]);
    }

    #[test]
    fn on_clause_offers_the_three_bases_with_both_spellings() {
        assert_eq!(
            keywords(&["DEFINE", "USER", "bob", "ON"]),
            vec!["ROOT", "NAMESPACE", "NS", "DATABASE", "DB"]
        );
    }

    #[test]
    fn field_and_event_on_slots_offer_the_table_keyword_and_table_names() {
        for words in [
            vec!["DEFINE", "FIELD", "name", "ON"],
            vec!["DEFINE", "EVENT", "audit", "ON"],
            vec!["DEFINE", "INDEX", "idx", "ON"],
            vec!["REMOVE", "FIELD", "name", "ON"],
            vec!["ALTER", "FIELD", "name", "ON"],
        ] {
            assert_eq!(
                head_slot(&words),
                SlotYield::KeywordsAndTables(&["TABLE"]),
                "{words:?} must offer TABLE and the known tables"
            );
        }
    }

    #[test]
    fn the_field_clause_bag_survives_the_optional_table_keyword() {
        let without = head_slot(&["DEFINE", "FIELD", "name", "ON", "person"]);
        let with = head_slot(&["DEFINE", "FIELD", "name", "ON", "TABLE", "person"]);
        assert_eq!(without, with, "`ON TABLE t` and `ON t` are the same slot");
    }

    #[test]
    fn access_purge_offers_only_the_two_grant_states() {
        assert_eq!(
            keywords(&["ACCESS", "api", "PURGE"]),
            vec!["EXPIRED", "REVOKED"]
        );
    }

    #[test]
    fn an_unmodelled_position_falls_back_to_the_current_behaviour() {
        // Every one of these keeps today's full list rather than guessing.
        for words in [
            vec![],
            vec!["SELECT"],
            vec!["SELECT", "*", "FROM", "person"],
            vec![
                "SELECT", "*", "FROM", "person", "WHERE", "age", ">", "3", "AND",
            ],
            vec!["CREATE"],
            vec!["LET"],
            vec!["DEFINE", "FIELD", "name", "ON", "person", "DEFAULT"],
            vec!["INFO", "FOR", "USER"],
            vec!["REMOVE", "USER"],
            vec!["SLEEP"],
            vec!["KILL"],
        ] {
            assert_eq!(
                head_slot(&words),
                SlotYield::Expression,
                "{words:?} must not be narrowed"
            );
        }
    }

    #[test]
    fn no_slot_ever_yields_an_empty_keyword_list() {
        // An empty list is a wrong answer; the fallback for "we do not know"
        // is `Expression`.
        for rule in HEAD_RULES {
            match rule.yields {
                SlotYield::Keywords(list) | SlotYield::KeywordsAndTables(list) => assert!(
                    !list.is_empty(),
                    "{:?} yields an empty keyword list",
                    rule.prefix
                ),
                SlotYield::Tables | SlotYield::Analyzers | SlotYield::Expression => {}
            }
        }
    }

    #[test]
    fn every_rule_prefix_starts_with_a_statement_keyword() {
        for rule in HEAD_RULES {
            let first = rule.prefix.first().expect("a rule needs a prefix");
            assert_ne!(*first, ANY, "a rule must not open with a wildcard");
            assert!(
                KEYWORDS.contains(first),
                "`{first}` is not a grammar keyword"
            );
        }
    }

    #[test]
    fn offers_outside_the_grammar_are_declared() {
        // Catches a typo in the tables: any offered word that the pinned
        // grammar cannot lex must be a known, documented gap.
        for rule in HEAD_RULES {
            let offered = match rule.yields {
                SlotYield::Keywords(list) | SlotYield::KeywordsAndTables(list) => list,
                SlotYield::Tables | SlotYield::Analyzers | SlotYield::Expression => continue,
            };
            for word in offered {
                assert!(
                    KEYWORDS.contains(word) || OFFERS_THE_GRAMMAR_CANNOT_PARSE.contains(word),
                    "`{word}` (offered at {:?}) is neither a grammar keyword nor a \
                     declared gap — fix the spelling or add it to \
                     OFFERS_THE_GRAMMAR_CANNOT_PARSE",
                    rule.prefix
                );
            }
        }
    }

    #[test]
    fn every_declared_grammar_gap_is_really_absent_from_the_grammar() {
        // Keeps the gap list honest: a grammar bump that adds one of these
        // words must shorten the list rather than leave a stale entry.
        for word in OFFERS_THE_GRAMMAR_CANNOT_PARSE {
            assert!(
                !KEYWORDS.contains(word),
                "`{word}` is in the grammar now — remove it from \
                 OFFERS_THE_GRAMMAR_CANNOT_PARSE"
            );
        }
    }
}