2019-09-16 18:54:22 -05:00
|
|
|
//! Parser recognizes special macro syntax, `$var` and `$(repeat)*`, in token
|
|
|
|
//! trees.
|
|
|
|
|
|
|
|
use smallvec::SmallVec;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::SmolStr;
|
2019-09-16 18:54:22 -05:00
|
|
|
|
2020-12-29 12:35:21 -06:00
|
|
|
use crate::{tt_iter::TtIter, ExpandError, MetaTemplate};
|
2019-09-16 18:54:22 -05:00
|
|
|
|
2020-12-29 12:35:21 -06:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub(crate) enum Op {
|
2021-01-03 04:47:57 -06:00
|
|
|
Var { name: SmolStr, kind: Option<SmolStr> },
|
2020-12-29 12:35:21 -06:00
|
|
|
Repeat { subtree: MetaTemplate, kind: RepeatKind, separator: Option<Separator> },
|
|
|
|
Leaf(tt::Leaf),
|
|
|
|
Subtree(MetaTemplate),
|
2019-09-16 18:54:22 -05:00
|
|
|
}
|
|
|
|
|
2020-12-29 12:35:21 -06:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2019-09-16 18:54:22 -05:00
|
|
|
pub(crate) enum RepeatKind {
|
|
|
|
ZeroOrMore,
|
|
|
|
OneOrMore,
|
|
|
|
ZeroOrOne,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq)]
|
|
|
|
pub(crate) enum Separator {
|
|
|
|
Literal(tt::Literal),
|
|
|
|
Ident(tt::Ident),
|
|
|
|
Puncts(SmallVec<[tt::Punct; 3]>),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that when we compare a Separator, we just care about its textual value.
|
|
|
|
impl PartialEq for Separator {
|
|
|
|
fn eq(&self, other: &Separator) -> bool {
|
|
|
|
use Separator::*;
|
|
|
|
|
|
|
|
match (self, other) {
|
|
|
|
(Ident(ref a), Ident(ref b)) => a.text == b.text,
|
|
|
|
(Literal(ref a), Literal(ref b)) => a.text == b.text,
|
|
|
|
(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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 12:35:21 -06:00
|
|
|
pub(crate) fn parse_template(template: &tt::Subtree) -> Vec<Result<Op, ExpandError>> {
|
|
|
|
parse_inner(&template, Mode::Template)
|
2019-09-16 18:54:22 -05:00
|
|
|
}
|
|
|
|
|
2020-12-29 12:35:21 -06:00
|
|
|
pub(crate) fn parse_pattern(pattern: &tt::Subtree) -> Vec<Result<Op, ExpandError>> {
|
|
|
|
parse_inner(&pattern, Mode::Pattern)
|
2019-09-16 18:54:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum Mode {
|
|
|
|
Pattern,
|
|
|
|
Template,
|
|
|
|
}
|
|
|
|
|
2020-12-29 12:35:21 -06:00
|
|
|
fn parse_inner(tt: &tt::Subtree, mode: Mode) -> Vec<Result<Op, ExpandError>> {
|
|
|
|
let mut src = TtIter::new(&tt);
|
2019-09-16 18:54:22 -05:00
|
|
|
std::iter::from_fn(move || {
|
|
|
|
let first = src.next()?;
|
|
|
|
Some(next_op(first, &mut src, mode))
|
|
|
|
})
|
2020-12-29 12:35:21 -06:00
|
|
|
.collect()
|
2019-09-16 18:54:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! err {
|
|
|
|
($($tt:tt)*) => {
|
|
|
|
ExpandError::UnexpectedToken
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! bail {
|
|
|
|
($($tt:tt)*) => {
|
|
|
|
return Err(err!($($tt)*))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-29 12:35:21 -06:00
|
|
|
fn next_op<'a>(first: &tt::TokenTree, src: &mut TtIter<'a>, mode: Mode) -> Result<Op, ExpandError> {
|
2019-09-16 18:54:22 -05:00
|
|
|
let res = match first {
|
2020-12-29 12:35:21 -06:00
|
|
|
tt::TokenTree::Leaf(leaf @ tt::Leaf::Punct(tt::Punct { char: '$', .. })) => {
|
2020-03-03 11:03:44 -06:00
|
|
|
// Note that the '$' itself is a valid token inside macro_rules.
|
|
|
|
let second = match src.next() {
|
2020-12-29 12:35:21 -06:00
|
|
|
None => return Ok(Op::Leaf(leaf.clone())),
|
2020-03-03 11:03:44 -06:00
|
|
|
Some(it) => it,
|
|
|
|
};
|
2019-09-16 18:54:22 -05:00
|
|
|
match second {
|
|
|
|
tt::TokenTree::Subtree(subtree) => {
|
|
|
|
let (separator, kind) = parse_repeat(src)?;
|
2020-12-29 12:35:21 -06:00
|
|
|
let delimiter = subtree.delimiter;
|
|
|
|
let tokens = parse_inner(&subtree, mode);
|
|
|
|
let subtree = MetaTemplate { tokens, delimiter };
|
2019-09-16 18:54:22 -05:00
|
|
|
Op::Repeat { subtree, separator, kind }
|
|
|
|
}
|
|
|
|
tt::TokenTree::Leaf(leaf) => match leaf {
|
2020-12-28 04:51:43 -06:00
|
|
|
tt::Leaf::Punct(punct) => {
|
|
|
|
static UNDERSCORE: SmolStr = SmolStr::new_inline("_");
|
|
|
|
|
|
|
|
if punct.char != '_' {
|
|
|
|
return Err(ExpandError::UnexpectedToken);
|
|
|
|
}
|
2020-12-29 12:35:21 -06:00
|
|
|
let name = UNDERSCORE.clone();
|
2020-12-28 04:51:43 -06:00
|
|
|
let kind = eat_fragment_kind(src, mode)?;
|
2021-01-03 04:47:57 -06:00
|
|
|
Op::Var { name, kind }
|
2020-12-18 09:47:48 -06:00
|
|
|
}
|
2019-09-16 18:54:22 -05:00
|
|
|
tt::Leaf::Ident(ident) => {
|
2020-12-29 12:35:21 -06:00
|
|
|
let name = ident.text.clone();
|
2019-09-16 18:54:22 -05:00
|
|
|
let kind = eat_fragment_kind(src, mode)?;
|
2021-01-03 04:47:57 -06:00
|
|
|
Op::Var { name, kind }
|
2019-09-16 18:54:22 -05:00
|
|
|
}
|
|
|
|
tt::Leaf::Literal(lit) => {
|
2020-12-29 12:35:21 -06:00
|
|
|
if is_boolean_literal(&lit) {
|
|
|
|
let name = lit.text.clone();
|
2019-09-16 18:54:22 -05:00
|
|
|
let kind = eat_fragment_kind(src, mode)?;
|
2021-01-03 04:47:57 -06:00
|
|
|
Op::Var { name, kind }
|
2019-09-16 18:54:22 -05:00
|
|
|
} else {
|
|
|
|
bail!("bad var 2");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-12-29 12:35:21 -06:00
|
|
|
tt::TokenTree::Leaf(tt) => Op::Leaf(tt.clone()),
|
|
|
|
tt::TokenTree::Subtree(subtree) => {
|
|
|
|
let delimiter = subtree.delimiter;
|
|
|
|
let tokens = parse_inner(&subtree, mode);
|
|
|
|
let subtree = MetaTemplate { tokens, delimiter };
|
|
|
|
Op::Subtree(subtree)
|
|
|
|
}
|
2019-09-16 18:54:22 -05:00
|
|
|
};
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2020-12-29 12:35:21 -06:00
|
|
|
fn eat_fragment_kind<'a>(src: &mut TtIter<'a>, mode: Mode) -> Result<Option<SmolStr>, ExpandError> {
|
2019-09-16 18:54:22 -05:00
|
|
|
if let Mode::Pattern = mode {
|
|
|
|
src.expect_char(':').map_err(|()| err!("bad fragment specifier 1"))?;
|
|
|
|
let ident = src.expect_ident().map_err(|()| err!("bad fragment specifier 1"))?;
|
2020-12-29 12:35:21 -06:00
|
|
|
return Ok(Some(ident.text.clone()));
|
2019-09-16 18:54:22 -05:00
|
|
|
};
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_boolean_literal(lit: &tt::Literal) -> bool {
|
2020-06-27 20:02:03 -05:00
|
|
|
matches!(lit.text.as_str(), "true" | "false")
|
2019-09-16 18:54:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_repeat(src: &mut TtIter) -> Result<(Option<Separator>, RepeatKind), ExpandError> {
|
|
|
|
let mut separator = Separator::Puncts(SmallVec::new());
|
|
|
|
for tt in src {
|
|
|
|
let tt = match tt {
|
|
|
|
tt::TokenTree::Leaf(leaf) => leaf,
|
2020-02-18 06:53:02 -06:00
|
|
|
tt::TokenTree::Subtree(_) => return Err(ExpandError::InvalidRepeat),
|
2019-09-16 18:54:22 -05:00
|
|
|
};
|
|
|
|
let has_sep = match &separator {
|
2020-02-18 06:53:02 -06:00
|
|
|
Separator::Puncts(puncts) => !puncts.is_empty(),
|
2019-09-16 18:54:22 -05:00
|
|
|
_ => true,
|
|
|
|
};
|
|
|
|
match tt {
|
|
|
|
tt::Leaf::Ident(_) | tt::Leaf::Literal(_) if has_sep => {
|
2020-02-18 06:53:02 -06:00
|
|
|
return Err(ExpandError::InvalidRepeat)
|
2019-09-16 18:54:22 -05:00
|
|
|
}
|
|
|
|
tt::Leaf::Ident(ident) => separator = Separator::Ident(ident.clone()),
|
|
|
|
tt::Leaf::Literal(lit) => separator = Separator::Literal(lit.clone()),
|
|
|
|
tt::Leaf::Punct(punct) => {
|
|
|
|
let repeat_kind = match punct.char {
|
|
|
|
'*' => RepeatKind::ZeroOrMore,
|
|
|
|
'+' => RepeatKind::OneOrMore,
|
|
|
|
'?' => RepeatKind::ZeroOrOne,
|
|
|
|
_ => {
|
|
|
|
match &mut separator {
|
|
|
|
Separator::Puncts(puncts) => {
|
|
|
|
if puncts.len() == 3 {
|
2020-02-18 06:53:02 -06:00
|
|
|
return Err(ExpandError::InvalidRepeat);
|
2019-09-16 18:54:22 -05:00
|
|
|
}
|
|
|
|
puncts.push(punct.clone())
|
|
|
|
}
|
2020-02-18 06:53:02 -06:00
|
|
|
_ => return Err(ExpandError::InvalidRepeat),
|
2019-09-16 18:54:22 -05:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let separator = if has_sep { Some(separator) } else { None };
|
|
|
|
return Ok((separator, repeat_kind));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(ExpandError::InvalidRepeat)
|
|
|
|
}
|