summaryrefslogtreecommitdiff
path: root/dhall_core/src/parser.rs
blob: 0ba15869dfa583a5d2153058dde65ca8544aa5e2 (plain)
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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
use pest::iterators::Pair;
use pest::Parser;
use std::collections::BTreeMap;
use std::path::PathBuf;

use dhall_parser::{DhallParser, Rule};

use crate::core;
use crate::core::*;
use crate::grammar_util::{BoxExpr, ParsedExpr};

pub type ParseError = pest::error::Error<Rule>;

pub type ParseResult<T> = Result<T, ParseError>;

pub fn custom_parse_error(pair: &Pair<Rule>, msg: String) -> ParseError {
    let msg =
        format!("{} while matching on:\n{}", msg, debug_pair(pair.clone()));
    let e = pest::error::ErrorVariant::CustomError { message: msg };
    pest::error::Error::new_from_span(e, pair.as_span())
}

fn debug_pair(pair: Pair<Rule>) -> String {
    use std::fmt::Write;
    let mut s = String::new();
    fn aux(s: &mut String, indent: usize, prefix: String, pair: Pair<Rule>) {
        let indent_str = "| ".repeat(indent);
        let rule = pair.as_rule();
        let contents = pair.as_str().clone();
        let mut inner = pair.into_inner();
        let mut first = true;
        while let Some(p) = inner.next() {
            if first {
                first = false;
                let last = inner.peek().is_none();
                if last && p.as_str() == contents {
                    let prefix = format!("{}{:?} > ", prefix, rule);
                    aux(s, indent, prefix, p);
                    continue;
                } else {
                    writeln!(
                        s,
                        r#"{}{}{:?}: "{}""#,
                        indent_str, prefix, rule, contents
                    )
                    .unwrap();
                }
            }
            aux(s, indent + 1, "".into(), p);
        }
        if first {
            writeln!(
                s,
                r#"{}{}{:?}: "{}""#,
                indent_str, prefix, rule, contents
            )
            .unwrap();
        }
    }
    aux(&mut s, 0, "".into(), pair);
    s
}

/* Macro to pattern-match iterators.
 * Returns `Result<_, IterMatchError<_>>`.
 *
 * Example:
 * ```
 * let vec = vec![1, 2, 3];
 *
 * match_iter!(vec.into_iter(); (x, y?, z) => {
 *  // x: T
 *  // y: Option<T>
 *  // z: T
 * })
 *
 * // or
 * match_iter!(vec.into_iter(); (x, y, z*) => {
 *  // x, y: T
 *  // z: impl Iterator<T>
 * })
 * ```
 *
*/
#[derive(Debug)]
enum IterMatchError<T> {
    NotEnoughItems,
    TooManyItems,
    NoMatchFound,
    Other(T), // Allow other macros to inject their own errors
}
macro_rules! match_iter {
    // Everything else pattern
    (@match 0, $iter:expr, $x:ident* $($rest:tt)*) => {
        match_iter!(@match 2, $iter $($rest)*);
        #[allow(unused_mut)]
        let mut $x = $iter;
    };
    // Alias to use in macros
    (@match 0, $iter:expr, $x:ident?? $($rest:tt)*) => {
        match_iter!(@match 2, $iter $($rest)*);
        #[allow(unused_mut)]
        let mut $x = $iter;
    };
    // Optional pattern
    (@match 0, $iter:expr, $x:ident? $($rest:tt)*) => {
        match_iter!(@match 1, $iter $($rest)*);
        #[allow(unused_mut)]
        let mut $x = $iter.next();
        match $iter.next() {
            Some(_) => break Err(IterMatchError::TooManyItems),
            None => {},
        };
    };
    // Normal pattern
    (@match 0, $iter:expr, $x:ident $($rest:tt)*) => {
        #[allow(unused_mut)]
        let mut $x = match $iter.next() {
            Some(x) => x,
            None => break Err(IterMatchError::NotEnoughItems),
        };
        match_iter!(@match 0, $iter $($rest)*);
    };
    // Normal pattern after a variable length one: declare reversed and take from the end
    (@match $w:expr, $iter:expr, $x:ident $($rest:tt)*) => {
        match_iter!(@match $w, $iter $($rest)*);
        #[allow(unused_mut)]
        let mut $x = match $iter.next_back() {
            Some(x) => x,
            None => break Err(IterMatchError::NotEnoughItems),
        };
    };

    // Check no elements remain
    (@match 0, $iter:expr $(,)*) => {
        match $iter.next() {
            Some(_) => break Err(IterMatchError::TooManyItems),
            None => {},
        };
    };
    (@match $_:expr, $iter:expr) => {};

    (@panic; $($args:tt)*) => {
        {
            let ret: Result<_, IterMatchError<()>> = match_iter!($($args)*);
            ret.unwrap()
        }
    };
    ($iter:expr; ($($args:tt)*) => $body:expr) => {
        {
            #[allow(unused_mut)]
            let mut iter = $iter;
            // Not a real loop; used for error handling
            let ret: Result<_, IterMatchError<_>> = loop {
                match_iter!(@match 0, iter, $($args)*);
                break Ok($body);
            };
            ret
        }
    };
}

/* Extends match_iter with typed matches. Takes a callback that determines
 * when a capture matches.
 * Returns `Result<_, IterMatchError<_>>`; errors returned by the callback will
 * get propagated using IterMatchError::Other.
 *
 * Example:
 * ```
 * macro_rules! callback {
 *     (@type_callback, positive, $x:expr) => {
 *         if $x >= 0 { Ok($x) } else { Err(()) }
 *     };
 *     (@type_callback, negative, $x:expr) => {
 *         if $x <= 0 { Ok($x) } else { Err(()) }
 *     };
 *     (@type_callback, any, $x:expr) => {
 *         Ok($x)
 *     };
 * }
 *
 * let vec = vec![-1, 2, 3];
 *
 * match_iter_typed!(callback; vec.into_iter();
 *     (x: positive, y?: negative, z: any) => { ... },
 * )
 * ```
 *
*/
macro_rules! match_iter_typed {
    // Collect untyped arguments to pass to match_iter!
    (@collect, ($($vars:tt)*), ($($args:tt)*), ($($acc:tt)*), ($x:ident : $ty:ident, $($rest:tt)*)) => {
        match_iter_typed!(@collect, ($($vars)*), ($($args)*), ($($acc)*, $x), ($($rest)*))
    };
    (@collect, ($($vars:tt)*), ($($args:tt)*), ($($acc:tt)*), ($x:ident? : $ty:ident, $($rest:tt)*)) => {
        match_iter_typed!(@collect, ($($vars)*), ($($args)*), ($($acc)*, $x?), ($($rest)*))
    };
    (@collect, ($($vars:tt)*), ($($args:tt)*), ($($acc:tt)*), ($x:ident* : $ty:ident, $($rest:tt)*)) => {
        match_iter_typed!(@collect, ($($vars)*), ($($args)*), ($($acc)*, $x??), ($($rest)*))
    };
    // Catch extra comma if exists
    (@collect, ($($vars:tt)*), ($($args:tt)*), (,$($acc:tt)*), ($(,)*)) => {
        match_iter_typed!(@collect, ($($vars)*), ($($args)*), ($($acc)*), ())
    };
    (@collect, ($iter:expr, $body:expr, $callback:ident, $error:ident), ($($args:tt)*), ($($acc:tt)*), ($(,)*)) => {
        match_iter!($iter; ($($acc)*) => {
            match_iter_typed!(@callback, $callback, $iter, $($args)*);
            $body
        })
    };

    // Pass the matches through the callback
    (@callback, $callback:ident, $iter:expr, $x:ident : $ty:ident $($rest:tt)*) => {
        let $x = $callback!(@type_callback, $ty, $x);
        #[allow(unused_mut)]
        let mut $x = match $x {
            Ok(x) => x,
            Err(e) => break Err(IterMatchError::Other(e)),
        };
        match_iter_typed!(@callback, $callback, $iter $($rest)*);
    };
    (@callback, $callback: ident, $iter:expr, $x:ident? : $ty:ident $($rest:tt)*) => {
        let $x = $x.map(|x| $callback!(@type_callback, $ty, x));
        #[allow(unused_mut)]
        let mut $x = match $x {
            Some(Ok(x)) => Some(x),
            Some(Err(e)) => break Err(IterMatchError::Other(e)),
            None => None,
        };
        match_iter_typed!(@callback, $callback, $iter $($rest)*);
    };
    (@callback, $callback: ident, $iter:expr, $x:ident* : $ty:ident $($rest:tt)*) => {
        let $x = $x.map(|x| $callback!(@type_callback, $ty, x)).collect();
        let $x: Vec<_> = match $x {
            Ok(x) => x,
            Err(e) => break Err(IterMatchError::Other(e)),
        };
        #[allow(unused_mut)]
        let mut $x = $x.into_iter();
        match_iter_typed!(@callback, $callback, $iter $($rest)*);
    };
    (@callback, $callback:ident, $iter:expr $(,)*) => {};

    ($callback:ident; $iter:expr; ($($args:tt)*) => $body:expr) => {
        {
            #[allow(unused_mut)]
            let mut iter = $iter;
            match_iter_typed!(@collect,
                (iter, $body, $callback, last_error),
                ($($args)*), (), ($($args)*,)
            )
        }
    };
}

/* Extends match_iter and match_iter_typed with branching.
 * Returns `Result<_, IterMatchError<_>>`; errors returned by the callback will
 * get propagated using IterMatchError::Other.
 * Allows multiple branches. The passed iterator must be Clone.
 * Will check the branches in order, testing each branch using the callback macro provided.
 *
 * Example:
 * ```
 * macro_rules! callback {
 *     (@type_callback, positive, $x:expr) => {
 *         if $x >= 0 { Ok($x) } else { Err(()) }
 *     };
 *     (@type_callback, negative, $x:expr) => {
 *         if $x <= 0 { Ok($x) } else { Err(()) }
 *     };
 *     (@type_callback, any, $x:expr) => {
 *         Ok($x)
 *     };
 *     (@branch_callback, typed, $($args:tt)*) => {
 *         match_iter_typed!(callback; $($args)*)
 *     };
 *     (@branch_callback, untyped, $($args:tt)*) => {
 *         match_iter!($($args)*)
 *     };
 * }
 *
 * let vec = vec![-1, 2, 3];
 *
 * match_iter_branching!(branch_callback; vec.into_iter();
 *     typed!(x: positive, y?: negative, z: any) => { ... },
 *     untyped!(x, y, z) => { ... },
 * )
 * ```
 *
*/
macro_rules! match_iter_branching {
    (@noclone, $callback:ident; $arg:expr; $( $submac:ident!($($args:tt)*) => $body:expr ),* $(,)*) => {
        {
            #[allow(unused_assignments)]
            let mut last_error = IterMatchError::NoMatchFound;
            // Not a real loop; used for error handling
            // Would use loop labels but they create warnings
            #[allow(unreachable_code)]
            loop {
                $(
                    let matched: Result<_, IterMatchError<_>> =
                        $callback!(@branch_callback, $submac, $arg; ($($args)*) => $body);
                    #[allow(unused_assignments)]
                    match matched {
                        Ok(v) => break Ok(v),
                        Err(e) => last_error = e,
                    };
                )*
                break Err(last_error);
            }
        }
    };
    ($callback:ident; $iter:expr; $($args:tt)*) => {
        {
            #[allow(unused_mut)]
            let mut iter = $iter;
            match_iter_branching!(@noclone, $callback; iter.clone(); $($args)*)
        }
    };
}

macro_rules! match_pair {
    (@type_callback, $ty:ident, $x:expr) => {
        $ty($x)
    };
    (@branch_callback, children, $pair:expr; $($args:tt)*) => {
        {
            #[allow(unused_mut)]
            let mut pairs = $pair.clone().into_inner();
            match_iter_typed!(match_pair; pairs; $($args)*)
        }
    };
    (@branch_callback, self, $pair:expr; ($x:ident : $ty:ident) => $body:expr) => {
        {
            let $x = match_pair!(@type_callback, $ty, $pair.clone());
            match $x {
                Ok($x) => Ok($body),
                Err(e) => Err(IterMatchError::Other(e)),
            }
        }
    };
    (@branch_callback, raw_pair, $pair:expr; ($x:ident) => $body:expr) => {
        {
            let $x = $pair.clone();
            Ok($body)
        }
    };
    (@branch_callback, captured_str, $pair:expr; ($x:ident) => $body:expr) => {
        {
            let $x = $pair.as_str();
            Ok($body)
        }
    };

    ($pair:expr; $($args:tt)*) => {
        {
            let pair = $pair;
            let result = match_iter_branching!(@noclone, match_pair; pair; $($args)*);
            result.map_err(|e| match e {
                IterMatchError::Other(e) => e,
                _ => custom_parse_error(&pair, "No match found".to_owned()),
            })
        }
    };
}

macro_rules! make_pest_parse_function {
    ($name:ident<$o:ty>; $submac:ident!( $($args:tt)* )) => (
        #[allow(unused_variables)]
        #[allow(non_snake_case)]
        fn $name<'a>(pair: Pair<'a, Rule>) -> ParseResult<$o> {
            $submac!(pair; $($args)*)
        }
    );
}

macro_rules! named {
    ($name:ident<$o:ty>; $($args:tt)*) => (
        make_pest_parse_function!($name<$o>; match_pair!( $($args)* ));
    );
}

macro_rules! rule {
    ($name:ident<$o:ty>; $($args:tt)*) => (
        make_pest_parse_function!($name<$o>; match_rule!(
            Rule::$name => match_pair!( $($args)* ),
        ));
    );
}

macro_rules! rule_group {
    ($name:ident<$o:ty>; $($ty:ident),*) => (
        make_pest_parse_function!($name<$o>; match_rule!(
            $(
                Rule::$ty => match_pair!(raw_pair!(p) => $ty(p)?),
            )*
        ));
    );
}

macro_rules! match_rule {
    ($pair:expr; $($pat:pat => $submac:ident!( $($args:tt)* ),)*) => {
        {
            #[allow(unreachable_patterns)]
            match $pair.as_rule() {
                $(
                    $pat => $submac!($pair; $($args)*),
                )*
                r => Err(custom_parse_error(&$pair, format!("Unexpected {:?}", r))),
            }
        }
    };
}

rule!(EOI<()>; children!() => ());

named!(str<&'a str>; captured_str!(s) => s.trim());

named!(raw_str<&'a str>; captured_str!(s) => s);

// TODO: parse escapes and interpolation
rule!(double_quote_literal<String>;
    children!(strs*: raw_str) => {
        strs.collect()
    }
);

rule!(single_quote_literal<String>;
    children!(eol: raw_str, contents: single_quote_continue) => {
        contents.push(eol);
        contents.into_iter().rev().collect()
    }
);
rule!(escaped_quote_pair<&'a str>;
    children!() => "''"
);
rule!(escaped_interpolation<&'a str>;
    children!() => "${"
);

rule!(single_quote_continue<Vec<&'a str>>;
    // TODO: handle interpolation
    // children!(c: expression, rest: single_quote_continue) => {
    //     rest.push(c); rest
    // },
    children!(c: escaped_quote_pair, rest: single_quote_continue) => {
        rest.push(c); rest
    },
    children!(c: escaped_interpolation, rest: single_quote_continue) => {
        rest.push(c); rest
    },
    // capture interpolation as string
    children!(c: raw_str, rest: single_quote_continue) => {
        rest.push(c); rest
    },
    children!() => {
        vec![]
    },
);

rule!(NaN_raw<()>; children!() => ());
rule!(minus_infinity_literal<()>; children!() => ());
rule!(plus_infinity_literal<()>; children!() => ());

rule!(double_literal_raw<core::Double>;
    raw_pair!(pair) => {
        pair.as_str().trim()
            .parse()
            .map_err(|e: std::num::ParseFloatError| custom_parse_error(&pair, format!("{}", e)))?
    }
);

rule!(natural_literal_raw<core::Natural>;
    raw_pair!(pair) => {
        pair.as_str().trim()
            .parse()
            .map_err(|e: std::num::ParseIntError| custom_parse_error(&pair, format!("{}", e)))?
    }
);

rule!(integer_literal_raw<core::Integer>;
    raw_pair!(pair) => {
        pair.as_str().trim()
            .parse()
            .map_err(|e: std::num::ParseIntError| custom_parse_error(&pair, format!("{}", e)))?
    }
);

rule!(path<PathBuf>;
    captured_str!(s) => (".".to_owned() + s).into()
);

rule!(parent_path<(FilePrefix, PathBuf)>;
    children!(p: path) => (FilePrefix::Parent, p)
);

rule!(here_path<(FilePrefix, PathBuf)>;
    children!(p: path) => (FilePrefix::Here, p)
);

rule!(home_path<(FilePrefix, PathBuf)>;
    children!(p: path) => (FilePrefix::Home, p)
);

rule!(absolute_path<(FilePrefix, PathBuf)>;
    children!(p: path) => (FilePrefix::Absolute, p)
);

rule_group!(local_raw<(FilePrefix, PathBuf)>;
    parent_path,
    here_path,
    home_path,
    absolute_path
);

// TODO: other import types
rule!(import_type_raw<ImportLocation>;
    // children!(_e: missing_raw) => {
    //     ImportLocation::Missing
    // }
    // children!(e: env_raw) => {
    //     ImportLocation::Env(e)
    // }
    // children!(url: http) => {
    //     ImportLocation::Remote(url)
    // }
    children!(import: local_raw) => {
        let (prefix, path) = import;
        ImportLocation::Local(prefix, path)
    }
);

rule!(import_hashed_raw<(ImportLocation, Option<()>)>;
    // TODO: handle hash
    children!(import: import_type_raw) => {
        (import, None)
    }
);

rule!(import_raw<BoxExpr<'a>>;
    // TODO: handle "as Text"
    children!(import: import_hashed_raw) => {
        let (location, hash) = import;
        bx(Expr::Embed(Import {
            mode: ImportMode::Code,
            hash,
            location,
        }))
    }
);

rule_group!(expression<BoxExpr<'a>>;
    identifier_raw,
    lambda_expression,
    ifthenelse_expression,
    let_expression,
    forall_expression,
    arrow_expression,
    merge_expression,
    empty_collection,
    non_empty_optional,

    annotated_expression,
    import_alt_expression,
    or_expression,
    plus_expression,
    text_append_expression,
    list_append_expression,
    and_expression,
    combine_expression,
    prefer_expression,
    combine_types_expression,
    times_expression,
    equal_expression,
    not_equal_expression,
    application_expression,

    import_raw,
    selector_expression_raw,
    literal_expression_raw,
    empty_record_type,
    empty_record_literal,
    non_empty_record_type_or_literal,
    union_type_or_literal,
    non_empty_list_literal_raw,
    final_expression
);

rule!(lambda_expression<BoxExpr<'a>>;
    children!(label: str, typ: expression, body: expression) => {
        bx(Expr::Lam(label, typ, body))
    }
);

rule!(ifthenelse_expression<BoxExpr<'a>>;
    children!(cond: expression, left: expression, right: expression) => {
        bx(Expr::BoolIf(cond, left, right))
    }
);

rule!(let_expression<BoxExpr<'a>>;
    children!(bindings*: let_binding, final_expr: expression) => {
        bindings.fold(final_expr, |acc, x| bx(Expr::Let(x.0, x.1, x.2, acc)))
    }
);

rule!(let_binding<(&'a str, Option<BoxExpr<'a>>, BoxExpr<'a>)>;
    children!(name: str, annot?: expression, expr: expression) => (name, annot, expr)
);

rule!(forall_expression<BoxExpr<'a>>;
    children!(label: str, typ: expression, body: expression) => {
        bx(Expr::Pi(label, typ, body))
    }
);

rule!(arrow_expression<BoxExpr<'a>>;
    children!(typ: expression, body: expression) => {
        bx(Expr::Pi("_", typ, body))
    }
);

rule!(merge_expression<BoxExpr<'a>>;
    children!(x: expression, y: expression, z?: expression) => {
        bx(Expr::Merge(x, y, z))
    }
);

rule!(empty_collection<BoxExpr<'a>>;
    children!(x: str, y: expression) => {
       match x {
          "Optional" => bx(Expr::OptionalLit(Some(y), vec![])),
          "List" => bx(Expr::ListLit(Some(y), vec![])),
          _ => unreachable!(),
       }
    }
);

rule!(non_empty_optional<BoxExpr<'a>>;
    children!(x: expression, _y: str, z: expression) => {
        bx(Expr::OptionalLit(Some(z), vec![*x]))
    }
);

macro_rules! binop {
    ($rule:ident, $op:ident) => {
        rule!($rule<BoxExpr<'a>>;
            children!(first: expression, rest*: expression) => {
                rest.fold(first, |acc, e| bx(Expr::BinOp(BinOp::$op, acc, e)))
            }
        );
    };
}

rule!(annotated_expression<BoxExpr<'a>>;
    children!(e: expression, annot: expression) => {
        bx(Expr::Annot(e, annot))
    },
    children!(e: expression) => e,
);

binop!(import_alt_expression, ImportAlt);
binop!(or_expression, BoolOr);
binop!(plus_expression, NaturalPlus);
binop!(text_append_expression, TextAppend);
binop!(list_append_expression, ListAppend);
binop!(and_expression, BoolAnd);
binop!(combine_expression, Combine);
binop!(prefer_expression, Prefer);
binop!(combine_types_expression, CombineTypes);
binop!(times_expression, NaturalTimes);
binop!(equal_expression, BoolEQ);
binop!(not_equal_expression, BoolNE);

rule!(application_expression<BoxExpr<'a>>;
    children!(first: expression, rest*: expression) => {
        rest.fold(first, |acc, e| bx(Expr::App(acc, e)))
    }
);

rule!(selector_expression_raw<BoxExpr<'a>>;
    children!(first: expression, rest*: str) => {
        rest.fold(first, |acc, e| bx(Expr::Field(acc, e)))
    }
);

rule!(literal_expression_raw<BoxExpr<'a>>;
    children!(n: double_literal_raw) => bx(Expr::DoubleLit(n)),
    children!(n: minus_infinity_literal) => bx(Expr::DoubleLit(std::f64::NEG_INFINITY)),
    children!(n: plus_infinity_literal) => bx(Expr::DoubleLit(std::f64::INFINITY)),
    children!(n: NaN_raw) => bx(Expr::DoubleLit(std::f64::NAN)),
    children!(n: natural_literal_raw) => bx(Expr::NaturalLit(n)),
    children!(n: integer_literal_raw) => bx(Expr::IntegerLit(n)),
    children!(s: double_quote_literal) => bx(Expr::TextLit(s)),
    children!(s: single_quote_literal) => bx(Expr::TextLit(s)),
    children!(e: expression) => e,
);

rule!(identifier_raw<BoxExpr<'a>>;
    children!(name: str, idx?: natural_literal_raw) => {
        match Builtin::parse(name) {
            Some(b) => bx(Expr::Builtin(b)),
            None => match name {
                "True" => bx(Expr::BoolLit(true)),
                "False" => bx(Expr::BoolLit(false)),
                "Type" => bx(Expr::Const(Const::Type)),
                "Kind" => bx(Expr::Const(Const::Kind)),
                name => bx(Expr::Var(V(name, idx.unwrap_or(0)))),
            }
        }
    }
);

rule!(empty_record_literal<BoxExpr<'a>>;
    children!() => bx(Expr::RecordLit(BTreeMap::new()))
);

rule!(empty_record_type<BoxExpr<'a>>;
    children!() => bx(Expr::Record(BTreeMap::new()))
);

rule!(non_empty_record_type_or_literal<BoxExpr<'a>>;
    children!(first_label: str, rest: non_empty_record_type) => {
        let (first_expr, mut map) = rest;
        map.insert(first_label, *first_expr);
        bx(Expr::Record(map))
    },
    children!(first_label: str, rest: non_empty_record_literal) => {
        let (first_expr, mut map) = rest;
        map.insert(first_label, *first_expr);
        bx(Expr::RecordLit(map))
    },
);

rule!(non_empty_record_type<(BoxExpr<'a>, BTreeMap<&'a str, ParsedExpr<'a>>)>;
    self!(x: partial_record_entries) => x
);

named!(partial_record_entries<(BoxExpr<'a>, BTreeMap<&'a str, ParsedExpr<'a>>)>;
    children!(expr: expression, entries*: record_entry) => {
        let mut map: BTreeMap<&str, ParsedExpr> = BTreeMap::new();
        for (n, e) in entries {
            map.insert(n, *e);
        }
        (expr, map)
    }
);

named!(record_entry<(&'a str, BoxExpr<'a>)>;
    children!(name: str, expr: expression) => (name, expr)
);

rule!(non_empty_record_literal<(BoxExpr<'a>, BTreeMap<&'a str, ParsedExpr<'a>>)>;
    self!(x: partial_record_entries) => x
);

rule!(union_type_or_literal<BoxExpr<'a>>;
    children!(_e: empty_union_type) => {
        bx(Expr::Union(BTreeMap::new()))
    },
    children!(x: non_empty_union_type_or_literal) => {
        match x {
            (Some((l, e)), entries) => bx(Expr::UnionLit(l, e, entries)),
            (None, entries) => bx(Expr::Union(entries)),
        }
    },
);

rule!(empty_union_type<()>; children!() => ());

rule!(non_empty_union_type_or_literal
      <(Option<(&'a str, BoxExpr<'a>)>, BTreeMap<&'a str, ParsedExpr<'a>>)>;
    children!(label: str, e: expression, entries: union_type_entries) => {
        (Some((label, e)), entries)
    },
    children!(l: str, e: expression, rest: non_empty_union_type_or_literal) => {
        let (x, mut entries) = rest;
        entries.insert(l, *e);
        (x, entries)
    },
    children!(l: str, e: expression) => {
        let mut entries = BTreeMap::new();
        entries.insert(l, *e);
        (None, entries)
    },
);

rule!(union_type_entries<BTreeMap<&'a str, ParsedExpr<'a>>>;
    children!(entries*: union_type_entry) => {
        let mut map: BTreeMap<&str, ParsedExpr> = BTreeMap::new();
        for (n, e) in entries {
            map.insert(n, *e);
        }
        map
    }
);

rule!(union_type_entry<(&'a str, BoxExpr<'a>)>;
    children!(name: str, expr: expression) => (name, expr)
);

rule!(non_empty_list_literal_raw<BoxExpr<'a>>;
    children!(items*: expression) => {
        bx(Expr::ListLit(None, items.map(|x| *x).collect()))
    }
);

rule!(final_expression<BoxExpr<'a>>;
    children!(e: expression, _eoi: EOI) => e
);

pub fn parse_expr<'i>(s: &'i str) -> ParseResult<BoxExpr<'i>> {
    let pairs = DhallParser::parse(Rule::final_expression, s)?;
    // Match the only item in the pairs iterator
    match_iter!(@panic; pairs; (p) => expression(p))
    // Ok(bx(Expr::BoolLit(false)))
}

// #[test]
// fn test_parse() {
//     use crate::core::BinOp::*;
//     use crate::core::Expr::*;
//     // let expr = r#"{ x = "foo", y = 4 }.x"#;
//     // let expr = r#"(1 + 2) * 3"#;
//     let expr = r#"if True then 1 + 3 * 5 else 2"#;
//     println!("{:?}", parse_expr(expr));
//     use std::thread;
//     // I don't understand why it stack overflows even on tiny expressions...
//     thread::Builder::new()
//         .stack_size(3 * 1024 * 1024)
//         .spawn(move || match parse_expr(expr) {
//             Err(e) => {
//                 println!("{:?}", e);
//                 println!("{}", e);
//             }
//             ok => println!("{:?}", ok),
//         })
//         .unwrap()
//         .join()
//         .unwrap();
//     // assert_eq!(parse_expr(expr).unwrap(), parse_expr(expr).unwrap());
//     // assert!(false);

//     println!("test {:?}", parse_expr("3 + 5 * 10"));
//     assert!(parse_expr("22").is_ok());
//     assert!(parse_expr("(22)").is_ok());
//     assert_eq!(
//         parse_expr("3 + 5 * 10").ok(),
//         Some(Box::new(BinOp(
//             NaturalPlus,
//             Box::new(NaturalLit(3)),
//             Box::new(BinOp(
//                 NaturalTimes,
//                 Box::new(NaturalLit(5)),
//                 Box::new(NaturalLit(10))
//             ))
//         )))
//     );
//     // The original parser is apparently right-associative
//     assert_eq!(
//         parse_expr("2 * 3 * 4").ok(),
//         Some(Box::new(BinOp(
//             NaturalTimes,
//             Box::new(NaturalLit(2)),
//             Box::new(BinOp(
//                 NaturalTimes,
//                 Box::new(NaturalLit(3)),
//                 Box::new(NaturalLit(4))
//             ))
//         )))
//     );
//     assert!(parse_expr("((((22))))").is_ok());
//     assert!(parse_expr("((22)").is_err());
//     println!("{:?}", parse_expr("\\(b : Bool) -> b == False"));
//     assert!(parse_expr("\\(b : Bool) -> b == False").is_ok());
//     println!("{:?}", parse_expr("foo.bar"));
//     assert!(parse_expr("foo.bar").is_ok());
//     assert!(parse_expr("[] : List Bool").is_ok());

//     // println!("{:?}", parse_expr("< Left = True | Right : Natural >"));
//     // println!("{:?}", parse_expr(r#""bl${42}ah""#));
//     // assert!(parse_expr("< Left = True | Right : Natural >").is_ok());
// }