]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_expand/src/mbe/quoted.rs
New upstream version 1.54.0+dfsg1
[rustc.git] / compiler / rustc_expand / src / mbe / quoted.rs
CommitLineData
e74abb32 1use crate::mbe::macro_parser;
dfeec247 2use crate::mbe::{Delimited, KleeneOp, KleeneToken, SequenceRepetition, TokenTree};
9fa01778 3
74b04a01
XL
4use rustc_ast::token::{self, Token};
5use rustc_ast::tokenstream;
b9856134 6use rustc_ast::{NodeId, DUMMY_NODE_ID};
74b04a01 7use rustc_ast_pretty::pprust;
5869c6ff 8use rustc_feature::Features;
cdc7bbd5
XL
9use rustc_session::parse::ParseSess;
10use rustc_span::symbol::{kw, Ident};
8bb4bdeb 11
17df50a5
XL
12use rustc_span::edition::Edition;
13use rustc_span::{Span, SyntaxContext};
416331ca 14
e74abb32 15use rustc_data_structures::sync::Lrc;
8bb4bdeb 16
3dfed10e
XL
17const VALID_FRAGMENT_NAMES_MSG: &str = "valid fragment specifiers are \
18 `ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, \
19 `literal`, `path`, `meta`, `tt`, `item` and `vis`";
20
2c00a5a8
XL
21/// Takes a `tokenstream::TokenStream` and returns a `Vec<self::TokenTree>`. Specifically, this
22/// takes a generic `TokenStream`, such as is used in the rest of the compiler, and returns a
23/// collection of `TokenTree` for use in parsing a macro.
24///
25/// # Parameters
26///
27/// - `input`: a token stream to read from, the contents of which we are parsing.
28/// - `expect_matchers`: `parse` can be used to parse either the "patterns" or the "body" of a
29/// macro. Both take roughly the same form _except_ that in a pattern, metavars are declared with
30/// their "matcher" type. For example `$var:expr` or `$id:ident`. In this example, `expr` and
31/// `ident` are "matchers". They are not present in the body of a macro rule -- just in the
32/// pattern, so we pass a parameter to indicate whether to expect them or not.
33/// - `sess`: the parsing session. Any errors will be emitted to this session.
5869c6ff
XL
34/// - `node_id`: the NodeId of the macro we are parsing.
35/// - `features`: language features so we can do feature gating.
17df50a5 36/// - `edition`: the edition of the crate defining the macro
2c00a5a8
XL
37///
38/// # Returns
39///
40/// A collection of `self::TokenTree`. There may also be some errors emitted to `sess`.
e74abb32 41pub(super) fn parse(
2c00a5a8
XL
42 input: tokenstream::TokenStream,
43 expect_matchers: bool,
44 sess: &ParseSess,
f035d41b 45 node_id: NodeId,
5869c6ff 46 features: &Features,
17df50a5 47 edition: Edition,
2c00a5a8
XL
48) -> Vec<TokenTree> {
49 // Will contain the final collection of `self::TokenTree`
8bb4bdeb 50 let mut result = Vec::new();
2c00a5a8
XL
51
52 // For each token tree in `input`, parse the token into a `self::TokenTree`, consuming
53 // additional trees if need be.
e74abb32 54 let mut trees = input.trees();
8bb4bdeb 55 while let Some(tree) = trees.next() {
2c00a5a8 56 // Given the parsed tree, if there is a metavar and we are expecting matchers, actually
0731742a 57 // parse out the matcher (i.e., in `$id:ident` this would parse the `:` and `ident`).
17df50a5 58 let tree = parse_tree(tree, &mut trees, expect_matchers, sess, node_id, features, edition);
8bb4bdeb 59 match tree {
041b39d2 60 TokenTree::MetaVar(start_sp, ident) if expect_matchers => {
8bb4bdeb 61 let span = match trees.next() {
dc9dc135
XL
62 Some(tokenstream::TokenTree::Token(Token { kind: token::Colon, span })) => {
63 match trees.next() {
64 Some(tokenstream::TokenTree::Token(token)) => match token.ident() {
3dfed10e 65 Some((frag, _)) => {
dc9dc135 66 let span = token.span.with_lo(start_sp.lo());
5869c6ff 67
5869c6ff
XL
68 let kind =
69 token::NonterminalKind::from_symbol(frag.name, || {
17df50a5
XL
70 // FIXME(#85708) - once we properly decode a foreign
71 // crate's `SyntaxContext::root`, then we can replace
72 // this with just `span.edition()`. A
73 // `SyntaxContext::root()` from the current crate will
74 // have the edition of the current crate, and a
75 // `SyntaxxContext::root()` from a foreign crate will
76 // have the edition of that crate (which we manually
77 // retrieve via the `edition` parameter).
78 if span.ctxt() == SyntaxContext::root() {
79 edition
80 } else {
81 span.edition()
82 }
5869c6ff
XL
83 })
84 .unwrap_or_else(
85 || {
86 let msg = format!(
87 "invalid fragment specifier `{}`",
88 frag.name
89 );
90 sess.span_diagnostic
91 .struct_span_err(span, &msg)
92 .help(VALID_FRAGMENT_NAMES_MSG)
93 .emit();
94 token::NonterminalKind::Ident
95 },
96 );
b9856134 97 result.push(TokenTree::MetaVarDecl(span, ident, Some(kind)));
dc9dc135
XL
98 continue;
99 }
100 _ => token.span,
101 },
5869c6ff 102 tree => tree.as_ref().map_or(span, tokenstream::TokenTree::span),
dc9dc135
XL
103 }
104 }
5869c6ff 105 tree => tree.as_ref().map_or(start_sp, tokenstream::TokenTree::span),
8bb4bdeb 106 };
b9856134
XL
107 if node_id != DUMMY_NODE_ID {
108 // Macros loaded from other crates have dummy node ids.
109 sess.missing_fragment_specifiers.borrow_mut().insert(span, node_id);
110 }
111 result.push(TokenTree::MetaVarDecl(span, ident, None));
8bb4bdeb 112 }
2c00a5a8
XL
113
114 // Not a metavar or no matchers allowed, so just return the tree
8bb4bdeb
XL
115 _ => result.push(tree),
116 }
117 }
118 result
119}
120
2c00a5a8
XL
121/// Takes a `tokenstream::TokenTree` and returns a `self::TokenTree`. Specifically, this takes a
122/// generic `TokenTree`, such as is used in the rest of the compiler, and returns a `TokenTree`
123/// for use in parsing a macro.
124///
125/// Converting the given tree may involve reading more tokens.
126///
127/// # Parameters
128///
129/// - `tree`: the tree we wish to convert.
f035d41b 130/// - `outer_trees`: an iterator over trees. We may need to read more tokens from it in order to finish
2c00a5a8
XL
131/// converting `tree`
132/// - `expect_matchers`: same as for `parse` (see above).
133/// - `sess`: the parsing session. Any errors will be emitted to this session.
5869c6ff 134/// - `features`: language features so we can do feature gating.
17df50a5 135/// - `edition` - the edition of the crate defining the macro
dc9dc135 136fn parse_tree(
2c00a5a8 137 tree: tokenstream::TokenTree,
f035d41b 138 outer_trees: &mut impl Iterator<Item = tokenstream::TokenTree>,
2c00a5a8
XL
139 expect_matchers: bool,
140 sess: &ParseSess,
f035d41b 141 node_id: NodeId,
5869c6ff 142 features: &Features,
17df50a5 143 edition: Edition,
dc9dc135 144) -> TokenTree {
2c00a5a8 145 // Depending on what `tree` is, we could be parsing different parts of a macro
8bb4bdeb 146 match tree {
2c00a5a8 147 // `tree` is a `$` token. Look at the next token in `trees`
f035d41b
XL
148 tokenstream::TokenTree::Token(Token { kind: token::Dollar, span }) => {
149 // FIXME: Handle `None`-delimited groups in a more systematic way
150 // during parsing.
151 let mut next = outer_trees.next();
152 let mut trees: Box<dyn Iterator<Item = tokenstream::TokenTree>>;
153 if let Some(tokenstream::TokenTree::Delimited(_, token::NoDelim, tts)) = next {
154 trees = Box::new(tts.into_trees());
155 next = trees.next();
156 } else {
157 trees = Box::new(outer_trees);
8bb4bdeb 158 }
2c00a5a8 159
f035d41b
XL
160 match next {
161 // `tree` is followed by a delimited set of token trees. This indicates the beginning
162 // of a repetition sequence in the macro (e.g. `$(pat)*`).
163 Some(tokenstream::TokenTree::Delimited(span, delim, tts)) => {
164 // Must have `(` not `{` or `[`
165 if delim != token::Paren {
166 let tok = pprust::token_kind_to_string(&token::OpenDelim(delim));
167 let msg = format!("expected `(`, found `{}`", tok);
168 sess.span_diagnostic.span_err(span.entire(), &msg);
169 }
170 // Parse the contents of the sequence itself
17df50a5 171 let sequence = parse(tts, expect_matchers, sess, node_id, features, edition);
f035d41b
XL
172 // Get the Kleene operator and optional separator
173 let (separator, kleene) =
174 parse_sep_and_kleene_op(&mut trees, span.entire(), sess);
175 // Count the number of captured "names" (i.e., named metavars)
176 let name_captures = macro_parser::count_names(&sequence);
177 TokenTree::Sequence(
178 span,
179 Lrc::new(SequenceRepetition {
180 tts: sequence,
181 separator,
182 kleene,
183 num_captures: name_captures,
184 }),
185 )
8bb4bdeb 186 }
2c00a5a8 187
f035d41b
XL
188 // `tree` is followed by an `ident`. This could be `$meta_var` or the `$crate` special
189 // metavariable that names the crate of the invocation.
190 Some(tokenstream::TokenTree::Token(token)) if token.is_ident() => {
191 let (ident, is_raw) = token.ident().unwrap();
192 let span = ident.span.with_lo(span.lo());
193 if ident.name == kw::Crate && !is_raw {
194 TokenTree::token(token::Ident(kw::DollarCrate, is_raw), span)
195 } else {
196 TokenTree::MetaVar(span, ident)
197 }
198 }
199
200 // `tree` is followed by a random token. This is an error.
201 Some(tokenstream::TokenTree::Token(token)) => {
202 let msg = format!(
203 "expected identifier, found `{}`",
204 pprust::token_to_string(&token),
205 );
206 sess.span_diagnostic.span_err(token.span, &msg);
207 TokenTree::MetaVar(token.span, Ident::invalid())
208 }
2c00a5a8 209
f035d41b
XL
210 // There are no more tokens. Just return the `$` we already have.
211 None => TokenTree::token(token::Dollar, span),
212 }
213 }
2c00a5a8
XL
214
215 // `tree` is an arbitrary token. Keep it.
dc9dc135 216 tokenstream::TokenTree::Token(token) => TokenTree::Token(token),
2c00a5a8 217
0731742a 218 // `tree` is the beginning of a delimited set of tokens (e.g., `(` or `{`). We need to
2c00a5a8 219 // descend into the delimited set and further parse it.
0731742a 220 tokenstream::TokenTree::Delimited(span, delim, tts) => TokenTree::Delimited(
2c00a5a8 221 span,
5869c6ff
XL
222 Lrc::new(Delimited {
223 delim,
17df50a5 224 tts: parse(tts, expect_matchers, sess, node_id, features, edition),
5869c6ff 225 }),
2c00a5a8 226 ),
8bb4bdeb
XL
227 }
228}
229
2c00a5a8
XL
230/// Takes a token and returns `Some(KleeneOp)` if the token is `+` `*` or `?`. Otherwise, return
231/// `None`.
dc9dc135
XL
232fn kleene_op(token: &Token) -> Option<KleeneOp> {
233 match token.kind {
2c00a5a8
XL
234 token::BinOp(token::Star) => Some(KleeneOp::ZeroOrMore),
235 token::BinOp(token::Plus) => Some(KleeneOp::OneOrMore),
236 token::Question => Some(KleeneOp::ZeroOrOne),
237 _ => None,
8bb4bdeb 238 }
2c00a5a8 239}
8bb4bdeb 240
2c00a5a8
XL
241/// Parse the next token tree of the input looking for a KleeneOp. Returns
242///
8faf50e0 243/// - Ok(Ok((op, span))) if the next token tree is a KleeneOp
2c00a5a8
XL
244/// - Ok(Err(tok, span)) if the next token tree is a token but not a KleeneOp
245/// - Err(span) if the next token tree is not a token
dc9dc135
XL
246fn parse_kleene_op(
247 input: &mut impl Iterator<Item = tokenstream::TokenTree>,
2c00a5a8 248 span: Span,
dc9dc135 249) -> Result<Result<(KleeneOp, Span), Token>, Span> {
2c00a5a8 250 match input.next() {
dc9dc135
XL
251 Some(tokenstream::TokenTree::Token(token)) => match kleene_op(&token) {
252 Some(op) => Ok(Ok((op, token.span))),
253 None => Ok(Err(token)),
2c00a5a8 254 },
5869c6ff 255 tree => Err(tree.as_ref().map_or(span, tokenstream::TokenTree::span)),
2c00a5a8
XL
256 }
257}
258
259/// Attempt to parse a single Kleene star, possibly with a separator.
260///
261/// For example, in a pattern such as `$(a),*`, `a` is the pattern to be repeated, `,` is the
262/// separator, and `*` is the Kleene operator. This function is specifically concerned with parsing
263/// the last two tokens of such a pattern: namely, the optional separator and the Kleene operator
264/// itself. Note that here we are parsing the _macro_ itself, rather than trying to match some
265/// stream of tokens in an invocation of a macro.
266///
267/// This function will take some input iterator `input` corresponding to `span` and a parsing
268/// session `sess`. If the next one (or possibly two) tokens in `input` correspond to a Kleene
269/// operator and separator, then a tuple with `(separator, KleeneOp)` is returned. Otherwise, an
270/// error with the appropriate span is emitted to `sess` and a dummy value is returned.
dc9dc135 271fn parse_sep_and_kleene_op(
e74abb32 272 input: &mut impl Iterator<Item = tokenstream::TokenTree>,
8faf50e0
XL
273 span: Span,
274 sess: &ParseSess,
416331ca 275) -> (Option<Token>, KleeneToken) {
8faf50e0
XL
276 // We basically look at two token trees here, denoted as #1 and #2 below
277 let span = match parse_kleene_op(input, span) {
dc9dc135 278 // #1 is a `?`, `+`, or `*` KleeneOp
416331ca 279 Ok(Ok((op, span))) => return (None, KleeneToken::new(op, span)),
8faf50e0 280
2c00a5a8 281 // #1 is a separator followed by #2, a KleeneOp
dc9dc135 282 Ok(Err(token)) => match parse_kleene_op(input, token.span) {
8faf50e0 283 // #2 is the `?` Kleene op, which does not take a separator (error)
416331ca 284 Ok(Ok((KleeneOp::ZeroOrOne, span))) => {
8faf50e0 285 // Error!
a1dfa0c6 286 sess.span_diagnostic.span_err(
dc9dc135 287 token.span,
a1dfa0c6
XL
288 "the `?` macro repetition operator does not take a separator",
289 );
8faf50e0
XL
290
291 // Return a dummy
416331ca 292 return (None, KleeneToken::new(KleeneOp::ZeroOrMore, span));
2c00a5a8 293 }
8faf50e0
XL
294
295 // #2 is a KleeneOp :D
416331ca 296 Ok(Ok((op, span))) => return (Some(token), KleeneToken::new(op, span)),
2c00a5a8 297
dc9dc135
XL
298 // #2 is a random token or not a token at all :(
299 Ok(Err(Token { span, .. })) | Err(span) => span,
8bb4bdeb 300 },
2c00a5a8
XL
301
302 // #1 is not a token
303 Err(span) => span,
8bb4bdeb
XL
304 };
305
8faf50e0 306 // If we ever get to this point, we have experienced an "unexpected token" error
dc9dc135 307 sess.span_diagnostic.span_err(span, "expected one of: `*`, `+`, or `?`");
8faf50e0
XL
308
309 // Return a dummy
416331ca 310 (None, KleeneToken::new(KleeneOp::ZeroOrMore, span))
8bb4bdeb 311}