From da18f1130756c4fdd611d95e4f98e553b4b65995 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 18 Apr 2020 19:28:07 +0800 Subject: Split LIFETIME to two tokens in mbe --- crates/ra_mbe/src/mbe_expander/matcher.rs | 34 +++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) (limited to 'crates/ra_mbe/src/mbe_expander') diff --git a/crates/ra_mbe/src/mbe_expander/matcher.rs b/crates/ra_mbe/src/mbe_expander/matcher.rs index 2579382da..9485c62b8 100644 --- a/crates/ra_mbe/src/mbe_expander/matcher.rs +++ b/crates/ra_mbe/src/mbe_expander/matcher.rs @@ -202,6 +202,13 @@ impl<'a> TtIter<'a> { } pub(crate) fn expect_tt(&mut self) -> Result { + match self.peek_n(0) { + Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) if punct.char == '\'' => { + return self.expect_lifetime(); + } + _ => (), + } + let tt = self.next().ok_or_else(|| ())?.clone(); let punct = match tt { tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) if punct.spacing == tt::Spacing::Joint => { @@ -255,13 +262,21 @@ impl<'a> TtIter<'a> { } } - pub(crate) fn expect_lifetime(&mut self) -> Result<&tt::Ident, ()> { - let ident = self.expect_ident()?; - // check if it start from "`" - if !ident.text.starts_with('\'') { + pub(crate) fn expect_lifetime(&mut self) -> Result { + let punct = self.expect_punct()?; + if punct.char != '\'' { return Err(()); } - Ok(ident) + let ident = self.expect_ident()?; + + Ok(tt::Subtree { + delimiter: None, + token_trees: vec![ + tt::Leaf::Punct(punct.clone()).into(), + tt::Leaf::Ident(ident.clone()).into(), + ], + } + .into()) } pub(crate) fn expect_fragment( @@ -274,7 +289,10 @@ impl<'a> TtIter<'a> { } impl<'a> TreeSink for OffsetTokenSink<'a> { - fn token(&mut self, _kind: SyntaxKind, n_tokens: u8) { + fn token(&mut self, kind: SyntaxKind, mut n_tokens: u8) { + if kind == SyntaxKind::LIFETIME { + n_tokens = 2; + } for _ in 0..n_tokens { self.cursor = self.cursor.bump_subtree(); } @@ -286,7 +304,7 @@ impl<'a> TtIter<'a> { } } - let buffer = TokenBuffer::new(self.inner.as_slice()); + let buffer = TokenBuffer::new(&self.inner.as_slice()); let mut src = SubtreeTokenSource::new(&buffer); let mut sink = OffsetTokenSink { cursor: buffer.begin(), error: false }; @@ -422,7 +440,7 @@ fn match_meta_var(kind: &str, input: &mut TtIter) -> ExpandResult input.expect_tt().map(Some).map_err(|()| err!()), "lifetime" => input .expect_lifetime() - .map(|ident| Some(tt::Leaf::Ident(ident.clone()).into())) + .map(|tt| Some(tt)) .map_err(|()| err!("expected lifetime")), "literal" => input .expect_literal() -- cgit v1.2.3