aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/macros.rs26
-rw-r--r--crates/ra_hir/src/macros/mbe.rs16
-rw-r--r--crates/ra_hir/src/macros/tt.rs7
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]
255fn test_convert_tt() {
256 let text = r#"
257macro_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
3use crate::macros::tt; 3use crate::macros::tt;
4 4
5struct MacroRules { 5#[derive(Debug)]
6pub(crate) struct MacroRules {
6 rules: Vec<Rule>, 7 rules: Vec<Rule>,
7} 8}
8 9
10#[derive(Debug)]
9struct Rule { 11struct Rule {
10 lhs: TokenTree, 12 lhs: TokenTree,
11 rhs: TokenTree, 13 rhs: TokenTree,
12} 14}
13 15
16#[derive(Debug)]
14enum TokenTree { 17enum 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)]
20enum Leaf { 24enum 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)]
27struct Subtree { 32struct Subtree {
28 delimiter: Delimiter, 33 delimiter: Delimiter,
29 token_trees: Vec<TokenTree>, 34 token_trees: Vec<TokenTree>,
30} 35}
31 36
37#[derive(Debug)]
32enum Delimiter { 38enum 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)]
39struct Repeat { 46struct Repeat {
40 subtree: Subtree, 47 subtree: Subtree,
41 kind: RepeatKind, 48 kind: RepeatKind,
42} 49}
43 50
51#[derive(Debug)]
44enum RepeatKind { 52enum RepeatKind {
45 ZeroOrMore, 53 ZeroOrMore,
46 OneOrMore, 54 OneOrMore,
47 ZeroOrOne, 55 ZeroOrOne,
48} 56}
49 57
58#[derive(Debug)]
50struct Literal { 59struct Literal {
51 text: SmolStr, 60 text: SmolStr,
52} 61}
53 62
63#[derive(Debug)]
54struct Punct { 64struct Punct {
55 char: char, 65 char: char,
56} 66}
57 67
68#[derive(Debug)]
58struct Ident { 69struct Ident {
59 text: SmolStr, 70 text: SmolStr,
60} 71}
61 72
73#[derive(Debug)]
62struct Var { 74struct Var {
63 text: SmolStr, 75 text: SmolStr,
64} 76}
65 77
66fn parse(tt: tt::TokenTree) -> MacroRules { 78pub(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 @@
1use ra_syntax::SmolStr; 1use ra_syntax::SmolStr;
2 2
3#[derive(Debug)]
3pub(crate) enum TokenTree { 4pub(crate) enum TokenTree {
4 Leaf(Leaf), 5 Leaf(Leaf),
5 Subtree(Subtree), 6 Subtree(Subtree),
6} 7}
7impl_froms!(TokenTree: Leaf, Subtree); 8impl_froms!(TokenTree: Leaf, Subtree);
8 9
10#[derive(Debug)]
9pub(crate) enum Leaf { 11pub(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}
14impl_froms!(Leaf: Literal, Punct, Ident); 16impl_froms!(Leaf: Literal, Punct, Ident);
15 17
18#[derive(Debug)]
16pub(crate) struct Subtree { 19pub(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)]
21pub(crate) enum Delimiter { 25pub(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)]
28pub(crate) struct Literal { 33pub(crate) struct Literal {
29 pub(crate) text: SmolStr, 34 pub(crate) text: SmolStr,
30} 35}
31 36
37#[derive(Debug)]
32pub(crate) struct Punct { 38pub(crate) struct Punct {
33 pub(crate) char: char, 39 pub(crate) char: char,
34} 40}
35 41
42#[derive(Debug)]
36pub(crate) struct Ident { 43pub(crate) struct Ident {
37 pub(crate) text: SmolStr, 44 pub(crate) text: SmolStr,
38} 45}