2019-02-11 16:07:49 +00:00
|
|
|
/// This module takes a (parsed) definition of `macro_rules` invocation, a
|
2019-01-31 20:01:34 +00:00
|
|
|
/// `tt::TokenTree` representing an argument of macro invocation, and produces a
|
|
|
|
/// `tt::TokenTree` for the result of the expansion.
|
2019-01-31 10:59:25 +00:00
|
|
|
use rustc_hash::FxHashMap;
|
2019-01-31 18:29:04 +00:00
|
|
|
use ra_syntax::SmolStr;
|
2019-02-11 16:28:39 +00:00
|
|
|
use tt::TokenId;
|
2019-01-31 10:59:25 +00:00
|
|
|
|
2019-03-03 09:40:03 +00:00
|
|
|
use crate::ExpandError;
|
2019-01-31 18:43:54 +00:00
|
|
|
use crate::tt_cursor::TtCursor;
|
2019-01-31 10:49:57 +00:00
|
|
|
|
2019-03-03 09:40:03 +00:00
|
|
|
pub(crate) fn expand(
|
|
|
|
rules: &crate::MacroRules,
|
|
|
|
input: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, ExpandError> {
|
|
|
|
rules.rules.iter().find_map(|it| expand_rule(it, input).ok()).ok_or(ExpandError::NoMatchingRule)
|
2019-01-31 10:59:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 09:40:03 +00:00
|
|
|
fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Result<tt::Subtree, ExpandError> {
|
2019-01-31 14:16:02 +00:00
|
|
|
let mut input = TtCursor::new(input);
|
|
|
|
let bindings = match_lhs(&rule.lhs, &mut input)?;
|
2019-02-03 15:06:09 +00:00
|
|
|
if !input.is_eof() {
|
2019-03-03 09:40:03 +00:00
|
|
|
return Err(ExpandError::UnexpectedToken);
|
2019-02-03 15:06:09 +00:00
|
|
|
}
|
2019-04-25 15:12:57 +00:00
|
|
|
|
|
|
|
let mut ctx = ExpandCtx { bindings: &bindings, nesting: Vec::new(), var_expanded: false };
|
|
|
|
|
|
|
|
expand_subtree(&rule.rhs, &mut ctx)
|
2019-01-31 10:59:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 20:01:34 +00:00
|
|
|
/// The actual algorithm for expansion is not too hard, but is pretty tricky.
|
|
|
|
/// `Bindings` structure is the key to understanding what we are doing here.
|
|
|
|
///
|
|
|
|
/// On the high level, it stores mapping from meta variables to the bits of
|
|
|
|
/// syntax it should be substituted with. For example, if `$e:expr` is matched
|
|
|
|
/// with `1 + 1` by macro_rules, the `Binding` will store `$e -> 1 + 1`.
|
|
|
|
///
|
|
|
|
/// The tricky bit is dealing with repetitions (`$()*`). Consider this example:
|
|
|
|
///
|
2019-02-08 10:55:45 +00:00
|
|
|
/// ```not_rust
|
2019-01-31 20:01:34 +00:00
|
|
|
/// macro_rules! foo {
|
|
|
|
/// ($($ i:ident $($ e:expr),*);*) => {
|
|
|
|
/// $(fn $ i() { $($ e);*; })*
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// foo! { foo 1,2,3; bar 4,5,6 }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Here, the `$i` meta variable is matched first with `foo` and then with
|
|
|
|
/// `bar`, and `$e` is matched in turn with `1`, `2`, `3`, `4`, `5`, `6`.
|
|
|
|
///
|
|
|
|
/// To represent such "multi-mappings", we use a recursive structures: we map
|
|
|
|
/// variables not to values, but to *lists* of values or other lists (that is,
|
|
|
|
/// to the trees).
|
|
|
|
///
|
|
|
|
/// For the above example, the bindings would store
|
|
|
|
///
|
2019-02-08 10:55:45 +00:00
|
|
|
/// ```not_rust
|
2019-01-31 20:01:34 +00:00
|
|
|
/// i -> [foo, bar]
|
|
|
|
/// e -> [[1, 2, 3], [4, 5, 6]]
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// We construct `Bindings` in the `match_lhs`. The interesting case is
|
|
|
|
/// `TokenTree::Repeat`, where we use `push_nested` to create the desired
|
|
|
|
/// nesting structure.
|
|
|
|
///
|
|
|
|
/// The other side of the puzzle is `expand_subtree`, where we use the bindings
|
|
|
|
/// to substitute meta variables in the output template. When expanding, we
|
2019-02-11 16:18:27 +00:00
|
|
|
/// maintain a `nesting` stack of indices which tells us which occurrence from
|
2019-01-31 20:01:34 +00:00
|
|
|
/// the `Bindings` we should take. We push to the stack when we enter a
|
|
|
|
/// repetition.
|
|
|
|
///
|
|
|
|
/// In other words, `Bindings` is a *multi* mapping from `SmolStr` to
|
|
|
|
/// `tt::TokenTree`, where the index to select a particular `TokenTree` among
|
|
|
|
/// many is not a plain `usize`, but an `&[usize]`.
|
2019-01-31 10:59:25 +00:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
struct Bindings {
|
2019-01-31 12:22:55 +00:00
|
|
|
inner: FxHashMap<SmolStr, Binding>,
|
2019-01-31 10:59:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 12:22:55 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum Binding {
|
|
|
|
Simple(tt::TokenTree),
|
|
|
|
Nested(Vec<Binding>),
|
|
|
|
}
|
|
|
|
|
2019-01-31 14:16:02 +00:00
|
|
|
impl Bindings {
|
2019-05-03 15:23:21 +00:00
|
|
|
fn contains(&self, name: &SmolStr) -> bool {
|
|
|
|
self.inner.contains_key(name)
|
|
|
|
}
|
|
|
|
|
2019-03-03 09:40:03 +00:00
|
|
|
fn get(&self, name: &SmolStr, nesting: &[usize]) -> Result<&tt::TokenTree, ExpandError> {
|
2019-03-02 19:49:13 +00:00
|
|
|
let mut b = self
|
|
|
|
.inner
|
|
|
|
.get(name)
|
2019-03-03 19:33:50 +00:00
|
|
|
.ok_or(ExpandError::BindingError(format!("could not find binding `{}`", name)))?;
|
2019-01-31 14:16:02 +00:00
|
|
|
for &idx in nesting.iter() {
|
|
|
|
b = match b {
|
|
|
|
Binding::Simple(_) => break,
|
2019-03-03 09:40:03 +00:00
|
|
|
Binding::Nested(bs) => bs.get(idx).ok_or(ExpandError::BindingError(format!(
|
2019-03-03 19:33:50 +00:00
|
|
|
"could not find nested binding `{}`",
|
2019-03-03 09:40:03 +00:00
|
|
|
name
|
|
|
|
)))?,
|
2019-01-31 14:16:02 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
match b {
|
2019-03-02 19:20:26 +00:00
|
|
|
Binding::Simple(it) => Ok(it),
|
2019-03-03 09:40:03 +00:00
|
|
|
Binding::Nested(_) => Err(ExpandError::BindingError(format!(
|
2019-03-03 19:33:50 +00:00
|
|
|
"expected simple binding, found nested binding `{}`",
|
2019-03-02 19:49:13 +00:00
|
|
|
name
|
|
|
|
))),
|
2019-01-31 14:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-02 19:20:26 +00:00
|
|
|
|
2019-05-02 15:23:14 +00:00
|
|
|
fn push_nested(&mut self, idx: usize, nested: Bindings) -> Result<(), ExpandError> {
|
2019-01-31 14:16:02 +00:00
|
|
|
for (key, value) in nested.inner {
|
|
|
|
if !self.inner.contains_key(&key) {
|
|
|
|
self.inner.insert(key.clone(), Binding::Nested(Vec::new()));
|
2019-01-31 12:22:55 +00:00
|
|
|
}
|
2019-01-31 14:16:02 +00:00
|
|
|
match self.inner.get_mut(&key) {
|
2019-05-02 15:23:14 +00:00
|
|
|
Some(Binding::Nested(it)) => {
|
|
|
|
// insert empty nested bindings before this one
|
|
|
|
while it.len() < idx {
|
|
|
|
it.push(Binding::Nested(vec![]));
|
|
|
|
}
|
|
|
|
it.push(value);
|
|
|
|
}
|
2019-03-02 19:49:13 +00:00
|
|
|
_ => {
|
2019-03-03 09:40:03 +00:00
|
|
|
return Err(ExpandError::BindingError(format!(
|
2019-03-03 19:33:50 +00:00
|
|
|
"could not find binding `{}`",
|
2019-03-02 19:49:13 +00:00
|
|
|
key
|
2019-03-03 09:40:03 +00:00
|
|
|
)));
|
2019-03-02 19:49:13 +00:00
|
|
|
}
|
2019-01-31 14:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-02 19:20:26 +00:00
|
|
|
Ok(())
|
2019-01-31 12:22:55 +00:00
|
|
|
}
|
2019-04-20 15:05:25 +00:00
|
|
|
|
|
|
|
fn merge(&mut self, nested: Bindings) {
|
|
|
|
self.inner.extend(nested.inner);
|
|
|
|
}
|
2019-01-31 12:22:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 09:40:03 +00:00
|
|
|
fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings, ExpandError> {
|
2019-01-31 12:22:55 +00:00
|
|
|
let mut res = Bindings::default();
|
|
|
|
for pat in pattern.token_trees.iter() {
|
|
|
|
match pat {
|
2019-01-31 18:43:54 +00:00
|
|
|
crate::TokenTree::Leaf(leaf) => match leaf {
|
|
|
|
crate::Leaf::Var(crate::Var { text, kind }) => {
|
2019-03-03 09:40:03 +00:00
|
|
|
let kind = kind.clone().ok_or(ExpandError::UnexpectedToken)?;
|
2019-01-31 12:22:55 +00:00
|
|
|
match kind.as_str() {
|
2019-01-31 14:16:02 +00:00
|
|
|
"ident" => {
|
2019-03-03 09:40:03 +00:00
|
|
|
let ident =
|
|
|
|
input.eat_ident().ok_or(ExpandError::UnexpectedToken)?.clone();
|
2019-01-31 14:16:02 +00:00
|
|
|
res.inner.insert(
|
|
|
|
text.clone(),
|
|
|
|
Binding::Simple(tt::Leaf::from(ident).into()),
|
|
|
|
);
|
|
|
|
}
|
2019-04-06 04:12:32 +00:00
|
|
|
"path" => {
|
|
|
|
let path =
|
|
|
|
input.eat_path().ok_or(ExpandError::UnexpectedToken)?.clone();
|
|
|
|
res.inner.insert(text.clone(), Binding::Simple(path.into()));
|
|
|
|
}
|
2019-04-13 10:38:31 +00:00
|
|
|
"expr" => {
|
|
|
|
let expr =
|
|
|
|
input.eat_expr().ok_or(ExpandError::UnexpectedToken)?.clone();
|
|
|
|
res.inner.insert(text.clone(), Binding::Simple(expr.into()));
|
|
|
|
}
|
|
|
|
"ty" => {
|
|
|
|
let ty = input.eat_ty().ok_or(ExpandError::UnexpectedToken)?.clone();
|
|
|
|
res.inner.insert(text.clone(), Binding::Simple(ty.into()));
|
|
|
|
}
|
|
|
|
"pat" => {
|
|
|
|
let pat = input.eat_pat().ok_or(ExpandError::UnexpectedToken)?.clone();
|
|
|
|
res.inner.insert(text.clone(), Binding::Simple(pat.into()));
|
|
|
|
}
|
2019-04-17 04:34:43 +00:00
|
|
|
"stmt" => {
|
|
|
|
let pat = input.eat_stmt().ok_or(ExpandError::UnexpectedToken)?.clone();
|
|
|
|
res.inner.insert(text.clone(), Binding::Simple(pat.into()));
|
|
|
|
}
|
2019-04-19 10:30:43 +00:00
|
|
|
"block" => {
|
|
|
|
let block =
|
|
|
|
input.eat_block().ok_or(ExpandError::UnexpectedToken)?.clone();
|
|
|
|
res.inner.insert(text.clone(), Binding::Simple(block.into()));
|
|
|
|
}
|
2019-04-19 11:33:29 +00:00
|
|
|
"meta" => {
|
|
|
|
let meta =
|
|
|
|
input.eat_meta().ok_or(ExpandError::UnexpectedToken)?.clone();
|
|
|
|
res.inner.insert(text.clone(), Binding::Simple(meta.into()));
|
|
|
|
}
|
2019-04-23 18:59:38 +00:00
|
|
|
"tt" => {
|
|
|
|
let token = input.eat().ok_or(ExpandError::UnexpectedToken)?.clone();
|
|
|
|
res.inner.insert(text.clone(), Binding::Simple(token.into()));
|
|
|
|
}
|
2019-04-18 02:21:36 +00:00
|
|
|
"item" => {
|
|
|
|
let item =
|
|
|
|
input.eat_item().ok_or(ExpandError::UnexpectedToken)?.clone();
|
|
|
|
res.inner.insert(text.clone(), Binding::Simple(item.into()));
|
|
|
|
}
|
2019-04-19 13:15:19 +00:00
|
|
|
"lifetime" => {
|
|
|
|
let lifetime =
|
|
|
|
input.eat_lifetime().ok_or(ExpandError::UnexpectedToken)?.clone();
|
|
|
|
res.inner.insert(text.clone(), Binding::Simple(lifetime.into()));
|
|
|
|
}
|
2019-04-19 13:21:47 +00:00
|
|
|
"literal" => {
|
|
|
|
let literal =
|
|
|
|
input.eat_literal().ok_or(ExpandError::UnexpectedToken)?.clone();
|
2019-04-24 15:01:32 +00:00
|
|
|
|
2019-04-19 13:21:47 +00:00
|
|
|
res.inner.insert(
|
|
|
|
text.clone(),
|
|
|
|
Binding::Simple(tt::Leaf::from(literal).into()),
|
|
|
|
);
|
|
|
|
}
|
2019-04-19 13:38:26 +00:00
|
|
|
"vis" => {
|
2019-05-02 13:24:51 +00:00
|
|
|
// `vis` is optional
|
|
|
|
if let Some(vis) = input.try_eat_vis() {
|
|
|
|
let vis = vis.clone();
|
|
|
|
res.inner.insert(text.clone(), Binding::Simple(vis.into()));
|
|
|
|
} else {
|
|
|
|
// FIXME: Do we have a better way to represent an empty token ?
|
|
|
|
// Insert an empty subtree for empty token
|
|
|
|
res.inner.insert(
|
|
|
|
text.clone(),
|
|
|
|
Binding::Simple(
|
|
|
|
tt::Subtree {
|
|
|
|
delimiter: tt::Delimiter::None,
|
|
|
|
token_trees: vec![],
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2019-04-19 13:38:26 +00:00
|
|
|
}
|
2019-04-19 13:21:47 +00:00
|
|
|
|
2019-03-03 09:40:03 +00:00
|
|
|
_ => return Err(ExpandError::UnexpectedToken),
|
2019-01-31 12:22:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-31 18:43:54 +00:00
|
|
|
crate::Leaf::Punct(punct) => {
|
2019-04-24 15:01:32 +00:00
|
|
|
if !input.eat_punct().map(|p| p.char == punct.char).unwrap_or(false) {
|
2019-03-03 09:40:03 +00:00
|
|
|
return Err(ExpandError::UnexpectedToken);
|
2019-01-31 14:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-03 20:16:55 +00:00
|
|
|
crate::Leaf::Ident(ident) => {
|
2019-03-03 09:40:03 +00:00
|
|
|
if input.eat_ident().map(|i| &i.text) != Some(&ident.text) {
|
|
|
|
return Err(ExpandError::UnexpectedToken);
|
2019-02-03 20:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-03 09:40:03 +00:00
|
|
|
_ => return Err(ExpandError::UnexpectedToken),
|
2019-01-31 12:22:55 +00:00
|
|
|
},
|
2019-04-21 20:17:20 +00:00
|
|
|
crate::TokenTree::Repeat(crate::Repeat { subtree, kind, separator }) => {
|
2019-03-16 12:45:21 +00:00
|
|
|
// Dirty hack to make macro-expansion terminate.
|
|
|
|
// This should be replaced by a propper macro-by-example implementation
|
2019-04-25 15:12:57 +00:00
|
|
|
let mut limit = 65536;
|
2019-04-21 20:17:20 +00:00
|
|
|
let mut counter = 0;
|
2019-04-23 18:59:38 +00:00
|
|
|
|
|
|
|
let mut memento = input.save();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match match_lhs(subtree, input) {
|
|
|
|
Ok(nested) => {
|
|
|
|
limit -= 1;
|
|
|
|
if limit == 0 {
|
2019-04-25 15:12:57 +00:00
|
|
|
log::warn!("match_lhs excced in repeat pattern exceed limit => {:#?}\n{:#?}\n{:#?}\n{:#?}", subtree, input, kind, separator);
|
2019-04-23 18:59:38 +00:00
|
|
|
break;
|
2019-02-05 01:18:53 +00:00
|
|
|
}
|
2019-04-23 18:59:38 +00:00
|
|
|
|
|
|
|
memento = input.save();
|
2019-05-02 15:23:14 +00:00
|
|
|
res.push_nested(counter, nested)?;
|
|
|
|
counter += 1;
|
2019-04-23 18:59:38 +00:00
|
|
|
if counter == 1 {
|
|
|
|
if let crate::RepeatKind::ZeroOrOne = kind {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 15:01:32 +00:00
|
|
|
if let Some(separator) = separator {
|
|
|
|
if !input
|
|
|
|
.eat_seperator()
|
2019-05-02 15:23:14 +00:00
|
|
|
.map(|sep| sep == *separator)
|
2019-04-24 15:01:32 +00:00
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2019-04-23 18:59:38 +00:00
|
|
|
input.rollback(memento);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
input.rollback(memento);
|
|
|
|
break;
|
2019-02-05 01:18:53 +00:00
|
|
|
}
|
2019-01-31 14:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-21 20:17:20 +00:00
|
|
|
|
|
|
|
match kind {
|
|
|
|
crate::RepeatKind::OneOrMore if counter == 0 => {
|
|
|
|
return Err(ExpandError::UnexpectedToken);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2019-01-31 14:16:02 +00:00
|
|
|
}
|
2019-04-20 15:05:25 +00:00
|
|
|
crate::TokenTree::Subtree(subtree) => {
|
|
|
|
let input_subtree =
|
|
|
|
input.eat_subtree().map_err(|_| ExpandError::UnexpectedToken)?;
|
|
|
|
if subtree.delimiter != input_subtree.delimiter {
|
|
|
|
return Err(ExpandError::UnexpectedToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut input = TtCursor::new(input_subtree);
|
|
|
|
let bindings = match_lhs(&subtree, &mut input)?;
|
|
|
|
if !input.is_eof() {
|
|
|
|
return Err(ExpandError::UnexpectedToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.merge(bindings);
|
|
|
|
}
|
2019-01-31 12:22:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-02 19:20:26 +00:00
|
|
|
Ok(res)
|
2019-01-31 10:59:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 15:12:57 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct ExpandCtx<'a> {
|
|
|
|
bindings: &'a Bindings,
|
|
|
|
nesting: Vec<usize>,
|
|
|
|
var_expanded: bool,
|
|
|
|
}
|
|
|
|
|
2019-01-31 14:16:02 +00:00
|
|
|
fn expand_subtree(
|
2019-01-31 18:43:54 +00:00
|
|
|
template: &crate::Subtree,
|
2019-04-25 15:12:57 +00:00
|
|
|
ctx: &mut ExpandCtx,
|
2019-03-03 09:40:03 +00:00
|
|
|
) -> Result<tt::Subtree, ExpandError> {
|
2019-01-31 14:16:02 +00:00
|
|
|
let token_trees = template
|
|
|
|
.token_trees
|
|
|
|
.iter()
|
2019-04-25 15:12:57 +00:00
|
|
|
.map(|it| expand_tt(it, ctx))
|
2019-05-03 13:38:00 +00:00
|
|
|
.filter(|it| {
|
|
|
|
// Filter empty subtree
|
|
|
|
if let Ok(tt::TokenTree::Subtree(subtree)) = it {
|
|
|
|
subtree.delimiter != tt::Delimiter::None || !subtree.token_trees.is_empty()
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
})
|
2019-03-03 09:40:03 +00:00
|
|
|
.collect::<Result<Vec<_>, ExpandError>>()?;
|
2019-01-31 14:16:02 +00:00
|
|
|
|
2019-03-02 19:20:26 +00:00
|
|
|
Ok(tt::Subtree { token_trees, delimiter: template.delimiter })
|
2019-01-31 14:16:02 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 20:17:20 +00:00
|
|
|
/// Reduce single token subtree to single token
|
|
|
|
/// In `tt` matcher case, all tt tokens will be braced by a Delimiter::None
|
|
|
|
/// which makes all sort of problems.
|
|
|
|
fn reduce_single_token(mut subtree: tt::Subtree) -> tt::TokenTree {
|
|
|
|
if subtree.delimiter != tt::Delimiter::None || subtree.token_trees.len() != 1 {
|
|
|
|
return subtree.into();
|
|
|
|
}
|
|
|
|
|
|
|
|
match subtree.token_trees.pop().unwrap() {
|
|
|
|
tt::TokenTree::Subtree(subtree) => reduce_single_token(subtree),
|
|
|
|
tt::TokenTree::Leaf(token) => token.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 14:16:02 +00:00
|
|
|
fn expand_tt(
|
2019-01-31 18:43:54 +00:00
|
|
|
template: &crate::TokenTree,
|
2019-04-25 15:12:57 +00:00
|
|
|
ctx: &mut ExpandCtx,
|
2019-03-03 09:40:03 +00:00
|
|
|
) -> Result<tt::TokenTree, ExpandError> {
|
2019-01-31 14:16:02 +00:00
|
|
|
let res: tt::TokenTree = match template {
|
2019-04-25 15:12:57 +00:00
|
|
|
crate::TokenTree::Subtree(subtree) => expand_subtree(subtree, ctx)?.into(),
|
2019-01-31 18:43:54 +00:00
|
|
|
crate::TokenTree::Repeat(repeat) => {
|
2019-04-21 20:17:20 +00:00
|
|
|
let mut token_trees: Vec<tt::TokenTree> = Vec::new();
|
2019-04-25 15:12:57 +00:00
|
|
|
ctx.nesting.push(0);
|
2019-03-16 12:45:21 +00:00
|
|
|
// Dirty hack to make macro-expansion terminate.
|
|
|
|
// This should be replaced by a propper macro-by-example implementation
|
2019-04-25 15:12:57 +00:00
|
|
|
let mut limit = 65536;
|
2019-04-24 15:01:32 +00:00
|
|
|
let mut has_seps = 0;
|
2019-04-25 15:12:57 +00:00
|
|
|
let mut counter = 0;
|
|
|
|
|
2019-05-02 16:01:48 +00:00
|
|
|
// We store the old var expanded value, and restore it later
|
2019-05-02 15:23:14 +00:00
|
|
|
// It is because before this `$repeat`,
|
|
|
|
// it is possible some variables already expanad in the same subtree
|
|
|
|
//
|
|
|
|
// `some_var_expanded` keep check if the deeper subtree has expanded variables
|
2019-04-25 15:12:57 +00:00
|
|
|
let mut some_var_expanded = false;
|
2019-05-02 15:23:14 +00:00
|
|
|
let old_var_expanded = ctx.var_expanded;
|
2019-04-25 15:12:57 +00:00
|
|
|
ctx.var_expanded = false;
|
|
|
|
|
|
|
|
while let Ok(t) = expand_subtree(&repeat.subtree, ctx) {
|
2019-05-02 16:01:48 +00:00
|
|
|
// if no var expanded in the child, we count it as a fail
|
2019-04-25 15:12:57 +00:00
|
|
|
if !ctx.var_expanded {
|
|
|
|
break;
|
|
|
|
}
|
2019-05-02 15:23:14 +00:00
|
|
|
|
2019-05-02 16:01:48 +00:00
|
|
|
// Reset `ctx.var_expandeded` to see if there is other expanded variable
|
2019-05-02 15:23:14 +00:00
|
|
|
// in the next matching
|
2019-04-25 15:12:57 +00:00
|
|
|
some_var_expanded = true;
|
|
|
|
ctx.var_expanded = false;
|
2019-04-21 20:17:20 +00:00
|
|
|
|
2019-04-25 15:12:57 +00:00
|
|
|
counter += 1;
|
2019-03-16 12:45:21 +00:00
|
|
|
limit -= 1;
|
|
|
|
if limit == 0 {
|
2019-04-25 15:12:57 +00:00
|
|
|
log::warn!(
|
|
|
|
"expand_tt excced in repeat pattern exceed limit => {:#?}\n{:#?}",
|
|
|
|
template,
|
|
|
|
ctx
|
|
|
|
);
|
2019-03-16 12:45:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-04-25 15:12:57 +00:00
|
|
|
|
|
|
|
let idx = ctx.nesting.pop().unwrap();
|
|
|
|
ctx.nesting.push(idx + 1);
|
2019-04-21 20:17:20 +00:00
|
|
|
token_trees.push(reduce_single_token(t).into());
|
|
|
|
|
2019-04-24 15:01:32 +00:00
|
|
|
if let Some(ref sep) = repeat.separator {
|
|
|
|
match sep {
|
|
|
|
crate::Separator::Ident(ident) => {
|
|
|
|
has_seps = 1;
|
|
|
|
token_trees.push(tt::Leaf::from(ident.clone()).into());
|
|
|
|
}
|
|
|
|
crate::Separator::Literal(lit) => {
|
|
|
|
has_seps = 1;
|
|
|
|
token_trees.push(tt::Leaf::from(lit.clone()).into());
|
|
|
|
}
|
|
|
|
|
|
|
|
crate::Separator::Puncts(puncts) => {
|
|
|
|
has_seps = puncts.len();
|
|
|
|
for punct in puncts {
|
|
|
|
token_trees.push(tt::Leaf::from(*punct).into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-21 20:17:20 +00:00
|
|
|
}
|
2019-04-25 15:12:57 +00:00
|
|
|
|
|
|
|
if let crate::RepeatKind::ZeroOrOne = repeat.kind {
|
|
|
|
break;
|
|
|
|
}
|
2019-01-31 14:16:02 +00:00
|
|
|
}
|
2019-04-25 15:12:57 +00:00
|
|
|
|
2019-05-02 15:23:14 +00:00
|
|
|
// Restore the `var_expanded` by combining old one and the new one
|
|
|
|
ctx.var_expanded = some_var_expanded || old_var_expanded;
|
2019-04-25 15:12:57 +00:00
|
|
|
|
|
|
|
ctx.nesting.pop().unwrap();
|
2019-04-24 15:01:32 +00:00
|
|
|
for _ in 0..has_seps {
|
2019-04-21 20:17:20 +00:00
|
|
|
token_trees.pop();
|
|
|
|
}
|
|
|
|
|
2019-04-25 15:12:57 +00:00
|
|
|
if crate::RepeatKind::OneOrMore == repeat.kind && counter == 0 {
|
|
|
|
return Err(ExpandError::UnexpectedToken);
|
|
|
|
}
|
|
|
|
|
2019-04-21 20:17:20 +00:00
|
|
|
// Check if it is a singel token subtree without any delimiter
|
|
|
|
// e.g {Delimiter:None> ['>'] /Delimiter:None>}
|
|
|
|
reduce_single_token(tt::Subtree { token_trees, delimiter: tt::Delimiter::None })
|
2019-01-31 14:16:02 +00:00
|
|
|
}
|
2019-01-31 18:43:54 +00:00
|
|
|
crate::TokenTree::Leaf(leaf) => match leaf {
|
2019-02-08 11:49:43 +00:00
|
|
|
crate::Leaf::Ident(ident) => {
|
2019-02-11 16:28:39 +00:00
|
|
|
tt::Leaf::from(tt::Ident { text: ident.text.clone(), id: TokenId::unspecified() })
|
|
|
|
.into()
|
2019-02-08 11:49:43 +00:00
|
|
|
}
|
2019-01-31 18:43:54 +00:00
|
|
|
crate::Leaf::Punct(punct) => tt::Leaf::from(punct.clone()).into(),
|
2019-04-20 15:05:25 +00:00
|
|
|
crate::Leaf::Var(v) => {
|
|
|
|
if v.text == "crate" {
|
|
|
|
// FIXME: Properly handle $crate token
|
|
|
|
tt::Leaf::from(tt::Ident { text: "$crate".into(), id: TokenId::unspecified() })
|
|
|
|
.into()
|
2019-05-03 15:23:21 +00:00
|
|
|
} else if !ctx.bindings.contains(&v.text) {
|
|
|
|
// Note that it is possible to have a `$var` inside a macro which is not bound.
|
|
|
|
// For example:
|
|
|
|
// ```
|
|
|
|
// macro_rules! foo {
|
|
|
|
// ($a:ident, $b:ident, $c:tt) => {
|
|
|
|
// macro_rules! bar {
|
|
|
|
// ($bi:ident) => {
|
|
|
|
// fn $bi() -> u8 {$c}
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// We just treat it a normal tokens
|
|
|
|
tt::Subtree {
|
|
|
|
delimiter: tt::Delimiter::None,
|
|
|
|
token_trees: vec![
|
|
|
|
tt::Leaf::from(tt::Punct { char: '$', spacing: tt::Spacing::Alone })
|
|
|
|
.into(),
|
|
|
|
tt::Leaf::from(tt::Ident {
|
|
|
|
text: v.text.clone(),
|
|
|
|
id: TokenId::unspecified(),
|
|
|
|
})
|
|
|
|
.into(),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
.into()
|
2019-04-20 15:05:25 +00:00
|
|
|
} else {
|
2019-04-25 15:12:57 +00:00
|
|
|
let tkn = ctx.bindings.get(&v.text, &ctx.nesting)?.clone();
|
|
|
|
ctx.var_expanded = true;
|
2019-04-21 20:17:20 +00:00
|
|
|
|
|
|
|
if let tt::TokenTree::Subtree(subtree) = tkn {
|
|
|
|
reduce_single_token(subtree)
|
|
|
|
} else {
|
|
|
|
tkn
|
|
|
|
}
|
2019-04-20 15:05:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-08 11:49:43 +00:00
|
|
|
crate::Leaf::Literal(l) => tt::Leaf::from(tt::Literal { text: l.text.clone() }).into(),
|
2019-01-31 14:16:02 +00:00
|
|
|
},
|
|
|
|
};
|
2019-03-02 19:20:26 +00:00
|
|
|
Ok(res)
|
2019-01-31 10:49:57 +00:00
|
|
|
}
|
2019-03-03 19:33:50 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use ra_syntax::{ast, AstNode};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::ast_to_token_tree;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_expand_rule() {
|
2019-05-03 15:23:21 +00:00
|
|
|
// FIXME: The missing $var check should be in parsing phase
|
|
|
|
// assert_err(
|
|
|
|
// "($i:ident) => ($j)",
|
|
|
|
// "foo!{a}",
|
|
|
|
// ExpandError::BindingError(String::from("could not find binding `j`")),
|
|
|
|
// );
|
2019-03-03 19:33:50 +00:00
|
|
|
|
|
|
|
assert_err(
|
|
|
|
"($($i:ident);*) => ($i)",
|
|
|
|
"foo!{a}",
|
|
|
|
ExpandError::BindingError(String::from(
|
|
|
|
"expected simple binding, found nested binding `i`",
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_err("($i) => ($i)", "foo!{a}", ExpandError::UnexpectedToken);
|
|
|
|
assert_err("($i:) => ($i)", "foo!{a}", ExpandError::UnexpectedToken);
|
2019-04-18 02:21:36 +00:00
|
|
|
|
|
|
|
// FIXME:
|
|
|
|
// Add an err test case for ($($i:ident)) => ($())
|
2019-03-03 19:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_err(macro_body: &str, invocation: &str, err: ExpandError) {
|
|
|
|
assert_eq!(expand_first(&create_rules(&format_macro(macro_body)), invocation), Err(err));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn format_macro(macro_body: &str) -> String {
|
|
|
|
format!(
|
|
|
|
"
|
|
|
|
macro_rules! foo {{
|
|
|
|
{}
|
|
|
|
}}
|
|
|
|
",
|
|
|
|
macro_body
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_rules(macro_definition: &str) -> crate::MacroRules {
|
|
|
|
let source_file = ast::SourceFile::parse(macro_definition);
|
|
|
|
let macro_definition =
|
|
|
|
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
|
|
|
|
|
|
|
|
let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
|
|
|
|
crate::MacroRules::parse(&definition_tt).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_first(
|
|
|
|
rules: &crate::MacroRules,
|
|
|
|
invocation: &str,
|
|
|
|
) -> Result<tt::Subtree, ExpandError> {
|
|
|
|
let source_file = ast::SourceFile::parse(invocation);
|
|
|
|
let macro_invocation =
|
|
|
|
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
|
|
|
|
|
|
|
|
let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
|
|
|
|
|
|
|
|
expand_rule(&rules.rules[0], &invocation_tt)
|
|
|
|
}
|
|
|
|
}
|