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 mbe_parser;
|
|
|
|
mod mbe_expander;
|
2019-01-31 18:29:04 +00:00
|
|
|
mod syntax_bridge;
|
2019-04-07 13:42:53 +00:00
|
|
|
mod tt_cursor;
|
|
|
|
mod subtree_source;
|
2019-04-07 16:12:07 +00:00
|
|
|
mod subtree_parser;
|
2019-01-31 18:09:43 +00:00
|
|
|
|
2019-01-31 18:29:04 +00:00
|
|
|
use ra_syntax::SmolStr;
|
2019-04-24 15:01:32 +00:00
|
|
|
use smallvec::SmallVec;
|
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-03-02 19:20:26 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2019-03-03 09:40:03 +00:00
|
|
|
pub enum ParseError {
|
2019-03-03 11:45:30 +00:00
|
|
|
Expected(String),
|
2019-03-03 09:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub enum ExpandError {
|
2019-03-02 19:20:26 +00:00
|
|
|
NoMatchingRule,
|
|
|
|
UnexpectedToken,
|
|
|
|
BindingError(String),
|
2019-04-18 18:47:29 +00:00
|
|
|
ConversionError,
|
2019-03-02 19:20:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 18:47:29 +00:00
|
|
|
pub use crate::syntax_bridge::{
|
|
|
|
ast_to_token_tree,
|
|
|
|
token_tree_to_ast_item_list,
|
|
|
|
syntax_node_to_token_tree,
|
2019-04-18 19:49:56 +00:00
|
|
|
token_tree_to_expr,
|
|
|
|
token_tree_to_pat,
|
|
|
|
token_tree_to_ty,
|
2019-04-18 18:47:29 +00:00
|
|
|
token_tree_to_macro_items,
|
2019-04-18 19:49:56 +00:00
|
|
|
token_tree_to_macro_stmts,
|
2019-04-18 18:47:29 +00:00
|
|
|
};
|
2019-01-31 10:46:40 +00:00
|
|
|
|
2019-02-11 16:07:49 +00:00
|
|
|
/// This struct contains AST for a single `macro_rules` definition. What might
|
2019-01-31 19:14:28 +00:00
|
|
|
/// be very confusing is that AST has almost exactly the same shape as
|
|
|
|
/// `tt::TokenTree`, but there's a crucial difference: in macro rules, `$ident`
|
|
|
|
/// and `$()*` have special meaning (see `Var` and `Repeat` data structures)
|
2019-03-13 13:04:28 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
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 19:14:28 +00:00
|
|
|
impl MacroRules {
|
2019-03-03 09:40:03 +00:00
|
|
|
pub fn parse(tt: &tt::Subtree) -> Result<MacroRules, ParseError> {
|
2019-01-31 19:14:28 +00:00
|
|
|
mbe_parser::parse(tt)
|
|
|
|
}
|
2019-03-03 09:40:03 +00:00
|
|
|
pub fn expand(&self, tt: &tt::Subtree) -> Result<tt::Subtree, ExpandError> {
|
2019-03-02 19:20:26 +00:00
|
|
|
mbe_expander::expand(self, tt)
|
2019-01-31 19:14:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 13:04:28 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
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-03-13 13:04:28 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
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-03-13 13:04:28 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
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-03-13 13:04:28 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
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-05-02 15:23:14 +00:00
|
|
|
#[derive(Clone, Debug, Eq)]
|
2019-04-24 15:01:32 +00:00
|
|
|
pub(crate) enum Separator {
|
|
|
|
Literal(tt::Literal),
|
|
|
|
Ident(tt::Ident),
|
|
|
|
Puncts(SmallVec<[tt::Punct; 3]>),
|
|
|
|
}
|
2019-05-02 15:23:14 +00:00
|
|
|
|
|
|
|
// Note that when we compare a Separator, we just care about its textual value.
|
|
|
|
impl PartialEq for crate::Separator {
|
|
|
|
fn eq(&self, other: &crate::Separator) -> bool {
|
|
|
|
use crate::Separator::*;
|
|
|
|
|
|
|
|
match (self, other) {
|
2019-05-02 15:59:13 +00:00
|
|
|
(Ident(ref a), Ident(ref b)) => a.text == b.text,
|
|
|
|
(Literal(ref a), Literal(ref b)) => a.text == b.text,
|
2019-05-02 15:23:14 +00:00
|
|
|
(Puncts(ref a), Puncts(ref b)) if a.len() == b.len() => {
|
|
|
|
let a_iter = a.iter().map(|a| a.char);
|
|
|
|
let b_iter = b.iter().map(|b| b.char);
|
|
|
|
a_iter.eq(b_iter)
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-24 15:01:32 +00:00
|
|
|
|
2019-03-13 13:04:28 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2019-01-31 10:46:40 +00:00
|
|
|
pub(crate) struct Repeat {
|
|
|
|
pub(crate) subtree: Subtree,
|
|
|
|
pub(crate) kind: RepeatKind,
|
2019-04-24 15:01:32 +00:00
|
|
|
pub(crate) separator: Option<Separator>,
|
2019-01-31 07:33:11 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 13:04:28 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2019-01-31 10:46:40 +00:00
|
|
|
pub(crate) enum RepeatKind {
|
2019-01-31 07:33:11 +00:00
|
|
|
ZeroOrMore,
|
|
|
|
OneOrMore,
|
|
|
|
ZeroOrOne,
|
|
|
|
}
|
|
|
|
|
2019-03-13 13:04:28 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
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-03-13 13:04:28 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
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-03-13 13:04:28 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
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
|
|
|
}
|
2019-01-31 19:14:28 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-05-03 13:38:00 +00:00
|
|
|
mod tests;
|