aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/mbe_expander.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-04-21 11:34:07 +0100
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-04-21 11:34:07 +0100
commitfa15c4d75e87bd3bf761c91f030c76aec59308ae (patch)
treeba7a3495b13e41231076d5a0e491132bbdb0670e /crates/ra_mbe/src/mbe_expander.rs
parent493bf20b3d1a0a890514d5252901f13d2878ff34 (diff)
parent9e35bf91b827900b089a7ea937cb73707bebc420 (diff)
Merge #1175
1175: Fix bugs and add error log about macro expansion r=matklad a=edwin0cheng This PR fixed / add following things: * Add a fused count which stop recursion of macro expansion in name resolution. * Add some logs when macro expansion fails * Add `$crate` meta variable support in mbe, which create a `$crate` ident token in token tree. * Fixed matching a `$REPEAT` pattern inside a subtree, e.g. `(fn $name:ident {$($i:ident)*} ) => {...}` * Remove composite-able punct token in syntax node to token conversion. Co-authored-by: Edwin Cheng <[email protected]>
Diffstat (limited to 'crates/ra_mbe/src/mbe_expander.rs')
-rw-r--r--crates/ra_mbe/src/mbe_expander.rs30
1 files changed, 28 insertions, 2 deletions
diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs
index 86867111f..66ea76698 100644
--- a/crates/ra_mbe/src/mbe_expander.rs
+++ b/crates/ra_mbe/src/mbe_expander.rs
@@ -121,6 +121,10 @@ impl Bindings {
121 } 121 }
122 Ok(()) 122 Ok(())
123 } 123 }
124
125 fn merge(&mut self, nested: Bindings) {
126 self.inner.extend(nested.inner);
127 }
124} 128}
125 129
126fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings, ExpandError> { 130fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings, ExpandError> {
@@ -236,7 +240,21 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings,
236 } 240 }
237 } 241 }
238 } 242 }
239 _ => {} 243 crate::TokenTree::Subtree(subtree) => {
244 let input_subtree =
245 input.eat_subtree().map_err(|_| ExpandError::UnexpectedToken)?;
246 if subtree.delimiter != input_subtree.delimiter {
247 return Err(ExpandError::UnexpectedToken);
248 }
249
250 let mut input = TtCursor::new(input_subtree);
251 let bindings = match_lhs(&subtree, &mut input)?;
252 if !input.is_eof() {
253 return Err(ExpandError::UnexpectedToken);
254 }
255
256 res.merge(bindings);
257 }
240 } 258 }
241 } 259 }
242 Ok(res) 260 Ok(res)
@@ -287,7 +305,15 @@ fn expand_tt(
287 .into() 305 .into()
288 } 306 }
289 crate::Leaf::Punct(punct) => tt::Leaf::from(punct.clone()).into(), 307 crate::Leaf::Punct(punct) => tt::Leaf::from(punct.clone()).into(),
290 crate::Leaf::Var(v) => bindings.get(&v.text, nesting)?.clone(), 308 crate::Leaf::Var(v) => {
309 if v.text == "crate" {
310 // FIXME: Properly handle $crate token
311 tt::Leaf::from(tt::Ident { text: "$crate".into(), id: TokenId::unspecified() })
312 .into()
313 } else {
314 bindings.get(&v.text, nesting)?.clone()
315 }
316 }
291 crate::Leaf::Literal(l) => tt::Leaf::from(tt::Literal { text: l.text.clone() }).into(), 317 crate::Leaf::Literal(l) => tt::Leaf::from(tt::Literal { text: l.text.clone() }).into(),
292 }, 318 },
293 }; 319 };