aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/syntax_bridge.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe/src/syntax_bridge.rs')
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs50
1 files changed, 2 insertions, 48 deletions
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs
index 3223f6ea5..2dc04d4e7 100644
--- a/crates/ra_mbe/src/syntax_bridge.rs
+++ b/crates/ra_mbe/src/syntax_bridge.rs
@@ -1,8 +1,7 @@
1use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxKind::*}; 1use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxKind::*};
2 2
3pub fn macro_call_to_tt(call: &ast::MacroCall) -> Option<tt::Subtree> { 3pub fn ast_to_token_tree(ast: &ast::TokenTree) -> Option<tt::Subtree> {
4 let tt = call.token_tree()?; 4 convert_tt(ast.syntax())
5 convert_tt(tt.syntax())
6} 5}
7 6
8fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> { 7fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> {
@@ -66,48 +65,3 @@ fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> {
66 }; 65 };
67 Some(res) 66 Some(res)
68} 67}
69
70#[test]
71fn test_convert_tt() {
72 let macro_definition = r#"
73macro_rules! impl_froms {
74 ($e:ident: $($v:ident),*) => {
75 $(
76 impl From<$v> for $e {
77 fn from(it: $v) -> $e {
78 $e::$v(it)
79 }
80 }
81 )*
82 }
83}
84"#;
85
86 let macro_invocation = r#"
87impl_froms!(TokenTree: Leaf, Subtree);
88"#;
89
90 let source_file = ast::SourceFile::parse(macro_definition);
91 let macro_definition = source_file
92 .syntax()
93 .descendants()
94 .find_map(ast::MacroCall::cast)
95 .unwrap();
96
97 let source_file = ast::SourceFile::parse(macro_invocation);
98 let macro_invocation = source_file
99 .syntax()
100 .descendants()
101 .find_map(ast::MacroCall::cast)
102 .unwrap();
103
104 let definition_tt = macro_call_to_tt(macro_definition).unwrap();
105 let invocation_tt = macro_call_to_tt(macro_invocation).unwrap();
106 let mbe = crate::parse(&definition_tt).unwrap();
107 let expansion = crate::exapnd(&mbe, &invocation_tt).unwrap();
108 assert_eq!(
109 expansion.to_string(),
110 "impl From < Leaf > for TokenTree {fn from (it : Leaf) -> TokenTree {TokenTree :: Leaf (it)}} \
111 impl From < Subtree > for TokenTree {fn from (it : Subtree) -> TokenTree {TokenTree :: Subtree (it)}}"
112 )
113}