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".
7 crate mod macro_parser
;
12 use rustc_ast
::token
::{self, NonterminalKind, Token, TokenKind}
;
13 use rustc_ast
::tokenstream
::DelimSpan
;
15 use rustc_span
::symbol
::Ident
;
18 use rustc_data_structures
::sync
::Lrc
;
20 /// Contains the sub-token-trees of a "delimited" token tree, such as the contents of `(`. Note
21 /// that the delimiter itself might be `NoDelim`.
22 #[derive(Clone, PartialEq, Encodable, Decodable, Debug)]
24 delim
: token
::DelimToken
,
29 /// Returns a `self::TokenTree` with a `Span` corresponding to the opening delimiter.
30 fn open_tt(&self, span
: DelimSpan
) -> TokenTree
{
31 TokenTree
::token(token
::OpenDelim(self.delim
), span
.open
)
34 /// Returns a `self::TokenTree` with a `Span` corresponding to the closing delimiter.
35 fn close_tt(&self, span
: DelimSpan
) -> TokenTree
{
36 TokenTree
::token(token
::CloseDelim(self.delim
), span
.close
)
40 #[derive(Clone, PartialEq, Encodable, Decodable, Debug)]
41 struct SequenceRepetition
{
42 /// The sequence of token trees
44 /// The optional separator
45 separator
: Option
<Token
>,
46 /// Whether the sequence can be repeated zero (*), or one or more times (+)
48 /// The number of `Match`s that appear in the sequence (and subsequences)
52 #[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy)]
59 fn new(op
: KleeneOp
, span
: Span
) -> KleeneToken
{
60 KleeneToken { span, op }
64 /// A Kleene-style [repetition operator](https://en.wikipedia.org/wiki/Kleene_star)
65 /// for token sequences.
66 #[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy)]
68 /// Kleene star (`*`) for zero or more repetitions
70 /// Kleene plus (`+`) for one or more repetitions
72 /// Kleene optional (`?`) for zero or one reptitions
76 /// Similar to `tokenstream::TokenTree`, except that `$i`, `$i:ident`, and `$(...)`
77 /// are "first-class" token trees. Useful for parsing macros.
78 #[derive(Debug, Clone, PartialEq, Encodable, Decodable)]
81 Delimited(DelimSpan
, Lrc
<Delimited
>),
82 /// A kleene-style repetition sequence
83 Sequence(DelimSpan
, Lrc
<SequenceRepetition
>),
86 /// e.g., `$var:expr`. This is only used in the left hand side of MBE macros.
87 MetaVarDecl(Span
, Ident
/* name to bind */, Option
<NonterminalKind
>),
91 /// Return the number of tokens in the tree.
92 fn len(&self) -> usize {
94 TokenTree
::Delimited(_
, ref delimed
) => match delimed
.delim
{
95 token
::NoDelim
=> delimed
.tts
.len(),
96 _
=> delimed
.tts
.len() + 2,
98 TokenTree
::Sequence(_
, ref seq
) => seq
.tts
.len(),
103 /// Returns `true` if the given token tree is delimited.
104 fn is_delimited(&self) -> bool
{
106 TokenTree
::Delimited(..) => true,
111 /// Returns `true` if the given token tree is a token of the given kind.
112 fn is_token(&self, expected_kind
: &TokenKind
) -> bool
{
114 TokenTree
::Token(Token { kind: actual_kind, .. }
) => actual_kind
== expected_kind
,
119 /// Gets the `index`-th sub-token-tree. This only makes sense for delimited trees and sequences.
120 fn get_tt(&self, index
: usize) -> TokenTree
{
121 match (self, index
) {
122 (&TokenTree
::Delimited(_
, ref delimed
), _
) if delimed
.delim
== token
::NoDelim
=> {
123 delimed
.tts
[index
].clone()
125 (&TokenTree
::Delimited(span
, ref delimed
), _
) => {
127 return delimed
.open_tt(span
);
129 if index
== delimed
.tts
.len() + 1 {
130 return delimed
.close_tt(span
);
132 delimed
.tts
[index
- 1].clone()
134 (&TokenTree
::Sequence(_
, ref seq
), _
) => seq
.tts
[index
].clone(),
135 _
=> panic
!("Cannot expand a token tree"),
139 /// Retrieves the `TokenTree`'s span.
140 fn span(&self) -> Span
{
142 TokenTree
::Token(Token { span, .. }
)
143 | TokenTree
::MetaVar(span
, _
)
144 | TokenTree
::MetaVarDecl(span
, _
, _
) => span
,
145 TokenTree
::Delimited(span
, _
) | TokenTree
::Sequence(span
, _
) => span
.entire(),
149 fn token(kind
: TokenKind
, span
: Span
) -> TokenTree
{
150 TokenTree
::Token(Token
::new(kind
, span
))