From 762819864fd78f2e8904a6bde6181b80895db360 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 19 Apr 2019 19:33:29 +0800 Subject: add block matcher --- crates/ra_mbe/src/lib.rs | 19 +++++++++++++++++++ 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, 33 insertions(+) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 8e3167e55..d7462d09d 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -743,4 +743,23 @@ MACRO_ITEMS@[0; 40) ); assert_expansion(&rules, "foo! { { 1; } }", "fn foo () {1 ;}"); } + + #[test] + fn test_meta() { + let rules = create_rules( + r#" + macro_rules! foo { + ($ i:meta) => ( + #[$ i] + fn bar() {} + ) + } +"#, + ); + assert_expansion( + &rules, + r#"foo! { cfg(target_os = "windows") }"#, + r#"# [cfg (target_os = "windows")] fn bar () {}"#, + ); + } } diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index a74e477d6..6ada580cc 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs @@ -166,6 +166,11 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result { + let meta = + input.eat_meta().ok_or(ExpandError::UnexpectedToken)?.clone(); + res.inner.insert(text.clone(), Binding::Simple(meta.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 2b11186c4..5d5557113 100644 --- a/crates/ra_mbe/src/subtree_parser.rs +++ b/crates/ra_mbe/src/subtree_parser.rs @@ -50,6 +50,10 @@ impl<'a> Parser<'a> { self.parse(ra_parser::parse_block) } + pub fn parse_meta(self) -> Option { + self.parse(ra_parser::parse_meta) + } + 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 d700aad69..04bb6b563 100644 --- a/crates/ra_mbe/src/tt_cursor.rs +++ b/crates/ra_mbe/src/tt_cursor.rs @@ -109,6 +109,11 @@ impl<'a> TtCursor<'a> { parser.parse_block() } + pub(crate) fn eat_meta(&mut self) -> Option { + let parser = Parser::new(&mut self.pos, self.subtree); + parser.parse_meta() + } + pub(crate) fn eat_item(&mut self) -> Option { let parser = Parser::new(&mut self.pos, self.subtree); parser.parse_item() -- cgit v1.2.3