rust-analyzer/crates/ra_mbe/src/lib.rs

96 lines
1.8 KiB
Rust
Raw Normal View History

2019-01-31 18:29:04 +00:00
/// `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!
2019-01-31 18:09:43 +00:00
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;
2019-01-31 18:29:04 +00:00
mod syntax_bridge;
2019-01-31 18:09:43 +00:00
2019-01-31 18:29:04 +00:00
use ra_syntax::SmolStr;
2019-01-30 20:17:32 +00:00
2019-01-31 18:09:43 +00:00
pub use tt::{Delimiter, Punct};
2019-01-30 20:25:02 +00:00
2019-01-31 10:49:57 +00:00
pub use crate::{
mbe_parser::parse,
mbe_expander::exapnd,
2019-01-31 18:29:04 +00:00
syntax_bridge::macro_call_to_tt,
2019-01-31 10:49:57 +00:00
};
2019-01-31 10:46:40 +00:00
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:40:05 +00:00
pub struct MacroRules {
2019-01-31 10:46:40 +00:00
pub(crate) rules: Vec<Rule>,
2019-01-30 20:17:32 +00:00
}
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) struct Rule {
pub(crate) lhs: Subtree,
pub(crate) rhs: Subtree,
2019-01-30 20:17:32 +00:00
}
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) enum TokenTree {
2019-01-30 20:17:32 +00:00
Leaf(Leaf),
Subtree(Subtree),
2019-01-31 07:33:11 +00:00
Repeat(Repeat),
2019-01-30 20:17:32 +00:00
}
2019-01-31 10:32:40 +00:00
impl_froms!(TokenTree: Leaf, Subtree, Repeat);
2019-01-30 20:17:32 +00:00
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) enum Leaf {
2019-01-30 20:17:32 +00:00
Literal(Literal),
Punct(Punct),
Ident(Ident),
Var(Var),
}
2019-01-31 10:32:40 +00:00
impl_froms!(Leaf: Literal, Punct, Ident, Var);
2019-01-30 20:17:32 +00:00
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) struct Subtree {
pub(crate) delimiter: Delimiter,
pub(crate) token_trees: Vec<TokenTree>,
2019-01-30 20:17:32 +00:00
}
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) struct Repeat {
pub(crate) subtree: Subtree,
pub(crate) kind: RepeatKind,
2019-01-31 15:51:17 +00:00
pub(crate) separator: Option<char>,
2019-01-31 07:33:11 +00:00
}
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) enum RepeatKind {
2019-01-31 07:33:11 +00:00
ZeroOrMore,
OneOrMore,
ZeroOrOne,
}
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) struct Literal {
pub(crate) text: SmolStr,
2019-01-30 20:17:32 +00:00
}
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) struct Ident {
pub(crate) text: SmolStr,
2019-01-30 20:17:32 +00:00
}
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) struct Var {
pub(crate) text: SmolStr,
pub(crate) kind: Option<SmolStr>,
2019-01-30 20:25:02 +00:00
}