Skip to content

Commit

Permalink
Add more multiline pipeline forms (#4740)
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiajt committed Mar 5, 2022
1 parent 32601bb commit a4a8f5d
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 7 deletions.
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

0 comments on commit a4a8f5d

Please sign in to comment.