Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more multiline pipeline forms #4740

Merged
merged 1 commit into from
Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions crates/nu-parser/src/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ pub fn lex(
let c = *c;
if c == b'|' {
// If the next character is `|`, it's either `|` or `||`.

let idx = curr_offset;
let prev_idx = idx;
curr_offset += 1;
Expand All @@ -262,10 +261,31 @@ pub fn lex(
}

// Otherwise, it's just a regular `|` token.
output.push(Token::new(
TokenContents::Pipe,
Span::new(span_offset + idx, span_offset + idx + 1),
));

// Before we push, check to see if the previous character was a newline.
// If so, then this is a continuation of the previous line
if let Some(prev) = output.last_mut() {
match prev.contents {
TokenContents::Eol => {
*prev = Token::new(
TokenContents::Pipe,
Span::new(span_offset + idx, span_offset + idx + 1),
)
}
_ => {
output.push(Token::new(
TokenContents::Pipe,
Span::new(span_offset + idx, span_offset + idx + 1),
));
}
}
} else {
output.push(Token::new(
TokenContents::Pipe,
Span::new(span_offset + idx, span_offset + idx + 1),
));
}

is_complete = false;
} else if c == b';' {
// If the next character is a `;`, we're looking at a semicolon token.
Expand All @@ -282,9 +302,11 @@ pub fn lex(
TokenContents::Semicolon,
Span::new(span_offset + idx, span_offset + idx + 1),
));
} else if c == b'\n' || c == b'\r' {
} else if c == b'\r' {
// Ignore a stand-alone carriage return
curr_offset += 1;
} else if c == b'\n' {
// If the next character is a newline, we're looking at an EOL (end of line) token.

let idx = curr_offset;
curr_offset += 1;
if !additional_whitespace.contains(&c) {
Expand Down
1 change: 1 addition & 0 deletions tests/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extern crate nu_test_support;

mod parsing;
mod path;
mod plugins;
mod shell;
46 changes: 46 additions & 0 deletions tests/parsing/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use nu_test_support::nu;

#[test]
fn run_nu_script_single_line() {
let actual = nu!(cwd: "tests/parsing/samples", r#"
nu single_line.nu
"#);

assert_eq!(actual.out, "5");
}

#[test]
fn run_nu_script_multiline_start_pipe() {
let actual = nu!(cwd: "tests/parsing/samples", r#"
nu multiline_start_pipe.nu
"#);

assert_eq!(actual.out, "4");
}

#[test]
fn run_nu_script_multiline_start_pipe_win() {
let actual = nu!(cwd: "tests/parsing/samples", r#"
nu multiline_start_pipe_win.nu
"#);

assert_eq!(actual.out, "3");
}

#[test]
fn run_nu_script_multiline_end_pipe() {
let actual = nu!(cwd: "tests/parsing/samples", r#"
nu multiline_end_pipe.nu
"#);

assert_eq!(actual.out, "2");
}

#[test]
fn run_nu_script_multiline_end_pipe_win() {
let actual = nu!(cwd: "tests/parsing/samples", r#"
nu multiline_end_pipe_win.nu
"#);

assert_eq!(actual.out, "3");
}
2 changes: 2 additions & 0 deletions tests/parsing/samples/multiline_end_pipe.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
echo "hi" |
str length
2 changes: 2 additions & 0 deletions tests/parsing/samples/multiline_end_pipe_win.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
echo "how" |
str length
2 changes: 2 additions & 0 deletions tests/parsing/samples/multiline_start_pipe.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
echo "four"
| str length
2 changes: 2 additions & 0 deletions tests/parsing/samples/multiline_start_pipe_win.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
echo "one"
| str length
1 change: 1 addition & 0 deletions tests/parsing/samples/single_line.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo "hello" | str length