From 35c463315032dcebb38f738cca57581608f19826 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Thu, 2 May 2019 21:24:51 +0800 Subject: Make `vis` matcher optional and fix typo --- crates/ra_mbe/src/mbe_expander.rs | 20 ++++++++++++++++++-- crates/ra_mbe/src/subtree_source.rs | 2 +- crates/ra_mbe/src/tt_cursor.rs | 11 +++++++++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index d5189b537..1b579f319 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs @@ -206,8 +206,24 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result { - let vis = input.eat_vis().ok_or(ExpandError::UnexpectedToken)?.clone(); - res.inner.insert(text.clone(), Binding::Simple(vis.into())); + // `vis` is optional + if let Some(vis) = input.try_eat_vis() { + let vis = vis.clone(); + res.inner.insert(text.clone(), Binding::Simple(vis.into())); + } else { + // FIXME: Do we have a better way to represent an empty token ? + // Insert an empty subtree for empty token + res.inner.insert( + text.clone(), + Binding::Simple( + tt::Subtree { + delimiter: tt::Delimiter::None, + token_trees: vec![], + } + .into(), + ), + ); + } } _ => return Err(ExpandError::UnexpectedToken), diff --git a/crates/ra_mbe/src/subtree_source.rs b/crates/ra_mbe/src/subtree_source.rs index 8176296e6..3554dc110 100644 --- a/crates/ra_mbe/src/subtree_source.rs +++ b/crates/ra_mbe/src/subtree_source.rs @@ -319,7 +319,7 @@ fn convert_ident(ident: &tt::Ident) -> TtToken { fn convert_punct(p: &tt::Punct) -> TtToken { let kind = match p.char { - // lexer may produce combpund tokens for these ones + // lexer may produce compound tokens for these ones '.' => DOT, ':' => COLON, '=' => EQ, diff --git a/crates/ra_mbe/src/tt_cursor.rs b/crates/ra_mbe/src/tt_cursor.rs index 8f15d215b..d85ab43e4 100644 --- a/crates/ra_mbe/src/tt_cursor.rs +++ b/crates/ra_mbe/src/tt_cursor.rs @@ -149,9 +149,16 @@ impl<'a> TtCursor<'a> { self.eat_ident().cloned().map(|ident| tt::Leaf::from(ident).into()) } - pub(crate) fn eat_vis(&mut self) -> Option { + pub(crate) fn try_eat_vis(&mut self) -> Option { + // `vis` matcher is optional + let old_pos = self.pos; let parser = Parser::new(&mut self.pos, self.subtree); - parser.parse_vis() + + let res = parser.parse_vis(); + if res.is_none() { + self.pos = old_pos; + } + res } pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ParseError> { -- cgit v1.2.3