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
use crate::language::Language;
use crate::match_tree::{extract_var_from_node, match_end_non_recursive, match_node_non_recursive};
use crate::matcher::{KindMatcher, KindMatcherError, Matcher};
use crate::ts_parser::TSParseError;
use crate::{meta_var::MetaVarEnv, Node, Root};
use bit_set::BitSet;
use thiserror::Error;
#[derive(Clone)]
enum PatternStyle<L: Language> {
Single,
Selector(KindMatcher<L>),
}
#[derive(Clone)]
pub struct Pattern<L: Language> {
pub(crate) root: Root<L>,
style: PatternStyle<L>,
}
#[derive(Debug, Error)]
pub enum PatternError {
#[error("Tree-Sitter fails to parse the pattern.")]
TSParse(#[from] TSParseError),
#[error("No AST root is detected. Please check the pattern source `{0}`.")]
NoContent(String),
#[error("Multiple AST nodes are detected. Please check the pattern source `{0}`.")]
MultipleNode(String),
#[error(transparent)]
InvalidKind(#[from] KindMatcherError),
#[error("Fails to create Contextual pattern: selector `{selector}` matches no node in the context `{context}`.")]
NoSelectorInContext { context: String, selector: String },
}
#[inline]
fn is_single_node(n: &tree_sitter::Node) -> bool {
match n.child_count() {
1 => true,
2 => n.child(1).expect("second child must exist").is_missing(),
_ => false,
}
}
impl<L: Language> Pattern<L> {
pub fn try_new(src: &str, lang: L) -> Result<Self, PatternError> {
let processed = lang.pre_process_pattern(src);
let root = Root::try_new(&processed, lang)?;
let goal = root.root();
if goal.inner.child_count() == 0 {
return Err(PatternError::NoContent(src.into()));
}
if !is_single_node(&goal.inner) {
return Err(PatternError::MultipleNode(src.into()));
}
Ok(Self {
root,
style: PatternStyle::Single,
})
}
pub fn new(src: &str, lang: L) -> Self {
Self::try_new(src, lang).unwrap()
}
pub fn contextual(context: &str, selector: &str, lang: L) -> Result<Self, PatternError> {
let processed = lang.pre_process_pattern(context);
let root = Root::try_new(&processed, lang.clone())?;
let goal = root.root();
let kind_matcher = KindMatcher::try_new(selector, lang)?;
if goal.find(&kind_matcher).is_none() {
return Err(PatternError::NoSelectorInContext {
context: context.into(),
selector: selector.into(),
});
}
Ok(Self {
root,
style: PatternStyle::Selector(kind_matcher),
})
}
fn single_matcher(&self) -> Node<L> {
debug_assert!(matches!(self.style, PatternStyle::Single));
let root = self.root.root();
let mut node = root.inner;
while is_single_node(&node) {
node = node.child(0).unwrap();
}
Node {
inner: node,
root: &self.root,
}
}
fn kind_matcher(&self, kind_matcher: &KindMatcher<L>) -> Node<L> {
debug_assert!(matches!(self.style, PatternStyle::Selector(_)));
self
.root
.root()
.find(kind_matcher)
.map(Node::from)
.expect("contextual match should succeed")
}
}
impl<L: Language> Matcher<L> for Pattern<L> {
fn match_node_with_env<'tree>(
&self,
node: Node<'tree, L>,
env: &mut MetaVarEnv<'tree, L>,
) -> Option<Node<'tree, L>> {
match &self.style {
PatternStyle::Single => {
let matcher = self.single_matcher();
match_node_non_recursive(&matcher, node, env)
}
PatternStyle::Selector(kind) => {
let matcher = self.kind_matcher(kind);
match_node_non_recursive(&matcher, node, env)
}
}
}
fn potential_kinds(&self) -> Option<bit_set::BitSet> {
let kind = match &self.style {
PatternStyle::Selector(kind) => return kind.potential_kinds(),
PatternStyle::Single => {
let matcher = self.single_matcher();
if matcher.is_leaf() && extract_var_from_node(&matcher).is_some() {
return None;
}
matcher.kind_id()
}
};
let mut kinds = BitSet::new();
kinds.insert(kind.into());
Some(kinds)
}
fn get_match_len(&self, node: Node<L>) -> Option<usize> {
let start = node.range().start;
let end = match &self.style {
PatternStyle::Single => match_end_non_recursive(&self.single_matcher(), node)?,
PatternStyle::Selector(kind) => match_end_non_recursive(&self.kind_matcher(kind), node)?,
};
Some(end - start)
}
}
impl<L: Language> std::fmt::Debug for Pattern<L> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.style {
PatternStyle::Single => write!(f, "{}", self.single_matcher().to_sexp()),
PatternStyle::Selector(kind) => write!(f, "{}", self.kind_matcher(kind).to_sexp()),
}
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::language::Tsx;
use std::collections::HashMap;
fn pattern_node(s: &str) -> Root<Tsx> {
Root::new(s, Tsx)
}
fn test_match(s1: &str, s2: &str) {
let pattern = Pattern::new(s1, Tsx);
let cand = pattern_node(s2);
let cand = cand.root();
assert!(
pattern.find_node(cand.clone()).is_some(),
"goal: {}, candidate: {}",
pattern.root.root().to_sexp(),
cand.to_sexp(),
);
}
fn test_non_match(s1: &str, s2: &str) {
let pattern = Pattern::new(s1, Tsx);
let cand = pattern_node(s2);
let cand = cand.root();
assert!(
pattern.find_node(cand.clone()).is_none(),
"goal: {}, candidate: {}",
pattern.root.root().to_sexp(),
cand.to_sexp(),
);
}
#[test]
fn test_meta_variable() {
test_match("const a = $VALUE", "const a = 123");
test_match("const $VARIABLE = $VALUE", "const a = 123");
test_match("const $VARIABLE = $VALUE", "const a = 123");
}
fn match_env(goal_str: &str, cand: &str) -> HashMap<String, String> {
let pattern = Pattern::new(goal_str, Tsx);
let cand = pattern_node(cand);
let cand = cand.root();
let nm = pattern.find_node(cand).unwrap();
HashMap::from(nm.get_env().clone())
}
#[test]
fn test_meta_variable_env() {
let env = match_env("const a = $VALUE", "const a = 123");
assert_eq!(env["VALUE"], "123");
}
#[test]
fn test_match_non_atomic() {
let env = match_env("const a = $VALUE", "const a = 5 + 3");
assert_eq!(env["VALUE"], "5 + 3");
}
#[test]
fn test_class_assignment() {
test_match("class $C { $MEMBER = $VAL}", "class A {a = 123}");
test_non_match("class $C { $MEMBER = $VAL; b = 123; }", "class A {a = 123}");
test_non_match("a = 123", "class B {b = 123}");
}
#[test]
fn test_return() {
test_match("$A($B)", "return test(123)");
}
#[test]
fn test_contextual_pattern() {
let pattern =
Pattern::contextual("class A { $F = $I }", "public_field_definition", Tsx).expect("test");
let cand = pattern_node("class B { b = 123 }");
assert!(pattern.find_node(cand.root()).is_some());
let cand = pattern_node("let b = 123");
assert!(pattern.find_node(cand.root()).is_none());
}
#[test]
fn test_contextual_match_with_env() {
let pattern =
Pattern::contextual("class A { $F = $I }", "public_field_definition", Tsx).expect("test");
let cand = pattern_node("class B { b = 123 }");
let nm = pattern.find_node(cand.root()).expect("test");
let env = nm.get_env();
let env = HashMap::from(env.clone());
assert_eq!(env["F"], "b");
assert_eq!(env["I"], "123");
}
#[test]
fn test_contextual_unmatch_with_env() {
let pattern =
Pattern::contextual("class A { $F = $I }", "public_field_definition", Tsx).expect("test");
let cand = pattern_node("let b = 123");
let nm = pattern.find_node(cand.root());
assert!(nm.is_none());
}
fn get_kind(kind_str: &str) -> usize {
Tsx
.get_ts_language()
.id_for_node_kind(kind_str, true)
.into()
}
#[test]
fn test_pattern_potential_kinds() {
let pattern = Pattern::new("const a = 1", Tsx);
let kind = get_kind("lexical_declaration");
let kinds = pattern.potential_kinds().expect("should have kinds");
assert_eq!(kinds.len(), 1);
assert!(kinds.contains(kind));
}
#[test]
fn test_pattern_with_non_root_meta_var() {
let pattern = Pattern::new("const $A = $B", Tsx);
let kind = get_kind("lexical_declaration");
let kinds = pattern.potential_kinds().expect("should have kinds");
assert_eq!(kinds.len(), 1);
assert!(kinds.contains(kind));
}
#[test]
fn test_bare_wildcard() {
let pattern = Pattern::new("$A", Tsx);
assert!(pattern.potential_kinds().is_none());
}
#[test]
fn test_contextual_potential_kinds() {
let pattern =
Pattern::contextual("class A { $F = $I }", "public_field_definition", Tsx).expect("test");
let kind = get_kind("public_field_definition");
let kinds = pattern.potential_kinds().expect("should have kinds");
assert_eq!(kinds.len(), 1);
assert!(kinds.contains(kind));
}
#[test]
fn test_contextual_wildcard() {
let pattern = Pattern::contextual("class A { $F }", "property_identifier", Tsx).expect("test");
let kind = get_kind("property_identifier");
let kinds = pattern.potential_kinds().expect("should have kinds");
assert_eq!(kinds.len(), 1);
assert!(kinds.contains(kind));
}
#[test]
#[ignore]
fn test_multi_node_pattern() {
let pattern = Pattern::new("a;b;c;", Tsx);
let kinds = pattern.potential_kinds().expect("should have kinds");
assert_eq!(kinds.len(), 1);
test_match("a;b;c", "a;b;c;");
}
#[test]
#[ignore]
fn test_multi_node_meta_var() {
let env = match_env("a;$B;c", "a;b;c");
assert_eq!(env["B"], "b");
let env = match_env("a;$B;c", "a;1+2+3;c");
assert_eq!(env["B"], "1+2+3");
}
#[test]
fn test_pattern_size() {
assert_eq!(std::mem::size_of::<Pattern<Tsx>>(), 40);
}
}