debug impls

This commit is contained in:
Aleksey Kladov 2019-01-31 11:09:19 +03:00
parent 82cf0185c3
commit 2980508ad2
3 changed files with 47 additions and 2 deletions

View file

@ -250,3 +250,29 @@ fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> {
};
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);
}

View file

@ -2,21 +2,25 @@ use ra_syntax::SmolStr;
use crate::macros::tt;
struct MacroRules {
#[derive(Debug)]
pub(crate) struct MacroRules {
rules: Vec<Rule>,
}
#[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<TokenTree>,
}
#[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() }
}

View file

@ -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<TokenTree>,
}
#[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,
}