diff options
author | Aleksey Kladov <[email protected]> | 2019-01-31 19:14:28 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-31 20:23:30 +0000 |
commit | b4b522fb3952fae365e02ffdc6ef05c8d75b7674 (patch) | |
tree | 8ae249dc026b71d91b0ce066d5fc0e2e146247eb /crates/ra_mbe/src/syntax_bridge.rs | |
parent | a16f6bb27df1a9a9fcb506081bb30ccbf966e5c2 (diff) |
cleanup the api
Diffstat (limited to 'crates/ra_mbe/src/syntax_bridge.rs')
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 50 |
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 @@ | |||
1 | use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxKind::*}; | 1 | use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxKind::*}; |
2 | 2 | ||
3 | pub fn macro_call_to_tt(call: &ast::MacroCall) -> Option<tt::Subtree> { | 3 | pub 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 | ||
8 | fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> { | 7 | fn 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] | ||
71 | fn test_convert_tt() { | ||
72 | let macro_definition = r#" | ||
73 | macro_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#" | ||
87 | impl_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 | } | ||