diff options
Diffstat (limited to 'crates/ra_mbe/src/mbe_expander.rs')
-rw-r--r-- | crates/ra_mbe/src/mbe_expander.rs | 147 |
1 files changed, 116 insertions, 31 deletions
diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index 1acba86ea..2dd97b665 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs | |||
@@ -5,17 +5,21 @@ use rustc_hash::FxHashMap; | |||
5 | use ra_syntax::SmolStr; | 5 | use ra_syntax::SmolStr; |
6 | use tt::TokenId; | 6 | use tt::TokenId; |
7 | 7 | ||
8 | use crate::ExpandError; | ||
8 | use crate::tt_cursor::TtCursor; | 9 | use crate::tt_cursor::TtCursor; |
9 | 10 | ||
10 | pub(crate) fn exapnd(rules: &crate::MacroRules, input: &tt::Subtree) -> Option<tt::Subtree> { | 11 | pub(crate) fn expand( |
11 | rules.rules.iter().find_map(|it| expand_rule(it, input)) | 12 | rules: &crate::MacroRules, |
13 | input: &tt::Subtree, | ||
14 | ) -> Result<tt::Subtree, ExpandError> { | ||
15 | rules.rules.iter().find_map(|it| expand_rule(it, input).ok()).ok_or(ExpandError::NoMatchingRule) | ||
12 | } | 16 | } |
13 | 17 | ||
14 | fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Option<tt::Subtree> { | 18 | fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Result<tt::Subtree, ExpandError> { |
15 | let mut input = TtCursor::new(input); | 19 | let mut input = TtCursor::new(input); |
16 | let bindings = match_lhs(&rule.lhs, &mut input)?; | 20 | let bindings = match_lhs(&rule.lhs, &mut input)?; |
17 | if !input.is_eof() { | 21 | if !input.is_eof() { |
18 | return None; | 22 | return Err(ExpandError::UnexpectedToken); |
19 | } | 23 | } |
20 | expand_subtree(&rule.rhs, &bindings, &mut Vec::new()) | 24 | expand_subtree(&rule.rhs, &bindings, &mut Vec::new()) |
21 | } | 25 | } |
@@ -77,70 +81,86 @@ enum Binding { | |||
77 | } | 81 | } |
78 | 82 | ||
79 | impl Bindings { | 83 | impl Bindings { |
80 | fn get(&self, name: &SmolStr, nesting: &[usize]) -> Option<&tt::TokenTree> { | 84 | fn get(&self, name: &SmolStr, nesting: &[usize]) -> Result<&tt::TokenTree, ExpandError> { |
81 | let mut b = self.inner.get(name)?; | 85 | let mut b = self |
86 | .inner | ||
87 | .get(name) | ||
88 | .ok_or(ExpandError::BindingError(format!("could not find binding `{}`", name)))?; | ||
82 | for &idx in nesting.iter() { | 89 | for &idx in nesting.iter() { |
83 | b = match b { | 90 | b = match b { |
84 | Binding::Simple(_) => break, | 91 | Binding::Simple(_) => break, |
85 | Binding::Nested(bs) => bs.get(idx)?, | 92 | Binding::Nested(bs) => bs.get(idx).ok_or(ExpandError::BindingError(format!( |
93 | "could not find nested binding `{}`", | ||
94 | name | ||
95 | )))?, | ||
86 | }; | 96 | }; |
87 | } | 97 | } |
88 | match b { | 98 | match b { |
89 | Binding::Simple(it) => Some(it), | 99 | Binding::Simple(it) => Ok(it), |
90 | Binding::Nested(_) => None, | 100 | Binding::Nested(_) => Err(ExpandError::BindingError(format!( |
101 | "expected simple binding, found nested binding `{}`", | ||
102 | name | ||
103 | ))), | ||
91 | } | 104 | } |
92 | } | 105 | } |
93 | fn push_nested(&mut self, nested: Bindings) -> Option<()> { | 106 | |
107 | fn push_nested(&mut self, nested: Bindings) -> Result<(), ExpandError> { | ||
94 | for (key, value) in nested.inner { | 108 | for (key, value) in nested.inner { |
95 | if !self.inner.contains_key(&key) { | 109 | if !self.inner.contains_key(&key) { |
96 | self.inner.insert(key.clone(), Binding::Nested(Vec::new())); | 110 | self.inner.insert(key.clone(), Binding::Nested(Vec::new())); |
97 | } | 111 | } |
98 | match self.inner.get_mut(&key) { | 112 | match self.inner.get_mut(&key) { |
99 | Some(Binding::Nested(it)) => it.push(value), | 113 | Some(Binding::Nested(it)) => it.push(value), |
100 | _ => return None, | 114 | _ => { |
115 | return Err(ExpandError::BindingError(format!( | ||
116 | "could not find binding `{}`", | ||
117 | key | ||
118 | ))); | ||
119 | } | ||
101 | } | 120 | } |
102 | } | 121 | } |
103 | Some(()) | 122 | Ok(()) |
104 | } | 123 | } |
105 | } | 124 | } |
106 | 125 | ||
107 | fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Option<Bindings> { | 126 | fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings, ExpandError> { |
108 | let mut res = Bindings::default(); | 127 | let mut res = Bindings::default(); |
109 | for pat in pattern.token_trees.iter() { | 128 | for pat in pattern.token_trees.iter() { |
110 | match pat { | 129 | match pat { |
111 | crate::TokenTree::Leaf(leaf) => match leaf { | 130 | crate::TokenTree::Leaf(leaf) => match leaf { |
112 | crate::Leaf::Var(crate::Var { text, kind }) => { | 131 | crate::Leaf::Var(crate::Var { text, kind }) => { |
113 | let kind = kind.clone()?; | 132 | let kind = kind.clone().ok_or(ExpandError::UnexpectedToken)?; |
114 | match kind.as_str() { | 133 | match kind.as_str() { |
115 | "ident" => { | 134 | "ident" => { |
116 | let ident = input.eat_ident()?.clone(); | 135 | let ident = |
136 | input.eat_ident().ok_or(ExpandError::UnexpectedToken)?.clone(); | ||
117 | res.inner.insert( | 137 | res.inner.insert( |
118 | text.clone(), | 138 | text.clone(), |
119 | Binding::Simple(tt::Leaf::from(ident).into()), | 139 | Binding::Simple(tt::Leaf::from(ident).into()), |
120 | ); | 140 | ); |
121 | } | 141 | } |
122 | _ => return None, | 142 | _ => return Err(ExpandError::UnexpectedToken), |
123 | } | 143 | } |
124 | } | 144 | } |
125 | crate::Leaf::Punct(punct) => { | 145 | crate::Leaf::Punct(punct) => { |
126 | if input.eat_punct()? != punct { | 146 | if input.eat_punct() != Some(punct) { |
127 | return None; | 147 | return Err(ExpandError::UnexpectedToken); |
128 | } | 148 | } |
129 | } | 149 | } |
130 | crate::Leaf::Ident(ident) => { | 150 | crate::Leaf::Ident(ident) => { |
131 | if input.eat_ident()?.text != ident.text { | 151 | if input.eat_ident().map(|i| &i.text) != Some(&ident.text) { |
132 | return None; | 152 | return Err(ExpandError::UnexpectedToken); |
133 | } | 153 | } |
134 | } | 154 | } |
135 | _ => return None, | 155 | _ => return Err(ExpandError::UnexpectedToken), |
136 | }, | 156 | }, |
137 | crate::TokenTree::Repeat(crate::Repeat { subtree, kind: _, separator }) => { | 157 | crate::TokenTree::Repeat(crate::Repeat { subtree, kind: _, separator }) => { |
138 | while let Some(nested) = match_lhs(subtree, input) { | 158 | while let Ok(nested) = match_lhs(subtree, input) { |
139 | res.push_nested(nested)?; | 159 | res.push_nested(nested)?; |
140 | if let Some(separator) = *separator { | 160 | if let Some(separator) = *separator { |
141 | if !input.is_eof() { | 161 | if !input.is_eof() { |
142 | if input.eat_punct()?.char != separator { | 162 | if input.eat_punct().map(|p| p.char) != Some(separator) { |
143 | return None; | 163 | return Err(ExpandError::UnexpectedToken); |
144 | } | 164 | } |
145 | } | 165 | } |
146 | } | 166 | } |
@@ -149,34 +169,34 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Option<Bindings> | |||
149 | _ => {} | 169 | _ => {} |
150 | } | 170 | } |
151 | } | 171 | } |
152 | Some(res) | 172 | Ok(res) |
153 | } | 173 | } |
154 | 174 | ||
155 | fn expand_subtree( | 175 | fn expand_subtree( |
156 | template: &crate::Subtree, | 176 | template: &crate::Subtree, |
157 | bindings: &Bindings, | 177 | bindings: &Bindings, |
158 | nesting: &mut Vec<usize>, | 178 | nesting: &mut Vec<usize>, |
159 | ) -> Option<tt::Subtree> { | 179 | ) -> Result<tt::Subtree, ExpandError> { |
160 | let token_trees = template | 180 | let token_trees = template |
161 | .token_trees | 181 | .token_trees |
162 | .iter() | 182 | .iter() |
163 | .map(|it| expand_tt(it, bindings, nesting)) | 183 | .map(|it| expand_tt(it, bindings, nesting)) |
164 | .collect::<Option<Vec<_>>>()?; | 184 | .collect::<Result<Vec<_>, ExpandError>>()?; |
165 | 185 | ||
166 | Some(tt::Subtree { token_trees, delimiter: template.delimiter }) | 186 | Ok(tt::Subtree { token_trees, delimiter: template.delimiter }) |
167 | } | 187 | } |
168 | 188 | ||
169 | fn expand_tt( | 189 | fn expand_tt( |
170 | template: &crate::TokenTree, | 190 | template: &crate::TokenTree, |
171 | bindings: &Bindings, | 191 | bindings: &Bindings, |
172 | nesting: &mut Vec<usize>, | 192 | nesting: &mut Vec<usize>, |
173 | ) -> Option<tt::TokenTree> { | 193 | ) -> Result<tt::TokenTree, ExpandError> { |
174 | let res: tt::TokenTree = match template { | 194 | let res: tt::TokenTree = match template { |
175 | crate::TokenTree::Subtree(subtree) => expand_subtree(subtree, bindings, nesting)?.into(), | 195 | crate::TokenTree::Subtree(subtree) => expand_subtree(subtree, bindings, nesting)?.into(), |
176 | crate::TokenTree::Repeat(repeat) => { | 196 | crate::TokenTree::Repeat(repeat) => { |
177 | let mut token_trees = Vec::new(); | 197 | let mut token_trees = Vec::new(); |
178 | nesting.push(0); | 198 | nesting.push(0); |
179 | while let Some(t) = expand_subtree(&repeat.subtree, bindings, nesting) { | 199 | while let Ok(t) = expand_subtree(&repeat.subtree, bindings, nesting) { |
180 | let idx = nesting.pop().unwrap(); | 200 | let idx = nesting.pop().unwrap(); |
181 | nesting.push(idx + 1); | 201 | nesting.push(idx + 1); |
182 | token_trees.push(t.into()) | 202 | token_trees.push(t.into()) |
@@ -194,5 +214,70 @@ fn expand_tt( | |||
194 | crate::Leaf::Literal(l) => tt::Leaf::from(tt::Literal { text: l.text.clone() }).into(), | 214 | crate::Leaf::Literal(l) => tt::Leaf::from(tt::Literal { text: l.text.clone() }).into(), |
195 | }, | 215 | }, |
196 | }; | 216 | }; |
197 | Some(res) | 217 | Ok(res) |
218 | } | ||
219 | |||
220 | #[cfg(test)] | ||
221 | mod tests { | ||
222 | use ra_syntax::{ast, AstNode}; | ||
223 | |||
224 | use super::*; | ||
225 | use crate::ast_to_token_tree; | ||
226 | |||
227 | #[test] | ||
228 | fn test_expand_rule() { | ||
229 | assert_err( | ||
230 | "($i:ident) => ($j)", | ||
231 | "foo!{a}", | ||
232 | ExpandError::BindingError(String::from("could not find binding `j`")), | ||
233 | ); | ||
234 | |||
235 | assert_err( | ||
236 | "($($i:ident);*) => ($i)", | ||
237 | "foo!{a}", | ||
238 | ExpandError::BindingError(String::from( | ||
239 | "expected simple binding, found nested binding `i`", | ||
240 | )), | ||
241 | ); | ||
242 | |||
243 | assert_err("($i) => ($i)", "foo!{a}", ExpandError::UnexpectedToken); | ||
244 | assert_err("($i:) => ($i)", "foo!{a}", ExpandError::UnexpectedToken); | ||
245 | } | ||
246 | |||
247 | fn assert_err(macro_body: &str, invocation: &str, err: ExpandError) { | ||
248 | assert_eq!(expand_first(&create_rules(&format_macro(macro_body)), invocation), Err(err)); | ||
249 | } | ||
250 | |||
251 | fn format_macro(macro_body: &str) -> String { | ||
252 | format!( | ||
253 | " | ||
254 | macro_rules! foo {{ | ||
255 | {} | ||
256 | }} | ||
257 | ", | ||
258 | macro_body | ||
259 | ) | ||
260 | } | ||
261 | |||
262 | fn create_rules(macro_definition: &str) -> crate::MacroRules { | ||
263 | let source_file = ast::SourceFile::parse(macro_definition); | ||
264 | let macro_definition = | ||
265 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); | ||
266 | |||
267 | let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap(); | ||
268 | crate::MacroRules::parse(&definition_tt).unwrap() | ||
269 | } | ||
270 | |||
271 | fn expand_first( | ||
272 | rules: &crate::MacroRules, | ||
273 | invocation: &str, | ||
274 | ) -> Result<tt::Subtree, ExpandError> { | ||
275 | let source_file = ast::SourceFile::parse(invocation); | ||
276 | let macro_invocation = | ||
277 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); | ||
278 | |||
279 | let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap(); | ||
280 | |||
281 | expand_rule(&rules.rules[0], &invocation_tt) | ||
282 | } | ||
198 | } | 283 | } |