1
//! The code inside a quasiquote template: its unquoted forms, wherever they
2
//! sit, including under a quote that is itself template text.
3

            
4
use crate::ast::Expr;
5
use crate::error::Result;
6

            
7
impl Expr {
8
    /// The forms the quasiquote template `self` runs: its unquotes and
9
    /// unquote-splicings at the template's own depth, in order.
10
    #[must_use]
11
1418375
    pub fn unquoted_forms(&self) -> Vec<&Expr> {
12
1418375
        let mut forms = Vec::new();
13
1418375
        self.collect_unquoted(1, &mut forms);
14
1418375
        forms
15
1418375
    }
16

            
17
13174442
    fn collect_unquoted<'a>(&'a self, depth: usize, forms: &mut Vec<&'a Expr>) {
18
13174442
        match self {
19
1628121
            Expr::Unquote(inner) | Expr::UnquoteSplicing(inner) if depth == 1 => forms.push(inner),
20
            Expr::Unquote(inner) | Expr::UnquoteSplicing(inner) => {
21
815289
                inner.collect_unquoted(depth - 1, forms);
22
            }
23
810360
            Expr::Quasiquote(inner) => inner.collect_unquoted(depth + 1, forms),
24
4265
            Expr::Quote(inner) | Expr::Datum(inner) => inner.collect_unquoted(depth, forms),
25
4099785
            Expr::List(elems) => elems
26
4099785
                .iter()
27
10126151
                .for_each(|elem| elem.collect_unquoted(depth, forms)),
28
            Expr::Cons(car, cdr) => {
29
1
                car.collect_unquoted(depth, forms);
30
1
                cdr.collect_unquoted(depth, forms);
31
            }
32
6273677
            _ => {}
33
        }
34
13174442
    }
35

            
36
    /// The template `self` with each of its [`Self::unquoted_forms`] replaced
37
    /// by `rewrite` of it.
38
617
    pub fn map_unquoted(&self, rewrite: &mut impl FnMut(&Expr) -> Result<Expr>) -> Result<Expr> {
39
617
        self.map_at_depth(1, rewrite)
40
617
    }
41

            
42
2367
    fn map_at_depth(
43
2367
        &self,
44
2367
        depth: usize,
45
2367
        rewrite: &mut impl FnMut(&Expr) -> Result<Expr>,
46
2367
    ) -> Result<Expr> {
47
1750
        let mut inner = |expr: &Expr, depth| expr.map_at_depth(depth, rewrite);
48
2367
        match self {
49
618
            Expr::Unquote(form) if depth == 1 => Ok(Expr::Unquote(Box::new(rewrite(form)?))),
50
33
            Expr::UnquoteSplicing(form) if depth == 1 => {
51
33
                Ok(Expr::UnquoteSplicing(Box::new(rewrite(form)?)))
52
            }
53
            Expr::Unquote(form) => Ok(Expr::Unquote(Box::new(inner(form, depth - 1)?))),
54
            Expr::UnquoteSplicing(form) => {
55
                Ok(Expr::UnquoteSplicing(Box::new(inner(form, depth - 1)?)))
56
            }
57
            Expr::Quasiquote(form) => Ok(Expr::Quasiquote(Box::new(inner(form, depth + 1)?))),
58
504
            Expr::Quote(form) => Ok(Expr::Quote(Box::new(inner(form, depth)?))),
59
            Expr::Datum(form) => Ok(Expr::Datum(Box::new(inner(form, depth)?))),
60
624
            Expr::List(elems) => elems
61
624
                .iter()
62
1246
                .map(|elem| inner(elem, depth))
63
                .collect::<Result<_>>()
64
624
                .map(Expr::List),
65
            Expr::Cons(car, cdr) => Ok(Expr::Cons(
66
                Box::new(inner(car, depth)?),
67
                Box::new(inner(cdr, depth)?),
68
            )),
69
588
            _ => Ok(self.clone()),
70
        }
71
2367
    }
72
}
73

            
74
#[cfg(test)]
75
#[path = "ast_template_tests.rs"]
76
mod tests;