Add lifetime matcher
This commit is contained in:
parent
59b6cc780b
commit
313854c728
@ -209,7 +209,6 @@ impl_froms!(TokenTree: Leaf, Subtree);
|
||||
|
||||
pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
|
||||
let expanded = expand(rules, invocation);
|
||||
assert_eq!(expanded.to_string(), expansion);
|
||||
|
||||
let tree = token_tree_to_macro_items(&expanded);
|
||||
|
||||
@ -786,4 +785,16 @@ MACRO_ITEMS@[0; 40)
|
||||
);
|
||||
assert_expansion(&rules, r#"foo! { fn foo() {} }"#, r#"fn foo () {}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lifetime() {
|
||||
let rules = create_rules(
|
||||
r#"
|
||||
macro_rules! foo {
|
||||
($ lt:lifetime) => { struct Ref<$ lt>{ s: &$ lt str } }
|
||||
}
|
||||
"#,
|
||||
);
|
||||
assert_expansion(&rules, r#"foo!{'a}"#, r#"struct Ref < 'a > {s : & 'a str}"#);
|
||||
}
|
||||
}
|
||||
|
@ -180,6 +180,11 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings,
|
||||
input.eat_item().ok_or(ExpandError::UnexpectedToken)?.clone();
|
||||
res.inner.insert(text.clone(), Binding::Simple(item.into()));
|
||||
}
|
||||
"lifetime" => {
|
||||
let lifetime =
|
||||
input.eat_lifetime().ok_or(ExpandError::UnexpectedToken)?.clone();
|
||||
res.inner.insert(text.clone(), Binding::Simple(lifetime.into()));
|
||||
}
|
||||
_ => return Err(ExpandError::UnexpectedToken),
|
||||
}
|
||||
}
|
||||
|
@ -157,7 +157,10 @@ fn convert_tt(
|
||||
);
|
||||
}
|
||||
} else {
|
||||
let child = if token.kind().is_keyword() || token.kind() == IDENT {
|
||||
let child: tt::TokenTree = if token.kind().is_keyword()
|
||||
|| token.kind() == IDENT
|
||||
|| token.kind() == LIFETIME
|
||||
{
|
||||
let relative_range = token.range() - global_offset;
|
||||
let id = token_map.alloc(relative_range);
|
||||
let text = token.text().clone();
|
||||
|
@ -119,6 +119,10 @@ impl<'a> TtCursor<'a> {
|
||||
parser.parse_item()
|
||||
}
|
||||
|
||||
pub(crate) fn eat_lifetime(&mut self) -> Option<tt::TokenTree> {
|
||||
self.eat_ident().cloned().map(|ident| tt::Leaf::from(ident).into())
|
||||
}
|
||||
|
||||
pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ParseError> {
|
||||
if self.at_char(char) {
|
||||
self.bump();
|
||||
|
Loading…
x
Reference in New Issue
Block a user