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 macro_check;
7 pub(crate) mod macro_parser;
8 pub(crate) mod macro_rules;
9 pub(crate) mod metavar_expr;
10 pub(crate) mod quoted;
11 pub(crate) mod transcribe;
13 use metavar_expr::MetaVarExpr;
14 use rustc_ast::token::{Delimiter, NonterminalKind, Token, TokenKind};
15 use rustc_ast::tokenstream::DelimSpan;
16 use rustc_span::symbol::Ident;
19 /// Contains the sub-token-trees of a "delimited" token tree such as `(a b c)`.
20 /// The delimiters are not represented explicitly in the `tts` vector.
21 #[derive(PartialEq, Encodable, Decodable, Debug)]
24 /// FIXME: #67062 has details about why this is sub-optimal.
28 #[derive(PartialEq, Encodable, Decodable, Debug)]
29 struct SequenceRepetition {
30 /// The sequence of token trees
32 /// The optional separator
33 separator: Option<Token>,
34 /// Whether the sequence can be repeated zero (*), or one or more times (+)
36 /// The number of `Match`s that appear in the sequence (and subsequences)
40 #[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy)]
47 fn new(op: KleeneOp, span: Span) -> KleeneToken {
48 KleeneToken { span, op }
52 /// A Kleene-style [repetition operator](https://en.wikipedia.org/wiki/Kleene_star)
53 /// for token sequences.
54 #[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy)]
56 /// Kleene star (`*`) for zero or more repetitions
58 /// Kleene plus (`+`) for one or more repetitions
60 /// Kleene optional (`?`) for zero or one repetitions
64 /// Similar to `tokenstream::TokenTree`, except that `Sequence`, `MetaVar`, `MetaVarDecl`, and
65 /// `MetaVarExpr` are "first-class" token trees. Useful for parsing macros.
66 #[derive(Debug, PartialEq, Encodable, Decodable)]
69 /// A delimited sequence, e.g. `($e:expr)` (RHS) or `{ $e }` (LHS).
70 Delimited(DelimSpan, Delimited),
71 /// A kleene-style repetition sequence, e.g. `$($e:expr)*` (RHS) or `$($e),*` (LHS).
72 Sequence(DelimSpan, SequenceRepetition),
75 /// e.g., `$var:expr`. Only appears on the LHS.
76 MetaVarDecl(Span, Ident /* name to bind */, Option<NonterminalKind>),
77 /// A meta-variable expression inside `${...}`.
78 MetaVarExpr(DelimSpan, MetaVarExpr),
82 /// Returns `true` if the given token tree is delimited.
83 fn is_delimited(&self) -> bool {
84 matches!(*self, TokenTree::Delimited(..))
87 /// Returns `true` if the given token tree is a token of the given kind.
88 fn is_token(&self, expected_kind: &TokenKind) -> bool {
90 TokenTree::Token(Token { kind: actual_kind, .. }) => actual_kind == expected_kind,
95 /// Retrieves the `TokenTree`'s span.
96 fn span(&self) -> Span {
98 TokenTree::Token(Token { span, .. })
99 | TokenTree::MetaVar(span, _)
100 | TokenTree::MetaVarDecl(span, _, _) => span,
101 TokenTree::Delimited(span, _)
102 | TokenTree::MetaVarExpr(span, _)
103 | TokenTree::Sequence(span, _) => span.entire(),
107 fn token(kind: TokenKind, span: Span) -> TokenTree {
108 TokenTree::Token(Token::new(kind, span))