forked from extern/nushell
Pipeline operators: &&
and ||
(#7448)
# Description We got some feedback from folks used to other shells that `try/catch` isn't quite as convenient as things like `||`. This PR adds `&&` as a synonym for `;` and `||` as equivalent to what `try/catch` would do. # User-Facing Changes Adds `&&` and `||` pipeline operators. # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
parent
7917cf9f00
commit
35bea5e044
@ -127,7 +127,6 @@ impl NuCompleter {
|
|||||||
match pipeline_element {
|
match pipeline_element {
|
||||||
PipelineElement::Expression(_, expr)
|
PipelineElement::Expression(_, expr)
|
||||||
| PipelineElement::Redirection(_, _, expr)
|
| PipelineElement::Redirection(_, _, expr)
|
||||||
| PipelineElement::And(_, expr)
|
|
||||||
| PipelineElement::Or(_, expr) => {
|
| PipelineElement::Or(_, expr) => {
|
||||||
let flattened: Vec<_> = flatten_expression(&working_set, &expr);
|
let flattened: Vec<_> = flatten_expression(&working_set, &expr);
|
||||||
let span_offset: usize = alias_offset.iter().sum();
|
let span_offset: usize = alias_offset.iter().sum();
|
||||||
|
@ -232,7 +232,6 @@ fn find_matching_block_end_in_block(
|
|||||||
match e {
|
match e {
|
||||||
PipelineElement::Expression(_, e)
|
PipelineElement::Expression(_, e)
|
||||||
| PipelineElement::Redirection(_, _, e)
|
| PipelineElement::Redirection(_, _, e)
|
||||||
| PipelineElement::And(_, e)
|
|
||||||
| PipelineElement::Or(_, e) => {
|
| PipelineElement::Or(_, e) => {
|
||||||
if e.span.contains(global_cursor_offset) {
|
if e.span.contains(global_cursor_offset) {
|
||||||
if let Some(pos) = find_matching_block_end_in_expr(
|
if let Some(pos) = find_matching_block_end_in_expr(
|
||||||
|
@ -146,7 +146,6 @@ impl Command for FromNuon {
|
|||||||
match pipeline.elements.remove(0) {
|
match pipeline.elements.remove(0) {
|
||||||
PipelineElement::Expression(_, expression)
|
PipelineElement::Expression(_, expression)
|
||||||
| PipelineElement::Redirection(_, _, expression)
|
| PipelineElement::Redirection(_, _, expression)
|
||||||
| PipelineElement::And(_, expression)
|
|
||||||
| PipelineElement::Or(_, expression) => expression,
|
| PipelineElement::Or(_, expression) => expression,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -789,14 +789,16 @@ pub fn eval_element_with_input(
|
|||||||
redirect_stderr: bool,
|
redirect_stderr: bool,
|
||||||
) -> Result<(PipelineData, bool), ShellError> {
|
) -> Result<(PipelineData, bool), ShellError> {
|
||||||
match element {
|
match element {
|
||||||
PipelineElement::Expression(_, expr) => eval_expression_with_input(
|
PipelineElement::Expression(_, expr) | PipelineElement::Or(_, expr) => {
|
||||||
|
eval_expression_with_input(
|
||||||
engine_state,
|
engine_state,
|
||||||
stack,
|
stack,
|
||||||
expr,
|
expr,
|
||||||
input,
|
input,
|
||||||
redirect_stdout,
|
redirect_stdout,
|
||||||
redirect_stderr,
|
redirect_stderr,
|
||||||
),
|
)
|
||||||
|
}
|
||||||
PipelineElement::Redirection(span, redirection, expr) => match &expr.expr {
|
PipelineElement::Redirection(span, redirection, expr) => match &expr.expr {
|
||||||
Expr::String(_) => {
|
Expr::String(_) => {
|
||||||
let input = match (redirection, input) {
|
let input = match (redirection, input) {
|
||||||
@ -903,22 +905,6 @@ pub fn eval_element_with_input(
|
|||||||
}
|
}
|
||||||
_ => Err(ShellError::CommandNotFound(*span)),
|
_ => Err(ShellError::CommandNotFound(*span)),
|
||||||
},
|
},
|
||||||
PipelineElement::And(_, expr) => eval_expression_with_input(
|
|
||||||
engine_state,
|
|
||||||
stack,
|
|
||||||
expr,
|
|
||||||
input,
|
|
||||||
redirect_stdout,
|
|
||||||
redirect_stderr,
|
|
||||||
),
|
|
||||||
PipelineElement::Or(_, expr) => eval_expression_with_input(
|
|
||||||
engine_state,
|
|
||||||
stack,
|
|
||||||
expr,
|
|
||||||
input,
|
|
||||||
redirect_stdout,
|
|
||||||
redirect_stderr,
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -952,10 +938,16 @@ pub fn eval_block(
|
|||||||
redirect_stderr: bool,
|
redirect_stderr: bool,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let num_pipelines = block.len();
|
let num_pipelines = block.len();
|
||||||
for (pipeline_idx, pipeline) in block.pipelines.iter().enumerate() {
|
let mut pipeline_idx = 0;
|
||||||
|
|
||||||
|
while pipeline_idx < block.pipelines.len() {
|
||||||
|
let pipeline = &block.pipelines[pipeline_idx];
|
||||||
|
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
|
|
||||||
while i < pipeline.elements.len() {
|
let mut last_element_is_pipeline_or = false;
|
||||||
|
|
||||||
|
while i < pipeline.elements.len() && !last_element_is_pipeline_or {
|
||||||
let redirect_stderr = redirect_stderr
|
let redirect_stderr = redirect_stderr
|
||||||
|| ((i < pipeline.elements.len() - 1)
|
|| ((i < pipeline.elements.len() - 1)
|
||||||
&& (matches!(
|
&& (matches!(
|
||||||
@ -964,6 +956,8 @@ pub fn eval_block(
|
|||||||
| PipelineElement::Redirection(_, Redirection::StdoutAndStderr, _)
|
| PipelineElement::Redirection(_, Redirection::StdoutAndStderr, _)
|
||||||
)));
|
)));
|
||||||
|
|
||||||
|
last_element_is_pipeline_or = matches!(&pipeline.elements[i], PipelineElement::Or(..));
|
||||||
|
|
||||||
// if eval internal command failed, it can just make early return with `Err(ShellError)`.
|
// if eval internal command failed, it can just make early return with `Err(ShellError)`.
|
||||||
let eval_result = eval_element_with_input(
|
let eval_result = eval_element_with_input(
|
||||||
engine_state,
|
engine_state,
|
||||||
@ -990,10 +984,16 @@ pub fn eval_block(
|
|||||||
// don't return `Err(ShellError)`, so nushell wouldn't show extra error message.
|
// don't return `Err(ShellError)`, so nushell wouldn't show extra error message.
|
||||||
}
|
}
|
||||||
(Err(error), true) => input = PipelineData::Value(Value::Error { error }, None),
|
(Err(error), true) => input = PipelineData::Value(Value::Error { error }, None),
|
||||||
|
(output, false) if last_element_is_pipeline_or => match output {
|
||||||
|
Ok(output) => {
|
||||||
|
input = output.0;
|
||||||
|
}
|
||||||
|
Err(error) => input = PipelineData::Value(Value::Error { error }, None),
|
||||||
|
},
|
||||||
(output, false) => {
|
(output, false) => {
|
||||||
let output = output?;
|
let output = output?;
|
||||||
input = output.0;
|
input = output.0;
|
||||||
// external command may runs to failed
|
// external command may have failed
|
||||||
// make early return so remaining commands will not be executed.
|
// make early return so remaining commands will not be executed.
|
||||||
// don't return `Err(ShellError)`, so nushell wouldn't show extra error message.
|
// don't return `Err(ShellError)`, so nushell wouldn't show extra error message.
|
||||||
if output.1 {
|
if output.1 {
|
||||||
@ -1006,6 +1006,40 @@ pub fn eval_block(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if pipeline_idx < (num_pipelines) - 1 {
|
if pipeline_idx < (num_pipelines) - 1 {
|
||||||
|
if last_element_is_pipeline_or {
|
||||||
|
let input_is_error = matches!(input, PipelineData::Value(Value::Error { .. }, ..));
|
||||||
|
|
||||||
|
let result =
|
||||||
|
drain_and_print(engine_state, stack, input, redirect_stdout, redirect_stderr);
|
||||||
|
|
||||||
|
let last_exit_code = stack.last_exit_code(engine_state).unwrap_or(0);
|
||||||
|
|
||||||
|
if last_exit_code == 0 && result.is_ok() && !input_is_error {
|
||||||
|
// Skip the next pipeline ot run because this pipeline was successful and the
|
||||||
|
// user used the `a || b` connector
|
||||||
|
pipeline_idx += 1;
|
||||||
|
}
|
||||||
|
input = PipelineData::empty()
|
||||||
|
} else {
|
||||||
|
drain_and_print(engine_state, stack, input, redirect_stdout, redirect_stderr)?;
|
||||||
|
|
||||||
|
input = PipelineData::empty()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pipeline_idx += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn drain_and_print(
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
mut input: PipelineData,
|
||||||
|
redirect_stdout: bool,
|
||||||
|
redirect_stderr: bool,
|
||||||
|
) -> Result<(), ShellError> {
|
||||||
match input {
|
match input {
|
||||||
PipelineData::Value(Value::Nothing { .. }, ..) => {}
|
PipelineData::Value(Value::Nothing { .. }, ..) => {}
|
||||||
PipelineData::ExternalStream {
|
PipelineData::ExternalStream {
|
||||||
@ -1059,12 +1093,8 @@ pub fn eval_block(
|
|||||||
redirect_stderr,
|
redirect_stderr,
|
||||||
)?;
|
)?;
|
||||||
} else {
|
} else {
|
||||||
let table = table.run(
|
let table =
|
||||||
engine_state,
|
table.run(engine_state, stack, &Call::new(Span::new(0, 0)), input)?;
|
||||||
stack,
|
|
||||||
&Call::new(Span::new(0, 0)),
|
|
||||||
input,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
print_or_return(table, config)?;
|
print_or_return(table, config)?;
|
||||||
}
|
}
|
||||||
@ -1076,11 +1106,7 @@ pub fn eval_block(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
input = PipelineData::empty()
|
Ok(())
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(input)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_or_return(pipeline_data: PipelineData, config: &Config) -> Result<(), ShellError> {
|
fn print_or_return(pipeline_data: PipelineData, config: &Config) -> Result<(), ShellError> {
|
||||||
|
@ -455,14 +455,10 @@ pub fn flatten_pipeline_element(
|
|||||||
output.append(&mut flatten_expression(working_set, expr));
|
output.append(&mut flatten_expression(working_set, expr));
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
PipelineElement::And(span, expr) => {
|
|
||||||
let mut output = vec![(*span, FlatShape::And)];
|
|
||||||
output.append(&mut flatten_expression(working_set, expr));
|
|
||||||
output
|
|
||||||
}
|
|
||||||
PipelineElement::Or(span, expr) => {
|
PipelineElement::Or(span, expr) => {
|
||||||
let mut output = vec![(*span, FlatShape::Or)];
|
let mut output = vec![];
|
||||||
output.append(&mut flatten_expression(working_set, expr));
|
output.append(&mut flatten_expression(working_set, expr));
|
||||||
|
output.push((*span, FlatShape::Or));
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ pub enum TokenContents {
|
|||||||
Comment,
|
Comment,
|
||||||
Pipe,
|
Pipe,
|
||||||
PipePipe,
|
PipePipe,
|
||||||
|
AndAnd,
|
||||||
Semicolon,
|
Semicolon,
|
||||||
OutGreaterThan,
|
OutGreaterThan,
|
||||||
ErrGreaterThan,
|
ErrGreaterThan,
|
||||||
@ -254,10 +255,10 @@ pub fn lex_item(
|
|||||||
),
|
),
|
||||||
b"&&" => (
|
b"&&" => (
|
||||||
Token {
|
Token {
|
||||||
contents: TokenContents::Item,
|
contents: TokenContents::AndAnd,
|
||||||
span,
|
span,
|
||||||
},
|
},
|
||||||
Some(ParseError::ShellAndAnd(span)),
|
None,
|
||||||
),
|
),
|
||||||
b"2>" => (
|
b"2>" => (
|
||||||
Token {
|
Token {
|
||||||
|
@ -1420,7 +1420,9 @@ pub fn parse_module_block(
|
|||||||
|
|
||||||
pipeline
|
pipeline
|
||||||
}
|
}
|
||||||
LiteElement::Redirection(_, _, command) => garbage_pipeline(&command.parts),
|
LiteElement::Redirection(_, _, command) | LiteElement::Or(_, command) => {
|
||||||
|
garbage_pipeline(&command.parts)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
error = Some(ParseError::Expected("not a pipeline".into(), span));
|
error = Some(ParseError::Expected("not a pipeline".into(), span));
|
||||||
|
@ -1223,6 +1223,7 @@ fn parse_binary_with_base(
|
|||||||
}
|
}
|
||||||
TokenContents::Pipe
|
TokenContents::Pipe
|
||||||
| TokenContents::PipePipe
|
| TokenContents::PipePipe
|
||||||
|
| TokenContents::AndAnd
|
||||||
| TokenContents::OutGreaterThan
|
| TokenContents::OutGreaterThan
|
||||||
| TokenContents::ErrGreaterThan
|
| TokenContents::ErrGreaterThan
|
||||||
| TokenContents::OutErrGreaterThan => {
|
| TokenContents::OutErrGreaterThan => {
|
||||||
@ -3804,7 +3805,9 @@ pub fn parse_table_expression(
|
|||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
match &output.block[0].commands[0] {
|
match &output.block[0].commands[0] {
|
||||||
LiteElement::Command(_, command) | LiteElement::Redirection(_, _, command) => {
|
LiteElement::Command(_, command)
|
||||||
|
| LiteElement::Redirection(_, _, command)
|
||||||
|
| LiteElement::Or(_, command) => {
|
||||||
let mut table_headers = vec![];
|
let mut table_headers = vec![];
|
||||||
|
|
||||||
let (headers, err) = parse_value(
|
let (headers, err) = parse_value(
|
||||||
@ -3825,7 +3828,8 @@ pub fn parse_table_expression(
|
|||||||
|
|
||||||
match &output.block[1].commands[0] {
|
match &output.block[1].commands[0] {
|
||||||
LiteElement::Command(_, command)
|
LiteElement::Command(_, command)
|
||||||
| LiteElement::Redirection(_, _, command) => {
|
| LiteElement::Redirection(_, _, command)
|
||||||
|
| LiteElement::Or(_, command) => {
|
||||||
let mut rows = vec![];
|
let mut rows = vec![];
|
||||||
for part in &command.parts {
|
for part in &command.parts {
|
||||||
let (values, err) = parse_value(
|
let (values, err) = parse_value(
|
||||||
@ -5282,7 +5286,9 @@ pub fn parse_block(
|
|||||||
for pipeline in &lite_block.block {
|
for pipeline in &lite_block.block {
|
||||||
if pipeline.commands.len() == 1 {
|
if pipeline.commands.len() == 1 {
|
||||||
match &pipeline.commands[0] {
|
match &pipeline.commands[0] {
|
||||||
LiteElement::Command(_, command) | LiteElement::Redirection(_, _, command) => {
|
LiteElement::Command(_, command)
|
||||||
|
| LiteElement::Redirection(_, _, command)
|
||||||
|
| LiteElement::Or(_, command) => {
|
||||||
if let Some(err) =
|
if let Some(err) =
|
||||||
parse_def_predecl(working_set, &command.parts, expand_aliases_denylist)
|
parse_def_predecl(working_set, &command.parts, expand_aliases_denylist)
|
||||||
{
|
{
|
||||||
@ -5319,6 +5325,21 @@ pub fn parse_block(
|
|||||||
|
|
||||||
PipelineElement::Expression(*span, expr)
|
PipelineElement::Expression(*span, expr)
|
||||||
}
|
}
|
||||||
|
LiteElement::Or(span, command) => {
|
||||||
|
let (expr, err) = parse_expression(
|
||||||
|
working_set,
|
||||||
|
&command.parts,
|
||||||
|
expand_aliases_denylist,
|
||||||
|
is_subexpression,
|
||||||
|
);
|
||||||
|
working_set.type_scope.add_type(expr.ty.clone());
|
||||||
|
|
||||||
|
if error.is_none() {
|
||||||
|
error = err;
|
||||||
|
}
|
||||||
|
|
||||||
|
PipelineElement::Or(*span, expr)
|
||||||
|
}
|
||||||
LiteElement::Redirection(span, redirection, command) => {
|
LiteElement::Redirection(span, redirection, command) => {
|
||||||
trace!("parsing: pipeline element: redirection");
|
trace!("parsing: pipeline element: redirection");
|
||||||
let (expr, err) = parse_string(
|
let (expr, err) = parse_string(
|
||||||
@ -5354,8 +5375,16 @@ pub fn parse_block(
|
|||||||
|
|
||||||
Pipeline { elements: output }
|
Pipeline { elements: output }
|
||||||
} else {
|
} else {
|
||||||
match &pipeline.commands[0] {
|
let (mut pipeline, err) = match &pipeline.commands[0] {
|
||||||
LiteElement::Command(_, command) | LiteElement::Redirection(_, _, command) => {
|
LiteElement::Command(_, command) | LiteElement::Redirection(_, _, command) => {
|
||||||
|
parse_builtin_commands(
|
||||||
|
working_set,
|
||||||
|
command,
|
||||||
|
expand_aliases_denylist,
|
||||||
|
is_subexpression,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
LiteElement::Or(span, command) => {
|
||||||
let (mut pipeline, err) = parse_builtin_commands(
|
let (mut pipeline, err) = parse_builtin_commands(
|
||||||
working_set,
|
working_set,
|
||||||
command,
|
command,
|
||||||
@ -5363,10 +5392,17 @@ pub fn parse_block(
|
|||||||
is_subexpression,
|
is_subexpression,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if let PipelineElement::Expression(_, expr) = &pipeline.elements[0] {
|
||||||
|
pipeline.elements[0] = PipelineElement::Or(*span, expr.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
(pipeline, err)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if idx == 0 {
|
if idx == 0 {
|
||||||
if let Some(let_decl_id) = working_set.find_decl(b"let", &Type::Any) {
|
if let Some(let_decl_id) = working_set.find_decl(b"let", &Type::Any) {
|
||||||
if let Some(let_env_decl_id) =
|
if let Some(let_env_decl_id) = working_set.find_decl(b"let-env", &Type::Any)
|
||||||
working_set.find_decl(b"let-env", &Type::Any)
|
|
||||||
{
|
{
|
||||||
for element in pipeline.elements.iter_mut() {
|
for element in pipeline.elements.iter_mut() {
|
||||||
if let PipelineElement::Expression(
|
if let PipelineElement::Expression(
|
||||||
@ -5397,14 +5433,11 @@ pub fn parse_block(
|
|||||||
} else if element.has_in_variable(working_set)
|
} else if element.has_in_variable(working_set)
|
||||||
&& !is_subexpression
|
&& !is_subexpression
|
||||||
{
|
{
|
||||||
*element =
|
*element = wrap_element_with_collect(working_set, element);
|
||||||
wrap_element_with_collect(working_set, element);
|
|
||||||
}
|
}
|
||||||
} else if element.has_in_variable(working_set)
|
} else if element.has_in_variable(working_set) && !is_subexpression
|
||||||
&& !is_subexpression
|
|
||||||
{
|
{
|
||||||
*element =
|
*element = wrap_element_with_collect(working_set, element);
|
||||||
wrap_element_with_collect(working_set, element);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -5417,8 +5450,6 @@ pub fn parse_block(
|
|||||||
|
|
||||||
pipeline
|
pipeline
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
@ -5494,7 +5525,6 @@ pub fn discover_captures_in_pipeline_element(
|
|||||||
match element {
|
match element {
|
||||||
PipelineElement::Expression(_, expression)
|
PipelineElement::Expression(_, expression)
|
||||||
| PipelineElement::Redirection(_, _, expression)
|
| PipelineElement::Redirection(_, _, expression)
|
||||||
| PipelineElement::And(_, expression)
|
|
||||||
| PipelineElement::Or(_, expression) => {
|
| PipelineElement::Or(_, expression) => {
|
||||||
discover_captures_in_expr(working_set, expression, seen, seen_blocks)
|
discover_captures_in_expr(working_set, expression, seen, seen_blocks)
|
||||||
}
|
}
|
||||||
@ -5755,9 +5785,6 @@ fn wrap_element_with_collect(
|
|||||||
wrap_expr_with_collect(working_set, expression),
|
wrap_expr_with_collect(working_set, expression),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
PipelineElement::And(span, expression) => {
|
|
||||||
PipelineElement::And(*span, wrap_expr_with_collect(working_set, expression))
|
|
||||||
}
|
|
||||||
PipelineElement::Or(span, expression) => {
|
PipelineElement::Or(span, expression) => {
|
||||||
PipelineElement::Or(*span, wrap_expr_with_collect(working_set, expression))
|
PipelineElement::Or(*span, wrap_expr_with_collect(working_set, expression))
|
||||||
}
|
}
|
||||||
@ -5861,6 +5888,7 @@ impl LiteCommand {
|
|||||||
pub enum LiteElement {
|
pub enum LiteElement {
|
||||||
Command(Option<Span>, LiteCommand),
|
Command(Option<Span>, LiteCommand),
|
||||||
Redirection(Span, Redirection, LiteCommand),
|
Redirection(Span, Redirection, LiteCommand),
|
||||||
|
Or(Span, LiteCommand),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -5929,15 +5957,8 @@ pub fn lite_parse(tokens: &[Token]) -> (LiteBlock, Option<ParseError>) {
|
|||||||
|
|
||||||
let mut curr_comment: Option<Vec<Span>> = None;
|
let mut curr_comment: Option<Vec<Span>> = None;
|
||||||
|
|
||||||
let mut error = None;
|
|
||||||
|
|
||||||
for token in tokens.iter() {
|
for token in tokens.iter() {
|
||||||
match &token.contents {
|
match &token.contents {
|
||||||
TokenContents::PipePipe => {
|
|
||||||
error = error.or(Some(ParseError::ShellOrOr(token.span)));
|
|
||||||
curr_command.push(token.span);
|
|
||||||
last_token = TokenContents::Item;
|
|
||||||
}
|
|
||||||
TokenContents::Item => {
|
TokenContents::Item => {
|
||||||
// If we have a comment, go ahead and attach it
|
// If we have a comment, go ahead and attach it
|
||||||
if let Some(curr_comment) = curr_comment.take() {
|
if let Some(curr_comment) = curr_comment.take() {
|
||||||
@ -6073,7 +6094,25 @@ pub fn lite_parse(tokens: &[Token]) -> (LiteBlock, Option<ParseError>) {
|
|||||||
|
|
||||||
last_token = TokenContents::Eol;
|
last_token = TokenContents::Eol;
|
||||||
}
|
}
|
||||||
TokenContents::Semicolon => {
|
TokenContents::PipePipe => {
|
||||||
|
// Different to redirection, for PipePipe, we'll wrap the current command
|
||||||
|
// in an "Or". This lets us know during eval that it's the current command
|
||||||
|
// whose error will be ignored rather than having to look ahead in the pipeline
|
||||||
|
if !curr_command.is_empty() {
|
||||||
|
curr_pipeline.push(LiteElement::Or(token.span, curr_command));
|
||||||
|
curr_command = LiteCommand::new();
|
||||||
|
}
|
||||||
|
if !curr_pipeline.is_empty() {
|
||||||
|
block.push(curr_pipeline);
|
||||||
|
|
||||||
|
curr_pipeline = LitePipeline::new();
|
||||||
|
last_connector = TokenContents::Pipe;
|
||||||
|
last_connector_span = None;
|
||||||
|
}
|
||||||
|
|
||||||
|
last_token = TokenContents::PipePipe;
|
||||||
|
}
|
||||||
|
TokenContents::Semicolon | TokenContents::AndAnd => {
|
||||||
if !curr_command.is_empty() {
|
if !curr_command.is_empty() {
|
||||||
match last_connector {
|
match last_connector {
|
||||||
TokenContents::OutGreaterThan => {
|
TokenContents::OutGreaterThan => {
|
||||||
@ -6102,7 +6141,7 @@ pub fn lite_parse(tokens: &[Token]) -> (LiteBlock, Option<ParseError>) {
|
|||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
curr_pipeline
|
curr_pipeline
|
||||||
.push(LiteElement::Command(last_connector_span, curr_command));
|
.push(LiteElement::Command(Some(token.span), curr_command));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -6174,7 +6213,10 @@ pub fn lite_parse(tokens: &[Token]) -> (LiteBlock, Option<ParseError>) {
|
|||||||
block.push(curr_pipeline);
|
block.push(curr_pipeline);
|
||||||
}
|
}
|
||||||
|
|
||||||
if last_token == TokenContents::Pipe {
|
if last_token == TokenContents::Pipe
|
||||||
|
|| last_token == TokenContents::PipePipe
|
||||||
|
|| last_token == TokenContents::AndAnd
|
||||||
|
{
|
||||||
(
|
(
|
||||||
block,
|
block,
|
||||||
Some(ParseError::UnexpectedEof(
|
Some(ParseError::UnexpectedEof(
|
||||||
@ -6183,7 +6225,7 @@ pub fn lite_parse(tokens: &[Token]) -> (LiteBlock, Option<ParseError>) {
|
|||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
(block, error)
|
(block, None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,6 @@ pub enum Redirection {
|
|||||||
pub enum PipelineElement {
|
pub enum PipelineElement {
|
||||||
Expression(Option<Span>, Expression),
|
Expression(Option<Span>, Expression),
|
||||||
Redirection(Span, Redirection, Expression),
|
Redirection(Span, Redirection, Expression),
|
||||||
And(Span, Expression),
|
|
||||||
Or(Span, Expression),
|
Or(Span, Expression),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,9 +22,8 @@ impl PipelineElement {
|
|||||||
match self {
|
match self {
|
||||||
PipelineElement::Expression(None, expression) => expression.span,
|
PipelineElement::Expression(None, expression) => expression.span,
|
||||||
PipelineElement::Expression(Some(span), expression)
|
PipelineElement::Expression(Some(span), expression)
|
||||||
| PipelineElement::Redirection(span, _, expression)
|
| PipelineElement::Or(span, expression)
|
||||||
| PipelineElement::And(span, expression)
|
| PipelineElement::Redirection(span, _, expression) => Span {
|
||||||
| PipelineElement::Or(span, expression) => Span {
|
|
||||||
start: span.start,
|
start: span.start,
|
||||||
end: expression.span.end,
|
end: expression.span.end,
|
||||||
},
|
},
|
||||||
@ -35,7 +33,6 @@ impl PipelineElement {
|
|||||||
match self {
|
match self {
|
||||||
PipelineElement::Expression(_, expression)
|
PipelineElement::Expression(_, expression)
|
||||||
| PipelineElement::Redirection(_, _, expression)
|
| PipelineElement::Redirection(_, _, expression)
|
||||||
| PipelineElement::And(_, expression)
|
|
||||||
| PipelineElement::Or(_, expression) => expression.has_in_variable(working_set),
|
| PipelineElement::Or(_, expression) => expression.has_in_variable(working_set),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -43,9 +40,8 @@ impl PipelineElement {
|
|||||||
pub fn replace_in_variable(&mut self, working_set: &mut StateWorkingSet, new_var_id: VarId) {
|
pub fn replace_in_variable(&mut self, working_set: &mut StateWorkingSet, new_var_id: VarId) {
|
||||||
match self {
|
match self {
|
||||||
PipelineElement::Expression(_, expression)
|
PipelineElement::Expression(_, expression)
|
||||||
| PipelineElement::Redirection(_, _, expression)
|
| PipelineElement::Or(_, expression)
|
||||||
| PipelineElement::And(_, expression)
|
| PipelineElement::Redirection(_, _, expression) => {
|
||||||
| PipelineElement::Or(_, expression) => {
|
|
||||||
expression.replace_in_variable(working_set, new_var_id)
|
expression.replace_in_variable(working_set, new_var_id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,9 +55,8 @@ impl PipelineElement {
|
|||||||
) {
|
) {
|
||||||
match self {
|
match self {
|
||||||
PipelineElement::Expression(_, expression)
|
PipelineElement::Expression(_, expression)
|
||||||
| PipelineElement::Redirection(_, _, expression)
|
| PipelineElement::Or(_, expression)
|
||||||
| PipelineElement::And(_, expression)
|
| PipelineElement::Redirection(_, _, expression) => {
|
||||||
| PipelineElement::Or(_, expression) => {
|
|
||||||
expression.replace_span(working_set, replaced, new_span)
|
expression.replace_span(working_set, replaced, new_span)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,6 +113,13 @@ impl Stack {
|
|||||||
.ok_or_else(|| ShellError::NushellFailed("No active overlay".into()))
|
.ok_or_else(|| ShellError::NushellFailed("No active overlay".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn last_exit_code(&self, engine_state: &EngineState) -> Option<i64> {
|
||||||
|
match self.get_env_var(engine_state, "LAST_EXIT_CODE") {
|
||||||
|
Some(Value::Int { val, .. }) => Some(val),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn captures_to_stack(&self, captures: &HashMap<VarId, Value>) -> Stack {
|
pub fn captures_to_stack(&self, captures: &HashMap<VarId, Value>) -> Stack {
|
||||||
// FIXME: this is probably slow
|
// FIXME: this is probably slow
|
||||||
let mut env_vars = self.env_vars.clone();
|
let mut env_vars = self.env_vars.clone();
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
mod commands;
|
mod commands;
|
||||||
|
mod pipeline_operators;
|
||||||
|
|
||||||
use nu_test_support::nu;
|
use nu_test_support::nu;
|
||||||
|
|
||||||
|
74
tests/shell/pipeline/pipeline_operators.rs
Normal file
74
tests/shell/pipeline/pipeline_operators.rs
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
use nu_test_support::nu;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn and_operator1() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".",
|
||||||
|
r#"
|
||||||
|
3 / 0 && 2 + 3
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(actual.err.contains("division by zero"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn and_operator2() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".",
|
||||||
|
r#"
|
||||||
|
0 | 3 / $in && 2 + 3
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(actual.err.contains("division by zero"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn or_operator1() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".",
|
||||||
|
r#"
|
||||||
|
3 / 0 || 2 + 3
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(actual.out.contains('5'));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn or_operator2() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".",
|
||||||
|
r#"
|
||||||
|
0 | 3 / $in || 2 + 3
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(actual.out.contains('5'));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn or_operator3() {
|
||||||
|
// On success, don't run the next step
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".",
|
||||||
|
r#"
|
||||||
|
0 || 2 + 3
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(actual.out, "0");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn or_operator4() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".",
|
||||||
|
r#"
|
||||||
|
1 / 0 || 2 / 0 || 10 + 9
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(actual.out.contains("19"));
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user