]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_expand/src/mbe.rs
New upstream version 1.49.0+dfsg1
[rustc.git] / compiler / rustc_expand / src / 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 rustc_ast::token::{self, NonterminalKind, Token, TokenKind};
13 use rustc_ast::tokenstream::DelimSpan;
14
15 use rustc_span::symbol::Ident;
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, Encodable, Decodable, 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, Encodable, Decodable, 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, Encodable, Decodable, 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](https://en.wikipedia.org/wiki/Kleene_star)
65 /// for token sequences.
66 #[derive(Clone, PartialEq, Encodable, Decodable, 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, Encodable, Decodable)]
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, Ident),
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>),
88 }
89
90 impl TokenTree {
91 /// Return the number of tokens in the tree.
92 fn len(&self) -> usize {
93 match *self {
94 TokenTree::Delimited(_, ref delimed) => match delimed.delim {
95 token::NoDelim => delimed.tts.len(),
96 _ => delimed.tts.len() + 2,
97 },
98 TokenTree::Sequence(_, ref seq) => seq.tts.len(),
99 _ => 0,
100 }
101 }
102
103 /// Returns `true` if the given token tree is delimited.
104 fn is_delimited(&self) -> bool {
105 matches!(*self, TokenTree::Delimited(..))
106 }
107
108 /// Returns `true` if the given token tree is a token of the given kind.
109 fn is_token(&self, expected_kind: &TokenKind) -> bool {
110 match self {
111 TokenTree::Token(Token { kind: actual_kind, .. }) => actual_kind == expected_kind,
112 _ => false,
113 }
114 }
115
116 /// Gets the `index`-th sub-token-tree. This only makes sense for delimited trees and sequences.
117 fn get_tt(&self, index: usize) -> TokenTree {
118 match (self, index) {
119 (&TokenTree::Delimited(_, ref delimed), _) if delimed.delim == token::NoDelim => {
120 delimed.tts[index].clone()
121 }
122 (&TokenTree::Delimited(span, ref delimed), _) => {
123 if index == 0 {
124 return delimed.open_tt(span);
125 }
126 if index == delimed.tts.len() + 1 {
127 return delimed.close_tt(span);
128 }
129 delimed.tts[index - 1].clone()
130 }
131 (&TokenTree::Sequence(_, ref seq), _) => seq.tts[index].clone(),
132 _ => panic!("Cannot expand a token tree"),
133 }
134 }
135
136 /// Retrieves the `TokenTree`'s span.
137 fn span(&self) -> Span {
138 match *self {
139 TokenTree::Token(Token { span, .. })
140 | TokenTree::MetaVar(span, _)
141 | TokenTree::MetaVarDecl(span, _, _) => span,
142 TokenTree::Delimited(span, _) | TokenTree::Sequence(span, _) => span.entire(),
143 }
144 }
145
146 fn token(kind: TokenKind, span: Span) -> TokenTree {
147 TokenTree::Token(Token::new(kind, span))
148 }
149 }