File size: 26,779 Bytes
29eea0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Static extractor for Cypher graph patterns. Standard library only; no engine needed.

It does not validate Cypher. It reads a query the way a reviewer skims one and pulls out what can
be compared with a schema:

    nodes       every node pattern: variable, labels, inline property map
    rels        every relationship pattern: variable, types, arrow direction, the two node patterns
    prop_refs   every `var.prop`, map-projection `.prop`, inline-map key and `SET var += {...}` key
    comparisons `var.prop <op> literal` (and inline `{prop: literal}`) with the literal's kind

Variables are resolved across the whole text (a label given in one clause counts everywhere) and
through simple `WITH a AS b` aliases. Anything the extractor cannot attribute to a pattern variable
(UNWIND rows, map values, list-comprehension variables) is left unbound so that a checker can stay
lenient: the aim is to never fail a correct query because of how it is written.

Works for Neo4j Cypher and for the Cypher dialect of Kuzu/LadybugDB, whose pattern syntax is the same.
"""
from __future__ import annotations

from dataclasses import dataclass, field

CLAUSES = {"MATCH", "MERGE", "CREATE", "WHERE", "WITH", "RETURN", "SET", "DELETE", "REMOVE", "UNWIND",
           "ORDER", "CALL", "FOREACH", "UNION", "LIMIT", "SKIP", "YIELD", "DETACH", "USE", "FINISH",
           "LOAD", "INSERT", "FILTER", "LET"}
KEYWORDS = CLAUSES | {"OPTIONAL", "AND", "OR", "XOR", "NOT", "IN", "IS", "NULL", "AS", "DISTINCT", "BY",
                      "ASC", "DESC", "ASCENDING", "DESCENDING", "CASE", "WHEN", "THEN", "ELSE", "END",
                      "STARTS", "ENDS", "CONTAINS", "TRUE", "FALSE", "ON", "ALL", "ANY", "NONE", "SINGLE",
                      "EXISTS", "COUNT", "COLLECT", "FROM", "HEADERS", "CSV", "FIELDTERMINATOR", "SHORTEST",
                      "REPEATABLE", "ELEMENTS", "DIFFERENT", "RELATIONSHIPS", "GROUPS", "PATH", "PATHS"}
TEMPORAL_FUNCS = {"date", "datetime", "localdatetime", "localtime", "time", "duration"}   # timestamp() is an integer
COMPARE_OPS = {"=", "<>", "!=", "<", ">", "<=", ">=", "=~"}
MULTI = ("<>", "<=", ">=", "=~", "..", "+=", "::", "!=")


@dataclass
class Tok:
    kind: str   # ident | num | str | param | punct
    text: str
    pos: int
    quoted: bool = False

    def is_kw(self, *words: str) -> bool:
        return self.kind == "ident" and not self.quoted and self.text.upper() in words

    def is_p(self, *chars: str) -> bool:
        return self.kind == "punct" and self.text in chars


def tokenize(text: str) -> list[Tok]:
    """Split Cypher into tokens, dropping comments. Never raises on odd input."""
    toks: list[Tok] = []
    i, n = 0, len(text)
    while i < n:
        c = text[i]
        if c.isspace():
            i += 1
        elif text.startswith("//", i):
            j = text.find("\n", i)
            i = n if j == -1 else j
        elif text.startswith("/*", i):
            j = text.find("*/", i + 2)
            i = n if j == -1 else j + 2
        elif c in "'\"":
            j, buf = i + 1, []
            while j < n:
                if text[j] == "\\" and j + 1 < n:
                    buf.append(text[j + 1]); j += 2
                elif text[j] == c:
                    if j + 1 < n and text[j + 1] == c:   # doubled quote
                        buf.append(c); j += 2
                    else:
                        break
                else:
                    buf.append(text[j]); j += 1
            toks.append(Tok("str", "".join(buf), i))
            i = j + 1
        elif c == "`":
            j, buf = i + 1, []
            while j < n:
                if text[j] == "`":
                    if j + 1 < n and text[j + 1] == "`":
                        buf.append("`"); j += 2
                    else:
                        break
                else:
                    buf.append(text[j]); j += 1
            toks.append(Tok("ident", "".join(buf), i, quoted=True))
            i = j + 1
        elif c == "$" and i + 1 < n and (text[i + 1].isalnum() or text[i + 1] in "_`"):
            j = i + 1
            if text[j] == "`":
                j = text.find("`", j + 1)
                j = n if j == -1 else j + 1
            else:
                while j < n and (text[j].isalnum() or text[j] == "_"):
                    j += 1
            toks.append(Tok("param", text[i:j], i))
            i = j
        elif c.isdigit():
            j = i
            while j < n and (text[j].isalnum() or text[j] == "_"):
                j += 1
            if j < n and text[j] == "." and j + 1 < n and text[j + 1].isdigit():   # 1.5, but not 1..3
                j += 1
                while j < n and (text[j].isalnum() or text[j] == "_"):
                    j += 1
                if j < n and text[j] in "+-" and text[j - 1] in "eE" and j + 1 < n and text[j + 1].isdigit():
                    j += 1
                    while j < n and text[j].isdigit():
                        j += 1
            toks.append(Tok("num", text[i:j], i))
            i = j
        elif c.isalpha() or c == "_":
            j = i
            while j < n and (text[j].isalnum() or text[j] == "_"):
                j += 1
            toks.append(Tok("ident", text[i:j], i))
            i = j
        else:
            two = text[i:i + 2]
            if two in MULTI:
                toks.append(Tok("punct", two, i)); i += 2
            else:
                toks.append(Tok("punct", c, i)); i += 1
    return toks


@dataclass
class Literal:
    kind: str    # string | integer | float | boolean | null | param | temporal | list | other
    text: str = ""


@dataclass
class NodeOcc:
    var: str | None
    labels: list[str]
    props: list[tuple[str, Literal]]
    pos: int
    in_rel: bool = False


@dataclass
class RelOcc:
    var: str | None
    types: list[str]
    direction: str          # "->" left to right, "<-" right to left, "--" no arrow
    left: int               # index into Extraction.nodes
    right: int
    varlen: bool
    props: list[tuple[str, Literal]]
    pos: int

    @property
    def directed(self) -> bool:
        return self.direction != "--"

    @property
    def source(self) -> int:
        return self.right if self.direction == "<-" else self.left

    @property
    def target(self) -> int:
        return self.left if self.direction == "<-" else self.right


@dataclass
class PropRef:
    var: str
    prop: str
    pos: int
    how: str = "access"     # access | projection | set-map


@dataclass
class Comparison:
    var: str | None          # None for an inline map on an anonymous pattern
    prop: str
    op: str
    literal: Literal
    node: int | None = None  # set for inline maps on node patterns
    rel: int | None = None   # set for inline maps on relationship patterns


@dataclass
class Extraction:
    nodes: list[NodeOcc] = field(default_factory=list)
    rels: list[RelOcc] = field(default_factory=list)
    prop_refs: list[PropRef] = field(default_factory=list)
    comparisons: list[Comparison] = field(default_factory=list)
    aliases: dict[str, str] = field(default_factory=dict)
    extra_labels: dict[str, set[str]] = field(default_factory=dict)   # from `WHERE n:Label`, `SET n:Label`
    notes: list[str] = field(default_factory=list)

    def resolve(self, var: str) -> str:
        seen = set()
        while var in self.aliases and var not in seen:
            seen.add(var)
            var = self.aliases[var]
        return var

    @property
    def node_vars(self) -> dict[str, set[str]]:
        out: dict[str, set[str]] = {}
        for nd in self.nodes:
            if nd.var:
                out.setdefault(nd.var, set()).update(nd.labels)
        for var, labels in self.extra_labels.items():
            if var in out:
                out[var].update(labels)
        return out

    @property
    def rel_vars(self) -> dict[str, set[str]]:
        out: dict[str, set[str]] = {}
        for r in self.rels:
            if r.var:
                out.setdefault(r.var, set()).update(r.types)
        return out

    @property
    def labels(self) -> set[str]:
        out = {lab for nd in self.nodes for lab in nd.labels}
        for labels in self.extra_labels.values():
            out |= labels
        return out

    @property
    def rel_types(self) -> set[str]:
        return {t for r in self.rels for t in r.types}

    def same_var(self, a: str | None, b: str | None) -> bool:
        """True if two variable names denote the same thing (equal, or linked by WITH ... AS aliases)."""
        return a is not None and b is not None and self.resolve(a) == self.resolve(b)

    def labels_of(self, node_index: int) -> set[str]:
        """Labels of one node pattern, including labels its variable (or an alias of it) was given elsewhere."""
        nd = self.nodes[node_index]
        if not nd.var:
            return set(nd.labels)
        out: set[str] = set()
        for var, labels in self.node_vars.items():
            if self.same_var(var, nd.var):
                out |= labels
        return out

    def summary(self) -> dict:
        return {
            "labels": sorted(self.labels),
            "rel_types": sorted(self.rel_types),
            "rels": [{"types": r.types, "direction": r.direction, "varlen": r.varlen,
                      "from": sorted(self.labels_of(r.source)) if r.directed else sorted(self.labels_of(r.left)),
                      "to": sorted(self.labels_of(r.target)) if r.directed else sorted(self.labels_of(r.right))}
                     for r in self.rels],
            "props": sorted({f"{self.resolve(p.var)}.{p.prop}" for p in self.prop_refs}),
        }


class _Parser:
    def __init__(self, text: str):
        self.t = tokenize(text)
        self.ex = Extraction()
        self.used = [False] * len(self.t)   # tokens that belong to a pattern

    # -- small helpers --------------------------------------------------------
    def tok(self, i: int) -> Tok | None:
        return self.t[i] if 0 <= i < len(self.t) else None

    def p(self, i: int, *chars: str) -> bool:
        tk = self.tok(i)
        return tk is not None and tk.is_p(*chars)

    def close_of(self, i: int) -> int | None:
        """Index of the bracket that closes the one at i."""
        pairs = {"(": ")", "[": "]", "{": "}"}
        stack = []
        for j in range(i, len(self.t)):
            tk = self.t[j]
            if tk.kind != "punct":
                continue
            if tk.text in pairs:
                stack.append(pairs[tk.text])
            elif tk.text in pairs.values():
                if not stack or stack.pop() != tk.text:
                    return None
                if not stack:
                    return j
        return None

    def literal_at(self, i: int, stops: tuple[str, ...]) -> Literal:
        """Classify the expression starting at i if it is a single literal ending at one of `stops`."""
        tk = self.tok(i)
        if tk is None:
            return Literal("other")
        j, sign = i, ""
        if tk.is_p("-", "+") and self.tok(i + 1) is not None and self.t[i + 1].kind == "num":
            sign, j, tk = tk.text, i + 1, self.t[i + 1]
        nxt = self.tok(j + 1)
        ends = nxt is None or (nxt.kind == "punct" and nxt.text in stops) or (
            nxt.kind == "ident" and not nxt.quoted and nxt.text.upper() in KEYWORDS)
        if tk.kind == "ident" and not tk.quoted and tk.text.lower() in TEMPORAL_FUNCS and self.p(j + 1, "("):
            end = self.close_of(j + 1)
            if end is not None and self.p(end + 1, "."):      # datetime().epochSeconds is a number, not a temporal
                return Literal("other")
            return Literal("temporal", tk.text.lower())
        if tk.is_p("["):
            end = self.close_of(j)
            kinds = set()
            if end is not None:
                k = j + 1
                while k < end:
                    kinds.add(self.literal_at(k, (",", "]")).kind)
                    while k < end and not self.p(k, ","):
                        k += 1
                    k += 1
            return Literal("list", ",".join(sorted(kinds)))
        if not ends:
            return Literal("other")
        if tk.kind == "str":
            return Literal("string", tk.text)
        if tk.kind == "num":
            is_float = any(ch in tk.text.lower() for ch in ".e") and not tk.text.lower().startswith("0x")
            return Literal("float" if is_float else "integer", sign + tk.text)
        if tk.kind == "param":
            return Literal("param", tk.text)
        if tk.is_kw("TRUE", "FALSE"):
            return Literal("boolean", tk.text.lower())
        if tk.is_kw("NULL"):
            return Literal("null")
        return Literal("other")

    def map_entries(self, i: int) -> tuple[list[tuple[str, Literal]], int] | None:
        """Parse `{key: value, ...}` at i. Returns the top-level keys and the index after `}`."""
        end = self.close_of(i)
        if end is None:
            return None
        out, j, depth = [], i + 1, 0
        expect_key = True
        while j < end:
            tk = self.t[j]
            if tk.kind == "punct" and tk.text in "([{":
                depth += 1
            elif tk.kind == "punct" and tk.text in ")]}":
                depth -= 1
            elif depth == 0 and tk.is_p(","):
                expect_key = True
            elif depth == 0 and expect_key and tk.kind in ("ident", "str") and self.p(j + 1, ":"):
                out.append((tk.text, self.literal_at(j + 2, (",", "}"))))
                expect_key = False
                j += 1
            j += 1
        return out, end + 1

    # -- patterns ---------------------------------------------------------------
    def label_expr(self, j: int, closers: tuple[str, ...]) -> tuple[list[str], int]:
        """Parse `:A:B`, `:A|B`, `:A&!B`, `:(A|B)&C` or `IS A`. Stops before a closer at depth 0."""
        labels, depth, after_name = [], 0, False
        j += 1   # skip ':' or IS
        while j < len(self.t):
            tk = self.t[j]
            if tk.kind == "ident" and not tk.is_kw("WHERE"):
                if after_name:                 # two names in a row: the label expression is over
                    break
                labels.append(tk.text)
                after_name = True
            elif tk.is_p(":", "|", "&", "!", "%"):
                after_name = False
            elif tk.is_p("("):
                depth += 1
            elif tk.is_p(")") and depth > 0:
                depth -= 1
            elif tk.is_p("$") and self.p(j + 1, "("):    # dynamic label :$(expr)
                end = self.close_of(j + 1)
                if end is None:
                    break
                self.ex.notes.append("dynamic label or type expression")
                j = end
            elif tk.kind == "param":
                self.ex.notes.append("dynamic label or type expression")
            else:
                break
            j += 1
        return labels, j

    def parse_node(self, i: int) -> tuple[NodeOcc, int] | None:
        if not self.p(i, "("):
            return None
        end = self.close_of(i)
        if end is None:
            return None
        j, var, labels, props = i + 1, None, [], []
        tk = self.tok(j)
        if tk and tk.kind == "ident" and not tk.is_kw("WHERE", "IS"):
            var, j = tk.text, j + 1
        tk = self.tok(j)
        if tk and (tk.is_p(":") or (tk.is_kw("IS") and var is not None)):
            labels, j = self.label_expr(j, (")",))
        if self.p(j, "{"):
            parsed = self.map_entries(j)
            if parsed is None:
                return None
            props, j = parsed
        elif self.tok(j) is not None and self.t[j].kind == "param":   # (n $props)
            j += 1
        if self.tok(j) is not None and self.t[j].is_kw("WHERE"):
            j = end
        if j != end:
            return None
        return NodeOcc(var, labels, props, self.t[i].pos), end + 1

    def parse_rel(self, j: int) -> tuple[dict, int] | None:
        """Parse `-[...]->`, `<-[...]-`, `--`, `-->`, `<--` starting at j. Returns the next index."""
        start = j
        left_arrow = False
        if self.p(j, "<"):
            left_arrow, j = True, j + 1
        if not self.p(j, "-"):
            return None
        j += 1
        var, types, varlen, props = None, [], False, []
        if self.p(j, "["):
            end = self.close_of(j)
            if end is None:
                return None
            k = j + 1
            tk = self.tok(k)
            if tk and tk.kind == "ident" and not tk.is_kw("WHERE", "IS"):
                var, k = tk.text, k + 1
            tk = self.tok(k)
            if tk and (tk.is_p(":") or (tk.is_kw("IS") and var is not None)):
                types, k = self.label_expr(k, ("]",))
            if self.p(k, "*"):
                varlen = True
                k += 1
                while k < end and (self.t[k].kind == "num" or self.p(k, "..")):
                    k += 1
            if self.p(k, "{"):
                parsed = self.map_entries(k)
                if parsed is None:
                    return None
                props, k = parsed
            elif self.tok(k) is not None and self.t[k].kind == "param":
                k += 1
            if self.tok(k) is not None and self.t[k].is_kw("WHERE"):
                k = end
            if k != end:
                return None
            j = end + 1
        if not self.p(j, "-"):
            return None
        j += 1
        right_arrow = False
        if self.p(j, ">"):
            right_arrow, j = True, j + 1
        # quantifier after a relationship: + * {n,m}
        if self.p(j, "+", "*"):
            varlen, j = True, j + 1
        elif self.p(j, "{"):
            end = self.close_of(j)
            if end is not None and all(self.t[x].kind == "num" or self.t[x].is_p(",") for x in range(j + 1, end)):
                varlen, j = True, end + 1
        direction = "->" if right_arrow and not left_arrow else "<-" if left_arrow and not right_arrow else "--"
        return {"var": var, "types": types, "varlen": varlen, "props": props, "direction": direction,
                "pos": self.t[start].pos}, j

    def scan_patterns(self) -> None:
        clause = ""
        i = 0
        while i < len(self.t):
            tk = self.t[i]
            if tk.kind == "ident" and not tk.quoted and tk.text.upper() in CLAUSES and not self.p(i - 1, "."):
                nxt = self.tok(i + 1)
                if not (nxt is not None and nxt.is_p("(", "{") and tk.text.upper() in ("COUNT", "COLLECT")):
                    clause = tk.text.upper()
            if not tk.is_p("(") or self.used[i]:
                i += 1
                continue
            parsed = self.parse_node(i)
            if parsed is None:
                i += 1
                continue
            node, j = parsed
            chain = [(node, i, j)]
            rels = []
            while True:
                r = self.parse_rel(j)
                if r is None:
                    break
                rel, k = r
                # a quantified path pattern may wrap the next node in an extra parenthesis; skip those
                nxt = self.parse_node(k)
                if nxt is None:
                    break
                rels.append(rel)
                chain.append((nxt[0], k, nxt[1]))
                j = nxt[1]
            prev = self.tok(i - 1)
            after_function = prev is not None and prev.kind == "ident" and (
                prev.quoted or prev.text.upper() not in KEYWORDS)
            bare = not node.labels and not node.props
            if not rels:
                standalone_ok = clause in ("MATCH", "MERGE", "CREATE") and not after_function
                if bare and not standalone_ok:
                    i += 1
                    continue
                if after_function and not node.labels:
                    i += 1
                    continue
            base = len(self.ex.nodes)
            for idx, (nd, a, b) in enumerate(chain):
                nd.in_rel = bool(rels)
                self.ex.nodes.append(nd)
                for x in range(a, b):
                    self.used[x] = True
                for key, lit in nd.props:
                    self.ex.comparisons.append(Comparison(nd.var, key, "=", lit, node=base + idx))
            for idx, rel in enumerate(rels):
                self.ex.rels.append(RelOcc(rel["var"], rel["types"], rel["direction"], base + idx, base + idx + 1,
                                           rel["varlen"], rel["props"], rel["pos"]))
                for key, lit in rel["props"]:
                    self.ex.comparisons.append(Comparison(rel["var"], key, "=", lit, rel=len(self.ex.rels) - 1))
            for x in range(i, j):
                self.used[x] = True
            i = j

    # -- everything outside patterns ------------------------------------------------
    def scan_rest(self) -> None:
        t = self.t
        bound = set(self.ex.node_vars) | set(self.ex.rel_vars)
        clause = ""
        for i, tk in enumerate(t):
            if tk.kind == "ident" and not tk.quoted and tk.text.upper() in CLAUSES and not self.p(i - 1, "."):
                clause = tk.text.upper()
            # aliases: WITH a AS b   /   , a AS b
            if tk.is_kw("AS") and i >= 2 and t[i - 1].kind == "ident" and i + 1 < len(t) and t[i + 1].kind == "ident":
                before = t[i - 2]
                if before.is_kw("WITH", "RETURN", "DISTINCT") or before.is_p(","):
                    src, dst = t[i - 1].text, t[i + 1].text
                    if src != dst and (src in bound or src in self.ex.aliases):
                        self.ex.aliases[dst] = src
            # label predicates outside patterns: WHERE n:Label, SET n:Label
            if tk.kind == "ident" and self.p(i + 1, ":") and not self.used[i] and tk.text in bound:
                prev = self.tok(i - 1)
                pred = prev is not None and (prev.is_kw("WHERE", "AND", "OR", "NOT", "XOR", "WHEN", "THEN", "ELSE", "SET", "REMOVE")
                                             or prev.is_p("(") or (prev.is_p(",") and clause in ("SET", "REMOVE")))
                if pred and not self.used[i + 1]:
                    labels, _ = self.label_expr(i + 1, (")",))
                    if labels:
                        self.ex.extra_labels.setdefault(tk.text, set()).update(labels)
        for i, tk in enumerate(t):
            if tk.kind != "ident":
                continue
            # property access: var.prop (not a.b.c, not ns.func(...))
            if self.p(i + 1, ".") and i + 2 < len(t) and t[i + 2].kind == "ident" and not self.p(i - 1, "."):
                j = i + 2
                while self.p(j + 1, ".") and j + 2 < len(t) and t[j + 2].kind == "ident":
                    j += 2
                is_call = self.p(j + 1, "(")
                prev = self.tok(i - 1)
                after_value = prev is not None and (prev.is_p(")", "]", "}") or prev.kind in ("str", "num", "param"))
                if not is_call and not after_value:
                    self.ex.prop_refs.append(PropRef(tk.text, t[i + 2].text, tk.pos))
                    if j == i + 2:
                        self._comparison(i)
            # map projection: var {.a, .b}
            if self.p(i + 1, "{") and not self.used[i + 1] and self.p(i + 2, "."):
                end = self.close_of(i + 1)
                if end is not None:
                    depth = 0
                    for k in range(i + 2, end):
                        if t[k].kind == "punct" and t[k].text in "([{":
                            depth += 1
                        elif t[k].kind == "punct" and t[k].text in ")]}":
                            depth -= 1
                        elif depth == 0 and t[k].is_p(".") and t[k + 1].kind == "ident" and (
                                t[k - 1].is_p("{", ",")):
                            self.ex.prop_refs.append(PropRef(tk.text, t[k + 1].text, t[k].pos, "projection"))
            # SET n += {a: 1}  /  SET n = {a: 1}
            if self.p(i + 1, "+=", "=") and self.p(i + 2, "{") and not self.p(i - 1, "."):
                prev = self.tok(i - 1)
                if prev is not None and (prev.is_kw("SET") or (prev.is_p(",") and clause in ("SET",))):
                    parsed = self.map_entries(i + 2)
                    if parsed:
                        for key, lit in parsed[0]:
                            self.ex.prop_refs.append(PropRef(tk.text, key, tk.pos, "set-map"))
                            self.ex.comparisons.append(Comparison(tk.text, key, "=", lit))

    def _comparison(self, i: int) -> None:
        """Record `var.prop <op> literal` or `literal <op> var.prop` for the access starting at token i."""
        t = self.t
        var, prop = t[i].text, t[i + 2].text
        nxt = self.tok(i + 3)
        stops = (")", ",", "]", "}", ";")
        if nxt is not None and nxt.kind == "punct" and nxt.text in COMPARE_OPS:
            lit = self.literal_at(i + 4, stops)
            self.ex.comparisons.append(Comparison(var, prop, nxt.text, lit))
        elif nxt is not None and nxt.is_kw("IN"):
            self.ex.comparisons.append(Comparison(var, prop, "IN", self.literal_at(i + 4, stops)))
        elif nxt is not None and nxt.is_kw("STARTS", "ENDS") and self.tok(i + 4) is not None and t[i + 4].is_kw("WITH"):
            self.ex.comparisons.append(Comparison(var, prop, nxt.text.upper() + " WITH", self.literal_at(i + 5, stops)))
        elif nxt is not None and nxt.is_kw("CONTAINS"):
            self.ex.comparisons.append(Comparison(var, prop, "CONTAINS", self.literal_at(i + 4, stops)))
        prev = self.tok(i - 1)
        if prev is not None and prev.kind == "punct" and prev.text in COMPARE_OPS and i >= 2:
            before = t[i - 2]
            flip = {"<": ">", ">": "<", "<=": ">=", ">=": "<="}
            kind = None
            if before.kind == "str":
                kind = Literal("string", before.text)
            elif before.kind == "num":
                kind = Literal("float" if "." in before.text else "integer", before.text)
            elif before.kind == "param":
                kind = Literal("param", before.text)
            if kind is not None:
                self.ex.comparisons.append(Comparison(var, prop, flip.get(prev.text, prev.text), kind))


def extract(text: str) -> Extraction:
    """Extract patterns from Cypher text (one or several statements)."""
    parser = _Parser(text)
    parser.scan_patterns()
    parser.scan_rest()
    return parser.ex


if __name__ == "__main__":
    import json
    import sys
    print(json.dumps(extract(open(sys.argv[1], encoding="utf-8").read()).summary(), indent=2))