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/lib.rs | |
parent | a16f6bb27df1a9a9fcb506081bb30ccbf966e5c2 (diff) |
cleanup the api
Diffstat (limited to 'crates/ra_mbe/src/lib.rs')
-rw-r--r-- | crates/ra_mbe/src/lib.rs | 71 |
1 files changed, 66 insertions, 5 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index c7be33b19..af3ccc0f5 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs | |||
@@ -24,17 +24,26 @@ use ra_syntax::SmolStr; | |||
24 | 24 | ||
25 | pub use tt::{Delimiter, Punct}; | 25 | pub use tt::{Delimiter, Punct}; |
26 | 26 | ||
27 | pub use crate::{ | 27 | pub use crate::syntax_bridge::ast_to_token_tree; |
28 | mbe_parser::parse, | ||
29 | mbe_expander::exapnd, | ||
30 | syntax_bridge::macro_call_to_tt, | ||
31 | }; | ||
32 | 28 | ||
29 | /// This struct contains AST for a single `macro_rules` defenition. What might | ||
30 | /// be very confusing is that AST has almost exactly the same shape as | ||
31 | /// `tt::TokenTree`, but there's a crucial difference: in macro rules, `$ident` | ||
32 | /// and `$()*` have special meaning (see `Var` and `Repeat` data structures) | ||
33 | #[derive(Debug)] | 33 | #[derive(Debug)] |
34 | pub struct MacroRules { | 34 | pub struct MacroRules { |
35 | pub(crate) rules: Vec<Rule>, | 35 | pub(crate) rules: Vec<Rule>, |
36 | } | 36 | } |
37 | 37 | ||
38 | impl MacroRules { | ||
39 | pub fn parse(tt: &tt::Subtree) -> Option<MacroRules> { | ||
40 | mbe_parser::parse(tt) | ||
41 | } | ||
42 | pub fn expand(&self, tt: &tt::Subtree) -> Option<tt::Subtree> { | ||
43 | mbe_expander::exapnd(self, tt) | ||
44 | } | ||
45 | } | ||
46 | |||
38 | #[derive(Debug)] | 47 | #[derive(Debug)] |
39 | pub(crate) struct Rule { | 48 | pub(crate) struct Rule { |
40 | pub(crate) lhs: Subtree, | 49 | pub(crate) lhs: Subtree, |
@@ -93,3 +102,55 @@ pub(crate) struct Var { | |||
93 | pub(crate) text: SmolStr, | 102 | pub(crate) text: SmolStr, |
94 | pub(crate) kind: Option<SmolStr>, | 103 | pub(crate) kind: Option<SmolStr>, |
95 | } | 104 | } |
105 | |||
106 | #[cfg(test)] | ||
107 | mod tests { | ||
108 | use ra_syntax::{ast, AstNode}; | ||
109 | |||
110 | use super::*; | ||
111 | |||
112 | #[test] | ||
113 | fn test_convert_tt() { | ||
114 | let macro_definition = r#" | ||
115 | macro_rules! impl_froms { | ||
116 | ($e:ident: $($v:ident),*) => { | ||
117 | $( | ||
118 | impl From<$v> for $e { | ||
119 | fn from(it: $v) -> $e { | ||
120 | $e::$v(it) | ||
121 | } | ||
122 | } | ||
123 | )* | ||
124 | } | ||
125 | } | ||
126 | "#; | ||
127 | |||
128 | let macro_invocation = r#" | ||
129 | impl_froms!(TokenTree: Leaf, Subtree); | ||
130 | "#; | ||
131 | |||
132 | let source_file = ast::SourceFile::parse(macro_definition); | ||
133 | let macro_definition = source_file | ||
134 | .syntax() | ||
135 | .descendants() | ||
136 | .find_map(ast::MacroCall::cast) | ||
137 | .unwrap(); | ||
138 | |||
139 | let source_file = ast::SourceFile::parse(macro_invocation); | ||
140 | let macro_invocation = source_file | ||
141 | .syntax() | ||
142 | .descendants() | ||
143 | .find_map(ast::MacroCall::cast) | ||
144 | .unwrap(); | ||
145 | |||
146 | let definition_tt = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap(); | ||
147 | let invocation_tt = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap(); | ||
148 | let rules = crate::MacroRules::parse(&definition_tt).unwrap(); | ||
149 | let expansion = rules.expand(&invocation_tt).unwrap(); | ||
150 | assert_eq!( | ||
151 | expansion.to_string(), | ||
152 | "impl From < Leaf > for TokenTree {fn from (it : Leaf) -> TokenTree {TokenTree :: Leaf (it)}} \ | ||
153 | impl From < Subtree > for TokenTree {fn from (it : Subtree) -> TokenTree {TokenTree :: Subtree (it)}}" | ||
154 | ) | ||
155 | } | ||
156 | } | ||