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
use super::*;
impl<'a> Parser<'a> {
pub(in crate::parser) fn parse_set(&mut self) -> ParseResult<Clause> {
self.next_token(); // consume SET
let items = self.parse_set_items()?;
Ok(Clause::Set(SetClause { items }))
}
pub(in crate::parser) fn parse_set_items(&mut self) -> ParseResult<Vec<SetItem>> {
let mut items = Vec::new();
loop {
items.push(self.parse_set_item()?);
if self.peek_type() != TokenType::Comma {
break;
}
self.next_token();
}
Ok(items)
}
/// Parses zero or more trailing SET/REMOVE-target accessors after a base
/// expression: `.prop` (static) or `[<expr>]` (CIP-131 dynamic). Used by
/// both `parse_set_item` and `parse_remove_item` so `n.a.b`, `n[a][b]`,
/// `n.a[b]`, `(expr)[a].b`, etc. all parse uniformly -- all but the last
/// accessor navigate to the entity ultimately mutated (see
/// `build_property_access`).
pub(in crate::parser) fn parse_property_target_accessors(
&mut self,
) -> ParseResult<Vec<PropertyTargetAccessor>> {
let mut accessors = Vec::new();
loop {
match self.peek_type() {
TokenType::Dot => {
self.next_token(); // consume .
let prop = self.parse_schema_name("property name")?;
accessors.push(PropertyTargetAccessor::Static(prop));
}
TokenType::LeftBracket => {
self.next_token(); // consume [
let key_expr = self.parse_expression()?;
self.expect(TokenType::RightBracket)?;
accessors.push(PropertyTargetAccessor::Dynamic(key_expr));
}
_ => break,
}
}
Ok(accessors)
}
/// Folds a base expression and a non-empty accessor chain into the
/// `PropertyAccess` a SET/REMOVE property item targets: all but the last
/// accessor become part of the entity-navigation expression (nested
/// `Expr::Property`/`Expr::Subscript`), and the last accessor becomes the
/// actual static or dynamic (CIP-131) property target.
pub(in crate::parser) fn build_property_access(
base: Expr,
mut accessors: Vec<PropertyTargetAccessor>,
) -> PropertyAccess {
let last = accessors.pop().expect("accessors must be non-empty");
let mut entity_expr = base;
for accessor in accessors {
entity_expr = match accessor {
PropertyTargetAccessor::Static(name) => Expr::Property(Box::new(PropertyAccess {
expr: entity_expr,
property: name,
key_expr: None,
})),
PropertyTargetAccessor::Dynamic(key_expr) => {
Expr::Subscript(Box::new(SubscriptExpr {
expr: entity_expr,
index: Some(key_expr),
from: None,
to: None,
slice: false,
}))
}
};
}
match last {
PropertyTargetAccessor::Static(name) => PropertyAccess {
expr: entity_expr,
property: name,
key_expr: None,
},
PropertyTargetAccessor::Dynamic(key_expr) => PropertyAccess {
expr: entity_expr,
property: SmallName::from_name(""),
key_expr: Some(key_expr),
},
}
}
/// Whether the current token starts a CIP-131 general property target —
/// the base forms [`Self::parse_general_property_target`] accepts: a
/// call/identifier followed by `(` (`coalesce(n, m)[c]`), a `[`-rooted
/// comprehension target, a parameter (`$node['k']`), and the literal
/// bases `null`/`true`/`false`/`CASE` (`REMOVE null.p`,
/// `SET CASE … END.age`). SET and REMOVE read the same production, so
/// they must classify its start with this one predicate; two token lists
/// had made `REMOVE null.p` parse while `SET null.p = 1` fell through to
/// a variable read.
pub(in crate::parser) fn starts_general_property_target(&self) -> bool {
(self.peek_type() == TokenType::Identifier && self.peek_at(1).ty == TokenType::LeftParen)
|| matches!(
self.peek_type(),
TokenType::LeftBracket
| TokenType::Parameter
| TokenType::NULL
| TokenType::TRUE
| TokenType::FALSE
| TokenType::CASE
)
}
/// Parses a SET/REMOVE property target whose base is neither the plain
/// `<variable>` form nor a parenthesized `(<expr>)` form -- e.g.
/// `coalesce(n, m)[c]` (CIP-131 basic-syntax.feature [14]: an
/// unparenthesized complex target is valid, unlike the doc's implication
/// that parens are required -- see that CIP's NOTES.md deviation #1).
/// Reuses the general postfix expression grammar (`parse_postfix`, the
/// same one that parses `n.prop` / `n[expr]` on the read side, including
/// chained accessors) so the whole target parses identically to the read
/// path, then peels off the outermost accessor as the actual target
/// (mirroring `build_property_access`'s last-accessor rule). Note
/// `parse_postfix`'s `:` (HasLabel) handling only fires when its operand
/// is *currently* a bare `Expr::Variable`, so it never fires here for a
/// function-call/complex base -- no conflict with SET/REMOVE's own
/// `n:Label` syntax, which is parsed by the caller before falling back
/// to this method.
pub(in crate::parser) fn parse_general_property_target(
&mut self,
) -> ParseResult<PropertyAccess> {
let expr = self.parse_postfix()?;
match expr {
Expr::Property(pa) => Ok(*pa),
Expr::Subscript(se) if !se.slice && se.index.is_some() => Ok(PropertyAccess {
expr: se.expr,
property: SmallName::from_name(""),
key_expr: se.index,
}),
_ => Err(self.error(
"expected a '.' or '[' property target after the SET/REMOVE target expression",
)),
}
}
pub(in crate::parser) fn parse_set_item(&mut self) -> ParseResult<SetItem> {
// Handle parenthesized expression: SET (n).prop = value / SET (n)[c] = value
if self.peek_type() == TokenType::LeftParen {
self.next_token(); // consume (
let inner_expr = self.parse_expression()?;
self.expect(TokenType::RightParen)?;
let accessors = self.parse_property_target_accessors()?;
if accessors.is_empty() {
let next = self.peek().clone();
return Err(self.error_at_token(
&next,
format!(
"expected '.' or '[' after parenthesized expression in SET, got {:?}",
next.literal
),
));
}
let prop_access = Self::build_property_access(inner_expr, accessors);
self.expect(TokenType::Equals)?;
let val = self.parse_expression()?;
return Ok(SetItem::property_assignment(prop_access, val));
}
// CIP-162 Alternatives: `SET labels(<expr>) = / += / -= <expr>`
// (deferred by the CIP itself but implemented here as its stated
// target semantics -- see `SetItem::labels_function`'s doc
// comment). Must be tried before the CIP-131 general complex-target
// branch below, since `labels(...)` also matches
// `Identifier LeftParen` -- backtracks to that branch if the tail
// after `labels(<expr>)` isn't actually `=`/`+=`/`-=` (e.g.
// `labels(n)[0] = ...`, an ordinary dynamic-property-key target).
if self.peek_is_ident("labels") && self.peek_at(1).ty == TokenType::LeftParen {
let saved = self.save_pos();
self.next_token(); // consume 'labels'
self.next_token(); // consume (
let target = self.parse_expression()?;
let op = if self.expect(TokenType::RightParen).is_ok() {
match self.peek_type() {
TokenType::Equals => {
self.next_token();
Some(LabelsFunctionOp::Overwrite)
}
TokenType::Plus if self.peek_at(1).ty == TokenType::Equals => {
self.next_token();
self.next_token();
Some(LabelsFunctionOp::Extend)
}
TokenType::Dash if self.peek_at(1).ty == TokenType::Equals => {
self.next_token();
self.next_token();
Some(LabelsFunctionOp::Remove)
}
_ => None,
}
} else {
None
};
match op {
Some(op) => {
let val = self.parse_expression()?;
return Ok(SetItem::labels_function(target, op, val));
}
None => self.restore_pos(saved),
}
}
// CIP-131: an unparenthesized complex target expression, e.g.
// `SET coalesce(n, m)[c] = ...` -- the base isn't a plain variable
// (it's a function call here), so it can't be the `n:Label` /
// `n = ...` / `n += ...` / `n.prop = ...` / `n[...] = ...` forms
// below, all of which start with a bare identifier NOT immediately
// followed by `(`. A target may also start with `[`: the list
// comprehension/static-property form `SET [x IN ns WHERE x.p = 1][0].p = -1`
// (TCK Set7 [8]), which is likewise no plain variable. The literal
// bases are the same production REMOVE reads (`SET null.p = 1`),
// classified by the one predicate.
if self.starts_general_property_target() {
let prop_access = self.parse_general_property_target()?;
self.expect(TokenType::Equals)?;
let val = self.parse_expression()?;
return Ok(SetItem::property_assignment(prop_access, val));
}
// ADR-0191 (Decision point 7's follow-up probe): Neo4j 2026.09
// accepts every keyword spelling as a SET target variable
// (`SET MATCH.prop = 1`, `SET MATCH:Label`, `SET MATCH += {...}`),
// so this reads through the shared symbolic-name read like every
// other variable slot.
let name = self.parse_schema_name("identifier in SET")?;
// CIP-131: `SET n.prop = ...` / `SET n[<expr>] = ...`, including
// chained accessors (`n[a][b]`, `n[a].b`, ...) where all but the
// last accessor navigate to the entity being mutated.
let accessors = self.parse_property_target_accessors()?;
if !accessors.is_empty() {
let prop_access = Self::build_property_access(Expr::Variable(name), accessors);
self.expect(TokenType::Equals)?;
let val = self.parse_expression()?;
return Ok(SetItem::property_assignment(prop_access, val));
}
let next = self.peek().clone();
match next.ty {
TokenType::Colon | TokenType::IS => {
let (labels, is_keyword) = self.parse_label_set_list()?;
Ok(SetItem::label_set(name, labels, is_keyword))
}
TokenType::Equals => {
self.next_token();
let val = self.parse_expression()?;
Ok(SetItem::variable_assignment(name, val, false))
}
TokenType::Plus => {
self.next_token(); // consume +
self.expect(TokenType::Equals)?;
let val = self.parse_expression()?;
Ok(SetItem::variable_assignment(name, val, true))
}
_ => Err(self.error_at_token(
&next,
format!("unexpected token {:?} in SET clause", next.literal),
)),
}
}
/// Parses a SET/REMOVE label-set item's labels after the variable
/// (CIP-126): `:A` / `:A:B:C` (colon, chainable) or `IS A` (IS keyword,
/// single label only). Each label atom may also be a CIP-162 dynamic
/// label (`$(<expr>)`/`$all(<expr>)`/`$any(<expr>)`), chainable the same
/// way (`n:$(a):$(b)`, `n IS $(a)`). Label-expression symbols (`&`, `|`,
/// `!`, `%`) are never allowed here, and `IS` cannot be chained with `:`
/// within one item. Returns the flat label-atom list and whether `IS`
/// was used (for the CIP-40/CIP-126 statement-level IS/colon-conjunction
/// mixing check).
pub(in crate::parser) fn parse_label_set_list(
&mut self,
) -> ParseResult<(Vec<LabelExpr>, bool)> {
let via_is = self.peek_type() == TokenType::IS;
self.next_token(); // consume ':' or 'IS'
if via_is && self.peek_is_labeled_keyword() {
return Err(self.error(
"invalid label specification context: IS LABELED is a predicate, not a label specification",
));
}
self.reject_label_spec_symbols()?;
let mut labels = vec![self.parse_label_atom()?];
if via_is {
if self.peek_type() == TokenType::Colon {
return Err(self.error(
"MixedIsAndColonConjunction: cannot mix IS and colon-conjunction within the same label item",
));
}
} else {
while self.peek_type() == TokenType::Colon {
self.next_token();
self.reject_label_spec_symbols()?;
labels.push(self.parse_label_atom()?);
}
}
self.reject_label_spec_symbols()?;
Ok((labels, via_is))
}
/// Fails if the current token is a label-expression symbol, which a
/// SET/REMOVE label specification never allows (see
/// `parse_label_set_list`).
pub(in crate::parser) fn reject_label_spec_symbols(&self) -> ParseResult<()> {
if matches!(
self.peek_type(),
TokenType::Ampersand | TokenType::Pipe | TokenType::Bang | TokenType::Percent
) {
return Err(self.error(
"InvalidLabelExpression: label expression symbols (&, |, !, %) are not allowed in a SET/REMOVE label specification",
));
}
Ok(())
}
pub(in crate::parser) fn parse_remove(&mut self) -> ParseResult<Clause> {
self.next_token(); // consume REMOVE
let mut items = Vec::new();
loop {
items.push(self.parse_remove_item()?);
if self.peek_type() != TokenType::Comma {
break;
}
self.next_token();
}
Ok(Clause::Remove(RemoveClause { items }))
}
pub(in crate::parser) fn parse_remove_item(&mut self) -> ParseResult<RemoveItem> {
// Handle a parenthesized expression property target (CIP-118 static
// `.prop`, CIP-131 dynamic `[<expr>]`): `REMOVE (<expr>).prop` /
// `REMOVE (<expr>)[<expr>]`, e.g. `REMOVE (COLLECT {...}[0]).prop`.
// Per the CIP's revision history, label removal still requires a
// plain variable -- only property removal accepts a general
// expression -- so `(expr):Label` is intentionally not accepted
// here (falls through to a syntax error).
if self.peek_type() == TokenType::LeftParen {
self.next_token(); // consume (
let inner_expr = self.parse_expression()?;
self.expect(TokenType::RightParen)?;
let accessors = self.parse_property_target_accessors()?;
if accessors.is_empty() {
let next = self.peek().clone();
return Err(self.error_at_token(
&next,
format!(
"expected '.' or '[' after parenthesized expression in REMOVE, got {:?}",
next.literal
),
));
}
return Ok(RemoveItem {
property: Some(Self::build_property_access(inner_expr, accessors)),
variable: None,
labels: Vec::new(),
is_keyword: false,
});
}
// CIP-131: an unparenthesized complex target expression, e.g.
// `REMOVE coalesce(n, m)[c]` or a list comprehension's element
// `REMOVE [x IN ns WHERE x.p = 1][0].p` (see `parse_set_item`'s
// matching comment). The one start-token predicate SET also uses.
if self.starts_general_property_target() {
let prop_access = self.parse_general_property_target()?;
return Ok(RemoveItem {
property: Some(prop_access),
variable: None,
labels: Vec::new(),
is_keyword: false,
});
}
// ADR-0191 (Decision point 7's follow-up probe): Neo4j 2026.09
// accepts every keyword spelling as a REMOVE target variable
// (`REMOVE MATCH.prop`, `REMOVE MATCH:Label`), matching
// `parse_set_item`'s equivalent read.
let name = self.parse_schema_name("identifier in REMOVE")?;
// CIP-131: `REMOVE n.prop` / `REMOVE n[<expr>]`, including chained
// accessors (see `parse_set_item`'s matching comment).
let accessors = self.parse_property_target_accessors()?;
if !accessors.is_empty() {
let prop_access = Self::build_property_access(Expr::Variable(name), accessors);
return Ok(RemoveItem {
property: Some(prop_access),
variable: None,
labels: Vec::new(),
is_keyword: false,
});
}
let next = self.peek().clone();
match next.ty {
TokenType::Colon | TokenType::IS => {
let (labels, is_keyword) = self.parse_label_set_list()?;
Ok(RemoveItem {
property: None,
variable: Some(name),
labels,
is_keyword,
})
}
_ => Err(self.error_at_token(
&next,
format!(
"expected . or : after identifier in REMOVE, got {:?}",
next.literal
),
)),
}
}
}