aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2019-04-19 14:38:26 +0100
committerEdwin Cheng <[email protected]>2019-04-19 14:38:26 +0100
commit87ff908135a28115593f8cf895d176aef331347c (patch)
tree5dd5b02918f9abbd3be1d6071ae266db63add4c6 /crates/ra_mbe/src
parentc5983b85fc9e520208684a8c625cdb96bb219b31 (diff)
Add vis matcher
Diffstat (limited to 'crates/ra_mbe/src')
-rw-r--r--crates/ra_mbe/src/lib.rs12
-rw-r--r--crates/ra_mbe/src/mbe_expander.rs4
-rw-r--r--crates/ra_mbe/src/subtree_parser.rs4
-rw-r--r--crates/ra_mbe/src/tt_cursor.rs5
4 files changed, 25 insertions, 0 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index b9dd22dc9..074be9043 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -809,4 +809,16 @@ MACRO_ITEMS@[0; 40)
809 ); 809 );
810 assert_expansion(&rules, r#"foo!(u8 0)"#, r#"const VALUE: u8 = 0;"#); 810 assert_expansion(&rules, r#"foo!(u8 0)"#, r#"const VALUE: u8 = 0;"#);
811 } 811 }
812
813 #[test]
814 fn test_vis() {
815 let rules = create_rules(
816 r#"
817 macro_rules! foo {
818 ($ vis:vis $ name:ident) => { $ vis fn $ name() {}};
819 }
820"#,
821 );
822 assert_expansion(&rules, r#"foo!(pub foo);"#, r#"pub fn foo() {}"#);
823 }
812} 824}
diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs
index 548b15535..01e29b556 100644
--- a/crates/ra_mbe/src/mbe_expander.rs
+++ b/crates/ra_mbe/src/mbe_expander.rs
@@ -193,6 +193,10 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings,
193 Binding::Simple(tt::Leaf::from(literal).into()), 193 Binding::Simple(tt::Leaf::from(literal).into()),
194 ); 194 );
195 } 195 }
196 "vis" => {
197 let vis = input.eat_vis().ok_or(ExpandError::UnexpectedToken)?.clone();
198 res.inner.insert(text.clone(), Binding::Simple(vis.into()));
199 }
196 200
197 _ => return Err(ExpandError::UnexpectedToken), 201 _ => return Err(ExpandError::UnexpectedToken),
198 } 202 }
diff --git a/crates/ra_mbe/src/subtree_parser.rs b/crates/ra_mbe/src/subtree_parser.rs
index 5d5557113..528aa0f8a 100644
--- a/crates/ra_mbe/src/subtree_parser.rs
+++ b/crates/ra_mbe/src/subtree_parser.rs
@@ -58,6 +58,10 @@ impl<'a> Parser<'a> {
58 self.parse(ra_parser::parse_item) 58 self.parse(ra_parser::parse_item)
59 } 59 }
60 60
61 pub fn parse_vis(self) -> Option<tt::TokenTree> {
62 self.parse(ra_parser::parse_vis)
63 }
64
61 fn parse<F>(self, f: F) -> Option<tt::TokenTree> 65 fn parse<F>(self, f: F) -> Option<tt::TokenTree>
62 where 66 where
63 F: FnOnce(&dyn TokenSource, &mut dyn TreeSink), 67 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 6184fb31f..818c412a0 100644
--- a/crates/ra_mbe/src/tt_cursor.rs
+++ b/crates/ra_mbe/src/tt_cursor.rs
@@ -137,6 +137,11 @@ impl<'a> TtCursor<'a> {
137 self.eat_ident().cloned().map(|ident| tt::Leaf::from(ident).into()) 137 self.eat_ident().cloned().map(|ident| tt::Leaf::from(ident).into())
138 } 138 }
139 139
140 pub(crate) fn eat_vis(&mut self) -> Option<tt::TokenTree> {
141 let parser = Parser::new(&mut self.pos, self.subtree);
142 parser.parse_vis()
143 }
144
140 pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ParseError> { 145 pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ParseError> {
141 if self.at_char(char) { 146 if self.at_char(char) {
142 self.bump(); 147 self.bump();