Skip to content

Commit

Permalink
Factor out range right-hand-side parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed May 11, 2024
1 parent 3ebe8be commit f556cf5
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1416,18 +1416,7 @@ pub(crate) mod parsing {
});
} else if Precedence::Range >= base && input.peek(Token![..]) {
let limits: RangeLimits = input.parse()?;
let rhs = if matches!(limits, RangeLimits::HalfOpen(_))
&& (input.is_empty()
|| input.peek(Token![,])
|| input.peek(Token![;])
|| input.peek(Token![.]) && !input.peek(Token![..])
|| !allow_struct.0 && input.peek(token::Brace))
{
None
} else {
let rhs = parse_binop_rhs(input, allow_struct, Precedence::Range)?;
Some(rhs)
};
let rhs = expr_range_rhs(input, &limits, allow_struct)?;
lhs = Expr::Range(ExprRange {
attrs: Vec::new(),
start: Some(Box::new(lhs)),
Expand Down Expand Up @@ -2870,24 +2859,33 @@ pub(crate) mod parsing {
#[cfg(feature = "full")]
fn expr_range(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprRange> {
let limits: RangeLimits = input.parse()?;
let end = if matches!(limits, RangeLimits::HalfOpen(_))
let rhs = expr_range_rhs(input, &limits, allow_struct)?;
Ok(ExprRange {
attrs: Vec::new(),
start: None,
limits,
end: rhs.map(Box::new),
})
}

#[cfg(feature = "full")]
fn expr_range_rhs(
input: ParseStream,
limits: &RangeLimits,
allow_struct: AllowStruct,
) -> Result<Option<Expr>> {
if matches!(limits, RangeLimits::HalfOpen(_))
&& (input.is_empty()
|| input.peek(Token![,])
|| input.peek(Token![;])
|| input.peek(Token![.]) && !input.peek(Token![..])
|| !allow_struct.0 && input.peek(token::Brace))
{
None
Ok(None)
} else {
let to = ambiguous_expr(input, allow_struct)?;
Some(Box::new(to))
};
Ok(ExprRange {
attrs: Vec::new(),
start: None,
limits,
end,
})
let rhs = parse_binop_rhs(input, allow_struct, Precedence::Range)?;
Ok(Some(rhs))
}
}

#[cfg(feature = "full")]
Expand Down

0 comments on commit f556cf5

Please sign in to comment.