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 | |
parent | a16f6bb27df1a9a9fcb506081bb30ccbf966e5c2 (diff) |
cleanup the api
Diffstat (limited to 'crates/ra_mbe/src')
-rw-r--r-- | crates/ra_mbe/src/lib.rs | 71 | ||||
-rw-r--r-- | crates/ra_mbe/src/mbe_parser.rs | 5 | ||||
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 50 |
3 files changed, 70 insertions, 56 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 | } | ||
diff --git a/crates/ra_mbe/src/mbe_parser.rs b/crates/ra_mbe/src/mbe_parser.rs index a76ea84db..a3e6abffc 100644 --- a/crates/ra_mbe/src/mbe_parser.rs +++ b/crates/ra_mbe/src/mbe_parser.rs | |||
@@ -1,10 +1,9 @@ | |||
1 | use crate::tt_cursor::TtCursor; | ||
2 | |||
3 | /// This module parses a raw `tt::TokenStream` into macro-by-example token | 1 | /// This module parses a raw `tt::TokenStream` into macro-by-example token |
4 | /// stream. This is a *mostly* identify function, expect for handling of | 2 | /// stream. This is a *mostly* identify function, expect for handling of |
5 | /// `$var:tt_kind` and `$(repeat),*` constructs. | 3 | /// `$var:tt_kind` and `$(repeat),*` constructs. |
4 | use crate::tt_cursor::TtCursor; | ||
6 | 5 | ||
7 | pub fn parse(tt: &tt::Subtree) -> Option<crate::MacroRules> { | 6 | pub(crate) fn parse(tt: &tt::Subtree) -> Option<crate::MacroRules> { |
8 | let mut parser = TtCursor::new(tt); | 7 | let mut parser = TtCursor::new(tt); |
9 | let mut rules = Vec::new(); | 8 | let mut rules = Vec::new(); |
10 | while !parser.is_eof() { | 9 | while !parser.is_eof() { |
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 | } | ||