grit_pattern_matcher/pattern/
file_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
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
use super::{
    patterns::{Matcher, Pattern},
    resolved_pattern::ResolvedPattern,
    state::State,
};
use crate::{
    constants::{ABSOLUTE_PATH_INDEX, FILENAME_INDEX, GLOBAL_VARS_SCOPE_INDEX, PROGRAM_INDEX},
    context::ExecContext,
};
use crate::{context::QueryContext, pattern::resolved_pattern::File};
use grit_util::{error::GritResult, AnalysisLogs};

#[derive(Debug, Clone)]
pub struct FilePattern<Q: QueryContext> {
    pub name: Pattern<Q>,
    pub body: Pattern<Q>,
}

impl<Q: QueryContext> FilePattern<Q> {
    pub fn new(name: Pattern<Q>, body: Pattern<Q>) -> Self {
        Self { name, body }
    }
}

impl<Q: QueryContext> Matcher<Q> for FilePattern<Q> {
    fn execute<'a>(
        &'a self,
        resolved_pattern: &Q::ResolvedPattern<'a>,
        state: &mut State<'a, Q>,
        context: &'a Q::ExecContext<'a>,
        logs: &mut AnalysisLogs,
    ) -> GritResult<bool> {
        let Some(file) = resolved_pattern.get_file() else {
            return Ok(false);
        };

        let name = file.name(&state.files);

        if !self.name.execute(&name, state, context, logs)? {
            return Ok(false);
        }

        // If the file isn't loaded yet, we must load it now
        if !context.load_file(file, state, logs)? {
            // The file wasn't loaded, so we can't match the body
            return Ok(false);
        }

        // Re-execute the name pattern to bind the name variable
        self.name
            .execute(&file.name(&state.files), state, context, logs)?;

        // Fill in the variables now - this is a bit of a hack
        state.bindings[GLOBAL_VARS_SCOPE_INDEX as usize]
            .last_mut()
            .unwrap()[PROGRAM_INDEX]
            .value = Some(file.binding(&state.files));
        state.bindings[GLOBAL_VARS_SCOPE_INDEX as usize]
            .last_mut()
            .unwrap()[FILENAME_INDEX]
            .value = Some(file.name(&state.files));
        state.bindings[GLOBAL_VARS_SCOPE_INDEX as usize]
            .last_mut()
            .unwrap()[ABSOLUTE_PATH_INDEX]
            .value = Some(file.name(&state.files));
        state.bindings[GLOBAL_VARS_SCOPE_INDEX as usize]
            .last_mut()
            .unwrap()[ABSOLUTE_PATH_INDEX]
            .value = Some(file.absolute_path(&state.files, context.language())?);

        if !self
            .body
            .execute(&file.binding(&state.files), state, context, logs)?
        {
            return Ok(false);
        }

        Ok(true)
    }
}