relate-parser 1.0.2

Cypher lexer, parser, and AST for the Relate graph database
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
use super::*;

impl<'a> Parser<'a> {
    // ---- SHOW command family (CIP-11/CIP-68/CIP-84 and friends) ----

    /// Parses the `YIELD col [AS alias] [, col [AS alias] ...] | YIELD *`
    /// production shared by every catalog-backed SHOW/TERMINATE command and
    /// by `CALL proc() YIELD ...`. The one parser for the YIELD tail; callers
    /// read [`YieldList::present`] when they need to know whether a `YIELD`
    /// was written at all.
    pub(super) fn parse_show_yield_list(&mut self) -> ParseResult<YieldList> {
        let mut items = Vec::new();
        let mut all = false;
        if self.peek_type() == TokenType::YIELD {
            self.next_token();
            if self.peek_type() == TokenType::Star {
                self.next_token();
                all = true;
            } else {
                loop {
                    let yield_tok = self.next_token();
                    if !yield_tok.ty.is_ident_like() {
                        return Err(self.error_at_token(
                            &yield_tok,
                            format!(
                                "expected identifier after YIELD, got {:?}",
                                yield_tok.literal
                            ),
                        ));
                    }
                    let alias = if self.peek_type() == TokenType::AS {
                        self.next_token();
                        Some(self.parse_alias_after_as()?)
                    } else {
                        None
                    };
                    items.push(YieldItem {
                        name: SmallName::from_name(&yield_tok.literal),
                        alias,
                    });
                    if self.peek_type() != TokenType::Comma {
                        break;
                    }
                    self.next_token();
                }
            }
        }
        Ok(YieldList { items, all })
    }

    /// The optional `EXECUTABLE [BY <user> | BY CURRENT USER]` selector of
    /// `SHOW FUNCTIONS`/`SHOW PROCEDURES`. `EXECUTABLE` is a plain
    /// (non-reserved) identifier, so it is recognized by spelling; the user
    /// after `BY` is an identifier or the `CURRENT USER` pair (live Neo4j
    /// rejects a quoted string there with 42I06). The bare `EXECUTABLE` form
    /// filters nothing, so it parses to `None` — the same state as writing no
    /// selector at all.
    fn parse_executable_tail(&mut self) -> ParseResult<Option<ExecutableBy>> {
        if !self.peek_is_ident("executable") {
            return Ok(None);
        }
        self.next_token(); // consume EXECUTABLE
        if self.peek_type() != TokenType::BY {
            return Ok(None);
        }
        self.next_token(); // consume BY
        if self.peek_is_ident("current") {
            self.next_token(); // consume CURRENT
            self.expect_keyword_ident("USER")?;
            return Ok(Some(ExecutableBy::Current));
        }
        let tok = self.next_token();
        if !tok.ty.is_ident_like() {
            return Err(self.error_at_token(&tok, "expected an identifier after BY"));
        }
        Ok(Some(ExecutableBy::User(tok.literal.into_owned())))
    }

    /// The optional trailing `WHERE expr` shared by every `parse_show_*`
    /// parser (permitted whether or not an explicit
    /// `YIELD` precedes it). Returns `None` when no `WHERE` was written.
    fn parse_show_where_tail(&mut self) -> ParseResult<Option<Expr>> {
        if self.peek_type() == TokenType::WHERE {
            self.next_token();
            Ok(Some(self.parse_expression()?))
        } else {
            Ok(None)
        }
    }

    /// Case-normalizing lookahead for the SHOW dispatch: the lowercased
    /// literal of the token `offset` ahead when it is a word -- an
    /// `Identifier` or a keyword token (`INDEX`/`CONSTRAINT`/`ALL`/
    /// `POINT`/`VECTOR`/`NODE`/`REL`/`UNIQUE` are reserved tokens; every
    /// other dispatch word is identifier-spelled) -- else `None`.
    fn word_at(&self, offset: usize) -> Option<String> {
        let tok = self.peek_at(offset);
        if tok.ty == TokenType::Identifier || tok.ty.is_keyword() {
            Some(tok.literal.to_ascii_lowercase())
        } else {
            None
        }
    }

    /// SHOW-command family dispatch: the current token is the `SHOW`
    /// identifier (a non-reserved word, recognized by spelling at clause
    /// position). Routes to the dedicated `parse_show_*` parser by
    /// lookahead on the kind word(s) following SHOW; the exact
    /// singular-vs-plural validation happens inside each parser.
    pub(super) fn parse_show_dispatch(&mut self) -> ParseResult<Clause> {
        // The `<type filter>` prefix (`[ALL]
        // [NODE|REL[ATIONSHIP]] [PROPERTY]?
        // (UNIQUE[NESS]|KEY|EXIST[ENCE]|TYPE)`) can push CONSTRAINT(S)
        // up to 4 tokens ahead of SHOW -- scan forward through only
        // recognized filter words looking for it, same lookahead
        // technique as the INDEX kind-word check below.
        let mut show_targets_constraints = false;
        for i in 1..=4 {
            match self.word_at(i).as_deref() {
                Some("constraint" | "constraints") => {
                    show_targets_constraints = true;
                    break;
                }
                Some(
                    "all" | "node" | "rel" | "unique" | "relationship" | "property" | "uniqueness"
                    | "key" | "type" | "exist" | "existence",
                ) => {}
                _ => break,
            }
        }
        if show_targets_constraints {
            return self.parse_show_constraints();
        }
        // CIP-68: the remaining SHOW-command families, each dispatched by
        // spelling on the KIND word right after SHOW (or, for
        // `DEFAULT`/`HOME DATABASE` and `[ALL|POPULATED] ROLE[S]`, one
        // word further ahead) -- same lookahead technique as the
        // INDEX/CONSTRAINT scan above. The exact singular-vs-plural (and
        // `DEFAULT`/`HOME`/`CURRENT` singular-only) validation happens
        // inside each dedicated parse function; this dispatch only needs
        // to recognize which family a scenario is naming.
        match (self.word_at(1).as_deref(), self.word_at(2).as_deref()) {
            (Some("index" | "indexes"), _)
            | (
                Some(
                    "all" | "point" | "vector" | "range" | "text" | "fulltext" | "lookup" | "btree",
                ),
                Some("index" | "indexes"),
            ) => self.parse_show_indexes(),
            (Some("database" | "databases" | "default" | "home"), _) => self.parse_show_databases(),
            (Some("alias" | "aliases"), _) => {
                // ADR-0116 §2: `SHOW ALIAS[ES] ... FOR DATABASE[S]` --
                // recognized, then rejected with CE's
                // `UnsupportedAdministrationCommand`.
                let start = self.pos;
                self.unsupported_admin_clause(start)
            }
            (Some("role" | "roles"), _) | (Some("all" | "populated"), Some("role" | "roles")) => {
                self.parse_show_roles()
            }
            (Some("server" | "servers"), _) => self.parse_show_servers(),
            // `SHOW CURRENT GRAPH TYPE`, checked before
            // the general `current` -> `SHOW CURRENT USER` fallback
            // below.
            (Some("current"), Some("graph")) => self.parse_show_current_graph_type(),
            // CIP-178/CIP-180: `SHOW GRAPH TYPE <name> [YIELD ...]`.
            (Some("graph"), Some("type")) => self.parse_show_graph_type(),
            // `SHOW USER[S]`. The `defined` guard is a
            // load-bearing negative lookahead: it lets `SHOW USER
            // DEFINED FUNCTIONS` fall through to the
            // `parse_show_functions` catch-all below (where it parses as
            // `FunctionScope::UserDefined`) instead of being swallowed
            // here as `SHOW USER[S]`.
            (Some("user" | "users"), second) if second != Some("defined") => {
                self.parse_show_users()
            }
            // `SHOW CURRENT USER`.
            (Some("current"), _) => self.parse_show_users(),
            (Some("procedure" | "procedures"), _) => self.parse_show_procedures(),
            (Some("setting" | "settings"), _) => self.parse_show_settings(),
            (Some("transaction" | "transactions"), _) => self.parse_show_transactions(),
            _ => self.parse_show_functions(),
        }
    }

    /// `SHOW [ALL | BUILT IN | USER DEFINED] FUNCTIONS [YIELD col [, col
    /// ...] | YIELD *] [WHERE expr]` (CIP-245/CIP-247/CIP-263/CIP-84/
    /// CIP-157). Only the function catalog is supported — not a general
    /// SHOW-command framework (indexes/constraints/databases are a
    /// separate, larger feature out of scope here).
    pub(super) fn parse_show_functions(&mut self) -> ParseResult<Clause> {
        self.next_token(); // consume SHOW

        let scope = if self.peek_type() == TokenType::ALL {
            self.next_token();
            FunctionScope::All
        } else if self.peek_is_ident("built") {
            self.next_token(); // consume BUILT
            self.expect(TokenType::IN)?;
            FunctionScope::BuiltIn
        } else if self.peek_is_ident("user") {
            self.next_token(); // consume USER
            self.expect_kind_word(TokenType::Identifier, "defined", None, "DEFINED after USER")?;
            FunctionScope::UserDefined
        } else {
            FunctionScope::All
        };

        // CIP-68: both FUNCTION and FUNCTIONS are accepted.
        self.expect_kind_word(
            TokenType::Identifier,
            "function",
            Some("functions"),
            "FUNCTIONS after SHOW",
        )?;

        let executable = self.parse_executable_tail()?;

        let yields = self.parse_show_yield_list()?;

        let stages = self.parse_page_stages()?;

        let where_expr = self.parse_show_where_tail()?;

        Ok(Clause::ShowFunctions(ShowFunctionsClause {
            yields,
            where_expr,
            stages,
            scope,
            executable,
        }))
    }

    /// `SHOW [<type filter>] CONSTRAINTS [YIELD col [, col ...] | YIELD *]
    /// [WHERE expr]` (CIP-85's observation surface for `propertyType`;
    /// the `<type filter>` grammar is CIP-62/CIP-64/
    /// CIP-74's `[ALL] [NODE|REL[ATIONSHIP]] [PROPERTY]?
    /// (UNIQUE[NESS]|KEY|EXIST[ENCE]|TYPE)`). Mirrors `parse_show_functions`
    /// otherwise.
    pub(super) fn parse_show_constraints(&mut self) -> ParseResult<Clause> {
        self.next_token(); // consume SHOW

        let type_filter = self.parse_constraint_type_filter()?;

        self.expect_kind_word(
            TokenType::CONSTRAINT,
            "constraint",
            Some("constraints"),
            "CONSTRAINT(S)",
        )?;

        let yields = self.parse_show_yield_list()?;

        // The YIELD's own `ORDER BY`/`SKIP`/
        // `LIMIT` paging sub-grammar, written directly after the YIELD
        // list and before any trailing `WHERE` -- distinct from a
        // `RETURN ... ORDER BY ...` (or a separate standalone
        // `ORDER BY`/`SKIP`/`LIMIT` clause) that follows this whole SHOW
        // clause instead. Shared with the standalone paging clause via
        // `parse_page_stages`.
        let stages = self.parse_page_stages()?;

        let where_expr = self.parse_show_where_tail()?;

        Ok(Clause::ShowConstraints(ShowConstraintsClause {
            yields,
            where_expr,
            type_filter,
            stages,
        }))
    }

    /// Parses the `<type filter>` prefix of `SHOW <type filter> CONSTRAINTS`
    /// (CIP-62/CIP-64/CIP-74): `[ALL] [NODE|REL[ATIONSHIP]] [PROPERTY]?
    /// (UNIQUE[NESS]|KEY|EXIST[ENCE]|TYPE)`, or nothing at all (bare `SHOW
    /// CONSTRAINTS`, equivalent to `ALL`). `PROPERTY` is only meaningful
    /// directly before `UNIQUE[NESS]`/`EXIST[ENCE]`/`TYPE` -- it never
    /// combines with `KEY` and never stands alone.
    pub(super) fn parse_constraint_type_filter(&mut self) -> ParseResult<ConstraintTypeFilter> {
        if self.peek_type() == TokenType::ALL {
            self.next_token();
            return Ok(ConstraintTypeFilter::all());
        }

        let mut entity = ConstraintEntityFilter::Any;
        if self.peek_type() == TokenType::NODE {
            self.next_token();
            entity = ConstraintEntityFilter::Node;
        } else if self.peek_type() == TokenType::REL || self.peek_is_ident("relationship") {
            self.next_token();
            entity = ConstraintEntityFilter::Relationship;
        }

        let had_property = if self.peek_is_ident("property") {
            self.next_token();
            true
        } else {
            false
        };

        // The kind half: read the kind word once. CIP-187's `PROPERTY
        // EXIST[ENCE]` spelling is its own kind variant (property
        // existence only), so the `PROPERTY` prefix folds into the
        // `EXIST[ENCE]` arm at parse time -- no side-channel bool on the
        // filter. `PROPERTY` never combines with `KEY` and never stands
        // alone.
        let kind: Option<ConstraintKindFilter> = if self.peek_is_ident("key") {
            Some(ConstraintKindFilter::Key)
        } else if self.peek_type() == TokenType::UNIQUE || self.peek_is_ident("uniqueness") {
            Some(ConstraintKindFilter::Unique)
        } else if self.peek_is_ident("exist") || self.peek_is_ident("existence") {
            Some(if had_property {
                ConstraintKindFilter::PropertyExistence
            } else {
                ConstraintKindFilter::Existence
            })
        } else if self.peek_is_ident("type") {
            Some(ConstraintKindFilter::PropertyType)
        } else {
            None
        };

        let filter = match (kind, had_property) {
            (Some(ConstraintKindFilter::Key), true) => {
                return Err(self.error(
                    "InvalidConstraintTypeFilter: PROPERTY cannot combine with the KEY filter",
                ));
            }
            (None, true) => {
                return Err(self.error(
                    "InvalidConstraintTypeFilter: PROPERTY must be followed by UNIQUE[NESS], \
                     EXIST[ENCE], or TYPE",
                ));
            }
            _ => ConstraintTypeFilter { entity, kind },
        };
        if kind.is_some() {
            self.next_token(); // consume the kind word
        }
        Ok(filter)
    }

    /// `SHOW [ALL|RANGE|POINT|TEXT|FULLTEXT|VECTOR|LOOKUP] INDEXES [YIELD
    /// col [, col ...] | YIELD *] [WHERE expr]` (CIP-11). `SHOW BTREE
    /// INDEXES` is dispatched here too, purely to raise a clean
    /// `UnsupportedIndexType` error (BTREE was removed, not
    /// deprecated).
    pub(super) fn parse_show_indexes(&mut self) -> ParseResult<Clause> {
        self.next_token(); // consume SHOW

        let tok = self.peek().clone();
        let kind_filter = match tok.ty {
            TokenType::ALL => {
                self.next_token();
                None
            }
            TokenType::POINT => {
                self.next_token();
                Some(IndexKind::Point)
            }
            TokenType::VECTOR => {
                self.next_token();
                Some(IndexKind::Vector)
            }
            TokenType::Identifier => match tok.literal.to_ascii_lowercase().as_str() {
                "range" => {
                    self.next_token();
                    Some(IndexKind::Range)
                }
                "text" => {
                    self.next_token();
                    Some(IndexKind::Text)
                }
                "fulltext" => {
                    self.next_token();
                    Some(IndexKind::Fulltext)
                }
                "lookup" => {
                    self.next_token();
                    Some(IndexKind::Lookup)
                }
                "btree" => {
                    return Err(self.error_at_token(
                        &tok,
                        "UnsupportedIndexType: BTREE indexes are no longer supported".to_string(),
                    ));
                }
                _ => None,
            },
            _ => None,
        };

        // consume INDEX / INDEXES
        self.expect_kind_word(TokenType::INDEX, "index", Some("indexes"), "INDEXES")?;

        let yields = self.parse_show_yield_list()?;

        // The YIELD's own `ORDER
        // BY`/`SKIP`/`LIMIT` paging sub-grammar, written directly after the
        // YIELD list and before any trailing `WHERE` -- shared with
        // `parse_show_constraints` via `parse_page_stages`.
        let stages = self.parse_page_stages()?;

        let where_expr = self.parse_show_where_tail()?;

        Ok(Clause::ShowIndexes(ShowIndexesClause {
            kind_filter,
            yields,
            where_expr,
            stages,
        }))
    }

    /// `SHOW GRAPH TYPE <name> [YIELD ...]` (CIP-180's SHOW surface).
    pub(super) fn parse_show_graph_type(&mut self) -> ParseResult<Clause> {
        self.next_token(); // consume SHOW
        self.expect_keyword_ident("GRAPH")?;
        self.expect_keyword_ident("TYPE")?;
        let name = self.parse_bare_name("identifier")?;
        let yields = self.parse_show_yield_list()?;
        Ok(Clause::ShowGraphType(ShowGraphTypeClause { name, yields }))
    }

    /// `SHOW CURRENT GRAPH TYPE [AS GRAPH [SINGLE | SEPARATE]] [YIELD ...]
    /// [WHERE ...]` (CIP-176 overview / CIP-184 / the `AS GRAPH`
    /// visualization extension).
    pub(super) fn parse_show_current_graph_type(&mut self) -> ParseResult<Clause> {
        self.next_token(); // consume SHOW
        self.expect_keyword_ident("CURRENT")?;
        self.expect_keyword_ident("GRAPH")?;
        self.expect_keyword_ident("TYPE")?;
        // `AS GRAPH` is a contextual, non-reserved extension --
        // `AS` is a real keyword token, but the following `GRAPH` (and the
        // optional `SINGLE`/`SEPARATE` any-node keyword after it) are plain
        // identifiers, so this only commits once both `AS` and an
        // identifier literally spelled `GRAPH` are seen.
        let as_graph = if self.peek_type() == TokenType::AS && self.peek_at_is_ident(1, "graph") {
            self.next_token(); // AS
            self.next_token(); // GRAPH
            let mode = if self.peek_is_ident("single") {
                self.next_token();
                AnyNodeMode::Single
            } else if self.peek_is_ident("separate") {
                self.next_token();
                AnyNodeMode::Separate
            } else {
                AnyNodeMode::Separate
            };
            Some(mode)
        } else {
            None
        };
        let yields = self.parse_show_yield_list()?;
        // Same YIELD paging sub-grammar as the other SHOW parsers
        // (`parse_page_stages`) -- OFFSET is accepted as a SKIP synonym
        // here too (CIP-168: OFFSET is usable everywhere SKIP is).
        let stages = self.parse_page_stages()?;
        let where_expr = self.parse_show_where_tail()?;
        Ok(Clause::ShowCurrentGraphType(ShowCurrentGraphTypeClause {
            yields,
            stages,
            where_expr,
            as_graph,
        }))
    }

    /// `SHOW DATABASE[S] [<name>] [YIELD ...] [WHERE ...]` / `SHOW DEFAULT
    /// DATABASE` / `SHOW HOME DATABASE` (CIP-42's four new non-default
    /// columns; CIP-68's singular/plural KIND acceptance). `DEFAULT`/`HOME`
    /// are singular-only: requiring the literal `DATABASE` spelling right
    /// after them (rather than the shared FUNCTION[S]-style
    /// singular-or-plural check) naturally rejects `SHOW DEFAULT
    /// DATABASES`/`SHOW HOME DATABASES` with a parse error, matching live
    /// Neo4j's documented exception.
    pub(super) fn parse_show_databases(&mut self) -> ParseResult<Clause> {
        self.next_token(); // consume SHOW

        let filter = if self.peek_is_ident("default") {
            self.next_token();
            self.expect_keyword_ident("DATABASE")?;
            DatabaseFilter::Default
        } else if self.peek_is_ident("home") {
            self.next_token();
            self.expect_keyword_ident("DATABASE")?;
            DatabaseFilter::Home
        } else {
            self.next_token(); // consume DATABASE / DATABASES
            match self.parse_optional_schema_name() {
                Some(name) => DatabaseFilter::Named(name),
                None => DatabaseFilter::All,
            }
        };

        let yields = self.parse_show_yield_list()?;
        let stages = self.parse_page_stages()?;
        let where_expr = self.parse_show_where_tail()?;

        Ok(Clause::ShowDatabases(ShowDatabasesClause {
            filter,
            yields,
            where_expr,
            stages,
        }))
    }

    /// `SHOW [ALL|POPULATED] ROLE[S] [WITH USER[S]] [YIELD ...] [WHERE
    /// ...]` (CIP-68).
    pub(super) fn parse_show_roles(&mut self) -> ParseResult<Clause> {
        self.next_token(); // consume SHOW

        let modifier = if self.peek_type() == TokenType::ALL {
            self.next_token();
            RoleModifier::All
        } else if self.peek_is_ident("populated") {
            self.next_token();
            RoleModifier::Populated
        } else {
            RoleModifier::Any
        };

        self.next_token(); // consume ROLE / ROLES

        let with_users = if self.peek_type() == TokenType::WITH {
            self.next_token();
            let user_tok = self.next_token();
            if !(user_tok.ty == TokenType::Identifier
                && (user_tok.literal.eq_ignore_ascii_case("user")
                    || user_tok.literal.eq_ignore_ascii_case("users")))
            {
                return Err(self.error_at_token(
                    &user_tok,
                    format!("expected USER(S) after WITH, got {:?}", user_tok.literal),
                ));
            }
            true
        } else {
            false
        };

        let yields = self.parse_show_yield_list()?;
        let stages = self.parse_page_stages()?;
        let where_expr = self.parse_show_where_tail()?;

        Ok(Clause::ShowRoles(ShowRolesClause {
            modifier,
            with_users,
            yields,
            where_expr,
            stages,
        }))
    }

    /// `SHOW SERVER[S] [YIELD ...] [WHERE ...]` (CIP-68).
    pub(super) fn parse_show_servers(&mut self) -> ParseResult<Clause> {
        self.next_token(); // consume SHOW
        self.next_token(); // consume SERVER / SERVERS

        let yields = self.parse_show_yield_list()?;
        let stages = self.parse_page_stages()?;
        let where_expr = self.parse_show_where_tail()?;

        Ok(Clause::ShowServers(ShowServersClause {
            yields,
            where_expr,
            stages,
        }))
    }

    /// `SHOW USER[S] [YIELD ...] [WHERE ...]` / `SHOW CURRENT USER`
    /// (CIP-68). `CURRENT` is singular-only, same technique as `DEFAULT`/
    /// `HOME DATABASE`.
    pub(super) fn parse_show_users(&mut self) -> ParseResult<Clause> {
        self.next_token(); // consume SHOW

        let current_only = if self.peek_is_ident("current") {
            self.next_token();
            self.expect_keyword_ident("USER")?;
            true
        } else {
            self.next_token(); // consume USER / USERS
            false
        };

        let yields = self.parse_show_yield_list()?;
        let stages = self.parse_page_stages()?;
        let where_expr = self.parse_show_where_tail()?;

        Ok(Clause::ShowUsers(ShowUsersClause {
            current_only,
            yields,
            where_expr,
            stages,
        }))
    }

    /// `SHOW PROCEDURE[S] [YIELD ...] [WHERE ...]` (CIP-68 grammar
    /// foundation).
    pub(super) fn parse_show_procedures(&mut self) -> ParseResult<Clause> {
        self.next_token(); // consume SHOW
        self.next_token(); // consume PROCEDURE / PROCEDURES

        let executable = self.parse_executable_tail()?;

        let yields = self.parse_show_yield_list()?;
        let stages = self.parse_page_stages()?;
        let where_expr = self.parse_show_where_tail()?;

        Ok(Clause::ShowProcedures(ShowProceduresClause {
            yields,
            where_expr,
            stages,
            executable,
        }))
    }

    /// True if the parser is positioned at a token that can only be the
    /// start of the optional `<names>`/`<id-list>` filter expression on
    /// `SHOW SETTINGS`/`SHOW TRANSACTIONS`/`TERMINATE TRANSACTIONS`.
    /// `YIELD`/`WHERE` are reserved keywords with their own `TokenType`
    /// (never `Identifier`), so they never need excluding here. CIP-123/
    /// CIP-253: the filter expression is no longer restricted to a
    /// literal/parameter/list -- a composed `SHOW SETTINGS`/`SHOW
    /// TRANSACTIONS`/`TERMINATE TRANSACTIONS` may reference a variable
    /// bound by an earlier clause (`SHOW SETTINGS inputs`, `SHOW SETTING
    /// name`) or a parenthesized expression
    /// (`TERMINATE TRANSACTIONS (database + '-transaction-999999')`), so a
    /// bare `Identifier`/`LeftParen`/numeric/boolean-literal start must also
    /// be accepted. The one thing this must *not* swallow is a following
    /// bare `SHOW`/`TERMINATE` clause composed right after this one with no
    /// filter expression of its own (`SHOW SETTINGS` then a fresh `SHOW
    /// TRANSACTIONS ...` clause) -- so the contextual (non-reserved)
    /// `Identifier`-spelled clause keywords are excluded here even though
    /// they syntactically look like a plain identifier reference. The
    /// excluded set is single-sourced as `CONTEXTUAL_CLAUSE_WORDS` in
    /// token.rs, shared with `parse_clause_inner`'s dispatch.
    pub(super) fn peek_starts_show_filter_expr(&self) -> bool {
        match self.peek_type() {
            TokenType::String
            | TokenType::Parameter
            | TokenType::LeftBracket
            | TokenType::LeftParen
            | TokenType::NULL
            | TokenType::Integer
            | TokenType::Float
            | TokenType::TRUE
            | TokenType::FALSE
            | TokenType::Dash => true,
            TokenType::Identifier => !is_contextual_clause_keyword(&self.peek().literal),
            _ => false,
        }
    }

    /// `SHOW SETTING[S] [<names>] [YIELD ...] [WHERE ...]` (CIP-68 grammar
    /// foundation; CIP-84 `SHOW SETTINGS 'name1', 'name2', ...` name-list
    /// form). `names` reports the settings catalog CIP-84 scenarios pin.
    pub(super) fn parse_show_settings(&mut self) -> ParseResult<Clause> {
        self.next_token(); // consume SHOW
        self.next_token(); // consume SETTING / SETTINGS

        let names = if self.peek_starts_show_filter_expr() {
            // CIP-84: `SHOW SETTINGS 'name1', 'name2', ...` -- a bare
            // comma-separated list of names, distinct from the single-
            // expression (`$param`/`[...]`/`null`) forms. Collapses to a
            // single `List` expression so `ShowSettingsClause::names`
            // stays a single `Option<Expr>`.
            let mut items = self.parse_comma_separated_exprs()?;
            if items.len() == 1 {
                Some(items.pop().unwrap())
            } else {
                Some(Expr::List(items))
            }
        } else {
            None
        };

        let yields = self.parse_show_yield_list()?;
        let stages = self.parse_page_stages()?;
        let where_expr = self.parse_show_where_tail()?;

        Ok(Clause::ShowSettings(ShowSettingsClause {
            names,
            yields,
            where_expr,
            stages,
        }))
    }

    /// `SHOW TRANSACTION[S] [<id-list>] [YIELD ...] [WHERE ...]` (CIP-68
    /// grammar foundation). Relate has no live-transaction catalog, so
    /// `ids` is parsed but never changes the (always empty) result.
    pub(super) fn parse_show_transactions(&mut self) -> ParseResult<Clause> {
        self.next_token(); // consume SHOW
        self.next_token(); // consume TRANSACTION / TRANSACTIONS

        let ids = if self.peek_starts_show_filter_expr() {
            self.parse_comma_separated_exprs()?
        } else {
            Vec::new()
        };

        let yields = self.parse_show_yield_list()?;
        let stages = self.parse_page_stages()?;
        let where_expr = self.parse_show_where_tail()?;

        Ok(Clause::ShowTransactions(ShowTransactionsClause {
            ids,
            yields,
            where_expr,
            stages,
        }))
    }
}