relate-parser 1.5.0

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
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
use std::borrow::Cow;

// The token-fact vocabulary (REL8-1423): the generic kind classification
// (`ends_expression` / `introduces_operand`) the bracket classification
// derives from — a submodule of the token module with the other kind
// predicates (its file lives at `token/facts.rs`).
mod facts;

/// Token types for the Cypher lexer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TokenType {
    // Special tokens
    EOF,
    Error,
    Identifier,
    Integer,
    Float,
    String,
    /// CIP-261 interpolated string literal `s'...'` / `s"..."`. Payload is
    /// the body between the quotes with standard escapes resolved; `\{` and
    /// `\}` stay as two characters so the parser can tell an escaped literal
    /// brace from an expression delimiter, and raw `{`/`}` are kept for the
    /// parser to split into text/expression parts.
    InterpString,
    /// The Neo4j internal obfuscated literal `******` (exactly six stars),
    /// lexed only under the `obfuscated-literals` preview gate: a dummy
    /// `false` for semantic analysis, never meant to execute. Without the
    /// gate the run lexes as ordinary `*` tokens and the query fails with
    /// the parser's own syntax error, Neo4j's disabled-form behavior.
    ObfuscatedLiteral,
    /// The CIP-281 annotation introducer `@`, lexed only under the
    /// `cypher-annotations` preview gate. Without the gate `@` is the
    /// ordinary `unexpected character: @` error token.
    At,
    Parameter, // $param

    // Punctuation
    LeftParen,    // (
    RightParen,   // )
    LeftBracket,  // [
    RightBracket, // ]
    LeftBrace,    // {
    RightBrace,   // }
    Colon,        // :
    DoubleColon,  // ::
    Comma,        // ,
    Dot,          // .
    Semicolon,    // ;
    Pipe,         // |
    DoublePipe,   // ||
    Dash,         // -
    ArrowRight,   // ->
    ArrowLeft,    // <-
    FatArrow,     // => (CIP-176/CIP-187 graph type declaration syntax)
    Star,         // *
    Plus,         // +
    Slash,        // /
    Percent,      // %
    Caret,        // ^
    DotDot,       // ..
    Bang,         // ! (CIP-102 NOT NULL type shorthand; also GQL label negation, CIP-14)
    Ampersand,    // & (GQL label expression conjunction, CIP-14)

    // Comparison operators
    Equals,       // =
    NotEquals,    // <>
    LessThan,     // <
    GreaterThan,  // >
    LessEqual,    // <=
    GreaterEqual, // >=
    RegexMatch,   // =~

    // Keywords
    MATCH,
    OPTIONAL,
    CREATE,
    INSERT,
    MERGE,
    DELETE,
    DETACH,
    SET,
    REMOVE,
    RETURN,
    WITH,
    UNWIND,
    WHERE,
    ORDER,
    BY,
    ASC,
    DESC,
    LIMIT,
    SKIP,
    AS,
    DISTINCT,
    AND,
    OR,
    NOT,
    XOR,
    IN,
    STARTS,
    ENDS,
    CONTAINS,
    IS,
    NULL,
    TRUE,
    FALSE,
    CASE,
    WHEN,
    THEN,
    ELSE,
    END,
    FOREACH,
    CALL,
    YIELD,
    UNION,
    ALL,
    ON,
    INDEX,
    CONSTRAINT,
    FOR,
    REQUIRE,
    UNIQUE,
    EXISTS,
    IF,
    DROP,
    VECTOR,
    POINT,
    OPTIONS,
    NODE,
    REL,
    COUNT,
    COLLECT,
    LOAD,
    FROM,
    CSV,
    REDUCE,
    SPATIAL,
    EXPLAIN,
    PROFILE,
    INFINITY, // Infinity/Inf literal (CIP-55)
    NAN,      // NaN literal (CIP-55)
}

impl TokenType {
    /// A relationship pattern's start after a node pattern's `)`: the `-`
    /// of `-[…]`/`--` or the `<-` of an incoming edge — the token pair
    /// that tells a pattern (a predicate, a comprehension) from a
    /// parenthesized expression. Consulted by the node-pattern lookahead
    /// gate and the schema reader's FOR-target discrimination; the
    /// pattern-body element loops spell the pair inside their wider
    /// element-alternations, where a standalone predicate does not fit.
    ///
    /// Three consumer classes, one deliberate split:
    ///
    /// - the expression-context gates (the pattern-predicate and
    ///   comprehension lookaheads) consult this fused pair only: there a
    ///   `<` after a `)` keeps its comparison meaning (`RETURN (a) < -1`
    ///   is a comparison against a negated operand), so the spaced left
    ///   head is never claimed in an expression;
    /// - the schema DDL FOR-target lookaheads are deliberately fused-only
    ///   too — a deferred acceptance, not an expression context: no
    ///   verified behavior exists yet for a spaced `< -` head in
    ///   `CREATE INDEX/CONSTRAINT … FOR () < - [r:R] - ()`, so they keep
    ///   rejecting it until one does;
    /// - the pattern-body element loops admit the full set through
    ///   [`Self::is_pattern_body_relationship_start`], the documented
    ///   superset.
    pub(crate) fn is_relationship_start(self) -> bool {
        matches!(self, Self::Dash | Self::ArrowLeft)
    }

    /// The pattern-body superset of [`Self::is_relationship_start`]:
    /// every token that opens a relationship element arm inside a
    /// pattern body -- the fused pair (derived, so the superset holds by
    /// construction), the spaced left head `< -` (a LessThan whose head
    /// dash follows), and the stranded `->` of the single-dash form,
    /// which the loop routes into the relationship head error instead of
    /// a generic clause error. Consulted by the pattern-body element
    /// loops and the unquantified-group juxtaposition rejection; never
    /// by an expression-context gate or a DDL lookahead (see
    /// [`Self::is_relationship_start`] for the split).
    pub(crate) fn is_pattern_body_relationship_start(self) -> bool {
        matches!(self, Self::LessThan | Self::ArrowRight) || self.is_relationship_start()
    }

    /// The tokens that continue a pattern body after an element: a new
    /// node or group's `(`, or any relationship head (derived). Consulted
    /// by the pattern-body element loop's continuation guard and the
    /// unquantified-group juxtaposition rejection, so "an element
    /// follows" has one home instead of two token lists.
    pub(crate) fn starts_pattern_element(self) -> bool {
        self == Self::LeftParen || self.is_pattern_body_relationship_start()
    }

    /// The tokens that can begin a CIP-28 quantifier (`*`, `+`, `{m,n}`):
    /// consulted by the two pattern-body guards that look ahead for one
    /// (the parenthesized-subpath group and the relationship pattern's
    /// postfix quantifier), and named by the quantifier reader's
    /// contract (`Parser::parse_quantifier`).
    pub(crate) fn is_quantifier_start(self) -> bool {
        matches!(self, Self::Star | Self::Plus | Self::LeftBrace)
    }

    /// The equality comparison's spelling — the one comparison with a
    /// build site that has no token to read the table from: simple-CASE's
    /// plain-value default (`parse_when_operand_element`). The table's
    /// own `Equals` row reads this constant, so the text has one owner.
    pub(crate) const EQUALS: &str = "=";

    /// The dialect's comparison operators and their ADR-0505 §2 spellings,
    /// ONE table at the lowest shared module (the token layer owns text
    /// policy — `quote_identifier`, `is_field_terminator_word`): the set
    /// and the spelling of every comparison variant live together. Its
    /// readers: `Parser::take_comparison_spelling` (the cursor's
    /// consuming read), the tokenless simple-CASE default
    /// ([`Self::EQUALS`]), and the token-fact pin, which iterates
    /// [`COMPARISON_SPELLINGS`].
    pub(crate) fn comparison_spelling(self) -> Option<&'static str> {
        COMPARISON_SPELLINGS
            .iter()
            .find(|(ty, _)| *ty == self)
            .map(|(_, spelling)| *spelling)
    }

    /// Returns true if this token type is a keyword (MATCH..PROFILE range).
    pub fn is_keyword(self) -> bool {
        matches!(
            self,
            TokenType::MATCH
                | TokenType::OPTIONAL
                | TokenType::CREATE
                | TokenType::INSERT
                | TokenType::MERGE
                | TokenType::DELETE
                | TokenType::DETACH
                | TokenType::SET
                | TokenType::REMOVE
                | TokenType::RETURN
                | TokenType::WITH
                | TokenType::UNWIND
                | TokenType::WHERE
                | TokenType::ORDER
                | TokenType::BY
                | TokenType::ASC
                | TokenType::DESC
                | TokenType::LIMIT
                | TokenType::SKIP
                | TokenType::AS
                | TokenType::DISTINCT
                | TokenType::AND
                | TokenType::OR
                | TokenType::NOT
                | TokenType::XOR
                | TokenType::IN
                | TokenType::STARTS
                | TokenType::ENDS
                | TokenType::CONTAINS
                | TokenType::IS
                | TokenType::NULL
                | TokenType::TRUE
                | TokenType::FALSE
                | TokenType::CASE
                | TokenType::WHEN
                | TokenType::THEN
                | TokenType::ELSE
                | TokenType::END
                | TokenType::FOREACH
                | TokenType::CALL
                | TokenType::YIELD
                | TokenType::UNION
                | TokenType::ALL
                | TokenType::ON
                | TokenType::INDEX
                | TokenType::CONSTRAINT
                | TokenType::FOR
                | TokenType::REQUIRE
                | TokenType::UNIQUE
                | TokenType::EXISTS
                | TokenType::IF
                | TokenType::DROP
                | TokenType::VECTOR
                | TokenType::POINT
                | TokenType::OPTIONS
                | TokenType::NODE
                | TokenType::REL
                | TokenType::COUNT
                | TokenType::COLLECT
                | TokenType::LOAD
                | TokenType::FROM
                | TokenType::CSV
                | TokenType::REDUCE
                | TokenType::SPATIAL
                | TokenType::EXPLAIN
                | TokenType::PROFILE
                | TokenType::INFINITY
                | TokenType::NAN
        )
    }

    /// Returns true if the token can be used as an identifier (variable,
    /// alias, map key, ...): `Identifier` or any keyword except the
    /// [`CLAUSE_KEYWORDS`].
    pub fn is_ident_like(self) -> bool {
        self.is_schema_name() && !CLAUSE_KEYWORDS.contains(&self)
    }

    /// Returns true if the token is `Identifier` or any keyword: openCypher's
    /// `SchemaName ::= SymbolicName | ReservedWord`, for positions where the
    /// grammar already requires a name, so no keyword there starts a clause
    /// -- property keys after `.`, the LET binding variable, and label and
    /// relationship-type names. ADR-0169: a strict superset of
    /// `is_ident_like()`, so `(o:Order)` and `-[:ORDER]->` parse unquoted
    /// with the name kept as written.
    ///
    /// This token class is unrelated to, and deliberately wider than, the
    /// narrower thing this crate also calls a schema name: the
    /// index/constraint name `ast::SchemaName` that
    /// `parse_optional_schema_name` / `parse_required_schema_name` read
    /// (the command-name readers' admissible set — see
    /// `Parser::parse_optional_schema_name` — plus a `$param`).
    pub fn is_schema_name(self) -> bool {
        self == TokenType::Identifier || self.is_keyword()
    }
}

/// The comparison family's variants and their ADR-0505 §2 spellings — the
/// one enumerable home of the set: [`TokenType::comparison_spelling`]
/// consults it, and the token-fact pin iterates it, so "every variant
/// with a spelling introduces an operand" is asserted over the table
/// itself, never over a hand-copied list.
pub(crate) const COMPARISON_SPELLINGS: [(TokenType, &str); 7] = [
    (TokenType::Equals, TokenType::EQUALS),
    (TokenType::NotEquals, "<>"),
    (TokenType::LessThan, "<"),
    (TokenType::GreaterThan, ">"),
    (TokenType::LessEqual, "<="),
    (TokenType::GreaterEqual, ">="),
    (TokenType::RegexMatch, "=~"),
];

/// The clause keywords: keyword tokens that start or continue a clause
/// (`MATCH`, `ORDER BY`, `AS`, `DISTINCT`, ...). `is_ident_like()` rejects
/// them as variables, aliases and map keys, so `(WHERE ...)` does not read
/// `WHERE` as the element variable. ADR-0169: they are still label and
/// relationship-type names (`is_schema_name()`).
///
/// `RETURN` is not one of them. Verified
/// against live Neo4j 2026.05 (both CYPHER 5 and CYPHER 25): `RETURN` is a
/// non-reserved word usable as an ordinary variable/alias name (e.g.
/// `WITH 1 AS RETURN RETURN RETURN AS r`) -- `is_ident_like()` is only
/// checked in positions the grammar already knows unambiguously expect an
/// identifier (after AS, as a pattern/FOREACH variable, ...), so it never
/// shadows RETURN's role starting a new clause.
pub const CLAUSE_KEYWORDS: [TokenType; 22] = [
    TokenType::MATCH,
    TokenType::OPTIONAL,
    TokenType::CREATE,
    TokenType::INSERT,
    TokenType::MERGE,
    TokenType::DELETE,
    TokenType::DETACH,
    TokenType::SET,
    TokenType::REMOVE,
    TokenType::WITH,
    TokenType::UNWIND,
    TokenType::WHERE,
    TokenType::ORDER,
    TokenType::BY,
    TokenType::LIMIT,
    TokenType::SKIP,
    TokenType::AS,
    TokenType::DISTINCT,
    TokenType::FOREACH,
    TokenType::UNION,
    TokenType::REQUIRE,
    TokenType::DROP,
];

/// The contextual `FIELDTERMINATOR` word of LOAD CSV: an identifier-spelled
/// keyword the clause parser discriminates on by spelling. The one
/// constant, shared with the statement normalizer's grammar guard (a
/// `String` after this word is a literal-only position there) — the
/// spelling is a centralized fact, never re-derived at a second site.
pub(crate) const FIELDTERMINATOR_WORD: &str = "FIELDTERMINATOR";

/// Whether an identifier's literal spells the LOAD CSV
/// [`FIELDTERMINATOR_WORD`] (case-insensitively, as the clause grammar
/// reads it).
pub(crate) fn is_field_terminator_word(literal: &str) -> bool {
    literal.eq_ignore_ascii_case(FIELDTERMINATOR_WORD)
}

/// The reserved-keyword clause-starter tokens: the keyword tokens that
/// start a new clause at clause position (`MATCH`, `CREATE`, `UNWIND`,
/// `FOR` (CIP-250), ...). The grammar authority is the clause dispatch
/// (`Parser::parse_clause_inner`); this table is the derived vocabulary
/// every *lookahead* over the same fact consults instead of re-spelling a
/// second hand-maintained list -- through [`is_clause_boundary`], by
/// `Parser::group_by_ends_clause` (whose round-5 comment records the
/// LOAD/DROP drift that motivated it) and the simple-CASE lookahead -- and
/// the dispatch-coverage pin test. A new clause-start dispatch arm
/// extends this one table. `OPTIONAL` is a member: it starts a clause
/// only immediately before `MATCH`/`CALL` (the arm that checks what
/// follows). The contextual (identifier-spelled) starters are the
/// separate [`CONTEXTUAL_CLAUSE_WORDS`] below.
pub const CLAUSE_STARTER_TOKENS: &[TokenType] = &[
    TokenType::MATCH,
    TokenType::OPTIONAL,
    TokenType::CREATE,
    TokenType::INSERT,
    TokenType::MERGE,
    TokenType::DELETE,
    TokenType::DETACH,
    TokenType::SET,
    TokenType::REMOVE,
    TokenType::UNWIND,
    // `FOR <variable> IN <expression>`.
    TokenType::FOR,
    TokenType::FOREACH,
    TokenType::CALL,
    TokenType::UNION,
    TokenType::LOAD,
    TokenType::DROP,
];

/// The one derived "this token ends the enclosing clause" predicate: a
/// reserved clause keyword ([`CLAUSE_KEYWORDS`]) or a reserved clause
/// starter ([`CLAUSE_STARTER_TOKENS`]). Every lookahead that must stop at a
/// clause boundary consults this instead of re-spelling the vocabulary —
/// `Parser::group_by_ends_clause` and the simple-CASE lookahead
/// (`ends_case_test_scan`), each adding only its own statement/item
/// terminals.
pub(crate) fn is_clause_boundary(ty: TokenType) -> bool {
    CLAUSE_KEYWORDS.contains(&ty) || CLAUSE_STARTER_TOKENS.contains(&ty)
}

/// The contextual (non-reserved, identifier-spelled) clause keywords:
/// words that start or continue a clause when they appear as a plain
/// `Identifier` at clause position, recognized by spelling -- the same
/// role as [`CLAUSE_KEYWORDS`], but these stay usable as ordinary
/// variable/label names everywhere else (`MATCH (show) RETURN show`).
/// Single-sourced here so the clause dispatcher
/// (`Parser::parse_clause_inner`) and the `SHOW SETTINGS`-style
/// filter-expression starter (`Parser::peek_starts_show_filter_expr`,
/// which excludes exactly this set so a following composed clause is not
/// swallowed as a filter expression) can never drift apart.
///
/// `NEXT` (CIP-213) and `CYPHER` (preamble) are members even though they
/// never start a clause inside `parse_clause_inner` -- they are
/// recognized at statement-block boundaries and in the pre-parser
/// preamble respectively. `START`/`STOP`/`NODETACH`/`OFFSET` are
/// deliberately *not* members: they only act clause-like before a
/// specific following word (`DATABASE`/`DELETE`/paging), so a bare
/// `SHOW SETTINGS start` still reads `start` as a name filter.
pub const CONTEXTUAL_CLAUSE_WORDS: [&str; 9] = [
    "alter",
    "cypher",
    "filter",
    "finish",
    "let",
    "next",
    "show",
    "terminate",
    "use",
];

/// True if `literal` (any case) spells one of the
/// [`CONTEXTUAL_CLAUSE_WORDS`].
pub fn is_contextual_clause_keyword(literal: &str) -> bool {
    CONTEXTUAL_CLAUSE_WORDS
        .iter()
        .any(|w| literal.eq_ignore_ascii_case(w))
}

/// A lexical token with its type, literal value, and source position.
///
/// The literal text borrows from the lexer's input (`'a`): identifiers,
/// keywords, numbers, parameters and escape-free string literals are slices
/// of the query text, and punctuation/operators and fixed error texts are
/// `&'static str` (which coerces to `'a`). Only synthesized text is owned —
/// escape-decoded string literals and `format!`-built error messages — so
/// the token stream costs no heap allocation per token beyond the
/// `Vec<Token>` itself (ADR-0505). Tokens never outlive the input: the only
/// consumer is the [`crate::parser::Parser`], built and dropped inside the
/// parse entry points.
#[derive(Debug, Clone, PartialEq)]
pub struct Token<'a> {
    pub ty: TokenType,
    pub literal: Cow<'a, str>,
    pub line: usize,
    pub col: usize,
    /// Optional symbolic openCypher-TCK error-*detail* name carried by a
    /// [`TokenType::Error`] token (e.g. `"InvalidNumberLiteral"`,
    /// `"InvalidUnicodeLiteral"`). The parser propagates it onto the
    /// [`crate::ParseError`] it raises when it surfaces the error token, so a
    /// malformed number/unicode literal reports its specific TCK detail rather
    /// than the generic `UnexpectedSyntax`. `None` for every non-error token.
    pub detail: Option<&'static str>,
}

impl<'a> Token<'a> {
    /// The `$(expr)` dynamic-opener sentinel: a Parameter token with the
    /// empty literal — the reserved encoding the lexer emits for the `(`
    /// glued to the `$`, the parser expands
    /// (`dynamic_label_quantifier_at`'s Default arm) and the normalizer
    /// declines to render (the empty literal is an encoding, never a
    /// name — see `cache_insert_eligible` for the separate
    /// dynamic-operand uncacheability rule). The one home of the
    /// encoding: no call site re-tests the raw literal. `pub(crate)`: both
    /// consumers are in-crate (the normalizer, the parser's quantifier) —
    /// widen only when a crate outside `relate-parser` must decode the
    /// sentinel.
    pub(crate) fn is_dynamic_opener_sentinel(&self) -> bool {
        matches!(self.ty, TokenType::Parameter) && self.literal.is_empty()
    }

    /// Build a token whose text is borrowed (`&'a str` / `&'static str`) or
    /// owned (`String`), per the ADR-0505 literal rule — `Into<Cow>` accepts
    /// all three without a hidden copy.
    pub fn new(ty: TokenType, literal: impl Into<Cow<'a, str>>, line: usize, col: usize) -> Self {
        Token {
            ty,
            literal: literal.into(),
            line,
            col,
            detail: None,
        }
    }

    /// Build a [`TokenType::Error`] token tagged with a TCK error-detail name
    /// (see [`Token::detail`]).
    pub fn error_with_detail(
        literal: impl Into<Cow<'a, str>>,
        line: usize,
        col: usize,
        detail: &'static str,
    ) -> Self {
        Token {
            ty: TokenType::Error,
            literal: literal.into(),
            line,
            col,
            detail: Some(detail),
        }
    }
}

/// The one declaration of the keyword vocabulary [`lookup_keyword`]
/// accepts: each `spelling => variant` pair (aliases included) generates
/// the stack-buffer match cascade, the derived [`LONGEST_KEYWORD_BYTES`]
/// bound, the test's spelling table and the [`keyword_spellings`]
/// iterator over it, so the cascade, the bound, the table and the
/// iterator can never drift apart — a spelling added here is classified,
/// bounded and exercised in the same change, and a spelling forgotten
/// cannot exist (there is no second list to forget it from).
macro_rules! declare_keywords {
    ($( $spelling:literal => $variant:expr ),+ $(,)?) => {
        /// The longest keyword spelling accepted by [`lookup_keyword`] in
        /// bytes (`CONSTRAINT`, `DESCENDING`) — derived from the one
        /// vocabulary declaration below, never re-spelled at the
        /// short-circuit or the stack buffer size, so no arm can fall
        /// outside the bound.
        const LONGEST_KEYWORD_BYTES: usize = {
            let lengths = [$($spelling.len()),+];
            let mut longest = 0;
            let mut i = 0;
            while i < lengths.len() {
                if lengths[i] > longest {
                    longest = lengths[i];
                }
                i += 1;
            }
            longest
        };

        /// Look up a keyword from an identifier string (case-insensitive
        /// uppercase).
        ///
        /// No heap copy (ADR-0505): identifiers longer than
        /// [`LONGEST_KEYWORD_BYTES`] short-circuit to `Identifier`; shorter
        /// ones are ASCII-uppercased into a fixed stack buffer and matched
        /// against byte literals. Non-ASCII bytes pass through untouched
        /// (only `make_ascii_uppercase` is applied), so a Unicode
        /// identifier (ADR-0182) never matches a keyword — the same
        /// behavior `to_ascii_uppercase` gave.
        pub fn lookup_keyword(ident: &str) -> TokenType {
            if ident.len() > LONGEST_KEYWORD_BYTES {
                return TokenType::Identifier;
            }
            let mut buf = [0u8; LONGEST_KEYWORD_BYTES];
            let bytes = ident.as_bytes();
            buf[..bytes.len()].copy_from_slice(bytes);
            buf[..bytes.len()].make_ascii_uppercase();
            match &buf[..bytes.len()] {
                $( $spelling => $variant, )+
                _ => TokenType::Identifier,
            }
        }

        /// Every `(spelling, variant)` pair of the vocabulary, aliases
        /// included — generated from the same declaration as the cascade,
        /// so the test below pins the cascade itself, not a hand-copied
        /// twin of it. The tests' one spelling iterator
        /// ([`keyword_spellings`], directly below) derives from this same
        /// single vocabulary home; the const itself stays private to the
        /// module, so every consumer goes through the iterator and no
        /// site decodes the byte-literal table inline.
        #[cfg(test)]
        const KEYWORD_SPELLINGS: &[(&[u8], TokenType)] = &[$(($spelling, $variant)),+];

        /// [`KEYWORD_SPELLINGS`] as `&'static str` spellings — the one
        /// decode of the byte-literal table, kept beside the table it
        /// iterates so no test site converts inline. The declaration's
        /// match patterns force byte-literal spellings, so every one is
        /// ASCII by construction and the conversion is infallible.
        /// `pub(crate)`: the token module's own tests and the parser's
        /// schema tests share it.
        #[cfg(test)]
        pub(crate) fn keyword_spellings() -> impl Iterator<Item = (&'static str, TokenType)> {
            KEYWORD_SPELLINGS.iter().map(|&(spelling, ty)| {
                (
                    std::str::from_utf8(spelling).expect("keyword spellings are ASCII"),
                    ty,
                )
            })
        }
    };
}

declare_keywords! {
    b"MATCH" => TokenType::MATCH,
    b"OPTIONAL" => TokenType::OPTIONAL,
    b"CREATE" => TokenType::CREATE,
    b"INSERT" => TokenType::INSERT,
    b"MERGE" => TokenType::MERGE,
    b"DELETE" => TokenType::DELETE,
    b"DETACH" => TokenType::DETACH,
    b"SET" => TokenType::SET,
    b"REMOVE" => TokenType::REMOVE,
    b"RETURN" => TokenType::RETURN,
    b"WITH" => TokenType::WITH,
    b"UNWIND" => TokenType::UNWIND,
    b"WHERE" => TokenType::WHERE,
    b"ORDER" => TokenType::ORDER,
    b"BY" => TokenType::BY,
    b"ASC" => TokenType::ASC,
    b"ASCENDING" => TokenType::ASC,
    b"DESC" => TokenType::DESC,
    b"DESCENDING" => TokenType::DESC,
    b"LIMIT" => TokenType::LIMIT,
    b"SKIP" => TokenType::SKIP,
    b"AS" => TokenType::AS,
    b"DISTINCT" => TokenType::DISTINCT,
    b"AND" => TokenType::AND,
    b"OR" => TokenType::OR,
    b"NOT" => TokenType::NOT,
    b"XOR" => TokenType::XOR,
    b"IN" => TokenType::IN,
    b"STARTS" => TokenType::STARTS,
    b"ENDS" => TokenType::ENDS,
    b"CONTAINS" => TokenType::CONTAINS,
    b"IS" => TokenType::IS,
    b"NULL" => TokenType::NULL,
    b"TRUE" => TokenType::TRUE,
    b"FALSE" => TokenType::FALSE,
    b"CASE" => TokenType::CASE,
    b"WHEN" => TokenType::WHEN,
    b"THEN" => TokenType::THEN,
    b"ELSE" => TokenType::ELSE,
    b"END" => TokenType::END,
    b"FOREACH" => TokenType::FOREACH,
    b"CALL" => TokenType::CALL,
    b"YIELD" => TokenType::YIELD,
    b"UNION" => TokenType::UNION,
    b"ALL" => TokenType::ALL,
    b"ON" => TokenType::ON,
    b"INDEX" => TokenType::INDEX,
    b"CONSTRAINT" => TokenType::CONSTRAINT,
    b"FOR" => TokenType::FOR,
    b"REQUIRE" => TokenType::REQUIRE,
    b"UNIQUE" => TokenType::UNIQUE,
    b"EXISTS" => TokenType::EXISTS,
    b"IF" => TokenType::IF,
    b"DROP" => TokenType::DROP,
    b"VECTOR" => TokenType::VECTOR,
    b"POINT" => TokenType::POINT,
    b"OPTIONS" => TokenType::OPTIONS,
    b"NODE" => TokenType::NODE,
    b"REL" => TokenType::REL,
    b"COUNT" => TokenType::COUNT,
    b"COLLECT" => TokenType::COLLECT,
    b"LOAD" => TokenType::LOAD,
    b"FROM" => TokenType::FROM,
    b"CSV" => TokenType::CSV,
    b"REDUCE" => TokenType::REDUCE,
    b"SPATIAL" => TokenType::SPATIAL,
    b"EXPLAIN" => TokenType::EXPLAIN,
    b"PROFILE" => TokenType::PROFILE,
    b"INFINITY" => TokenType::INFINITY,
    b"INF" => TokenType::INFINITY,
    b"NAN" => TokenType::NAN,
}

/// True when `name` must be backtick-quoted to round-trip as a Cypher name.
///
/// The DDL / canonical identifier-quoting policy: a name is bare only when it
/// has the plain identifier shape
/// ([`relate_model::identifier::has_plain_identifier_shape`]) **and**
/// [`lookup_keyword`] does not classify it as a keyword.
///
/// Keyword names need quotes even in positions that accept them bare (a
/// property key after `.`, a label or relationship type per ADR-0169) because
/// the same spelling is a keyword in the positions that call
/// [`TokenType::is_ident_like`] — a variable, alias, map key, or the
/// `ON (n.prop)` property name of `CREATE INDEX` — so the unquoted form
/// would not parse everywhere. Quoting every keyword keeps one rendered
/// statement runnable in every position instead of deciding per site.
pub fn identifier_needs_quotes(name: &str) -> bool {
    !relate_model::identifier::has_plain_identifier_shape(name) || lookup_keyword(name).is_keyword()
}

/// Renders `name` as a Cypher identifier for generated Cypher text: bare
/// when [`identifier_needs_quotes`] is false, otherwise through
/// [`relate_model::identifier::quote_identifier_always`] (the one
/// spelling of the quoting escape).
pub fn quote_identifier(name: &str) -> String {
    if identifier_needs_quotes(name) {
        relate_model::identifier::quote_identifier_always(name)
    } else {
        name.to_string()
    }
}

/// Byte offset of the first byte of each 1-based line; O(source) once.
/// The one spelling of the line table, shared by the parser's
/// `token_byte_offset` and the statement normalizer (ADR-0503).
pub(crate) fn line_starts_of(source: &str) -> Vec<usize> {
    let mut starts = vec![0];
    for (i, b) in source.bytes().enumerate() {
        if b == b'\n' {
            starts.push(i + 1);
        }
    }
    starts
}

/// The byte offset of `tok`'s first byte in its source, given that
/// source's [`line_starts_of`] table and its byte length: the lexer
/// counts columns in bytes (`Lexer::advance` does `col += 1` per byte),
/// so `line_start + col - 1` is exactly the token's byte offset — no
/// per-token source rescan, and no char/byte-column mismatch after
/// non-ASCII text. `None` when the position does not land inside the
/// source — the one spelling of that half of the contract, so no caller
/// re-checks it.
pub(crate) fn token_start_byte(
    tok: &Token<'_>,
    line_starts: &[usize],
    source_len: usize,
) -> Option<usize> {
    let line_idx = tok.line.checked_sub(1)?;
    let col_bytes = tok.col.checked_sub(1)?;
    let offset = line_starts.get(line_idx)? + col_bytes;
    (offset <= source_len).then_some(offset)
}

#[cfg(test)]
#[path = "token_tests.rs"]
mod tests;