1 //! This module implements declarative macros: old `macro_rules` and the newer
2 //! `macro`. Declarative macros are also known as "macro by example", and that's
3 //! why we call this module `mbe`. For external documentation, prefer the
4 //! official terminology: "declarative macros".
6 pub(crate) mod diagnostics;
7 pub(crate) mod macro_check;
8 pub(crate) mod macro_parser;
9 pub(crate) mod macro_rules;
10 pub(crate) mod metavar_expr;
11 pub(crate) mod quoted;
12 pub(crate) mod transcribe;
14 use metavar_expr::MetaVarExpr;
15 use rustc_ast::token::{Delimiter, NonterminalKind, Token, TokenKind};
16 use rustc_ast::tokenstream::DelimSpan;
17 use rustc_span::symbol::Ident;
20 /// Contains the sub-token-trees of a "delimited" token tree such as `(a b c)`.
21 /// The delimiters are not represented explicitly in the `tts` vector.
22 #[derive(PartialEq, Encodable, Decodable, Debug)]
25 /// FIXME: #67062 has details about why this is sub-optimal.
29 #[derive(PartialEq, Encodable, Decodable, Debug)]
30 struct SequenceRepetition {
31 /// The sequence of token trees
33 /// The optional separator
34 separator: Option<Token>,
35 /// Whether the sequence can be repeated zero (*), or one or more times (+)
37 /// The number of `Match`s that appear in the sequence (and subsequences)
41 #[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy)]
48 fn new(op: KleeneOp, span: Span) -> KleeneToken {
49 KleeneToken { span, op }
53 /// A Kleene-style [repetition operator](https://en.wikipedia.org/wiki/Kleene_star)
54 /// for token sequences.
55 #[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy)]
56 pub(crate) enum KleeneOp {
57 /// Kleene star (`*`) for zero or more repetitions
59 /// Kleene plus (`+`) for one or more repetitions
61 /// Kleene optional (`?`) for zero or one repetitions
65 /// Similar to `tokenstream::TokenTree`, except that `Sequence`, `MetaVar`, `MetaVarDecl`, and
66 /// `MetaVarExpr` are "first-class" token trees. Useful for parsing macros.
67 #[derive(Debug, PartialEq, Encodable, Decodable)]
70 /// A delimited sequence, e.g. `($e:expr)` (RHS) or `{ $e }` (LHS).
71 Delimited(DelimSpan, Delimited),
72 /// A kleene-style repetition sequence, e.g. `$($e:expr)*` (RHS) or `$($e),*` (LHS).
73 Sequence(DelimSpan, SequenceRepetition),
76 /// e.g., `$var:expr`. Only appears on the LHS.
77 MetaVarDecl(Span, Ident /* name to bind */, Option<NonterminalKind>),
78 /// A meta-variable expression inside `${...}`.
79 MetaVarExpr(DelimSpan, MetaVarExpr),
83 /// Returns `true` if the given token tree is delimited.
84 fn is_delimited(&self) -> bool {
85 matches!(*self, TokenTree::Delimited(..))
88 /// Returns `true` if the given token tree is a token of the given kind.
89 fn is_token(&self, expected_kind: &TokenKind) -> bool {
91 TokenTree::Token(Token { kind: actual_kind, .. }) => actual_kind == expected_kind,
96 /// Retrieves the `TokenTree`'s span.
97 fn span(&self) -> Span {
99 TokenTree::Token(Token { span, .. })
100 | TokenTree::MetaVar(span, _)
101 | TokenTree::MetaVarDecl(span, _, _) => span,
102 TokenTree::Delimited(span, _)
103 | TokenTree::MetaVarExpr(span, _)
104 | TokenTree::Sequence(span, _) => span.entire(),
108 fn token(kind: TokenKind, span: Span) -> TokenTree {
109 TokenTree::Token(Token::new(kind, span))