]> git.proxmox.com Git - rustc.git/blob - src/librustc_expand/mbe.rs
New upstream version 1.42.0+dfsg1
[rustc.git] / src / librustc_expand / mbe.rs
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".
5
6 crate mod macro_check;
7 crate mod macro_parser;
8 crate mod macro_rules;
9 crate mod quoted;
10 crate mod transcribe;
11
12 use syntax::ast;
13 use syntax::token::{self, Token, TokenKind};
14 use syntax::tokenstream::DelimSpan;
15
16 use rustc_span::Span;
17
18 use rustc_data_structures::sync::Lrc;
19
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)]
23 struct Delimited {
24 delim: token::DelimToken,
25 tts: Vec<TokenTree>,
26 }
27
28 impl Delimited {
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)
32 }
33
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)
37 }
38 }
39
40 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
41 struct SequenceRepetition {
42 /// The sequence of token trees
43 tts: Vec<TokenTree>,
44 /// The optional separator
45 separator: Option<Token>,
46 /// Whether the sequence can be repeated zero (*), or one or more times (+)
47 kleene: KleeneToken,
48 /// The number of `Match`s that appear in the sequence (and subsequences)
49 num_captures: usize,
50 }
51
52 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, Copy)]
53 struct KleeneToken {
54 span: Span,
55 op: KleeneOp,
56 }
57
58 impl KleeneToken {
59 fn new(op: KleeneOp, span: Span) -> KleeneToken {
60 KleeneToken { span, op }
61 }
62 }
63
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)]
67 enum KleeneOp {
68 /// Kleene star (`*`) for zero or more repetitions
69 ZeroOrMore,
70 /// Kleene plus (`+`) for one or more repetitions
71 OneOrMore,
72 /// Kleene optional (`?`) for zero or one reptitions
73 ZeroOrOne,
74 }
75
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)]
79 enum TokenTree {
80 Token(Token),
81 Delimited(DelimSpan, Lrc<Delimited>),
82 /// A kleene-style repetition sequence
83 Sequence(DelimSpan, Lrc<SequenceRepetition>),
84 /// e.g., `$var`
85 MetaVar(Span, ast::Ident),
86 /// e.g., `$var:expr`. This is only used in the left hand side of MBE macros.
87 MetaVarDecl(
88 Span,
89 ast::Ident, /* name to bind */
90 ast::Ident, /* kind of nonterminal */
91 ),
92 }
93
94 impl TokenTree {
95 /// Return the number of tokens in the tree.
96 fn len(&self) -> usize {
97 match *self {
98 TokenTree::Delimited(_, ref delimed) => match delimed.delim {
99 token::NoDelim => delimed.tts.len(),
100 _ => delimed.tts.len() + 2,
101 },
102 TokenTree::Sequence(_, ref seq) => seq.tts.len(),
103 _ => 0,
104 }
105 }
106
107 /// Returns `true` if the given token tree is delimited.
108 fn is_delimited(&self) -> bool {
109 match *self {
110 TokenTree::Delimited(..) => true,
111 _ => false,
112 }
113 }
114
115 /// Returns `true` if the given token tree is a token of the given kind.
116 fn is_token(&self, expected_kind: &TokenKind) -> bool {
117 match self {
118 TokenTree::Token(Token { kind: actual_kind, .. }) => actual_kind == expected_kind,
119 _ => false,
120 }
121 }
122
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()
128 }
129 (&TokenTree::Delimited(span, ref delimed), _) => {
130 if index == 0 {
131 return delimed.open_tt(span);
132 }
133 if index == delimed.tts.len() + 1 {
134 return delimed.close_tt(span);
135 }
136 delimed.tts[index - 1].clone()
137 }
138 (&TokenTree::Sequence(_, ref seq), _) => seq.tts[index].clone(),
139 _ => panic!("Cannot expand a token tree"),
140 }
141 }
142
143 /// Retrieves the `TokenTree`'s span.
144 fn span(&self) -> Span {
145 match *self {
146 TokenTree::Token(Token { span, .. })
147 | TokenTree::MetaVar(span, _)
148 | TokenTree::MetaVarDecl(span, _, _) => span,
149 TokenTree::Delimited(span, _) | TokenTree::Sequence(span, _) => span.entire(),
150 }
151 }
152
153 fn token(kind: TokenKind, span: Span) -> TokenTree {
154 TokenTree::Token(Token::new(kind, span))
155 }
156 }