refactor parse_math_expression, reduce indentation (#3093)

This commit is contained in:
Saeed Rasooli 2021-02-26 08:41:20 +03:30 committed by GitHub
parent aa6c6120f6
commit 120e80d1b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1093,8 +1093,9 @@ pub fn parse_math_expression(
shorthand_mode: bool,
) -> (usize, SpannedExpression, Option<ParseError>) {
// Precedence parsing is included
// Short_hand mode means that the left-hand side of an expression can point to a column-path. To make this possible,
// we parse as normal, but then go back and when we detect a left-hand side, reparse that value if it's a string
// shorthand_mode means that the left-hand side of an expression can point to a column-path.
// To make this possible, we parse as normal, but then go back and when we detect a
// left-hand side, reparse that value if it's a string
let mut idx = 0;
let mut error = None;
@ -1121,7 +1122,19 @@ pub fn parse_math_expression(
}
idx += 1;
if idx < lite_args.len() {
if idx == lite_args.len() {
if error.is_none() {
error = Some(ParseError::argument_error(
lite_args[idx - 1].clone(),
ArgumentError::MissingMandatoryPositional("right hand side".into()),
));
}
working_exprs.push((None, garbage(op.span)));
working_exprs.push((None, garbage(op.span)));
prec.push(0);
break;
}
trace!(
"idx: {} working_exprs: {:#?} prec: {:?}",
idx,
@ -1142,7 +1155,9 @@ pub fn parse_math_expression(
prec.push(next_prec);
working_exprs.push((None, op));
working_exprs.push(rhs_working_expr);
} else {
idx += 1;
continue;
}
while !prec.is_empty()
&& *prec.last().expect("This shouldn't happen") >= next_prec
&& next_prec > 0 // Not garbage
@ -1157,8 +1172,7 @@ pub fn parse_math_expression(
);
let (_, right) = working_exprs.pop().expect("This shouldn't be possible");
let (_, op) = working_exprs.pop().expect("This shouldn't be possible");
let (orig_left, left) =
working_exprs.pop().expect("This shouldn't be possible");
let (orig_left, left) = working_exprs.pop().expect("This shouldn't be possible");
// If we're in shorthand mode, we need to reparse the left-hand side if possible
let (left, err) = shorthand_reparse(left, orig_left, scope, shorthand_mode);
@ -1179,20 +1193,8 @@ pub fn parse_math_expression(
working_exprs.push((None, op));
working_exprs.push(rhs_working_expr);
prec.push(next_prec);
}
idx += 1;
} else {
if error.is_none() {
error = Some(ParseError::argument_error(
lite_args[idx - 1].clone(),
ArgumentError::MissingMandatoryPositional("right hand side".into()),
));
}
working_exprs.push((None, garbage(op.span)));
working_exprs.push((None, garbage(op.span)));
prec.push(0);
}
}
while working_exprs.len() >= 3 {