2020-12-16 17:34:47 -08:00
|
|
|
//! This is an NFA-based parser, which calls out to the main Rust parser for named non-terminals
|
2017-07-23 11:55:52 +02:00
|
|
|
//! (which it commits to fully when it hits one in a grammar). There's a set of current NFA threads
|
|
|
|
//! and a set of next ones. Instead of NTs, we have a special case for Kleene star. The big-O, in
|
|
|
|
//! pathological cases, is worse than traditional use of NFA or Earley parsing, but it's an easier
|
|
|
|
//! fit for Macro-by-Example-style rules.
|
|
|
|
//!
|
|
|
|
//! (In order to prevent the pathological case, we'd need to lazily construct the resulting
|
|
|
|
//! `NamedMatch`es at the very end. It'd be a pain, and require more memory to keep around old
|
|
|
|
//! items, but it would also save overhead)
|
|
|
|
//!
|
2018-05-27 09:47:04 +09:00
|
|
|
//! We don't say this parser uses the Earley algorithm, because it's unnecessarily inaccurate.
|
2017-07-23 11:55:52 +02:00
|
|
|
//! The macro parser restricts itself to the features of finite state automata. Earley parsers
|
|
|
|
//! can be described as an extension of NFAs with completion rules, prediction rules, and recursion.
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
|
|
|
//! Quick intro to how the parser works:
|
|
|
|
//!
|
|
|
|
//! A 'position' is a dot in the middle of a matcher, usually represented as a
|
|
|
|
//! dot. For example `· a $( a )* a b` is a position, as is `a $( · a )* a b`.
|
|
|
|
//!
|
|
|
|
//! The parser walks through the input a character at a time, maintaining a list
|
2017-07-23 11:55:52 +02:00
|
|
|
//! of threads consistent with the current position in the input string: `cur_items`.
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2017-07-23 11:55:52 +02:00
|
|
|
//! As it processes them, it fills up `eof_items` with threads that would be valid if
|
|
|
|
//! the macro invocation is now over, `bb_items` with threads that are waiting on
|
2019-02-08 14:53:55 +01:00
|
|
|
//! a Rust non-terminal like `$e:expr`, and `next_items` with threads that are waiting
|
2015-10-07 23:11:25 +01:00
|
|
|
//! on a particular token. Most of the logic concerns moving the · through the
|
2017-07-23 11:55:52 +02:00
|
|
|
//! repetitions indicated by Kleene stars. The rules for moving the · without
|
|
|
|
//! consuming any input are called epsilon transitions. It only advances or calls
|
|
|
|
//! out to the real Rust parser when no `cur_items` threads remain.
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2017-05-13 21:40:06 +02:00
|
|
|
//! Example:
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2017-05-13 21:40:06 +02:00
|
|
|
//! ```text, ignore
|
|
|
|
//! Start parsing a a a a b against [· a $( a )* a b].
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2017-05-13 21:40:06 +02:00
|
|
|
//! Remaining input: a a a a b
|
2017-07-23 11:55:52 +02:00
|
|
|
//! next: [· a $( a )* a b]
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2017-05-13 21:40:06 +02:00
|
|
|
//! - - - Advance over an a. - - -
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2017-05-13 21:40:06 +02:00
|
|
|
//! Remaining input: a a a b
|
2014-06-09 13:12:30 -07:00
|
|
|
//! cur: [a · $( a )* a b]
|
|
|
|
//! Descend/Skip (first item).
|
|
|
|
//! next: [a $( · a )* a b] [a $( a )* · a b].
|
|
|
|
//!
|
2017-05-13 21:40:06 +02:00
|
|
|
//! - - - Advance over an a. - - -
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2017-05-13 21:40:06 +02:00
|
|
|
//! Remaining input: a a b
|
2017-07-23 11:55:52 +02:00
|
|
|
//! cur: [a $( a · )* a b] [a $( a )* a · b]
|
|
|
|
//! Follow epsilon transition: Finish/Repeat (first item)
|
2014-06-09 13:12:30 -07:00
|
|
|
//! next: [a $( a )* · a b] [a $( · a )* a b] [a $( a )* a · b]
|
|
|
|
//!
|
2017-05-13 21:40:06 +02:00
|
|
|
//! - - - Advance over an a. - - - (this looks exactly like the last step)
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2017-05-13 21:40:06 +02:00
|
|
|
//! Remaining input: a b
|
2017-07-23 11:55:52 +02:00
|
|
|
//! cur: [a $( a · )* a b] [a $( a )* a · b]
|
|
|
|
//! Follow epsilon transition: Finish/Repeat (first item)
|
2014-06-09 13:12:30 -07:00
|
|
|
//! next: [a $( a )* · a b] [a $( · a )* a b] [a $( a )* a · b]
|
|
|
|
//!
|
2017-05-13 21:40:06 +02:00
|
|
|
//! - - - Advance over an a. - - - (this looks exactly like the last step)
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2017-05-13 21:40:06 +02:00
|
|
|
//! Remaining input: b
|
2017-07-23 11:55:52 +02:00
|
|
|
//! cur: [a $( a · )* a b] [a $( a )* a · b]
|
|
|
|
//! Follow epsilon transition: Finish/Repeat (first item)
|
|
|
|
//! next: [a $( a )* · a b] [a $( · a )* a b] [a $( a )* a · b]
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2017-05-13 21:40:06 +02:00
|
|
|
//! - - - Advance over a b. - - -
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2017-05-13 21:40:06 +02:00
|
|
|
//! Remaining input: ''
|
2014-06-09 13:12:30 -07:00
|
|
|
//! eof: [a $( a )* a b ·]
|
2017-05-13 21:40:06 +02:00
|
|
|
//! ```
|
2014-06-09 13:12:30 -07:00
|
|
|
|
2019-09-22 17:42:17 +03:00
|
|
|
crate use NamedMatch::*;
|
|
|
|
crate use ParseResult::*;
|
2019-02-07 02:33:01 +09:00
|
|
|
|
2022-03-19 16:20:07 +11:00
|
|
|
use crate::mbe::{self, SequenceRepetition, TokenTree};
|
2019-10-16 10:59:30 +02:00
|
|
|
|
2020-08-01 17:45:17 +02:00
|
|
|
use rustc_ast::token::{self, DocComment, Nonterminal, Token};
|
2020-12-28 16:57:13 -06:00
|
|
|
use rustc_parse::parser::Parser;
|
2020-01-11 15:03:15 +01:00
|
|
|
use rustc_session::parse::ParseSess;
|
2020-12-28 16:57:13 -06:00
|
|
|
use rustc_span::symbol::MacroRulesNormalizedIdent;
|
2019-02-07 02:33:01 +09:00
|
|
|
|
|
|
|
use smallvec::{smallvec, SmallVec};
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2018-08-18 13:55:43 +03:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2019-02-15 12:36:10 +11:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2021-06-07 20:17:48 -05:00
|
|
|
use rustc_span::symbol::Ident;
|
2020-02-05 09:44:03 +11:00
|
|
|
use std::borrow::Cow;
|
2018-08-18 13:55:43 +03:00
|
|
|
use std::collections::hash_map::Entry::{Occupied, Vacant};
|
2014-10-06 23:00:56 +01:00
|
|
|
use std::mem;
|
2012-06-12 10:59:50 -07:00
|
|
|
|
2018-01-24 22:03:57 -06:00
|
|
|
/// An unzipping of `TokenTree`s... see the `stack` field of `MatcherPos`.
|
|
|
|
///
|
2022-03-09 14:34:24 +11:00
|
|
|
/// This is used by `parse_tt_inner` to keep track of delimited submatchers that we have
|
2018-01-24 22:03:57 -06:00
|
|
|
/// descended into.
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone)]
|
2018-11-12 09:18:57 +11:00
|
|
|
struct MatcherTtFrame<'tt> {
|
2018-01-24 22:03:57 -06:00
|
|
|
/// The "parent" matcher that we are descending into.
|
2022-03-19 16:20:07 +11:00
|
|
|
elts: &'tt [TokenTree],
|
2018-01-24 22:03:57 -06:00
|
|
|
/// The position of the "dot" in `elts` at the time we descended.
|
2015-01-17 23:33:05 +00:00
|
|
|
idx: usize,
|
2014-10-06 23:00:56 +01:00
|
|
|
}
|
|
|
|
|
2018-11-01 08:41:57 +11:00
|
|
|
type NamedMatchVec = SmallVec<[NamedMatch; 4]>;
|
|
|
|
|
2022-03-23 11:44:13 +11:00
|
|
|
// This type is used a lot. Make sure it doesn't unintentionally get bigger.
|
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
|
|
|
rustc_data_structures::static_assert_size!(NamedMatchVec, 72);
|
|
|
|
|
2018-11-12 09:18:57 +11:00
|
|
|
/// Represents a single "position" (aka "matcher position", aka "item"), as
|
|
|
|
/// described in the module documentation.
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone)]
|
2022-03-19 08:56:24 +11:00
|
|
|
struct MatcherPos<'tt> {
|
2022-03-18 17:39:13 +11:00
|
|
|
/// The token or slice of tokens that make up the matcher. `elts` is short for "elements".
|
2022-03-19 16:20:07 +11:00
|
|
|
top_elts: &'tt [TokenTree],
|
2018-11-12 09:18:57 +11:00
|
|
|
|
2018-01-24 22:03:57 -06:00
|
|
|
/// The position of the "dot" in this matcher
|
2015-01-17 23:33:05 +00:00
|
|
|
idx: usize,
|
2018-11-12 09:18:57 +11:00
|
|
|
|
2018-01-24 22:03:57 -06:00
|
|
|
/// For each named metavar in the matcher, we keep track of token trees matched against the
|
|
|
|
/// metavar by the black box parser. In particular, there may be more than one match per
|
|
|
|
/// metavar if we are in a repetition (each repetition matches each of the variables).
|
|
|
|
/// Moreover, matchers and repetitions can be nested; the `matches` field is shared (hence the
|
|
|
|
/// `Rc`) among all "nested" matchers. `match_lo`, `match_cur`, and `match_hi` keep track of
|
|
|
|
/// the current position of the `self` matcher position in the shared `matches` list.
|
2018-01-24 22:59:11 -06:00
|
|
|
///
|
|
|
|
/// Also, note that while we are descending into a sequence, matchers are given their own
|
|
|
|
/// `matches` vector. Only once we reach the end of a full repetition of the sequence do we add
|
|
|
|
/// all bound matches from the submatcher into the shared top-level `matches` vector. If `sep`
|
|
|
|
/// and `up` are `Some`, then `matches` is _not_ the shared top-level list. Instead, if one
|
|
|
|
/// wants the shared `matches`, one should use `up.matches`.
|
2019-02-15 12:36:10 +11:00
|
|
|
matches: Box<[Lrc<NamedMatchVec>]>,
|
2018-01-24 22:03:57 -06:00
|
|
|
/// The position in `matches` corresponding to the first metavar in this matcher's sequence of
|
|
|
|
/// token trees. In other words, the first metavar in the first token of `top_elts` corresponds
|
|
|
|
/// to `matches[match_lo]`.
|
2015-01-17 23:33:05 +00:00
|
|
|
match_lo: usize,
|
2018-01-24 22:03:57 -06:00
|
|
|
/// The position in `matches` corresponding to the metavar we are currently trying to match
|
|
|
|
/// against the source token stream. `match_lo <= match_cur <= match_hi`.
|
2015-01-17 23:33:05 +00:00
|
|
|
match_cur: usize,
|
2018-01-24 22:03:57 -06:00
|
|
|
/// Similar to `match_lo` except `match_hi` is the position in `matches` of the _last_ metavar
|
|
|
|
/// in this matcher.
|
2015-01-17 23:33:05 +00:00
|
|
|
match_hi: usize,
|
2018-01-24 22:03:57 -06:00
|
|
|
|
2022-03-03 11:04:04 +11:00
|
|
|
/// This field is only used if we are matching a repetition.
|
2022-03-19 08:56:24 +11:00
|
|
|
repetition: Option<MatcherPosRepetition<'tt>>,
|
2018-01-24 22:03:57 -06:00
|
|
|
|
2018-11-12 09:18:57 +11:00
|
|
|
/// Specifically used to "unzip" token trees. By "unzip", we mean to unwrap the delimiters from
|
2018-11-27 02:59:49 +00:00
|
|
|
/// a delimited token tree (e.g., something wrapped in `(` `)`) or to get the contents of a doc
|
2018-11-12 09:18:57 +11:00
|
|
|
/// comment...
|
|
|
|
///
|
2018-11-27 02:59:49 +00:00
|
|
|
/// When matching against matchers with nested delimited submatchers (e.g., `pat ( pat ( .. )
|
2018-01-24 22:03:57 -06:00
|
|
|
/// pat ) pat`), we need to keep track of the matchers we are descending into. This stack does
|
|
|
|
/// that where the bottom of the stack is the outermost matcher.
|
2018-11-12 09:18:57 +11:00
|
|
|
/// Also, throughout the comments, this "descent" is often referred to as "unzipping"...
|
|
|
|
stack: SmallVec<[MatcherTtFrame<'tt>; 1]>,
|
2013-02-21 00:16:31 -08:00
|
|
|
}
|
2012-06-12 10:59:50 -07:00
|
|
|
|
2022-03-03 11:02:43 +11:00
|
|
|
// This type is used a lot. Make sure it doesn't unintentionally get bigger.
|
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
2022-03-19 16:20:07 +11:00
|
|
|
rustc_data_structures::static_assert_size!(MatcherPos<'_>, 136);
|
2022-03-03 11:02:43 +11:00
|
|
|
|
2022-03-19 08:56:24 +11:00
|
|
|
impl<'tt> MatcherPos<'tt> {
|
2022-03-18 14:09:02 +11:00
|
|
|
/// `len` `Vec`s (initially shared and empty) that will store matches of metavars.
|
|
|
|
fn create_matches(len: usize) -> Box<[Lrc<NamedMatchVec>]> {
|
|
|
|
if len == 0 {
|
|
|
|
vec![]
|
|
|
|
} else {
|
|
|
|
let empty_matches = Lrc::new(SmallVec::new());
|
|
|
|
vec![empty_matches; len]
|
|
|
|
}
|
|
|
|
.into_boxed_slice()
|
|
|
|
}
|
|
|
|
|
2022-03-03 12:14:27 +11:00
|
|
|
/// Generates the top-level matcher position in which the "dot" is before the first token of
|
|
|
|
/// the matcher `ms`.
|
|
|
|
fn new(ms: &'tt [TokenTree]) -> Self {
|
|
|
|
let match_idx_hi = count_names(ms);
|
|
|
|
MatcherPos {
|
|
|
|
// Start with the top level matcher given to us.
|
2022-03-19 16:20:07 +11:00
|
|
|
top_elts: ms,
|
2022-03-03 12:14:27 +11:00
|
|
|
|
|
|
|
// The "dot" is before the first token of the matcher.
|
|
|
|
idx: 0,
|
|
|
|
|
|
|
|
// Initialize `matches` to a bunch of empty `Vec`s -- one for each metavar in
|
|
|
|
// `top_elts`. `match_lo` for `top_elts` is 0 and `match_hi` is `match_idx_hi`.
|
|
|
|
// `match_cur` is 0 since we haven't actually matched anything yet.
|
2022-03-18 14:09:02 +11:00
|
|
|
matches: Self::create_matches(match_idx_hi),
|
2022-03-03 12:14:27 +11:00
|
|
|
match_lo: 0,
|
|
|
|
match_cur: 0,
|
|
|
|
match_hi: match_idx_hi,
|
|
|
|
|
|
|
|
// Haven't descended into any delimiters, so this is empty.
|
|
|
|
stack: smallvec![],
|
|
|
|
|
|
|
|
// Haven't descended into any sequences, so this is `None`.
|
|
|
|
repetition: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 16:20:07 +11:00
|
|
|
fn repetition(up: Box<MatcherPos<'tt>>, seq: &'tt SequenceRepetition) -> Self {
|
2022-03-18 14:09:02 +11:00
|
|
|
MatcherPos {
|
2022-03-19 16:20:07 +11:00
|
|
|
top_elts: &seq.tts,
|
2022-03-18 14:09:02 +11:00
|
|
|
idx: 0,
|
|
|
|
matches: Self::create_matches(up.matches.len()),
|
|
|
|
match_lo: up.match_cur,
|
|
|
|
match_cur: up.match_cur,
|
|
|
|
match_hi: up.match_cur + seq.num_captures,
|
|
|
|
repetition: Some(MatcherPosRepetition {
|
|
|
|
up,
|
|
|
|
sep: seq.separator.clone(),
|
|
|
|
seq_op: seq.kleene.op,
|
|
|
|
}),
|
2022-03-19 16:20:07 +11:00
|
|
|
stack: smallvec![],
|
2022-03-18 14:09:02 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Adds `m` as a named match for the `idx`-th metavar.
|
2017-06-08 05:51:32 -06:00
|
|
|
fn push_match(&mut self, idx: usize, m: NamedMatch) {
|
2019-02-15 12:36:10 +11:00
|
|
|
let matches = Lrc::make_mut(&mut self.matches[idx]);
|
2017-06-08 05:51:32 -06:00
|
|
|
matches.push(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:04:04 +11:00
|
|
|
#[derive(Clone)]
|
2022-03-19 08:56:24 +11:00
|
|
|
struct MatcherPosRepetition<'tt> {
|
2022-03-03 11:04:04 +11:00
|
|
|
/// The KleeneOp of this sequence.
|
|
|
|
seq_op: mbe::KleeneOp,
|
|
|
|
|
|
|
|
/// The separator.
|
|
|
|
sep: Option<Token>,
|
|
|
|
|
|
|
|
/// The "parent" matcher position. That is, the matcher position just before we enter the
|
|
|
|
/// sequence.
|
2022-03-19 08:56:24 +11:00
|
|
|
up: Box<MatcherPos<'tt>>,
|
2018-05-18 11:23:31 +10:00
|
|
|
}
|
|
|
|
|
2022-03-19 08:56:24 +11:00
|
|
|
enum EofItems<'tt> {
|
2022-03-03 11:10:21 +11:00
|
|
|
None,
|
2022-03-19 08:56:24 +11:00
|
|
|
One(Box<MatcherPos<'tt>>),
|
2022-03-03 11:10:21 +11:00
|
|
|
Multiple,
|
|
|
|
}
|
|
|
|
|
2018-01-24 22:03:57 -06:00
|
|
|
/// Represents the possible results of an attempted parse.
|
2019-09-22 17:42:17 +03:00
|
|
|
crate enum ParseResult<T> {
|
2018-01-24 22:03:57 -06:00
|
|
|
/// Parsed successfully.
|
|
|
|
Success(T),
|
|
|
|
/// Arm failed to match. If the second parameter is `token::Eof`, it indicates an unexpected
|
|
|
|
/// end of macro invocation. Otherwise, it indicates that no rules expected the given token.
|
2019-06-05 01:17:07 +03:00
|
|
|
Failure(Token, &'static str),
|
2018-01-24 22:03:57 -06:00
|
|
|
/// Fatal error (malformed macro?). Abort compilation.
|
2019-12-31 20:15:40 +03:00
|
|
|
Error(rustc_span::Span, String),
|
2020-03-17 14:13:32 +01:00
|
|
|
ErrorReported,
|
2018-01-24 22:03:57 -06:00
|
|
|
}
|
|
|
|
|
2020-03-11 20:05:19 +00:00
|
|
|
/// A `ParseResult` where the `Success` variant contains a mapping of
|
|
|
|
/// `MacroRulesNormalizedIdent`s to `NamedMatch`es. This represents the mapping
|
|
|
|
/// of metavars to the token trees they bind to.
|
|
|
|
crate type NamedParseResult = ParseResult<FxHashMap<MacroRulesNormalizedIdent, NamedMatch>>;
|
2016-11-07 19:40:00 -07:00
|
|
|
|
2018-01-24 22:03:57 -06:00
|
|
|
/// Count how many metavars are named in the given matcher `ms`.
|
2019-09-22 19:42:52 +03:00
|
|
|
pub(super) fn count_names(ms: &[TokenTree]) -> usize {
|
2014-10-06 23:00:56 +01:00
|
|
|
ms.iter().fold(0, |count, elt| {
|
2019-12-22 17:42:04 -05:00
|
|
|
count
|
2022-03-19 16:20:07 +11:00
|
|
|
+ match elt {
|
|
|
|
TokenTree::Delimited(_, delim) => count_names(delim.inner_tts()),
|
2019-12-22 17:42:04 -05:00
|
|
|
TokenTree::MetaVar(..) => 0,
|
|
|
|
TokenTree::MetaVarDecl(..) => 1,
|
2022-03-14 08:29:20 -03:00
|
|
|
// Panicking here would abort execution because `parse_tree` makes use of this
|
|
|
|
// function. In other words, RHS meta-variable expressions eventually end-up here.
|
|
|
|
//
|
|
|
|
// `0` is still returned to inform that no meta-variable was found. `Meta-variables
|
|
|
|
// != Meta-variable expressions`
|
2022-03-09 16:46:23 -03:00
|
|
|
TokenTree::MetaVarExpr(..) => 0,
|
2022-03-19 16:20:07 +11:00
|
|
|
TokenTree::Sequence(_, seq) => seq.num_captures,
|
2019-12-22 17:42:04 -05:00
|
|
|
TokenTree::Token(..) => 0,
|
|
|
|
}
|
2014-10-06 23:00:56 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-05-12 20:05:39 +02:00
|
|
|
/// `NamedMatch` is a pattern-match result for a single `token::MATCH_NONTERMINAL`:
|
2014-06-09 13:12:30 -07:00
|
|
|
/// so it is associated with a single ident in a parse, and all
|
2019-02-08 14:53:55 +01:00
|
|
|
/// `MatchedNonterminal`s in the `NamedMatch` have the same non-terminal type
|
2017-05-12 20:05:39 +02:00
|
|
|
/// (expr, item, etc). Each leaf in a single `NamedMatch` corresponds to a
|
|
|
|
/// single `token::MATCH_NONTERMINAL` in the `TokenTree` that produced it.
|
2014-06-09 13:12:30 -07:00
|
|
|
///
|
2017-05-12 20:05:39 +02:00
|
|
|
/// The in-memory structure of a particular `NamedMatch` represents the match
|
2014-10-06 23:00:56 +01:00
|
|
|
/// that occurred when a particular subset of a matcher was applied to a
|
|
|
|
/// particular token tree.
|
2014-06-09 13:12:30 -07:00
|
|
|
///
|
2017-05-12 20:05:39 +02:00
|
|
|
/// The width of each `MatchedSeq` in the `NamedMatch`, and the identity of
|
|
|
|
/// the `MatchedNonterminal`s, will depend on the token tree it was applied
|
|
|
|
/// to: each `MatchedSeq` corresponds to a single `TTSeq` in the originating
|
|
|
|
/// token tree. The depth of the `NamedMatch` structure will therefore depend
|
2014-10-07 00:18:24 +01:00
|
|
|
/// only on the nesting depth of `ast::TTSeq`s in the originating
|
|
|
|
/// token tree it was derived from.
|
2022-03-02 21:33:43 -06:00
|
|
|
///
|
|
|
|
/// In layman's terms: `NamedMatch` will form a tree representing nested matches of a particular
|
|
|
|
/// meta variable. For example, if we are matching the following macro against the following
|
|
|
|
/// invocation...
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// macro_rules! foo {
|
|
|
|
/// ($($($x:ident),+);+) => {}
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// foo!(a, b, c, d; a, b, c, d, e);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Then, the tree will have the following shape:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// MatchedSeq([
|
|
|
|
/// MatchedSeq([
|
|
|
|
/// MatchedNonterminal(a),
|
|
|
|
/// MatchedNonterminal(b),
|
|
|
|
/// MatchedNonterminal(c),
|
|
|
|
/// MatchedNonterminal(d),
|
|
|
|
/// ]),
|
|
|
|
/// MatchedSeq([
|
|
|
|
/// MatchedNonterminal(a),
|
|
|
|
/// MatchedNonterminal(b),
|
|
|
|
/// MatchedNonterminal(c),
|
|
|
|
/// MatchedNonterminal(d),
|
|
|
|
/// MatchedNonterminal(e),
|
|
|
|
/// ])
|
|
|
|
/// ])
|
|
|
|
/// ```
|
2017-06-08 05:51:32 -06:00
|
|
|
#[derive(Debug, Clone)]
|
2019-09-22 17:42:17 +03:00
|
|
|
crate enum NamedMatch {
|
2019-12-12 15:48:30 +11:00
|
|
|
MatchedSeq(Lrc<NamedMatchVec>),
|
2019-02-15 12:36:10 +11:00
|
|
|
MatchedNonterminal(Lrc<Nonterminal>),
|
2012-07-27 19:14:46 -07:00
|
|
|
}
|
2012-06-12 10:59:50 -07:00
|
|
|
|
2022-03-18 17:39:13 +11:00
|
|
|
/// Takes a slice of token trees `ms` representing a matcher which successfully matched input
|
2018-01-24 23:23:01 -06:00
|
|
|
/// and an iterator of items that matched input and produces a `NamedParseResult`.
|
2018-01-19 19:00:29 -06:00
|
|
|
fn nameize<I: Iterator<Item = NamedMatch>>(
|
|
|
|
sess: &ParseSess,
|
|
|
|
ms: &[TokenTree],
|
|
|
|
mut res: I,
|
|
|
|
) -> NamedParseResult {
|
2018-11-27 02:59:49 +00:00
|
|
|
// Recursively descend into each type of matcher (e.g., sequences, delimited, metavars) and make
|
2018-01-24 23:23:01 -06:00
|
|
|
// sure that each metavar has _exactly one_ binding. If a metavar does not have exactly one
|
|
|
|
// binding, then there is an error. If it does, then we insert the binding into the
|
|
|
|
// `NamedParseResult`.
|
2018-01-19 19:00:29 -06:00
|
|
|
fn n_rec<I: Iterator<Item = NamedMatch>>(
|
|
|
|
sess: &ParseSess,
|
|
|
|
m: &TokenTree,
|
|
|
|
res: &mut I,
|
2020-03-11 20:05:19 +00:00
|
|
|
ret_val: &mut FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
|
2019-12-31 20:15:40 +03:00
|
|
|
) -> Result<(), (rustc_span::Span, String)> {
|
2015-11-17 23:24:49 +09:00
|
|
|
match *m {
|
2019-12-22 17:42:04 -05:00
|
|
|
TokenTree::Sequence(_, ref seq) => {
|
|
|
|
for next_m in &seq.tts {
|
|
|
|
n_rec(sess, next_m, res.by_ref(), ret_val)?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TokenTree::Delimited(_, ref delim) => {
|
2022-03-19 16:20:07 +11:00
|
|
|
for next_m in delim.inner_tts() {
|
2019-12-22 17:42:04 -05:00
|
|
|
n_rec(sess, next_m, res.by_ref(), ret_val)?;
|
|
|
|
}
|
|
|
|
}
|
2020-12-19 16:30:56 -05:00
|
|
|
TokenTree::MetaVarDecl(span, _, None) => {
|
|
|
|
if sess.missing_fragment_specifiers.borrow_mut().remove(&span).is_some() {
|
|
|
|
return Err((span, "missing fragment specifier".to_string()));
|
|
|
|
}
|
|
|
|
}
|
2020-03-11 20:05:19 +00:00
|
|
|
TokenTree::MetaVarDecl(sp, bind_name, _) => match ret_val
|
|
|
|
.entry(MacroRulesNormalizedIdent::new(bind_name))
|
|
|
|
{
|
2019-12-22 17:42:04 -05:00
|
|
|
Vacant(spot) => {
|
|
|
|
spot.insert(res.next().unwrap());
|
2014-10-06 23:00:56 +01:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
Occupied(..) => return Err((sp, format!("duplicated bind name: {}", bind_name))),
|
|
|
|
},
|
2022-03-09 16:58:13 +11:00
|
|
|
TokenTree::Token(..) => (),
|
|
|
|
TokenTree::MetaVar(..) | TokenTree::MetaVarExpr(..) => unreachable!(),
|
2012-06-27 15:29:35 -07:00
|
|
|
}
|
2015-11-25 20:58:57 +01:00
|
|
|
|
|
|
|
Ok(())
|
2012-06-27 15:29:35 -07:00
|
|
|
}
|
2015-11-25 20:58:57 +01:00
|
|
|
|
2018-08-18 13:55:43 +03:00
|
|
|
let mut ret_val = FxHashMap::default();
|
2015-11-25 20:58:57 +01:00
|
|
|
for m in ms {
|
2017-02-26 03:25:22 +00:00
|
|
|
match n_rec(sess, m, res.by_ref(), &mut ret_val) {
|
2018-01-19 19:00:29 -06:00
|
|
|
Ok(_) => {}
|
2015-11-25 20:58:57 +01:00
|
|
|
Err((sp, msg)) => return Error(sp, msg),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Success(ret_val)
|
2012-06-27 15:29:35 -07:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Performs a token equality check, ignoring syntax context (that is, an unhygienic comparison)
|
2019-06-08 19:45:12 +03:00
|
|
|
fn token_name_eq(t1: &Token, t2: &Token) -> bool {
|
|
|
|
if let (Some((ident1, is_raw1)), Some((ident2, is_raw2))) = (t1.ident(), t2.ident()) {
|
|
|
|
ident1.name == ident2.name && is_raw1 == is_raw2
|
|
|
|
} else if let (Some(ident1), Some(ident2)) = (t1.lifetime(), t2.lifetime()) {
|
|
|
|
ident1.name == ident2.name
|
2017-05-15 09:26:26 +00:00
|
|
|
} else {
|
2019-06-08 19:45:12 +03:00
|
|
|
t1.kind == t2.kind
|
2013-09-05 14:14:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 09:53:41 +11:00
|
|
|
// Note: the item vectors could be created and dropped within `parse_tt`, but to avoid excess
|
|
|
|
// allocations we have a single vector fo each kind that is cleared and reused repeatedly.
|
|
|
|
pub struct TtParser<'tt> {
|
2022-03-19 08:03:48 +11:00
|
|
|
macro_name: Ident,
|
2022-03-19 08:56:24 +11:00
|
|
|
|
2022-03-19 09:53:41 +11:00
|
|
|
/// The set of current items to be processed. This should be empty by the end of a successful
|
|
|
|
/// execution of `parse_tt_inner`.
|
2022-03-19 08:56:24 +11:00
|
|
|
cur_items: Vec<Box<MatcherPos<'tt>>>,
|
2022-03-19 09:53:41 +11:00
|
|
|
|
|
|
|
/// The set of newly generated items. These are used to replenish `cur_items` in the function
|
|
|
|
/// `parse_tt`.
|
2022-03-19 08:56:24 +11:00
|
|
|
next_items: Vec<Box<MatcherPos<'tt>>>,
|
2022-03-19 09:53:41 +11:00
|
|
|
|
|
|
|
/// The set of items that are waiting for the black-box parser.
|
2022-03-19 08:56:24 +11:00
|
|
|
bb_items: Vec<Box<MatcherPos<'tt>>>,
|
2022-03-19 08:03:48 +11:00
|
|
|
}
|
2022-03-19 07:47:22 +11:00
|
|
|
|
2022-03-19 09:53:41 +11:00
|
|
|
impl<'tt> TtParser<'tt> {
|
2022-03-19 16:20:07 +11:00
|
|
|
pub(super) fn new(macro_name: Ident) -> TtParser<'tt> {
|
|
|
|
TtParser { macro_name, cur_items: vec![], next_items: vec![], bb_items: vec![] }
|
2022-03-19 08:03:48 +11:00
|
|
|
}
|
|
|
|
|
2022-03-19 07:47:22 +11:00
|
|
|
/// Process the matcher positions of `cur_items` until it is empty. In the process, this will
|
|
|
|
/// produce more items in `next_items` and `bb_items`.
|
|
|
|
///
|
|
|
|
/// For more info about the how this happens, see the module-level doc comments and the inline
|
|
|
|
/// comments of this function.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
///
|
|
|
|
/// `Some(result)` if everything is finished, `None` otherwise. Note that matches are kept
|
|
|
|
/// track of through the items generated.
|
2022-03-19 09:53:41 +11:00
|
|
|
fn parse_tt_inner(
|
|
|
|
&mut self,
|
2022-03-19 07:47:22 +11:00
|
|
|
sess: &ParseSess,
|
|
|
|
ms: &[TokenTree],
|
|
|
|
token: &Token,
|
|
|
|
) -> Option<NamedParseResult> {
|
|
|
|
// Matcher positions that would be valid if the macro invocation was over now. Only
|
|
|
|
// modified if `token == Eof`.
|
|
|
|
let mut eof_items = EofItems::None;
|
|
|
|
|
2022-03-19 09:53:41 +11:00
|
|
|
while let Some(mut item) = self.cur_items.pop() {
|
2022-03-19 07:47:22 +11:00
|
|
|
// When unzipped trees end, remove them. This corresponds to backtracking out of a
|
|
|
|
// delimited submatcher into which we already descended. When backtracking out again, we
|
|
|
|
// need to advance the "dot" past the delimiters in the outer matcher.
|
|
|
|
while item.idx >= item.top_elts.len() {
|
|
|
|
match item.stack.pop() {
|
|
|
|
Some(MatcherTtFrame { elts, idx }) => {
|
|
|
|
item.top_elts = elts;
|
|
|
|
item.idx = idx + 1;
|
|
|
|
}
|
|
|
|
None => break,
|
2014-10-06 23:00:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 07:47:22 +11:00
|
|
|
// Get the current position of the "dot" (`idx`) in `item` and the number of token
|
|
|
|
// trees in the matcher (`len`).
|
|
|
|
let idx = item.idx;
|
|
|
|
let len = item.top_elts.len();
|
|
|
|
|
|
|
|
if idx < len {
|
|
|
|
// We are in the middle of a matcher. Compare the matcher's current tt against
|
|
|
|
// `token`.
|
2022-03-19 16:20:07 +11:00
|
|
|
match &item.top_elts[idx] {
|
|
|
|
TokenTree::Sequence(_sp, seq) => {
|
2022-03-19 07:47:22 +11:00
|
|
|
let op = seq.kleene.op;
|
|
|
|
if op == mbe::KleeneOp::ZeroOrMore || op == mbe::KleeneOp::ZeroOrOne {
|
|
|
|
// Allow for the possibility of zero matches of this sequence.
|
|
|
|
let mut new_item = item.clone();
|
|
|
|
new_item.match_cur += seq.num_captures;
|
|
|
|
new_item.idx += 1;
|
|
|
|
for idx in item.match_cur..item.match_cur + seq.num_captures {
|
|
|
|
new_item.push_match(idx, MatchedSeq(Lrc::new(smallvec![])));
|
|
|
|
}
|
2022-03-19 09:53:41 +11:00
|
|
|
self.cur_items.push(new_item);
|
2014-10-06 23:00:56 +01:00
|
|
|
}
|
2022-03-19 07:47:22 +11:00
|
|
|
|
|
|
|
// Allow for the possibility of one or more matches of this sequence.
|
2022-03-19 16:20:07 +11:00
|
|
|
self.cur_items.push(box MatcherPos::repetition(item, &seq));
|
2014-10-06 23:15:12 -07:00
|
|
|
}
|
2016-11-11 16:28:47 -07:00
|
|
|
|
2022-03-19 16:20:07 +11:00
|
|
|
&TokenTree::MetaVarDecl(span, _, None) => {
|
2022-03-19 07:47:22 +11:00
|
|
|
// E.g. `$e` instead of `$e:expr`.
|
|
|
|
if sess.missing_fragment_specifiers.borrow_mut().remove(&span).is_some() {
|
|
|
|
return Some(Error(span, "missing fragment specifier".to_string()));
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 23:10:39 -06:00
|
|
|
|
2022-03-19 16:20:07 +11:00
|
|
|
&TokenTree::MetaVarDecl(_, _, Some(kind)) => {
|
2022-03-19 07:47:22 +11:00
|
|
|
// Built-in nonterminals never start with these tokens, so we can eliminate
|
|
|
|
// them from consideration.
|
|
|
|
//
|
|
|
|
// We use the span of the metavariable declaration to determine any
|
|
|
|
// edition-specific matching behavior for non-terminals.
|
|
|
|
if Parser::nonterminal_may_begin_with(kind, token) {
|
2022-03-19 09:53:41 +11:00
|
|
|
self.bb_items.push(item);
|
2022-03-19 07:47:22 +11:00
|
|
|
}
|
2020-12-19 16:30:56 -05:00
|
|
|
}
|
|
|
|
|
2022-03-19 16:20:07 +11:00
|
|
|
TokenTree::Delimited(_, delimited) => {
|
2022-03-19 08:07:04 +11:00
|
|
|
// To descend into a delimited submatcher, we push the current matcher onto
|
|
|
|
// a stack and push a new item containing the submatcher onto `cur_items`.
|
2022-03-19 07:47:22 +11:00
|
|
|
//
|
|
|
|
// At the beginning of the loop, if we reach the end of the delimited
|
2022-03-19 16:20:07 +11:00
|
|
|
// submatcher, we pop the stack to backtrack out of the descent. Note that
|
|
|
|
// we use `all_tts` to include the open and close delimiter tokens.
|
|
|
|
let lower_elts = mem::replace(&mut item.top_elts, &delimited.all_tts);
|
2022-03-19 07:47:22 +11:00
|
|
|
let idx = item.idx;
|
|
|
|
item.stack.push(MatcherTtFrame { elts: lower_elts, idx });
|
|
|
|
item.idx = 0;
|
2022-03-19 09:53:41 +11:00
|
|
|
self.cur_items.push(item);
|
2014-10-06 23:00:56 +01:00
|
|
|
}
|
2018-01-24 23:10:39 -06:00
|
|
|
|
2022-03-19 07:47:22 +11:00
|
|
|
TokenTree::Token(t) => {
|
2022-03-19 08:07:04 +11:00
|
|
|
// Doc comments cannot appear in a matcher.
|
|
|
|
debug_assert!(!matches!(t, Token { kind: DocComment(..), .. }));
|
|
|
|
|
2022-03-19 07:47:22 +11:00
|
|
|
// If the token matches, we can just advance the parser. Otherwise, this
|
|
|
|
// match hash failed, there is nothing to do, and hopefully another item in
|
|
|
|
// `cur_items` will match.
|
|
|
|
if token_name_eq(&t, token) {
|
|
|
|
item.idx += 1;
|
2022-03-19 09:53:41 +11:00
|
|
|
self.next_items.push(item);
|
2022-03-19 07:47:22 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These cannot appear in a matcher.
|
|
|
|
TokenTree::MetaVar(..) | TokenTree::MetaVarExpr(..) => unreachable!(),
|
|
|
|
}
|
|
|
|
} else if let Some(repetition) = &item.repetition {
|
|
|
|
// We are past the end of a repetition.
|
|
|
|
debug_assert!(idx <= len + 1);
|
|
|
|
|
|
|
|
if idx == len {
|
|
|
|
// Add all matches from the sequence to `up`, and move the "dot" past the
|
|
|
|
// repetition in `up`. This allows for the case where the sequence matching is
|
|
|
|
// finished.
|
|
|
|
let mut new_pos = repetition.up.clone();
|
|
|
|
for idx in item.match_lo..item.match_hi {
|
|
|
|
let sub = item.matches[idx].clone();
|
|
|
|
new_pos.push_match(idx, MatchedSeq(sub));
|
|
|
|
}
|
|
|
|
new_pos.match_cur = item.match_hi;
|
|
|
|
new_pos.idx += 1;
|
2022-03-19 09:53:41 +11:00
|
|
|
self.cur_items.push(new_pos);
|
2016-11-11 16:28:47 -07:00
|
|
|
}
|
2018-01-24 23:10:39 -06:00
|
|
|
|
2022-03-19 07:47:22 +11:00
|
|
|
if idx == len && repetition.sep.is_some() {
|
|
|
|
if repetition.sep.as_ref().map_or(false, |sep| token_name_eq(token, sep)) {
|
|
|
|
// The matcher has a separator, and it matches the current token. We can
|
|
|
|
// advance past the separator token.
|
2022-03-18 17:13:41 +11:00
|
|
|
item.idx += 1;
|
2022-03-19 09:53:41 +11:00
|
|
|
self.next_items.push(item);
|
2022-03-18 17:13:41 +11:00
|
|
|
}
|
2022-03-19 07:47:22 +11:00
|
|
|
} else if repetition.seq_op != mbe::KleeneOp::ZeroOrOne {
|
|
|
|
// We don't need a separator. Move the "dot" back to the beginning of the
|
|
|
|
// matcher and try to match again UNLESS we are only allowed to have _one_
|
|
|
|
// repetition.
|
|
|
|
item.match_cur = item.match_lo;
|
|
|
|
item.idx = 0;
|
2022-03-19 09:53:41 +11:00
|
|
|
self.cur_items.push(item);
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
2022-03-19 07:47:22 +11:00
|
|
|
} else {
|
|
|
|
// We are past the end of the matcher, and not in a repetition. Look for end of
|
|
|
|
// input.
|
|
|
|
debug_assert_eq!(idx, len);
|
|
|
|
if *token == token::Eof {
|
|
|
|
eof_items = match eof_items {
|
|
|
|
EofItems::None => EofItems::One(item),
|
|
|
|
EofItems::One(_) | EofItems::Multiple => EofItems::Multiple,
|
|
|
|
}
|
2022-03-18 14:16:45 +11:00
|
|
|
}
|
|
|
|
}
|
2022-03-19 07:47:22 +11:00
|
|
|
}
|
2022-03-18 14:16:45 +11:00
|
|
|
|
2022-03-19 07:47:22 +11:00
|
|
|
// If we reached the end of input, check that there is EXACTLY ONE possible matcher.
|
|
|
|
// Otherwise, either the parse is ambiguous (which is an error) or there is a syntax error.
|
|
|
|
if *token == token::Eof {
|
|
|
|
Some(match eof_items {
|
|
|
|
EofItems::One(mut eof_item) => {
|
|
|
|
let matches =
|
|
|
|
eof_item.matches.iter_mut().map(|dv| Lrc::make_mut(dv).pop().unwrap());
|
|
|
|
nameize(sess, ms, matches)
|
2022-03-18 14:16:45 +11:00
|
|
|
}
|
2022-03-19 07:47:22 +11:00
|
|
|
EofItems::Multiple => {
|
|
|
|
Error(token.span, "ambiguity: multiple successful parses".to_string())
|
2022-03-18 14:16:45 +11:00
|
|
|
}
|
2022-03-19 07:47:22 +11:00
|
|
|
EofItems::None => Failure(
|
|
|
|
Token::new(
|
|
|
|
token::Eof,
|
|
|
|
if token.span.is_dummy() { token.span } else { token.span.shrink_to_hi() },
|
|
|
|
),
|
|
|
|
"missing tokens in macro arguments",
|
|
|
|
),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
2016-11-11 16:28:47 -07:00
|
|
|
}
|
|
|
|
|
2022-03-19 07:47:22 +11:00
|
|
|
/// Use the given slice of token trees (`ms`) as a matcher. Match the token stream from the
|
|
|
|
/// given `parser` against it and return the match.
|
|
|
|
pub(super) fn parse_tt(
|
2022-03-19 09:53:41 +11:00
|
|
|
&mut self,
|
2022-03-19 07:47:22 +11:00
|
|
|
parser: &mut Cow<'_, Parser<'_>>,
|
2022-03-19 09:53:41 +11:00
|
|
|
ms: &'tt [TokenTree],
|
2022-03-19 07:47:22 +11:00
|
|
|
) -> NamedParseResult {
|
|
|
|
// A queue of possible matcher positions. We initialize it with the matcher position in
|
|
|
|
// which the "dot" is before the first token of the first token tree in `ms`.
|
|
|
|
// `parse_tt_inner` then processes all of these possible matcher positions and produces
|
|
|
|
// possible next positions into `next_items`. After some post-processing, the contents of
|
|
|
|
// `next_items` replenish `cur_items` and we start over again.
|
2022-03-19 09:53:41 +11:00
|
|
|
self.cur_items.clear();
|
|
|
|
self.cur_items.push(box MatcherPos::new(ms));
|
2022-03-19 07:47:22 +11:00
|
|
|
|
|
|
|
loop {
|
2022-03-19 09:53:41 +11:00
|
|
|
self.next_items.clear();
|
|
|
|
self.bb_items.clear();
|
2022-03-19 07:47:22 +11:00
|
|
|
|
|
|
|
// Process `cur_items` until either we have finished the input or we need to get some
|
|
|
|
// parsing from the black-box parser done.
|
2022-03-19 09:53:41 +11:00
|
|
|
if let Some(result) = self.parse_tt_inner(parser.sess, ms, &parser.token) {
|
2022-03-19 07:47:22 +11:00
|
|
|
return result;
|
2022-03-09 14:34:24 +11:00
|
|
|
}
|
2016-11-11 16:28:47 -07:00
|
|
|
|
2022-03-19 07:47:22 +11:00
|
|
|
// `parse_tt_inner` handled all cur_items, so it's empty.
|
2022-03-19 09:53:41 +11:00
|
|
|
assert!(self.cur_items.is_empty());
|
2022-03-19 07:47:22 +11:00
|
|
|
|
|
|
|
// Error messages here could be improved with links to original rules.
|
2022-03-19 09:53:41 +11:00
|
|
|
match (self.next_items.len(), self.bb_items.len()) {
|
2022-03-19 07:47:22 +11:00
|
|
|
(0, 0) => {
|
|
|
|
// There are no possible next positions AND we aren't waiting for the black-box
|
|
|
|
// parser: syntax error.
|
|
|
|
return Failure(
|
|
|
|
parser.token.clone(),
|
|
|
|
"no rules expected this token in macro call",
|
|
|
|
);
|
|
|
|
}
|
2012-06-12 10:59:50 -07:00
|
|
|
|
2022-03-19 07:47:22 +11:00
|
|
|
(_, 0) => {
|
|
|
|
// Dump all possible `next_items` into `cur_items` for the next iteration. Then
|
|
|
|
// process the next token.
|
2022-03-19 09:53:41 +11:00
|
|
|
self.cur_items.extend(self.next_items.drain(..));
|
2022-03-19 07:47:22 +11:00
|
|
|
parser.to_mut().bump();
|
|
|
|
}
|
2022-03-03 11:00:50 +11:00
|
|
|
|
2022-03-19 07:47:22 +11:00
|
|
|
(0, 1) => {
|
|
|
|
// We need to call the black-box parser to get some nonterminal.
|
2022-03-19 09:53:41 +11:00
|
|
|
let mut item = self.bb_items.pop().unwrap();
|
2022-03-19 16:20:07 +11:00
|
|
|
if let TokenTree::MetaVarDecl(span, _, Some(kind)) = item.top_elts[item.idx] {
|
2022-03-19 07:47:22 +11:00
|
|
|
let match_cur = item.match_cur;
|
|
|
|
// We use the span of the metavariable declaration to determine any
|
|
|
|
// edition-specific matching behavior for non-terminals.
|
|
|
|
let nt = match parser.to_mut().parse_nonterminal(kind) {
|
|
|
|
Err(mut err) => {
|
|
|
|
err.span_label(
|
|
|
|
span,
|
|
|
|
format!(
|
|
|
|
"while parsing argument for this `{kind}` macro fragment"
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
return ErrorReported;
|
|
|
|
}
|
|
|
|
Ok(nt) => nt,
|
|
|
|
};
|
|
|
|
item.push_match(match_cur, MatchedNonterminal(Lrc::new(nt)));
|
|
|
|
item.idx += 1;
|
|
|
|
item.match_cur += 1;
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
2022-03-19 09:53:41 +11:00
|
|
|
self.cur_items.push(item);
|
2022-03-19 07:47:22 +11:00
|
|
|
}
|
2022-03-03 11:00:50 +11:00
|
|
|
|
2022-03-19 07:47:22 +11:00
|
|
|
(_, _) => {
|
|
|
|
// Too many possibilities!
|
2022-03-19 09:53:41 +11:00
|
|
|
return self.ambiguity_error(parser.token.span);
|
2022-03-09 14:18:32 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 09:53:41 +11:00
|
|
|
assert!(!self.cur_items.is_empty());
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
|
|
|
}
|
2022-03-09 14:51:31 +11:00
|
|
|
|
2022-03-19 09:53:41 +11:00
|
|
|
fn ambiguity_error(&self, token_span: rustc_span::Span) -> NamedParseResult {
|
|
|
|
let nts = self
|
|
|
|
.bb_items
|
2022-03-19 07:47:22 +11:00
|
|
|
.iter()
|
2022-03-19 16:20:07 +11:00
|
|
|
.map(|item| match item.top_elts[item.idx] {
|
2022-03-19 07:47:22 +11:00
|
|
|
TokenTree::MetaVarDecl(_, bind, Some(kind)) => {
|
|
|
|
format!("{} ('{}')", kind, bind)
|
|
|
|
}
|
|
|
|
_ => panic!(),
|
|
|
|
})
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(" or ");
|
|
|
|
|
|
|
|
Error(
|
|
|
|
token_span,
|
|
|
|
format!(
|
2022-03-19 08:03:48 +11:00
|
|
|
"local ambiguity when calling macro `{}`: multiple parsing options: {}",
|
|
|
|
self.macro_name,
|
2022-03-19 09:53:41 +11:00
|
|
|
match self.next_items.len() {
|
2022-03-19 07:47:22 +11:00
|
|
|
0 => format!("built-in NTs {}.", nts),
|
|
|
|
1 => format!("built-in NTs {} or 1 other option.", nts),
|
|
|
|
n => format!("built-in NTs {} or {} other options.", nts, n),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2022-03-09 14:51:31 +11:00
|
|
|
}
|