@@ -29,27 +29,10 @@ impl<'a> Parser<'a> {
29
29
loop {
30
30
pats. push ( self . parse_top_level_pat ( ) ?) ;
31
31
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 ( ) {
39
33
return Ok ( pats) ;
40
34
}
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
+ }
53
36
}
54
37
55
38
/// A wrapper around `parse_pat` with some special error handling for the
@@ -127,17 +110,7 @@ impl<'a> Parser<'a> {
127
110
128
111
let lo = first_pat. span ;
129
112
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 ( ) {
141
114
let pat = self . parse_pat ( expected) ?;
142
115
self . maybe_recover_unexpected_comma ( pat. span , top_level) ?;
143
116
pats. push ( pat) ;
@@ -152,6 +125,31 @@ impl<'a> Parser<'a> {
152
125
Ok ( self . mk_pat ( or_pattern_span, PatKind :: Or ( pats) ) )
153
126
}
154
127
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
+
155
153
/// Parses a pattern, with a setting whether modern range patterns (e.g., `a..=b`, `a..b` are
156
154
/// allowed).
157
155
fn parse_pat_with_range_pat (
0 commit comments