grit_pattern_matcher/pattern/
container.rs

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
use grit_util::error::{GritPatternError, GritResult};

use super::{
    accessor::Accessor, list_index::ListIndex, patterns::Pattern, state::State, variable::Variable,
    CallBuiltIn,
};
use crate::context::QueryContext;

/// A `Container` represents anything which "contains" a reference to a Pattern.
///
/// We have three types of containers:
/// - Variable: a variable reference (ex. `$foo`)
/// - Accessor: a map accessor (ex. `$foo.bar`)
/// - ListIndex: a list index (ex. `$foo[0]`)
#[derive(Debug, Clone)]
pub enum Container<Q: QueryContext> {
    Variable(Variable),
    Accessor(Box<Accessor<Q>>),
    ListIndex(Box<ListIndex<Q>>),
    FunctionCall(Box<CallBuiltIn<Q>>),
}

#[derive(Debug)]
pub enum PatternOrResolved<'a, 'b, Q: QueryContext> {
    Pattern(&'a Pattern<Q>),
    Resolved(&'b Q::ResolvedPattern<'a>),
    ResolvedBinding(Q::ResolvedPattern<'a>),
}

#[derive(Debug)]
pub enum PatternOrResolvedMut<'a, 'b, Q: QueryContext> {
    Pattern(&'a Pattern<Q>),
    Resolved(&'b mut Q::ResolvedPattern<'a>),
    _ResolvedBinding,
}

impl<Q: QueryContext> Container<Q> {
    pub fn set_resolved<'a>(
        &'a self,
        state: &mut State<'a, Q>,
        lang: &Q::Language<'a>,
        value: Q::ResolvedPattern<'a>,
    ) -> GritResult<bool> {
        match self {
            Container::Variable(v) => {
                let var = state.trace_var_mut(v);
                let content = &mut state.bindings[var.try_scope().unwrap() as usize]
                    .last_mut()
                    .unwrap()[var.try_index().unwrap() as usize];
                match content.pattern {
                    Some(Pattern::Accessor(a)) => a.set_resolved(state, lang, value),
                    Some(Pattern::ListIndex(l)) => l.set_resolved(state, lang, value),
                    None | Some(_) => {
                        content.set_value(value);
                        Ok(true)
                    }
                }
            }
            Container::Accessor(a) => a.set_resolved(state, lang, value),
            Container::ListIndex(l) => l.set_resolved(state, lang, value),
            Container::FunctionCall(f) => Err(GritPatternError::new(format!(
                "You cannot assign to the result of a function call: {:?}",
                f
            ))),
        }
    }

    pub fn get_pattern_or_resolved<'a, 'b>(
        &'a self,
        state: &'b State<'a, Q>,
        lang: &Q::Language<'a>,
    ) -> GritResult<Option<PatternOrResolved<'a, 'b, Q>>> {
        match self {
            Container::Variable(v) => v.get_pattern_or_resolved(state),
            Container::Accessor(a) => a.get(state, lang),
            Container::ListIndex(a) => a.get(state, lang),
            Container::FunctionCall(f) => Err(GritPatternError::new(format!(
                "You cannot get the value of a function call: {:?}",
                f
            ))),
        }
    }

    pub fn get_pattern_or_resolved_mut<'a, 'b>(
        &'a self,
        state: &'b mut State<'a, Q>,
        lang: &Q::Language<'a>,
    ) -> GritResult<Option<PatternOrResolvedMut<'a, 'b, Q>>> {
        match self {
            Container::Variable(v) => v.get_pattern_or_resolved_mut(state),
            Container::Accessor(a) => a.get_mut(state, lang),
            Container::ListIndex(l) => l.get_mut(state, lang),
            Container::FunctionCall(f) => Err(GritPatternError::new(format!(
                "You cannot get the value of a function call: {:?}",
                f
            ))),
        }
    }
}