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
// Allow unsafe code for the single `std::mem::transmute` in `SyntaxKind::from`
// (see `syntax.rs`). The transmute is bounded and sound: we only convert
// `u16` values that are known to match a valid `SyntaxKind` discriminant.
//! A Rust library for parsing Cypher queries into a typed AST.
//!
//! # Overview
//!
//! This crate exposes three levels of representation for a Cypher query
//! string:
//!
//! 1. **CST** – a lossless concrete syntax tree built on [rowan], available
//! via [`parse_cst`] and the [`cst`] module.
//! 2. **AST** – a typed, high-level abstract syntax tree, available via
//! [`parse`] and the [`ast`] module.
//! 3. **HIR** – a lowered, scope-resolved high-level intermediate
//! representation, available via `analyze` and the `hir` module
//! (requires the `hir` feature, enabled by default).
//!
//! # Quick start
//!
//! ```
//! use decypher::parse;
//!
//! let query = parse("MATCH (n:Person) RETURN n.name").unwrap();
//! println!("{} statement(s)", query.statements.len());
//! ```
//!
//! For multi-error recovery use [`parse_all`]:
//!
//! ```
//! use decypher::parse_all;
//!
//! let (query, diagnostics) = parse_all("RETURN;");
//! assert!(query.is_none());
//! assert!(!diagnostics.is_empty());
//! ```
//!
//! [rowan]: https://docs.rs/rowan
/// Typed CST/AST wrappers over the lossless rowan CST.
///
/// This module provides rust-analyzer-style typed newtypes (`SourceFile`,
/// `MatchClause`, `NodePattern`, `Expression`, …) that wrap the raw
/// `SyntaxNode`/`SyntaxToken` produced by the rowan parser. Each wrapper
/// exposes accessor methods for semantically meaningful children instead of
/// requiring raw `SyntaxKind` matches.
///
/// # Stability
///
/// This API is **unstable** and may change as the CST matures.
///
/// # Example
///
/// ```ignore
/// use decypher::cst::{parse, AstNode};
///
/// let result = parse("MATCH (n:Person) RETURN n.name");
/// let source = result.tree();
/// for stmt in source.statements() {
/// for clause in stmt.clauses() {
/// // …
/// }
/// }
/// ```
pub use crateQuery;
pub use crate;
pub use crateParse;
pub use crate;
use Arc;
/// Parse a Cypher query into a typed [`Query`] AST.
///
/// The input can be either a `&str` (which will be parsed into a CST on the
/// fly) or an already-parsed [`Parse`] CST (which is used as-is, skipping the
/// lexer/parser step).
///
/// Returns `Ok(Query)` on success. On the first parse error the function
/// returns `Err(CypherError)` with position information and diagnostic notes.
/// For collecting *all* errors in one pass, use [`parse_all`].
///
/// # Errors
///
/// Returns [`CypherError`] when the input is empty, syntactically invalid, or
/// contains unsupported grammar constructs.
///
/// # Example: from a string
///
/// ```
/// use decypher::parse;
///
/// let query = parse("MATCH (n:Person) RETURN n.name").unwrap();
/// assert_eq!(query.statements.len(), 1);
/// ```
///
/// # Example: from a pre-built CST
///
/// ```
/// let cst = decypher::parse_cst("MATCH (n:Person) RETURN n.name");
/// let query = decypher::parse(cst).unwrap();
/// assert_eq!(query.statements.len(), 1);
/// ```
/// Parse a Cypher query into a typed [`Query`] AST with an explicit source label
/// used in diagnostics.
///
/// The input can be either a `&str` (which will be parsed into a CST on the
/// fly) or an already-parsed [`Parse`] CST (which is used as-is, skipping the
/// lexer/parser step).
///
/// The `label` is stored in any [`CypherError`] produced, allowing consumers to
/// display the originating file or source name alongside error messages.
///
/// # Errors
///
/// Returns [`CypherError`] on any parse or AST-construction error.
///
/// # Example: from a string
///
/// ```
/// use decypher::parse_with_label;
///
/// let result = parse_with_label("RETURN 1", "my_script.cypher");
/// assert!(result.is_ok());
///
/// let result = parse_with_label("RETURN;", "my_script.cypher");
/// let err = result.unwrap_err();
/// assert_eq!(err.source_label(), Some("my_script.cypher"));
/// ```
///
/// # Example: from a pre-built CST
///
/// ```
/// let cst = decypher::parse_cst("RETURN 1");
/// let result = decypher::parse_with_label(cst, "my_script.cypher");
/// assert!(result.is_ok());
/// ```
/// Parse a Cypher query string in error-recovery mode, returning all
/// diagnostics discovered during parsing.
///
/// Unlike [`parse`], this function does not stop at the first error; it
/// attempts to resynchronise at statement boundaries and continue. The
/// returned `Option<Query>` is `Some` only when a statement was successfully
/// parsed *after* the last resynchronisation point. A valid statement that
/// appears before a later syntax error does not guarantee `Some`: the
/// implementation keeps only the successfully parsed suffix after recovery,
/// not any valid prefix.
///
/// # Example
///
/// ```
/// use decypher::parse_all;
///
/// let (query, diagnostics) = parse_all("RETURN;");
/// assert!(query.is_none());
/// assert!(!diagnostics.is_empty());
/// ```
/// Parse a Cypher query string into the lossless rowan CST.
///
/// This returns the raw [`Parse`] result containing the concrete syntax tree
/// and any parser diagnostics. For the typed AST, use [`parse`] instead.
///
/// # Example
///
/// ```
/// use decypher::parse_cst;
///
/// let cst = parse_cst("MATCH (n) RETURN n");
/// assert!(cst.errors.is_empty());
/// ```
/// Parse and lower a Cypher query into a [`hir::HirQuery`].
///
/// This is a convenience function that chains [`parse`] and
/// [`hir::lower::lower`]. It performs syntax parsing, AST construction, and
/// HIR lowering (scope resolution, graph pattern normalisation) in a single
/// call. Returns the first [`CypherError`] on failure.
///
/// The input can be either a `&str` (which will be parsed via [`parse`]) or an
/// already-parsed [`Query`] (which is used as-is, skipping the parse step).
///
/// # Errors
///
/// Returns the first error encountered during parsing or HIR lowering.
///
/// # Example: from a string
///
/// ```
/// let hir = decypher::analyze("MATCH (n:Person) RETURN n.name").unwrap();
/// assert!(!hir.parts.is_empty());
/// ```
///
/// # Example: from a previously parsed AST
///
/// ```
/// let query = decypher::parse("MATCH (n:Person) RETURN n.name").unwrap();
/// let hir = decypher::analyze(query).unwrap();
/// assert!(!hir.parts.is_empty());
/// ```
/// Parse and lower a Cypher query with a custom [`hir::LowerConfig`].
///
/// Identical to [`analyze`] except the caller supplies a [`hir::LowerConfig`]
/// to control lowering behaviour — for example, to register user-defined or
/// plugin aggregate functions via [`hir::AggregateRegistry`].
///
/// # Errors
///
/// Returns the first error encountered during parsing or HIR lowering.
///
/// # Example
///
/// ```
/// let mut config = decypher::hir::LowerConfig::default();
/// config.aggregates.register("apoc.agg.percentiles");
/// let hir = decypher::analyze_with_config(
/// "MATCH (n) WITH apoc.agg.percentiles(n.score) AS p RETURN p",
/// &config,
/// )
/// .unwrap();
/// assert!(!hir.parts.is_empty());
/// ```