mentat_edn 1.18.0

EDN parser for Project Mentat
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
// Copyright 2016-2018 Mozilla
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

extern crate bytes;
extern crate chrono;
extern crate hex;
extern crate itertools;
extern crate num;
extern crate ordered_float;
extern crate peg;
extern crate pretty;
#[cfg(feature = "serde_support")]
extern crate serde;
#[cfg(feature = "serde_support")]
#[macro_use]
extern crate serde_derive;
extern crate uuid;

pub mod depth;
pub use crate::depth::{check_nesting, MAX_NESTING};
pub mod entities;
pub mod intern_set;
pub use crate::intern_set::InternSet;
// Intentionally not pub.
pub mod matcher;
mod namespaceable_name;
pub mod pretty_print;
pub mod query;
pub mod symbols;
pub mod types;
pub mod utils;
pub mod value_rc;
pub use crate::value_rc::{Cloned, FromRc, ValueRc};

// Re-export the types we use.
use bytes::Bytes;
pub use chrono::{DateTime, Utc};
use hex::decode;
pub use num::BigInt;
pub use ordered_float::OrderedFloat;
pub use uuid::Uuid;

// Export from our modules.
pub use crate::types::{
    FromMicros, FromMillis, Span, SpannedValue, ToMicros, ToMillis, Value, ValueAndSpan,
};

pub use crate::symbols::{Keyword, NamespacedSymbol, PlainSymbol};

use std::collections::{BTreeMap, BTreeSet, LinkedList};
use std::iter::FromIterator;

use chrono::TimeZone;

use crate::entities::*;
use crate::query::FromValue;

// Goal: Be able to parse https://github.com/edn-format/edn
// Also extensible to help parse http://docs.datomic.com/query.html

// Debugging hint: test using `cargo test --features peg/trace -- --nocapture`
// to trace where the parser is failing

// TODO: Support tagged elements
// TODO: Support discard

pub type ParseError = peg::error::ParseError<peg::str::LineCol>;

/// One item of an `:in` clause: a source var (`$`, `$named`) or a binding form.
/// The grammar partitions these into `in_sources` and `in_bindings`.
enum InItem {
    Source(query::SrcVar),
    Binding(query::Binding),
}

peg::parser!(grammar grammar() for str {

    pub rule nil() -> SpannedValue = "nil" { SpannedValue::Nil }
    pub rule nan() -> SpannedValue = "#f" whitespace()+ "NaN" { SpannedValue::Float(OrderedFloat(f64::NAN)) }

    pub rule infinity() -> SpannedValue = "#f" whitespace()+ s:$(sign()) "Infinity"
        { SpannedValue::Float(OrderedFloat(if s == "+" { f64::INFINITY } else { f64::NEG_INFINITY })) }

    pub rule boolean() -> SpannedValue
        = "true"  { SpannedValue::Boolean(true) }
        / "false" { SpannedValue::Boolean(false) }

    rule digit() = ['0'..='9']
    rule alphanumeric() = ['0'..='9' | 'a'..='z' | 'A'..='Z']
    rule octaldigit() = ['0'..='7']
    rule validbase() = ['3']['0'..='6'] / ['1' | '2']['0'..='9'] / ['2'..='9']
    rule hex() = ['0'..='9' | 'a'..='f' | 'A'..='F']
    rule sign() = ['+' | '-']

    pub rule raw_bigint() -> BigInt = b:$( sign()? digit()+ ) "N"
        { b.parse::<BigInt>().unwrap() }
    pub rule raw_octalinteger() -> i64 = "0" i:$( octaldigit()+ )
        { i64::from_str_radix(i, 8).unwrap() }
    pub rule raw_hexinteger() -> i64 = "0x" i:$( hex()+ )
        { i64::from_str_radix(i, 16).unwrap() }
    pub rule raw_basedinteger() -> i64 = b:$( validbase() ) "r" i:$( alphanumeric()+ )
        { i64::from_str_radix(i, b.parse::<u32>().unwrap()).unwrap() }
    pub rule raw_integer() -> i64 = i:$( sign()? digit()+ ) !("." / (['e' | 'E']))
        { i.parse::<i64>().unwrap() }
    pub rule raw_float() -> OrderedFloat<f64> = f:$(sign()? digit()+ ("." digit()+)? (['e' | 'E'] sign()? digit()+)?)
        { OrderedFloat(f.parse::<f64>().unwrap()) }

    pub rule bigint() -> SpannedValue = v:raw_bigint() { SpannedValue::BigInteger(v) }
    pub rule octalinteger() -> SpannedValue = v:raw_octalinteger() { SpannedValue::Integer(v) }
    pub rule hexinteger() -> SpannedValue = v:raw_hexinteger() { SpannedValue::Integer(v) }
    pub rule basedinteger() -> SpannedValue = v:raw_basedinteger() { SpannedValue::Integer(v) }
    pub rule integer() -> SpannedValue = v:raw_integer() { SpannedValue::Integer(v) }
    pub rule float() -> SpannedValue = v:raw_float() { SpannedValue::Float(v) }

    rule number() -> SpannedValue = ( bigint() / basedinteger() / hexinteger() / octalinteger() / integer() / float() )

    // TODO: standalone characters: \<char>, \newline, \return, \space and \tab.
    // rule string_standalone_chars() ->
    // The escape must be *translated*, not echoed: matching the backslash and
    // returning the following character verbatim yields the LETTERS 'n'/'t'/'r'
    // instead of the control characters they denote, so "a\nb" unescaped to
    // "anb" -- silent data loss for any string carrying a newline or tab.
    // `\\` and `\"` are their own translation, hence the passthrough arm.
    rule string_special_char() -> &'input str = "\\" c:$(['\\' | '"' | 'n' | 't' | 'r']) {
        match c {
            "n" => "\n",
            "t" => "\t",
            "r" => "\r",
            literal => literal,
        }
    }
    rule string_normal_chars() -> &'input str = c:$((!['\"' | '\\'][_])+) { c }

    // This is what we need to do in order to unescape. We can't just match the entire string slice:
    // we get a Vec<&str> from rust-peg, where some parts might be unescaped special characters and
    // we join it together to form an output string.
    // E.g., input = r#"\"foo\\\\bar\""#
    //      output = [quote, "foo", backslash, "bar", quote]
    //      result = r#""foo\\bar""#
    // For the typical case, string_normal_chars will match multiple, leading to a single-element vec.
    pub rule raw_text() -> String = "\"" t:((string_special_char() / string_normal_chars())*) "\""
        {  t.join("") }

    pub rule text() -> SpannedValue
        = v:raw_text() { SpannedValue::Text(v) }

    // RFC 3339 timestamps. #inst "1985-04-12T23:20:50.52Z"
    // We accept an arbitrary depth of decimals.
    // TODO: Note that we discard the timezone information -- all times are translated to UTC.  Should we?
    rule inst_string() -> DateTime<Utc> =
        "#inst" whitespace()+ "\"" d:$( ['0'..='9']*<4> "-" ['0'..='2']['0'..='9'] "-" ['0'..='3']['0'..='9']
                "T"
                ['0'..='2']['0'..='9'] ":" ['0'..='5']['0'..='9'] ":" ['0'..='6']['0'..='9']
                ("." ['0'..='9']+)?
                ("Z" / (("+" / "-") ['0'..='2']['0'..='9'] ":" ['0'..='5']['0'..='9']))
            )
        "\"" {?
            DateTime::parse_from_rfc3339(d)
                .map(|t| t.with_timezone(&Utc))
                .map_err(|_| "invalid datetime")        // TODO Oh, rustpeg.
        }

    rule inst_micros() -> DateTime<Utc> =
        "#instmicros" whitespace()+ d:$( digit()+ ) {
            let micros = d.parse::<i64>().unwrap();
            let seconds: i64 = micros / 1_000_000;
            let nanos: u32 = ((micros % 1_000_000).unsigned_abs() as u32) * 1000;
            Utc.timestamp_opt(seconds, nanos).unwrap()
        }

    rule inst_millis() -> DateTime<Utc> =
        "#instmillis" whitespace()+ d:$( digit()+ ) {
            let millis = d.parse::<i64>().unwrap();
            let seconds: i64 = millis / 1000;
            let nanos: u32 = ((millis % 1000).unsigned_abs() as u32) * 1_000_000;
            Utc.timestamp_opt(seconds, nanos).unwrap()
        }

    rule inst() -> SpannedValue = t:(inst_millis() / inst_micros() / inst_string())
        { SpannedValue::Instant(t) }

    rule uuid_string() -> Uuid =
        "\"" u:$( ['a'..='f' | '0'..='9']*<8> "-" ['a'..='f' | '0'..='9']*<4> "-" ['a'..='f' | '0'..='9']*<4> "-" ['a'..='f' | '0'..='9']*<4> "-" ['a'..='f' | '0'..='9']*<12> ) "\"" {
            Uuid::parse_str(u).expect("this is a valid UUID string")
        }

    pub rule uuid() -> SpannedValue = "#uuid" whitespace()+ u:uuid_string()
        { SpannedValue::Uuid(u) }

    rule byte_buffer() -> Bytes =
        u:$( hex()+ ) {
            let b = decode(u).expect("this is a valid hex byte string");
            Bytes::copy_from_slice(&b)
        }
    pub rule bytes() -> SpannedValue = "#bytes" whitespace()+ u:byte_buffer()
        { SpannedValue::Bytes(u) }

    rule namespace_divider() = "."
    rule namespace_separator() = "/"

    // TODO: Be more picky here.
    // Keywords follow the rules of symbols, except they can (and must) begin with :
    // e.g. :fred or :my/fred. See https://github.com/edn-format/edn#keywords
    rule symbol_char_initial() = ['a'..='z' | 'A'..='Z' | '0'..='9' | '*' | '!' | '_' | '?' | '$' | '%' | '&' | '=' | '<' | '>']
    rule symbol_char_subsequent() = ['+' | 'a'..='z' | 'A'..='Z' | '0'..='9' | '*' | '!' | '_' | '?' | '$' | '%' | '&' | '=' | '<' | '>' | '-']

    rule symbol_namespace() = symbol_char_initial() symbol_char_subsequent()* (namespace_divider() symbol_char_subsequent()+)*
    rule symbol_name() = ( symbol_char_initial()+ symbol_char_subsequent()* )
    // EDN: `+`, `-` and `/` are symbols, and so is `+x`/`-x` when the second
    // character isn't a digit (`-5` stays an integer: value() tries number()
    // first). Without this, `(+ ?a 1)` was a parse error.
    rule sign_symbol_name() = ['+' | '-'] !['0'..='9'] symbol_char_subsequent()*
    rule plain_symbol_name() = symbol_name() / "..." / "." / sign_symbol_name() / "/"

    rule keyword_prefix() = ":"

    pub rule symbol() -> SpannedValue =
        ns:( sns:$(symbol_namespace()) namespace_separator() { sns })?
        n:$(plain_symbol_name())
        { SpannedValue::from_symbol(ns, n) }
        / expected!("symbol")

    pub rule keyword() -> SpannedValue =
        keyword_prefix()
        ns:( sns:$(symbol_namespace()) namespace_separator() { sns })?
        n:$(symbol_name())
        { SpannedValue::from_keyword(ns, n) }
        / expected!("keyword")

    pub rule list() -> SpannedValue = "(" __ v:(value())* __ ")"
        { SpannedValue::List(LinkedList::from_iter(v)) }

    pub rule vector() -> SpannedValue = "[" __ v:(value())* __ "]"
        { SpannedValue::Vector(v) }

    pub rule set() -> SpannedValue = "#{" __ v:(value())* __ "}"
        { SpannedValue::Set(BTreeSet::from_iter(v)) }

    pub rule pair() -> (ValueAndSpan, ValueAndSpan) =
        k:(value()) v:(value()) {
            (k, v)
        }

    pub rule map() -> SpannedValue = "{" __ v:(pair())* __ "}"
        { SpannedValue::Map(BTreeMap::from_iter(v)) }

    // Note: It's important that float comes before integer or the parser assumes that floats are integers and fails to parse.
    pub rule value() -> ValueAndSpan =
        __ start:position!() v:(nil() / nan() / infinity() / boolean() / number() / inst() / uuid() / bytes() / text() / keyword() / symbol() / list() / vector() / map() / set()  ) end:position!() __ {
            ValueAndSpan {
                inner: v,
                span: Span::new(start, end)
            }
        }
        / expected!("value")

    rule atom() -> ValueAndSpan
        = v:value() {? if v.is_atom() { Ok(v) } else { Err("expected atom") } }

    // Clojure (and thus EDN) regards commas as whitespace, and thus the two-element vectors [1 2] and
    // [1,,,,2] are equivalent, as are the maps {:a 1, :b 2} and {:a 1 :b 2}.
    rule whitespace() = quiet!{[' ' | '\r' | '\n' | '\t' | ',']}
    rule comment() = quiet!{";" (!['\r' | '\n'][_])* ['\r' | '\n']?}

    rule __() = (whitespace() / comment())*

    // Transaction entity parser starts here.

    pub rule op() -> OpType
        = ":db/add"     { OpType::Add }
        / ":db/retract" { OpType::Retract }

    // Recognize a built-in transaction-function keyword. Both the Datomic
    // `:db.fn/*` spelling and the shorthand `:db/*` spelling are accepted.
    rule builtin_tx_fn() -> BuiltinTxFn
        = ":db.fn/cas"          { BuiltinTxFn::Cas }
        / ":db/cas"             { BuiltinTxFn::Cas }
        / ":db.fn/retractEntity" { BuiltinTxFn::RetractEntity }
        / ":db/retractEntity"    { BuiltinTxFn::RetractEntity }

    rule raw_keyword() -> Keyword =
        keyword_prefix()
        ns:( sns:$(symbol_namespace()) namespace_separator() { sns })?
        n:$(symbol_name()) {
            match ns {
                Some(ns) => Keyword::namespaced(ns, n),
                None => Keyword::plain(n),
            }
        }
        / expected!("keyword")

    rule raw_forward_keyword() -> Keyword
        = v:raw_keyword() {? if v.is_forward() { Ok(v) } else { Err("expected :forward or :forward/keyword") } }

    rule raw_backward_keyword() -> Keyword
        = v:raw_keyword() {? if v.is_backward() { Ok(v) } else { Err("expected :_backward or :backward/_keyword") } }

    rule raw_namespaced_keyword() -> Keyword
        = keyword_prefix() ns:$(symbol_namespace()) namespace_separator() n:$(symbol_name()) { Keyword::namespaced(ns, n) }
        / expected!("namespaced keyword")

    rule raw_forward_namespaced_keyword() -> Keyword
        = v:raw_namespaced_keyword() {? if v.is_forward() { Ok(v) } else { Err("expected namespaced :forward/keyword") } }

    rule raw_backward_namespaced_keyword() -> Keyword
        = v:raw_namespaced_keyword() {? if v.is_backward() { Ok(v) } else { Err("expected namespaced :backward/_keyword") } }

    rule entid() -> EntidOrIdent
        = v:( raw_basedinteger() / raw_hexinteger() / raw_octalinteger() / raw_integer() ) { EntidOrIdent::Entid(v) }
        / v:raw_namespaced_keyword() { EntidOrIdent::Ident(v) }
        / expected!("entid")

    rule forward_entid() -> EntidOrIdent
        = v:( raw_basedinteger() / raw_hexinteger() / raw_octalinteger() / raw_integer() ) { EntidOrIdent::Entid(v) }
        / v:raw_forward_namespaced_keyword() { EntidOrIdent::Ident(v) }
        / expected!("forward entid")

    rule backward_entid() -> EntidOrIdent
        = v:raw_backward_namespaced_keyword() { EntidOrIdent::Ident(v.to_reversed()) }
        / expected!("backward entid")

    rule lookup_ref() -> LookupRef<ValueAndSpan>
        = "(" __ "lookup-ref" __ a:(entid()) __ v:(value()) __ ")" { LookupRef { a: AttributePlace::Entid(a), v } }
        / expected!("lookup-ref")

    // Datomic's `[a v]` lookup ref (#152). Only in entity position: in value position a
    // vector may also be a list of values, so the transactor decides from the schema.
    rule vector_lookup_ref() -> LookupRef<ValueAndSpan>
        = "[" __ a:(raw_namespaced_keyword()) __ v:(atom()) __ "]" { LookupRef { a: AttributePlace::Entid(EntidOrIdent::Ident(a)), v } }

    rule tx_function() -> TxFunction
        = "(" __ n:$(symbol_name()) __ ")" { TxFunction { op: PlainSymbol::plain(n) } }

    rule entity_place() -> EntityPlace<ValueAndSpan>
        = v:raw_text() { EntityPlace::TempId(TempId::External(v).into()) }
        / v:entid() { EntityPlace::Entid(v) }
        / v:lookup_ref() { EntityPlace::LookupRef(v) }
        / v:vector_lookup_ref() { EntityPlace::LookupRef(v) }
        / v:tx_function() { EntityPlace::TxFunction(v) }

    rule value_place_pair() -> (EntidOrIdent, ValuePlace<ValueAndSpan>)
        = k:(entid()) __ v:(value_place()) { (k, v) }

    rule map_notation() -> MapNotation<ValueAndSpan>
        = "{" __ kvs:(value_place_pair()*) __ "}" { kvs.into_iter().collect() }

    rule value_place() -> ValuePlace<ValueAndSpan>
        = __ v:lookup_ref() __ { ValuePlace::LookupRef(v) }
        / __ v:tx_function() __ { ValuePlace::TxFunction(v) }
        / __ "[" __ vs:(value_place()*) __ "]" __ { ValuePlace::Vector(vs) }
        / __ v:map_notation() __ { ValuePlace::MapNotation(v) }
        / __ v:atom() __ { ValuePlace::Atom(v) }

    pub rule entity() -> Entity<ValueAndSpan>
        = __ "[" __ fun:(builtin_tx_fn()) __ args:(value_place()*) __ "]" __ { Entity::TxFunction { fun, args } }
        / __ "[" __ op:(op()) __ e:(entity_place()) __ a:(forward_entid())  __ v:(value_place()) __  "]" __ { Entity::AddOrRetract { op, e, a: AttributePlace::Entid(a), v } }
        / __ "[" __ op:(op()) __ e:(value_place())  __ a:(backward_entid()) __ v:(entity_place()) __ "]" __ { Entity::AddOrRetract { op, e: v, a: AttributePlace::Entid(a), v: e } }
        / __ map:map_notation() __ { Entity::MapNotation(map) }
        / expected!("entity")

    pub rule entities() -> Vec<Entity<ValueAndSpan>>
        = __ "[" __ es:(entity()*) __ "]" __ { es }

    // Query parser starts here.
    //
    // We expect every rule except the `raw_*` rules to eat whitespace
    // (with `__`) at its start and finish.  That means that every string
    // pattern (say "[") should be bracketed on either side with either a
    // whitespace-eating rule or an explicit whitespace eating `__`.

    // `+`, `-` and `/` don't start like other symbols (symbol_char_initial);
    // name them so the arithmetic where-fns [(+ ?a ?b) ?c] etc. parse.
    rule query_function() -> query::QueryFunction
        = __ n:$(symbol_name() / "+" / "-" / "/") __ {? query::QueryFunction::from_symbol(&PlainSymbol::plain(n)).ok_or("expected query function") }

    rule fn_arg() -> query::FnArg
        = v:value() {? query::FnArg::from_value(&v).ok_or("expected query function argument") }
        / __ "[" args:fn_arg()+ "]" __ { query::FnArg::Vector(args) }

    rule find_elem() -> query::Element
        = __ v:variable() __ { query::Element::Variable(v) }
        / __ "(" __ "the" v:variable() ")" __ { query::Element::Corresponding(v) }
        / __ "(" __ "pull" var:variable() patterns:pull_pattern() ")" __ { query::Element::Pull(query::Pull { var, patterns }) }
        / __ "(" func:query_function() args:fn_arg()* ")" __ { query::Element::Aggregate(query::Aggregate { func, args }) }

    rule find_spec() -> query::FindSpec
        = f:find_elem() "." __ { query::FindSpec::FindScalar(f) }
        / fs:find_elem()+ { query::FindSpec::FindRel(fs) }
        / __ "[" f:find_elem() __ "..." __ "]" __ { query::FindSpec::FindColl(f) }
        / __ "[" fs:find_elem()+ "]" __ { query::FindSpec::FindTuple(fs) }

    /// A pull pattern on its own, e.g. `[:person/name {:person/friend [*]}]`.
    pub rule pull_pattern() -> Vec<query::PullAttributeSpec>
        = __ "[" patterns:pull_attribute()+ "]" __ { patterns.into_iter().flatten().collect() }

    // One pattern item; a map spec `{:a [..] :b [..]}` yields one spec per entry.
    rule pull_attribute() -> Vec<query::PullAttributeSpec>
        = __ "*" __ { vec![query::PullAttributeSpec::Wildcard] }
        / a:pull_keyword() alias:(":as" __ alias:raw_forward_keyword() __ { alias })? {
            vec![query::PullAttributeSpec::Attribute(query::NamedPullAttribute {
                alias: alias.map(::std::rc::Rc::new),
                ..a
            })]
        }
        / __ "{" es:pull_map_entry()+ "}" __ { es }

    rule pull_map_entry() -> query::PullAttributeSpec
        = a:pull_keyword() p:pull_pattern() { query::PullAttributeSpec::Nested(a, p) }

    rule pull_keyword() -> query::NamedPullAttribute
        = __ k:raw_backward_namespaced_keyword() __ {
            // Reverse pull, e.g. `:person/_friends`. Store the *forward* ident
            // (k.to_reversed()) for schema lookup and mark it reverse.
            query::NamedPullAttribute {
                attribute: query::PullConcreteAttribute::Ident(::std::rc::Rc::new(k.to_reversed())),
                alias: None,
                reverse: true,
            }
        }
        / __ k:raw_forward_namespaced_keyword() __ {
            query::NamedPullAttribute {
                attribute: query::PullConcreteAttribute::Ident(::std::rc::Rc::new(k)),
                alias: None,
                reverse: false,
            }
        }

    rule limit() -> query::Limit
        = __ v:variable() __ { query::Limit::Variable(v) }
        / __ n:(raw_octalinteger() / raw_hexinteger() / raw_basedinteger() / raw_integer()) __ {?
            if n > 0 {
                Ok(query::Limit::Fixed(n as u64))
            } else {
                Err("expected positive integer")
            }
        }

    rule offset() -> query::Offset
        = __ v:variable() __ { query::Offset::Variable(v) }
        / __ n:(raw_octalinteger() / raw_hexinteger() / raw_basedinteger() / raw_integer()) __ {?
            if n >= 0 {
                Ok(query::Offset::Fixed(n as u64))
            } else {
                Err("expected non-negative integer")
            }
        }

    rule order() -> query::Order
        = __ "(" __ "asc" v:variable() ")" __ { query::Order(query::Direction::Ascending, v) }
        / __ "(" __ "desc" v:variable() ")" __ { query::Order(query::Direction::Descending, v) }
        / v:variable() { query::Order(query::Direction::Ascending, v) }


    rule pattern_value_place() -> query::PatternValuePlace
        = v:value() {? query::PatternValuePlace::from_value(&v).ok_or("expected pattern_value_place") }

    rule pattern_non_value_place() -> query::PatternNonValuePlace
        = v:value() {? query::PatternNonValuePlace::from_value(&v).ok_or("expected pattern_non_value_place") }

    rule pattern() -> query::WhereClause
        = __ "["
          src:src_var()?
          e:pattern_non_value_place()
          a:pattern_non_value_place()
          v:pattern_value_place()?
          tx:pattern_non_value_place()?
          added:pattern_non_value_place()?
        "]" __
        {?
            let v = v.unwrap_or(query::PatternValuePlace::Placeholder);
            let tx = tx.unwrap_or(query::PatternNonValuePlace::Placeholder);
            let added = added.unwrap_or(query::PatternNonValuePlace::Placeholder);

            // Pattern::new takes care of reversal of reversed
            // attributes: [?x :foo/_bar ?y] turns into
            // [?y :foo/bar ?x].
            //
            // This is a bit messy: the inner conversion to a Pattern can
            // fail if the input is something like
            //
            // ```edn
            // [?x :foo/_reversed 23.4]
            // ```
            //
            // because
            //
            // ```edn
            // [23.4 :foo/reversed ?x]
            // ```
            //
            // is nonsense. That leaves us with a nested optional, which we unwrap here.
            query::Pattern::new(src, e, a, v, tx, added)
                .map(query::WhereClause::Pattern)
                .ok_or("expected pattern")
        }

    // TODO: This shouldn't be checked at parse time.
    rule rule_vars() -> BTreeSet<query::Variable>
        = vs:variable()+ {?
            let given = vs.len();
            let set: BTreeSet<query::Variable> = vs.into_iter().collect();
            if given != set.len() {
                Err("expected unique variables")
            } else {
                Ok(set)
            }
        }

    rule or_pattern_clause() -> query::OrWhereClause
        = clause:where_clause() { query::OrWhereClause::Clause(clause) }

    rule or_and_clause() -> query::OrWhereClause
        = __ "(" __ "and" clauses:where_clause()+ ")" __ { query::OrWhereClause::And(clauses) }

    rule or_where_clause() -> query::OrWhereClause
        = or_pattern_clause()
        / or_and_clause()

    rule or_clause() -> query::WhereClause
        = __ "(" __ "or" clauses:or_where_clause()+ ")" __ {
             query::WhereClause::OrJoin(query::OrJoin::new(query::UnifyVars::Implicit, clauses))
        }

    rule or_join_clause() -> query::WhereClause
        = __ "(" __ "or-join" __ "[" vars:rule_vars() "]" clauses:or_where_clause()+ ")" __ {
             query::WhereClause::OrJoin(query::OrJoin::new(query::UnifyVars::Explicit(vars), clauses))
        }

    rule not_clause() -> query::WhereClause
        = __ "(" __ "not" clauses:where_clause()+ ")" __ {
             query::WhereClause::NotJoin(query::NotJoin::new(query::UnifyVars::Implicit, clauses))
        }

    rule not_join_clause() -> query::WhereClause
        = __ "(" __ "not-join" __ "[" vars:rule_vars() "]" clauses:where_clause()+ ")" __ {
             query::WhereClause::NotJoin(query::NotJoin::new(query::UnifyVars::Explicit(vars), clauses))
        }

    rule type_annotation() -> query::WhereClause
        = __ "[" __ "(" __ "type" var:variable() __ ty:raw_keyword() __ ")" __ "]" __ {
            query::WhereClause::TypeAnnotation(
                query::TypeAnnotation {
                    value_type: ty,
                    variable: var,
                })
        }

    rule pred() -> query::WhereClause
        = __ "[" __ "(" func:query_function() args:fn_arg()* ")" __ "]" __ {
            query::WhereClause::Pred(
                query::Predicate {
                    operator: func.0,
                    args,
                })
        }

    pub rule where_fn() -> query::WhereClause
        = __ "[" __ "(" func:query_function() args:fn_arg()* ")" __ binding:binding() "]" __ {
            query::WhereClause::WhereFn(
                query::WhereFn {
                    operator: func.0,
                    args,
                    binding,
                })
        }

    rule where_clause() -> query::WhereClause
        // Right now we only support patterns and predicates. See #239 for more.
        = pattern()
        / or_join_clause()
        / or_clause()
        / not_join_clause()
        / not_clause()
        / type_annotation()
        / pred()
        / where_fn()
        / rule_invocation()

    // A rule invocation in a query body, e.g. `(ancestor ?x ?y)`. Ported from
    // pg_mentat. Only matches names that are NOT reserved clause keywords
    // (those are handled by their own rules above).
    rule rule_invocation() -> query::WhereClause
        = __ "(" __ n:$(symbol_name()) args:fn_arg()+ ")" __ {?
            let name = n.to_string();
            if name == "or" || name == "not" || name == "and"
                || name == "or-join" || name == "not-join"
                || name == "type" || name == "fulltext" || name == "ground"
                || name == "get-else" || name == "missing?" {
                Err("not a rule invocation")
            } else {
                Ok(query::WhereClause::RuleExpr(query::RuleInvocation {
                    name: PlainSymbol::plain(&name),
                    args,
                }))
            }
        }

    // A rule head: (rule-name ?arg1 ?arg2)
    rule rule_head() -> query::RuleInvocation
        = __ "(" __ n:$(symbol_name()) args:fn_arg()+ ")" __ {
            query::RuleInvocation {
                name: PlainSymbol::plain(n),
                args,
            }
        }

    // A rule clause: [(rule-name ?a ?b) body-patterns...]
    rule rule_clause() -> query::RuleClause
        = __ "[" head:rule_head() body:where_clause()+ "]" __ {
            query::RuleClause { head, body }
        }

    // A set of rule clauses: [clause1 clause2 ...], grouped by rule name.
    rule rule_definitions() -> Vec<query::Rule>
        = __ "[" clauses:rule_clause()+ "]" __ {
            let mut rule_map: std::collections::BTreeMap<String, Vec<query::RuleClause>> =
                std::collections::BTreeMap::new();
            for clause in clauses {
                let name = clause.head.name.0.clone();
                rule_map.entry(name).or_default().push(clause);
            }
            rule_map.into_iter().map(|(name, clauses)| {
                query::Rule {
                    name: PlainSymbol::plain(&name),
                    clauses,
                }
            }).collect()
        }

    rule query_part() -> Vec<query::QueryPart>
        = __ ":find" fs:find_spec() { vec![query::QueryPart::FindSpec(fs)] }
        / __ ":in" parts:in_clause() { parts }
        / __ ":limit" l:limit() { vec![query::QueryPart::Limit(l)] }
        / __ ":offset" o:offset() { vec![query::QueryPart::Offset(o)] }
        / __ ":order" os:order()+ { vec![query::QueryPart::Order(os)] }
        / __ ":where" ws:where_clause()+ { vec![query::QueryPart::WhereClauses(ws)] }
        / __ ":rules" rules:rule_definitions() { vec![query::QueryPart::Rules(rules)] }
        / __ ":with" rules:rule_definitions() { vec![query::QueryPart::Rules(rules)] }
        / __ ":with" with_vars:variable()+ { vec![query::QueryPart::WithVars(with_vars)] }
        / __ ":distinct" { vec![query::QueryPart::Distinct] }

    // `:in` accepts source vars (`$`, `$named`) and binding forms, in any mix.
    // Sources go to InSources, everything else to InBindings; the scalar
    // bindings are later derived into in_vars. Emits up to two parts.
    rule in_clause() -> Vec<query::QueryPart>
        = items:in_item()+ {
            let mut sources = Vec::new();
            let mut bindings = Vec::new();
            for item in items {
                match item {
                    InItem::Source(s) => sources.push(s),
                    InItem::Binding(b) => bindings.push(b),
                }
            }
            let mut parts = Vec::new();
            if !sources.is_empty() { parts.push(query::QueryPart::InSources(sources)); }
            parts.push(query::QueryPart::InBindings(bindings));
            parts
        }

    // A source var if it starts with `$`, else a binding. `src_var()` accepts
    // any symbol, so try it only on `$`-led input to avoid swallowing `?vars`.
    rule in_item() -> InItem
        = __ &"$" s:src_var() { InItem::Source(s) }
        / b:binding() { InItem::Binding(b) }

    pub rule parse_query() -> query::ParsedQuery
        = __ "[" qps:query_part()+ "]" __ {? query::ParsedQuery::from_parts(qps.into_iter().flatten().collect()) }

    rule variable() -> query::Variable
        = v:value() {? query::Variable::from_value(&v).ok_or("expected variable") }

    rule src_var() -> query::SrcVar
        = v:value() {? query::SrcVar::from_value(&v).ok_or("expected src_var") }

    rule variable_or_placeholder() -> query::VariableOrPlaceholder
        = v:variable() { query::VariableOrPlaceholder::Variable(v) }
        / __ "_" __ { query::VariableOrPlaceholder::Placeholder }

    rule binding() -> query::Binding
        = __ "[" __ "[" vs:variable_or_placeholder()+ "]" __ "]" __ { query::Binding::BindRel(vs) }
        / __ "[" v:variable() "..." __ "]" __ { query::Binding::BindColl(v) }
        / __ "[" vs:variable_or_placeholder()+ "]" __ { query::Binding::BindTuple(vs) }
        / v:variable() { query::Binding::BindScalar(v) }

});

/// The parser. Every entry point rejects input nested deeper than
/// [`MAX_NESTING`] before the grammar runs; see [`depth`] for why.
pub mod parse {
    use super::*;
    pub fn nil(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::nil(input)
    }
    pub fn nan(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::nan(input)
    }
    pub fn infinity(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::infinity(input)
    }
    pub fn boolean(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::boolean(input)
    }
    pub fn raw_bigint(input: &str) -> Result<BigInt, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::raw_bigint(input)
    }
    pub fn raw_octalinteger(input: &str) -> Result<i64, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::raw_octalinteger(input)
    }
    pub fn raw_hexinteger(input: &str) -> Result<i64, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::raw_hexinteger(input)
    }
    pub fn raw_basedinteger(input: &str) -> Result<i64, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::raw_basedinteger(input)
    }
    pub fn raw_integer(input: &str) -> Result<i64, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::raw_integer(input)
    }
    pub fn raw_float(input: &str) -> Result<OrderedFloat<f64>, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::raw_float(input)
    }
    pub fn bigint(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::bigint(input)
    }
    pub fn octalinteger(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::octalinteger(input)
    }
    pub fn hexinteger(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::hexinteger(input)
    }
    pub fn basedinteger(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::basedinteger(input)
    }
    pub fn integer(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::integer(input)
    }
    pub fn float(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::float(input)
    }
    pub fn raw_text(input: &str) -> Result<String, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::raw_text(input)
    }
    pub fn text(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::text(input)
    }
    pub fn uuid(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::uuid(input)
    }
    pub fn bytes(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::bytes(input)
    }
    pub fn symbol(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::symbol(input)
    }
    pub fn keyword(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::keyword(input)
    }
    pub fn list(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::list(input)
    }
    pub fn vector(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::vector(input)
    }
    pub fn set(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::set(input)
    }
    pub fn pair(input: &str) -> Result<(ValueAndSpan, ValueAndSpan), ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::pair(input)
    }
    pub fn map(input: &str) -> Result<SpannedValue, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::map(input)
    }
    pub fn value(input: &str) -> Result<ValueAndSpan, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::value(input)
    }
    pub fn op(input: &str) -> Result<OpType, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::op(input)
    }
    pub fn entity(input: &str) -> Result<Entity<ValueAndSpan>, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::entity(input)
    }
    pub fn entities(input: &str) -> Result<Vec<Entity<ValueAndSpan>>, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::entities(input)
    }
    pub fn where_fn(input: &str) -> Result<crate::query::WhereClause, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::where_fn(input)
    }
    pub fn parse_query(input: &str) -> Result<crate::query::ParsedQuery, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::parse_query(input)
    }
    pub fn pull_pattern(input: &str) -> Result<Vec<crate::query::PullAttributeSpec>, ParseError> {
        crate::depth::check_nesting(input, crate::depth::MAX_NESTING)?;
        grammar::pull_pattern(input)
    }
}