Revert "Fix for escaping backslashes in interpolated strings (fixes #6737) (#7020)" (#7038)

This reverts commit d4798d6ee1.
This commit is contained in:
Darren Schroeder 2022-11-06 16:17:00 -06:00 committed by GitHub
parent d4798d6ee1
commit a6118eed8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 105 additions and 257 deletions

View file

@ -1604,24 +1604,14 @@ pub fn parse_string_interpolation(
let mut b = start;
let mut consecutive_backslashes: usize = 0;
while b != end {
let current_byte = contents[b - start];
match mode {
InterpolationMode::String => {
let preceding_consecutive_backslashes = consecutive_backslashes;
let is_backslash = current_byte == b'\\';
consecutive_backslashes = if is_backslash {
preceding_consecutive_backslashes + 1
if contents[b - start] == b'('
&& (if double_quote && (b - start) > 0 {
contents[b - start - 1] != b'\\'
} else {
0
};
if current_byte == b'('
&& (!double_quote || preceding_consecutive_backslashes % 2 == 0)
true
})
&& mode == InterpolationMode::String
{
mode = InterpolationMode::Expression;
if token_start < b {
@ -1649,9 +1639,8 @@ pub fn parse_string_interpolation(
token_start = b;
}
}
}
InterpolationMode::Expression => {
let byte = current_byte;
if mode == InterpolationMode::Expression {
let byte = contents[b - start];
if let Some(b'\'') = delimiter_stack.last() {
if byte == b'\'' {
delimiter_stack.pop();
@ -1685,12 +1674,8 @@ pub fn parse_string_interpolation(
end: b + 1,
};
let (expr, err) = parse_full_cell_path(
working_set,
None,
span,
expand_aliases_denylist,
);
let (expr, err) =
parse_full_cell_path(working_set, None, span, expand_aliases_denylist);
error = error.or(err);
output.push(expr);
}
@ -1700,8 +1685,6 @@ pub fn parse_string_interpolation(
}
}
}
}
b += 1;
}

View file

@ -175,6 +175,46 @@ pub fn parse_binary_with_multi_byte_char() {
assert!(!matches!(&expressions[0].expr, Expr::Binary(_)))
}
#[test]
pub fn parse_string() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(&mut working_set, None, b"\"hello nushell\"", true, &[]);
assert!(err.is_none());
assert!(block.len() == 1);
let expressions = &block[0];
assert!(expressions.len() == 1);
assert_eq!(
expressions[0].expr,
Expr::String("hello nushell".to_string())
)
}
#[test]
pub fn parse_escaped_string() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(
&mut working_set,
None,
b"\"hello \\u006e\\u0075\\u0073hell\"",
true,
&[],
);
assert!(err.is_none());
assert!(block.len() == 1);
let expressions = &block[0];
assert!(expressions.len() == 1);
assert_eq!(
expressions[0].expr,
Expr::String("hello nushell".to_string())
)
}
#[test]
pub fn parse_call() {
let engine_state = EngineState::new();
@ -324,181 +364,6 @@ fn test_nothing_comparisson_neq() {
))
}
mod string {
use super::*;
#[test]
pub fn parse_string() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(&mut working_set, None, b"\"hello nushell\"", true, &[]);
assert!(err.is_none());
assert!(block.len() == 1);
let expressions = &block[0];
assert!(expressions.len() == 1);
assert_eq!(
expressions[0].expr,
Expr::String("hello nushell".to_string())
)
}
#[test]
pub fn parse_escaped_string() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(
&mut working_set,
None,
b"\"hello \\u006e\\u0075\\u0073hell\"",
true,
&[],
);
assert!(err.is_none());
assert!(block.len() == 1);
let expressions = &block[0];
assert!(expressions.len() == 1);
assert_eq!(
expressions[0].expr,
Expr::String("hello nushell".to_string())
)
}
mod interpolation {
use super::*;
#[test]
pub fn parse_string_interpolation() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(&mut working_set, None, b"$\"hello (39 + 3)\"", true, &[]);
assert!(err.is_none());
assert!(block.len() == 1);
let expressions = &block[0];
assert!(expressions.len() == 1);
let expr = &expressions[0].expr;
let subexprs: Vec<&Expr>;
match expr {
Expr::StringInterpolation(expressions) => {
subexprs = expressions.iter().map(|e| &e.expr).collect();
}
_ => panic!("Expected an `Expr::StringInterpolation`"),
}
assert_eq!(subexprs.len(), 2);
assert_eq!(subexprs[0], &Expr::String("hello ".to_string()));
assert!(matches!(subexprs[1], &Expr::FullCellPath(..)));
}
#[test]
pub fn parse_string_interpolation_escaped_parenthesis() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(&mut working_set, None, b"$\"hello \\(39 + 3)\"", true, &[]);
assert!(err.is_none());
assert!(block.len() == 1);
let expressions = &block[0];
assert!(expressions.len() == 1);
let expr = &expressions[0].expr;
let subexprs: Vec<&Expr>;
match expr {
Expr::StringInterpolation(expressions) => {
subexprs = expressions.iter().map(|e| &e.expr).collect();
}
_ => panic!("Expected an `Expr::StringInterpolation`"),
}
assert_eq!(subexprs.len(), 1);
assert_eq!(subexprs[0], &Expr::String("hello (39 + 3)".to_string()));
}
#[test]
pub fn parse_string_interpolation_escaped_backslash_before_parenthesis() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(
&mut working_set,
None,
b"$\"hello \\\\(39 + 3)\"",
true,
&[],
);
assert!(err.is_none());
assert!(block.len() == 1);
let expressions = &block[0];
assert!(expressions.len() == 1);
let expr = &expressions[0].expr;
let subexprs: Vec<&Expr>;
match expr {
Expr::StringInterpolation(expressions) => {
subexprs = expressions.iter().map(|e| &e.expr).collect();
}
_ => panic!("Expected an `Expr::StringInterpolation`"),
}
assert_eq!(subexprs.len(), 2);
assert_eq!(subexprs[0], &Expr::String("hello \\".to_string()));
assert!(matches!(subexprs[1], &Expr::FullCellPath(..)));
}
#[test]
pub fn parse_string_interpolation_backslash_count_reset_by_expression() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(
&mut working_set,
None,
b"$\"\\(1 + 3)\\(7 - 5)\"",
true,
&[],
);
assert!(err.is_none());
assert!(block.len() == 1);
let expressions = &block[0];
assert!(expressions.len() == 1);
let expr = &expressions[0].expr;
let subexprs: Vec<&Expr>;
match expr {
Expr::StringInterpolation(expressions) => {
subexprs = expressions.iter().map(|e| &e.expr).collect();
}
_ => panic!("Expected an `Expr::StringInterpolation`"),
}
assert_eq!(subexprs.len(), 1);
assert_eq!(subexprs[0], &Expr::String("(1 + 3)(7 - 5)".to_string()));
}
}
}
mod range {
use super::*;
use nu_protocol::ast::{RangeInclusion, RangeOperator};