From ac0c16d3b5cc5644b3311811e127411b87f3abf0 Mon Sep 17 00:00:00 2001 From: Mark Mansi Date: Fri, 19 Jan 2018 19:00:29 -0600 Subject: [PATCH 1/7] Run rustfmt on /libsyntax/ext/tt/macro_parser.rs --- src/libsyntax/ext/tt/macro_parser.rs | 192 ++++++++++++++++----------- 1 file changed, 114 insertions(+), 78 deletions(-) diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 3e3c1618fffb2..a5b573f18db1d 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -90,8 +90,8 @@ use codemap::Spanned; use errors::FatalError; use ext::tt::quoted::{self, TokenTree}; use parse::{Directory, ParseSess}; -use parse::parser::{PathStyle, Parser}; -use parse::token::{self, DocComment, Token, Nonterminal}; +use parse::parser::{Parser, PathStyle}; +use parse::token::{self, DocComment, Nonterminal, Token}; use print::pprust; use symbol::keywords; use tokenstream::TokenStream; @@ -100,7 +100,7 @@ use util::small_vector::SmallVector; use std::mem; use std::rc::Rc; use std::collections::HashMap; -use std::collections::hash_map::Entry::{Vacant, Occupied}; +use std::collections::hash_map::Entry::{Occupied, Vacant}; // To avoid costly uniqueness checks, we require that `MatchSeq` always has // a nonempty body. @@ -182,7 +182,7 @@ fn initial_matcher_pos(ms: Vec, lo: BytePos) -> Box { match_lo: 0, match_cur: 0, match_hi: match_idx_hi, - sp_lo: lo + sp_lo: lo, }) } @@ -206,25 +206,27 @@ fn initial_matcher_pos(ms: Vec, lo: BytePos) -> Box { #[derive(Debug, Clone)] pub enum NamedMatch { MatchedSeq(Rc>, syntax_pos::Span), - MatchedNonterminal(Rc) + MatchedNonterminal(Rc), } -fn nameize>(sess: &ParseSess, ms: &[TokenTree], mut res: I) - -> NamedParseResult { - fn n_rec>(sess: &ParseSess, m: &TokenTree, res: &mut I, - ret_val: &mut HashMap>) - -> Result<(), (syntax_pos::Span, String)> { +fn nameize>( + sess: &ParseSess, + ms: &[TokenTree], + mut res: I, +) -> NamedParseResult { + fn n_rec>( + sess: &ParseSess, + m: &TokenTree, + res: &mut I, + ret_val: &mut HashMap>, + ) -> Result<(), (syntax_pos::Span, String)> { match *m { - TokenTree::Sequence(_, ref seq) => { - for next_m in &seq.tts { - n_rec(sess, next_m, res.by_ref(), ret_val)? - } - } - TokenTree::Delimited(_, ref delim) => { - for next_m in &delim.tts { - n_rec(sess, next_m, res.by_ref(), ret_val)?; - } - } + TokenTree::Sequence(_, ref seq) => for next_m in &seq.tts { + n_rec(sess, next_m, res.by_ref(), ret_val)? + }, + TokenTree::Delimited(_, ref delim) => for next_m in &delim.tts { + n_rec(sess, next_m, res.by_ref(), ret_val)?; + }, TokenTree::MetaVarDecl(span, _, id) if id.name == keywords::Invalid.name() => { if sess.missing_fragment_specifiers.borrow_mut().remove(&span) { return Err((span, "missing fragment specifier".to_string())); @@ -250,7 +252,7 @@ fn nameize>(sess: &ParseSess, ms: &[TokenTree], mut let mut ret_val = HashMap::new(); for m in ms { match n_rec(sess, m, res.by_ref(), &mut ret_val) { - Ok(_) => {}, + Ok(_) => {} Err((sp, msg)) => return Error(sp, msg), } } @@ -265,18 +267,21 @@ pub enum ParseResult { /// indicates that no rules expected the given token. Failure(syntax_pos::Span, Token), /// Fatal error (malformed macro?). Abort compilation. - Error(syntax_pos::Span, String) + Error(syntax_pos::Span, String), } pub fn parse_failure_msg(tok: Token) -> String { match tok { token::Eof => "unexpected end of macro invocation".to_string(), - _ => format!("no rules expected the token `{}`", pprust::token_to_string(&tok)), + _ => format!( + "no rules expected the token `{}`", + pprust::token_to_string(&tok) + ), } } /// Perform a token equality check, ignoring syntax context (that is, an unhygienic comparison) -fn token_name_eq(t1 : &Token, t2 : &Token) -> bool { +fn token_name_eq(t1: &Token, t2: &Token) -> bool { if let (Some(id1), Some(id2)) = (t1.ident(), t2.ident()) { id1.name == id2.name } else if let (&token::Lifetime(id1), &token::Lifetime(id2)) = (t1, t2) { @@ -290,14 +295,15 @@ fn create_matches(len: usize) -> Vec>> { (0..len).into_iter().map(|_| Rc::new(Vec::new())).collect() } -fn inner_parse_loop(sess: &ParseSess, - cur_items: &mut SmallVector>, - next_items: &mut Vec>, - eof_items: &mut SmallVector>, - bb_items: &mut SmallVector>, - token: &Token, - span: syntax_pos::Span) - -> ParseResult<()> { +fn inner_parse_loop( + sess: &ParseSess, + cur_items: &mut SmallVector>, + next_items: &mut Vec>, + eof_items: &mut SmallVector>, + bb_items: &mut SmallVector>, + token: &Token, + span: syntax_pos::Span, +) -> ParseResult<()> { while let Some(mut item) = cur_items.pop() { // When unzipped trees end, remove them while item.idx >= item.top_elts.len() { @@ -306,7 +312,7 @@ fn inner_parse_loop(sess: &ParseSess, item.top_elts = elts; item.idx = idx + 1; } - None => break + None => break, } } @@ -341,11 +347,16 @@ fn inner_parse_loop(sess: &ParseSess, // Check if we need a separator if idx == len && item.sep.is_some() { // We have a separator, and it is the current token. - if item.sep.as_ref().map(|sep| token_name_eq(token, sep)).unwrap_or(false) { + if item.sep + .as_ref() + .map(|sep| token_name_eq(token, sep)) + .unwrap_or(false) + { item.idx += 1; next_items.push(item); } - } else { // we don't need a separator + } else { + // we don't need a separator item.match_cur = item.match_lo; item.idx = 0; cur_items.push(item); @@ -418,12 +429,13 @@ fn inner_parse_loop(sess: &ParseSess, Success(()) } -pub fn parse(sess: &ParseSess, - tts: TokenStream, - ms: &[TokenTree], - directory: Option, - recurse_into_modules: bool) - -> NamedParseResult { +pub fn parse( + sess: &ParseSess, + tts: TokenStream, + ms: &[TokenTree], + directory: Option, + recurse_into_modules: bool, +) -> NamedParseResult { let mut parser = Parser::new(sess, tts, directory, recurse_into_modules, true); let mut cur_items = SmallVector::one(initial_matcher_pos(ms.to_owned(), parser.span.lo())); let mut next_items = Vec::new(); // or proceed normally @@ -433,9 +445,16 @@ pub fn parse(sess: &ParseSess, let mut eof_items = SmallVector::new(); assert!(next_items.is_empty()); - match inner_parse_loop(sess, &mut cur_items, &mut next_items, &mut eof_items, &mut bb_items, - &parser.token, parser.span) { - Success(_) => {}, + match inner_parse_loop( + sess, + &mut cur_items, + &mut next_items, + &mut eof_items, + &mut bb_items, + &parser.token, + parser.span, + ) { + Success(_) => {} Failure(sp, tok) => return Failure(sp, tok), Error(sp, msg) => return Error(sp, msg), } @@ -446,43 +465,56 @@ pub fn parse(sess: &ParseSess, /* error messages here could be improved with links to orig. rules */ if token_name_eq(&parser.token, &token::Eof) { if eof_items.len() == 1 { - let matches = eof_items[0].matches.iter_mut().map(|dv| { - Rc::make_mut(dv).pop().unwrap() - }); + let matches = eof_items[0] + .matches + .iter_mut() + .map(|dv| Rc::make_mut(dv).pop().unwrap()); return nameize(sess, ms, matches); } else if eof_items.len() > 1 { - return Error(parser.span, "ambiguity: multiple successful parses".to_string()); + return Error( + parser.span, + "ambiguity: multiple successful parses".to_string(), + ); } else { return Failure(parser.span, token::Eof); } } else if (!bb_items.is_empty() && !next_items.is_empty()) || bb_items.len() > 1 { - let nts = bb_items.iter().map(|item| match item.top_elts.get_tt(item.idx) { - TokenTree::MetaVarDecl(_, bind, name) => { - format!("{} ('{}')", name, bind) - } - _ => panic!() - }).collect::>().join(" or "); - - return Error(parser.span, format!( - "local ambiguity: multiple parsing options: {}", - match next_items.len() { - 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), - } - )); + let nts = bb_items + .iter() + .map(|item| match item.top_elts.get_tt(item.idx) { + TokenTree::MetaVarDecl(_, bind, name) => format!("{} ('{}')", name, bind), + _ => panic!(), + }) + .collect::>() + .join(" or "); + + return Error( + parser.span, + format!( + "local ambiguity: multiple parsing options: {}", + match next_items.len() { + 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), + } + ), + ); } else if bb_items.is_empty() && next_items.is_empty() { return Failure(parser.span, parser.token); } else if !next_items.is_empty() { /* Now process the next token */ cur_items.extend(next_items.drain(..)); parser.bump(); - } else /* bb_items.len() == 1 */ { + } else + /* bb_items.len() == 1 */ + { let mut item = bb_items.pop().unwrap(); if let TokenTree::MetaVarDecl(span, _, ident) = item.top_elts.get_tt(item.idx) { let match_cur = item.match_cur; - item.push_match(match_cur, - MatchedNonterminal(Rc::new(parse_nt(&mut parser, span, &ident.name.as_str())))); + item.push_match( + match_cur, + MatchedNonterminal(Rc::new(parse_nt(&mut parser, span, &ident.name.as_str()))), + ); item.idx += 1; item.match_cur += 1; } else { @@ -512,20 +544,21 @@ fn may_begin_with(name: &str, token: &Token) -> bool { "expr" => token.can_begin_expr(), "ty" => token.can_begin_type(), "ident" => token.is_ident(), - "vis" => match *token { // The follow-set of :vis + "priv" keyword + interpolated + "vis" => match *token { + // The follow-set of :vis + "priv" keyword + interpolated Token::Comma | Token::Ident(_) | Token::Interpolated(_) => true, _ => token.can_begin_type(), }, "block" => match *token { Token::OpenDelim(token::Brace) => true, Token::Interpolated(ref nt) => match nt.0 { - token::NtItem(_) | - token::NtPat(_) | - token::NtTy(_) | - token::NtIdent(_) | - token::NtMeta(_) | - token::NtPath(_) | - token::NtVis(_) => false, // none of these may start with '{'. + token::NtItem(_) + | token::NtPat(_) + | token::NtTy(_) + | token::NtIdent(_) + | token::NtMeta(_) + | token::NtPath(_) + | token::NtVis(_) => false, // none of these may start with '{'. _ => true, }, _ => false, @@ -591,12 +624,15 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal { "ident" => match p.token { token::Ident(sn) => { p.bump(); - token::NtIdent(Spanned::{node: sn, span: p.prev_span}) + token::NtIdent(Spanned:: { + node: sn, + span: p.prev_span, + }) } _ => { let token_str = pprust::token_to_string(&p.token); - p.fatal(&format!("expected ident, found {}", - &token_str[..])).emit(); + p.fatal(&format!("expected ident, found {}", &token_str[..])) + .emit(); FatalError.raise() } }, @@ -606,6 +642,6 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal { "lifetime" => token::NtLifetime(p.expect_lifetime()), // this is not supposed to happen, since it has been checked // when compiling the macro. - _ => p.span_bug(sp, "invalid fragment specifier") + _ => p.span_bug(sp, "invalid fragment specifier"), } } From 0d7f193dd358cdc13506cac2e0b84fc473b628be Mon Sep 17 00:00:00 2001 From: Mark Mansi Date: Fri, 19 Jan 2018 20:47:39 -0600 Subject: [PATCH 2/7] Added a bunch of comments to macro_parser.rs --- src/libsyntax/ext/tt/macro_parser.rs | 72 ++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 10 deletions(-) diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index a5b573f18db1d..cb671d75a002f 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -429,6 +429,17 @@ fn inner_parse_loop( Success(()) } +/// Parse the given set of token trees (`ms`), possibly consuming additional token trees from the +/// tokenstream (`tts`). +/// +/// # Parameters +/// +/// - `sess`: The session into which errors are emitted +/// - `tts`: The tokenstream from which additional token trees may be consumed if needed +/// - `ms`: The token trees we want to parse as macros +/// - `directory`: Information about the file locations (needed for the black-box parser) +/// - `recurse_into_modules`: Whether or not to recurse into modules (needed for the black-box +/// parser) pub fn parse( sess: &ParseSess, tts: TokenStream, @@ -436,15 +447,28 @@ pub fn parse( directory: Option, recurse_into_modules: bool, ) -> NamedParseResult { + // Create a parser that can be used for the "black box" parts. let mut parser = Parser::new(sess, tts, directory, recurse_into_modules, true); + + // 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. `inner_parse_loop` then + // processes all of these possible matcher positions and produces posible next positions into + // `next_items`. After some post-processing, the contents of `next_items` replenish + // `cur_items` and we start over again. let mut cur_items = SmallVector::one(initial_matcher_pos(ms.to_owned(), parser.span.lo())); - let mut next_items = Vec::new(); // or proceed normally + let mut next_items = Vec::new(); loop { - let mut bb_items = SmallVector::new(); // black-box parsed by parser.rs + // Matcher positions black-box parsed by parser.rs (`parser`) + let mut bb_items = SmallVector::new(); + + // Matcher positions that would be valid if the macro invocation was over now let mut eof_items = SmallVector::new(); assert!(next_items.is_empty()); + // Process `cur_items` until either we have finished the input or we need to get some + // parsing from the black-box parser done. The result is that `next_items` will contain a + // bunch of possible next matcher positions in `next_items`. match inner_parse_loop( sess, &mut cur_items, @@ -462,7 +486,12 @@ pub fn parse( // inner parse loop handled all cur_items, so it's empty assert!(cur_items.is_empty()); - /* error messages here could be improved with links to orig. rules */ + // We need to do some post processing after the `inner_parser_loop`. + // + // Error messages here could be improved with links to original rules. + + // If we reached the EOF, check that there is EXACTLY ONE possible matcher. Otherwise, + // either the parse is ambiguous (which should never happen) or their is a syntax error. if token_name_eq(&parser.token, &token::Eof) { if eof_items.len() == 1 { let matches = eof_items[0] @@ -478,7 +507,10 @@ pub fn parse( } else { return Failure(parser.span, token::Eof); } - } else if (!bb_items.is_empty() && !next_items.is_empty()) || bb_items.len() > 1 { + } + // Another possibility is that we need to call out to parse some rust nonterminal + // (black-box) parser. However, if there is not EXACTLY ONE of these, something is wrong. + else if (!bb_items.is_empty() && !next_items.is_empty()) || bb_items.len() > 1 { let nts = bb_items .iter() .map(|item| match item.top_elts.get_tt(item.idx) { @@ -499,15 +531,23 @@ pub fn parse( } ), ); - } else if bb_items.is_empty() && next_items.is_empty() { + } + // If there are no posible next positions AND we aren't waiting for the black-box parser, + // then their is a syntax error. + else if bb_items.is_empty() && next_items.is_empty() { return Failure(parser.span, parser.token); - } else if !next_items.is_empty() { - /* Now process the next token */ + } + // Dump all possible `next_items` into `cur_items` for the next iteration. + else if !next_items.is_empty() { + // Now process the next token cur_items.extend(next_items.drain(..)); parser.bump(); - } else - /* bb_items.len() == 1 */ - { + } + // Finally, we have the case where we need to call the black-box parser to get some + // nonterminal. + else { + assert_eq!(bb_items.len(), 1); + let mut item = bb_items.pop().unwrap(); if let TokenTree::MetaVarDecl(span, _, ident) = item.top_elts.get_tt(item.idx) { let match_cur = item.match_cur; @@ -595,6 +635,18 @@ fn may_begin_with(name: &str, token: &Token) -> bool { } } +/// A call to the "black-box" parser to parse some rust nonterminal. +/// +/// # Parameters +/// +/// - `p`: the "black-box" parser to use +/// - `sp`: the `Span` we want to parse +/// - `name`: the name of the metavar _matcher_ we want to match (e.g. `tt`, `ident`, `block`, +/// etc...) +/// +/// # Returns +/// +/// The parsed nonterminal. fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal { if name == "tt" { return token::NtTT(p.parse_token_tree()); From 6d4ed65585ebdd1e5cd7aa37936682675825b919 Mon Sep 17 00:00:00 2001 From: Mark Mansi Date: Wed, 24 Jan 2018 22:03:57 -0600 Subject: [PATCH 3/7] Added lots of comments + minor reorganization --- src/libsyntax/ext/tt/macro_parser.rs | 128 ++++++++++++++++++++------- 1 file changed, 94 insertions(+), 34 deletions(-) diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index cb671d75a002f..28d4f5f832f55 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -102,9 +102,10 @@ use std::rc::Rc; use std::collections::HashMap; use std::collections::hash_map::Entry::{Occupied, Vacant}; -// To avoid costly uniqueness checks, we require that `MatchSeq` always has -// a nonempty body. +// To avoid costly uniqueness checks, we require that `MatchSeq` always has a nonempty body. +/// Either a sequence of token trees or a single one. This is used as the representation of the +/// sequence of tokens that make up a matcher. #[derive(Clone)] enum TokenTreeOrTokenTreeVec { Tt(TokenTree), @@ -112,6 +113,7 @@ enum TokenTreeOrTokenTreeVec { } impl TokenTreeOrTokenTreeVec { + /// Returns the number of constituent token trees of `self`. fn len(&self) -> usize { match *self { TtSeq(ref v) => v.len(), @@ -119,6 +121,7 @@ impl TokenTreeOrTokenTreeVec { } } + /// The the `index`-th token tree of `self`. fn get_tt(&self, index: usize) -> TokenTree { match *self { TtSeq(ref v) => v[index].clone(), @@ -127,36 +130,90 @@ impl TokenTreeOrTokenTreeVec { } } -/// an unzipping of `TokenTree`s +/// An unzipping of `TokenTree`s... see the `stack` field of `MatcherPos`. +/// +/// This is used by `inner_parse_loop` to keep track of delimited submatchers that we have +/// descended into. #[derive(Clone)] struct MatcherTtFrame { + /// The "parent" matcher that we are descending into. elts: TokenTreeOrTokenTreeVec, + /// The position of the "dot" in `elts` at the time we descended. idx: usize, } +/// Represents a single "position" (aka "matcher position", aka "item"), as described in the module +/// documentation. #[derive(Clone)] struct MatcherPos { - stack: Vec, + /// The token or sequence of tokens that make up the matcher top_elts: TokenTreeOrTokenTreeVec, - sep: Option, + /// The position of the "dot" in this matcher idx: usize, - up: Option>, + /// The beginning position in the source that the beginning of this matcher corresponds to. In + /// other words, the token in the source at `sp_lo` is matched against the first token of the + /// matcher. + sp_lo: BytePos, + + /// 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. matches: Vec>>, + /// 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]`. match_lo: usize, + /// 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`. match_cur: usize, + /// Similar to `match_lo` except `match_hi` is the position in `matches` of the _last_ metavar + /// in this matcher. match_hi: usize, - sp_lo: BytePos, + + // Specifically used if we are matching a repetition. If we aren't both should be `None`. + /// The separator if we are in a repetition + sep: Option, + /// The "parent" matcher position if we are in a repetition. That is, the matcher position just + /// before we enter the sequence. + up: Option>, + + // Specifically used to "unzip" token trees. By "unzip", we mean to unwrap the delimiters from + // a delimited token tree (e.g. something wrapped in `(` `)`) or to get the contents of a doc + // comment... + /// When matching against matchers with nested delimited submatchers (e.g. `pat ( pat ( .. ) + /// 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. + // Also, throughout the comments, this "descent" is often referred to as "unzipping"... + stack: Vec, } impl MatcherPos { + /// Add `m` as a named match for the `idx`-th metavar. fn push_match(&mut self, idx: usize, m: NamedMatch) { let matches = Rc::make_mut(&mut self.matches[idx]); matches.push(m); } } +/// Represents the possible results of an attempted parse. +pub enum ParseResult { + /// 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. + Failure(syntax_pos::Span, Token), + /// Fatal error (malformed macro?). Abort compilation. + Error(syntax_pos::Span, String), +} + +/// A `ParseResult` where the `Success` variant contains a mapping of `Ident`s to `NamedMatch`es. +/// This represents the mapping of metavars to the token trees they bind to. pub type NamedParseResult = ParseResult>>; +/// Count how many metavars are named in the given matcher `ms`. pub fn count_names(ms: &[TokenTree]) -> usize { ms.iter().fold(0, |count, elt| { count + match *elt { @@ -169,20 +226,38 @@ pub fn count_names(ms: &[TokenTree]) -> usize { }) } +/// Initialize `len` empty shared `Vec`s to be used to store matches of metavars. +fn create_matches(len: usize) -> Vec>> { + (0..len).into_iter().map(|_| Rc::new(Vec::new())).collect() +} + +/// Generate the top-level matcher position in which the "dot" is before the first token of the +/// matcher `ms` and we are going to start matching at position `lo` in the source. fn initial_matcher_pos(ms: Vec, lo: BytePos) -> Box { let match_idx_hi = count_names(&ms[..]); let matches = create_matches(match_idx_hi); Box::new(MatcherPos { - stack: vec![], - top_elts: TtSeq(ms), - sep: None, + // Start with the top level matcher given to us + top_elts: TtSeq(ms), // "elts" is an abbr. for "elements" + // The "dot" is before the first token of the matcher idx: 0, - up: None, + // We start matching with byte `lo` in the source code + sp_lo: lo, + + // 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 `matches.len()`. `match_cur` is 0 since + // we haven't actually matched anything yet. matches, match_lo: 0, match_cur: 0, match_hi: match_idx_hi, - sp_lo: lo, + + // Haven't descended into any delimiters, so empty stack + stack: vec![], + + // Haven't descended into any sequences, so both of these are `None` + sep: None, + up: None, }) } @@ -202,7 +277,6 @@ fn initial_matcher_pos(ms: Vec, lo: BytePos) -> Box { /// token tree. The depth of the `NamedMatch` structure will therefore depend /// only on the nesting depth of `ast::TTSeq`s in the originating /// token tree it was derived from. - #[derive(Debug, Clone)] pub enum NamedMatch { MatchedSeq(Rc>, syntax_pos::Span), @@ -260,16 +334,6 @@ fn nameize>( Success(ret_val) } -pub enum ParseResult { - 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. - Failure(syntax_pos::Span, Token), - /// Fatal error (malformed macro?). Abort compilation. - Error(syntax_pos::Span, String), -} - pub fn parse_failure_msg(tok: Token) -> String { match tok { token::Eof => "unexpected end of macro invocation".to_string(), @@ -291,10 +355,6 @@ fn token_name_eq(t1: &Token, t2: &Token) -> bool { } } -fn create_matches(len: usize) -> Vec>> { - (0..len).into_iter().map(|_| Rc::new(Vec::new())).collect() -} - fn inner_parse_loop( sess: &ParseSess, cur_items: &mut SmallVector>, @@ -429,14 +489,14 @@ fn inner_parse_loop( Success(()) } -/// Parse the given set of token trees (`ms`), possibly consuming additional token trees from the -/// tokenstream (`tts`). +/// Use the given sequence of token trees (`ms`) as a matcher. Match the given token stream `tts` +/// against it and return the match. /// /// # Parameters /// /// - `sess`: The session into which errors are emitted -/// - `tts`: The tokenstream from which additional token trees may be consumed if needed -/// - `ms`: The token trees we want to parse as macros +/// - `tts`: The tokenstream we are matching against the pattern `ms` +/// - `ms`: A sequence of token trees representing a pattern against which we are matching /// - `directory`: Information about the file locations (needed for the black-box parser) /// - `recurse_into_modules`: Whether or not to recurse into modules (needed for the black-box /// parser) @@ -451,10 +511,10 @@ pub fn parse( let mut parser = Parser::new(sess, tts, directory, recurse_into_modules, true); // 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. `inner_parse_loop` then + // the "dot" is before the first token of the first token tree in `ms`. `inner_parse_loop` then // processes all of these possible matcher positions and produces posible next positions into - // `next_items`. After some post-processing, the contents of `next_items` replenish - // `cur_items` and we start over again. + // `next_items`. After some post-processing, the contents of `next_items` replenish `cur_items` + // and we start over again. let mut cur_items = SmallVector::one(initial_matcher_pos(ms.to_owned(), parser.span.lo())); let mut next_items = Vec::new(); From b01b481db3ed9e13ab2fedc711f7cdabc8c1c53c Mon Sep 17 00:00:00 2001 From: Mark Mansi Date: Wed, 24 Jan 2018 22:59:11 -0600 Subject: [PATCH 4/7] Added/improved comments --- src/libsyntax/ext/tt/macro_parser.rs | 78 ++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 17 deletions(-) diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 28d4f5f832f55..f6c4359167f63 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -161,6 +161,12 @@ struct MatcherPos { /// 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. + /// + /// 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`. matches: Vec>>, /// 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 @@ -255,7 +261,7 @@ fn initial_matcher_pos(ms: Vec, lo: BytePos) -> Box { // Haven't descended into any delimiters, so empty stack stack: vec![], - // Haven't descended into any sequences, so both of these are `None` + // Haven't descended into any sequences, so both of these are `None`. sep: None, up: None, }) @@ -355,6 +361,28 @@ fn token_name_eq(t1: &Token, t2: &Token) -> bool { } } +/// Process the matcher positions of `cur_items` until it is empty. In the process, this will +/// produce more items in `next_items`, `eof_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. +/// +/// # Parameters +/// +/// - `sess`: the parsing session into which errors are emitted. +/// - `cur_items`: the set of current items to be processed. This should be empty by the end of a +/// successful execution of this function. +/// - `next_items`: the set of newly generated items. These are used to replenish `cur_items` in +/// the function `parse`. +/// - `eof_items`: the set of items that would be valid if this was the EOF. +/// - `bb_items`: the set of items that are waiting for the black-box parser. +/// - `token`: the current token of the parser. +/// - `span`: the `Span` in the source code corresponding to the token trees we are trying to match +/// against the matcher positions in `cur_items`. +/// +/// # Returns +/// +/// A `ParseResult`. Note that matches are kept track of through the items generated. fn inner_parse_loop( sess: &ParseSess, cur_items: &mut SmallVector>, @@ -364,8 +392,11 @@ fn inner_parse_loop( token: &Token, span: syntax_pos::Span, ) -> ParseResult<()> { + // Pop items from `cur_items` until it is empty. while let Some(mut item) = cur_items.pop() { - // When unzipped trees end, remove them + // When unzipped trees end, remove them. This corresponds to backtracking out of a + // delimited submatcher into which we already descended. In 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 }) => { @@ -376,37 +407,46 @@ fn inner_parse_loop( } } + // 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(); - // at end of sequence + // If `idx >= len`, then we are at or past the end of the matcher of `item`. if idx >= len { - // We are repeating iff there is a parent + // We are repeating iff there is a parent. If the matcher is inside of a repetition, + // then we could be at the end of a sequence or at the beginning of the next + // repetition. if item.up.is_some() { - // Disregarding the separator, add the "up" case to the tokens that should be - // examined. - // (remove this condition to make trailing seps ok) + // At this point, regardless of whether there is a separator, we should add all + // matches from the complete repetition of the sequence to the shared, top-level + // `matches` list (actually, `up.matches`, which could itself not be the top-level, + // but anyway...). Moreover, we add another item to `cur_items` in which the "dot" + // is at the end of the `up` matcher. This ensures that the "dot" in the `up` + // matcher is also advanced sufficiently. + // + // NOTE: removing the condition `idx == len` allows trailing separators. if idx == len { + // Get the `up` matcher let mut new_pos = item.up.clone().unwrap(); - // update matches (the MBE "parse tree") by appending - // each tree as a subtree. - - // Only touch the binders we have actually bound + // Add matches from this repetition to the `matches` of `up` for idx in item.match_lo..item.match_hi { let sub = item.matches[idx].clone(); let span = span.with_lo(item.sp_lo); new_pos.push_match(idx, MatchedSeq(sub, span)); } + // Move the "dot" past the repetition in `up` new_pos.match_cur = item.match_hi; new_pos.idx += 1; cur_items.push(new_pos); } - // Check if we need a separator + // Check if we need a separator. if idx == len && item.sep.is_some() { - // We have a separator, and it is the current token. + // We have a separator, and it is the current token. We can advance past the + // separator token. if item.sep .as_ref() .map(|sep| token_name_eq(token, sep)) @@ -415,14 +455,18 @@ fn inner_parse_loop( item.idx += 1; next_items.push(item); } - } else { - // we don't need a separator + } + // We don't need a separator. Move the "dot" back to the beginning of the matcher + // and try to match again. + else { item.match_cur = item.match_lo; item.idx = 0; cur_items.push(item); } - } else { - // We aren't repeating, so we must be potentially at the end of the input. + } + // If we are not in a repetition, then being at the end of a matcher means that we have + // reached the potential end of the input. + else { eof_items.push(item); } } else { From 02d1d92878181a1c99b6d6029f44fbbb91bff499 Mon Sep 17 00:00:00 2001 From: Mark Mansi Date: Wed, 24 Jan 2018 23:10:39 -0600 Subject: [PATCH 5/7] Still more comments --- src/libsyntax/ext/tt/macro_parser.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index f6c4359167f63..e15198cc3835e 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -469,9 +469,13 @@ fn inner_parse_loop( else { eof_items.push(item); } - } else { + } + // We are in the middle of a matcher. + else { + // Look at what token in the matcher we are trying to match the current token (`token`) + // against. Depending on that, we may generate new items. match item.top_elts.get_tt(idx) { - /* need to descend into sequence */ + // Need to descend into a sequence TokenTree::Sequence(sp, seq) => { if seq.op == quoted::KleeneOp::ZeroOrMore { // Examine the case where there are 0 matches of this sequence @@ -499,11 +503,16 @@ fn inner_parse_loop( top_elts: Tt(TokenTree::Sequence(sp, seq)), })); } + + // We need to match a metavar (but the identifier is invalid)... this is an error TokenTree::MetaVarDecl(span, _, id) if id.name == keywords::Invalid.name() => { if sess.missing_fragment_specifiers.borrow_mut().remove(&span) { return Error(span, "missing fragment specifier".to_string()); } } + + // We need to match a metavar with a valid ident... call out to the black-box + // parser by adding an item to `bb_items`. TokenTree::MetaVarDecl(_, _, id) => { // Built-in nonterminals never start with these tokens, // so we can eliminate them from consideration. @@ -511,6 +520,13 @@ fn inner_parse_loop( bb_items.push(item); } } + + // We need to descend into a delimited submatcher or a doc comment. To do this, we + // push the current matcher onto a stack and push a new item containing the + // submatcher onto `cur_items`. + // + // At the beginning of the loop, if we reach the end of the delimited submatcher, + // we pop the stack to backtrack out of the descent. seq @ TokenTree::Delimited(..) | seq @ TokenTree::Token(_, DocComment(..)) => { let lower_elts = mem::replace(&mut item.top_elts, Tt(seq)); let idx = item.idx; @@ -521,15 +537,23 @@ fn inner_parse_loop( item.idx = 0; cur_items.push(item); } + + // We just matched a normal token. We can just advance the parser. TokenTree::Token(_, ref t) if token_name_eq(t, token) => { item.idx += 1; next_items.push(item); } + + // There was another token that was not `token`... This means we can't add any + // rules. NOTE that this is not necessarily an error unless _all_ items in + // `cur_items` end up doing this. There may still be some other matchers that do + // end up working out. TokenTree::Token(..) | TokenTree::MetaVar(..) => {} } } } + // Yay a successful parse (so far)! Success(()) } From e2d558ad56d03bdc59ab2aaa28aaedf9172a0539 Mon Sep 17 00:00:00 2001 From: Mark Mansi Date: Wed, 24 Jan 2018 23:23:01 -0600 Subject: [PATCH 6/7] A few more comments --- src/libsyntax/ext/tt/macro_parser.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index e15198cc3835e..af18801c97e6f 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -289,11 +289,17 @@ pub enum NamedMatch { MatchedNonterminal(Rc), } +/// Takes a sequence of token trees `ms` representing a matcher which successfully matched input +/// and an iterator of items that matched input and produces a `NamedParseResult`. fn nameize>( sess: &ParseSess, ms: &[TokenTree], mut res: I, ) -> NamedParseResult { + // Recursively descend into each type of matcher (e.g. sequences, delimited, metavars) and make + // 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`. fn n_rec>( sess: &ParseSess, m: &TokenTree, @@ -340,6 +346,8 @@ fn nameize>( Success(ret_val) } +/// Generate an appropriate parsing failure message. For EOF, this is "unexpected end...". For +/// other tokens, this is "unexpected token...". pub fn parse_failure_msg(tok: Token) -> String { match tok { token::Eof => "unexpected end of macro invocation".to_string(), From 2184400be7f6b695792af7ddde14482f3e72f1e1 Mon Sep 17 00:00:00 2001 From: Mark Mansi Date: Mon, 29 Jan 2018 16:37:57 -0600 Subject: [PATCH 7/7] Update comment --- src/libsyntax/ext/tt/macro_parser.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index af18801c97e6f..1a9849ca5307d 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -113,7 +113,8 @@ enum TokenTreeOrTokenTreeVec { } impl TokenTreeOrTokenTreeVec { - /// Returns the number of constituent token trees of `self`. + /// Returns the number of constituent top-level token trees of `self` (top-level in that it + /// will not recursively descend into subtrees). fn len(&self) -> usize { match *self { TtSeq(ref v) => v.len(),