From fd449fd4ce6e0d9328fa72ee217c788f0f7a5715 Mon Sep 17 00:00:00 2001 From: Phoenix Himself Date: Sat, 6 Jul 2024 21:15:27 +0200 Subject: [PATCH 1/3] fix: escaped escape is not an escape character anymore --- src/compiling/lexing/region_handler.rs | 31 +++++++++++++++++--------- tests/cobra.rs | 4 ++-- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/compiling/lexing/region_handler.rs b/src/compiling/lexing/region_handler.rs index 658bd99..283a992 100644 --- a/src/compiling/lexing/region_handler.rs +++ b/src/compiling/lexing/region_handler.rs @@ -29,7 +29,7 @@ impl RegionHandler { self.region_stack.last() } - // Error if after code lexing + // Error if after code lexing // some region was left unclosed #[inline] pub fn is_region_closed(&self, reader: &Reader) -> Result<(),((usize, usize), Region)> { @@ -93,15 +93,23 @@ impl RegionHandler { let predicate = |candidate: &Region| match reader.get_history_or_future(cb(candidate).len(), &read_mode) { Some(code_chunk) => { // Check if the region was escaped (the escape symbol will always be in a history) - let is_escaped = match read_mode { + let escaped_suffix = match read_mode { ReadMode::History => reader.get_history(cb(candidate).len() + 1), ReadMode::Future => reader.get_history(2) - }; - // Check if the region was escaped - let is_escaped = match is_escaped { - Some(code_chunk_with_escape) => code_chunk_with_escape.chars().next().unwrap() == self.escape, - None => false - }; + }.is_some_and(|val| val.starts_with(self.escape)); + // Check if the escape symbol was escaped + let escape_escaped = match read_mode { + ReadMode::History => reader.get_history(cb(candidate).len() + 2), + ReadMode::Future => reader.get_history(3) + }.is_some_and(|val| val.starts_with(&self.escape.to_string().repeat(2))); + if escaped_suffix { + dbg!( + reader.get_history(cb(candidate).len() + 1), + reader.get_history(cb(candidate).len() + 2), + escape_escaped + ); + } + let is_escaped = escaped_suffix && !escape_escaped; !is_escaped && &code_chunk == cb(candidate) } None => false @@ -184,10 +192,10 @@ mod test { #[test] fn handle_region() { let lines = vec![ - "'My name is {name}.'" + "'My name is \\\\{name}.\\\\'" ]; let expected = vec![ - 0, 12, 17, 19 + 0, 14, 19, 23 ]; let code = lines.join("\n"); let region = reg![ @@ -203,7 +211,7 @@ mod test { ]; let mut reader = Reader::new(&code); let mut rh = RegionHandler { - region_stack: vec![region.clone()], + region_stack: vec![region.clone()], region_map: region.generate_region_map(), escape: '\\' }; @@ -215,6 +223,7 @@ mod test { result.push(reader.get_index()); } } + dbg!(expected.clone(), result.clone()); assert_eq!(expected, result); } } diff --git a/tests/cobra.rs b/tests/cobra.rs index 6f40c7b..61ac4ad 100644 --- a/tests/cobra.rs +++ b/tests/cobra.rs @@ -15,11 +15,11 @@ fn cobra() { let mut compiler = Compiler::new("Cobra", rules); compiler.use_indents(); compiler.load(vec![ - "if 'condition':", + "if 'condition\\\\':", " 'do + this'", " 'do ++ that'" ].join("\n")); let mut ast = cobra_modules::IfStatement::new(); compiler.debug(); assert!(compiler.compile(&mut ast).is_ok()); -} \ No newline at end of file +} From 0ef8190c196a95094d5638252220d1e66be9bca7 Mon Sep 17 00:00:00 2001 From: Phoenix Himself Date: Sat, 6 Jul 2024 21:17:09 +0200 Subject: [PATCH 2/3] fix: resolve vulnerabilities --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ee4e49c..7804c76 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,9 +15,9 @@ dependencies = [ [[package]] name = "capitalize" -version = "0.1.0" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "702cfe2f052c6542d55a9924efef63dd315c168cc4f315a824fb0f47da4e11c8" +checksum = "6b5271031022835ee8c7582fe67403bd6cb3d962095787af7921027234bab5bf" [[package]] name = "colored" diff --git a/Cargo.toml b/Cargo.toml index 6bd97d8..71b3e91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,4 @@ keywords = ["heraclitus", "compiler", "parser"] [dependencies] colored = "2.0.0" pad = "0.1.6" -capitalize = "0.1.0" +capitalize = "0.3.4" From ea4d1bea00599b63e9ae95c313766eb04b4f7c8c Mon Sep 17 00:00:00 2001 From: Phoenix Himself Date: Sat, 6 Jul 2024 21:40:39 +0200 Subject: [PATCH 3/3] feat: bump version --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7804c76..be74700 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,7 +32,7 @@ dependencies = [ [[package]] name = "heraclitus-compiler" -version = "1.5.8" +version = "1.5.9" dependencies = [ "capitalize", "colored", diff --git a/Cargo.toml b/Cargo.toml index 71b3e91..451f289 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "heraclitus-compiler" -version = "1.5.8" +version = "1.5.9" edition = "2021" description = "Compiler frontend for developing great programming languages" license = "MIT" diff --git a/README.md b/README.md index 09d7d4d..36048df 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,10 @@ let tokens = cc.tokenize()?; # Change log 🚀 +## Version 1.5.9 +### Fix: +- Escaping escape key now treats it as a character + ## Version 1.5.8 ### Fix: - Escaping regions is now properly handled