grit_pattern_matcher/pattern/
variable.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use super::{
    container::{PatternOrResolved, PatternOrResolvedMut},
    patterns::{Matcher, PatternName},
    resolved_pattern::ResolvedPattern,
    State,
};
use crate::{
    binding::Binding,
    constants::{ABSOLUTE_PATH_INDEX, DEFAULT_FILE_NAME, FILENAME_INDEX, GLOBAL_VARS_SCOPE_INDEX},
    context::{ExecContext, QueryContext},
};
use core::fmt::Debug;
use grit_util::{
    constants::GRIT_METAVARIABLE_PREFIX,
    error::{GritPatternError, GritResult},
    AnalysisLogs, ByteRange, Language,
};
use std::{
    borrow::Cow,
    collections::BTreeSet,
    sync::{Arc, RwLock},
};

#[derive(Debug, Clone, Copy)]
pub(crate) struct VariableScope {
    pub(crate) scope: u16,
    pub(crate) index: u16,
}

impl VariableScope {
    pub fn new(scope: usize, index: usize) -> Self {
        Self {
            scope: scope as u16,
            index: index as u16,
        }
    }
}

#[derive(Debug, Clone)]
struct DynamicVariableInternal {
    name: String,
    /// Track the last scope we registed this variable in
    /// This is mainly just used for cases where we don't have state available
    last_scope: Arc<RwLock<Option<VariableScope>>>,
}

#[derive(Debug, Clone)]
enum VariableInternal {
    /// Static variable, which is bound at compile time (ex. global variables).
    /// These are slightly more efficient, and follow the traditional approach in Grit.
    /// However, they require more direct control over scopes and indexes.
    Static(VariableScope),
    /// Dynamic variables are lazy, so we just need to register them by name.
    /// They will then automatically be bound to the first scope that attempts to use them.
    /// This should be avoided where possible, since it means names will likely overwrite each other across scopes.
    Dynamic(DynamicVariableInternal),
}

#[derive(Clone, Debug)]
pub struct Variable {
    internal: VariableInternal,
}

/// VariableSource is used to track the origin of a variable
/// It can come from
#[derive(Debug, Clone)]
pub enum VariableSource {
    /// Compiled from a pattern
    Compiled {
        name: String,
        file: String,
        locations: BTreeSet<ByteRange>,
    },
    /// Global variable, which is not defined anywhere
    Global { name: String },
}

impl VariableSource {
    pub fn new(name: String, file: String) -> Self {
        Self::Compiled {
            name,
            file,
            locations: BTreeSet::new(),
        }
    }

    pub fn new_global(name: String) -> Self {
        Self::Global { name }
    }

    /// Register a location in a GritQL file where a variable is referenced
    pub fn register_location(&mut self, location: ByteRange) -> GritResult<()> {
        match self {
            VariableSource::Compiled { locations, .. } => {
                locations.insert(location);
                Ok(())
            }
            VariableSource::Global { .. } => Ok(()),
        }
    }

    /// Get locations where the variable is referenced from the main pattern file
    pub fn get_main_locations(&self) -> Vec<ByteRange> {
        if let VariableSource::Compiled {
            locations, file, ..
        } = self
        {
            if file != DEFAULT_FILE_NAME {
                return vec![];
            }
            locations.iter().cloned().collect()
        } else {
            vec![]
        }
    }

    /// Get the registered variable name
    pub fn name(&self) -> &str {
        match self {
            VariableSource::Compiled { name, .. } => name,
            VariableSource::Global { name } => name,
        }
    }
}

struct VariableMirror<'a, Q: QueryContext> {
    scope: u16,
    index: u16,
    binding: Q::Binding<'a>,
}

impl Variable {
    /// Create a variable, where we already know the scope and index it will be bound to
    pub fn new(scope: usize, index: usize) -> Self {
        Self {
            internal: VariableInternal::Static(VariableScope {
                scope: scope as u16,
                index: index as u16,
            }),
        }
    }

    /// Create a dynamic variable, which will be bound to the first scope that uses it
    ///
    /// Warning: this is not stable or tested yet. This implementation is still incomplete.
    pub fn new_dynamic(name: &str) -> Self {
        Self {
            internal: VariableInternal::Dynamic(DynamicVariableInternal {
                name: name.to_string(),
                last_scope: Arc::new(RwLock::new(None)),
            }),
        }
    }

    fn try_internal(&self) -> GritResult<VariableScope> {
        match &self.internal {
            VariableInternal::Static(scope) => Ok(*scope),
            VariableInternal::Dynamic(lock) => {
                if let Ok(reader) = lock.last_scope.try_read() {
                    if let Some(scope) = *reader {
                        return Ok(scope);
                    }
                }
                Err(GritPatternError::new_matcher(format!(
                    "variable {} not initialized",
                    lock.name
                )))
            }
        }
    }

    fn get_internal<Q: QueryContext>(&self, state: &mut State<'_, Q>) -> GritResult<VariableScope> {
        match &self.internal {
            VariableInternal::Static(internal) => Ok(*internal),
            VariableInternal::Dynamic(lock) => {
                let scope = state.register_var(&lock.name);
                if let Ok(mut writer) = lock.last_scope.write() {
                    *writer = Some(scope);
                }
                Ok(scope)
            }
        }
    }

    /// Try to get the scope of the variable, if it has been bound to a scope.
    /// If the variable has not been bound to a scope, return an error.
    /// When possible, prefer to use `get_scope()` instead, which will initialize the variable's scope if it is not already bound.
    pub fn try_scope(&self) -> GritResult<u16> {
        Ok(self.try_internal()?.scope)
    }

    /// Try to get the index of the variable, if it has been bound to an index.
    /// If the variable has not been bound to an index, return an error.
    /// When possible, prefer to use `get_index()` instead, which will initialize the variable's index if it is not already bound.
    pub fn try_index(&self) -> GritResult<u16> {
        Ok(self.try_internal()?.index)
    }

    /// Get the scope of the variable, initializing it if it is not already bound.
    pub fn get_scope<Q: QueryContext>(&self, state: &mut State<'_, Q>) -> GritResult<u16> {
        Ok(self.get_internal(state)?.scope)
    }

    /// Get the index of the variable, initializing it if it is not already bound.
    pub fn get_index<Q: QueryContext>(&self, state: &mut State<'_, Q>) -> GritResult<u16> {
        Ok(self.get_internal(state)?.index)
    }

    pub fn get_pattern_or_resolved<'a, 'b, Q: QueryContext>(
        &self,
        state: &'b State<'a, Q>,
    ) -> GritResult<Option<PatternOrResolved<'a, 'b, Q>>> {
        let v = state.trace_var(self);
        let content = &state.bindings[v.try_scope().unwrap() as usize]
            .last()
            .unwrap()[v.try_index().unwrap() as usize];
        if let Some(pattern) = content.pattern {
            Ok(Some(PatternOrResolved::Pattern(pattern)))
        } else if let Some(resolved) = &content.value {
            Ok(Some(PatternOrResolved::Resolved(resolved)))
        } else {
            Ok(None)
        }
    }
    pub fn get_pattern_or_resolved_mut<'a, 'b, Q: QueryContext>(
        &self,
        state: &'b mut State<'a, Q>,
    ) -> GritResult<Option<PatternOrResolvedMut<'a, 'b, Q>>> {
        let v = state.trace_var_mut(self);
        let content = &mut state.bindings[v.try_scope().unwrap() as usize]
            .last_mut()
            .unwrap()[v.try_index().unwrap() as usize];
        if let Some(pattern) = content.pattern {
            Ok(Some(PatternOrResolvedMut::Pattern(pattern)))
        } else if let Some(resolved) = &mut content.value {
            Ok(Some(PatternOrResolvedMut::Resolved(resolved)))
        } else {
            Ok(None)
        }
    }

    pub fn file_name() -> Self {
        Self::new(GLOBAL_VARS_SCOPE_INDEX.into(), FILENAME_INDEX)
    }

    pub fn is_file_name(&self) -> bool {
        let VariableInternal::Static(scope) = &self.internal else {
            return false;
        };
        scope.scope == GLOBAL_VARS_SCOPE_INDEX && scope.index as usize == FILENAME_INDEX
    }

    pub fn text<'a, Q: QueryContext>(
        &self,
        state: &State<'a, Q>,
        lang: &Q::Language<'a>,
    ) -> GritResult<Cow<'a, str>> {
        state.bindings[self.try_scope().unwrap() as usize]
            .last()
            .unwrap()[self.try_index().unwrap() as usize]
            .text(state, lang)
    }

    fn execute_resolved<'a, Q: QueryContext>(
        &self,
        resolved_pattern: &Q::ResolvedPattern<'a>,
        state: &mut State<'a, Q>,
        language: &Q::Language<'a>,
    ) -> GritResult<Option<bool>> {
        let mut variable_mirrors: Vec<VariableMirror<Q>> = Vec::new();
        {
            let scope = self.get_scope(state)?;
            let index = self.get_index(state)?;
            let variable_content = state
                .bindings
                .get_mut(scope as usize)
                .unwrap()
                .last_mut()
                .unwrap()
                .get_mut(index as usize);
            let Some(variable_content) = variable_content else {
                return Ok(None);
            };
            let variable_content = &mut **(variable_content);
            let value = &mut variable_content.value;

            if let Some(var_side_resolve_pattern) = value {
                if let (Some(var_binding), Some(binding)) = (
                    var_side_resolve_pattern.get_last_binding(),
                    resolved_pattern.get_last_binding(),
                ) {
                    if !var_binding.is_equivalent_to(binding, language) {
                        return Ok(Some(false));
                    }
                    let value_history = &mut variable_content.value_history;
                    var_side_resolve_pattern.push_binding(binding.clone())?;

                    // feels wrong maybe we should push ResolvedPattern::Binding(bindings)?
                    value_history.push(ResolvedPattern::from_binding(binding.clone()));
                    variable_mirrors.extend(variable_content.mirrors.iter().map(|mirror| {
                        VariableMirror {
                            scope: mirror.try_scope().unwrap(),
                            index: mirror.try_index().unwrap(),
                            binding: binding.clone(),
                        }
                    }));
                } else {
                    return Ok(Some(
                        resolved_pattern.text(&state.files, language)?
                            == var_side_resolve_pattern.text(&state.files, language)?,
                    ));
                }
            } else {
                return Ok(None);
            };
        }
        for mirror in variable_mirrors {
            let mirror_content = &mut **(state
                .bindings
                .get_mut(mirror.scope as usize)
                .unwrap()
                .last_mut()
                .unwrap()
                .get_mut(mirror.index as usize)
                .unwrap());
            if let Some(value) = &mut mirror_content.value {
                if value.is_binding() {
                    value.push_binding(mirror.binding.clone())?;
                    let value_history = &mut mirror_content.value_history;
                    value_history.push(ResolvedPattern::from_binding(mirror.binding));
                }
            }
        }
        Ok(Some(true))
    }
}

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

impl<Q: QueryContext> Matcher<Q> for Variable {
    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> {
        if let Some(res) = self.execute_resolved(resolved_pattern, state, context.language())? {
            return Ok(res);
        }
        // we only check the assignment if the variable is not bound already
        // otherwise, we assume that the assignment is correct

        // we do this convoluted check to avoid double-borrowing of state
        // via the variable_content variable
        let scope = self.get_scope(state)?;
        let index = self.get_index(state)?;

        let variable_content = state
            .bindings
            .get_mut(scope as usize)
            .unwrap()
            .last_mut()
            .unwrap()
            .get_mut(index as usize);
        let Some(variable_content) = variable_content else {
            logs.add_warning(
                None,
                format!("Variable unexpectedly not found in scope {:?}", scope),
            );
            return Ok(false);
        };

        let variable_content = &mut **(variable_content);
        if let Some(pattern) = variable_content.pattern {
            if !pattern.execute(resolved_pattern, state, context, logs)? {
                return Ok(false);
            }
        }
        let variable_content = &mut **(state
            .bindings
            .get_mut(scope as usize)
            .unwrap()
            .last_mut()
            .unwrap()
            .get_mut(index as usize)
            .unwrap());
        variable_content.value = Some(resolved_pattern.clone());
        variable_content
            .value_history
            .push(resolved_pattern.clone());
        Ok(true)
    }
}

pub fn get_absolute_file_name<'a, Q: QueryContext>(
    state: &State<'a, Q>,
    lang: &Q::Language<'a>,
) -> GritResult<String> {
    let file = state.bindings[GLOBAL_VARS_SCOPE_INDEX as usize]
        .last()
        .unwrap()[ABSOLUTE_PATH_INDEX]
        .value
        .as_ref();
    let file = file
        .map(|f| f.text(&state.files, lang).map(|s| s.to_string()))
        .unwrap_or(Ok("No File Found".to_string()))?;
    Ok(file)
}

pub fn get_file_name<'a, Q: QueryContext>(
    state: &State<'a, Q>,
    lang: &Q::Language<'a>,
) -> GritResult<String> {
    let file = state.bindings[GLOBAL_VARS_SCOPE_INDEX as usize]
        .last()
        .unwrap()[FILENAME_INDEX]
        .value
        .as_ref();
    let file = file
        .map(|f| f.text(&state.files, lang).map(|s| s.to_string()))
        .unwrap_or(Ok("No File Found".to_string()))?;
    Ok(file)
}

pub fn is_reserved_metavariable(var: &str, lang: Option<&impl Language>) -> bool {
    let name = var.trim_start_matches(GRIT_METAVARIABLE_PREFIX);
    let name = if let Some(lang) = lang {
        name.trim_start_matches(lang.metavariable_prefix_substitute())
    } else {
        name
    };
    name == "match"
        || name == "filename"
        || name == "absolute_filename"
        || name == "new_files"
        || name == "program"
        || name.starts_with("grit_")
}