2019-09-22 12:36:35 -05:00
|
|
|
//! This module implements declarative macros: old `macro_rules` and the newer
|
|
|
|
//! `macro`. Declarative macros are also known as "macro by example", and that's
|
|
|
|
//! why we call this module `mbe`. For external documentation, prefer the
|
|
|
|
//! official terminology: "declarative macros".
|
|
|
|
|
2019-09-22 10:28:32 -05:00
|
|
|
crate mod macro_check;
|
|
|
|
crate mod macro_parser;
|
|
|
|
crate mod macro_rules;
|
2022-03-09 13:46:23 -06:00
|
|
|
crate mod metavar_expr;
|
2019-09-22 10:28:32 -05:00
|
|
|
crate mod quoted;
|
2019-12-22 16:42:04 -06:00
|
|
|
crate mod transcribe;
|
2019-09-22 11:17:30 -05:00
|
|
|
|
2022-03-09 13:46:23 -06:00
|
|
|
use metavar_expr::MetaVarExpr;
|
2020-08-01 10:45:17 -05:00
|
|
|
use rustc_ast::token::{self, NonterminalKind, Token, TokenKind};
|
2020-02-29 11:37:32 -06:00
|
|
|
use rustc_ast::tokenstream::DelimSpan;
|
2022-03-09 13:46:23 -06:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2020-04-19 06:00:18 -05:00
|
|
|
use rustc_span::symbol::Ident;
|
2019-12-31 11:15:40 -06:00
|
|
|
use rustc_span::Span;
|
2019-09-22 11:17:30 -05:00
|
|
|
|
2022-03-19 00:20:07 -05:00
|
|
|
/// Contains the sub-token-trees of a "delimited" token tree such as `(a b c)`. The delimiter itself
|
|
|
|
/// might be `NoDelim`.
|
2020-06-11 09:49:57 -05:00
|
|
|
#[derive(Clone, PartialEq, Encodable, Decodable, Debug)]
|
2019-09-22 11:42:52 -05:00
|
|
|
struct Delimited {
|
|
|
|
delim: token::DelimToken,
|
2022-03-19 00:20:07 -05:00
|
|
|
/// Note: This contains the opening and closing delimiters tokens (e.g. `(` and `)`). Note that
|
|
|
|
/// these could be `NoDelim`. These token kinds must match `delim`, and the methods below
|
|
|
|
/// debug_assert this.
|
|
|
|
all_tts: Vec<TokenTree>,
|
2019-09-22 11:17:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Delimited {
|
2022-03-19 00:20:07 -05:00
|
|
|
/// Returns a `self::TokenTree` with a `Span` corresponding to the opening delimiter. Panics if
|
|
|
|
/// the delimiter is `NoDelim`.
|
|
|
|
fn open_tt(&self) -> &TokenTree {
|
|
|
|
let tt = self.all_tts.first().unwrap();
|
|
|
|
debug_assert!(matches!(
|
|
|
|
tt,
|
|
|
|
&TokenTree::Token(token::Token { kind: token::OpenDelim(d), .. }) if d == self.delim
|
|
|
|
));
|
|
|
|
tt
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a `self::TokenTree` with a `Span` corresponding to the closing delimiter. Panics if
|
2022-03-30 00:39:38 -05:00
|
|
|
/// the delimiter is `NoDelim`.
|
2022-03-19 00:20:07 -05:00
|
|
|
fn close_tt(&self) -> &TokenTree {
|
|
|
|
let tt = self.all_tts.last().unwrap();
|
|
|
|
debug_assert!(matches!(
|
|
|
|
tt,
|
|
|
|
&TokenTree::Token(token::Token { kind: token::CloseDelim(d), .. }) if d == self.delim
|
|
|
|
));
|
|
|
|
tt
|
2019-09-22 11:17:30 -05:00
|
|
|
}
|
|
|
|
|
2022-03-19 00:20:07 -05:00
|
|
|
/// Returns the tts excluding the outer delimiters.
|
|
|
|
///
|
|
|
|
/// FIXME: #67062 has details about why this is sub-optimal.
|
|
|
|
fn inner_tts(&self) -> &[TokenTree] {
|
|
|
|
// These functions are called for the assertions within them.
|
|
|
|
let _open_tt = self.open_tt();
|
|
|
|
let _close_tt = self.close_tt();
|
|
|
|
&self.all_tts[1..self.all_tts.len() - 1]
|
2019-09-22 11:17:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
#[derive(Clone, PartialEq, Encodable, Decodable, Debug)]
|
2019-09-22 11:42:52 -05:00
|
|
|
struct SequenceRepetition {
|
2019-09-22 11:17:30 -05:00
|
|
|
/// The sequence of token trees
|
2019-09-22 11:42:52 -05:00
|
|
|
tts: Vec<TokenTree>,
|
2019-09-22 11:17:30 -05:00
|
|
|
/// The optional separator
|
2019-09-22 11:42:52 -05:00
|
|
|
separator: Option<Token>,
|
2019-09-22 11:17:30 -05:00
|
|
|
/// Whether the sequence can be repeated zero (*), or one or more times (+)
|
2019-09-22 11:42:52 -05:00
|
|
|
kleene: KleeneToken,
|
2019-09-22 11:17:30 -05:00
|
|
|
/// The number of `Match`s that appear in the sequence (and subsequences)
|
2019-09-22 11:42:52 -05:00
|
|
|
num_captures: usize,
|
2019-09-22 11:17:30 -05:00
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy)]
|
2019-09-22 11:42:52 -05:00
|
|
|
struct KleeneToken {
|
|
|
|
span: Span,
|
|
|
|
op: KleeneOp,
|
2019-09-22 11:17:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl KleeneToken {
|
2019-09-22 11:42:52 -05:00
|
|
|
fn new(op: KleeneOp, span: Span) -> KleeneToken {
|
2019-09-22 11:17:30 -05:00
|
|
|
KleeneToken { span, op }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 05:02:42 -05:00
|
|
|
/// A Kleene-style [repetition operator](https://en.wikipedia.org/wiki/Kleene_star)
|
2019-09-22 11:17:30 -05:00
|
|
|
/// for token sequences.
|
2020-06-11 09:49:57 -05:00
|
|
|
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy)]
|
2019-09-22 11:42:52 -05:00
|
|
|
enum KleeneOp {
|
2019-09-22 11:17:30 -05:00
|
|
|
/// Kleene star (`*`) for zero or more repetitions
|
|
|
|
ZeroOrMore,
|
|
|
|
/// Kleene plus (`+`) for one or more repetitions
|
|
|
|
OneOrMore,
|
2021-04-19 07:57:08 -05:00
|
|
|
/// Kleene optional (`?`) for zero or one repetitions
|
2019-09-22 11:17:30 -05:00
|
|
|
ZeroOrOne,
|
|
|
|
}
|
|
|
|
|
2022-03-19 00:20:07 -05:00
|
|
|
/// Similar to `tokenstream::TokenTree`, except that `Sequence`, `MetaVar`, `MetaVarDecl`, and
|
|
|
|
/// `MetaVarExpr` are "first-class" token trees. Useful for parsing macros.
|
2020-06-11 09:49:57 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Encodable, Decodable)]
|
2019-09-22 11:42:52 -05:00
|
|
|
enum TokenTree {
|
2019-09-22 11:17:30 -05:00
|
|
|
Token(Token),
|
2022-03-19 00:20:07 -05:00
|
|
|
/// A delimited sequence, e.g. `($e:expr)` (RHS) or `{ $e }` (LHS).
|
2019-09-22 11:17:30 -05:00
|
|
|
Delimited(DelimSpan, Lrc<Delimited>),
|
2022-03-19 00:20:07 -05:00
|
|
|
/// A kleene-style repetition sequence, e.g. `$($e:expr)*` (RHS) or `$($e),*` (LHS).
|
2019-09-22 11:17:30 -05:00
|
|
|
Sequence(DelimSpan, Lrc<SequenceRepetition>),
|
2022-03-19 00:20:07 -05:00
|
|
|
/// e.g., `$var`.
|
2020-04-19 06:00:18 -05:00
|
|
|
MetaVar(Span, Ident),
|
2022-03-19 00:20:07 -05:00
|
|
|
/// e.g., `$var:expr`. Only appears on the LHS.
|
2020-12-19 15:30:56 -06:00
|
|
|
MetaVarDecl(Span, Ident /* name to bind */, Option<NonterminalKind>),
|
2022-03-19 00:20:07 -05:00
|
|
|
/// A meta-variable expression inside `${...}`.
|
2022-03-09 13:46:23 -06:00
|
|
|
MetaVarExpr(DelimSpan, MetaVarExpr),
|
2019-09-22 11:17:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TokenTree {
|
|
|
|
/// Returns `true` if the given token tree is delimited.
|
2019-09-22 11:42:52 -05:00
|
|
|
fn is_delimited(&self) -> bool {
|
2020-10-26 19:02:06 -05:00
|
|
|
matches!(*self, TokenTree::Delimited(..))
|
2019-09-22 11:17:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the given token tree is a token of the given kind.
|
2019-09-22 11:42:52 -05:00
|
|
|
fn is_token(&self, expected_kind: &TokenKind) -> bool {
|
2019-09-22 11:17:30 -05:00
|
|
|
match self {
|
|
|
|
TokenTree::Token(Token { kind: actual_kind, .. }) => actual_kind == expected_kind,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the `TokenTree`'s span.
|
2019-09-22 11:42:52 -05:00
|
|
|
fn span(&self) -> Span {
|
2019-09-22 11:17:30 -05:00
|
|
|
match *self {
|
|
|
|
TokenTree::Token(Token { span, .. })
|
|
|
|
| TokenTree::MetaVar(span, _)
|
|
|
|
| TokenTree::MetaVarDecl(span, _, _) => span,
|
2022-03-09 13:46:23 -06:00
|
|
|
TokenTree::Delimited(span, _)
|
|
|
|
| TokenTree::MetaVarExpr(span, _)
|
|
|
|
| TokenTree::Sequence(span, _) => span.entire(),
|
2019-09-22 11:17:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-22 11:42:52 -05:00
|
|
|
fn token(kind: TokenKind, span: Span) -> TokenTree {
|
2019-09-22 11:17:30 -05:00
|
|
|
TokenTree::Token(Token::new(kind, span))
|
|
|
|
}
|
|
|
|
}
|