Skip to content

Commit 7b59b4f

Browse files
committed
parser: extract eat_or_separator.
1 parent a4a34ab commit 7b59b4f

File tree

1 file changed

+28
-30
lines changed
  • src/libsyntax/parse/parser

1 file changed

+28
-30
lines changed

src/libsyntax/parse/parser/pat.rs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,10 @@ impl<'a> Parser<'a> {
2929
loop {
3030
pats.push(self.parse_top_level_pat()?);
3131

32-
if self.token == token::OrOr {
33-
self.ban_unexpected_or_or();
34-
self.bump();
35-
} else if self.eat(&token::BinOp(token::Or)) {
36-
// This is a No-op. Continue the loop to parse the next
37-
// pattern.
38-
} else {
32+
if !self.eat_or_separator() {
3933
return Ok(pats);
4034
}
41-
};
42-
}
43-
44-
fn ban_unexpected_or_or(&mut self) {
45-
self.struct_span_err(self.token.span, "unexpected token `||` after pattern")
46-
.span_suggestion(
47-
self.token.span,
48-
"use a single `|` to specify multiple patterns",
49-
"|".to_owned(),
50-
Applicability::MachineApplicable
51-
)
52-
.emit();
35+
}
5336
}
5437

5538
/// A wrapper around `parse_pat` with some special error handling for the
@@ -127,17 +110,7 @@ impl<'a> Parser<'a> {
127110

128111
let lo = first_pat.span;
129112
let mut pats = vec![first_pat];
130-
loop {
131-
if self.token == token::OrOr {
132-
// Found `||`; Recover and pretend we parsed `|`.
133-
self.ban_unexpected_or_or();
134-
self.bump();
135-
} else if self.eat(&token::BinOp(token::Or)) {
136-
// Found `|`. Working towards a proper or-pattern.
137-
} else {
138-
break;
139-
}
140-
113+
while self.eat_or_separator() {
141114
let pat = self.parse_pat(expected)?;
142115
self.maybe_recover_unexpected_comma(pat.span, top_level)?;
143116
pats.push(pat);
@@ -152,6 +125,31 @@ impl<'a> Parser<'a> {
152125
Ok(self.mk_pat(or_pattern_span, PatKind::Or(pats)))
153126
}
154127

128+
/// Eat the or-pattern `|` separator.
129+
/// If instead a `||` token is encountered, recover and pretend we parsed `|`.
130+
fn eat_or_separator(&mut self) -> bool {
131+
match self.token.kind {
132+
token::OrOr => {
133+
// Found `||`; Recover and pretend we parsed `|`.
134+
self.ban_unexpected_or_or();
135+
self.bump();
136+
true
137+
}
138+
_ => self.eat(&token::BinOp(token::Or)),
139+
}
140+
}
141+
142+
fn ban_unexpected_or_or(&mut self) {
143+
self.struct_span_err(self.token.span, "unexpected token `||` after pattern")
144+
.span_suggestion(
145+
self.token.span,
146+
"use a single `|` to specify multiple patterns",
147+
"|".to_owned(),
148+
Applicability::MachineApplicable
149+
)
150+
.emit();
151+
}
152+
155153
/// Parses a pattern, with a setting whether modern range patterns (e.g., `a..=b`, `a..b` are
156154
/// allowed).
157155
fn parse_pat_with_range_pat(

0 commit comments

Comments
 (0)