forked from extern/nushell
Canonical expr block (#1584)
* Add the canonical form for an expression block * Remove commented out code
This commit is contained in:
parent
08a09e2273
commit
e3da037b80
36
crates/nu-cli/src/commands/classified/expr.rs
Normal file
36
crates/nu-cli/src/commands/classified/expr.rs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
use crate::evaluate::evaluate_baseline_expr;
|
||||||
|
use crate::prelude::*;
|
||||||
|
use log::{log_enabled, trace};
|
||||||
|
use nu_errors::ShellError;
|
||||||
|
use nu_protocol::hir::SpannedExpression;
|
||||||
|
|
||||||
|
use futures_util::pin_mut;
|
||||||
|
use nu_protocol::Scope;
|
||||||
|
|
||||||
|
pub(crate) fn run_expression_block(
|
||||||
|
expr: SpannedExpression,
|
||||||
|
context: &mut Context,
|
||||||
|
input: Option<InputStream>,
|
||||||
|
) -> Result<Option<InputStream>, ShellError> {
|
||||||
|
if log_enabled!(log::Level::Trace) {
|
||||||
|
trace!(target: "nu::run::expr", "->");
|
||||||
|
trace!(target: "nu::run::expr", "{:?}", expr);
|
||||||
|
}
|
||||||
|
|
||||||
|
let registry = context.registry().clone();
|
||||||
|
|
||||||
|
let stream = async_stream! {
|
||||||
|
if let Some(input) = input {
|
||||||
|
let values = input.values;
|
||||||
|
pin_mut!(values);
|
||||||
|
|
||||||
|
while let Some(row) = values.next().await {
|
||||||
|
yield evaluate_baseline_expr(&expr, ®istry, &Scope::new(row));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
yield evaluate_baseline_expr(&expr, ®istry, &Scope::empty());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Some(stream.to_input_stream()))
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
mod dynamic;
|
mod dynamic;
|
||||||
|
pub(crate) mod expr;
|
||||||
pub(crate) mod external;
|
pub(crate) mod external;
|
||||||
pub(crate) mod internal;
|
pub(crate) mod internal;
|
||||||
pub(crate) mod pipeline;
|
pub(crate) mod pipeline;
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use crate::commands::classified::expr::run_expression_block;
|
||||||
use crate::commands::classified::external::run_external_command;
|
use crate::commands::classified::external::run_external_command;
|
||||||
use crate::commands::classified::internal::run_internal_command;
|
use crate::commands::classified::internal::run_internal_command;
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
@ -21,9 +22,7 @@ pub(crate) async fn run_pipeline(
|
|||||||
return Err(ShellError::unimplemented("Dynamic commands"))
|
return Err(ShellError::unimplemented("Dynamic commands"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// (Some(ClassifiedCommand::Expr(_)), _) | (_, Some(ClassifiedCommand::Expr(_))) => {
|
(Some(ClassifiedCommand::Expr(expr)), _) => run_expression_block(*expr, ctx, input)?,
|
||||||
// return Err(ShellError::unimplemented("Expression-only commands"))
|
|
||||||
// }
|
|
||||||
(Some(ClassifiedCommand::Error(err)), _) => return Err(err.into()),
|
(Some(ClassifiedCommand::Error(err)), _) => return Err(err.into()),
|
||||||
(_, Some(ClassifiedCommand::Error(err))) => return Err(err.clone().into()),
|
(_, Some(ClassifiedCommand::Error(err))) => return Err(err.clone().into()),
|
||||||
|
|
||||||
@ -38,7 +37,6 @@ pub(crate) async fn run_pipeline(
|
|||||||
}
|
}
|
||||||
|
|
||||||
(None, _) => break,
|
(None, _) => break,
|
||||||
_ => unimplemented!("Not yet implented cases in run_pipeline"),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +20,24 @@ fn filters_with_nothing_comparison() {
|
|||||||
assert_eq!(actual, "7");
|
assert_eq!(actual, "7");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn explicit_block_condition() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: "tests/fixtures/formats", pipeline(
|
||||||
|
r#"
|
||||||
|
open sample.db
|
||||||
|
| where table_name == ints
|
||||||
|
| get table_values
|
||||||
|
| first 4
|
||||||
|
| where {= $it.z > 4200}
|
||||||
|
| get z
|
||||||
|
| echo $it
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
assert_eq!(actual, "4253");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn binary_operator_comparisons() {
|
fn binary_operator_comparisons() {
|
||||||
let actual = nu!(
|
let actual = nu!(
|
||||||
|
@ -459,7 +459,7 @@ fn parse_arg(
|
|||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SyntaxShape::Block => {
|
SyntaxShape::Block | SyntaxShape::Condition => {
|
||||||
// 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();
|
||||||
@ -496,14 +496,6 @@ fn parse_arg(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SyntaxShape::Condition => {
|
|
||||||
// We have an implied condition, but we can't parse this here
|
|
||||||
// it needed to have been parsed up higher where we have control over more than one arg
|
|
||||||
(
|
|
||||||
garbage(lite_arg.span),
|
|
||||||
Some(ParseError::mismatch("condition", lite_arg.clone())),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -604,19 +596,19 @@ fn classify_positional_arg(
|
|||||||
commands.push(ClassifiedCommand::Expr(Box::new(binary)));
|
commands.push(ClassifiedCommand::Expr(Box::new(binary)));
|
||||||
SpannedExpression::new(Expression::Block(commands), span)
|
SpannedExpression::new(Expression::Block(commands), span)
|
||||||
} else if idx < lite_cmd.args.len() {
|
} else if idx < lite_cmd.args.len() {
|
||||||
let (arg, err) =
|
|
||||||
parse_arg(SyntaxShape::FullColumnPath, registry, &lite_cmd.args[idx]);
|
|
||||||
if error.is_none() {
|
|
||||||
error = err;
|
|
||||||
}
|
|
||||||
arg
|
|
||||||
} else {
|
|
||||||
// TODO - this needs to get fixed and return an actual error
|
|
||||||
let (arg, err) = parse_arg(SyntaxShape::Condition, registry, &lite_cmd.args[idx]);
|
let (arg, err) = parse_arg(SyntaxShape::Condition, registry, &lite_cmd.args[idx]);
|
||||||
if error.is_none() {
|
if error.is_none() {
|
||||||
error = err;
|
error = err;
|
||||||
}
|
}
|
||||||
arg
|
arg
|
||||||
|
} else {
|
||||||
|
if error.is_none() {
|
||||||
|
error = Some(ParseError::argument_error(
|
||||||
|
lite_cmd.name.clone(),
|
||||||
|
ArgumentError::MissingMandatoryPositional("condition".into()),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
garbage(lite_cmd.span())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PositionalType::Mandatory(_, shape) => {
|
PositionalType::Mandatory(_, shape) => {
|
||||||
@ -806,6 +798,42 @@ pub fn classify_pipeline(
|
|||||||
span: Span::new(0, 0),
|
span: Span::new(0, 0),
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
|
} else if lite_cmd.name.item == "=" {
|
||||||
|
let idx = 0;
|
||||||
|
let expr = if (idx + 2) < lite_cmd.args.len() {
|
||||||
|
let (lhs, err) = parse_arg(SyntaxShape::Any, registry, &lite_cmd.args[idx]);
|
||||||
|
if error.is_none() {
|
||||||
|
error = err;
|
||||||
|
}
|
||||||
|
let (op, err) = parse_arg(SyntaxShape::Operator, registry, &lite_cmd.args[idx + 1]);
|
||||||
|
if error.is_none() {
|
||||||
|
error = err;
|
||||||
|
}
|
||||||
|
let (rhs, err) = parse_arg(SyntaxShape::Any, registry, &lite_cmd.args[idx + 2]);
|
||||||
|
if error.is_none() {
|
||||||
|
error = err;
|
||||||
|
}
|
||||||
|
let span = Span::new(lhs.span.start(), rhs.span.end());
|
||||||
|
SpannedExpression::new(
|
||||||
|
Expression::Binary(Box::new(Binary::new(lhs, op, rhs))),
|
||||||
|
span,
|
||||||
|
)
|
||||||
|
} else if idx < lite_cmd.args.len() {
|
||||||
|
let (arg, err) = parse_arg(SyntaxShape::Any, registry, &lite_cmd.args[idx]);
|
||||||
|
if error.is_none() {
|
||||||
|
error = err;
|
||||||
|
}
|
||||||
|
arg
|
||||||
|
} else {
|
||||||
|
if error.is_none() {
|
||||||
|
error = Some(ParseError::argument_error(
|
||||||
|
lite_cmd.name.clone(),
|
||||||
|
ArgumentError::MissingMandatoryPositional("an expression".into()),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
garbage(lite_cmd.span())
|
||||||
|
};
|
||||||
|
commands.push(ClassifiedCommand::Expr(Box::new(expr)))
|
||||||
} else if let Some(signature) = registry.get(&lite_cmd.name.item) {
|
} else if let Some(signature) = registry.get(&lite_cmd.name.item) {
|
||||||
let (internal_command, err) =
|
let (internal_command, err) =
|
||||||
classify_internal_command(&lite_cmd, registry, &signature);
|
classify_internal_command(&lite_cmd, registry, &signature);
|
||||||
|
Loading…
Reference in New Issue
Block a user