aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/lib.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-01-31 19:14:28 +0000
committerAleksey Kladov <[email protected]>2019-01-31 20:23:30 +0000
commitb4b522fb3952fae365e02ffdc6ef05c8d75b7674 (patch)
tree8ae249dc026b71d91b0ce066d5fc0e2e146247eb /crates/ra_mbe/src/lib.rs
parenta16f6bb27df1a9a9fcb506081bb30ccbf966e5c2 (diff)
cleanup the api
Diffstat (limited to 'crates/ra_mbe/src/lib.rs')
-rw-r--r--crates/ra_mbe/src/lib.rs71
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
25pub use tt::{Delimiter, Punct}; 25pub use tt::{Delimiter, Punct};
26 26
27pub use crate::{ 27pub 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)]
34pub struct MacroRules { 34pub struct MacroRules {
35 pub(crate) rules: Vec<Rule>, 35 pub(crate) rules: Vec<Rule>,
36} 36}
37 37
38impl 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)]
39pub(crate) struct Rule { 48pub(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)]
107mod tests {
108 use ra_syntax::{ast, AstNode};
109
110 use super::*;
111
112 #[test]
113 fn test_convert_tt() {
114 let macro_definition = r#"
115macro_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#"
129impl_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}