lora-ast 0.2.0

AST types for the Cypher query language used by LoraDB.
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
use smallvec::SmallVec;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Span {
    pub start: usize,
    pub end: usize,
}

impl Span {
    pub const fn new(start: usize, end: usize) -> Self {
        Self { start, end }
    }
}

#[derive(Debug, Clone)]
pub struct Document {
    pub statement: Statement,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum Statement {
    Query(Query),
}

#[derive(Debug, Clone)]
pub enum Query {
    Regular(RegularQuery),
    StandaloneCall(StandaloneCall),
}

#[derive(Debug, Clone)]
pub struct RegularQuery {
    pub head: SingleQuery,
    pub unions: Vec<UnionPart>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct UnionPart {
    pub all: bool,
    pub query: SingleQuery,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum SingleQuery {
    SinglePart(SinglePartQuery),
    MultiPart(MultiPartQuery),
}

#[derive(Debug, Clone)]
pub struct SinglePartQuery {
    pub reading_clauses: Vec<ReadingClause>,
    pub updating_clauses: Vec<UpdatingClause>,
    pub return_clause: Option<Return>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct MultiPartQuery {
    pub parts: Vec<QueryPart>,
    pub tail: Box<SinglePartQuery>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct QueryPart {
    pub reading_clauses: Vec<ReadingClause>,
    pub updating_clauses: Vec<UpdatingClause>,
    pub with_clause: With,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum ReadingClause {
    Match(Match),
    Unwind(Unwind),
    InQueryCall(InQueryCall),
}

#[derive(Debug, Clone)]
pub enum UpdatingClause {
    Create(Create),
    Merge(Merge),
    Delete(Delete),
    Set(Set),
    Remove(Remove),
}

#[derive(Debug, Clone)]
pub struct Match {
    pub optional: bool,
    pub pattern: Pattern,
    pub where_: Option<Expr>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct Unwind {
    pub expr: Expr,
    pub alias: Variable,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct Create {
    pub pattern: Pattern,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct Merge {
    pub pattern_part: PatternPart,
    pub actions: Vec<MergeAction>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct MergeAction {
    pub on_match: bool,
    pub set: Set,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct Delete {
    pub detach: bool,
    pub expressions: Vec<Expr>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct Set {
    pub items: Vec<SetItem>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum SetItem {
    SetProperty {
        target: Expr,
        value: Expr,
        span: Span,
    },
    SetVariable {
        variable: Variable,
        value: Expr,
        span: Span,
    },
    MutateVariable {
        variable: Variable,
        value: Expr,
        span: Span,
    },
    SetLabels {
        variable: Variable,
        labels: Vec<String>,
        span: Span,
    },
}

#[derive(Debug, Clone)]
pub struct Remove {
    pub items: Vec<RemoveItem>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum RemoveItem {
    Labels {
        variable: Variable,
        labels: Vec<String>,
        span: Span,
    },
    Property {
        expr: Expr,
        span: Span,
    },
}

#[derive(Debug, Clone)]
pub struct InQueryCall {
    pub procedure: ProcedureInvocation,
    pub yield_items: Vec<YieldItem>,
    pub where_: Option<Expr>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct StandaloneCall {
    pub procedure: ProcedureInvocationKind,
    pub yield_items: Vec<YieldItem>,
    pub yield_all: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum ProcedureInvocationKind {
    Explicit(ProcedureInvocation),
    Implicit(ProcedureName),
}

#[derive(Debug, Clone)]
pub struct ProcedureInvocation {
    pub name: ProcedureName,
    pub args: Vec<Expr>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct YieldItem {
    pub field: Option<String>,
    pub alias: Variable,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct With {
    pub body: ProjectionBody,
    pub where_: Option<Expr>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct Return {
    pub body: ProjectionBody,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct ProjectionBody {
    pub distinct: bool,
    pub items: Vec<ProjectionItem>,
    pub order: Vec<SortItem>,
    pub skip: Option<Expr>,
    pub limit: Option<Expr>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum ProjectionItem {
    Expr {
        expr: Expr,
        alias: Option<Variable>,
        span: Span,
    },
    Star {
        span: Span,
    },
}

#[derive(Debug, Clone)]
pub struct SortItem {
    pub expr: Expr,
    pub direction: SortDirection,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SortDirection {
    Asc,
    Desc,
}

#[derive(Debug, Clone)]
pub struct Pattern {
    pub parts: Vec<PatternPart>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct PatternPart {
    pub binding: Option<Variable>,
    pub element: PatternElement,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum PatternElement {
    NodeChain {
        head: NodePattern,
        chain: Vec<PatternElementChain>,
        span: Span,
    },
    Parenthesized(Box<PatternElement>, Span),
    ShortestPath {
        all: bool,
        element: Box<PatternElement>,
        span: Span,
    },
}

#[derive(Debug, Clone)]
pub struct PatternElementChain {
    pub relationship: RelationshipPattern,
    pub node: NodePattern,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct NodePattern {
    pub variable: Option<Variable>,
    /// Each inner `Vec` is a disjunctive group (OR).
    /// The outer `SmallVec` is conjunctive (AND across groups).
    /// `:A:B` → `[[A], [B]]`;  `:A|B` → `[[A, B]]`.
    pub labels: SmallVec<SmallVec<String, 2>, 2>,
    pub properties: Option<Expr>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct RelationshipPattern {
    pub direction: Direction,
    pub detail: Option<RelationshipDetail>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct RelationshipDetail {
    pub variable: Option<Variable>,
    pub types: SmallVec<String, 2>,
    pub range: Option<RangeLiteral>,
    pub properties: Option<Expr>,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
    Left,
    Right,
    Undirected,
}

#[derive(Debug, Clone)]
pub struct RangeLiteral {
    pub start: Option<u64>,
    pub end: Option<u64>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct Variable {
    pub name: String,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct ProcedureName {
    pub parts: Vec<String>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum Expr {
    Variable(Variable),
    Integer(i64, Span),
    Float(f64, Span),
    String(String, Span),
    Bool(bool, Span),
    Null(Span),
    Parameter(String, Span),
    List(Vec<Expr>, Span),
    Map(Vec<(String, Expr)>, Span),
    Property {
        expr: Box<Expr>,
        key: String,
        span: Span,
    },
    Binary {
        lhs: Box<Expr>,
        op: BinaryOp,
        rhs: Box<Expr>,
        span: Span,
    },
    Unary {
        op: UnaryOp,
        expr: Box<Expr>,
        span: Span,
    },
    FunctionCall {
        name: Vec<String>,
        distinct: bool,
        args: Vec<Expr>,
        span: Span,
    },
    Case {
        input: Option<Box<Expr>>,
        alternatives: Vec<(Expr, Expr)>,
        else_expr: Option<Box<Expr>>,
        span: Span,
    },
    ListPredicate {
        kind: ListPredicateKind,
        variable: Variable,
        list: Box<Expr>,
        predicate: Box<Expr>,
        span: Span,
    },
    ListComprehension {
        variable: Variable,
        list: Box<Expr>,
        filter: Option<Box<Expr>>,
        map_expr: Option<Box<Expr>>,
        span: Span,
    },
    Reduce {
        accumulator: Variable,
        init: Box<Expr>,
        variable: Variable,
        list: Box<Expr>,
        expr: Box<Expr>,
        span: Span,
    },
    MapProjection {
        base: Box<Expr>,
        selectors: Vec<MapProjectionSelector>,
        span: Span,
    },
    Index {
        expr: Box<Expr>,
        index: Box<Expr>,
        span: Span,
    },
    Slice {
        expr: Box<Expr>,
        from: Option<Box<Expr>>,
        to: Option<Box<Expr>>,
        span: Span,
    },
    ExistsSubquery {
        pattern: Pattern,
        where_: Option<Box<Expr>>,
        span: Span,
    },
    PatternComprehension {
        pattern: Box<PatternElement>,
        where_: Option<Box<Expr>>,
        map_expr: Box<Expr>,
        span: Span,
    },
}

#[derive(Debug, Clone)]
pub enum MapProjectionSelector {
    /// `.propertyName` — include a specific property
    Property(String),
    /// `.*` — include all properties
    AllProperties,
    /// `key: expr` — include a computed entry
    Literal(String, Expr),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ListPredicateKind {
    Any,
    All,
    None,
    Single,
}

impl Expr {
    pub fn span(&self) -> Span {
        match self {
            Expr::Variable(v) => v.span,
            Expr::Integer(_, s)
            | Expr::Float(_, s)
            | Expr::String(_, s)
            | Expr::Bool(_, s)
            | Expr::Null(s)
            | Expr::Parameter(_, s)
            | Expr::List(_, s)
            | Expr::Map(_, s)
            | Expr::Property { span: s, .. }
            | Expr::Binary { span: s, .. }
            | Expr::Unary { span: s, .. }
            | Expr::FunctionCall { span: s, .. }
            | Expr::Case { span: s, .. }
            | Expr::ListPredicate { span: s, .. }
            | Expr::ListComprehension { span: s, .. }
            | Expr::Reduce { span: s, .. }
            | Expr::MapProjection { span: s, .. }
            | Expr::Index { span: s, .. }
            | Expr::Slice { span: s, .. }
            | Expr::ExistsSubquery { span: s, .. }
            | Expr::PatternComprehension { span: s, .. } => *s,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOp {
    Or,
    Xor,
    And,
    Eq,
    Ne,
    Lt,
    Gt,
    Le,
    Ge,
    Add,
    Sub,
    Mul,
    Div,
    Mod,
    Pow,
    In,
    StartsWith,
    EndsWith,
    Contains,
    IsNull,
    IsNotNull,
    RegexMatch,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOp {
    Not,
    Pos,
    Neg,
}