diff options
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/macros.rs | 26 | ||||
-rw-r--r-- | crates/ra_hir/src/macros/mbe.rs | 16 | ||||
-rw-r--r-- | crates/ra_hir/src/macros/tt.rs | 7 |
3 files changed, 47 insertions, 2 deletions
diff --git a/crates/ra_hir/src/macros.rs b/crates/ra_hir/src/macros.rs index c23ad53cc..4740e5337 100644 --- a/crates/ra_hir/src/macros.rs +++ b/crates/ra_hir/src/macros.rs | |||
@@ -250,3 +250,29 @@ fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> { | |||
250 | }; | 250 | }; |
251 | Some(res) | 251 | Some(res) |
252 | } | 252 | } |
253 | |||
254 | #[test] | ||
255 | fn test_convert_tt() { | ||
256 | let text = r#" | ||
257 | macro_rules! impl_froms { | ||
258 | ($e:ident: $($v:ident), *) => { | ||
259 | $( | ||
260 | impl From<$v> for $e { | ||
261 | fn from(it: $v) -> $e { | ||
262 | $e::$v(it) | ||
263 | } | ||
264 | } | ||
265 | )* | ||
266 | } | ||
267 | } | ||
268 | "#; | ||
269 | let source_file = ast::SourceFile::parse(text); | ||
270 | let maco_call = source_file | ||
271 | .syntax() | ||
272 | .descendants() | ||
273 | .find_map(ast::MacroCall::cast) | ||
274 | .unwrap(); | ||
275 | let tt = macro_call_to_tt(maco_call).unwrap(); | ||
276 | let tt = mbe::parse(&tt); | ||
277 | dbg!(tt); | ||
278 | } | ||
diff --git a/crates/ra_hir/src/macros/mbe.rs b/crates/ra_hir/src/macros/mbe.rs index 1408ed511..7af7066fb 100644 --- a/crates/ra_hir/src/macros/mbe.rs +++ b/crates/ra_hir/src/macros/mbe.rs | |||
@@ -2,21 +2,25 @@ use ra_syntax::SmolStr; | |||
2 | 2 | ||
3 | use crate::macros::tt; | 3 | use crate::macros::tt; |
4 | 4 | ||
5 | struct MacroRules { | 5 | #[derive(Debug)] |
6 | pub(crate) struct MacroRules { | ||
6 | rules: Vec<Rule>, | 7 | rules: Vec<Rule>, |
7 | } | 8 | } |
8 | 9 | ||
10 | #[derive(Debug)] | ||
9 | struct Rule { | 11 | struct Rule { |
10 | lhs: TokenTree, | 12 | lhs: TokenTree, |
11 | rhs: TokenTree, | 13 | rhs: TokenTree, |
12 | } | 14 | } |
13 | 15 | ||
16 | #[derive(Debug)] | ||
14 | enum TokenTree { | 17 | enum TokenTree { |
15 | Leaf(Leaf), | 18 | Leaf(Leaf), |
16 | Subtree(Subtree), | 19 | Subtree(Subtree), |
17 | Repeat(Repeat), | 20 | Repeat(Repeat), |
18 | } | 21 | } |
19 | 22 | ||
23 | #[derive(Debug)] | ||
20 | enum Leaf { | 24 | enum Leaf { |
21 | Literal(Literal), | 25 | Literal(Literal), |
22 | Punct(Punct), | 26 | Punct(Punct), |
@@ -24,11 +28,13 @@ enum Leaf { | |||
24 | Var(Var), | 28 | Var(Var), |
25 | } | 29 | } |
26 | 30 | ||
31 | #[derive(Debug)] | ||
27 | struct Subtree { | 32 | struct Subtree { |
28 | delimiter: Delimiter, | 33 | delimiter: Delimiter, |
29 | token_trees: Vec<TokenTree>, | 34 | token_trees: Vec<TokenTree>, |
30 | } | 35 | } |
31 | 36 | ||
37 | #[derive(Debug)] | ||
32 | enum Delimiter { | 38 | enum Delimiter { |
33 | Parenthesis, | 39 | Parenthesis, |
34 | Brace, | 40 | Brace, |
@@ -36,33 +42,39 @@ enum Delimiter { | |||
36 | None, | 42 | None, |
37 | } | 43 | } |
38 | 44 | ||
45 | #[derive(Debug)] | ||
39 | struct Repeat { | 46 | struct Repeat { |
40 | subtree: Subtree, | 47 | subtree: Subtree, |
41 | kind: RepeatKind, | 48 | kind: RepeatKind, |
42 | } | 49 | } |
43 | 50 | ||
51 | #[derive(Debug)] | ||
44 | enum RepeatKind { | 52 | enum RepeatKind { |
45 | ZeroOrMore, | 53 | ZeroOrMore, |
46 | OneOrMore, | 54 | OneOrMore, |
47 | ZeroOrOne, | 55 | ZeroOrOne, |
48 | } | 56 | } |
49 | 57 | ||
58 | #[derive(Debug)] | ||
50 | struct Literal { | 59 | struct Literal { |
51 | text: SmolStr, | 60 | text: SmolStr, |
52 | } | 61 | } |
53 | 62 | ||
63 | #[derive(Debug)] | ||
54 | struct Punct { | 64 | struct Punct { |
55 | char: char, | 65 | char: char, |
56 | } | 66 | } |
57 | 67 | ||
68 | #[derive(Debug)] | ||
58 | struct Ident { | 69 | struct Ident { |
59 | text: SmolStr, | 70 | text: SmolStr, |
60 | } | 71 | } |
61 | 72 | ||
73 | #[derive(Debug)] | ||
62 | struct Var { | 74 | struct Var { |
63 | text: SmolStr, | 75 | text: SmolStr, |
64 | } | 76 | } |
65 | 77 | ||
66 | fn parse(tt: tt::TokenTree) -> MacroRules { | 78 | pub(crate) fn parse(tt: &tt::Subtree) -> MacroRules { |
67 | MacroRules { rules: Vec::new() } | 79 | MacroRules { rules: Vec::new() } |
68 | } | 80 | } |
diff --git a/crates/ra_hir/src/macros/tt.rs b/crates/ra_hir/src/macros/tt.rs index 11b1089d3..02ff422b5 100644 --- a/crates/ra_hir/src/macros/tt.rs +++ b/crates/ra_hir/src/macros/tt.rs | |||
@@ -1,11 +1,13 @@ | |||
1 | use ra_syntax::SmolStr; | 1 | use ra_syntax::SmolStr; |
2 | 2 | ||
3 | #[derive(Debug)] | ||
3 | pub(crate) enum TokenTree { | 4 | pub(crate) enum TokenTree { |
4 | Leaf(Leaf), | 5 | Leaf(Leaf), |
5 | Subtree(Subtree), | 6 | Subtree(Subtree), |
6 | } | 7 | } |
7 | impl_froms!(TokenTree: Leaf, Subtree); | 8 | impl_froms!(TokenTree: Leaf, Subtree); |
8 | 9 | ||
10 | #[derive(Debug)] | ||
9 | pub(crate) enum Leaf { | 11 | pub(crate) enum Leaf { |
10 | Literal(Literal), | 12 | Literal(Literal), |
11 | Punct(Punct), | 13 | Punct(Punct), |
@@ -13,11 +15,13 @@ pub(crate) enum Leaf { | |||
13 | } | 15 | } |
14 | impl_froms!(Leaf: Literal, Punct, Ident); | 16 | impl_froms!(Leaf: Literal, Punct, Ident); |
15 | 17 | ||
18 | #[derive(Debug)] | ||
16 | pub(crate) struct Subtree { | 19 | pub(crate) struct Subtree { |
17 | pub(crate) delimiter: Delimiter, | 20 | pub(crate) delimiter: Delimiter, |
18 | pub(crate) token_trees: Vec<TokenTree>, | 21 | pub(crate) token_trees: Vec<TokenTree>, |
19 | } | 22 | } |
20 | 23 | ||
24 | #[derive(Debug)] | ||
21 | pub(crate) enum Delimiter { | 25 | pub(crate) enum Delimiter { |
22 | Parenthesis, | 26 | Parenthesis, |
23 | Brace, | 27 | Brace, |
@@ -25,14 +29,17 @@ pub(crate) enum Delimiter { | |||
25 | None, | 29 | None, |
26 | } | 30 | } |
27 | 31 | ||
32 | #[derive(Debug)] | ||
28 | pub(crate) struct Literal { | 33 | pub(crate) struct Literal { |
29 | pub(crate) text: SmolStr, | 34 | pub(crate) text: SmolStr, |
30 | } | 35 | } |
31 | 36 | ||
37 | #[derive(Debug)] | ||
32 | pub(crate) struct Punct { | 38 | pub(crate) struct Punct { |
33 | pub(crate) char: char, | 39 | pub(crate) char: char, |
34 | } | 40 | } |
35 | 41 | ||
42 | #[derive(Debug)] | ||
36 | pub(crate) struct Ident { | 43 | pub(crate) struct Ident { |
37 | pub(crate) text: SmolStr, | 44 | pub(crate) text: SmolStr, |
38 | } | 45 | } |