aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/mbe_expander.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe/src/mbe_expander.rs')
-rw-r--r--crates/ra_mbe/src/mbe_expander.rs38
1 files changed, 18 insertions, 20 deletions
diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs
index a0bd0c5f8..55a6ecf58 100644
--- a/crates/ra_mbe/src/mbe_expander.rs
+++ b/crates/ra_mbe/src/mbe_expander.rs
@@ -105,17 +105,15 @@ impl Bindings {
105 } 105 }
106 106
107 fn get(&self, name: &SmolStr, nesting: &[usize]) -> Result<&tt::TokenTree, ExpandError> { 107 fn get(&self, name: &SmolStr, nesting: &[usize]) -> Result<&tt::TokenTree, ExpandError> {
108 let mut b = self 108 let mut b = self.inner.get(name).ok_or_else(|| {
109 .inner 109 ExpandError::BindingError(format!("could not find binding `{}`", name))
110 .get(name) 110 })?;
111 .ok_or(ExpandError::BindingError(format!("could not find binding `{}`", name)))?;
112 for &idx in nesting.iter() { 111 for &idx in nesting.iter() {
113 b = match b { 112 b = match b {
114 Binding::Simple(_) => break, 113 Binding::Simple(_) => break,
115 Binding::Nested(bs) => bs.get(idx).ok_or(ExpandError::BindingError(format!( 114 Binding::Nested(bs) => bs.get(idx).ok_or_else(|| {
116 "could not find nested binding `{}`", 115 ExpandError::BindingError(format!("could not find nested binding `{}`", name))
117 name 116 })?,
118 )))?,
119 Binding::Empty => { 117 Binding::Empty => {
120 return Err(ExpandError::BindingError(format!( 118 return Err(ExpandError::BindingError(format!(
121 "could not find empty binding `{}`", 119 "could not find empty binding `{}`",
@@ -206,48 +204,48 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings,
206 "path" => { 204 "path" => {
207 let path = 205 let path =
208 input.eat_path().ok_or(ExpandError::UnexpectedToken)?.clone(); 206 input.eat_path().ok_or(ExpandError::UnexpectedToken)?.clone();
209 res.inner.insert(text.clone(), Binding::Simple(path.into())); 207 res.inner.insert(text.clone(), Binding::Simple(path));
210 } 208 }
211 "expr" => { 209 "expr" => {
212 let expr = 210 let expr =
213 input.eat_expr().ok_or(ExpandError::UnexpectedToken)?.clone(); 211 input.eat_expr().ok_or(ExpandError::UnexpectedToken)?.clone();
214 res.inner.insert(text.clone(), Binding::Simple(expr.into())); 212 res.inner.insert(text.clone(), Binding::Simple(expr));
215 } 213 }
216 "ty" => { 214 "ty" => {
217 let ty = input.eat_ty().ok_or(ExpandError::UnexpectedToken)?.clone(); 215 let ty = input.eat_ty().ok_or(ExpandError::UnexpectedToken)?.clone();
218 res.inner.insert(text.clone(), Binding::Simple(ty.into())); 216 res.inner.insert(text.clone(), Binding::Simple(ty));
219 } 217 }
220 "pat" => { 218 "pat" => {
221 let pat = input.eat_pat().ok_or(ExpandError::UnexpectedToken)?.clone(); 219 let pat = input.eat_pat().ok_or(ExpandError::UnexpectedToken)?.clone();
222 res.inner.insert(text.clone(), Binding::Simple(pat.into())); 220 res.inner.insert(text.clone(), Binding::Simple(pat));
223 } 221 }
224 "stmt" => { 222 "stmt" => {
225 let pat = input.eat_stmt().ok_or(ExpandError::UnexpectedToken)?.clone(); 223 let pat = input.eat_stmt().ok_or(ExpandError::UnexpectedToken)?.clone();
226 res.inner.insert(text.clone(), Binding::Simple(pat.into())); 224 res.inner.insert(text.clone(), Binding::Simple(pat));
227 } 225 }
228 "block" => { 226 "block" => {
229 let block = 227 let block =
230 input.eat_block().ok_or(ExpandError::UnexpectedToken)?.clone(); 228 input.eat_block().ok_or(ExpandError::UnexpectedToken)?.clone();
231 res.inner.insert(text.clone(), Binding::Simple(block.into())); 229 res.inner.insert(text.clone(), Binding::Simple(block));
232 } 230 }
233 "meta" => { 231 "meta" => {
234 let meta = 232 let meta =
235 input.eat_meta().ok_or(ExpandError::UnexpectedToken)?.clone(); 233 input.eat_meta().ok_or(ExpandError::UnexpectedToken)?.clone();
236 res.inner.insert(text.clone(), Binding::Simple(meta.into())); 234 res.inner.insert(text.clone(), Binding::Simple(meta));
237 } 235 }
238 "tt" => { 236 "tt" => {
239 let token = input.eat().ok_or(ExpandError::UnexpectedToken)?.clone(); 237 let token = input.eat().ok_or(ExpandError::UnexpectedToken)?.clone();
240 res.inner.insert(text.clone(), Binding::Simple(token.into())); 238 res.inner.insert(text.clone(), Binding::Simple(token));
241 } 239 }
242 "item" => { 240 "item" => {
243 let item = 241 let item =
244 input.eat_item().ok_or(ExpandError::UnexpectedToken)?.clone(); 242 input.eat_item().ok_or(ExpandError::UnexpectedToken)?.clone();
245 res.inner.insert(text.clone(), Binding::Simple(item.into())); 243 res.inner.insert(text.clone(), Binding::Simple(item));
246 } 244 }
247 "lifetime" => { 245 "lifetime" => {
248 let lifetime = 246 let lifetime =
249 input.eat_lifetime().ok_or(ExpandError::UnexpectedToken)?.clone(); 247 input.eat_lifetime().ok_or(ExpandError::UnexpectedToken)?.clone();
250 res.inner.insert(text.clone(), Binding::Simple(lifetime.into())); 248 res.inner.insert(text.clone(), Binding::Simple(lifetime));
251 } 249 }
252 "literal" => { 250 "literal" => {
253 let literal = 251 let literal =
@@ -262,7 +260,7 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings,
262 // `vis` is optional 260 // `vis` is optional
263 if let Some(vis) = input.try_eat_vis() { 261 if let Some(vis) = input.try_eat_vis() {
264 let vis = vis.clone(); 262 let vis = vis.clone();
265 res.inner.insert(text.clone(), Binding::Simple(vis.into())); 263 res.inner.insert(text.clone(), Binding::Simple(vis));
266 } else { 264 } else {
267 res.push_optional(&text); 265 res.push_optional(&text);
268 } 266 }
@@ -452,7 +450,7 @@ fn expand_tt(
452 450
453 let idx = ctx.nesting.pop().unwrap(); 451 let idx = ctx.nesting.pop().unwrap();
454 ctx.nesting.push(idx + 1); 452 ctx.nesting.push(idx + 1);
455 token_trees.push(reduce_single_token(t).into()); 453 token_trees.push(reduce_single_token(t));
456 454
457 if let Some(ref sep) = repeat.separator { 455 if let Some(ref sep) = repeat.separator {
458 match sep { 456 match sep {