Skip to content

Commit

Permalink
parser: TopLevel -> RecoverComma.
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Aug 25, 2019
1 parent 6a73199 commit acb1130
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/libsyntax/parse/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ pub(super) const PARAM_EXPECTED: Expected = Some("parameter name");
#[derive(PartialEq)]
pub enum GateOr { Yes, No }

/// Whether or not this is the top level pattern context.
/// Whether or not to recover a `,` when parsing or-patterns.
#[derive(PartialEq, Copy, Clone)]
enum TopLevel { Yes, No }
enum RecoverComma { Yes, No }

impl<'a> Parser<'a> {
/// Parses a pattern.
Expand Down Expand Up @@ -52,7 +52,7 @@ impl<'a> Parser<'a> {
let gated_leading_vert = self.eat_or_separator() && gate_or == GateOr::Yes;

// Parse the possibly-or-pattern.
let pat = self.parse_pat_with_or(None, gate_or, TopLevel::Yes)?;
let pat = self.parse_pat_with_or(None, gate_or, RecoverComma::Yes)?;

// If we parsed a leading `|` which should be gated,
// and no other gated or-pattern has been parsed thus far,
Expand All @@ -72,7 +72,7 @@ impl<'a> Parser<'a> {
/// Special recovery is provided for or-patterns and leading `|`.
pub(super) fn parse_fn_param_pat(&mut self) -> PResult<'a, P<Pat>> {
self.recover_leading_vert("not allowed in a parameter pattern");
let pat = self.parse_pat_with_or(PARAM_EXPECTED, GateOr::No, TopLevel::No)?;
let pat = self.parse_pat_with_or(PARAM_EXPECTED, GateOr::No, RecoverComma::No)?;

if let PatKind::Or(..) = &pat.node {
self.ban_illegal_fn_param_or_pat(&pat);
Expand All @@ -96,11 +96,11 @@ impl<'a> Parser<'a> {
&mut self,
expected: Expected,
gate_or: GateOr,
top_level: TopLevel,
rc: RecoverComma,
) -> PResult<'a, P<Pat>> {
// Parse the first pattern.
let first_pat = self.parse_pat(expected)?;
self.maybe_recover_unexpected_comma(first_pat.span, top_level)?;
self.maybe_recover_unexpected_comma(first_pat.span, rc)?;

// If the next token is not a `|`,
// this is not an or-pattern and we should exit here.
Expand All @@ -115,7 +115,7 @@ impl<'a> Parser<'a> {
err.span_label(lo, "while parsing this or-pattern staring here");
err
})?;
self.maybe_recover_unexpected_comma(pat.span, top_level)?;
self.maybe_recover_unexpected_comma(pat.span, rc)?;
pats.push(pat);
}
let or_pattern_span = lo.to(self.prev_span);
Expand Down Expand Up @@ -156,8 +156,8 @@ impl<'a> Parser<'a> {

/// Some special error handling for the "top-level" patterns in a match arm,
/// `for` loop, `let`, &c. (in contrast to subpatterns within such).
fn maybe_recover_unexpected_comma(&mut self, lo: Span, top_level: TopLevel) -> PResult<'a, ()> {
if top_level == TopLevel::No || self.token != token::Comma {
fn maybe_recover_unexpected_comma(&mut self, lo: Span, rc: RecoverComma) -> PResult<'a, ()> {
if rc == RecoverComma::No || self.token != token::Comma {
return Ok(());
}

Expand Down Expand Up @@ -207,7 +207,7 @@ impl<'a> Parser<'a> {
/// See `parse_pat_with_or` for details on parsing or-patterns.
fn parse_pat_with_or_inner(&mut self) -> PResult<'a, P<Pat>> {
self.recover_leading_vert("only allowed in a top-level pattern");
self.parse_pat_with_or(None, GateOr::Yes, TopLevel::No)
self.parse_pat_with_or(None, GateOr::Yes, RecoverComma::No)
}

/// Recover if `|` or `||` is here.
Expand Down

0 comments on commit acb1130

Please sign in to comment.