diff --git a/crates/nu-parser/src/lex.rs b/crates/nu-parser/src/lex.rs index 5d0916326b25..c8a5aa8f9308 100644 --- a/crates/nu-parser/src/lex.rs +++ b/crates/nu-parser/src/lex.rs @@ -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; @@ -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. @@ -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) { diff --git a/tests/main.rs b/tests/main.rs index 30f5e27fe217..600cb507421c 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -1,5 +1,6 @@ extern crate nu_test_support; +mod parsing; mod path; mod plugins; mod shell; diff --git a/tests/parsing/mod.rs b/tests/parsing/mod.rs new file mode 100644 index 000000000000..9915b7624237 --- /dev/null +++ b/tests/parsing/mod.rs @@ -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"); +} diff --git a/tests/parsing/samples/multiline_end_pipe.nu b/tests/parsing/samples/multiline_end_pipe.nu new file mode 100644 index 000000000000..4f2502ef75e6 --- /dev/null +++ b/tests/parsing/samples/multiline_end_pipe.nu @@ -0,0 +1,2 @@ +echo "hi" | +str length \ No newline at end of file diff --git a/tests/parsing/samples/multiline_end_pipe_win.nu b/tests/parsing/samples/multiline_end_pipe_win.nu new file mode 100644 index 000000000000..c2c15f9762d1 --- /dev/null +++ b/tests/parsing/samples/multiline_end_pipe_win.nu @@ -0,0 +1,2 @@ +echo "how" | +str length \ No newline at end of file diff --git a/tests/parsing/samples/multiline_start_pipe.nu b/tests/parsing/samples/multiline_start_pipe.nu new file mode 100644 index 000000000000..c2e50f4b5471 --- /dev/null +++ b/tests/parsing/samples/multiline_start_pipe.nu @@ -0,0 +1,2 @@ +echo "four" +| str length \ No newline at end of file diff --git a/tests/parsing/samples/multiline_start_pipe_win.nu b/tests/parsing/samples/multiline_start_pipe_win.nu new file mode 100644 index 000000000000..010c1ca1e134 --- /dev/null +++ b/tests/parsing/samples/multiline_start_pipe_win.nu @@ -0,0 +1,2 @@ +echo "one" +| str length \ No newline at end of file diff --git a/tests/parsing/samples/single_line.nu b/tests/parsing/samples/single_line.nu new file mode 100644 index 000000000000..0c0dd9718f4d --- /dev/null +++ b/tests/parsing/samples/single_line.nu @@ -0,0 +1 @@ +echo "hello" | str length \ No newline at end of file