2019-09-16 18:54:22 -05:00
|
|
|
//! Parser recognizes special macro syntax, `$var` and `$(repeat)*`, in token
|
|
|
|
//! trees.
|
|
|
|
|
2022-12-27 03:18:18 -06:00
|
|
|
use smallvec::{smallvec, SmallVec};
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::SmolStr;
|
2019-09-16 18:54:22 -05:00
|
|
|
|
2021-02-01 14:42:37 -06:00
|
|
|
use crate::{tt_iter::TtIter, ParseError};
|
|
|
|
|
2021-10-02 12:21:23 -05:00
|
|
|
/// Consider
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// macro_rules! an_macro {
|
|
|
|
/// ($x:expr + $y:expr) => ($y * $x)
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Stuff to the left of `=>` is a [`MetaTemplate`] pattern (which is matched
|
|
|
|
/// with input).
|
|
|
|
///
|
|
|
|
/// Stuff to the right is a [`MetaTemplate`] template which is used to produce
|
|
|
|
/// output.
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub(crate) struct MetaTemplate(pub(crate) Vec<Op>);
|
|
|
|
|
|
|
|
impl MetaTemplate {
|
|
|
|
pub(crate) fn parse_pattern(pattern: &tt::Subtree) -> Result<MetaTemplate, ParseError> {
|
2021-10-02 12:38:28 -05:00
|
|
|
MetaTemplate::parse(pattern, Mode::Pattern)
|
2021-10-02 12:21:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn parse_template(template: &tt::Subtree) -> Result<MetaTemplate, ParseError> {
|
2021-10-02 12:38:28 -05:00
|
|
|
MetaTemplate::parse(template, Mode::Template)
|
2021-10-02 12:21:23 -05:00
|
|
|
}
|
2021-02-01 14:42:37 -06:00
|
|
|
|
2021-10-02 12:21:23 -05:00
|
|
|
pub(crate) fn iter(&self) -> impl Iterator<Item = &Op> {
|
|
|
|
self.0.iter()
|
|
|
|
}
|
2021-10-02 12:38:28 -05:00
|
|
|
|
|
|
|
fn parse(tt: &tt::Subtree, mode: Mode) -> Result<MetaTemplate, ParseError> {
|
|
|
|
let mut src = TtIter::new(tt);
|
|
|
|
|
|
|
|
let mut res = Vec::new();
|
2022-12-27 03:18:18 -06:00
|
|
|
while let Some(first) = src.peek_n(0) {
|
2021-10-02 12:38:28 -05:00
|
|
|
let op = next_op(first, &mut src, mode)?;
|
2022-01-02 09:35:58 -06:00
|
|
|
res.push(op);
|
2021-10-02 12:38:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(MetaTemplate(res))
|
|
|
|
}
|
2021-02-01 14:42:37 -06:00
|
|
|
}
|
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 {
|
2022-10-10 07:25:14 -05:00
|
|
|
Var { name: SmolStr, kind: Option<MetaVarKind>, id: tt::TokenId },
|
2022-07-11 11:31:42 -05:00
|
|
|
Ignore { name: SmolStr, id: tt::TokenId },
|
|
|
|
Index { depth: u32 },
|
2021-01-30 02:12:30 -06:00
|
|
|
Repeat { tokens: MetaTemplate, kind: RepeatKind, separator: Option<Separator> },
|
2021-10-02 12:11:08 -05:00
|
|
|
Subtree { tokens: MetaTemplate, delimiter: Option<tt::Delimiter> },
|
2022-12-27 03:18:18 -06:00
|
|
|
Literal(tt::Literal),
|
|
|
|
Punct(SmallVec<[tt::Punct; 3]>),
|
|
|
|
Ident(tt::Ident),
|
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,
|
|
|
|
}
|
|
|
|
|
2022-10-10 07:25:14 -05:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub(crate) enum MetaVarKind {
|
|
|
|
Path,
|
|
|
|
Ty,
|
|
|
|
Pat,
|
|
|
|
PatParam,
|
|
|
|
Stmt,
|
|
|
|
Block,
|
|
|
|
Meta,
|
|
|
|
Item,
|
|
|
|
Vis,
|
|
|
|
Expr,
|
|
|
|
Ident,
|
|
|
|
Tt,
|
|
|
|
Lifetime,
|
|
|
|
Literal,
|
|
|
|
}
|
|
|
|
|
2019-09-16 18:54:22 -05:00
|
|
|
#[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) {
|
2022-01-01 19:39:14 -06:00
|
|
|
(Ident(a), Ident(b)) => a.text == b.text,
|
|
|
|
(Literal(a), Literal(b)) => a.text == b.text,
|
|
|
|
(Puncts(a), Puncts(b)) if a.len() == b.len() => {
|
2019-09-16 18:54:22 -05:00
|
|
|
let a_iter = a.iter().map(|a| a.char);
|
|
|
|
let b_iter = b.iter().map(|b| b.char);
|
|
|
|
a_iter.eq(b_iter)
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum Mode {
|
|
|
|
Pattern,
|
|
|
|
Template,
|
|
|
|
}
|
|
|
|
|
2022-12-27 03:18:18 -06:00
|
|
|
fn next_op<'a>(
|
|
|
|
first_peeked: &tt::TokenTree,
|
|
|
|
src: &mut TtIter<'a>,
|
|
|
|
mode: Mode,
|
|
|
|
) -> Result<Op, ParseError> {
|
|
|
|
let res = match first_peeked {
|
|
|
|
tt::TokenTree::Leaf(tt::Leaf::Punct(p @ tt::Punct { char: '$', .. })) => {
|
|
|
|
src.next().expect("first token already peeked");
|
2020-03-03 11:03:44 -06:00
|
|
|
// Note that the '$' itself is a valid token inside macro_rules.
|
|
|
|
let second = match src.next() {
|
2022-12-27 03:18:18 -06:00
|
|
|
None => return Ok(Op::Punct(smallvec![p.clone()])),
|
2020-03-03 11:03:44 -06:00
|
|
|
Some(it) => it,
|
|
|
|
};
|
2019-09-16 18:54:22 -05:00
|
|
|
match second {
|
2022-07-11 11:31:42 -05:00
|
|
|
tt::TokenTree::Subtree(subtree) => match subtree.delimiter_kind() {
|
|
|
|
Some(tt::DelimiterKind::Parenthesis) => {
|
|
|
|
let (separator, kind) = parse_repeat(src)?;
|
|
|
|
let tokens = MetaTemplate::parse(subtree, mode)?;
|
|
|
|
Op::Repeat { tokens, separator, kind }
|
|
|
|
}
|
|
|
|
Some(tt::DelimiterKind::Brace) => match mode {
|
|
|
|
Mode::Template => {
|
|
|
|
parse_metavar_expr(&mut TtIter::new(subtree)).map_err(|()| {
|
|
|
|
ParseError::unexpected("invalid metavariable expression")
|
|
|
|
})?
|
|
|
|
}
|
|
|
|
Mode::Pattern => {
|
|
|
|
return Err(ParseError::unexpected(
|
|
|
|
"`${}` metavariable expressions are not allowed in matchers",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
return Err(ParseError::expected(
|
|
|
|
"expected `$()` repetition or `${}` expression",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
},
|
2019-09-16 18:54:22 -05:00
|
|
|
tt::TokenTree::Leaf(leaf) => match leaf {
|
2021-01-08 00:00:16 -06:00
|
|
|
tt::Leaf::Ident(ident) if ident.text == "crate" => {
|
|
|
|
// We simply produce identifier `$crate` here. And it will be resolved when lowering ast to Path.
|
2022-12-27 03:18:18 -06:00
|
|
|
Op::Ident(tt::Ident { text: "$crate".into(), id: ident.id })
|
2021-01-08 00:00:16 -06:00
|
|
|
}
|
2019-09-16 18:54:22 -05:00
|
|
|
tt::Leaf::Ident(ident) => {
|
|
|
|
let kind = eat_fragment_kind(src, mode)?;
|
2022-01-02 09:35:58 -06:00
|
|
|
let name = ident.text.clone();
|
2021-01-03 20:53:31 -06:00
|
|
|
let id = ident.id;
|
|
|
|
Op::Var { name, kind, id }
|
2019-09-16 18:54:22 -05:00
|
|
|
}
|
2022-01-02 09:35:58 -06:00
|
|
|
tt::Leaf::Literal(lit) if is_boolean_literal(lit) => {
|
|
|
|
let kind = eat_fragment_kind(src, mode)?;
|
|
|
|
let name = lit.text.clone();
|
|
|
|
let id = lit.id;
|
|
|
|
Op::Var { name, kind, id }
|
|
|
|
}
|
2022-06-02 14:36:11 -05:00
|
|
|
tt::Leaf::Punct(punct @ tt::Punct { char: '$', .. }) => match mode {
|
|
|
|
Mode::Pattern => {
|
|
|
|
return Err(ParseError::unexpected(
|
|
|
|
"`$$` is not allowed on the pattern side",
|
|
|
|
))
|
|
|
|
}
|
2022-12-27 03:18:18 -06:00
|
|
|
Mode::Template => Op::Punct(smallvec![*punct]),
|
2022-06-02 14:36:11 -05:00
|
|
|
},
|
2022-01-02 09:35:58 -06:00
|
|
|
tt::Leaf::Punct(_) | tt::Leaf::Literal(_) => {
|
2022-02-03 10:25:24 -06:00
|
|
|
return Err(ParseError::expected("expected ident"))
|
2019-09-16 18:54:22 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-12-27 03:18:18 -06:00
|
|
|
|
|
|
|
tt::TokenTree::Leaf(tt::Leaf::Literal(it)) => {
|
|
|
|
src.next().expect("first token already peeked");
|
|
|
|
Op::Literal(it.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
tt::TokenTree::Leaf(tt::Leaf::Ident(it)) => {
|
|
|
|
src.next().expect("first token already peeked");
|
|
|
|
Op::Ident(it.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
tt::TokenTree::Leaf(tt::Leaf::Punct(_)) => {
|
|
|
|
// There's at least one punct so this shouldn't fail.
|
|
|
|
let puncts = src.expect_glued_punct().unwrap();
|
|
|
|
Op::Punct(puncts)
|
|
|
|
}
|
|
|
|
|
2020-12-29 12:35:21 -06:00
|
|
|
tt::TokenTree::Subtree(subtree) => {
|
2022-12-27 03:18:18 -06:00
|
|
|
src.next().expect("first token already peeked");
|
2021-10-02 12:38:28 -05:00
|
|
|
let tokens = MetaTemplate::parse(subtree, mode)?;
|
|
|
|
Op::Subtree { tokens, delimiter: subtree.delimiter }
|
2020-12-29 12:35:21 -06:00
|
|
|
}
|
2019-09-16 18:54:22 -05:00
|
|
|
};
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2022-10-10 07:25:14 -05:00
|
|
|
fn eat_fragment_kind(src: &mut TtIter<'_>, mode: Mode) -> Result<Option<MetaVarKind>, ParseError> {
|
2019-09-16 18:54:22 -05:00
|
|
|
if let Mode::Pattern = mode {
|
2022-02-03 10:25:24 -06:00
|
|
|
src.expect_char(':').map_err(|()| ParseError::unexpected("missing fragment specifier"))?;
|
|
|
|
let ident = src
|
|
|
|
.expect_ident()
|
|
|
|
.map_err(|()| ParseError::unexpected("missing fragment specifier"))?;
|
2022-10-10 07:25:14 -05:00
|
|
|
let kind = match ident.text.as_str() {
|
|
|
|
"path" => MetaVarKind::Path,
|
|
|
|
"ty" => MetaVarKind::Ty,
|
|
|
|
"pat" => MetaVarKind::Pat,
|
|
|
|
"pat_param" => MetaVarKind::PatParam,
|
|
|
|
"stmt" => MetaVarKind::Stmt,
|
|
|
|
"block" => MetaVarKind::Block,
|
|
|
|
"meta" => MetaVarKind::Meta,
|
|
|
|
"item" => MetaVarKind::Item,
|
|
|
|
"vis" => MetaVarKind::Vis,
|
|
|
|
"expr" => MetaVarKind::Expr,
|
|
|
|
"ident" => MetaVarKind::Ident,
|
|
|
|
"tt" => MetaVarKind::Tt,
|
|
|
|
"lifetime" => MetaVarKind::Lifetime,
|
|
|
|
"literal" => MetaVarKind::Literal,
|
|
|
|
_ => return Ok(None),
|
|
|
|
};
|
|
|
|
return Ok(Some(kind));
|
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
|
|
|
}
|
|
|
|
|
2022-07-20 08:02:08 -05:00
|
|
|
fn parse_repeat(src: &mut TtIter<'_>) -> Result<(Option<Separator>, RepeatKind), ParseError> {
|
2019-09-16 18:54:22 -05:00
|
|
|
let mut separator = Separator::Puncts(SmallVec::new());
|
|
|
|
for tt in src {
|
|
|
|
let tt = match tt {
|
|
|
|
tt::TokenTree::Leaf(leaf) => leaf,
|
2021-01-29 10:21:43 -06:00
|
|
|
tt::TokenTree::Subtree(_) => return Err(ParseError::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 => {
|
2021-01-29 10:21:43 -06:00
|
|
|
return Err(ParseError::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,
|
2022-01-02 09:35:58 -06:00
|
|
|
_ => match &mut separator {
|
|
|
|
Separator::Puncts(puncts) if puncts.len() != 3 => {
|
|
|
|
puncts.push(*punct);
|
|
|
|
continue;
|
2019-09-16 18:54:22 -05:00
|
|
|
}
|
2022-01-02 09:35:58 -06:00
|
|
|
_ => return Err(ParseError::InvalidRepeat),
|
|
|
|
},
|
2019-09-16 18:54:22 -05:00
|
|
|
};
|
2022-01-02 09:35:58 -06:00
|
|
|
return Ok((has_sep.then(|| separator), repeat_kind));
|
2019-09-16 18:54:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 10:21:43 -06:00
|
|
|
Err(ParseError::InvalidRepeat)
|
2019-09-16 18:54:22 -05:00
|
|
|
}
|
2022-07-11 11:31:42 -05:00
|
|
|
|
2022-07-20 08:02:08 -05:00
|
|
|
fn parse_metavar_expr(src: &mut TtIter<'_>) -> Result<Op, ()> {
|
2022-07-11 11:31:42 -05:00
|
|
|
let func = src.expect_ident()?;
|
|
|
|
let args = src.expect_subtree()?;
|
|
|
|
|
|
|
|
if args.delimiter_kind() != Some(tt::DelimiterKind::Parenthesis) {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut args = TtIter::new(args);
|
|
|
|
|
|
|
|
let op = match &*func.text {
|
|
|
|
"ignore" => {
|
|
|
|
let ident = args.expect_ident()?;
|
|
|
|
Op::Ignore { name: ident.text.clone(), id: ident.id }
|
|
|
|
}
|
|
|
|
"index" => {
|
|
|
|
let depth = if args.len() == 0 { 0 } else { args.expect_u32_literal()? };
|
|
|
|
Op::Index { depth }
|
|
|
|
}
|
|
|
|
_ => return Err(()),
|
|
|
|
};
|
|
|
|
|
|
|
|
if args.next().is_some() {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(op)
|
|
|
|
}
|