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

Escaping escape character keeps treating it as escape character #49

Merged
merged 3 commits into from
Jul 6, 2024
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
6 changes: 3 additions & 3 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 20 additions & 11 deletions src/compiling/lexing/region_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)> {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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![
Expand All @@ -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: '\\'
};
Expand All @@ -215,6 +223,7 @@ mod test {
result.push(reader.get_index());
}
}
dbg!(expected.clone(), result.clone());
assert_eq!(expected, result);
}
}
4 changes: 2 additions & 2 deletions tests/cobra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Loading