From 8092b6487f301bf9219c55fc714744fa2616fb9a Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 19 Apr 2019 18:30:43 +0800 Subject: Add block matcher --- crates/ra_mbe/src/lib.rs | 12 ++++++++++++ crates/ra_mbe/src/mbe_expander.rs | 5 +++++ crates/ra_mbe/src/subtree_parser.rs | 4 ++++ crates/ra_mbe/src/tt_cursor.rs | 5 +++++ 4 files changed, 26 insertions(+) (limited to 'crates/ra_mbe/src') diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 9d4744838..8e3167e55 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -731,4 +731,16 @@ MACRO_ITEMS@[0; 40) } "#, r#"extern crate a ; mod b ; mod c {} use d ; const E : i32 = 0 ; static F : i32 = 0 ; impl G {} struct H ; enum I {Foo} trait J {} fn h () {} extern {} type T = u8 ;"#); } + + #[test] + fn test_block() { + let rules = create_rules( + r#" + macro_rules! foo { + ($ i:block) => { fn foo() $ i } + } +"#, + ); + assert_expansion(&rules, "foo! { { 1; } }", "fn foo () {1 ;}"); + } } diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index acba42809..a74e477d6 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs @@ -161,6 +161,11 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result { + let block = + input.eat_block().ok_or(ExpandError::UnexpectedToken)?.clone(); + res.inner.insert(text.clone(), Binding::Simple(block.into())); + } "item" => { let item = input.eat_item().ok_or(ExpandError::UnexpectedToken)?.clone(); diff --git a/crates/ra_mbe/src/subtree_parser.rs b/crates/ra_mbe/src/subtree_parser.rs index 195e4c3ac..2b11186c4 100644 --- a/crates/ra_mbe/src/subtree_parser.rs +++ b/crates/ra_mbe/src/subtree_parser.rs @@ -46,6 +46,10 @@ impl<'a> Parser<'a> { self.parse(|src, sink| ra_parser::parse_stmt(src, sink, false)) } + pub fn parse_block(self) -> Option { + self.parse(ra_parser::parse_block) + } + pub fn parse_item(self) -> Option { self.parse(ra_parser::parse_item) } diff --git a/crates/ra_mbe/src/tt_cursor.rs b/crates/ra_mbe/src/tt_cursor.rs index 484437b0e..d700aad69 100644 --- a/crates/ra_mbe/src/tt_cursor.rs +++ b/crates/ra_mbe/src/tt_cursor.rs @@ -104,6 +104,11 @@ impl<'a> TtCursor<'a> { parser.parse_stmt() } + pub(crate) fn eat_block(&mut self) -> Option { + let parser = Parser::new(&mut self.pos, self.subtree); + parser.parse_block() + } + pub(crate) fn eat_item(&mut self) -> Option { let parser = Parser::new(&mut self.pos, self.subtree); parser.parse_item() -- cgit v1.2.3