Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: amber now handles escapes properly #283

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repository = "https://github.com/Ph0enixKM/Amber"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
heraclitus-compiler = "1.5.8"
heraclitus-compiler = "1.6.1"
similar-string = "1.4.2"
colored = "2.0.0"
itertools = "0.11.0"
Expand Down
15 changes: 12 additions & 3 deletions src/modules/expression/literal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ pub fn parse_interpolated_region(meta: &mut ParserMetadata, letter: char) -> Res
let mut strings = vec![];
let mut interps = vec![];
// Handle full string
if let Ok(word) = token_by(meta, |word| word.starts_with(letter) && (word.ends_with(letter) && !word.ends_with(format!("\\{}", letter).as_str())) && word.len() > 1) {
if let Ok(word) = token_by(meta, |word| {
let is_escaped =
!word.ends_with(format!("\\\\{}", letter).as_str())
&& word.ends_with(format!("\\{}", letter).as_str());
word.starts_with(letter) && word.ends_with(letter) && word.len() > 1 && !is_escaped
}) {
let stripped = word.chars().take(word.chars().count() - 1).skip(1).collect::<String>();
strings.push(stripped);
Ok((strings, interps))
Expand All @@ -40,7 +45,10 @@ pub fn parse_interpolated_region(meta: &mut ParserMetadata, letter: char) -> Res
}
else {
strings.push(tok.word.clone());
if tok.word.ends_with(letter) && !tok.word.ends_with(format!("\\{}", letter).as_str()) {
let is_escaped =
!tok.word.ends_with(format!("\\\\{}", letter).as_str())
&& tok.word.ends_with(format!("\\{}", letter).as_str());
if tok.word.ends_with(letter) && !is_escaped {
meta.increment_index();
// Right trim the symbol
let trimmed = strings.last().unwrap()
Expand Down Expand Up @@ -126,6 +134,7 @@ fn translate_escaped_string(string: String, is_str: bool) -> String {
chars.next();
},
Some('\\') => {
result.push('\\');
result.push('\\');
chars.next();
},
Expand Down Expand Up @@ -170,4 +179,4 @@ pub fn translate_interpolated_region(strings: Vec<String>, interps: Vec<String>,
is_even = !is_even;
}
result.join("")
}
}
10 changes: 10 additions & 0 deletions src/tests/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ fn text_escaped() {
test_amber!("echo \"Hello \\\"World\\\"\"", "Hello \"World\"");
}

#[test]
fn text_unescaped_after() {
test_amber!("echo \"Hello\\\\\"", "Hello\\");
}

#[test]
fn text_unescaped_before() {
test_amber!("echo \"Hello\\\\{12}\"", "Hello\\12");
}

#[test]
fn bool() {
test_amber!("echo true", "1");
Expand Down
Loading