Add vis matcher

This commit is contained in:
Edwin Cheng 2019-04-19 21:38:26 +08:00
parent c5983b85fc
commit 87ff908135
6 changed files with 33 additions and 1 deletions

View file

@ -809,4 +809,16 @@ MACRO_ITEMS@[0; 40)
);
assert_expansion(&rules, r#"foo!(u8 0)"#, r#"const VALUE: u8 = 0;"#);
}
#[test]
fn test_vis() {
let rules = create_rules(
r#"
macro_rules! foo {
($ vis:vis $ name:ident) => { $ vis fn $ name() {}};
}
"#,
);
assert_expansion(&rules, r#"foo!(pub foo);"#, r#"pub fn foo() {}"#);
}
}

View file

@ -193,6 +193,10 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings,
Binding::Simple(tt::Leaf::from(literal).into()),
);
}
"vis" => {
let vis = input.eat_vis().ok_or(ExpandError::UnexpectedToken)?.clone();
res.inner.insert(text.clone(), Binding::Simple(vis.into()));
}
_ => return Err(ExpandError::UnexpectedToken),
}

View file

@ -58,6 +58,10 @@ impl<'a> Parser<'a> {
self.parse(ra_parser::parse_item)
}
pub fn parse_vis(self) -> Option<tt::TokenTree> {
self.parse(ra_parser::parse_vis)
}
fn parse<F>(self, f: F) -> Option<tt::TokenTree>
where
F: FnOnce(&dyn TokenSource, &mut dyn TreeSink),

View file

@ -137,6 +137,11 @@ impl<'a> TtCursor<'a> {
self.eat_ident().cloned().map(|ident| tt::Leaf::from(ident).into())
}
pub(crate) fn eat_vis(&mut self) -> Option<tt::TokenTree> {
let parser = Parser::new(&mut self.pos, self.subtree);
parser.parse_vis()
}
pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ParseError> {
if self.at_char(char) {
self.bump();