]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/tt/quoted.rs
Rollup merge of #58802 - nnethercote:inline-record_layout, r=oli-obk
[rust.git] / src / libsyntax / ext / tt / quoted.rs
1 use crate::ast::NodeId;
2 use crate::early_buffered_lints::BufferedEarlyLintId;
3 use crate::ext::tt::macro_parser;
4 use crate::feature_gate::Features;
5 use crate::parse::{token, ParseSess};
6 use crate::print::pprust;
7 use crate::tokenstream::{self, DelimSpan};
8 use crate::ast;
9 use crate::symbol::keywords;
10
11 use syntax_pos::{edition::Edition, BytePos, Span};
12
13 use rustc_data_structures::sync::Lrc;
14 use std::iter::Peekable;
15
16 /// Contains the sub-token-trees of a "delimited" token tree, such as the contents of `(`. Note
17 /// that the delimiter itself might be `NoDelim`.
18 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
19 pub struct Delimited {
20     pub delim: token::DelimToken,
21     pub tts: Vec<TokenTree>,
22 }
23
24 impl Delimited {
25     /// Returns the opening delimiter (possibly `NoDelim`).
26     pub fn open_token(&self) -> token::Token {
27         token::OpenDelim(self.delim)
28     }
29
30     /// Returns the closing delimiter (possibly `NoDelim`).
31     pub fn close_token(&self) -> token::Token {
32         token::CloseDelim(self.delim)
33     }
34
35     /// Returns a `self::TokenTree` with a `Span` corresponding to the opening delimiter.
36     pub fn open_tt(&self, span: Span) -> TokenTree {
37         let open_span = if span.is_dummy() {
38             span
39         } else {
40             span.with_lo(span.lo() + BytePos(self.delim.len() as u32))
41         };
42         TokenTree::Token(open_span, self.open_token())
43     }
44
45     /// Returns a `self::TokenTree` with a `Span` corresponding to the closing delimiter.
46     pub fn close_tt(&self, span: Span) -> TokenTree {
47         let close_span = if span.is_dummy() {
48             span
49         } else {
50             span.with_lo(span.hi() - BytePos(self.delim.len() as u32))
51         };
52         TokenTree::Token(close_span, self.close_token())
53     }
54 }
55
56 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
57 pub struct SequenceRepetition {
58     /// The sequence of token trees
59     pub tts: Vec<TokenTree>,
60     /// The optional separator
61     pub separator: Option<token::Token>,
62     /// Whether the sequence can be repeated zero (*), or one or more times (+)
63     pub op: KleeneOp,
64     /// The number of `Match`s that appear in the sequence (and subsequences)
65     pub num_captures: usize,
66 }
67
68 /// A Kleene-style [repetition operator](http://en.wikipedia.org/wiki/Kleene_star)
69 /// for token sequences.
70 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
71 pub enum KleeneOp {
72     /// Kleene star (`*`) for zero or more repetitions
73     ZeroOrMore,
74     /// Kleene plus (`+`) for one or more repetitions
75     OneOrMore,
76     ZeroOrOne,
77 }
78
79 /// Similar to `tokenstream::TokenTree`, except that `$i`, `$i:ident`, and `$(...)`
80 /// are "first-class" token trees. Useful for parsing macros.
81 #[derive(Debug, Clone, PartialEq, RustcEncodable, RustcDecodable)]
82 pub enum TokenTree {
83     Token(Span, token::Token),
84     Delimited(DelimSpan, Lrc<Delimited>),
85     /// A kleene-style repetition sequence
86     Sequence(DelimSpan, Lrc<SequenceRepetition>),
87     /// e.g., `$var`
88     MetaVar(Span, ast::Ident),
89     /// e.g., `$var:expr`. This is only used in the left hand side of MBE macros.
90     MetaVarDecl(
91         Span,
92         ast::Ident, /* name to bind */
93         ast::Ident, /* kind of nonterminal */
94     ),
95 }
96
97 impl TokenTree {
98     /// Return the number of tokens in the tree.
99     pub fn len(&self) -> usize {
100         match *self {
101             TokenTree::Delimited(_, ref delimed) => match delimed.delim {
102                 token::NoDelim => delimed.tts.len(),
103                 _ => delimed.tts.len() + 2,
104             },
105             TokenTree::Sequence(_, ref seq) => seq.tts.len(),
106             _ => 0,
107         }
108     }
109
110     /// Returns `true` if the given token tree contains no other tokens. This is vacuously true for
111     /// single tokens or metavar/decls, but may be false for delimited trees or sequences.
112     pub fn is_empty(&self) -> bool {
113         match *self {
114             TokenTree::Delimited(_, ref delimed) => match delimed.delim {
115                 token::NoDelim => delimed.tts.is_empty(),
116                 _ => false,
117             },
118             TokenTree::Sequence(_, ref seq) => seq.tts.is_empty(),
119             _ => true,
120         }
121     }
122
123     /// Gets the `index`-th sub-token-tree. This only makes sense for delimited trees and sequences.
124     pub 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.open);
132                 }
133                 if index == delimed.tts.len() + 1 {
134                     return delimed.close_tt(span.close);
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     pub fn span(&self) -> Span {
145         match *self {
146             TokenTree::Token(sp, _)
147             | TokenTree::MetaVar(sp, _)
148             | TokenTree::MetaVarDecl(sp, _, _) => sp,
149             TokenTree::Delimited(sp, _)
150             | TokenTree::Sequence(sp, _) => sp.entire(),
151         }
152     }
153 }
154
155 /// Takes a `tokenstream::TokenStream` and returns a `Vec<self::TokenTree>`. Specifically, this
156 /// takes a generic `TokenStream`, such as is used in the rest of the compiler, and returns a
157 /// collection of `TokenTree` for use in parsing a macro.
158 ///
159 /// # Parameters
160 ///
161 /// - `input`: a token stream to read from, the contents of which we are parsing.
162 /// - `expect_matchers`: `parse` can be used to parse either the "patterns" or the "body" of a
163 ///   macro. Both take roughly the same form _except_ that in a pattern, metavars are declared with
164 ///   their "matcher" type. For example `$var:expr` or `$id:ident`. In this example, `expr` and
165 ///   `ident` are "matchers". They are not present in the body of a macro rule -- just in the
166 ///   pattern, so we pass a parameter to indicate whether to expect them or not.
167 /// - `sess`: the parsing session. Any errors will be emitted to this session.
168 /// - `features`, `attrs`: language feature flags and attributes so that we know whether to use
169 ///   unstable features or not.
170 /// - `edition`: which edition are we in.
171 /// - `macro_node_id`: the NodeId of the macro we are parsing.
172 ///
173 /// # Returns
174 ///
175 /// A collection of `self::TokenTree`. There may also be some errors emitted to `sess`.
176 pub fn parse(
177     input: tokenstream::TokenStream,
178     expect_matchers: bool,
179     sess: &ParseSess,
180     features: &Features,
181     attrs: &[ast::Attribute],
182     edition: Edition,
183     macro_node_id: NodeId,
184 ) -> Vec<TokenTree> {
185     // Will contain the final collection of `self::TokenTree`
186     let mut result = Vec::new();
187
188     // For each token tree in `input`, parse the token into a `self::TokenTree`, consuming
189     // additional trees if need be.
190     let mut trees = input.trees().peekable();
191     while let Some(tree) = trees.next() {
192         // Given the parsed tree, if there is a metavar and we are expecting matchers, actually
193         // parse out the matcher (i.e., in `$id:ident` this would parse the `:` and `ident`).
194         let tree = parse_tree(
195             tree,
196             &mut trees,
197             expect_matchers,
198             sess,
199             features,
200             attrs,
201             edition,
202             macro_node_id,
203         );
204         match tree {
205             TokenTree::MetaVar(start_sp, ident) if expect_matchers => {
206                 let span = match trees.next() {
207                     Some(tokenstream::TokenTree::Token(span, token::Colon)) => match trees.next() {
208                         Some(tokenstream::TokenTree::Token(end_sp, ref tok)) => match tok.ident() {
209                             Some((kind, _)) => {
210                                 let span = end_sp.with_lo(start_sp.lo());
211                                 result.push(TokenTree::MetaVarDecl(span, ident, kind));
212                                 continue;
213                             }
214                             _ => end_sp,
215                         },
216                         tree => tree
217                             .as_ref()
218                             .map(tokenstream::TokenTree::span)
219                             .unwrap_or(span),
220                     },
221                     tree => tree
222                         .as_ref()
223                         .map(tokenstream::TokenTree::span)
224                         .unwrap_or(start_sp),
225                 };
226                 sess.missing_fragment_specifiers.borrow_mut().insert(span);
227                 result.push(TokenTree::MetaVarDecl(
228                     span,
229                     ident,
230                     keywords::Invalid.ident(),
231                 ));
232             }
233
234             // Not a metavar or no matchers allowed, so just return the tree
235             _ => result.push(tree),
236         }
237     }
238     result
239 }
240
241 /// Takes a `tokenstream::TokenTree` and returns a `self::TokenTree`. Specifically, this takes a
242 /// generic `TokenTree`, such as is used in the rest of the compiler, and returns a `TokenTree`
243 /// for use in parsing a macro.
244 ///
245 /// Converting the given tree may involve reading more tokens.
246 ///
247 /// # Parameters
248 ///
249 /// - `tree`: the tree we wish to convert.
250 /// - `trees`: an iterator over trees. We may need to read more tokens from it in order to finish
251 ///   converting `tree`
252 /// - `expect_matchers`: same as for `parse` (see above).
253 /// - `sess`: the parsing session. Any errors will be emitted to this session.
254 /// - `features`, `attrs`: language feature flags and attributes so that we know whether to use
255 ///   unstable features or not.
256 fn parse_tree<I>(
257     tree: tokenstream::TokenTree,
258     trees: &mut Peekable<I>,
259     expect_matchers: bool,
260     sess: &ParseSess,
261     features: &Features,
262     attrs: &[ast::Attribute],
263     edition: Edition,
264     macro_node_id: NodeId,
265 ) -> TokenTree
266 where
267     I: Iterator<Item = tokenstream::TokenTree>,
268 {
269     // Depending on what `tree` is, we could be parsing different parts of a macro
270     match tree {
271         // `tree` is a `$` token. Look at the next token in `trees`
272         tokenstream::TokenTree::Token(span, token::Dollar) => match trees.next() {
273             // `tree` is followed by a delimited set of token trees. This indicates the beginning
274             // of a repetition sequence in the macro (e.g. `$(pat)*`).
275             Some(tokenstream::TokenTree::Delimited(span, delim, tts)) => {
276                 // Must have `(` not `{` or `[`
277                 if delim != token::Paren {
278                     let tok = pprust::token_to_string(&token::OpenDelim(delim));
279                     let msg = format!("expected `(`, found `{}`", tok);
280                     sess.span_diagnostic.span_err(span.entire(), &msg);
281                 }
282                 // Parse the contents of the sequence itself
283                 let sequence = parse(
284                     tts.into(),
285                     expect_matchers,
286                     sess,
287                     features,
288                     attrs,
289                     edition,
290                     macro_node_id,
291                 );
292                 // Get the Kleene operator and optional separator
293                 let (separator, op) =
294                     parse_sep_and_kleene_op(
295                         trees,
296                         span.entire(),
297                         sess,
298                         features,
299                         attrs,
300                         edition,
301                         macro_node_id,
302                     );
303                 // Count the number of captured "names" (i.e., named metavars)
304                 let name_captures = macro_parser::count_names(&sequence);
305                 TokenTree::Sequence(
306                     span,
307                     Lrc::new(SequenceRepetition {
308                         tts: sequence,
309                         separator,
310                         op,
311                         num_captures: name_captures,
312                     }),
313                 )
314             }
315
316             // `tree` is followed by an `ident`. This could be `$meta_var` or the `$crate` special
317             // metavariable that names the crate of the invocation.
318             Some(tokenstream::TokenTree::Token(ident_span, ref token)) if token.is_ident() => {
319                 let (ident, is_raw) = token.ident().unwrap();
320                 let span = ident_span.with_lo(span.lo());
321                 if ident.name == keywords::Crate.name() && !is_raw {
322                     let ident = ast::Ident::new(keywords::DollarCrate.name(), ident.span);
323                     TokenTree::Token(span, token::Ident(ident, is_raw))
324                 } else {
325                     TokenTree::MetaVar(span, ident)
326                 }
327             }
328
329             // `tree` is followed by a random token. This is an error.
330             Some(tokenstream::TokenTree::Token(span, tok)) => {
331                 let msg = format!(
332                     "expected identifier, found `{}`",
333                     pprust::token_to_string(&tok)
334                 );
335                 sess.span_diagnostic.span_err(span, &msg);
336                 TokenTree::MetaVar(span, keywords::Invalid.ident())
337             }
338
339             // There are no more tokens. Just return the `$` we already have.
340             None => TokenTree::Token(span, token::Dollar),
341         },
342
343         // `tree` is an arbitrary token. Keep it.
344         tokenstream::TokenTree::Token(span, tok) => TokenTree::Token(span, tok),
345
346         // `tree` is the beginning of a delimited set of tokens (e.g., `(` or `{`). We need to
347         // descend into the delimited set and further parse it.
348         tokenstream::TokenTree::Delimited(span, delim, tts) => TokenTree::Delimited(
349             span,
350             Lrc::new(Delimited {
351                 delim: delim,
352                 tts: parse(
353                     tts.into(),
354                     expect_matchers,
355                     sess,
356                     features,
357                     attrs,
358                     edition,
359                     macro_node_id,
360                 ),
361             }),
362         ),
363     }
364 }
365
366 /// Takes a token and returns `Some(KleeneOp)` if the token is `+` `*` or `?`. Otherwise, return
367 /// `None`.
368 fn kleene_op(token: &token::Token) -> Option<KleeneOp> {
369     match *token {
370         token::BinOp(token::Star) => Some(KleeneOp::ZeroOrMore),
371         token::BinOp(token::Plus) => Some(KleeneOp::OneOrMore),
372         token::Question => Some(KleeneOp::ZeroOrOne),
373         _ => None,
374     }
375 }
376
377 /// Parse the next token tree of the input looking for a KleeneOp. Returns
378 ///
379 /// - Ok(Ok((op, span))) if the next token tree is a KleeneOp
380 /// - Ok(Err(tok, span)) if the next token tree is a token but not a KleeneOp
381 /// - Err(span) if the next token tree is not a token
382 fn parse_kleene_op<I>(
383     input: &mut I,
384     span: Span,
385 ) -> Result<Result<(KleeneOp, Span), (token::Token, Span)>, Span>
386 where
387     I: Iterator<Item = tokenstream::TokenTree>,
388 {
389     match input.next() {
390         Some(tokenstream::TokenTree::Token(span, tok)) => match kleene_op(&tok) {
391             Some(op) => Ok(Ok((op, span))),
392             None => Ok(Err((tok, span))),
393         },
394         tree => Err(tree
395             .as_ref()
396             .map(tokenstream::TokenTree::span)
397             .unwrap_or(span)),
398     }
399 }
400
401 /// Attempt to parse a single Kleene star, possibly with a separator.
402 ///
403 /// For example, in a pattern such as `$(a),*`, `a` is the pattern to be repeated, `,` is the
404 /// separator, and `*` is the Kleene operator. This function is specifically concerned with parsing
405 /// the last two tokens of such a pattern: namely, the optional separator and the Kleene operator
406 /// itself. Note that here we are parsing the _macro_ itself, rather than trying to match some
407 /// stream of tokens in an invocation of a macro.
408 ///
409 /// This function will take some input iterator `input` corresponding to `span` and a parsing
410 /// session `sess`. If the next one (or possibly two) tokens in `input` correspond to a Kleene
411 /// operator and separator, then a tuple with `(separator, KleeneOp)` is returned. Otherwise, an
412 /// error with the appropriate span is emitted to `sess` and a dummy value is returned.
413 ///
414 /// N.B., in the 2015 edition, `*` and `+` are the only Kleene operators, and `?` is a separator.
415 /// In the 2018 edition however, `?` is a Kleene operator, and not a separator.
416 fn parse_sep_and_kleene_op<I>(
417     input: &mut Peekable<I>,
418     span: Span,
419     sess: &ParseSess,
420     features: &Features,
421     attrs: &[ast::Attribute],
422     edition: Edition,
423     macro_node_id: NodeId,
424 ) -> (Option<token::Token>, KleeneOp)
425 where
426     I: Iterator<Item = tokenstream::TokenTree>,
427 {
428     match edition {
429         Edition::Edition2015 => parse_sep_and_kleene_op_2015(
430             input,
431             span,
432             sess,
433             features,
434             attrs,
435             macro_node_id,
436         ),
437         Edition::Edition2018 => parse_sep_and_kleene_op_2018(input, span, sess, features, attrs),
438     }
439 }
440
441 // `?` is a separator (with a migration warning) and never a KleeneOp.
442 fn parse_sep_and_kleene_op_2015<I>(
443     input: &mut Peekable<I>,
444     span: Span,
445     sess: &ParseSess,
446     _features: &Features,
447     _attrs: &[ast::Attribute],
448     macro_node_id: NodeId,
449 ) -> (Option<token::Token>, KleeneOp)
450 where
451     I: Iterator<Item = tokenstream::TokenTree>,
452 {
453     // We basically look at two token trees here, denoted as #1 and #2 below
454     let span = match parse_kleene_op(input, span) {
455         // #1 is a `+` or `*` KleeneOp
456         //
457         // `?` is ambiguous: it could be a separator (warning) or a Kleene::ZeroOrOne (error), so
458         // we need to look ahead one more token to be sure.
459         Ok(Ok((op, _))) if op != KleeneOp::ZeroOrOne => return (None, op),
460
461         // #1 is `?` token, but it could be a Kleene::ZeroOrOne (error in 2015) without a separator
462         // or it could be a `?` separator followed by any Kleene operator. We need to look ahead 1
463         // token to find out which.
464         Ok(Ok((op, op1_span))) => {
465             assert_eq!(op, KleeneOp::ZeroOrOne);
466
467             // Lookahead at #2. If it is a KleenOp, then #1 is a separator.
468             let is_1_sep = if let Some(&tokenstream::TokenTree::Token(_, ref tok2)) = input.peek() {
469                 kleene_op(tok2).is_some()
470             } else {
471                 false
472             };
473
474             if is_1_sep {
475                 // #1 is a separator and #2 should be a KleepeOp.
476                 // (N.B. We need to advance the input iterator.)
477                 match parse_kleene_op(input, span) {
478                     // #2 is `?`, which is not allowed as a Kleene op in 2015 edition,
479                     // but is allowed in the 2018 edition.
480                     Ok(Ok((op, op2_span))) if op == KleeneOp::ZeroOrOne => {
481                         sess.span_diagnostic
482                             .struct_span_err(op2_span, "expected `*` or `+`")
483                             .note("`?` is not a macro repetition operator in the 2015 edition, \
484                                  but is accepted in the 2018 edition")
485                             .emit();
486
487                         // Return a dummy
488                         return (None, KleeneOp::ZeroOrMore);
489                     }
490
491                     // #2 is a Kleene op, which is the only valid option
492                     Ok(Ok((op, _))) => {
493                         // Warn that `?` as a separator will be deprecated
494                         sess.buffer_lint(
495                             BufferedEarlyLintId::QuestionMarkMacroSep,
496                             op1_span,
497                             macro_node_id,
498                             "using `?` as a separator is deprecated and will be \
499                              a hard error in an upcoming edition",
500                         );
501
502                         return (Some(token::Question), op);
503                     }
504
505                     // #2 is a random token (this is an error) :(
506                     Ok(Err((_, _))) => op1_span,
507
508                     // #2 is not even a token at all :(
509                     Err(_) => op1_span,
510                 }
511             } else {
512                 // `?` is not allowed as a Kleene op in 2015,
513                 // but is allowed in the 2018 edition
514                 sess.span_diagnostic
515                     .struct_span_err(op1_span, "expected `*` or `+`")
516                     .note("`?` is not a macro repetition operator in the 2015 edition, \
517                          but is accepted in the 2018 edition")
518                     .emit();
519
520                 // Return a dummy
521                 return (None, KleeneOp::ZeroOrMore);
522             }
523         }
524
525         // #1 is a separator followed by #2, a KleeneOp
526         Ok(Err((tok, span))) => match parse_kleene_op(input, span) {
527             // #2 is a `?`, which is not allowed as a Kleene op in 2015 edition,
528             // but is allowed in the 2018 edition
529             Ok(Ok((op, op2_span))) if op == KleeneOp::ZeroOrOne => {
530                 sess.span_diagnostic
531                     .struct_span_err(op2_span, "expected `*` or `+`")
532                     .note("`?` is not a macro repetition operator in the 2015 edition, \
533                         but is accepted in the 2018 edition")
534                     .emit();
535
536                 // Return a dummy
537                 return (None, KleeneOp::ZeroOrMore);
538             }
539
540             // #2 is a KleeneOp :D
541             Ok(Ok((op, _))) => return (Some(tok), op),
542
543             // #2 is a random token :(
544             Ok(Err((_, span))) => span,
545
546             // #2 is not a token at all :(
547             Err(span) => span,
548         },
549
550         // #1 is not a token
551         Err(span) => span,
552     };
553
554     sess.span_diagnostic.span_err(span, "expected `*` or `+`");
555
556     // Return a dummy
557     (None, KleeneOp::ZeroOrMore)
558 }
559
560 // `?` is a Kleene op, not a separator
561 fn parse_sep_and_kleene_op_2018<I>(
562     input: &mut Peekable<I>,
563     span: Span,
564     sess: &ParseSess,
565     _features: &Features,
566     _attrs: &[ast::Attribute],
567 ) -> (Option<token::Token>, KleeneOp)
568 where
569     I: Iterator<Item = tokenstream::TokenTree>,
570 {
571     // We basically look at two token trees here, denoted as #1 and #2 below
572     let span = match parse_kleene_op(input, span) {
573         // #1 is a `?` (needs feature gate)
574         Ok(Ok((op, _op1_span))) if op == KleeneOp::ZeroOrOne => {
575             return (None, op);
576         }
577
578         // #1 is a `+` or `*` KleeneOp
579         Ok(Ok((op, _))) => return (None, op),
580
581         // #1 is a separator followed by #2, a KleeneOp
582         Ok(Err((tok, span))) => match parse_kleene_op(input, span) {
583             // #2 is the `?` Kleene op, which does not take a separator (error)
584             Ok(Ok((op, _op2_span))) if op == KleeneOp::ZeroOrOne => {
585                 // Error!
586                 sess.span_diagnostic.span_err(
587                     span,
588                     "the `?` macro repetition operator does not take a separator",
589                 );
590
591                 // Return a dummy
592                 return (None, KleeneOp::ZeroOrMore);
593             }
594
595             // #2 is a KleeneOp :D
596             Ok(Ok((op, _))) => return (Some(tok), op),
597
598             // #2 is a random token :(
599             Ok(Err((_, span))) => span,
600
601             // #2 is not a token at all :(
602             Err(span) => span,
603         },
604
605         // #1 is not a token
606         Err(span) => span,
607     };
608
609     // If we ever get to this point, we have experienced an "unexpected token" error
610     sess.span_diagnostic
611         .span_err(span, "expected one of: `*`, `+`, or `?`");
612
613     // Return a dummy
614     (None, KleeneOp::ZeroOrMore)
615 }