From 57e4122b890d56c11f9d74c1bdfed40f186331a4 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 17 Apr 2019 12:34:43 +0800 Subject: Add mbe stmt matcher --- crates/ra_mbe/src/lib.rs | 15 +++++++++++++++ crates/ra_mbe/src/mbe_expander.rs | 4 ++++ crates/ra_mbe/src/subtree_parser.rs | 4 ++++ crates/ra_mbe/src/tt_cursor.rs | 5 +++++ 4 files changed, 28 insertions(+) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index a530f3b03..a1f438906 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -582,4 +582,19 @@ SOURCE_FILE@[0; 40) ); assert_expansion(&rules, "foo! { (a, b) }", "fn foo () {let (a , b) ;}"); } + + #[test] + fn test_stmt() { + let rules = create_rules( + r#" + macro_rules! foo { + ($ i:stmt) => ( + fn bar() { $ i; } + ) + } +"#, + ); + assert_expansion(&rules, "foo! { 2 }", "fn bar () {2 ;}"); + assert_expansion(&rules, "foo! { let a = 0 }", "fn bar () {let a = 0 ;}"); + } } diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index 7a259f338..7587b575d 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs @@ -157,6 +157,10 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result { + let pat = input.eat_stmt().ok_or(ExpandError::UnexpectedToken)?.clone(); + res.inner.insert(text.clone(), Binding::Simple(pat.into())); + } _ => return Err(ExpandError::UnexpectedToken), } } diff --git a/crates/ra_mbe/src/subtree_parser.rs b/crates/ra_mbe/src/subtree_parser.rs index 13d5d2169..f075ce245 100644 --- a/crates/ra_mbe/src/subtree_parser.rs +++ b/crates/ra_mbe/src/subtree_parser.rs @@ -42,6 +42,10 @@ impl<'a> Parser<'a> { self.parse(ra_parser::parse_pat) } + pub fn parse_stmt(self) -> Option { + self.parse(|src, sink| ra_parser::parse_stmt(src, sink, false)) + } + fn parse(self, f: F) -> Option where F: FnOnce(&dyn TokenSource, &mut dyn TreeSink), diff --git a/crates/ra_mbe/src/tt_cursor.rs b/crates/ra_mbe/src/tt_cursor.rs index f6cefe087..adfe5520d 100644 --- a/crates/ra_mbe/src/tt_cursor.rs +++ b/crates/ra_mbe/src/tt_cursor.rs @@ -99,6 +99,11 @@ impl<'a> TtCursor<'a> { parser.parse_pat() } + pub(crate) fn eat_stmt(&mut self) -> Option { + let parser = Parser::new(&mut self.pos, self.subtree); + parser.parse_stmt() + } + pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ParseError> { if self.at_char(char) { self.bump(); -- cgit v1.2.3