mirror of
https://github.com/nushell/nushell.git
synced 2025-01-11 08:48:23 +01:00
Rename cond math (#2807)
* Simplifies 'if' to work on the available scope rather than a stream * Rename initializer/math for better readability * Fix description * fmt
This commit is contained in:
parent
e3da546e23
commit
67acaae53c
@ -26,7 +26,7 @@ impl WholeStreamCommand for If {
|
||||
Signature::build("if")
|
||||
.required(
|
||||
"condition",
|
||||
SyntaxShape::Math,
|
||||
SyntaxShape::MathExpression,
|
||||
"the condition that must match",
|
||||
)
|
||||
.required(
|
||||
|
@ -18,7 +18,7 @@ impl WholeStreamCommand for SubCommand {
|
||||
Signature::build("keep until")
|
||||
.required(
|
||||
"condition",
|
||||
SyntaxShape::Math,
|
||||
SyntaxShape::RowCondition,
|
||||
"The condition that must be met to stop keeping rows",
|
||||
)
|
||||
.filter()
|
||||
|
@ -17,7 +17,7 @@ impl WholeStreamCommand for SubCommand {
|
||||
Signature::build("keep while")
|
||||
.required(
|
||||
"condition",
|
||||
SyntaxShape::Math,
|
||||
SyntaxShape::RowCondition,
|
||||
"The condition that must be met to keep rows",
|
||||
)
|
||||
.filter()
|
||||
|
@ -29,7 +29,7 @@ impl WholeStreamCommand for Set {
|
||||
.required("equals", SyntaxShape::String, "the equals sign")
|
||||
.required(
|
||||
"expr",
|
||||
SyntaxShape::Initializer,
|
||||
SyntaxShape::MathExpression,
|
||||
"the value to set the variable to",
|
||||
)
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ impl WholeStreamCommand for SetEnv {
|
||||
.required("equals", SyntaxShape::String, "the equals sign")
|
||||
.required(
|
||||
"expr",
|
||||
SyntaxShape::Initializer,
|
||||
SyntaxShape::MathExpression,
|
||||
"the value to set the environment variable to",
|
||||
)
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ impl WholeStreamCommand for SubCommand {
|
||||
Signature::build("skip until")
|
||||
.required(
|
||||
"condition",
|
||||
SyntaxShape::Math,
|
||||
SyntaxShape::RowCondition,
|
||||
"The condition that must be met to stop skipping",
|
||||
)
|
||||
.filter()
|
||||
|
@ -17,7 +17,7 @@ impl WholeStreamCommand for SubCommand {
|
||||
Signature::build("skip while")
|
||||
.required(
|
||||
"condition",
|
||||
SyntaxShape::Math,
|
||||
SyntaxShape::RowCondition,
|
||||
"The condition that must be met to continue skipping",
|
||||
)
|
||||
.filter()
|
||||
|
@ -22,7 +22,7 @@ impl WholeStreamCommand for Where {
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("where").required(
|
||||
"condition",
|
||||
SyntaxShape::Math,
|
||||
SyntaxShape::RowCondition,
|
||||
"the condition that must match",
|
||||
)
|
||||
}
|
||||
|
@ -290,7 +290,7 @@ fn get_shape_of_expr(expr: &SpannedExpression) -> Option<SyntaxShape> {
|
||||
Expression::ExternalWord => Some(SyntaxShape::String),
|
||||
Expression::Synthetic(_) => Some(SyntaxShape::String),
|
||||
|
||||
Expression::Binary(_) => Some(SyntaxShape::Math),
|
||||
Expression::Binary(_) => Some(SyntaxShape::RowCondition),
|
||||
Expression::Range(_) => Some(SyntaxShape::Range),
|
||||
Expression::List(_) => Some(SyntaxShape::Table),
|
||||
Expression::Boolean(_) => Some(SyntaxShape::String),
|
||||
@ -690,7 +690,8 @@ impl VarSyntaxShapeDeductor {
|
||||
match shape {
|
||||
//If the shape of expr is math, we return the result shape of this math expr if
|
||||
//possible
|
||||
SyntaxShape::Math => self.get_result_shape_of_math_expr_or_insert_dependency(
|
||||
SyntaxShape::RowCondition => self
|
||||
.get_result_shape_of_math_expr_or_insert_dependency(
|
||||
(var, expr),
|
||||
source_bin,
|
||||
(pipeline_idx, pipeline),
|
||||
|
@ -910,9 +910,9 @@ fn parse_arg(
|
||||
),
|
||||
}
|
||||
}
|
||||
SyntaxShape::Initializer => parse_arg(SyntaxShape::Any, scope, lite_arg),
|
||||
SyntaxShape::MathExpression => parse_arg(SyntaxShape::Any, scope, lite_arg),
|
||||
|
||||
SyntaxShape::Block | SyntaxShape::Math => {
|
||||
SyntaxShape::Block | SyntaxShape::RowCondition => {
|
||||
// Blocks have one of two forms: the literal block and the implied block
|
||||
// To parse a literal block, we need to detect that what we have is itself a block
|
||||
let mut chars = lite_arg.item.chars();
|
||||
@ -1355,8 +1355,8 @@ fn parse_positional_argument(
|
||||
let mut idx = idx;
|
||||
let mut error = None;
|
||||
let arg = match positional_type {
|
||||
PositionalType::Mandatory(_, SyntaxShape::Initializer)
|
||||
| PositionalType::Optional(_, SyntaxShape::Initializer) => {
|
||||
PositionalType::Mandatory(_, SyntaxShape::MathExpression)
|
||||
| PositionalType::Optional(_, SyntaxShape::MathExpression) => {
|
||||
let end_idx = if (lite_cmd.parts.len() - 1) > remaining_positionals {
|
||||
lite_cmd.parts.len() - remaining_positionals
|
||||
} else {
|
||||
@ -1385,15 +1385,16 @@ fn parse_positional_argument(
|
||||
}
|
||||
arg
|
||||
}
|
||||
PositionalType::Mandatory(_, SyntaxShape::Math)
|
||||
| PositionalType::Optional(_, SyntaxShape::Math) => {
|
||||
PositionalType::Mandatory(_, SyntaxShape::RowCondition)
|
||||
| PositionalType::Optional(_, SyntaxShape::RowCondition) => {
|
||||
// A condition can take up multiple arguments, as we build the operation as <arg> <operator> <arg>
|
||||
// We need to do this here because in parse_arg, we have access to only one arg at a time
|
||||
|
||||
if idx < lite_cmd.parts.len() {
|
||||
if lite_cmd.parts[idx].item.starts_with('{') {
|
||||
// It's an explicit math expression, so parse it deeper in
|
||||
let (arg, err) = parse_arg(SyntaxShape::Math, scope, &lite_cmd.parts[idx]);
|
||||
let (arg, err) =
|
||||
parse_arg(SyntaxShape::RowCondition, scope, &lite_cmd.parts[idx]);
|
||||
if error.is_none() {
|
||||
error = err;
|
||||
}
|
||||
@ -1413,7 +1414,7 @@ fn parse_positional_argument(
|
||||
commands.push(ClassifiedCommand::Expr(Box::new(arg)));
|
||||
|
||||
let block = hir::Block::new(
|
||||
Signature::new("<math>"),
|
||||
Signature::new("<cond>"),
|
||||
vec![Group::new(vec![commands], lite_cmd.span())],
|
||||
IndexMap::new(),
|
||||
span,
|
||||
|
@ -31,9 +31,10 @@ pub enum SyntaxShape {
|
||||
/// An operator
|
||||
Operator,
|
||||
/// A math expression which expands shorthand forms on the lefthand side, eg `foo > 1`
|
||||
Math,
|
||||
/// An initializer expression, eg the right hand side of `set x = 1 + 2`
|
||||
Initializer,
|
||||
/// The shorthand allows us to more easily reach columns inside of the row being passed in
|
||||
RowCondition,
|
||||
/// A general math expression, eg the `1 + 2` of `= 1 + 2`
|
||||
MathExpression,
|
||||
}
|
||||
|
||||
impl PrettyDebug for SyntaxShape {
|
||||
@ -53,8 +54,8 @@ impl PrettyDebug for SyntaxShape {
|
||||
SyntaxShape::Table => "table",
|
||||
SyntaxShape::Unit => "unit",
|
||||
SyntaxShape::Operator => "operator",
|
||||
SyntaxShape::Math => "condition",
|
||||
SyntaxShape::Initializer => "initializer",
|
||||
SyntaxShape::RowCondition => "condition",
|
||||
SyntaxShape::MathExpression => "math expression",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user