rust-analyzer/crates/mbe/src/parser.rs

224 lines
7.4 KiB
Rust
Raw Normal View History

//! Parser recognizes special macro syntax, `$var` and `$(repeat)*`, in token
//! trees.
use smallvec::SmallVec;
2020-08-12 16:26:51 +00:00
use syntax::SmolStr;
2021-02-01 20:42:37 +00:00
use crate::{tt_iter::TtIter, ParseError};
2021-10-02 17:21:23 +00: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 17:38:28 +00:00
MetaTemplate::parse(pattern, Mode::Pattern)
2021-10-02 17:21:23 +00:00
}
pub(crate) fn parse_template(template: &tt::Subtree) -> Result<MetaTemplate, ParseError> {
2021-10-02 17:38:28 +00:00
MetaTemplate::parse(template, Mode::Template)
2021-10-02 17:21:23 +00:00
}
2021-02-01 20:42:37 +00:00
2021-10-02 17:21:23 +00:00
pub(crate) fn iter(&self) -> impl Iterator<Item = &Op> {
self.0.iter()
}
2021-10-02 17:38:28 +00:00
fn parse(tt: &tt::Subtree, mode: Mode) -> Result<MetaTemplate, ParseError> {
let mut src = TtIter::new(tt);
let mut res = Vec::new();
while let Some(first) = src.next() {
let op = next_op(first, &mut src, mode)?;
res.push(op)
}
Ok(MetaTemplate(res))
}
2021-02-01 20:42:37 +00:00
}
2020-12-29 18:35:21 +00:00
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Op {
2021-01-04 02:53:31 +00:00
Var { name: SmolStr, kind: Option<SmolStr>, id: tt::TokenId },
2021-01-30 08:12:30 +00:00
Repeat { tokens: MetaTemplate, kind: RepeatKind, separator: Option<Separator> },
2020-12-29 18:35:21 +00:00
Leaf(tt::Leaf),
Subtree { tokens: MetaTemplate, delimiter: Option<tt::Delimiter> },
}
2020-12-29 18:35:21 +00:00
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
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,
}
}
}
2021-02-01 20:42:37 +00:00
impl Separator {
pub(crate) fn tt_count(&self) -> usize {
match self {
Separator::Literal(_) => 1,
Separator::Ident(_) => 1,
Separator::Puncts(it) => it.len(),
}
}
}
#[derive(Clone, Copy)]
enum Mode {
Pattern,
Template,
}
macro_rules! err {
($($tt:tt)*) => {
ParseError::UnexpectedToken(($($tt)*).to_string())
};
}
macro_rules! bail {
($($tt:tt)*) => {
return Err(err!($($tt)*))
};
}
fn next_op<'a>(first: &tt::TokenTree, src: &mut TtIter<'a>, mode: Mode) -> Result<Op, ParseError> {
let res = match first {
2020-12-29 18:35:21 +00:00
tt::TokenTree::Leaf(leaf @ tt::Leaf::Punct(tt::Punct { char: '$', .. })) => {
2020-03-03 17:03:44 +00:00
// Note that the '$' itself is a valid token inside macro_rules.
let second = match src.next() {
2020-12-29 18:35:21 +00:00
None => return Ok(Op::Leaf(leaf.clone())),
2020-03-03 17:03:44 +00:00
Some(it) => it,
};
match second {
tt::TokenTree::Subtree(subtree) => {
let (separator, kind) = parse_repeat(src)?;
2021-10-02 17:38:28 +00:00
let tokens = MetaTemplate::parse(subtree, mode)?;
Op::Repeat { tokens, separator, kind }
}
tt::TokenTree::Leaf(leaf) => match leaf {
tt::Leaf::Punct(_) => {
return Err(ParseError::Expected("ident".to_string()));
2020-12-18 15:47:48 +00:00
}
2021-01-08 06:00:16 +00: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.
Op::Leaf(tt::Leaf::from(tt::Ident { text: "$crate".into(), id: ident.id }))
}
tt::Leaf::Ident(ident) => {
2020-12-29 18:35:21 +00:00
let name = ident.text.clone();
let kind = eat_fragment_kind(src, mode)?;
2021-01-04 02:53:31 +00:00
let id = ident.id;
Op::Var { name, kind, id }
}
tt::Leaf::Literal(lit) => {
2021-06-13 03:54:16 +00:00
if is_boolean_literal(lit) {
2020-12-29 18:35:21 +00:00
let name = lit.text.clone();
let kind = eat_fragment_kind(src, mode)?;
2021-01-04 02:53:31 +00:00
let id = lit.id;
Op::Var { name, kind, id }
} else {
bail!("bad var 2");
}
}
},
}
}
2020-12-29 18:35:21 +00:00
tt::TokenTree::Leaf(tt) => Op::Leaf(tt.clone()),
tt::TokenTree::Subtree(subtree) => {
2021-10-02 17:38:28 +00:00
let tokens = MetaTemplate::parse(subtree, mode)?;
Op::Subtree { tokens, delimiter: subtree.delimiter }
2020-12-29 18:35:21 +00:00
}
};
Ok(res)
}
fn eat_fragment_kind(src: &mut TtIter<'_>, mode: Mode) -> Result<Option<SmolStr>, ParseError> {
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 18:35:21 +00:00
return Ok(Some(ident.text.clone()));
};
Ok(None)
}
fn is_boolean_literal(lit: &tt::Literal) -> bool {
2020-06-28 01:02:03 +00:00
matches!(lit.text.as_str(), "true" | "false")
}
fn parse_repeat(src: &mut TtIter) -> Result<(Option<Separator>, RepeatKind), ParseError> {
let mut separator = Separator::Puncts(SmallVec::new());
for tt in src {
let tt = match tt {
tt::TokenTree::Leaf(leaf) => leaf,
tt::TokenTree::Subtree(_) => return Err(ParseError::InvalidRepeat),
};
let has_sep = match &separator {
Separator::Puncts(puncts) => !puncts.is_empty(),
_ => true,
};
match tt {
tt::Leaf::Ident(_) | tt::Leaf::Literal(_) if has_sep => {
return Err(ParseError::InvalidRepeat)
}
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 {
return Err(ParseError::InvalidRepeat);
}
puncts.push(*punct)
}
_ => return Err(ParseError::InvalidRepeat),
}
continue;
}
};
let separator = if has_sep { Some(separator) } else { None };
return Ok((separator, repeat_kind));
}
}
}
Err(ParseError::InvalidRepeat)
}