diff options
author | Wilco Kusee <[email protected]> | 2019-03-03 09:40:03 +0000 |
---|---|---|
committer | Wilco Kusee <[email protected]> | 2019-03-03 09:40:03 +0000 |
commit | 725805dc795159399c90a5c9b6f11ffeadec2e0f (patch) | |
tree | ddc799a626c1b46d67bb274c87b4cfdaa22cd1ec /crates/ra_mbe/src/mbe_expander.rs | |
parent | dffe318701dcbd7da2b241cd8f623be9d4744ee3 (diff) |
Split parse and expand errors
Diffstat (limited to 'crates/ra_mbe/src/mbe_expander.rs')
-rw-r--r-- | crates/ra_mbe/src/mbe_expander.rs | 65 |
1 files changed, 33 insertions, 32 deletions
diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index 0a5bbf6b1..d4ce3bfe3 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs | |||
@@ -5,22 +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::{MacroRulesError, Result}; | 8 | use crate::ExpandError; |
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( |
12 | rules | 12 | rules: &crate::MacroRules, |
13 | .rules | 13 | input: &tt::Subtree, |
14 | .iter() | 14 | ) -> Result<tt::Subtree, ExpandError> { |
15 | .find_map(|it| expand_rule(it, input).ok()) | 15 | rules.rules.iter().find_map(|it| expand_rule(it, input).ok()).ok_or(ExpandError::NoMatchingRule) |
16 | .ok_or(MacroRulesError::NoMatchingRule) | ||
17 | } | 16 | } |
18 | 17 | ||
19 | fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Result<tt::Subtree> { | 18 | fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Result<tt::Subtree, ExpandError> { |
20 | let mut input = TtCursor::new(input); | 19 | let mut input = TtCursor::new(input); |
21 | let bindings = match_lhs(&rule.lhs, &mut input)?; | 20 | let bindings = match_lhs(&rule.lhs, &mut input)?; |
22 | if !input.is_eof() { | 21 | if !input.is_eof() { |
23 | return Err(MacroRulesError::UnexpectedToken); | 22 | return Err(ExpandError::UnexpectedToken); |
24 | } | 23 | } |
25 | expand_subtree(&rule.rhs, &bindings, &mut Vec::new()) | 24 | expand_subtree(&rule.rhs, &bindings, &mut Vec::new()) |
26 | } | 25 | } |
@@ -82,29 +81,30 @@ enum Binding { | |||
82 | } | 81 | } |
83 | 82 | ||
84 | impl Bindings { | 83 | impl Bindings { |
85 | fn get(&self, name: &SmolStr, nesting: &[usize]) -> Result<&tt::TokenTree> { | 84 | fn get(&self, name: &SmolStr, nesting: &[usize]) -> Result<&tt::TokenTree, ExpandError> { |
86 | let mut b = self | 85 | let mut b = self |
87 | .inner | 86 | .inner |
88 | .get(name) | 87 | .get(name) |
89 | .ok_or(MacroRulesError::BindingError(format!("could not find binding {}", name)))?; | 88 | .ok_or(ExpandError::BindingError(format!("could not find binding {}", name)))?; |
90 | for &idx in nesting.iter() { | 89 | for &idx in nesting.iter() { |
91 | b = match b { | 90 | b = match b { |
92 | Binding::Simple(_) => break, | 91 | Binding::Simple(_) => break, |
93 | Binding::Nested(bs) => bs.get(idx).ok_or(MacroRulesError::BindingError( | 92 | Binding::Nested(bs) => bs.get(idx).ok_or(ExpandError::BindingError(format!( |
94 | format!("could not find nested binding {}", name), | 93 | "could not find nested binding {}", |
95 | ))?, | 94 | name |
95 | )))?, | ||
96 | }; | 96 | }; |
97 | } | 97 | } |
98 | match b { | 98 | match b { |
99 | Binding::Simple(it) => Ok(it), | 99 | Binding::Simple(it) => Ok(it), |
100 | Binding::Nested(_) => Err(MacroRulesError::BindingError(format!( | 100 | Binding::Nested(_) => Err(ExpandError::BindingError(format!( |
101 | "expected simple binding, found nested binding {}", | 101 | "expected simple binding, found nested binding {}", |
102 | name | 102 | name |
103 | ))), | 103 | ))), |
104 | } | 104 | } |
105 | } | 105 | } |
106 | 106 | ||
107 | fn push_nested(&mut self, nested: Bindings) -> Result<()> { | 107 | fn push_nested(&mut self, nested: Bindings) -> Result<(), ExpandError> { |
108 | for (key, value) in nested.inner { | 108 | for (key, value) in nested.inner { |
109 | if !self.inner.contains_key(&key) { | 109 | if !self.inner.contains_key(&key) { |
110 | self.inner.insert(key.clone(), Binding::Nested(Vec::new())); | 110 | self.inner.insert(key.clone(), Binding::Nested(Vec::new())); |
@@ -112,10 +112,10 @@ impl Bindings { | |||
112 | match self.inner.get_mut(&key) { | 112 | match self.inner.get_mut(&key) { |
113 | Some(Binding::Nested(it)) => it.push(value), | 113 | Some(Binding::Nested(it)) => it.push(value), |
114 | _ => { | 114 | _ => { |
115 | return Err(MacroRulesError::BindingError(format!( | 115 | return Err(ExpandError::BindingError(format!( |
116 | "nested binding for {} not found", | 116 | "nested binding for {} not found", |
117 | key | 117 | key |
118 | ))) | 118 | ))); |
119 | } | 119 | } |
120 | } | 120 | } |
121 | } | 121 | } |
@@ -123,43 +123,44 @@ impl Bindings { | |||
123 | } | 123 | } |
124 | } | 124 | } |
125 | 125 | ||
126 | fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings> { | 126 | fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings, ExpandError> { |
127 | let mut res = Bindings::default(); | 127 | let mut res = Bindings::default(); |
128 | for pat in pattern.token_trees.iter() { | 128 | for pat in pattern.token_trees.iter() { |
129 | match pat { | 129 | match pat { |
130 | crate::TokenTree::Leaf(leaf) => match leaf { | 130 | crate::TokenTree::Leaf(leaf) => match leaf { |
131 | crate::Leaf::Var(crate::Var { text, kind }) => { | 131 | crate::Leaf::Var(crate::Var { text, kind }) => { |
132 | let kind = kind.clone().ok_or(MacroRulesError::ParseError)?; | 132 | let kind = kind.clone().ok_or(ExpandError::UnexpectedToken)?; |
133 | match kind.as_str() { | 133 | match kind.as_str() { |
134 | "ident" => { | 134 | "ident" => { |
135 | let ident = input.eat_ident()?.clone(); | 135 | let ident = |
136 | input.eat_ident().ok_or(ExpandError::UnexpectedToken)?.clone(); | ||
136 | res.inner.insert( | 137 | res.inner.insert( |
137 | text.clone(), | 138 | text.clone(), |
138 | Binding::Simple(tt::Leaf::from(ident).into()), | 139 | Binding::Simple(tt::Leaf::from(ident).into()), |
139 | ); | 140 | ); |
140 | } | 141 | } |
141 | _ => return Err(MacroRulesError::UnexpectedToken), | 142 | _ => return Err(ExpandError::UnexpectedToken), |
142 | } | 143 | } |
143 | } | 144 | } |
144 | crate::Leaf::Punct(punct) => { | 145 | crate::Leaf::Punct(punct) => { |
145 | if input.eat_punct()? != punct { | 146 | if input.eat_punct() != Some(punct) { |
146 | return Err(MacroRulesError::UnexpectedToken); | 147 | return Err(ExpandError::UnexpectedToken); |
147 | } | 148 | } |
148 | } | 149 | } |
149 | crate::Leaf::Ident(ident) => { | 150 | crate::Leaf::Ident(ident) => { |
150 | if input.eat_ident()?.text != ident.text { | 151 | if input.eat_ident().map(|i| &i.text) != Some(&ident.text) { |
151 | return Err(MacroRulesError::UnexpectedToken); | 152 | return Err(ExpandError::UnexpectedToken); |
152 | } | 153 | } |
153 | } | 154 | } |
154 | _ => return Err(MacroRulesError::UnexpectedToken), | 155 | _ => return Err(ExpandError::UnexpectedToken), |
155 | }, | 156 | }, |
156 | crate::TokenTree::Repeat(crate::Repeat { subtree, kind: _, separator }) => { | 157 | crate::TokenTree::Repeat(crate::Repeat { subtree, kind: _, separator }) => { |
157 | while let Ok(nested) = match_lhs(subtree, input) { | 158 | while let Ok(nested) = match_lhs(subtree, input) { |
158 | res.push_nested(nested)?; | 159 | res.push_nested(nested)?; |
159 | if let Some(separator) = *separator { | 160 | if let Some(separator) = *separator { |
160 | if !input.is_eof() { | 161 | if !input.is_eof() { |
161 | if input.eat_punct()?.char != separator { | 162 | if input.eat_punct().map(|p| p.char) != Some(separator) { |
162 | return Err(MacroRulesError::UnexpectedToken); | 163 | return Err(ExpandError::UnexpectedToken); |
163 | } | 164 | } |
164 | } | 165 | } |
165 | } | 166 | } |
@@ -175,12 +176,12 @@ fn expand_subtree( | |||
175 | template: &crate::Subtree, | 176 | template: &crate::Subtree, |
176 | bindings: &Bindings, | 177 | bindings: &Bindings, |
177 | nesting: &mut Vec<usize>, | 178 | nesting: &mut Vec<usize>, |
178 | ) -> Result<tt::Subtree> { | 179 | ) -> Result<tt::Subtree, ExpandError> { |
179 | let token_trees = template | 180 | let token_trees = template |
180 | .token_trees | 181 | .token_trees |
181 | .iter() | 182 | .iter() |
182 | .map(|it| expand_tt(it, bindings, nesting)) | 183 | .map(|it| expand_tt(it, bindings, nesting)) |
183 | .collect::<Result<Vec<_>>>()?; | 184 | .collect::<Result<Vec<_>, ExpandError>>()?; |
184 | 185 | ||
185 | Ok(tt::Subtree { token_trees, delimiter: template.delimiter }) | 186 | Ok(tt::Subtree { token_trees, delimiter: template.delimiter }) |
186 | } | 187 | } |
@@ -189,7 +190,7 @@ fn expand_tt( | |||
189 | template: &crate::TokenTree, | 190 | template: &crate::TokenTree, |
190 | bindings: &Bindings, | 191 | bindings: &Bindings, |
191 | nesting: &mut Vec<usize>, | 192 | nesting: &mut Vec<usize>, |
192 | ) -> Result<tt::TokenTree> { | 193 | ) -> Result<tt::TokenTree, ExpandError> { |
193 | let res: tt::TokenTree = match template { | 194 | let res: tt::TokenTree = match template { |
194 | crate::TokenTree::Subtree(subtree) => expand_subtree(subtree, bindings, nesting)?.into(), | 195 | crate::TokenTree::Subtree(subtree) => expand_subtree(subtree, bindings, nesting)?.into(), |
195 | crate::TokenTree::Repeat(repeat) => { | 196 | crate::TokenTree::Repeat(repeat) => { |