mirror of
https://github.com/nushell/nushell
synced 2025-01-13 13:49:21 +00:00
Paren interpolation (#3452)
* Switch interp to use parens * improve interp parsing
This commit is contained in:
parent
722f191e82
commit
4fdbf30308
4 changed files with 43 additions and 13 deletions
|
@ -52,7 +52,7 @@ impl WholeStreamCommand for Each {
|
||||||
Example {
|
Example {
|
||||||
description: "Number each item and echo a message",
|
description: "Number each item and echo a message",
|
||||||
example:
|
example:
|
||||||
"echo ['bob' 'fred'] | each --numbered { echo $\"{$it.index} is {$it.item}\" }",
|
"echo ['bob' 'fred'] | each --numbered { echo $\"($it.index) is ($it.item)\" }",
|
||||||
result: Some(vec![Value::from("0 is bob"), Value::from("1 is fred")]),
|
result: Some(vec![Value::from("0 is bob"), Value::from("1 is fred")]),
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
|
|
|
@ -8,20 +8,23 @@ fn reduce_table_column() {
|
||||||
echo "[{month:2,total:30}, {month:3,total:10}, {month:4,total:3}, {month:5,total:60}]"
|
echo "[{month:2,total:30}, {month:3,total:10}, {month:4,total:3}, {month:5,total:60}]"
|
||||||
| from json
|
| from json
|
||||||
| get total
|
| get total
|
||||||
| reduce -f 20 { $it + (math eval $"{$acc}^1.05")}
|
| reduce -f 20 { $it + (math eval $"($acc)^1.05")}
|
||||||
| str from -d 1
|
| str from -d 1
|
||||||
"#
|
"#
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(actual.out, "180.6");
|
assert_eq!(actual.out, "180.6");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn reduce_table_column_with_path() {
|
||||||
let actual = nu!(
|
let actual = nu!(
|
||||||
cwd: ".", pipeline(
|
cwd: ".", pipeline(
|
||||||
r#"
|
r#"
|
||||||
echo "[{month:2,total:30}, {month:3,total:10}, {month:4,total:3}, {month:5,total:60}]"
|
echo "[{month:2,total:30}, {month:3,total:10}, {month:4,total:3}, {month:5,total:60}]"
|
||||||
| from json
|
| from json
|
||||||
| reduce -f 20 { $it.total + (math eval $"{$acc}^1.05")}
|
| reduce -f 20 { $it.total + (math eval $"($acc)^1.05")}
|
||||||
| str from -d 1
|
| str from -d 1
|
||||||
"#
|
"#
|
||||||
)
|
)
|
||||||
|
|
|
@ -499,7 +499,7 @@ fn format(input: &str, start: usize) -> (Vec<FormatCommand>, Option<ParseError>)
|
||||||
loop {
|
loop {
|
||||||
end += 1;
|
end += 1;
|
||||||
if let Some(c) = loop_input.next() {
|
if let Some(c) = loop_input.next() {
|
||||||
if c == '{' {
|
if c == '(' {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
before.push(c);
|
before.push(c);
|
||||||
|
@ -518,14 +518,29 @@ fn format(input: &str, start: usize) -> (Vec<FormatCommand>, Option<ParseError>)
|
||||||
start = end;
|
start = end;
|
||||||
|
|
||||||
let mut found_end = false;
|
let mut found_end = false;
|
||||||
let mut open_count = 1;
|
let mut delimiter_stack = vec![')'];
|
||||||
|
|
||||||
while let Some(c) = loop_input.next() {
|
while let Some(c) = loop_input.next() {
|
||||||
end += 1;
|
end += 1;
|
||||||
if c == '{' {
|
if let Some('\'') = delimiter_stack.last() {
|
||||||
open_count += 1;
|
if c == '\'' {
|
||||||
} else if c == '}' {
|
delimiter_stack.pop();
|
||||||
open_count -= 1;
|
}
|
||||||
if open_count == 0 {
|
} else if let Some('"') = delimiter_stack.last() {
|
||||||
|
if c == '"' {
|
||||||
|
delimiter_stack.pop();
|
||||||
|
}
|
||||||
|
} else if c == '\'' {
|
||||||
|
delimiter_stack.push('\'');
|
||||||
|
} else if c == '"' {
|
||||||
|
delimiter_stack.push('"');
|
||||||
|
} else if c == '(' {
|
||||||
|
delimiter_stack.push(')');
|
||||||
|
} else if c == ')' {
|
||||||
|
if let Some(')') = delimiter_stack.last() {
|
||||||
|
delimiter_stack.pop();
|
||||||
|
}
|
||||||
|
if delimiter_stack.is_empty() {
|
||||||
found_end = true;
|
found_end = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,7 +118,7 @@ fn string_interpolation_with_it() {
|
||||||
let actual = nu!(
|
let actual = nu!(
|
||||||
cwd: ".",
|
cwd: ".",
|
||||||
r#"
|
r#"
|
||||||
echo "foo" | each { echo $"{$it}" }
|
echo "foo" | each { echo $"($it)" }
|
||||||
"#
|
"#
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -130,7 +130,7 @@ fn string_interpolation_with_it_column_path() {
|
||||||
let actual = nu!(
|
let actual = nu!(
|
||||||
cwd: ".",
|
cwd: ".",
|
||||||
r#"
|
r#"
|
||||||
echo [[name]; [sammie]] | each { echo $"{$it.name}" }
|
echo [[name]; [sammie]] | each { echo $"($it.name)" }
|
||||||
"#
|
"#
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -142,13 +142,25 @@ fn string_interpolation_shorthand_overlap() {
|
||||||
let actual = nu!(
|
let actual = nu!(
|
||||||
cwd: ".",
|
cwd: ".",
|
||||||
r#"
|
r#"
|
||||||
$"3 + 4 = {3 + 4}"
|
$"3 + 4 = (3 + 4)"
|
||||||
"#
|
"#
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(actual.out, "3 + 4 = 7");
|
assert_eq!(actual.out, "3 + 4 = 7");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn string_interpolation_and_paren() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".",
|
||||||
|
r#"
|
||||||
|
$"a paren is ('(')"
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(actual.out, "a paren is (");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn bignum_large_integer() {
|
fn bignum_large_integer() {
|
||||||
let actual = nu!(
|
let actual = nu!(
|
||||||
|
|
Loading…
Reference in a new issue