Improve if/else formatting in macro_parser.rs.

To avoid the strange style where comments force `else` onto its own
line.

The commit also removes several else-after-return constructs, which can
be hard to read.
This commit is contained in:
Nicholas Nethercote 2022-03-03 11:00:50 +11:00
parent 2f8d1a835b
commit 11c565f14b

View File

@ -505,25 +505,23 @@ fn inner_parse_loop<'root, 'tt>(
item.idx += 1; item.idx += 1;
next_items.push(item); next_items.push(item);
} }
} } else if item.seq_op != Some(mbe::KleeneOp::ZeroOrOne) {
// We don't need a separator. Move the "dot" back to the beginning of the matcher // We don't need a separator. Move the "dot" back to the beginning of the
// and try to match again UNLESS we are only allowed to have _one_ repetition. // matcher and try to match again UNLESS we are only allowed to have _one_
else if item.seq_op != Some(mbe::KleeneOp::ZeroOrOne) { // repetition.
item.match_cur = item.match_lo; item.match_cur = item.match_lo;
item.idx = 0; item.idx = 0;
cur_items.push(item); cur_items.push(item);
} }
} } else {
// If we are not in a repetition, then being at the end of a matcher means that we have // If we are not in a repetition, then being at the end of a matcher means that we
// reached the potential end of the input. // have reached the potential end of the input.
else {
eof_items.push(item); eof_items.push(item);
} }
} } else {
// We are in the middle of a matcher. // We are in the middle of a matcher. Look at what token in the matcher we are trying
else { // to match the current token (`token`) against. Depending on that, we may generate new
// Look at what token in the matcher we are trying to match the current token (`token`) // items.
// against. Depending on that, we may generate new items.
match item.top_elts.get_tt(idx) { match item.top_elts.get_tt(idx) {
// Need to descend into a sequence // Need to descend into a sequence
TokenTree::Sequence(sp, seq) => { TokenTree::Sequence(sp, seq) => {
@ -666,17 +664,14 @@ pub(super) fn parse_tt(
// If we reached the EOF, check that there is EXACTLY ONE possible matcher. Otherwise, // If we reached the EOF, check that there is EXACTLY ONE possible matcher. Otherwise,
// either the parse is ambiguous (which should never happen) or there is a syntax error. // either the parse is ambiguous (which should never happen) or there is a syntax error.
if parser.token == token::Eof { if parser.token == token::Eof {
if eof_items.len() == 1 { return if eof_items.len() == 1 {
let matches = let matches =
eof_items[0].matches.iter_mut().map(|dv| Lrc::make_mut(dv).pop().unwrap()); eof_items[0].matches.iter_mut().map(|dv| Lrc::make_mut(dv).pop().unwrap());
return nameize(parser.sess, ms, matches); nameize(parser.sess, ms, matches)
} else if eof_items.len() > 1 { } else if eof_items.len() > 1 {
return Error( Error(parser.token.span, "ambiguity: multiple successful parses".to_string())
parser.token.span,
"ambiguity: multiple successful parses".to_string(),
);
} else { } else {
return Failure( Failure(
Token::new( Token::new(
token::Eof, token::Eof,
if parser.token.span.is_dummy() { if parser.token.span.is_dummy() {
@ -686,8 +681,8 @@ pub(super) fn parse_tt(
}, },
), ),
"missing tokens in macro arguments", "missing tokens in macro arguments",
); )
} };
} }
// Performance hack: eof_items may share matchers via Rc with other things that we want // Performance hack: eof_items may share matchers via Rc with other things that we want
// to modify. Dropping eof_items now may drop these refcounts to 1, preventing an // to modify. Dropping eof_items now may drop these refcounts to 1, preventing an
@ -699,9 +694,10 @@ pub(super) fn parse_tt(
if bb_items.is_empty() && next_items.is_empty() { if bb_items.is_empty() && next_items.is_empty() {
return Failure(parser.token.clone(), "no rules expected this token in macro call"); return Failure(parser.token.clone(), "no rules expected this token in macro call");
} }
// 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. if (!bb_items.is_empty() && !next_items.is_empty()) || bb_items.len() > 1 {
else if (!bb_items.is_empty() && !next_items.is_empty()) || bb_items.len() > 1 { // We need to call out to parse some rust nonterminal (black-box) parser. But something
// is wrong, because there is not EXACTLY ONE of these.
let nts = bb_items let nts = bb_items
.iter() .iter()
.map(|item| match item.top_elts.get_tt(item.idx) { .map(|item| match item.top_elts.get_tt(item.idx) {
@ -723,15 +719,15 @@ pub(super) fn parse_tt(
), ),
); );
} }
// Dump all possible `next_items` into `cur_items` for the next iteration.
else if !next_items.is_empty() { if !next_items.is_empty() {
// Now process the next token // Dump all possible `next_items` into `cur_items` for the next iteration. Then process
// the next token.
cur_items.extend(next_items.drain(..)); cur_items.extend(next_items.drain(..));
parser.to_mut().bump(); parser.to_mut().bump();
} } else {
// Finally, we have the case where we need to call the black-box parser to get some // Finally, we have the case where we need to call the black-box parser to get some
// nonterminal. // nonterminal.
else {
assert_eq!(bb_items.len(), 1); assert_eq!(bb_items.len(), 1);
let mut item = bb_items.pop().unwrap(); let mut item = bb_items.pop().unwrap();