mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
Factor out rules parsing
This commit is contained in:
parent
998ed13d09
commit
1997797adc
1 changed files with 20 additions and 24 deletions
|
@ -161,6 +161,18 @@ impl_froms!(TokenTree: Leaf, Subtree);
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn create_rules(macro_definition: &str) -> MacroRules {
|
||||||
|
let source_file = ast::SourceFile::parse(macro_definition);
|
||||||
|
let macro_definition = source_file
|
||||||
|
.syntax()
|
||||||
|
.descendants()
|
||||||
|
.find_map(ast::MacroCall::cast)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let definition_tt = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
|
||||||
|
crate::MacroRules::parse(&definition_tt).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
|
fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
|
||||||
let source_file = ast::SourceFile::parse(invocation);
|
let source_file = ast::SourceFile::parse(invocation);
|
||||||
let macro_invocation = source_file
|
let macro_invocation = source_file
|
||||||
|
@ -177,7 +189,8 @@ impl_froms!(TokenTree: Leaf, Subtree);
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fail_match_pattern_by_first_token() {
|
fn test_fail_match_pattern_by_first_token() {
|
||||||
let macro_definition = r#"
|
let rules = create_rules(
|
||||||
|
r#"
|
||||||
macro_rules! foo {
|
macro_rules! foo {
|
||||||
($ i:ident) => (
|
($ i:ident) => (
|
||||||
mod $ i {}
|
mod $ i {}
|
||||||
|
@ -189,17 +202,8 @@ impl_froms!(TokenTree: Leaf, Subtree);
|
||||||
struct $ i;
|
struct $ i;
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
"#;
|
"#,
|
||||||
|
);
|
||||||
let source_file = ast::SourceFile::parse(macro_definition);
|
|
||||||
let macro_definition = source_file
|
|
||||||
.syntax()
|
|
||||||
.descendants()
|
|
||||||
.find_map(ast::MacroCall::cast)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let definition_tt = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
|
|
||||||
let rules = crate::MacroRules::parse(&definition_tt).unwrap();
|
|
||||||
|
|
||||||
assert_expansion(&rules, "foo! { foo }", "mod foo {}");
|
assert_expansion(&rules, "foo! { foo }", "mod foo {}");
|
||||||
assert_expansion(&rules, "foo! { = bar }", "fn bar () {}");
|
assert_expansion(&rules, "foo! { = bar }", "fn bar () {}");
|
||||||
|
@ -208,7 +212,8 @@ impl_froms!(TokenTree: Leaf, Subtree);
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fail_match_pattern_by_last_token() {
|
fn test_fail_match_pattern_by_last_token() {
|
||||||
let macro_definition = r#"
|
let rules = create_rules(
|
||||||
|
r#"
|
||||||
macro_rules! foo {
|
macro_rules! foo {
|
||||||
($ i:ident) => (
|
($ i:ident) => (
|
||||||
mod $ i {}
|
mod $ i {}
|
||||||
|
@ -220,17 +225,8 @@ impl_froms!(TokenTree: Leaf, Subtree);
|
||||||
struct $ i;
|
struct $ i;
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
"#;
|
"#,
|
||||||
|
);
|
||||||
let source_file = ast::SourceFile::parse(macro_definition);
|
|
||||||
let macro_definition = source_file
|
|
||||||
.syntax()
|
|
||||||
.descendants()
|
|
||||||
.find_map(ast::MacroCall::cast)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let definition_tt = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
|
|
||||||
let rules = crate::MacroRules::parse(&definition_tt).unwrap();
|
|
||||||
|
|
||||||
assert_expansion(&rules, "foo! { foo }", "mod foo {}");
|
assert_expansion(&rules, "foo! { foo }", "mod foo {}");
|
||||||
assert_expansion(&rules, "foo! { bar = }", "fn bar () {}");
|
assert_expansion(&rules, "foo! { bar = }", "fn bar () {}");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue