summaryrefslogtreecommitdiff
path: root/src/chomp/check/deepen.rs
blob: b9f606ddd070c32e5ee28dbc43414efb0a23e6d6 (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
use super::super::{
    ast::{Alt, Call, Cat, Epsilon, Fix, Literal, Parameter, Variable},
    visit::{Mapper, Visitable},
};

#[derive(Clone, Copy, Debug, Default)]
pub struct DeepenVars {
    depth: usize,
}

impl Mapper for DeepenVars {
    type Out = ();

    fn map_epsilon(&mut self, _: &mut Epsilon) -> Self::Out {}

    fn map_literal(&mut self, _: &mut Literal) -> Self::Out {}

    fn map_cat(&mut self, cat: &mut Cat) -> Self::Out {
        cat.first_mut().map(self);
        cat.second_mut().map(self);
    }

    fn map_alt(&mut self, alt: &mut Alt) -> Self::Out {
        alt.left_mut().map(self);
        alt.right_mut().map(self);
    }

    fn map_fix(&mut self, fix: &mut Fix) -> Self::Out {
        self.depth += 1;
        fix.inner_mut().map(self);
        self.depth -= 1;
    }

    fn map_variable(&mut self, bind: &mut Variable) -> Self::Out {
        if bind.index() >= self.depth {
            *bind.index_mut() += 1;
        }
    }

    fn map_parameter(&mut self, _param: &mut Parameter) -> Self::Out {}

    fn map_call(&mut self, call: &mut Call) -> Self::Out {
        for arg in call.args_mut() {
            arg.map(self);
        }
    }
}