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:
Jonathan Turner 2020-12-21 17:32:06 +13:00 committed by GitHub
parent e3da546e23
commit 67acaae53c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 31 additions and 28 deletions

View File

@ -26,7 +26,7 @@ impl WholeStreamCommand for If {
Signature::build("if") Signature::build("if")
.required( .required(
"condition", "condition",
SyntaxShape::Math, SyntaxShape::MathExpression,
"the condition that must match", "the condition that must match",
) )
.required( .required(

View File

@ -18,7 +18,7 @@ impl WholeStreamCommand for SubCommand {
Signature::build("keep until") Signature::build("keep until")
.required( .required(
"condition", "condition",
SyntaxShape::Math, SyntaxShape::RowCondition,
"The condition that must be met to stop keeping rows", "The condition that must be met to stop keeping rows",
) )
.filter() .filter()

View File

@ -17,7 +17,7 @@ impl WholeStreamCommand for SubCommand {
Signature::build("keep while") Signature::build("keep while")
.required( .required(
"condition", "condition",
SyntaxShape::Math, SyntaxShape::RowCondition,
"The condition that must be met to keep rows", "The condition that must be met to keep rows",
) )
.filter() .filter()

View File

@ -29,7 +29,7 @@ impl WholeStreamCommand for Set {
.required("equals", SyntaxShape::String, "the equals sign") .required("equals", SyntaxShape::String, "the equals sign")
.required( .required(
"expr", "expr",
SyntaxShape::Initializer, SyntaxShape::MathExpression,
"the value to set the variable to", "the value to set the variable to",
) )
} }

View File

@ -33,7 +33,7 @@ impl WholeStreamCommand for SetEnv {
.required("equals", SyntaxShape::String, "the equals sign") .required("equals", SyntaxShape::String, "the equals sign")
.required( .required(
"expr", "expr",
SyntaxShape::Initializer, SyntaxShape::MathExpression,
"the value to set the environment variable to", "the value to set the environment variable to",
) )
} }

View File

@ -17,7 +17,7 @@ impl WholeStreamCommand for SubCommand {
Signature::build("skip until") Signature::build("skip until")
.required( .required(
"condition", "condition",
SyntaxShape::Math, SyntaxShape::RowCondition,
"The condition that must be met to stop skipping", "The condition that must be met to stop skipping",
) )
.filter() .filter()

View File

@ -17,7 +17,7 @@ impl WholeStreamCommand for SubCommand {
Signature::build("skip while") Signature::build("skip while")
.required( .required(
"condition", "condition",
SyntaxShape::Math, SyntaxShape::RowCondition,
"The condition that must be met to continue skipping", "The condition that must be met to continue skipping",
) )
.filter() .filter()

View File

@ -22,7 +22,7 @@ impl WholeStreamCommand for Where {
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("where").required( Signature::build("where").required(
"condition", "condition",
SyntaxShape::Math, SyntaxShape::RowCondition,
"the condition that must match", "the condition that must match",
) )
} }

View File

@ -290,7 +290,7 @@ fn get_shape_of_expr(expr: &SpannedExpression) -> Option<SyntaxShape> {
Expression::ExternalWord => Some(SyntaxShape::String), Expression::ExternalWord => Some(SyntaxShape::String),
Expression::Synthetic(_) => Some(SyntaxShape::String), Expression::Synthetic(_) => Some(SyntaxShape::String),
Expression::Binary(_) => Some(SyntaxShape::Math), Expression::Binary(_) => Some(SyntaxShape::RowCondition),
Expression::Range(_) => Some(SyntaxShape::Range), Expression::Range(_) => Some(SyntaxShape::Range),
Expression::List(_) => Some(SyntaxShape::Table), Expression::List(_) => Some(SyntaxShape::Table),
Expression::Boolean(_) => Some(SyntaxShape::String), Expression::Boolean(_) => Some(SyntaxShape::String),
@ -690,12 +690,13 @@ impl VarSyntaxShapeDeductor {
match shape { match shape {
//If the shape of expr is math, we return the result shape of this math expr if //If the shape of expr is math, we return the result shape of this math expr if
//possible //possible
SyntaxShape::Math => self.get_result_shape_of_math_expr_or_insert_dependency( SyntaxShape::RowCondition => self
(var, expr), .get_result_shape_of_math_expr_or_insert_dependency(
source_bin, (var, expr),
(pipeline_idx, pipeline), source_bin,
scope, (pipeline_idx, pipeline),
), scope,
),
_ => Ok(Some(shape)), _ => Ok(Some(shape)),
} }
} else { } else {

View File

@ -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 // 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 // To parse a literal block, we need to detect that what we have is itself a block
let mut chars = lite_arg.item.chars(); let mut chars = lite_arg.item.chars();
@ -1355,8 +1355,8 @@ fn parse_positional_argument(
let mut idx = idx; let mut idx = idx;
let mut error = None; let mut error = None;
let arg = match positional_type { let arg = match positional_type {
PositionalType::Mandatory(_, SyntaxShape::Initializer) PositionalType::Mandatory(_, SyntaxShape::MathExpression)
| PositionalType::Optional(_, SyntaxShape::Initializer) => { | PositionalType::Optional(_, SyntaxShape::MathExpression) => {
let end_idx = if (lite_cmd.parts.len() - 1) > remaining_positionals { let end_idx = if (lite_cmd.parts.len() - 1) > remaining_positionals {
lite_cmd.parts.len() - remaining_positionals lite_cmd.parts.len() - remaining_positionals
} else { } else {
@ -1385,15 +1385,16 @@ fn parse_positional_argument(
} }
arg arg
} }
PositionalType::Mandatory(_, SyntaxShape::Math) PositionalType::Mandatory(_, SyntaxShape::RowCondition)
| PositionalType::Optional(_, SyntaxShape::Math) => { | PositionalType::Optional(_, SyntaxShape::RowCondition) => {
// A condition can take up multiple arguments, as we build the operation as <arg> <operator> <arg> // 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 // 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 idx < lite_cmd.parts.len() {
if lite_cmd.parts[idx].item.starts_with('{') { if lite_cmd.parts[idx].item.starts_with('{') {
// It's an explicit math expression, so parse it deeper in // 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() { if error.is_none() {
error = err; error = err;
} }
@ -1413,7 +1414,7 @@ fn parse_positional_argument(
commands.push(ClassifiedCommand::Expr(Box::new(arg))); commands.push(ClassifiedCommand::Expr(Box::new(arg)));
let block = hir::Block::new( let block = hir::Block::new(
Signature::new("<math>"), Signature::new("<cond>"),
vec![Group::new(vec![commands], lite_cmd.span())], vec![Group::new(vec![commands], lite_cmd.span())],
IndexMap::new(), IndexMap::new(),
span, span,

View File

@ -31,9 +31,10 @@ pub enum SyntaxShape {
/// An operator /// An operator
Operator, Operator,
/// A math expression which expands shorthand forms on the lefthand side, eg `foo > 1` /// A math expression which expands shorthand forms on the lefthand side, eg `foo > 1`
Math, /// The shorthand allows us to more easily reach columns inside of the row being passed in
/// An initializer expression, eg the right hand side of `set x = 1 + 2` RowCondition,
Initializer, /// A general math expression, eg the `1 + 2` of `= 1 + 2`
MathExpression,
} }
impl PrettyDebug for SyntaxShape { impl PrettyDebug for SyntaxShape {
@ -53,8 +54,8 @@ impl PrettyDebug for SyntaxShape {
SyntaxShape::Table => "table", SyntaxShape::Table => "table",
SyntaxShape::Unit => "unit", SyntaxShape::Unit => "unit",
SyntaxShape::Operator => "operator", SyntaxShape::Operator => "operator",
SyntaxShape::Math => "condition", SyntaxShape::RowCondition => "condition",
SyntaxShape::Initializer => "initializer", SyntaxShape::MathExpression => "math expression",
}) })
} }
} }