]> git.proxmox.com Git - rustc.git/blob - src/librustc_expand/mbe/macro_parser.rs
New upstream version 1.43.0+dfsg1
[rustc.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_ast::ast::{Ident, Name};
80 use rustc_ast::ptr::P;
81 use rustc_ast::token::{self, DocComment, Nonterminal, Token};
82 use rustc_ast_pretty::pprust;
83 use rustc_parse::parser::{FollowedByType, Parser, PathStyle};
84 use rustc_session::parse::ParseSess;
85 use rustc_span::symbol::{kw, sym, Symbol};
86
87 use rustc_errors::{FatalError, PResult};
88 use rustc_span::Span;
89 use smallvec::{smallvec, SmallVec};
90
91 use rustc_data_structures::fx::FxHashMap;
92 use rustc_data_structures::sync::Lrc;
93 use std::borrow::Cow;
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 token
616 /// stream from the given `parser` against it and return the match.
617 pub(super) fn parse_tt(parser: &mut Cow<'_, Parser<'_>>, ms: &[TokenTree]) -> NamedParseResult {
618 // A queue of possible matcher positions. We initialize it with the matcher position in which
619 // the "dot" is before the first token of the first token tree in `ms`. `inner_parse_loop` then
620 // processes all of these possible matcher positions and produces possible next positions into
621 // `next_items`. After some post-processing, the contents of `next_items` replenish `cur_items`
622 // and we start over again.
623 //
624 // This MatcherPos instance is allocated on the stack. All others -- and
625 // there are frequently *no* others! -- are allocated on the heap.
626 let mut initial = initial_matcher_pos(ms);
627 let mut cur_items = smallvec![MatcherPosHandle::Ref(&mut initial)];
628 let mut next_items = Vec::new();
629
630 loop {
631 // Matcher positions black-box parsed by parser.rs (`parser`)
632 let mut bb_items = SmallVec::new();
633
634 // Matcher positions that would be valid if the macro invocation was over now
635 let mut eof_items = SmallVec::new();
636 assert!(next_items.is_empty());
637
638 // Process `cur_items` until either we have finished the input or we need to get some
639 // parsing from the black-box parser done. The result is that `next_items` will contain a
640 // bunch of possible next matcher positions in `next_items`.
641 match inner_parse_loop(
642 parser.sess,
643 &mut cur_items,
644 &mut next_items,
645 &mut eof_items,
646 &mut bb_items,
647 &parser.token,
648 ) {
649 Success(_) => {}
650 Failure(token, msg) => return Failure(token, msg),
651 Error(sp, msg) => return Error(sp, msg),
652 }
653
654 // inner parse loop handled all cur_items, so it's empty
655 assert!(cur_items.is_empty());
656
657 // We need to do some post processing after the `inner_parser_loop`.
658 //
659 // Error messages here could be improved with links to original rules.
660
661 // If we reached the EOF, check that there is EXACTLY ONE possible matcher. Otherwise,
662 // either the parse is ambiguous (which should never happen) or there is a syntax error.
663 if parser.token == token::Eof {
664 if eof_items.len() == 1 {
665 let matches =
666 eof_items[0].matches.iter_mut().map(|dv| Lrc::make_mut(dv).pop().unwrap());
667 return nameize(parser.sess, ms, matches);
668 } else if eof_items.len() > 1 {
669 return Error(
670 parser.token.span,
671 "ambiguity: multiple successful parses".to_string(),
672 );
673 } else {
674 return Failure(
675 Token::new(
676 token::Eof,
677 if parser.token.span.is_dummy() {
678 parser.token.span
679 } else {
680 parser.token.span.shrink_to_hi()
681 },
682 ),
683 "missing tokens in macro arguments",
684 );
685 }
686 }
687 // Performance hack: eof_items may share matchers via Rc with other things that we want
688 // to modify. Dropping eof_items now may drop these refcounts to 1, preventing an
689 // unnecessary implicit clone later in Rc::make_mut.
690 drop(eof_items);
691
692 // If there are no possible next positions AND we aren't waiting for the black-box parser,
693 // then there is a syntax error.
694 if bb_items.is_empty() && next_items.is_empty() {
695 return Failure(parser.token.clone(), "no rules expected this token in macro call");
696 }
697 // Another possibility is that we need to call out to parse some rust nonterminal
698 // (black-box) parser. However, if there is not EXACTLY ONE of these, something is wrong.
699 else if (!bb_items.is_empty() && !next_items.is_empty()) || bb_items.len() > 1 {
700 let nts = bb_items
701 .iter()
702 .map(|item| match item.top_elts.get_tt(item.idx) {
703 TokenTree::MetaVarDecl(_, bind, name) => format!("{} ('{}')", name, bind),
704 _ => panic!(),
705 })
706 .collect::<Vec<String>>()
707 .join(" or ");
708
709 return Error(
710 parser.token.span,
711 format!(
712 "local ambiguity: multiple parsing options: {}",
713 match next_items.len() {
714 0 => format!("built-in NTs {}.", nts),
715 1 => format!("built-in NTs {} or 1 other option.", nts),
716 n => format!("built-in NTs {} or {} other options.", nts, n),
717 }
718 ),
719 );
720 }
721 // Dump all possible `next_items` into `cur_items` for the next iteration.
722 else if !next_items.is_empty() {
723 // Now process the next token
724 cur_items.extend(next_items.drain(..));
725 parser.to_mut().bump();
726 }
727 // Finally, we have the case where we need to call the black-box parser to get some
728 // nonterminal.
729 else {
730 assert_eq!(bb_items.len(), 1);
731
732 let mut item = bb_items.pop().unwrap();
733 if let TokenTree::MetaVarDecl(span, _, ident) = item.top_elts.get_tt(item.idx) {
734 let match_cur = item.match_cur;
735 item.push_match(
736 match_cur,
737 MatchedNonterminal(Lrc::new(parse_nt(parser.to_mut(), span, ident.name))),
738 );
739 item.idx += 1;
740 item.match_cur += 1;
741 } else {
742 unreachable!()
743 }
744 cur_items.push(item);
745 }
746
747 assert!(!cur_items.is_empty());
748 }
749 }
750
751 /// The token is an identifier, but not `_`.
752 /// We prohibit passing `_` to macros expecting `ident` for now.
753 fn get_macro_ident(token: &Token) -> Option<(Ident, bool)> {
754 token.ident().filter(|(ident, _)| ident.name != kw::Underscore)
755 }
756
757 /// Checks whether a non-terminal may begin with a particular token.
758 ///
759 /// Returning `false` is a *stability guarantee* that such a matcher will *never* begin with that
760 /// token. Be conservative (return true) if not sure.
761 fn may_begin_with(token: &Token, name: Name) -> bool {
762 /// Checks whether the non-terminal may contain a single (non-keyword) identifier.
763 fn may_be_ident(nt: &token::Nonterminal) -> bool {
764 match *nt {
765 token::NtItem(_) | token::NtBlock(_) | token::NtVis(_) | token::NtLifetime(_) => false,
766 _ => true,
767 }
768 }
769
770 match name {
771 sym::expr => {
772 token.can_begin_expr()
773 // This exception is here for backwards compatibility.
774 && !token.is_keyword(kw::Let)
775 }
776 sym::ty => token.can_begin_type(),
777 sym::ident => get_macro_ident(token).is_some(),
778 sym::literal => token.can_begin_literal_maybe_minus(),
779 sym::vis => match token.kind {
780 // The follow-set of :vis + "priv" keyword + interpolated
781 token::Comma | token::Ident(..) | token::Interpolated(_) => true,
782 _ => token.can_begin_type(),
783 },
784 sym::block => match token.kind {
785 token::OpenDelim(token::Brace) => true,
786 token::Interpolated(ref nt) => match **nt {
787 token::NtItem(_)
788 | token::NtPat(_)
789 | token::NtTy(_)
790 | token::NtIdent(..)
791 | token::NtMeta(_)
792 | token::NtPath(_)
793 | token::NtVis(_) => false, // none of these may start with '{'.
794 _ => true,
795 },
796 _ => false,
797 },
798 sym::path | sym::meta => match token.kind {
799 token::ModSep | token::Ident(..) => true,
800 token::Interpolated(ref nt) => match **nt {
801 token::NtPath(_) | token::NtMeta(_) => true,
802 _ => may_be_ident(&nt),
803 },
804 _ => false,
805 },
806 sym::pat => match token.kind {
807 token::Ident(..) | // box, ref, mut, and other identifiers (can stricten)
808 token::OpenDelim(token::Paren) | // tuple pattern
809 token::OpenDelim(token::Bracket) | // slice pattern
810 token::BinOp(token::And) | // reference
811 token::BinOp(token::Minus) | // negative literal
812 token::AndAnd | // double reference
813 token::Literal(..) | // literal
814 token::DotDot | // range pattern (future compat)
815 token::DotDotDot | // range pattern (future compat)
816 token::ModSep | // path
817 token::Lt | // path (UFCS constant)
818 token::BinOp(token::Shl) => true, // path (double UFCS)
819 token::Interpolated(ref nt) => may_be_ident(nt),
820 _ => false,
821 },
822 sym::lifetime => match token.kind {
823 token::Lifetime(_) => true,
824 token::Interpolated(ref nt) => match **nt {
825 token::NtLifetime(_) | token::NtTT(_) => true,
826 _ => false,
827 },
828 _ => false,
829 },
830 _ => match token.kind {
831 token::CloseDelim(_) => false,
832 _ => true,
833 },
834 }
835 }
836
837 /// A call to the "black-box" parser to parse some Rust non-terminal.
838 ///
839 /// # Parameters
840 ///
841 /// - `p`: the "black-box" parser to use
842 /// - `sp`: the `Span` we want to parse
843 /// - `name`: the name of the metavar _matcher_ we want to match (e.g., `tt`, `ident`, `block`,
844 /// etc...)
845 ///
846 /// # Returns
847 ///
848 /// The parsed non-terminal.
849 fn parse_nt(p: &mut Parser<'_>, sp: Span, name: Symbol) -> Nonterminal {
850 // FIXME(Centril): Consider moving this to `parser.rs` to make
851 // the visibilities of the methods used below `pub(super)` at most.
852
853 if name == sym::tt {
854 return token::NtTT(p.parse_token_tree());
855 }
856 match parse_nt_inner(p, sp, name) {
857 Ok(nt) => nt,
858 Err(mut err) => {
859 err.emit();
860 FatalError.raise();
861 }
862 }
863 }
864
865 fn parse_nt_inner<'a>(p: &mut Parser<'a>, sp: Span, name: Symbol) -> PResult<'a, Nonterminal> {
866 Ok(match name {
867 sym::item => match p.parse_item()? {
868 Some(i) => token::NtItem(i),
869 None => return Err(p.struct_span_err(p.token.span, "expected an item keyword")),
870 },
871 sym::block => token::NtBlock(p.parse_block()?),
872 sym::stmt => match p.parse_stmt()? {
873 Some(s) => token::NtStmt(s),
874 None => return Err(p.struct_span_err(p.token.span, "expected a statement")),
875 },
876 sym::pat => token::NtPat(p.parse_pat(None)?),
877 sym::expr => token::NtExpr(p.parse_expr()?),
878 sym::literal => token::NtLiteral(p.parse_literal_maybe_minus()?),
879 sym::ty => token::NtTy(p.parse_ty()?),
880 // this could be handled like a token, since it is one
881 sym::ident => {
882 if let Some((ident, is_raw)) = get_macro_ident(&p.token) {
883 p.bump();
884 token::NtIdent(ident, is_raw)
885 } else {
886 let token_str = pprust::token_to_string(&p.token);
887 let msg = &format!("expected ident, found {}", &token_str);
888 return Err(p.struct_span_err(p.token.span, msg));
889 }
890 }
891 sym::path => token::NtPath(p.parse_path(PathStyle::Type)?),
892 sym::meta => token::NtMeta(P(p.parse_attr_item()?)),
893 sym::vis => token::NtVis(p.parse_visibility(FollowedByType::Yes)?),
894 sym::lifetime => {
895 if p.check_lifetime() {
896 token::NtLifetime(p.expect_lifetime().ident)
897 } else {
898 let token_str = pprust::token_to_string(&p.token);
899 let msg = &format!("expected a lifetime, found `{}`", &token_str);
900 return Err(p.struct_span_err(p.token.span, msg));
901 }
902 }
903 // this is not supposed to happen, since it has been checked
904 // when compiling the macro.
905 _ => p.span_bug(sp, "invalid fragment specifier"),
906 })
907 }