2019-09-30 08:58:53 +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:29:04 +00:00
|
|
|
|
2019-09-16 23:54:22 +00:00
|
|
|
mod parser;
|
2019-01-31 18:09:43 +00:00
|
|
|
mod mbe_expander;
|
2019-01-31 18:29:04 +00:00
|
|
|
mod syntax_bridge;
|
2019-09-16 23:54:22 +00:00
|
|
|
mod tt_iter;
|
2019-04-07 13:42:53 +00:00
|
|
|
mod subtree_source;
|
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-09-16 23:54:22 +00:00
|
|
|
use crate::{
|
|
|
|
parser::{parse_pattern, Op},
|
|
|
|
tt_iter::TtIter,
|
|
|
|
};
|
|
|
|
|
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-09-16 23:54:22 +00:00
|
|
|
InvalidRepeat,
|
2019-03-02 19:20:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 18:47:29 +00:00
|
|
|
pub use crate::syntax_bridge::{
|
2019-09-10 19:12:37 +00:00
|
|
|
ast_to_token_tree, syntax_node_to_token_tree, token_tree_to_expr, token_tree_to_items,
|
|
|
|
token_tree_to_macro_stmts, token_tree_to_pat, token_tree_to_ty,
|
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-11-04 15:22:18 +00:00
|
|
|
/// Highest id of the token we have in TokenMap
|
|
|
|
pub(crate) shift: Option<u32>,
|
2019-01-30 20:17:32 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 23:54:22 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub(crate) struct Rule {
|
|
|
|
pub(crate) lhs: tt::Subtree,
|
|
|
|
pub(crate) rhs: tt::Subtree,
|
|
|
|
}
|
|
|
|
|
2019-11-04 15:22:18 +00:00
|
|
|
/// Find the "shift" (the highest id of the TokenId) inside a subtree
|
|
|
|
fn find_subtree_shift(tt: &tt::Subtree, mut cur: Option<u32>) -> Option<u32> {
|
|
|
|
use std::cmp::max;
|
|
|
|
|
|
|
|
for t in &tt.token_trees {
|
|
|
|
cur = match t {
|
|
|
|
tt::TokenTree::Leaf(leaf) => match leaf {
|
|
|
|
tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => {
|
|
|
|
Some(max(cur.unwrap_or(0), ident.id.0))
|
|
|
|
}
|
|
|
|
_ => cur,
|
|
|
|
},
|
|
|
|
tt::TokenTree::Subtree(tt) => find_subtree_shift(tt, cur),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cur
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Shift given TokenTree token id
|
2019-11-04 16:16:03 +00:00
|
|
|
fn shift_subtree(tt: &mut tt::Subtree, shift: u32) {
|
2019-11-04 15:22:18 +00:00
|
|
|
for t in tt.token_trees.iter_mut() {
|
|
|
|
match t {
|
|
|
|
tt::TokenTree::Leaf(leaf) => match leaf {
|
|
|
|
tt::Leaf::Ident(ident) if ident.id != tt::TokenId::unspecified() => {
|
|
|
|
// Note that TokenId is started from zero,
|
|
|
|
// We have to add 1 to prevent duplication.
|
|
|
|
ident.id.0 += shift + 1;
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
},
|
2019-11-04 16:16:03 +00:00
|
|
|
tt::TokenTree::Subtree(tt) => shift_subtree(tt, shift),
|
2019-11-04 15:22:18 +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-09-22 20:39:29 +00:00
|
|
|
// Note: this parsing can be implemented using mbe machinery itself, by
|
|
|
|
// matching against `$($lhs:tt => $rhs:tt);*` pattern, but implementing
|
|
|
|
// manually seems easier.
|
2019-09-16 23:54:22 +00:00
|
|
|
let mut src = TtIter::new(tt);
|
|
|
|
let mut rules = Vec::new();
|
|
|
|
while src.len() > 0 {
|
|
|
|
let rule = Rule::parse(&mut src)?;
|
|
|
|
rules.push(rule);
|
|
|
|
if let Err(()) = src.expect_char(';') {
|
|
|
|
if src.len() > 0 {
|
|
|
|
return Err(ParseError::Expected("expected `:`".to_string()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-09-22 20:39:29 +00:00
|
|
|
|
|
|
|
for rule in rules.iter() {
|
|
|
|
validate(&rule.lhs)?;
|
|
|
|
}
|
|
|
|
|
2019-11-04 15:22:18 +00:00
|
|
|
Ok(MacroRules { rules, shift: find_subtree_shift(tt, None) })
|
2019-01-31 19:14:28 +00:00
|
|
|
}
|
2019-11-04 15:22:18 +00:00
|
|
|
|
2019-03-03 09:40:03 +00:00
|
|
|
pub fn expand(&self, tt: &tt::Subtree) -> Result<tt::Subtree, ExpandError> {
|
2019-11-04 15:22:18 +00:00
|
|
|
// apply shift
|
|
|
|
let mut tt = tt.clone();
|
|
|
|
if let Some(shift) = self.shift {
|
2019-11-04 16:16:03 +00:00
|
|
|
shift_subtree(&mut tt, shift)
|
2019-11-04 15:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mbe_expander::expand(self, &tt)
|
2019-01-31 19:14:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 23:54:22 +00:00
|
|
|
impl Rule {
|
|
|
|
fn parse(src: &mut TtIter) -> Result<Rule, ParseError> {
|
|
|
|
let mut lhs = src
|
|
|
|
.expect_subtree()
|
|
|
|
.map_err(|()| ParseError::Expected("expected subtree".to_string()))?
|
|
|
|
.clone();
|
|
|
|
lhs.delimiter = tt::Delimiter::None;
|
|
|
|
src.expect_char('=').map_err(|()| ParseError::Expected("expected `=`".to_string()))?;
|
|
|
|
src.expect_char('>').map_err(|()| ParseError::Expected("expected `>`".to_string()))?;
|
|
|
|
let mut rhs = src
|
|
|
|
.expect_subtree()
|
|
|
|
.map_err(|()| ParseError::Expected("expected subtree".to_string()))?
|
|
|
|
.clone();
|
|
|
|
rhs.delimiter = tt::Delimiter::None;
|
|
|
|
Ok(crate::Rule { lhs, rhs })
|
|
|
|
}
|
2019-04-24 15:01:32 +00:00
|
|
|
}
|
2019-05-02 15:23:14 +00:00
|
|
|
|
2019-09-16 23:54:22 +00:00
|
|
|
fn validate(pattern: &tt::Subtree) -> Result<(), ParseError> {
|
|
|
|
for op in parse_pattern(pattern) {
|
|
|
|
let op = match op {
|
|
|
|
Ok(it) => it,
|
|
|
|
Err(e) => {
|
|
|
|
let msg = match e {
|
|
|
|
ExpandError::InvalidRepeat => "invalid repeat".to_string(),
|
|
|
|
_ => "invalid macro definition".to_string(),
|
|
|
|
};
|
|
|
|
return Err(ParseError::Expected(msg));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
match op {
|
|
|
|
Op::TokenTree(tt::TokenTree::Subtree(subtree)) | Op::Repeat { subtree, .. } => {
|
|
|
|
validate(subtree)?
|
2019-05-02 15:23:14 +00:00
|
|
|
}
|
2019-09-16 23:54:22 +00:00
|
|
|
_ => (),
|
2019-05-02 15:23:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-16 23:54:22 +00:00
|
|
|
Ok(())
|
2019-01-30 20:25:02 +00:00
|
|
|
}
|
2019-01-31 19:14:28 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-05-03 15:23:21 +00:00
|
|
|
mod tests;
|