mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
Add lifetime matcher
This commit is contained in:
parent
59b6cc780b
commit
313854c728
4 changed files with 25 additions and 2 deletions
|
@ -209,7 +209,6 @@ impl_froms!(TokenTree: Leaf, Subtree);
|
||||||
|
|
||||||
pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
|
pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
|
||||||
let expanded = expand(rules, invocation);
|
let expanded = expand(rules, invocation);
|
||||||
assert_eq!(expanded.to_string(), expansion);
|
|
||||||
|
|
||||||
let tree = token_tree_to_macro_items(&expanded);
|
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 () {}"#);
|
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();
|
input.eat_item().ok_or(ExpandError::UnexpectedToken)?.clone();
|
||||||
res.inner.insert(text.clone(), Binding::Simple(item.into()));
|
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),
|
_ => return Err(ExpandError::UnexpectedToken),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -157,7 +157,10 @@ fn convert_tt(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} 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 relative_range = token.range() - global_offset;
|
||||||
let id = token_map.alloc(relative_range);
|
let id = token_map.alloc(relative_range);
|
||||||
let text = token.text().clone();
|
let text = token.text().clone();
|
||||||
|
|
|
@ -119,6 +119,10 @@ impl<'a> TtCursor<'a> {
|
||||||
parser.parse_item()
|
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> {
|
pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ParseError> {
|
||||||
if self.at_char(char) {
|
if self.at_char(char) {
|
||||||
self.bump();
|
self.bump();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue