Skip to content

Commit 49740b7

Browse files
committed
extract parse_pat_range_starting_with_lit
1 parent e6f980f commit 49740b7

File tree

1 file changed

+21
-20
lines changed
  • src/libsyntax/parse/parser

1 file changed

+21
-20
lines changed

src/libsyntax/parse/parser/pat.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -202,26 +202,10 @@ impl<'a> Parser<'a> {
202202
} else {
203203
// Try to parse everything else as literal with optional minus
204204
match self.parse_literal_maybe_minus() {
205-
Ok(begin) => {
206-
let op_span = self.token.span;
207-
if self.check(&token::DotDot) || self.check(&token::DotDotEq) ||
208-
self.check(&token::DotDotDot) {
209-
let (end_kind, form) = if self.eat(&token::DotDotDot) {
210-
(RangeEnd::Included(RangeSyntax::DotDotDot), "...")
211-
} else if self.eat(&token::DotDotEq) {
212-
(RangeEnd::Included(RangeSyntax::DotDotEq), "..=")
213-
} else if self.eat(&token::DotDot) {
214-
(RangeEnd::Excluded, "..")
215-
} else {
216-
panic!("impossible case: we already matched \
217-
on a range-operator token")
218-
};
219-
let end = self.parse_pat_range_end_opt(&begin, form)?;
220-
PatKind::Range(begin, end, respan(op_span, end_kind))
221-
} else {
222-
PatKind::Lit(begin)
223-
}
224-
}
205+
Ok(begin) if self.check(&token::DotDot) || self.check(&token::DotDotEq)
206+
|| self.check(&token::DotDotDot)
207+
=> self.parse_pat_range_starting_with_lit(begin)?,
208+
Ok(begin) => PatKind::Lit(begin),
225209
Err(mut err) => {
226210
self.cancel(&mut err);
227211
let expected = expected.unwrap_or("pattern");
@@ -360,6 +344,23 @@ impl<'a> Parser<'a> {
360344
Ok(PatKind::Range(begin, end, respan(op_span, end_kind)))
361345
}
362346

347+
/// Parse a range pattern `$literal $form $end?` where `$form = ".." | "..." | "..=" ;`.
348+
/// The `$path` has already been parsed and the next token is the `$form`.
349+
fn parse_pat_range_starting_with_lit(&mut self, begin: P<Expr>) -> PResult<'a, PatKind> {
350+
let op_span = self.token.span;
351+
let (end_kind, form) = if self.eat(&token::DotDotDot) {
352+
(RangeEnd::Included(RangeSyntax::DotDotDot), "...")
353+
} else if self.eat(&token::DotDotEq) {
354+
(RangeEnd::Included(RangeSyntax::DotDotEq), "..=")
355+
} else if self.eat(&token::DotDot) {
356+
(RangeEnd::Excluded, "..")
357+
} else {
358+
panic!("impossible case: we already matched on a range-operator token")
359+
};
360+
let end = self.parse_pat_range_end_opt(&begin, form)?;
361+
Ok(PatKind::Range(begin, end, respan(op_span, end_kind)))
362+
}
363+
363364
// Helper function to decide whether to parse as ident binding
364365
// or to try to do something more complex like range patterns.
365366
fn parse_as_ident(&mut self) -> bool {

0 commit comments

Comments
 (0)