]> git.lizzy.rs Git - rust.git/blob - src/librustc_expand/mbe/macro_parser.rs
Suggest defining type parameter when appropriate
[rust.git] / src / librustc_expand / mbe / macro_parser.rs
1 //! This is an NFA-based parser, which calls out to the main rust parser for named non-terminals
2 //! (which it commits to fully when it hits one in a grammar). There's a set of current NFA threads
3 //! and a set of next ones. Instead of NTs, we have a special case for Kleene star. The big-O, in
4 //! pathological cases, is worse than traditional use of NFA or Earley parsing, but it's an easier
5 //! fit for Macro-by-Example-style rules.
6 //!
7 //! (In order to prevent the pathological case, we'd need to lazily construct the resulting
8 //! `NamedMatch`es at the very end. It'd be a pain, and require more memory to keep around old
9 //! items, but it would also save overhead)
10 //!
11 //! We don't say this parser uses the Earley algorithm, because it's unnecessarily inaccurate.
12 //! The macro parser restricts itself to the features of finite state automata. Earley parsers
13 //! can be described as an extension of NFAs with completion rules, prediction rules, and recursion.
14 //!
15 //! Quick intro to how the parser works:
16 //!
17 //! A 'position' is a dot in the middle of a matcher, usually represented as a
18 //! dot. For example `· a $( a )* a b` is a position, as is `a $( · a )* a b`.
19 //!
20 //! The parser walks through the input a character at a time, maintaining a list
21 //! of threads consistent with the current position in the input string: `cur_items`.
22 //!
23 //! As it processes them, it fills up `eof_items` with threads that would be valid if
24 //! the macro invocation is now over, `bb_items` with threads that are waiting on
25 //! a Rust non-terminal like `$e:expr`, and `next_items` with threads that are waiting
26 //! on a particular token. Most of the logic concerns moving the · through the
27 //! repetitions indicated by Kleene stars. The rules for moving the · without
28 //! consuming any input are called epsilon transitions. It only advances or calls
29 //! out to the real Rust parser when no `cur_items` threads remain.
30 //!
31 //! Example:
32 //!
33 //! ```text, ignore
34 //! Start parsing a a a a b against [· a $( a )* a b].
35 //!
36 //! Remaining input: a a a a b
37 //! next: [· a $( a )* a b]
38 //!
39 //! - - - Advance over an a. - - -
40 //!
41 //! Remaining input: a a a b
42 //! cur: [a · $( a )* a b]
43 //! Descend/Skip (first item).
44 //! next: [a $( · a )* a b]  [a $( a )* · a b].
45 //!
46 //! - - - Advance over an a. - - -
47 //!
48 //! Remaining input: a a b
49 //! cur: [a $( a · )* a b]  [a $( a )* a · b]
50 //! Follow epsilon transition: Finish/Repeat (first item)
51 //! next: [a $( a )* · a b]  [a $( · a )* a b]  [a $( a )* a · b]
52 //!
53 //! - - - Advance over an a. - - - (this looks exactly like the last step)
54 //!
55 //! Remaining input: a b
56 //! cur: [a $( a · )* a b]  [a $( a )* a · b]
57 //! Follow epsilon transition: Finish/Repeat (first item)
58 //! next: [a $( a )* · a b]  [a $( · a )* a b]  [a $( a )* a · b]
59 //!
60 //! - - - Advance over an a. - - - (this looks exactly like the last step)
61 //!
62 //! Remaining input: b
63 //! cur: [a $( a · )* a b]  [a $( a )* a · b]
64 //! Follow epsilon transition: Finish/Repeat (first item)
65 //! next: [a $( a )* · a b]  [a $( · a )* a b]  [a $( a )* a · b]
66 //!
67 //! - - - Advance over a b. - - -
68 //!
69 //! Remaining input: ''
70 //! eof: [a $( a )* a b ·]
71 //! ```
72
73 crate use NamedMatch::*;
74 crate use ParseResult::*;
75 use TokenTreeOrTokenTreeSlice::*;
76
77 use crate::mbe::{self, TokenTree};
78
79 use rustc_parse::parser::{FollowedByType, Parser, PathStyle};
80 use rustc_parse::Directory;
81 use rustc_span::symbol::{kw, sym, Symbol};
82 use syntax::ast::{Ident, Name};
83 use syntax::print::pprust;
84 use syntax::sess::ParseSess;
85 use syntax::token::{self, DocComment, Nonterminal, Token};
86 use syntax::tokenstream::TokenStream;
87
88 use rustc_errors::{FatalError, PResult};
89 use rustc_span::Span;
90 use smallvec::{smallvec, SmallVec};
91
92 use rustc_data_structures::fx::FxHashMap;
93 use rustc_data_structures::sync::Lrc;
94 use std::collections::hash_map::Entry::{Occupied, Vacant};
95 use std::mem;
96 use std::ops::{Deref, DerefMut};
97
98 // To avoid costly uniqueness checks, we require that `MatchSeq` always has a nonempty body.
99
100 /// Either a sequence of token trees or a single one. This is used as the representation of the
101 /// sequence of tokens that make up a matcher.
102 #[derive(Clone)]
103 enum TokenTreeOrTokenTreeSlice<'tt> {
104     Tt(TokenTree),
105     TtSeq(&'tt [TokenTree]),
106 }
107
108 impl<'tt> TokenTreeOrTokenTreeSlice<'tt> {
109     /// Returns the number of constituent top-level token trees of `self` (top-level in that it
110     /// will not recursively descend into subtrees).
111     fn len(&self) -> usize {
112         match *self {
113             TtSeq(ref v) => v.len(),
114             Tt(ref tt) => tt.len(),
115         }
116     }
117
118     /// The `index`-th token tree of `self`.
119     fn get_tt(&self, index: usize) -> TokenTree {
120         match *self {
121             TtSeq(ref v) => v[index].clone(),
122             Tt(ref tt) => tt.get_tt(index),
123         }
124     }
125 }
126
127 /// An unzipping of `TokenTree`s... see the `stack` field of `MatcherPos`.
128 ///
129 /// This is used by `inner_parse_loop` to keep track of delimited submatchers that we have
130 /// descended into.
131 #[derive(Clone)]
132 struct MatcherTtFrame<'tt> {
133     /// The "parent" matcher that we are descending into.
134     elts: TokenTreeOrTokenTreeSlice<'tt>,
135     /// The position of the "dot" in `elts` at the time we descended.
136     idx: usize,
137 }
138
139 type NamedMatchVec = SmallVec<[NamedMatch; 4]>;
140
141 /// Represents a single "position" (aka "matcher position", aka "item"), as
142 /// described in the module documentation.
143 ///
144 /// Here:
145 ///
146 /// - `'root` represents the lifetime of the stack slot that holds the root
147 ///   `MatcherPos`. As described in `MatcherPosHandle`, the root `MatcherPos`
148 ///   structure is stored on the stack, but subsequent instances are put into
149 ///   the heap.
150 /// - `'tt` represents the lifetime of the token trees that this matcher
151 ///   position refers to.
152 ///
153 /// It is important to distinguish these two lifetimes because we have a
154 /// `SmallVec<TokenTreeOrTokenTreeSlice<'tt>>` below, and the destructor of
155 /// that is considered to possibly access the data from its elements (it lacks
156 /// a `#[may_dangle]` attribute). As a result, the compiler needs to know that
157 /// all the elements in that `SmallVec` strictly outlive the root stack slot
158 /// lifetime. By separating `'tt` from `'root`, we can show that.
159 #[derive(Clone)]
160 struct MatcherPos<'root, 'tt> {
161     /// The token or sequence of tokens that make up the matcher
162     top_elts: TokenTreeOrTokenTreeSlice<'tt>,
163
164     /// The position of the "dot" in this matcher
165     idx: usize,
166
167     /// For each named metavar in the matcher, we keep track of token trees matched against the
168     /// metavar by the black box parser. In particular, there may be more than one match per
169     /// metavar if we are in a repetition (each repetition matches each of the variables).
170     /// Moreover, matchers and repetitions can be nested; the `matches` field is shared (hence the
171     /// `Rc`) among all "nested" matchers. `match_lo`, `match_cur`, and `match_hi` keep track of
172     /// the current position of the `self` matcher position in the shared `matches` list.
173     ///
174     /// Also, note that while we are descending into a sequence, matchers are given their own
175     /// `matches` vector. Only once we reach the end of a full repetition of the sequence do we add
176     /// all bound matches from the submatcher into the shared top-level `matches` vector. If `sep`
177     /// and `up` are `Some`, then `matches` is _not_ the shared top-level list. Instead, if one
178     /// wants the shared `matches`, one should use `up.matches`.
179     matches: Box<[Lrc<NamedMatchVec>]>,
180     /// The position in `matches` corresponding to the first metavar in this matcher's sequence of
181     /// token trees. In other words, the first metavar in the first token of `top_elts` corresponds
182     /// to `matches[match_lo]`.
183     match_lo: usize,
184     /// The position in `matches` corresponding to the metavar we are currently trying to match
185     /// against the source token stream. `match_lo <= match_cur <= match_hi`.
186     match_cur: usize,
187     /// Similar to `match_lo` except `match_hi` is the position in `matches` of the _last_ metavar
188     /// in this matcher.
189     match_hi: usize,
190
191     // The following fields are used if we are matching a repetition. If we aren't, they should be
192     // `None`.
193     /// The KleeneOp of this sequence if we are in a repetition.
194     seq_op: Option<mbe::KleeneOp>,
195
196     /// The separator if we are in a repetition.
197     sep: Option<Token>,
198
199     /// The "parent" matcher position if we are in a repetition. That is, the matcher position just
200     /// before we enter the sequence.
201     up: Option<MatcherPosHandle<'root, 'tt>>,
202
203     /// Specifically used to "unzip" token trees. By "unzip", we mean to unwrap the delimiters from
204     /// a delimited token tree (e.g., something wrapped in `(` `)`) or to get the contents of a doc
205     /// comment...
206     ///
207     /// When matching against matchers with nested delimited submatchers (e.g., `pat ( pat ( .. )
208     /// pat ) pat`), we need to keep track of the matchers we are descending into. This stack does
209     /// that where the bottom of the stack is the outermost matcher.
210     /// Also, throughout the comments, this "descent" is often referred to as "unzipping"...
211     stack: SmallVec<[MatcherTtFrame<'tt>; 1]>,
212 }
213
214 impl<'root, 'tt> MatcherPos<'root, 'tt> {
215     /// Adds `m` as a named match for the `idx`-th metavar.
216     fn push_match(&mut self, idx: usize, m: NamedMatch) {
217         let matches = Lrc::make_mut(&mut self.matches[idx]);
218         matches.push(m);
219     }
220 }
221
222 // Lots of MatcherPos instances are created at runtime. Allocating them on the
223 // heap is slow. Furthermore, using SmallVec<MatcherPos> to allocate them all
224 // on the stack is also slow, because MatcherPos is quite a large type and
225 // instances get moved around a lot between vectors, which requires lots of
226 // slow memcpy calls.
227 //
228 // Therefore, the initial MatcherPos is always allocated on the stack,
229 // subsequent ones (of which there aren't that many) are allocated on the heap,
230 // and this type is used to encapsulate both cases.
231 enum MatcherPosHandle<'root, 'tt> {
232     Ref(&'root mut MatcherPos<'root, 'tt>),
233     Box(Box<MatcherPos<'root, 'tt>>),
234 }
235
236 impl<'root, 'tt> Clone for MatcherPosHandle<'root, 'tt> {
237     // This always produces a new Box.
238     fn clone(&self) -> Self {
239         MatcherPosHandle::Box(match *self {
240             MatcherPosHandle::Ref(ref r) => Box::new((**r).clone()),
241             MatcherPosHandle::Box(ref b) => b.clone(),
242         })
243     }
244 }
245
246 impl<'root, 'tt> Deref for MatcherPosHandle<'root, 'tt> {
247     type Target = MatcherPos<'root, 'tt>;
248     fn deref(&self) -> &Self::Target {
249         match *self {
250             MatcherPosHandle::Ref(ref r) => r,
251             MatcherPosHandle::Box(ref b) => b,
252         }
253     }
254 }
255
256 impl<'root, 'tt> DerefMut for MatcherPosHandle<'root, 'tt> {
257     fn deref_mut(&mut self) -> &mut MatcherPos<'root, 'tt> {
258         match *self {
259             MatcherPosHandle::Ref(ref mut r) => r,
260             MatcherPosHandle::Box(ref mut b) => b,
261         }
262     }
263 }
264
265 /// Represents the possible results of an attempted parse.
266 crate enum ParseResult<T> {
267     /// Parsed successfully.
268     Success(T),
269     /// Arm failed to match. If the second parameter is `token::Eof`, it indicates an unexpected
270     /// end of macro invocation. Otherwise, it indicates that no rules expected the given token.
271     Failure(Token, &'static str),
272     /// Fatal error (malformed macro?). Abort compilation.
273     Error(rustc_span::Span, String),
274 }
275
276 /// A `ParseResult` where the `Success` variant contains a mapping of `Ident`s to `NamedMatch`es.
277 /// This represents the mapping of metavars to the token trees they bind to.
278 crate type NamedParseResult = ParseResult<FxHashMap<Ident, NamedMatch>>;
279
280 /// Count how many metavars are named in the given matcher `ms`.
281 pub(super) fn count_names(ms: &[TokenTree]) -> usize {
282     ms.iter().fold(0, |count, elt| {
283         count
284             + match *elt {
285                 TokenTree::Sequence(_, ref seq) => seq.num_captures,
286                 TokenTree::Delimited(_, ref delim) => count_names(&delim.tts),
287                 TokenTree::MetaVar(..) => 0,
288                 TokenTree::MetaVarDecl(..) => 1,
289                 TokenTree::Token(..) => 0,
290             }
291     })
292 }
293
294 /// `len` `Vec`s (initially shared and empty) that will store matches of metavars.
295 fn create_matches(len: usize) -> Box<[Lrc<NamedMatchVec>]> {
296     if len == 0 {
297         vec![]
298     } else {
299         let empty_matches = Lrc::new(SmallVec::new());
300         vec![empty_matches; len]
301     }
302     .into_boxed_slice()
303 }
304
305 /// Generates the top-level matcher position in which the "dot" is before the first token of the
306 /// matcher `ms`.
307 fn initial_matcher_pos<'root, 'tt>(ms: &'tt [TokenTree]) -> MatcherPos<'root, 'tt> {
308     let match_idx_hi = count_names(ms);
309     let matches = create_matches(match_idx_hi);
310     MatcherPos {
311         // Start with the top level matcher given to us
312         top_elts: TtSeq(ms), // "elts" is an abbr. for "elements"
313         // The "dot" is before the first token of the matcher
314         idx: 0,
315
316         // Initialize `matches` to a bunch of empty `Vec`s -- one for each metavar in `top_elts`.
317         // `match_lo` for `top_elts` is 0 and `match_hi` is `matches.len()`. `match_cur` is 0 since
318         // we haven't actually matched anything yet.
319         matches,
320         match_lo: 0,
321         match_cur: 0,
322         match_hi: match_idx_hi,
323
324         // Haven't descended into any delimiters, so empty stack
325         stack: smallvec![],
326
327         // Haven't descended into any sequences, so both of these are `None`.
328         seq_op: None,
329         sep: None,
330         up: None,
331     }
332 }
333
334 /// `NamedMatch` is a pattern-match result for a single `token::MATCH_NONTERMINAL`:
335 /// so it is associated with a single ident in a parse, and all
336 /// `MatchedNonterminal`s in the `NamedMatch` have the same non-terminal type
337 /// (expr, item, etc). Each leaf in a single `NamedMatch` corresponds to a
338 /// single `token::MATCH_NONTERMINAL` in the `TokenTree` that produced it.
339 ///
340 /// The in-memory structure of a particular `NamedMatch` represents the match
341 /// that occurred when a particular subset of a matcher was applied to a
342 /// particular token tree.
343 ///
344 /// The width of each `MatchedSeq` in the `NamedMatch`, and the identity of
345 /// the `MatchedNonterminal`s, will depend on the token tree it was applied
346 /// to: each `MatchedSeq` corresponds to a single `TTSeq` in the originating
347 /// token tree. The depth of the `NamedMatch` structure will therefore depend
348 /// only on the nesting depth of `ast::TTSeq`s in the originating
349 /// token tree it was derived from.
350 #[derive(Debug, Clone)]
351 crate enum NamedMatch {
352     MatchedSeq(Lrc<NamedMatchVec>),
353     MatchedNonterminal(Lrc<Nonterminal>),
354 }
355
356 /// Takes a sequence of token trees `ms` representing a matcher which successfully matched input
357 /// and an iterator of items that matched input and produces a `NamedParseResult`.
358 fn nameize<I: Iterator<Item = NamedMatch>>(
359     sess: &ParseSess,
360     ms: &[TokenTree],
361     mut res: I,
362 ) -> NamedParseResult {
363     // Recursively descend into each type of matcher (e.g., sequences, delimited, metavars) and make
364     // sure that each metavar has _exactly one_ binding. If a metavar does not have exactly one
365     // binding, then there is an error. If it does, then we insert the binding into the
366     // `NamedParseResult`.
367     fn n_rec<I: Iterator<Item = NamedMatch>>(
368         sess: &ParseSess,
369         m: &TokenTree,
370         res: &mut I,
371         ret_val: &mut FxHashMap<Ident, NamedMatch>,
372     ) -> Result<(), (rustc_span::Span, String)> {
373         match *m {
374             TokenTree::Sequence(_, ref seq) => {
375                 for next_m in &seq.tts {
376                     n_rec(sess, next_m, res.by_ref(), ret_val)?
377                 }
378             }
379             TokenTree::Delimited(_, ref delim) => {
380                 for next_m in &delim.tts {
381                     n_rec(sess, next_m, res.by_ref(), ret_val)?;
382                 }
383             }
384             TokenTree::MetaVarDecl(span, _, id) if id.name == kw::Invalid => {
385                 if sess.missing_fragment_specifiers.borrow_mut().remove(&span) {
386                     return Err((span, "missing fragment specifier".to_string()));
387                 }
388             }
389             TokenTree::MetaVarDecl(sp, bind_name, _) => match ret_val.entry(bind_name) {
390                 Vacant(spot) => {
391                     spot.insert(res.next().unwrap());
392                 }
393                 Occupied(..) => return Err((sp, format!("duplicated bind name: {}", bind_name))),
394             },
395             TokenTree::MetaVar(..) | TokenTree::Token(..) => (),
396         }
397
398         Ok(())
399     }
400
401     let mut ret_val = FxHashMap::default();
402     for m in ms {
403         match n_rec(sess, m, res.by_ref(), &mut ret_val) {
404             Ok(_) => {}
405             Err((sp, msg)) => return Error(sp, msg),
406         }
407     }
408
409     Success(ret_val)
410 }
411
412 /// Performs a token equality check, ignoring syntax context (that is, an unhygienic comparison)
413 fn token_name_eq(t1: &Token, t2: &Token) -> bool {
414     if let (Some((ident1, is_raw1)), Some((ident2, is_raw2))) = (t1.ident(), t2.ident()) {
415         ident1.name == ident2.name && is_raw1 == is_raw2
416     } else if let (Some(ident1), Some(ident2)) = (t1.lifetime(), t2.lifetime()) {
417         ident1.name == ident2.name
418     } else {
419         t1.kind == t2.kind
420     }
421 }
422
423 /// Process the matcher positions of `cur_items` until it is empty. In the process, this will
424 /// produce more items in `next_items`, `eof_items`, and `bb_items`.
425 ///
426 /// For more info about the how this happens, see the module-level doc comments and the inline
427 /// comments of this function.
428 ///
429 /// # Parameters
430 ///
431 /// - `sess`: the parsing session into which errors are emitted.
432 /// - `cur_items`: the set of current items to be processed. This should be empty by the end of a
433 ///   successful execution of this function.
434 /// - `next_items`: the set of newly generated items. These are used to replenish `cur_items` in
435 ///   the function `parse`.
436 /// - `eof_items`: the set of items that would be valid if this was the EOF.
437 /// - `bb_items`: the set of items that are waiting for the black-box parser.
438 /// - `token`: the current token of the parser.
439 /// - `span`: the `Span` in the source code corresponding to the token trees we are trying to match
440 ///   against the matcher positions in `cur_items`.
441 ///
442 /// # Returns
443 ///
444 /// A `ParseResult`. Note that matches are kept track of through the items generated.
445 fn inner_parse_loop<'root, 'tt>(
446     sess: &ParseSess,
447     cur_items: &mut SmallVec<[MatcherPosHandle<'root, 'tt>; 1]>,
448     next_items: &mut Vec<MatcherPosHandle<'root, 'tt>>,
449     eof_items: &mut SmallVec<[MatcherPosHandle<'root, 'tt>; 1]>,
450     bb_items: &mut SmallVec<[MatcherPosHandle<'root, 'tt>; 1]>,
451     token: &Token,
452 ) -> ParseResult<()> {
453     // Pop items from `cur_items` until it is empty.
454     while let Some(mut item) = cur_items.pop() {
455         // When unzipped trees end, remove them. This corresponds to backtracking out of a
456         // delimited submatcher into which we already descended. In backtracking out again, we need
457         // to advance the "dot" past the delimiters in the outer matcher.
458         while item.idx >= item.top_elts.len() {
459             match item.stack.pop() {
460                 Some(MatcherTtFrame { elts, idx }) => {
461                     item.top_elts = elts;
462                     item.idx = idx + 1;
463                 }
464                 None => break,
465             }
466         }
467
468         // Get the current position of the "dot" (`idx`) in `item` and the number of token trees in
469         // the matcher (`len`).
470         let idx = item.idx;
471         let len = item.top_elts.len();
472
473         // If `idx >= len`, then we are at or past the end of the matcher of `item`.
474         if idx >= len {
475             // We are repeating iff there is a parent. If the matcher is inside of a repetition,
476             // then we could be at the end of a sequence or at the beginning of the next
477             // repetition.
478             if item.up.is_some() {
479                 // At this point, regardless of whether there is a separator, we should add all
480                 // matches from the complete repetition of the sequence to the shared, top-level
481                 // `matches` list (actually, `up.matches`, which could itself not be the top-level,
482                 // but anyway...). Moreover, we add another item to `cur_items` in which the "dot"
483                 // is at the end of the `up` matcher. This ensures that the "dot" in the `up`
484                 // matcher is also advanced sufficiently.
485                 //
486                 // NOTE: removing the condition `idx == len` allows trailing separators.
487                 if idx == len {
488                     // Get the `up` matcher
489                     let mut new_pos = item.up.clone().unwrap();
490
491                     // Add matches from this repetition to the `matches` of `up`
492                     for idx in item.match_lo..item.match_hi {
493                         let sub = item.matches[idx].clone();
494                         new_pos.push_match(idx, MatchedSeq(sub));
495                     }
496
497                     // Move the "dot" past the repetition in `up`
498                     new_pos.match_cur = item.match_hi;
499                     new_pos.idx += 1;
500                     cur_items.push(new_pos);
501                 }
502
503                 // Check if we need a separator.
504                 if idx == len && item.sep.is_some() {
505                     // We have a separator, and it is the current token. We can advance past the
506                     // separator token.
507                     if item.sep.as_ref().map(|sep| token_name_eq(token, sep)).unwrap_or(false) {
508                         item.idx += 1;
509                         next_items.push(item);
510                     }
511                 }
512                 // We don't need a separator. Move the "dot" back to the beginning of the matcher
513                 // and try to match again UNLESS we are only allowed to have _one_ repetition.
514                 else if item.seq_op != Some(mbe::KleeneOp::ZeroOrOne) {
515                     item.match_cur = item.match_lo;
516                     item.idx = 0;
517                     cur_items.push(item);
518                 }
519             }
520             // If we are not in a repetition, then being at the end of a matcher means that we have
521             // reached the potential end of the input.
522             else {
523                 eof_items.push(item);
524             }
525         }
526         // We are in the middle of a matcher.
527         else {
528             // Look at what token in the matcher we are trying to match the current token (`token`)
529             // against. Depending on that, we may generate new items.
530             match item.top_elts.get_tt(idx) {
531                 // Need to descend into a sequence
532                 TokenTree::Sequence(sp, seq) => {
533                     // Examine the case where there are 0 matches of this sequence. We are
534                     // implicitly disallowing OneOrMore from having 0 matches here. Thus, that will
535                     // result in a "no rules expected token" error by virtue of this matcher not
536                     // working.
537                     if seq.kleene.op == mbe::KleeneOp::ZeroOrMore
538                         || seq.kleene.op == mbe::KleeneOp::ZeroOrOne
539                     {
540                         let mut new_item = item.clone();
541                         new_item.match_cur += seq.num_captures;
542                         new_item.idx += 1;
543                         for idx in item.match_cur..item.match_cur + seq.num_captures {
544                             new_item.push_match(idx, MatchedSeq(Lrc::new(smallvec![])));
545                         }
546                         cur_items.push(new_item);
547                     }
548
549                     let matches = create_matches(item.matches.len());
550                     cur_items.push(MatcherPosHandle::Box(Box::new(MatcherPos {
551                         stack: smallvec![],
552                         sep: seq.separator.clone(),
553                         seq_op: Some(seq.kleene.op),
554                         idx: 0,
555                         matches,
556                         match_lo: item.match_cur,
557                         match_cur: item.match_cur,
558                         match_hi: item.match_cur + seq.num_captures,
559                         up: Some(item),
560                         top_elts: Tt(TokenTree::Sequence(sp, seq)),
561                     })));
562                 }
563
564                 // We need to match a metavar (but the identifier is invalid)... this is an error
565                 TokenTree::MetaVarDecl(span, _, id) if id.name == kw::Invalid => {
566                     if sess.missing_fragment_specifiers.borrow_mut().remove(&span) {
567                         return Error(span, "missing fragment specifier".to_string());
568                     }
569                 }
570
571                 // We need to match a metavar with a valid ident... call out to the black-box
572                 // parser by adding an item to `bb_items`.
573                 TokenTree::MetaVarDecl(_, _, id) => {
574                     // Built-in nonterminals never start with these tokens,
575                     // so we can eliminate them from consideration.
576                     if may_begin_with(token, id.name) {
577                         bb_items.push(item);
578                     }
579                 }
580
581                 // We need to descend into a delimited submatcher or a doc comment. To do this, we
582                 // push the current matcher onto a stack and push a new item containing the
583                 // submatcher onto `cur_items`.
584                 //
585                 // At the beginning of the loop, if we reach the end of the delimited submatcher,
586                 // we pop the stack to backtrack out of the descent.
587                 seq @ TokenTree::Delimited(..)
588                 | seq @ TokenTree::Token(Token { kind: DocComment(..), .. }) => {
589                     let lower_elts = mem::replace(&mut item.top_elts, Tt(seq));
590                     let idx = item.idx;
591                     item.stack.push(MatcherTtFrame { elts: lower_elts, idx });
592                     item.idx = 0;
593                     cur_items.push(item);
594                 }
595
596                 // We just matched a normal token. We can just advance the parser.
597                 TokenTree::Token(t) if token_name_eq(&t, token) => {
598                     item.idx += 1;
599                     next_items.push(item);
600                 }
601
602                 // There was another token that was not `token`... This means we can't add any
603                 // rules. NOTE that this is not necessarily an error unless _all_ items in
604                 // `cur_items` end up doing this. There may still be some other matchers that do
605                 // end up working out.
606                 TokenTree::Token(..) | TokenTree::MetaVar(..) => {}
607             }
608         }
609     }
610
611     // Yay a successful parse (so far)!
612     Success(())
613 }
614
615 /// Use the given sequence of token trees (`ms`) as a matcher. Match the given token stream `tts`
616 /// against it and return the match.
617 ///
618 /// # Parameters
619 ///
620 /// - `sess`: The session into which errors are emitted
621 /// - `tts`: The tokenstream we are matching against the pattern `ms`
622 /// - `ms`: A sequence of token trees representing a pattern against which we are matching
623 /// - `directory`: Information about the file locations (needed for the black-box parser)
624 /// - `recurse_into_modules`: Whether or not to recurse into modules (needed for the black-box
625 ///   parser)
626 pub(super) fn parse(
627     sess: &ParseSess,
628     tts: TokenStream,
629     ms: &[TokenTree],
630     directory: Option<Directory<'_>>,
631     recurse_into_modules: bool,
632 ) -> NamedParseResult {
633     // Create a parser that can be used for the "black box" parts.
634     let mut parser =
635         Parser::new(sess, tts, directory, recurse_into_modules, true, rustc_parse::MACRO_ARGUMENTS);
636
637     // A queue of possible matcher positions. We initialize it with the matcher position in which
638     // the "dot" is before the first token of the first token tree in `ms`. `inner_parse_loop` then
639     // processes all of these possible matcher positions and produces possible next positions into
640     // `next_items`. After some post-processing, the contents of `next_items` replenish `cur_items`
641     // and we start over again.
642     //
643     // This MatcherPos instance is allocated on the stack. All others -- and
644     // there are frequently *no* others! -- are allocated on the heap.
645     let mut initial = initial_matcher_pos(ms);
646     let mut cur_items = smallvec![MatcherPosHandle::Ref(&mut initial)];
647     let mut next_items = Vec::new();
648
649     loop {
650         // Matcher positions black-box parsed by parser.rs (`parser`)
651         let mut bb_items = SmallVec::new();
652
653         // Matcher positions that would be valid if the macro invocation was over now
654         let mut eof_items = SmallVec::new();
655         assert!(next_items.is_empty());
656
657         // Process `cur_items` until either we have finished the input or we need to get some
658         // parsing from the black-box parser done. The result is that `next_items` will contain a
659         // bunch of possible next matcher positions in `next_items`.
660         match inner_parse_loop(
661             sess,
662             &mut cur_items,
663             &mut next_items,
664             &mut eof_items,
665             &mut bb_items,
666             &parser.token,
667         ) {
668             Success(_) => {}
669             Failure(token, msg) => return Failure(token, msg),
670             Error(sp, msg) => return Error(sp, msg),
671         }
672
673         // inner parse loop handled all cur_items, so it's empty
674         assert!(cur_items.is_empty());
675
676         // We need to do some post processing after the `inner_parser_loop`.
677         //
678         // Error messages here could be improved with links to original rules.
679
680         // If we reached the EOF, check that there is EXACTLY ONE possible matcher. Otherwise,
681         // either the parse is ambiguous (which should never happen) or there is a syntax error.
682         if parser.token == token::Eof {
683             if eof_items.len() == 1 {
684                 let matches =
685                     eof_items[0].matches.iter_mut().map(|dv| Lrc::make_mut(dv).pop().unwrap());
686                 return nameize(sess, ms, matches);
687             } else if eof_items.len() > 1 {
688                 return Error(
689                     parser.token.span,
690                     "ambiguity: multiple successful parses".to_string(),
691                 );
692             } else {
693                 return Failure(
694                     Token::new(
695                         token::Eof,
696                         if parser.token.span.is_dummy() {
697                             parser.token.span
698                         } else {
699                             parser.token.span.shrink_to_hi()
700                         },
701                     ),
702                     "missing tokens in macro arguments",
703                 );
704             }
705         }
706         // Performance hack: eof_items may share matchers via Rc with other things that we want
707         // to modify. Dropping eof_items now may drop these refcounts to 1, preventing an
708         // unnecessary implicit clone later in Rc::make_mut.
709         drop(eof_items);
710
711         // Another possibility is that we need to call out to parse some rust nonterminal
712         // (black-box) parser. However, if there is not EXACTLY ONE of these, something is wrong.
713         if (!bb_items.is_empty() && !next_items.is_empty()) || bb_items.len() > 1 {
714             let nts = bb_items
715                 .iter()
716                 .map(|item| match item.top_elts.get_tt(item.idx) {
717                     TokenTree::MetaVarDecl(_, bind, name) => format!("{} ('{}')", name, bind),
718                     _ => panic!(),
719                 })
720                 .collect::<Vec<String>>()
721                 .join(" or ");
722
723             return Error(
724                 parser.token.span,
725                 format!(
726                     "local ambiguity: multiple parsing options: {}",
727                     match next_items.len() {
728                         0 => format!("built-in NTs {}.", nts),
729                         1 => format!("built-in NTs {} or 1 other option.", nts),
730                         n => format!("built-in NTs {} or {} other options.", nts, n),
731                     }
732                 ),
733             );
734         }
735         // If there are no possible next positions AND we aren't waiting for the black-box parser,
736         // then there is a syntax error.
737         else if bb_items.is_empty() && next_items.is_empty() {
738             return Failure(parser.token.take(), "no rules expected this token in macro call");
739         }
740         // Dump all possible `next_items` into `cur_items` for the next iteration.
741         else if !next_items.is_empty() {
742             // Now process the next token
743             cur_items.extend(next_items.drain(..));
744             parser.bump();
745         }
746         // Finally, we have the case where we need to call the black-box parser to get some
747         // nonterminal.
748         else {
749             assert_eq!(bb_items.len(), 1);
750
751             let mut item = bb_items.pop().unwrap();
752             if let TokenTree::MetaVarDecl(span, _, ident) = item.top_elts.get_tt(item.idx) {
753                 let match_cur = item.match_cur;
754                 item.push_match(
755                     match_cur,
756                     MatchedNonterminal(Lrc::new(parse_nt(&mut parser, span, ident.name))),
757                 );
758                 item.idx += 1;
759                 item.match_cur += 1;
760             } else {
761                 unreachable!()
762             }
763             cur_items.push(item);
764         }
765
766         assert!(!cur_items.is_empty());
767     }
768 }
769
770 /// The token is an identifier, but not `_`.
771 /// We prohibit passing `_` to macros expecting `ident` for now.
772 fn get_macro_name(token: &Token) -> Option<(Name, bool)> {
773     match token.kind {
774         token::Ident(name, is_raw) if name != kw::Underscore => Some((name, is_raw)),
775         _ => None,
776     }
777 }
778
779 /// Checks whether a non-terminal may begin with a particular token.
780 ///
781 /// Returning `false` is a *stability guarantee* that such a matcher will *never* begin with that
782 /// token. Be conservative (return true) if not sure.
783 fn may_begin_with(token: &Token, name: Name) -> bool {
784     /// Checks whether the non-terminal may contain a single (non-keyword) identifier.
785     fn may_be_ident(nt: &token::Nonterminal) -> bool {
786         match *nt {
787             token::NtItem(_) | token::NtBlock(_) | token::NtVis(_) => false,
788             _ => true,
789         }
790     }
791
792     match name {
793         sym::expr => {
794             token.can_begin_expr()
795             // This exception is here for backwards compatibility.
796             && !token.is_keyword(kw::Let)
797         }
798         sym::ty => token.can_begin_type(),
799         sym::ident => get_macro_name(token).is_some(),
800         sym::literal => token.can_begin_literal_or_bool(),
801         sym::vis => match token.kind {
802             // The follow-set of :vis + "priv" keyword + interpolated
803             token::Comma | token::Ident(..) | token::Interpolated(_) => true,
804             _ => token.can_begin_type(),
805         },
806         sym::block => match token.kind {
807             token::OpenDelim(token::Brace) => true,
808             token::Interpolated(ref nt) => match **nt {
809                 token::NtItem(_)
810                 | token::NtPat(_)
811                 | token::NtTy(_)
812                 | token::NtIdent(..)
813                 | token::NtMeta(_)
814                 | token::NtPath(_)
815                 | token::NtVis(_) => false, // none of these may start with '{'.
816                 _ => true,
817             },
818             _ => false,
819         },
820         sym::path | sym::meta => match token.kind {
821             token::ModSep | token::Ident(..) => true,
822             token::Interpolated(ref nt) => match **nt {
823                 token::NtPath(_) | token::NtMeta(_) => true,
824                 _ => may_be_ident(&nt),
825             },
826             _ => false,
827         },
828         sym::pat => match token.kind {
829             token::Ident(..) |               // box, ref, mut, and other identifiers (can stricten)
830             token::OpenDelim(token::Paren) |    // tuple pattern
831             token::OpenDelim(token::Bracket) |  // slice pattern
832             token::BinOp(token::And) |          // reference
833             token::BinOp(token::Minus) |        // negative literal
834             token::AndAnd |                     // double reference
835             token::Literal(..) |                // literal
836             token::DotDot |                     // range pattern (future compat)
837             token::DotDotDot |                  // range pattern (future compat)
838             token::ModSep |                     // path
839             token::Lt |                         // path (UFCS constant)
840             token::BinOp(token::Shl) => true,   // path (double UFCS)
841             token::Interpolated(ref nt) => may_be_ident(nt),
842             _ => false,
843         },
844         sym::lifetime => match token.kind {
845             token::Lifetime(_) => true,
846             token::Interpolated(ref nt) => match **nt {
847                 token::NtLifetime(_) | token::NtTT(_) => true,
848                 _ => false,
849             },
850             _ => false,
851         },
852         _ => match token.kind {
853             token::CloseDelim(_) => false,
854             _ => true,
855         },
856     }
857 }
858
859 /// A call to the "black-box" parser to parse some Rust non-terminal.
860 ///
861 /// # Parameters
862 ///
863 /// - `p`: the "black-box" parser to use
864 /// - `sp`: the `Span` we want to parse
865 /// - `name`: the name of the metavar _matcher_ we want to match (e.g., `tt`, `ident`, `block`,
866 ///   etc...)
867 ///
868 /// # Returns
869 ///
870 /// The parsed non-terminal.
871 fn parse_nt(p: &mut Parser<'_>, sp: Span, name: Symbol) -> Nonterminal {
872     // FIXME(Centril): Consider moving this to `parser.rs` to make
873     // the visibilities of the methods used below `pub(super)` at most.
874
875     if name == sym::tt {
876         return token::NtTT(p.parse_token_tree());
877     }
878     // check at the beginning and the parser checks after each bump
879     p.process_potential_macro_variable();
880     match parse_nt_inner(p, sp, name) {
881         Ok(nt) => nt,
882         Err(mut err) => {
883             err.emit();
884             FatalError.raise();
885         }
886     }
887 }
888
889 fn parse_nt_inner<'a>(p: &mut Parser<'a>, sp: Span, name: Symbol) -> PResult<'a, Nonterminal> {
890     Ok(match name {
891         sym::item => match p.parse_item()? {
892             Some(i) => token::NtItem(i),
893             None => return Err(p.struct_span_err(p.token.span, "expected an item keyword")),
894         },
895         sym::block => token::NtBlock(p.parse_block()?),
896         sym::stmt => match p.parse_stmt()? {
897             Some(s) => token::NtStmt(s),
898             None => return Err(p.struct_span_err(p.token.span, "expected a statement")),
899         },
900         sym::pat => token::NtPat(p.parse_pat(None)?),
901         sym::expr => token::NtExpr(p.parse_expr()?),
902         sym::literal => token::NtLiteral(p.parse_literal_maybe_minus()?),
903         sym::ty => token::NtTy(p.parse_ty()?),
904         // this could be handled like a token, since it is one
905         sym::ident => {
906             if let Some((name, is_raw)) = get_macro_name(&p.token) {
907                 let span = p.token.span;
908                 p.bump();
909                 token::NtIdent(Ident::new(name, span), is_raw)
910             } else {
911                 let token_str = pprust::token_to_string(&p.token);
912                 let msg = &format!("expected ident, found {}", &token_str);
913                 return Err(p.struct_span_err(p.token.span, msg));
914             }
915         }
916         sym::path => token::NtPath(p.parse_path(PathStyle::Type)?),
917         sym::meta => token::NtMeta(p.parse_attr_item()?),
918         sym::vis => token::NtVis(p.parse_visibility(FollowedByType::Yes)?),
919         sym::lifetime => {
920             if p.check_lifetime() {
921                 token::NtLifetime(p.expect_lifetime().ident)
922             } else {
923                 let token_str = pprust::token_to_string(&p.token);
924                 let msg = &format!("expected a lifetime, found `{}`", &token_str);
925                 return Err(p.struct_span_err(p.token.span, msg));
926             }
927         }
928         // this is not supposed to happen, since it has been checked
929         // when compiling the macro.
930         _ => p.span_bug(sp, "invalid fragment specifier"),
931     })
932 }