Skip to content

Commit

Permalink
Add atom escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
LostKobrakai committed Nov 6, 2024
1 parent ab68637 commit 0631852
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions compiler-core/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ impl<'a> ErlangApp<'a> {
.chain(native_modules)
.unique()
.sorted()
.map(|m| Self::format_atom(&m))
.join(",\n ");

// TODO: When precompiling for production (i.e. as a precompiled hex
Expand Down Expand Up @@ -151,6 +152,27 @@ impl<'a> ErlangApp<'a> {

writer.write(&path, &text)
}

fn format_atom(s: &EcoString) -> EcoString {
match s.chars().next() {
Some(first_char) => {
// Check if first character is not lowercase
let needs_quotes = !first_char.is_ascii_lowercase();

// Check if string contains any characters other than alphanumeric, underscore, or @
let contains_special = s
.chars()
.any(|c| !(c.is_alphanumeric() || c == '_' || c == '@'));

if needs_quotes || contains_special {
EcoString::from(format!("'{}'", s))
} else {
s.clone()
}
}
None => EcoString::from("''"),
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down

0 comments on commit 0631852

Please # to comment.