mirror of
https://github.com/nushell/nushell.git
synced 2025-04-01 03:36:53 +02:00
refactor parse_math_expression, reduce indentation (#3093)
This commit is contained in:
parent
aa6c6120f6
commit
120e80d1b6
@ -1093,8 +1093,9 @@ pub fn parse_math_expression(
|
|||||||
shorthand_mode: bool,
|
shorthand_mode: bool,
|
||||||
) -> (usize, SpannedExpression, Option<ParseError>) {
|
) -> (usize, SpannedExpression, Option<ParseError>) {
|
||||||
// Precedence parsing is included
|
// 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,
|
// shorthand_mode means that the left-hand side of an expression can point to a column-path.
|
||||||
// we parse as normal, but then go back and when we detect a left-hand side, reparse that value if it's a string
|
// 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 idx = 0;
|
||||||
let mut error = None;
|
let mut error = None;
|
||||||
@ -1121,7 +1122,19 @@ pub fn parse_math_expression(
|
|||||||
}
|
}
|
||||||
idx += 1;
|
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!(
|
trace!(
|
||||||
"idx: {} working_exprs: {:#?} prec: {:?}",
|
"idx: {} working_exprs: {:#?} prec: {:?}",
|
||||||
idx,
|
idx,
|
||||||
@ -1142,7 +1155,9 @@ pub fn parse_math_expression(
|
|||||||
prec.push(next_prec);
|
prec.push(next_prec);
|
||||||
working_exprs.push((None, op));
|
working_exprs.push((None, op));
|
||||||
working_exprs.push(rhs_working_expr);
|
working_exprs.push(rhs_working_expr);
|
||||||
} else {
|
idx += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
while !prec.is_empty()
|
while !prec.is_empty()
|
||||||
&& *prec.last().expect("This shouldn't happen") >= next_prec
|
&& *prec.last().expect("This shouldn't happen") >= next_prec
|
||||||
&& next_prec > 0 // Not garbage
|
&& 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 (_, right) = working_exprs.pop().expect("This shouldn't be possible");
|
||||||
let (_, op) = working_exprs.pop().expect("This shouldn't be possible");
|
let (_, op) = working_exprs.pop().expect("This shouldn't be possible");
|
||||||
let (orig_left, left) =
|
let (orig_left, left) = working_exprs.pop().expect("This shouldn't be possible");
|
||||||
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
|
// 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);
|
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((None, op));
|
||||||
working_exprs.push(rhs_working_expr);
|
working_exprs.push(rhs_working_expr);
|
||||||
prec.push(next_prec);
|
prec.push(next_prec);
|
||||||
}
|
|
||||||
|
|
||||||
idx += 1;
|
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 {
|
while working_exprs.len() >= 3 {
|
||||||
|
Loading…
Reference in New Issue
Block a user