open-cypher 0.2.0-alpha.1

An unofficial Rust parser for the openCypher query language.
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
//! Backend-neutral lexical tokens.
//!
//! Tokens contain only their kind and source span. The original spelling is
//! intentionally not copied: use [`Token::text`] with the source query when a
//! lossless representation is needed.

use std::fmt;

use crate::span::Span;

/// A token produced by the lexer.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Token {
    /// The lexical class of the token.
    pub kind: TokenKind,
    /// The token's half-open UTF-8 byte range.
    pub span: Span,
}

impl Token {
    /// Creates a token.
    #[must_use]
    pub const fn new(kind: TokenKind, span: Span) -> Self {
        Self { kind, span }
    }

    /// Returns the original token spelling, when `span` is valid for `source`.
    #[must_use]
    pub fn text(self, source: &str) -> Option<&str> {
        self.span.text(source)
    }

    /// Returns `true` for whitespace and comments.
    #[must_use]
    pub const fn is_trivia(self) -> bool {
        self.kind.is_trivia()
    }
}

/// A lexical token class understood by the openCypher parser.
///
/// Keyword recognition is case-insensitive. The parser reclassifies
/// non-reserved [`Keyword`] tokens contextually when a name is required.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TokenKind {
    /// A case-insensitive language keyword.
    Keyword(Keyword),
    /// An unescaped symbolic name.
    Identifier,
    /// A backtick-delimited symbolic name.
    EscapedIdentifier,
    /// A parameter whose separated name is unescaped, such as `$name`,
    /// `$123`, or `$1_name`.
    Parameter,
    /// A base-ten integer literal.
    Integer,
    /// A hexadecimal integer literal.
    HexInteger,
    /// An octal integer literal.
    OctalInteger,
    /// A decimal floating-point literal, optionally with an exponent.
    Float,
    /// A single- or double-quoted string literal.
    String,

    LeftParen,
    RightParen,
    LeftBracket,
    RightBracket,
    LeftBrace,
    RightBrace,
    Comma,
    Dot,
    DotDot,
    Colon,
    DoubleColon,
    Semicolon,
    Pipe,
    DoublePipe,
    Ampersand,
    Question,
    /// A bare `$`; the parser combines it with a following backtick-delimited
    /// identifier to form an escaped parameter name.
    Dollar,

    Plus,
    Minus,
    Star,
    Slash,
    Percent,
    Caret,
    Bang,
    Equal,
    NotEqual,
    Less,
    LessEqual,
    Greater,
    GreaterEqual,
    PlusEqual,
    FatArrow,
    RegexMatch,
    /// A contiguous `<-`; spaced or comment-separated forms remain separate
    /// [`Less`](Self::Less) and [`Minus`](Self::Minus) tokens.
    LeftArrow,
    /// A contiguous `->`; spaced or comment-separated forms remain separate
    /// [`Minus`](Self::Minus) and [`Greater`](Self::Greater) tokens.
    RightArrow,

    /// One or more Unicode whitespace characters.
    Whitespace,
    /// A `//` comment, including its marker but excluding a line terminator.
    LineComment,
    /// A `/* ... */` comment, including its delimiters.
    BlockComment,
    /// A source region which cannot begin any valid token.
    Invalid,
}

impl TokenKind {
    /// Returns `true` for whitespace and comments.
    #[must_use]
    pub const fn is_trivia(self) -> bool {
        matches!(
            self,
            Self::Whitespace | Self::LineComment | Self::BlockComment
        )
    }

    /// Returns `true` for literal tokens.
    #[must_use]
    pub const fn is_literal(self) -> bool {
        matches!(
            self,
            Self::Integer
                | Self::HexInteger
                | Self::OctalInteger
                | Self::Float
                | Self::String
                | Self::Keyword(Keyword::True | Keyword::False | Keyword::Null)
        )
    }

    /// Returns a concise, user-facing name suitable for diagnostics.
    #[must_use]
    pub const fn display_name(self) -> &'static str {
        match self {
            Self::Keyword(keyword) => keyword.as_str(),
            Self::Identifier => "an identifier",
            Self::EscapedIdentifier => "an escaped identifier",
            Self::Parameter => "a parameter",
            Self::Integer => "an integer",
            Self::HexInteger => "a hexadecimal integer",
            Self::OctalInteger => "an octal integer",
            Self::Float => "a floating-point number",
            Self::String => "a string",
            Self::LeftParen => "`(`",
            Self::RightParen => "`)`",
            Self::LeftBracket => "`[`",
            Self::RightBracket => "`]`",
            Self::LeftBrace => "`{`",
            Self::RightBrace => "`}`",
            Self::Comma => "`,`",
            Self::Dot => "`.`",
            Self::DotDot => "`..`",
            Self::Colon => "`:`",
            Self::DoubleColon => "`::`",
            Self::Semicolon => "`;`",
            Self::Pipe => "`|`",
            Self::DoublePipe => "`||`",
            Self::Ampersand => "`&`",
            Self::Question => "`?`",
            Self::Dollar => "`$`",
            Self::Plus => "`+`",
            Self::Minus => "`-`",
            Self::Star => "`*`",
            Self::Slash => "`/`",
            Self::Percent => "`%`",
            Self::Caret => "`^`",
            Self::Bang => "`!`",
            Self::Equal => "`=`",
            Self::NotEqual => "`<>` or `!=`",
            Self::Less => "`<`",
            Self::LessEqual => "`<=`",
            Self::Greater => "`>`",
            Self::GreaterEqual => "`>=`",
            Self::PlusEqual => "`+=`",
            Self::FatArrow => "`=>`",
            Self::RegexMatch => "`=~`",
            Self::LeftArrow => "`<-`",
            Self::RightArrow => "`->`",
            Self::Whitespace => "whitespace",
            Self::LineComment => "a line comment",
            Self::BlockComment => "a block comment",
            Self::Invalid => "an invalid token",
        }
    }
}

impl fmt::Display for TokenKind {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.display_name())
    }
}

/// A word recognized specially by this crate's lexer.
///
/// This contains the openCypher 2024.3 keyword spellings plus the documented
/// path-mode and subquery-expression extensions consumed by the grammar. The
/// parser decides whether a keyword can act as a symbolic name at a particular
/// location.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Keyword {
    Acyclic,
    All,
    AllShortestPaths,
    And,
    Any,
    As,
    Asc,
    Ascending,
    By,
    Call,
    Case,
    Collect,
    Contains,
    Count,
    Create,
    Delete,
    Desc,
    Descending,
    Detach,
    Distinct,
    Else,
    End,
    Ends,
    Exists,
    False,
    Group,
    Groups,
    In,
    Inf,
    Infinity,
    Is,
    Limit,
    Match,
    Merge,
    Nan,
    None,
    Not,
    Null,
    Offset,
    On,
    Optional,
    Or,
    Order,
    Path,
    Paths,
    Reduce,
    Remove,
    Return,
    Set,
    Shortest,
    ShortestPath,
    Simple,
    Single,
    Skip,
    Starts,
    Then,
    Trail,
    Trim,
    True,
    Union,
    Unwind,
    Walk,
    When,
    Where,
    With,
    Xor,
    Yield,
}

impl Keyword {
    /// Returns the canonical uppercase spelling.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Acyclic => "ACYCLIC",
            Self::All => "ALL",
            Self::AllShortestPaths => "ALLSHORTESTPATHS",
            Self::And => "AND",
            Self::Any => "ANY",
            Self::As => "AS",
            Self::Asc => "ASC",
            Self::Ascending => "ASCENDING",
            Self::By => "BY",
            Self::Call => "CALL",
            Self::Case => "CASE",
            Self::Collect => "COLLECT",
            Self::Contains => "CONTAINS",
            Self::Count => "COUNT",
            Self::Create => "CREATE",
            Self::Delete => "DELETE",
            Self::Desc => "DESC",
            Self::Descending => "DESCENDING",
            Self::Detach => "DETACH",
            Self::Distinct => "DISTINCT",
            Self::Else => "ELSE",
            Self::End => "END",
            Self::Ends => "ENDS",
            Self::Exists => "EXISTS",
            Self::False => "FALSE",
            Self::Group => "GROUP",
            Self::Groups => "GROUPS",
            Self::In => "IN",
            Self::Inf => "INF",
            Self::Infinity => "INFINITY",
            Self::Is => "IS",
            Self::Limit => "LIMIT",
            Self::Match => "MATCH",
            Self::Merge => "MERGE",
            Self::Nan => "NAN",
            Self::None => "NONE",
            Self::Not => "NOT",
            Self::Null => "NULL",
            Self::Offset => "OFFSET",
            Self::On => "ON",
            Self::Optional => "OPTIONAL",
            Self::Or => "OR",
            Self::Order => "ORDER",
            Self::Path => "PATH",
            Self::Paths => "PATHS",
            Self::Reduce => "REDUCE",
            Self::Remove => "REMOVE",
            Self::Return => "RETURN",
            Self::Set => "SET",
            Self::Shortest => "SHORTEST",
            Self::ShortestPath => "SHORTESTPATH",
            Self::Simple => "SIMPLE",
            Self::Single => "SINGLE",
            Self::Skip => "SKIP",
            Self::Starts => "STARTS",
            Self::Then => "THEN",
            Self::Trail => "TRAIL",
            Self::Trim => "TRIM",
            Self::True => "TRUE",
            Self::Union => "UNION",
            Self::Unwind => "UNWIND",
            Self::Walk => "WALK",
            Self::When => "WHEN",
            Self::Where => "WHERE",
            Self::With => "WITH",
            Self::Xor => "XOR",
            Self::Yield => "YIELD",
        }
    }

    /// Recognizes an ASCII keyword without regard to case.
    ///
    /// Non-ASCII input is never a keyword.
    #[must_use]
    pub fn from_ascii_case_insensitive(text: &str) -> Option<Self> {
        if !text.is_ascii() {
            return None;
        }

        let candidates: &[Keyword] = match text.len() {
            2 => &[Self::As, Self::By, Self::In, Self::Is, Self::On, Self::Or],
            3 => &[
                Self::All,
                Self::And,
                Self::Any,
                Self::Asc,
                Self::End,
                Self::Inf,
                Self::Nan,
                Self::Not,
                Self::Set,
                Self::Xor,
            ],
            4 => &[
                Self::Call,
                Self::Case,
                Self::Desc,
                Self::Else,
                Self::Ends,
                Self::None,
                Self::Null,
                Self::Path,
                Self::Skip,
                Self::Then,
                Self::Trim,
                Self::True,
                Self::Walk,
                Self::When,
                Self::With,
            ],
            5 => &[
                Self::Count,
                Self::False,
                Self::Group,
                Self::Limit,
                Self::Match,
                Self::Merge,
                Self::Order,
                Self::Paths,
                Self::Trail,
                Self::Union,
                Self::Where,
                Self::Yield,
            ],
            6 => &[
                Self::Create,
                Self::Delete,
                Self::Detach,
                Self::Exists,
                Self::Groups,
                Self::Offset,
                Self::Reduce,
                Self::Remove,
                Self::Return,
                Self::Simple,
                Self::Single,
                Self::Starts,
                Self::Unwind,
            ],
            7 => &[Self::Acyclic, Self::Collect],
            8 => &[
                Self::Contains,
                Self::Distinct,
                Self::Optional,
                Self::Shortest,
                Self::Infinity,
            ],
            9 => &[Self::Ascending],
            10 => &[Self::Descending],
            12 => &[Self::ShortestPath],
            16 => &[Self::AllShortestPaths],
            _ => return None,
        };

        candidates
            .iter()
            .copied()
            .find(|keyword| text.eq_ignore_ascii_case(keyword.as_str()))
    }
}

impl fmt::Display for Keyword {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}