Skip to content

Commit

Permalink
Combine canonicalization functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Omnikar committed Jun 27, 2024
1 parent e29f4c0 commit 4e67e85
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ impl<'a> Formatter<'a> {
}

self.output
.push_str(&crate::parse::canonicalize_exclams(&binding.name.value));
.push_str(&crate::parse::canonicalize_ident(&binding.name.value));
self.output
.push_str(if binding.public { " ←" } else { " ↚" });
if binding.array_macro {
Expand Down
15 changes: 9 additions & 6 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,7 @@ impl<'i> Parser<'i> {
match token {
Token::Ident if line.is_some() => {
let line = line.as_mut().unwrap();
let ident = canonicalize_exclams(&self.input[span.byte_range()]);
let ident = canonicalize_subscripts(&ident);
let ident = canonicalize_ident(&self.input[span.byte_range()]);
let name = span.clone().sp(ident);
line.items.push(name);
}
Expand Down Expand Up @@ -524,8 +523,7 @@ impl<'i> Parser<'i> {
}
fn try_ident(&mut self) -> Option<Sp<Ident>> {
let span = self.try_exact(Token::Ident)?;
let ident = canonicalize_exclams(&self.input[span.byte_range()]);
let ident = canonicalize_subscripts(&ident);
let ident = canonicalize_ident(&self.input[span.byte_range()]);
Some(span.sp(ident))
}
fn try_ref(&mut self) -> Option<Sp<Word>> {
Expand Down Expand Up @@ -1350,12 +1348,13 @@ pub fn place_exclams(ident: &str, count: usize) -> Ident {
}

/// Rewrite the identifier with the same number of exclamation points using double and single exclamation point characters as needed
pub fn canonicalize_exclams(ident: &str) -> Ident {
fn canonicalize_exclams(ident: &str) -> Ident {
let num_margs = crate::parse::ident_modifier_args(ident);
place_exclams(ident, num_margs)
}

pub fn canonicalize_subscripts(ident: &str) -> Ident {
/// Rewrite the identifier with numerals preceded by `__` replaced with subscript characters
fn canonicalize_subscripts(ident: &str) -> Ident {
// This hasty canonicalization is okay because the stricter
// rules about the syntax are handled in the lexer
ident
Expand All @@ -1371,6 +1370,10 @@ pub fn canonicalize_subscripts(ident: &str) -> Ident {
.collect()
}

pub fn canonicalize_ident(ident: &str) -> Ident {
canonicalize_subscripts(&canonicalize_exclams(ident))
}

pub(crate) fn count_placeholders(words: &[Sp<Word>]) -> usize {
let mut count = 0;
for word in words {
Expand Down

0 comments on commit 4e67e85

Please sign in to comment.