aboutsummaryrefslogtreecommitdiff
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
parentc5983b85fc9e520208684a8c625cdb96bb219b31 (diff)
Add vis matcher
-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
-rw-r--r--crates/ra_parser/src/grammar.rs2
-rw-r--r--crates/ra_parser/src/lib.rs7
6 files changed, 33 insertions, 1 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();
diff --git a/crates/ra_parser/src/grammar.rs b/crates/ra_parser/src/grammar.rs
index 13c50c79c..67eae749d 100644
--- a/crates/ra_parser/src/grammar.rs
+++ b/crates/ra_parser/src/grammar.rs
@@ -167,7 +167,7 @@ impl BlockLike {
167 } 167 }
168} 168}
169 169
170fn opt_visibility(p: &mut Parser) -> bool { 170pub(crate) fn opt_visibility(p: &mut Parser) -> bool {
171 match p.current() { 171 match p.current() {
172 PUB_KW => { 172 PUB_KW => {
173 let m = p.start(); 173 let m = p.start();
diff --git a/crates/ra_parser/src/lib.rs b/crates/ra_parser/src/lib.rs
index 4787b5b9e..970d699c0 100644
--- a/crates/ra_parser/src/lib.rs
+++ b/crates/ra_parser/src/lib.rs
@@ -107,6 +107,13 @@ pub fn parse_item(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink)
107 parse_from_tokens(token_source, tree_sink, grammar::item); 107 parse_from_tokens(token_source, tree_sink, grammar::item);
108} 108}
109 109
110/// Parse given tokens into the given sink as an visibility qualifier
111pub fn parse_vis(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) {
112 parse_from_tokens(token_source, tree_sink, |p| {
113 grammar::opt_visibility(p);
114 });
115}
116
110pub fn parse_macro_items(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) { 117pub fn parse_macro_items(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) {
111 parse_from_tokens(token_source, tree_sink, grammar::macro_items); 118 parse_from_tokens(token_source, tree_sink, grammar::macro_items);
112} 119}