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
;
13 use syntax
::token
::{self, Token, TokenKind}
;
14 use syntax
::tokenstream
::DelimSpan
;
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, RustcEncodable, RustcDecodable, 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, RustcEncodable, RustcDecodable, 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, RustcEncodable, RustcDecodable, Debug, Copy)]
59 fn new(op
: KleeneOp
, span
: Span
) -> KleeneToken
{
60 KleeneToken { span, op }
64 /// A Kleene-style [repetition operator](http://en.wikipedia.org/wiki/Kleene_star)
65 /// for token sequences.
66 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, 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, RustcEncodable, RustcDecodable)]
81 Delimited(DelimSpan
, Lrc
<Delimited
>),
82 /// A kleene-style repetition sequence
83 Sequence(DelimSpan
, Lrc
<SequenceRepetition
>),
85 MetaVar(Span
, ast
::Ident
),
86 /// e.g., `$var:expr`. This is only used in the left hand side of MBE macros.
89 ast
::Ident
, /* name to bind */
90 ast
::Ident
, /* kind of nonterminal */
95 /// Return the number of tokens in the tree.
96 fn len(&self) -> usize {
98 TokenTree
::Delimited(_
, ref delimed
) => match delimed
.delim
{
99 token
::NoDelim
=> delimed
.tts
.len(),
100 _
=> delimed
.tts
.len() + 2,
102 TokenTree
::Sequence(_
, ref seq
) => seq
.tts
.len(),
107 /// Returns `true` if the given token tree is delimited.
108 fn is_delimited(&self) -> bool
{
110 TokenTree
::Delimited(..) => true,
115 /// Returns `true` if the given token tree is a token of the given kind.
116 fn is_token(&self, expected_kind
: &TokenKind
) -> bool
{
118 TokenTree
::Token(Token { kind: actual_kind, .. }
) => actual_kind
== expected_kind
,
123 /// Gets the `index`-th sub-token-tree. This only makes sense for delimited trees and sequences.
124 fn get_tt(&self, index
: usize) -> TokenTree
{
125 match (self, index
) {
126 (&TokenTree
::Delimited(_
, ref delimed
), _
) if delimed
.delim
== token
::NoDelim
=> {
127 delimed
.tts
[index
].clone()
129 (&TokenTree
::Delimited(span
, ref delimed
), _
) => {
131 return delimed
.open_tt(span
);
133 if index
== delimed
.tts
.len() + 1 {
134 return delimed
.close_tt(span
);
136 delimed
.tts
[index
- 1].clone()
138 (&TokenTree
::Sequence(_
, ref seq
), _
) => seq
.tts
[index
].clone(),
139 _
=> panic
!("Cannot expand a token tree"),
143 /// Retrieves the `TokenTree`'s span.
144 fn span(&self) -> Span
{
146 TokenTree
::Token(Token { span, .. }
)
147 | TokenTree
::MetaVar(span
, _
)
148 | TokenTree
::MetaVarDecl(span
, _
, _
) => span
,
149 TokenTree
::Delimited(span
, _
) | TokenTree
::Sequence(span
, _
) => span
.entire(),
153 fn token(kind
: TokenKind
, span
: Span
) -> TokenTree
{
154 TokenTree
::Token(Token
::new(kind
, span
))