Add mbe stmt matcher

This commit is contained in:
Edwin Cheng 2019-04-17 12:34:43 +08:00
parent 546d9be2a7
commit 57e4122b89
7 changed files with 136 additions and 89 deletions

View file

@ -582,4 +582,19 @@ SOURCE_FILE@[0; 40)
);
assert_expansion(&rules, "foo! { (a, b) }", "fn foo () {let (a , b) ;}");
}
#[test]
fn test_stmt() {
let rules = create_rules(
r#"
macro_rules! foo {
($ i:stmt) => (
fn bar() { $ i; }
)
}
"#,
);
assert_expansion(&rules, "foo! { 2 }", "fn bar () {2 ;}");
assert_expansion(&rules, "foo! { let a = 0 }", "fn bar () {let a = 0 ;}");
}
}

View file

@ -157,6 +157,10 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings,
let pat = input.eat_pat().ok_or(ExpandError::UnexpectedToken)?.clone();
res.inner.insert(text.clone(), Binding::Simple(pat.into()));
}
"stmt" => {
let pat = input.eat_stmt().ok_or(ExpandError::UnexpectedToken)?.clone();
res.inner.insert(text.clone(), Binding::Simple(pat.into()));
}
_ => return Err(ExpandError::UnexpectedToken),
}
}

View file

@ -42,6 +42,10 @@ impl<'a> Parser<'a> {
self.parse(ra_parser::parse_pat)
}
pub fn parse_stmt(self) -> Option<tt::TokenTree> {
self.parse(|src, sink| ra_parser::parse_stmt(src, sink, false))
}
fn parse<F>(self, f: F) -> Option<tt::TokenTree>
where
F: FnOnce(&dyn TokenSource, &mut dyn TreeSink),

View file

@ -99,6 +99,11 @@ impl<'a> TtCursor<'a> {
parser.parse_pat()
}
pub(crate) fn eat_stmt(&mut self) -> Option<tt::TokenTree> {
let parser = Parser::new(&mut self.pos, self.subtree);
parser.parse_stmt()
}
pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ParseError> {
if self.at_char(char) {
self.bump();