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
use thiserror::Error;
use tree_sitter::CaptureQuantifier;
use tree_sitter::QueryMatch;
use tree_sitter::Tree;
use crate::ast::File;
use crate::execution::error::ExecutionError;
use crate::functions::Functions;
use crate::graph::Graph;
use crate::graph::Value;
use crate::variables::Globals;
use crate::Identifier;
pub(crate) mod error;
mod lazy;
mod strict;
impl File {
pub fn execute<'a, 'tree>(
&self,
tree: &'tree Tree,
source: &'tree str,
config: &mut ExecutionConfig,
cancellation_flag: &dyn CancellationFlag,
) -> Result<Graph<'tree>, ExecutionError> {
let mut graph = Graph::new();
self.execute_into(&mut graph, tree, source, config, cancellation_flag)?;
Ok(graph)
}
pub fn execute_into<'a, 'tree>(
&self,
graph: &mut Graph<'tree>,
tree: &'tree Tree,
source: &'tree str,
config: &mut ExecutionConfig,
cancellation_flag: &dyn CancellationFlag,
) -> Result<(), ExecutionError> {
if config.lazy {
self.execute_lazy_into(graph, tree, source, config, cancellation_flag)
} else {
self.execute_strict_into(graph, tree, source, config, cancellation_flag)
}
}
pub(self) fn check_globals(&self, globals: &Globals) -> Result<(), ExecutionError> {
for global in &self.globals {
match globals.get(&global.name) {
None => {
return Err(ExecutionError::MissingGlobalVariable(
global.name.as_str().to_string(),
));
}
Some(value) => {
if global.quantifier == CaptureQuantifier::ZeroOrMore
|| global.quantifier == CaptureQuantifier::OneOrMore
{
if value.as_list().is_err() {
return Err(ExecutionError::ExpectedList(
global.name.as_str().to_string(),
));
}
}
}
}
}
Ok(())
}
}
pub struct ExecutionConfig<'a, 'g> {
pub(crate) functions: &'a Functions,
pub(crate) globals: &'a Globals<'g>,
pub(crate) lazy: bool,
pub(crate) location_attr: Option<Identifier>,
pub(crate) variable_name_attr: Option<Identifier>,
}
impl<'a, 'g> ExecutionConfig<'a, 'g> {
pub fn new(functions: &'a Functions, globals: &'a Globals<'g>) -> Self {
Self {
functions,
globals,
lazy: false,
location_attr: None,
variable_name_attr: None,
}
}
pub fn debug_attributes(
self,
location_attr: Identifier,
variable_name_attr: Identifier,
) -> Self {
Self {
functions: self.functions,
globals: self.globals,
lazy: self.lazy,
location_attr: location_attr.into(),
variable_name_attr: variable_name_attr.into(),
}
}
pub fn lazy(self, lazy: bool) -> Self {
Self {
functions: self.functions,
globals: self.globals,
lazy,
location_attr: self.location_attr,
variable_name_attr: self.variable_name_attr,
}
}
}
pub trait CancellationFlag {
fn check(&self, at: &'static str) -> Result<(), CancellationError>;
}
pub struct NoCancellation;
impl CancellationFlag for NoCancellation {
fn check(&self, _at: &'static str) -> Result<(), CancellationError> {
Ok(())
}
}
#[derive(Debug, Error)]
#[error("Cancelled at \"{0}\"")]
pub struct CancellationError(pub &'static str);
pub(self) fn query_capture_value<'tree>(
index: usize,
quantifier: CaptureQuantifier,
mat: &QueryMatch<'_, 'tree>,
graph: &mut Graph<'tree>,
) -> Value {
let mut nodes = mat
.captures
.iter()
.filter(|c| c.index as usize == index)
.map(|c| c.node);
match quantifier {
CaptureQuantifier::Zero => panic!("Capture with quantifier 0 has no value"),
CaptureQuantifier::One => {
let syntax_node = graph.add_syntax_node(nodes.next().unwrap());
syntax_node.into()
}
CaptureQuantifier::ZeroOrMore | CaptureQuantifier::OneOrMore => {
let syntax_nodes = nodes
.map(|n| graph.add_syntax_node(n.clone()).into())
.collect::<Vec<Value>>();
syntax_nodes.into()
}
CaptureQuantifier::ZeroOrOne => match nodes.next() {
None => Value::Null.into(),
Some(node) => {
let syntax_node = graph.add_syntax_node(node);
syntax_node.into()
}
},
}
}