Add raw strings, use raw strings for env (#5090)

This commit is contained in:
JT 2022-04-05 08:42:26 +12:00 committed by GitHub
parent ef1cf7e634
commit abe028f930
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 13 deletions

View file

@ -109,19 +109,14 @@ pub fn gather_parent_env_vars(engine_state: &mut EngineState) {
);
}
fn escape(input: &str) -> String {
let output = input.replace('\\', "\\\\");
output.replace('"', "\\\"")
}
fn put_env_to_fake_file(name: &str, val: &str, fake_env_file: &mut String) {
fake_env_file.push('"');
fake_env_file.push_str(&escape(name));
fake_env_file.push('"');
fake_env_file.push('`');
fake_env_file.push_str(name);
fake_env_file.push('`');
fake_env_file.push('=');
fake_env_file.push('"');
fake_env_file.push_str(&escape(val));
fake_env_file.push('"');
fake_env_file.push('`');
fake_env_file.push_str(val);
fake_env_file.push('`');
fake_env_file.push('\n');
}

View file

@ -147,7 +147,7 @@ pub fn lex_item(
} else if is_special_item(&block_level, c, special_tokens) && token_start == *curr_offset {
*curr_offset += 1;
break;
} else if c == b'\'' || c == b'"' {
} else if c == b'\'' || c == b'"' || c == b'`' {
// We encountered the opening quote of a string literal.
quote_start = Some(c);
} else if c == b'[' {

View file

@ -71,6 +71,7 @@ pub fn is_math_expression_like(bytes: &[u8]) -> bool {
|| b == b'$'
|| b == b'"'
|| b == b'\''
|| b == b'`'
|| b == b'-'
}
@ -89,6 +90,7 @@ fn is_variable(bytes: &[u8]) -> bool {
pub fn trim_quotes(bytes: &[u8]) -> &[u8] {
if (bytes.starts_with(b"\"") && bytes.ends_with(b"\"") && bytes.len() > 1)
|| (bytes.starts_with(b"\'") && bytes.ends_with(b"\'") && bytes.len() > 1)
|| (bytes.starts_with(b"`") && bytes.ends_with(b"`") && bytes.len() > 1)
{
&bytes[1..(bytes.len() - 1)]
} else {
@ -1482,10 +1484,16 @@ pub fn parse_string_interpolation(
if byte == b'"' {
delimiter_stack.pop();
}
} 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'(' {
delimiter_stack.push(b')');
} else if byte == b')' {
@ -2681,7 +2689,11 @@ pub fn parse_var_with_opt_type(
) -> (Expression, Option<ParseError>) {
let bytes = working_set.get_span_contents(spans[*spans_idx]).to_vec();
if bytes.contains(&b' ') || bytes.contains(&b'"') || bytes.contains(&b'\'') {
if bytes.contains(&b' ')
|| bytes.contains(&b'"')
|| bytes.contains(&b'\'')
|| bytes.contains(&b'`')
{
return (
garbage(spans[*spans_idx]),
Some(ParseError::VariableNotValid(spans[*spans_idx])),