Repository navigation
Expand file tree
/
Copy pathcaptures.rs
More file actions
153 lines (135 loc) · 4.37 KB
/
Copy pathcaptures.rs
File metadata and controls
153 lines (135 loc) · 4.37 KB
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
use std::collections::{BTreeMap, BTreeSet};
use crate::{Id, OutputId};
#[derive(Debug, Clone)]
pub struct Captures {
captures: BTreeMap<&'static str, Vec<Id>>,
}
impl Default for Captures {
fn default() -> Self {
Self::new()
}
}
impl Captures {
pub fn new() -> Self {
Captures {
captures: BTreeMap::new(),
}
}
pub fn get_var(&self, key: &str) -> Result<Id, String> {
let ids = self.captures.get(key);
if let Some(ids) = ids {
if ids.len() == 1 {
Ok(ids[0])
} else {
Err(format!(
"Variable {} has {} matches, use * to allow repetition",
key,
ids.len()
))
}
} else {
Err(format!("No variable named {key}"))
}
}
/// Get all values of a capture variable (for repeated captures).
pub fn get_all(&self, key: &str) -> Vec<Id> {
self.captures.get(key).cloned().unwrap_or_default()
}
/// Get an optional capture variable. Returns None if unmatched,
/// Some(id) if matched exactly once.
pub fn get_opt(&self, key: &str) -> Option<Id> {
self.captures
.get(key)
.and_then(|ids| (ids.len() == 1).then(|| ids[0]))
}
pub fn insert(&mut self, key: &'static str, id: Id) {
self.captures.entry(key).or_default().push(id);
}
pub(crate) fn translate_except(
&self,
skip: &[&str],
mut f: impl FnMut(Id) -> Vec<OutputId>,
) -> TranslatedCaptures {
let mut translated = TranslatedCaptures::new();
for (name, ids) in &self.captures {
if skip.contains(name) {
continue;
}
for &id in ids {
for translated_id in f(id) {
translated.insert(name, translated_id);
}
}
}
translated
}
pub fn merge(&mut self, other: &Captures) {
for (key, ids) in &other.captures {
self.captures.entry(key).or_default().extend(ids);
}
}
pub fn un_star<'a>(
&'a self,
children: &'a BTreeSet<&'static str>,
) -> Result<impl Iterator<Item = Captures> + 'a, String> {
let mut id_iter = children.iter();
if let Some(fst) = id_iter.next() {
let repeats = self
.captures
.get(fst)
.ok_or_else(|| format!("No variable named {fst}"))?
.len();
// TODO: better error on missing capture
if id_iter.any(|id| self.captures.get(id).map_or(0, Vec::len) != repeats) {
return Err("Repeated captures must have the same number of matches".to_string());
}
Ok((0..repeats).map(move |iter| {
let mut new_vars = Captures::new();
for id in children {
let child_capture = self.captures.get(id).unwrap()[iter];
new_vars.captures.insert(id, vec![child_capture]);
}
new_vars
}))
} else {
Err("Repeated captures must have at least one capture".to_string())
}
}
}
/// Output ids produced by eagerly translating non-raw query captures.
#[derive(Debug, Clone, Default)]
pub struct TranslatedCaptures {
captures: BTreeMap<&'static str, Vec<OutputId>>,
}
impl TranslatedCaptures {
pub fn new() -> Self {
Self::default()
}
pub fn get_var(&self, key: &str) -> Result<OutputId, String> {
let ids = self.captures.get(key);
if let Some(ids) = ids {
if ids.len() == 1 {
Ok(ids[0])
} else {
Err(format!(
"Variable {} has {} matches, use * to allow repetition",
key,
ids.len()
))
}
} else {
Err(format!("No variable named {key}"))
}
}
pub fn get_all(&self, key: &str) -> Vec<OutputId> {
self.captures.get(key).cloned().unwrap_or_default()
}
pub fn get_opt(&self, key: &str) -> Option<OutputId> {
self.captures
.get(key)
.and_then(|ids| (ids.len() == 1).then(|| ids[0]))
}
fn insert(&mut self, key: &'static str, id: OutputId) {
self.captures.entry(key).or_default().push(id);
}
}