1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
/// `mbe` (short for Macro By Example) crate contains code for handling
/// `macro_rules` macros. It uses `TokenTree` (from `ra_tt` package) as the
/// interface, although it contains some code to bridge `SyntaxNode`s and
/// `TokenTree`s as well!
macro_rules! impl_froms {
($e:ident: $($v:ident), *) => {
$(
impl From<$v> for $e {
fn from(it: $v) -> $e {
$e::$v(it)
}
}
)*
}
}
mod tt_cursor;
mod mbe_parser;
mod mbe_expander;
mod syntax_bridge;
use ra_syntax::SmolStr;
pub use tt::{Delimiter, Punct};
pub use crate::syntax_bridge::ast_to_token_tree;
/// This struct contains AST for a single `macro_rules` definition. What might
/// be very confusing is that AST has almost exactly the same shape as
/// `tt::TokenTree`, but there's a crucial difference: in macro rules, `$ident`
/// and `$()*` have special meaning (see `Var` and `Repeat` data structures)
#[derive(Debug, PartialEq, Eq)]
pub struct MacroRules {
pub(crate) rules: Vec<Rule>,
}
impl MacroRules {
pub fn parse(tt: &tt::Subtree) -> Option<MacroRules> {
mbe_parser::parse(tt)
}
pub fn expand(&self, tt: &tt::Subtree) -> Option<tt::Subtree> {
mbe_expander::exapnd(self, tt)
}
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Rule {
pub(crate) lhs: Subtree,
pub(crate) rhs: Subtree,
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum TokenTree {
Leaf(Leaf),
Subtree(Subtree),
Repeat(Repeat),
}
impl_froms!(TokenTree: Leaf, Subtree, Repeat);
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum Leaf {
Literal(Literal),
Punct(Punct),
Ident(Ident),
Var(Var),
}
impl_froms!(Leaf: Literal, Punct, Ident, Var);
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Subtree {
pub(crate) delimiter: Delimiter,
pub(crate) token_trees: Vec<TokenTree>,
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Repeat {
pub(crate) subtree: Subtree,
pub(crate) kind: RepeatKind,
pub(crate) separator: Option<char>,
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum RepeatKind {
ZeroOrMore,
OneOrMore,
ZeroOrOne,
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Literal {
pub(crate) text: SmolStr,
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Ident {
pub(crate) text: SmolStr,
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Var {
pub(crate) text: SmolStr,
pub(crate) kind: Option<SmolStr>,
}
#[cfg(test)]
mod tests {
use ra_syntax::{ast, AstNode};
use super::*;
// Good first issue (although a slightly chellegning one):
//
// * Pick a random test from here
// https://github.com/intellij-rust/intellij-rust/blob/c4e9feee4ad46e7953b1948c112533360b6087bb/src/test/kotlin/org/rust/lang/core/macros/RsMacroExpansionTest.kt
// * Port the test to rust and add it to this module
// * Make it pass :-)
#[test]
fn test_convert_tt() {
let macro_definition = r#"
macro_rules! impl_froms {
($e:ident: $($v:ident),*) => {
$(
impl From<$v> for $e {
fn from(it: $v) -> $e {
$e::$v(it)
}
}
)*
}
}
"#;
let macro_invocation = r#"
impl_froms!(TokenTree: Leaf, Subtree);
"#;
let source_file = ast::SourceFile::parse(macro_definition);
let macro_definition =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
let source_file = ast::SourceFile::parse(macro_invocation);
let macro_invocation =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
let rules = crate::MacroRules::parse(&definition_tt).unwrap();
let expansion = rules.expand(&invocation_tt).unwrap();
assert_eq!(
expansion.to_string(),
"impl From < Leaf > for TokenTree {fn from (it : Leaf) -> TokenTree {TokenTree :: Leaf (it)}} \
impl From < Subtree > for TokenTree {fn from (it : Subtree) -> TokenTree {TokenTree :: Subtree (it)}}"
)
}
fn create_rules(macro_definition: &str) -> MacroRules {
let source_file = ast::SourceFile::parse(macro_definition);
let macro_definition =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
crate::MacroRules::parse(&definition_tt).unwrap()
}
fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
let source_file = ast::SourceFile::parse(invocation);
let macro_invocation =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
let expaned = rules.expand(&invocation_tt).unwrap();
assert_eq!(expaned.to_string(), expansion);
}
#[test]
fn test_fail_match_pattern_by_first_token() {
let rules = create_rules(
r#"
macro_rules! foo {
($ i:ident) => (
mod $ i {}
);
(= $ i:ident) => (
fn $ i() {}
);
(+ $ i:ident) => (
struct $ i;
)
}
"#,
);
assert_expansion(&rules, "foo! { foo }", "mod foo {}");
assert_expansion(&rules, "foo! { = bar }", "fn bar () {}");
assert_expansion(&rules, "foo! { + Baz }", "struct Baz ;");
}
#[test]
fn test_fail_match_pattern_by_last_token() {
let rules = create_rules(
r#"
macro_rules! foo {
($ i:ident) => (
mod $ i {}
);
($ i:ident =) => (
fn $ i() {}
);
($ i:ident +) => (
struct $ i;
)
}
"#,
);
assert_expansion(&rules, "foo! { foo }", "mod foo {}");
assert_expansion(&rules, "foo! { bar = }", "fn bar () {}");
assert_expansion(&rules, "foo! { Baz + }", "struct Baz ;");
}
#[test]
fn test_fail_match_pattern_by_word_token() {
let rules = create_rules(
r#"
macro_rules! foo {
($ i:ident) => (
mod $ i {}
);
(spam $ i:ident) => (
fn $ i() {}
);
(eggs $ i:ident) => (
struct $ i;
)
}
"#,
);
assert_expansion(&rules, "foo! { foo }", "mod foo {}");
assert_expansion(&rules, "foo! { spam bar }", "fn bar () {}");
assert_expansion(&rules, "foo! { eggs Baz }", "struct Baz ;");
}
#[test]
fn test_match_group_pattern_by_separator_token() {
let rules = create_rules(
r#"
macro_rules! foo {
($ ($ i:ident),*) => ($ (
mod $ i {}
)*);
($ ($ i:ident)#*) => ($ (
fn $ i() {}
)*);
($ i:ident ,# $ j:ident) => (
struct $ i;
struct $ j;
)
}
"#,
);
assert_expansion(&rules, "foo! { foo, bar }", "mod foo {} mod bar {}");
assert_expansion(&rules, "foo! { foo# bar }", "fn foo () {} fn bar () {}");
assert_expansion(&rules, "foo! { Foo,# Bar }", "struct Foo ; struct Bar ;");
}
}
|