From 1997797adc4453718aa95603926950343c1616bc Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Sun, 3 Feb 2019 15:06:59 -0500 Subject: Factor out rules parsing --- crates/ra_mbe/src/lib.rs | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) (limited to 'crates') diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index ec12192cc..6b648e7af 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -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) { let source_file = ast::SourceFile::parse(invocation); let macro_invocation = source_file @@ -177,7 +189,8 @@ impl_froms!(TokenTree: Leaf, Subtree); #[test] fn test_fail_match_pattern_by_first_token() { - let macro_definition = r#" + let rules = create_rules( + r#" macro_rules! foo { ($ i:ident) => ( mod $ i {} @@ -189,17 +202,8 @@ impl_froms!(TokenTree: Leaf, Subtree); 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! { = bar }", "fn bar () {}"); @@ -208,7 +212,8 @@ impl_froms!(TokenTree: Leaf, Subtree); #[test] fn test_fail_match_pattern_by_last_token() { - let macro_definition = r#" + let rules = create_rules( + r#" macro_rules! foo { ($ i:ident) => ( mod $ i {} @@ -220,17 +225,8 @@ impl_froms!(TokenTree: Leaf, Subtree); 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! { bar = }", "fn bar () {}"); -- cgit v1.2.3 From 0000f007873a3de2b454bd808083c0c0e8c6c6fa Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Sun, 3 Feb 2019 15:16:55 -0500 Subject: mbe: Add support matching for matching idents --- crates/ra_mbe/src/lib.rs | 24 ++++++++++++++++++++++++ crates/ra_mbe/src/mbe_expander.rs | 5 +++++ 2 files changed, 29 insertions(+) (limited to 'crates') diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 6b648e7af..6f719acbf 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -232,4 +232,28 @@ impl_froms!(TokenTree: Leaf, Subtree); assert_expansion(&rules, "foo! { bar = }", "fn bar () {}"); assert_expansion(&rules, "foo! { Baz + }", "struct Baz ;"); } + + #[test] + fn test_fail_match_pattern_by_word_token() { + let rules = create_rules( + r#" + macro_rules! foo { + ($ i:ident) => ( + mod $ i {} + ); + (spam $ i:ident) => ( + fn $ i() {} + ); + (eggs $ i:ident) => ( + struct $ i; + ) + } +"#, + ); + + assert_expansion(&rules, "foo! { foo }", "mod foo {}"); + assert_expansion(&rules, "foo! { spam bar }", "fn bar () {}"); + assert_expansion(&rules, "foo! { eggs Baz }", "struct Baz ;"); + } + } diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index 2945e7359..212e2ea92 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs @@ -126,6 +126,11 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Option return None; } } + crate::Leaf::Ident(ident) => { + if input.eat_ident()?.text != ident.text { + return None; + } + } _ => return None, }, crate::TokenTree::Repeat(crate::Repeat { -- cgit v1.2.3