From 67acaae53cd82843c1f8af889e0733f27af2e1b5 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Mon, 21 Dec 2020 17:32:06 +1300 Subject: [PATCH] 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 --- crates/nu-cli/src/commands/if_.rs | 2 +- crates/nu-cli/src/commands/keep/until.rs | 2 +- crates/nu-cli/src/commands/keep/while_.rs | 2 +- crates/nu-cli/src/commands/set.rs | 2 +- crates/nu-cli/src/commands/set_env.rs | 2 +- crates/nu-cli/src/commands/skip/until.rs | 2 +- crates/nu-cli/src/commands/skip/while_.rs | 2 +- crates/nu-cli/src/commands/where_.rs | 2 +- crates/nu-cli/src/types/deduction.rs | 15 ++++++++------- crates/nu-parser/src/parse.rs | 17 +++++++++-------- crates/nu-protocol/src/syntax_shape.rs | 11 ++++++----- 11 files changed, 31 insertions(+), 28 deletions(-) diff --git a/crates/nu-cli/src/commands/if_.rs b/crates/nu-cli/src/commands/if_.rs index dddd2bee3b..58a076bd7b 100644 --- a/crates/nu-cli/src/commands/if_.rs +++ b/crates/nu-cli/src/commands/if_.rs @@ -26,7 +26,7 @@ impl WholeStreamCommand for If { Signature::build("if") .required( "condition", - SyntaxShape::Math, + SyntaxShape::MathExpression, "the condition that must match", ) .required( diff --git a/crates/nu-cli/src/commands/keep/until.rs b/crates/nu-cli/src/commands/keep/until.rs index 777183dbcf..e5c5ff6162 100644 --- a/crates/nu-cli/src/commands/keep/until.rs +++ b/crates/nu-cli/src/commands/keep/until.rs @@ -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() diff --git a/crates/nu-cli/src/commands/keep/while_.rs b/crates/nu-cli/src/commands/keep/while_.rs index 6c2415368e..0fb0bc2322 100644 --- a/crates/nu-cli/src/commands/keep/while_.rs +++ b/crates/nu-cli/src/commands/keep/while_.rs @@ -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() diff --git a/crates/nu-cli/src/commands/set.rs b/crates/nu-cli/src/commands/set.rs index d3f78225b4..4e80289ead 100644 --- a/crates/nu-cli/src/commands/set.rs +++ b/crates/nu-cli/src/commands/set.rs @@ -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", ) } diff --git a/crates/nu-cli/src/commands/set_env.rs b/crates/nu-cli/src/commands/set_env.rs index 1c5c8764bf..2d20e1468b 100644 --- a/crates/nu-cli/src/commands/set_env.rs +++ b/crates/nu-cli/src/commands/set_env.rs @@ -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", ) } diff --git a/crates/nu-cli/src/commands/skip/until.rs b/crates/nu-cli/src/commands/skip/until.rs index ea0b9b50b6..f8fb830934 100644 --- a/crates/nu-cli/src/commands/skip/until.rs +++ b/crates/nu-cli/src/commands/skip/until.rs @@ -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() diff --git a/crates/nu-cli/src/commands/skip/while_.rs b/crates/nu-cli/src/commands/skip/while_.rs index 96865a2d78..9d9c112fbd 100644 --- a/crates/nu-cli/src/commands/skip/while_.rs +++ b/crates/nu-cli/src/commands/skip/while_.rs @@ -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() diff --git a/crates/nu-cli/src/commands/where_.rs b/crates/nu-cli/src/commands/where_.rs index 793f72b475..661e9374a3 100644 --- a/crates/nu-cli/src/commands/where_.rs +++ b/crates/nu-cli/src/commands/where_.rs @@ -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", ) } diff --git a/crates/nu-cli/src/types/deduction.rs b/crates/nu-cli/src/types/deduction.rs index a4ecc3a4f5..ebdb433ed9 100644 --- a/crates/nu-cli/src/types/deduction.rs +++ b/crates/nu-cli/src/types/deduction.rs @@ -290,7 +290,7 @@ fn get_shape_of_expr(expr: &SpannedExpression) -> Option { 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,12 +690,13 @@ 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( - (var, expr), - source_bin, - (pipeline_idx, pipeline), - scope, - ), + SyntaxShape::RowCondition => self + .get_result_shape_of_math_expr_or_insert_dependency( + (var, expr), + source_bin, + (pipeline_idx, pipeline), + scope, + ), _ => Ok(Some(shape)), } } else { diff --git a/crates/nu-parser/src/parse.rs b/crates/nu-parser/src/parse.rs index cffe4d7d13..c43ae343a9 100644 --- a/crates/nu-parser/src/parse.rs +++ b/crates/nu-parser/src/parse.rs @@ -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 // 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(""), + Signature::new(""), vec![Group::new(vec![commands], lite_cmd.span())], IndexMap::new(), span, diff --git a/crates/nu-protocol/src/syntax_shape.rs b/crates/nu-protocol/src/syntax_shape.rs index 26ae84a358..16aca72cf4 100644 --- a/crates/nu-protocol/src/syntax_shape.rs +++ b/crates/nu-protocol/src/syntax_shape.rs @@ -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", }) } }