grit_pattern_matcher/pattern/
callback_pattern.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
use super::{Matcher, PatternName, State};
use crate::context::ExecContext;
use crate::context::QueryContext;
use grit_util::{error::GritResult, AnalysisLogs};
use std::fmt::Debug;

/// A callback pattern is used to reference against a callback function.
/// The actual callback function is stored in the context, just the index is provided here.
/// This is useful for adding new user-defined functions in Rust.
pub struct CallbackPattern {
    pub callback_index: usize,
}

impl Clone for CallbackPattern {
    fn clone(&self) -> Self {
        Self {
            callback_index: self.callback_index,
        }
    }
}

impl CallbackPattern {
    pub fn new(callback_index: usize) -> Self {
        Self { callback_index }
    }
}

impl PatternName for CallbackPattern {
    fn name(&self) -> &'static str {
        "CALLBACK_PATTERN"
    }
}

impl Debug for CallbackPattern {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "CallbackPattern")
    }
}

impl<Q: QueryContext> Matcher<Q> for CallbackPattern {
    fn execute<'a>(
        &'a self,
        binding: &Q::ResolvedPattern<'a>,
        state: &mut State<'a, Q>,
        context: &'a Q::ExecContext<'a>,
        logs: &mut AnalysisLogs,
    ) -> GritResult<bool> {
        context.call_callback(self, context, binding, state, logs)
    }
}