mirror of
https://github.com/nushell/nushell
synced 2025-01-13 05:38:57 +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 {
|
||||
description: "Number each item and echo a message",
|
||||
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")]),
|
||||
},
|
||||
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}]"
|
||||
| from json
|
||||
| get total
|
||||
| reduce -f 20 { $it + (math eval $"{$acc}^1.05")}
|
||||
| reduce -f 20 { $it + (math eval $"($acc)^1.05")}
|
||||
| str from -d 1
|
||||
"#
|
||||
)
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "180.6");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reduce_table_column_with_path() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"
|
||||
echo "[{month:2,total:30}, {month:3,total:10}, {month:4,total:3}, {month:5,total:60}]"
|
||||
| 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
|
||||
"#
|
||||
)
|
||||
|
|
|
@ -499,7 +499,7 @@ fn format(input: &str, start: usize) -> (Vec<FormatCommand>, Option<ParseError>)
|
|||
loop {
|
||||
end += 1;
|
||||
if let Some(c) = loop_input.next() {
|
||||
if c == '{' {
|
||||
if c == '(' {
|
||||
break;
|
||||
}
|
||||
before.push(c);
|
||||
|
@ -518,14 +518,29 @@ fn format(input: &str, start: usize) -> (Vec<FormatCommand>, Option<ParseError>)
|
|||
start = end;
|
||||
|
||||
let mut found_end = false;
|
||||
let mut open_count = 1;
|
||||
let mut delimiter_stack = vec![')'];
|
||||
|
||||
while let Some(c) = loop_input.next() {
|
||||
end += 1;
|
||||
if c == '{' {
|
||||
open_count += 1;
|
||||
} else if c == '}' {
|
||||
open_count -= 1;
|
||||
if open_count == 0 {
|
||||
if let Some('\'') = delimiter_stack.last() {
|
||||
if c == '\'' {
|
||||
delimiter_stack.pop();
|
||||
}
|
||||
} 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;
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -118,7 +118,7 @@ fn string_interpolation_with_it() {
|
|||
let actual = nu!(
|
||||
cwd: ".",
|
||||
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!(
|
||||
cwd: ".",
|
||||
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!(
|
||||
cwd: ".",
|
||||
r#"
|
||||
$"3 + 4 = {3 + 4}"
|
||||
$"3 + 4 = (3 + 4)"
|
||||
"#
|
||||
);
|
||||
|
||||
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]
|
||||
fn bignum_large_integer() {
|
||||
let actual = nu!(
|
||||
|
|
Loading…
Reference in a new issue