graphforge-ast 0.5.1

GraphForge AST — syntax-faithful, span-rich parse tree
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
//! Syntax-faithful, span-rich AST for openCypher queries.
//!
//! Produced by `graphforge-cypher`; consumed by `graphforge-ir` (the binder). No other crate
//! should depend on this module directly.
//!
//! Every public type is `Clone + Debug + PartialEq + Serialize + Deserialize`
//! so the differential test harness can round-trip ASTs through JSON and
//! compare parser outputs.
//!
//! Struct fields carry `span: Span` to locate source positions — doc comments
//! on these repetitive span fields are omitted intentionally.
#![allow(missing_docs)]

use graphforge_core::Span;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// ---------------------------------------------------------------------------
// Top-level query
// ---------------------------------------------------------------------------

/// openCypher dialect version.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum DialectVersion {
    /// openCypher 9 (the baseline target for GraphForge).
    #[default]
    OpenCypher9,
}

/// A fully parsed Cypher query.
///
/// At the skeleton stage `clauses` may be empty; the real parser populates it.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AstQuery {
    /// Cypher dialect in use.
    pub dialect: DialectVersion,
    /// Top-level clauses in source order.
    pub clauses: Vec<AstClause>,
    /// Span covering the full query source.
    pub span: Span,
}

// ---------------------------------------------------------------------------
// Clause variants
// ---------------------------------------------------------------------------

/// A top-level clause in a Cypher query.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum AstClause {
    Match(MatchClause),
    OptionalMatch(MatchClause),
    Where(WhereClause),
    With(WithClause),
    Return(ReturnClause),
    Create(CreateClause),
    Merge(MergeClause),
    Set(SetClause),
    Remove(RemoveClause),
    Delete(DeleteClause),
    Unwind(UnwindClause),
    Call(CallClause),
    Union(UnionClause),
}

impl AstClause {
    /// Span of the clause keyword plus its body.
    #[must_use]
    pub fn span(&self) -> Span {
        match self {
            Self::Match(c) | Self::OptionalMatch(c) => c.span,
            Self::Where(c) => c.span,
            Self::With(c) => c.span,
            Self::Return(c) => c.span,
            Self::Create(c) => c.span,
            Self::Merge(c) => c.span,
            Self::Set(c) => c.span,
            Self::Remove(c) => c.span,
            Self::Delete(c) => c.span,
            Self::Unwind(c) => c.span,
            Self::Call(c) => c.span,
            Self::Union(c) => c.span,
        }
    }
}

// ---------------------------------------------------------------------------
// MATCH
// ---------------------------------------------------------------------------

/// A `MATCH` or `OPTIONAL MATCH` clause.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MatchClause {
    /// The path patterns being matched.
    pub patterns: Vec<PathPattern>,
    /// Optional inline `WHERE` predicate.
    pub where_clause: Option<WhereClause>,
    pub span: Span,
}

// ---------------------------------------------------------------------------
// WHERE
// ---------------------------------------------------------------------------

/// A `WHERE` clause (also used inline in MATCH/WITH).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WhereClause {
    pub predicate: Expr,
    pub span: Span,
}

// ---------------------------------------------------------------------------
// WITH
// ---------------------------------------------------------------------------

/// A `WITH` clause (pipeline stage with optional ORDER/SKIP/LIMIT/WHERE).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WithClause {
    pub distinct: bool,
    pub items: Vec<ReturnItem>,
    pub order_by: Option<OrderByClause>,
    pub skip: Option<Expr>,
    pub limit: Option<Expr>,
    pub where_clause: Option<WhereClause>,
    pub span: Span,
}

// ---------------------------------------------------------------------------
// RETURN
// ---------------------------------------------------------------------------

/// A `RETURN` clause.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ReturnClause {
    pub distinct: bool,
    pub items: Vec<ReturnItem>,
    pub order_by: Option<OrderByClause>,
    pub skip: Option<Expr>,
    pub limit: Option<Expr>,
    pub span: Span,
}

/// A single item in a `RETURN` or `WITH` projection.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ReturnItem {
    pub expr: Expr,
    /// Optional `AS alias` name.
    pub alias: Option<String>,
    /// The verbatim source text of `expr` (no `AS` part), captured by the parser
    /// for an un-aliased item. openCypher names an un-aliased projection column by
    /// the expression as written (`n.prop`, `count(*)`, `a.x IS NULL`); the binder
    /// uses this as the default `RETURN` column name when there is no `alias`.
    /// `None` for `RETURN *` and for items built outside the parser.
    #[serde(default)]
    pub display: Option<String>,
    pub span: Span,
}

// ---------------------------------------------------------------------------
// ORDER BY
// ---------------------------------------------------------------------------

/// An `ORDER BY` sub-clause carrying one or more sort keys.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OrderByClause {
    pub items: Vec<SortItem>,
    pub span: Span,
}

/// A single sort key.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SortItem {
    pub expr: Expr,
    pub order: SortOrder,
    pub span: Span,
}

/// Sort direction.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum SortOrder {
    #[default]
    Ascending,
    Descending,
}

// ---------------------------------------------------------------------------
// CREATE / MERGE
// ---------------------------------------------------------------------------

/// A `CREATE` clause.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CreateClause {
    pub patterns: Vec<PathPattern>,
    pub span: Span,
}

/// A `MERGE` clause (with optional ON CREATE / ON MATCH SET actions).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MergeClause {
    pub pattern: PathPattern,
    pub on_create: Vec<SetItem>,
    pub on_match: Vec<SetItem>,
    pub span: Span,
}

// ---------------------------------------------------------------------------
// SET / REMOVE / DELETE
// ---------------------------------------------------------------------------

/// A `SET` clause.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SetClause {
    pub items: Vec<SetItem>,
    pub span: Span,
}

/// A single item inside a SET clause.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum SetItem {
    /// `n.prop = expr`
    Property {
        target: PropertyAccess,
        value: Expr,
        span: Span,
    },
    /// `n += {map}` (property merge)
    PropertyMerge { var: String, map: Expr, span: Span },
    /// `n = {map}` (property replace)
    PropertyReplace { var: String, map: Expr, span: Span },
    /// `n:Label` (add label)
    Label {
        var: String,
        labels: Vec<String>,
        span: Span,
    },
}

/// A `REMOVE` clause.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RemoveClause {
    pub items: Vec<RemoveItem>,
    pub span: Span,
}

/// A single item inside a `REMOVE` clause.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum RemoveItem {
    /// `n.prop`
    Property(PropertyAccess, Span),
    /// `n:Label`
    Label {
        var: String,
        labels: Vec<String>,
        span: Span,
    },
}

/// A `DELETE` or `DETACH DELETE` clause.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DeleteClause {
    pub detach: bool,
    pub exprs: Vec<Expr>,
    pub span: Span,
}

// ---------------------------------------------------------------------------
// UNWIND
// ---------------------------------------------------------------------------

/// An `UNWIND` clause.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UnwindClause {
    pub expr: Expr,
    pub alias: String,
    pub span: Span,
}

// ---------------------------------------------------------------------------
// CALL
// ---------------------------------------------------------------------------

/// A `CALL procedure YIELD ...` clause.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CallClause {
    pub procedure: Vec<String>,
    pub args: Vec<Expr>,
    /// Whether the call included an explicit parenthesized argument list.
    #[serde(default)]
    pub args_explicit: bool,
    pub yield_items: Vec<ReturnItem>,
    pub span: Span,
}

// ---------------------------------------------------------------------------
// UNION
// ---------------------------------------------------------------------------

/// A `UNION` or `UNION ALL` clause joining two query halves.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UnionClause {
    pub all: bool,
    pub span: Span,
}

// ---------------------------------------------------------------------------
// Path patterns
// ---------------------------------------------------------------------------

/// A path pattern: one or more alternating node/relationship patterns.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PathPattern {
    /// Optional `variable = ...` path binding.
    pub var: Option<String>,
    /// Elements: must start and end with a `NodePattern`, with
    /// `RelPattern` in between.
    pub elements: Vec<PathElement>,
    pub span: Span,
}

/// One element in a path pattern.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum PathElement {
    Node(NodePattern),
    Rel(RelPattern),
}

/// A node pattern: `(var:Label {props})`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NodePattern {
    /// Optional variable binding.
    pub var: Option<String>,
    /// Label constraints.
    pub labels: Vec<String>,
    /// Property constraints (map literal).
    pub properties: Option<Expr>,
    pub span: Span,
}

/// A relationship pattern: `-[var:TYPE*min..max {props}]->`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RelPattern {
    pub var: Option<String>,
    /// Relationship type constraints.
    pub types: Vec<String>,
    pub direction: Direction,
    /// Minimum hops for variable-length patterns (`None` = exactly 1).
    pub min_hops: Option<u32>,
    /// Maximum hops (`None` = unbounded or exactly 1 if `min_hops` is also None).
    pub max_hops: Option<u32>,
    pub properties: Option<Expr>,
    pub span: Span,
}

/// Edge direction in a relationship pattern.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Direction {
    /// `-[r]->` — outgoing.
    Out,
    /// `<-[r]-` — incoming.
    In,
    /// `-[r]-` — undirected.
    Undirected,
}

// ---------------------------------------------------------------------------
// Expressions
// ---------------------------------------------------------------------------

/// A Cypher expression.
///
/// `Expr` is recursive: `Box<Expr>` is used wherever a child expression is
/// needed to keep the enum `Sized`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Expr {
    /// An integer, float, string, boolean, or null literal.
    Literal(Literal),
    /// A variable reference: `n`.
    Var(VarRef),
    /// A property access: `n.prop`.
    Property(PropertyAccess),
    /// A binary operation: `left op right`.
    BinaryOp(BinaryOp),
    /// A unary operation: `op expr` (e.g. `NOT x`, `-x`).
    UnaryOp(UnaryOp),
    /// A function call: `name(args)` or `name(DISTINCT arg)`.
    FunctionCall(FunctionCall),
    /// A list literal: `[1, 2, 3]`.
    List(ListLiteral),
    /// A map literal: `{key: expr}`.
    Map(MapLiteral),
    /// A Cypher parameter: `$name`.
    Param(ParamRef),
    /// A `CASE` expression.
    Case(CaseExpr),
    /// A list comprehension: `[x IN list WHERE pred | expr]`.
    ListComprehension(ListComprehension),
    /// A quantifier predicate: `all/any/none/single(x IN list WHERE pred)`.
    Quantifier(Quantifier),
    /// A pattern comprehension: `[(n)-[r]->(m) | r.weight]`.
    PatternComprehension(PatternComprehension),
    /// A pattern predicate: `(n)-[:KNOWS]->(m)`.
    PatternPredicate(PatternPredicate),
    /// A simple or full existential subquery: `exists { (n)-->(m) }` or
    /// `exists { MATCH (n)-->(m) RETURN true }`.
    ExistentialSubquery(ExistentialSubquery),
    /// A label predicate: `n:Person`.
    LabelPredicate(LabelPredicate),
    /// `IS NULL` / `IS NOT NULL`.
    IsNull {
        expr: Box<Expr>,
        negated: bool,
        span: Span,
    },
    /// `x IN list` / `x NOT IN list`.
    InList {
        expr: Box<Expr>,
        list: Box<Expr>,
        negated: bool,
        span: Span,
    },
    /// `x STARTS WITH y` / `x ENDS WITH y` / `x CONTAINS y`.
    StringOp {
        expr: Box<Expr>,
        op: StringOpKind,
        pattern: Box<Expr>,
        span: Span,
    },
    /// `x =~ pattern` — regular expression match.
    RegexMatch {
        expr: Box<Expr>,
        pattern: Box<Expr>,
        span: Span,
    },
    /// A subexpression wrapped in parentheses (preserved for span accuracy).
    Parenthesized { inner: Box<Expr>, span: Span },
}

impl Expr {
    /// Return the span of this expression.
    #[must_use]
    pub fn span(&self) -> Span {
        match self {
            Self::Literal(l) => l.span(),
            Self::Var(v) => v.span,
            Self::Property(p) => p.span,
            Self::BinaryOp(b) => b.span,
            Self::UnaryOp(u) => u.span,
            Self::FunctionCall(f) => f.span,
            Self::List(l) => l.span,
            Self::Map(m) => m.span,
            Self::Param(p) => p.span,
            Self::Case(c) => c.span,
            Self::ListComprehension(lc) => lc.span,
            Self::Quantifier(q) => q.span,
            Self::PatternComprehension(pc) => pc.span,
            Self::PatternPredicate(pp) => pp.span,
            Self::ExistentialSubquery(es) => es.span,
            Self::LabelPredicate(lp) => lp.span,
            Self::IsNull { span, .. }
            | Self::InList { span, .. }
            | Self::StringOp { span, .. }
            | Self::RegexMatch { span, .. }
            | Self::Parenthesized { span, .. } => *span,
        }
    }
}

// ---------------------------------------------------------------------------
// Literal
// ---------------------------------------------------------------------------

/// A scalar literal value: integer, float, string, boolean, or null.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Literal {
    Int(i64, Span),
    Float(f64, Span),
    Str(String, Span),
    Bool(bool, Span),
    Null(Span),
}

impl Literal {
    #[must_use]
    pub fn span(&self) -> Span {
        match self {
            Self::Int(_, s)
            | Self::Float(_, s)
            | Self::Str(_, s)
            | Self::Bool(_, s)
            | Self::Null(s) => *s,
        }
    }
}

// ---------------------------------------------------------------------------
// Leaf expression types
// ---------------------------------------------------------------------------

/// A variable reference: `n`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct VarRef {
    pub name: String,
    pub span: Span,
}

/// A property access: `n.prop`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PropertyAccess {
    pub object: Box<Expr>,
    pub key: String,
    pub span: Span,
}

/// A query parameter reference: `$name`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ParamRef {
    pub name: String,
    pub span: Span,
}

/// A label predicate: `n:Person` used as a boolean expression.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LabelPredicate {
    pub var: String,
    pub labels: Vec<String>,
    pub span: Span,
}

// ---------------------------------------------------------------------------
// Compound expression types
// ---------------------------------------------------------------------------

/// A binary infix expression: `left op right`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BinaryOp {
    pub op: BinaryOpKind,
    pub left: Box<Expr>,
    pub right: Box<Expr>,
    pub span: Span,
}

/// All binary operators in openCypher.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BinaryOpKind {
    // Comparison
    Eq,
    Neq,
    Lt,
    Lte,
    Gt,
    Gte,
    // Logical
    And,
    Or,
    Xor,
    // Arithmetic
    Add,
    Sub,
    Mul,
    Div,
    Mod,
    Pow,
    // String
    Concat,
}

/// A unary prefix expression: `NOT expr` or `-expr`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UnaryOp {
    pub op: UnaryOpKind,
    pub expr: Box<Expr>,
    pub span: Span,
}

/// The operator in a unary expression.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum UnaryOpKind {
    Not,
    Neg,
}

/// A function call: `name(args)`, `name(DISTINCT arg)`, or `name(*)`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FunctionCall {
    /// Namespace-qualified name, e.g. `["apoc", "coll", "sum"]`.
    pub name: Vec<String>,
    pub distinct: bool,
    /// `true` when called as `f(*)` — `args` is empty in this case.
    pub star: bool,
    pub args: Vec<Expr>,
    pub span: Span,
}

/// A list literal: `[1, 2, 3]`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ListLiteral {
    pub elements: Vec<Expr>,
    pub span: Span,
}

/// A map literal: `{key: expr, …}`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MapLiteral {
    pub entries: HashMap<String, Expr>,
    /// Exact source span for each parsed key token. Synthetic and legacy
    /// deserialized maps may omit entries here.
    #[serde(default)]
    pub key_spans: HashMap<String, Span>,
    pub span: Span,
}

// ---------------------------------------------------------------------------
// CASE expression
// ---------------------------------------------------------------------------

/// A `CASE` expression — simple (`CASE expr WHEN … THEN …`) or searched (`CASE WHEN … THEN …`).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CaseExpr {
    /// `CASE expr WHEN ...` — the subject is `Some`; simple `CASE WHEN` is `None`.
    pub subject: Option<Box<Expr>>,
    pub when_clauses: Vec<WhenClause>,
    pub else_expr: Option<Box<Expr>>,
    pub span: Span,
}

/// A single `WHEN condition THEN result` arm inside a `CASE` expression.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WhenClause {
    pub condition: Expr,
    pub result: Expr,
    pub span: Span,
}

// ---------------------------------------------------------------------------
// List / pattern comprehensions
// ---------------------------------------------------------------------------

/// A list comprehension: `[x IN list WHERE pred | projection]`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ListComprehension {
    pub var: String,
    pub list: Box<Expr>,
    pub filter: Option<Box<Expr>>,
    pub projection: Option<Box<Expr>>,
    pub span: Span,
}

/// Which quantifier a [`Quantifier`] predicate applies.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum QuantifierKind {
    /// `all(x IN list WHERE pred)` — true iff every element satisfies `pred`.
    All,
    /// `any(x IN list WHERE pred)` — true iff some element satisfies `pred`.
    Any,
    /// `none(x IN list WHERE pred)` — true iff no element satisfies `pred`.
    None,
    /// `single(x IN list WHERE pred)` — true iff exactly one element satisfies `pred`.
    Single,
}

/// A quantifier predicate: `all/any/none/single(var IN list WHERE pred)`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Quantifier {
    pub kind: QuantifierKind,
    pub var: String,
    pub list: Box<Expr>,
    pub predicate: Box<Expr>,
    pub span: Span,
}

/// A pattern comprehension: `[(n)-[r]->(m) WHERE pred | r.weight]`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PatternComprehension {
    pub var: Option<String>,
    pub pattern: PathPattern,
    pub filter: Option<Box<Expr>>,
    pub projection: Box<Expr>,
    pub span: Span,
}

/// A pattern predicate used as a boolean expression, e.g. `(n)-->()`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PatternPredicate {
    pub pattern: PathPattern,
    pub span: Span,
}

/// A simple or full existential subquery.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ExistentialSubquery {
    pub body: ExistentialSubqueryBody,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ExistentialSubqueryBody {
    Simple {
        pattern: PathPattern,
        filter: Option<Box<Expr>>,
    },
    Full(Box<AstQuery>),
}

// ---------------------------------------------------------------------------
// String operations
// ---------------------------------------------------------------------------

/// The operation in a `STARTS WITH` / `ENDS WITH` / `CONTAINS` string predicate.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum StringOpKind {
    StartsWith,
    EndsWith,
    Contains,
}