diff --git a/crates/ra_hir/src/macros.rs b/crates/ra_hir/src/macros.rs index c23ad53cc4..4740e53374 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 { }; Some(res) } + +#[test] +fn test_convert_tt() { + let text = r#" +macro_rules! impl_froms { + ($e:ident: $($v:ident), *) => { + $( + impl From<$v> for $e { + fn from(it: $v) -> $e { + $e::$v(it) + } + } + )* + } +} +"#; + let source_file = ast::SourceFile::parse(text); + let maco_call = source_file + .syntax() + .descendants() + .find_map(ast::MacroCall::cast) + .unwrap(); + let tt = macro_call_to_tt(maco_call).unwrap(); + let tt = mbe::parse(&tt); + dbg!(tt); +} diff --git a/crates/ra_hir/src/macros/mbe.rs b/crates/ra_hir/src/macros/mbe.rs index 1408ed511b..7af7066fb8 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; use crate::macros::tt; -struct MacroRules { +#[derive(Debug)] +pub(crate) struct MacroRules { rules: Vec, } +#[derive(Debug)] struct Rule { lhs: TokenTree, rhs: TokenTree, } +#[derive(Debug)] enum TokenTree { Leaf(Leaf), Subtree(Subtree), Repeat(Repeat), } +#[derive(Debug)] enum Leaf { Literal(Literal), Punct(Punct), @@ -24,11 +28,13 @@ enum Leaf { Var(Var), } +#[derive(Debug)] struct Subtree { delimiter: Delimiter, token_trees: Vec, } +#[derive(Debug)] enum Delimiter { Parenthesis, Brace, @@ -36,33 +42,39 @@ enum Delimiter { None, } +#[derive(Debug)] struct Repeat { subtree: Subtree, kind: RepeatKind, } +#[derive(Debug)] enum RepeatKind { ZeroOrMore, OneOrMore, ZeroOrOne, } +#[derive(Debug)] struct Literal { text: SmolStr, } +#[derive(Debug)] struct Punct { char: char, } +#[derive(Debug)] struct Ident { text: SmolStr, } +#[derive(Debug)] struct Var { text: SmolStr, } -fn parse(tt: tt::TokenTree) -> MacroRules { +pub(crate) fn parse(tt: &tt::Subtree) -> MacroRules { MacroRules { rules: Vec::new() } } diff --git a/crates/ra_hir/src/macros/tt.rs b/crates/ra_hir/src/macros/tt.rs index 11b1089d36..02ff422b5f 100644 --- a/crates/ra_hir/src/macros/tt.rs +++ b/crates/ra_hir/src/macros/tt.rs @@ -1,11 +1,13 @@ use ra_syntax::SmolStr; +#[derive(Debug)] pub(crate) enum TokenTree { Leaf(Leaf), Subtree(Subtree), } impl_froms!(TokenTree: Leaf, Subtree); +#[derive(Debug)] pub(crate) enum Leaf { Literal(Literal), Punct(Punct), @@ -13,11 +15,13 @@ pub(crate) enum Leaf { } impl_froms!(Leaf: Literal, Punct, Ident); +#[derive(Debug)] pub(crate) struct Subtree { pub(crate) delimiter: Delimiter, pub(crate) token_trees: Vec, } +#[derive(Debug)] pub(crate) enum Delimiter { Parenthesis, Brace, @@ -25,14 +29,17 @@ pub(crate) enum Delimiter { None, } +#[derive(Debug)] pub(crate) struct Literal { pub(crate) text: SmolStr, } +#[derive(Debug)] pub(crate) struct Punct { pub(crate) char: char, } +#[derive(Debug)] pub(crate) struct Ident { pub(crate) text: SmolStr, }