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".
8 crate mod macro_parser;
13 use syntax::parse::token::{self, Token, TokenKind};
14 use syntax::tokenstream::{DelimSpan};
16 use syntax_pos::{BytePos, Span};
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: Span) -> TokenTree {
31 let open_span = if span.is_dummy() {
34 span.with_hi(span.lo() + BytePos(self.delim.len() as u32))
36 TokenTree::token(token::OpenDelim(self.delim), open_span)
39 /// Returns a `self::TokenTree` with a `Span` corresponding to the closing delimiter.
40 fn close_tt(&self, span: Span) -> TokenTree {
41 let close_span = if span.is_dummy() {
44 span.with_lo(span.hi() - BytePos(self.delim.len() as u32))
46 TokenTree::token(token::CloseDelim(self.delim), close_span)
50 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
51 struct SequenceRepetition {
52 /// The sequence of token trees
54 /// The optional separator
55 separator: Option<Token>,
56 /// Whether the sequence can be repeated zero (*), or one or more times (+)
58 /// The number of `Match`s that appear in the sequence (and subsequences)
62 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, Copy)]
69 fn new(op: KleeneOp, span: Span) -> KleeneToken {
70 KleeneToken { span, op }
74 /// A Kleene-style [repetition operator](http://en.wikipedia.org/wiki/Kleene_star)
75 /// for token sequences.
76 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, Copy)]
78 /// Kleene star (`*`) for zero or more repetitions
80 /// Kleene plus (`+`) for one or more repetitions
82 /// Kleene optional (`?`) for zero or one reptitions
86 /// Similar to `tokenstream::TokenTree`, except that `$i`, `$i:ident`, and `$(...)`
87 /// are "first-class" token trees. Useful for parsing macros.
88 #[derive(Debug, Clone, PartialEq, RustcEncodable, RustcDecodable)]
91 Delimited(DelimSpan, Lrc<Delimited>),
92 /// A kleene-style repetition sequence
93 Sequence(DelimSpan, Lrc<SequenceRepetition>),
95 MetaVar(Span, ast::Ident),
96 /// e.g., `$var:expr`. This is only used in the left hand side of MBE macros.
99 ast::Ident, /* name to bind */
100 ast::Ident, /* kind of nonterminal */
105 /// Return the number of tokens in the tree.
106 fn len(&self) -> usize {
108 TokenTree::Delimited(_, ref delimed) => match delimed.delim {
109 token::NoDelim => delimed.tts.len(),
110 _ => delimed.tts.len() + 2,
112 TokenTree::Sequence(_, ref seq) => seq.tts.len(),
117 /// Returns `true` if the given token tree is delimited.
118 fn is_delimited(&self) -> bool {
120 TokenTree::Delimited(..) => true,
125 /// Returns `true` if the given token tree is a token of the given kind.
126 fn is_token(&self, expected_kind: &TokenKind) -> bool {
128 TokenTree::Token(Token { kind: actual_kind, .. }) => actual_kind == expected_kind,
133 /// Gets the `index`-th sub-token-tree. This only makes sense for delimited trees and sequences.
134 fn get_tt(&self, index: usize) -> TokenTree {
135 match (self, index) {
136 (&TokenTree::Delimited(_, ref delimed), _) if delimed.delim == token::NoDelim => {
137 delimed.tts[index].clone()
139 (&TokenTree::Delimited(span, ref delimed), _) => {
141 return delimed.open_tt(span.open);
143 if index == delimed.tts.len() + 1 {
144 return delimed.close_tt(span.close);
146 delimed.tts[index - 1].clone()
148 (&TokenTree::Sequence(_, ref seq), _) => seq.tts[index].clone(),
149 _ => panic!("Cannot expand a token tree"),
153 /// Retrieves the `TokenTree`'s span.
154 fn span(&self) -> Span {
156 TokenTree::Token(Token { span, .. })
157 | TokenTree::MetaVar(span, _)
158 | TokenTree::MetaVarDecl(span, _, _) => span,
159 TokenTree::Delimited(span, _) | TokenTree::Sequence(span, _) => span.entire(),
163 fn token(kind: TokenKind, span: Span) -> TokenTree {
164 TokenTree::Token(Token::new(kind, span))