diff options
-rw-r--r-- | crates/ra_mbe/src/mbe_expander.rs | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index c393d8487..0a5bbf6b1 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs | |||
@@ -9,7 +9,10 @@ use crate::{MacroRulesError, Result}; | |||
9 | use crate::tt_cursor::TtCursor; | 9 | use crate::tt_cursor::TtCursor; |
10 | 10 | ||
11 | pub(crate) fn expand(rules: &crate::MacroRules, input: &tt::Subtree) -> Result<tt::Subtree> { | 11 | pub(crate) fn expand(rules: &crate::MacroRules, input: &tt::Subtree) -> Result<tt::Subtree> { |
12 | rules.rules.iter().find_map(|it| expand_rule(it, input).ok()) | 12 | rules |
13 | .rules | ||
14 | .iter() | ||
15 | .find_map(|it| expand_rule(it, input).ok()) | ||
13 | .ok_or(MacroRulesError::NoMatchingRule) | 16 | .ok_or(MacroRulesError::NoMatchingRule) |
14 | } | 17 | } |
15 | 18 | ||
@@ -80,21 +83,24 @@ enum Binding { | |||
80 | 83 | ||
81 | impl Bindings { | 84 | impl Bindings { |
82 | fn get(&self, name: &SmolStr, nesting: &[usize]) -> Result<&tt::TokenTree> { | 85 | fn get(&self, name: &SmolStr, nesting: &[usize]) -> Result<&tt::TokenTree> { |
83 | let mut b = self.inner.get(name).ok_or(MacroRulesError::BindingError( | 86 | let mut b = self |
84 | format!("could not find binding {}", name) | 87 | .inner |
85 | ))?; | 88 | .get(name) |
89 | .ok_or(MacroRulesError::BindingError(format!("could not find binding {}", name)))?; | ||
86 | for &idx in nesting.iter() { | 90 | for &idx in nesting.iter() { |
87 | b = match b { | 91 | b = match b { |
88 | Binding::Simple(_) => break, | 92 | Binding::Simple(_) => break, |
89 | Binding::Nested(bs) => bs.get(idx).ok_or(MacroRulesError::BindingError( | 93 | Binding::Nested(bs) => bs.get(idx).ok_or(MacroRulesError::BindingError( |
90 | format!("could not find nested binding {}", name)) | 94 | format!("could not find nested binding {}", name), |
91 | )?, | 95 | ))?, |
92 | }; | 96 | }; |
93 | } | 97 | } |
94 | match b { | 98 | match b { |
95 | Binding::Simple(it) => Ok(it), | 99 | Binding::Simple(it) => Ok(it), |
96 | Binding::Nested(_) => Err(MacroRulesError::BindingError( | 100 | Binding::Nested(_) => Err(MacroRulesError::BindingError(format!( |
97 | format!("expected simple binding, found nested binding {}", name))), | 101 | "expected simple binding, found nested binding {}", |
102 | name | ||
103 | ))), | ||
98 | } | 104 | } |
99 | } | 105 | } |
100 | 106 | ||
@@ -105,8 +111,12 @@ impl Bindings { | |||
105 | } | 111 | } |
106 | match self.inner.get_mut(&key) { | 112 | match self.inner.get_mut(&key) { |
107 | Some(Binding::Nested(it)) => it.push(value), | 113 | Some(Binding::Nested(it)) => it.push(value), |
108 | _ => return Err(MacroRulesError::BindingError( | 114 | _ => { |
109 | format!("nested binding for {} not found", key))), | 115 | return Err(MacroRulesError::BindingError(format!( |
116 | "nested binding for {} not found", | ||
117 | key | ||
118 | ))) | ||
119 | } | ||
110 | } | 120 | } |
111 | } | 121 | } |
112 | Ok(()) | 122 | Ok(()) |