grit_pattern_matcher/pattern/
call_built_in.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
use super::{
    functions::GritCall,
    patterns::{Pattern, PatternName},
    State,
};
use crate::context::{ExecContext, QueryContext};
use grit_util::{error::GritResult, AnalysisLogs};

// todo we can probably use a macro to generate a function that takes a vec and
// and calls the input function with the vec args unpacked.

#[derive(Debug, Clone)]
pub struct CallBuiltIn<Q: QueryContext> {
    pub index: usize,
    pub name: String,
    pub args: Vec<Option<Pattern<Q>>>,
}

impl<Q: QueryContext> CallBuiltIn<Q> {
    pub fn new(index: usize, name: &str, args: Vec<Option<Pattern<Q>>>) -> Self {
        Self {
            index,
            name: name.to_string(),
            args,
        }
    }
}

impl<Q: QueryContext> GritCall<Q> for CallBuiltIn<Q> {
    fn call<'a>(
        &'a self,
        state: &mut State<'a, Q>,
        context: &'a Q::ExecContext<'a>,
        logs: &mut AnalysisLogs,
    ) -> GritResult<Q::ResolvedPattern<'a>> {
        context.call_built_in(self, context, state, logs)
    }
}

impl<Q: QueryContext> PatternName for CallBuiltIn<Q> {
    fn name(&self) -> &'static str {
        "CALL_BUILT_IN"
    }
}