Add more multiline pipeline forms (#4740)

This commit is contained in:
JT 2022-03-05 08:20:13 -05:00 committed by GitHub
parent 32601bb352
commit a4a8f5df54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 85 additions and 7 deletions

View file

@ -243,7 +243,6 @@ pub fn lex(
let c = *c; let c = *c;
if c == b'|' { if c == b'|' {
// If the next character is `|`, it's either `|` or `||`. // If the next character is `|`, it's either `|` or `||`.
let idx = curr_offset; let idx = curr_offset;
let prev_idx = idx; let prev_idx = idx;
curr_offset += 1; curr_offset += 1;
@ -262,10 +261,31 @@ pub fn lex(
} }
// Otherwise, it's just a regular `|` token. // Otherwise, it's just a regular `|` token.
// 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( output.push(Token::new(
TokenContents::Pipe, TokenContents::Pipe,
Span::new(span_offset + idx, span_offset + idx + 1), 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; is_complete = false;
} else if c == b';' { } else if c == b';' {
// If the next character is a `;`, we're looking at a semicolon token. // If the next character is a `;`, we're looking at a semicolon token.
@ -282,9 +302,11 @@ pub fn lex(
TokenContents::Semicolon, TokenContents::Semicolon,
Span::new(span_offset + idx, span_offset + idx + 1), 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. // If the next character is a newline, we're looking at an EOL (end of line) token.
let idx = curr_offset; let idx = curr_offset;
curr_offset += 1; curr_offset += 1;
if !additional_whitespace.contains(&c) { if !additional_whitespace.contains(&c) {

View file

@ -1,5 +1,6 @@
extern crate nu_test_support; extern crate nu_test_support;
mod parsing;
mod path; mod path;
mod plugins; mod plugins;
mod shell; mod shell;

46
tests/parsing/mod.rs Normal file
View file

@ -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");
}

View file

@ -0,0 +1,2 @@
echo "hi" |
str length

View file

@ -0,0 +1,2 @@
echo "how" |
str length

View file

@ -0,0 +1,2 @@
echo "four"
| str length

View file

@ -0,0 +1,2 @@
echo "one"
| str length

View file

@ -0,0 +1 @@
echo "hello" | str length