mirror of
https://github.com/nushell/nushell.git
synced 2025-01-11 08:48:23 +01:00
Fix string interpolation paren cases (#4410)
This commit is contained in:
parent
5cf91cb30d
commit
2e3b2a48ee
@ -1260,14 +1260,13 @@ pub fn parse_string_interpolation(
|
|||||||
let mut output = vec![];
|
let mut output = vec![];
|
||||||
let mut mode = InterpolationMode::String;
|
let mut mode = InterpolationMode::String;
|
||||||
let mut token_start = start;
|
let mut token_start = start;
|
||||||
let mut depth = 0;
|
let mut delimiter_stack = vec![];
|
||||||
|
|
||||||
let mut b = start;
|
let mut b = start;
|
||||||
|
|
||||||
#[allow(clippy::needless_range_loop)]
|
#[allow(clippy::needless_range_loop)]
|
||||||
while b != end {
|
while b != end {
|
||||||
if contents[b - start] == b'(' && mode == InterpolationMode::String {
|
if contents[b - start] == b'(' && mode == InterpolationMode::String {
|
||||||
depth = 1;
|
|
||||||
mode = InterpolationMode::Expression;
|
mode = InterpolationMode::Expression;
|
||||||
if token_start < b {
|
if token_start < b {
|
||||||
let span = Span {
|
let span = Span {
|
||||||
@ -1281,14 +1280,30 @@ pub fn parse_string_interpolation(
|
|||||||
ty: Type::String,
|
ty: Type::String,
|
||||||
custom_completion: None,
|
custom_completion: None,
|
||||||
});
|
});
|
||||||
|
token_start = b;
|
||||||
}
|
}
|
||||||
token_start = b;
|
}
|
||||||
} else if contents[b - start] == b'(' && mode == InterpolationMode::Expression {
|
if mode == InterpolationMode::Expression {
|
||||||
depth += 1;
|
let byte = contents[b - start];
|
||||||
} else if contents[b - start] == b')' && mode == InterpolationMode::Expression {
|
if let Some(b'\'') = delimiter_stack.last() {
|
||||||
match depth {
|
if byte == b'\'' {
|
||||||
0 => {}
|
delimiter_stack.pop();
|
||||||
1 => {
|
}
|
||||||
|
} else if let Some(b'"') = delimiter_stack.last() {
|
||||||
|
if byte == b'"' {
|
||||||
|
delimiter_stack.pop();
|
||||||
|
}
|
||||||
|
} else if byte == b'\'' {
|
||||||
|
delimiter_stack.push(b'\'')
|
||||||
|
} else if byte == b'"' {
|
||||||
|
delimiter_stack.push(b'"');
|
||||||
|
} else if byte == b'(' {
|
||||||
|
delimiter_stack.push(b')');
|
||||||
|
} else if byte == b')' {
|
||||||
|
if let Some(b')') = delimiter_stack.last() {
|
||||||
|
delimiter_stack.pop();
|
||||||
|
}
|
||||||
|
if delimiter_stack.is_empty() {
|
||||||
mode = InterpolationMode::String;
|
mode = InterpolationMode::String;
|
||||||
|
|
||||||
if token_start < b {
|
if token_start < b {
|
||||||
@ -1303,8 +1318,8 @@ pub fn parse_string_interpolation(
|
|||||||
}
|
}
|
||||||
|
|
||||||
token_start = b + 1;
|
token_start = b + 1;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
_ => depth -= 1,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
b += 1;
|
b += 1;
|
||||||
|
@ -197,3 +197,18 @@ fn let_env_expressions() -> TestResult {
|
|||||||
"done",
|
"done",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn string_interpolation_paren_test() -> TestResult {
|
||||||
|
run_test(r#"$"('(')(')')""#, "()")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn string_interpolation_paren_test2() -> TestResult {
|
||||||
|
run_test(r#"$"('(')test(')')""#, "(test)")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn string_interpolation_paren_test3() -> TestResult {
|
||||||
|
run_test(r#"$"('(')("test")test(')')""#, "(testtest)")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user