Skip to content

don't add underscore to end of "is_keyword" #501

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

Merged
merged 1 commit into from
Apr 16, 2021
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Keyword sanitizing (`async`)
- Keyword sanitizing (`async` and unneeded underscores)

- Expand derived clusters.

Expand Down
8 changes: 6 additions & 2 deletions src/generate/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ pub fn fields(

for v in &variants {
let pc = &v.pc;
let sc = &v.sc;
let sc = &v.nksc;

let is_variant = Ident::new(
&if sc.to_string().starts_with('_') {
Expand Down Expand Up @@ -762,6 +762,7 @@ fn unsafety(write_constraint: Option<&WriteConstraint>, width: u32) -> Option<Id
struct Variant {
doc: String,
pc: Ident,
nksc: Ident,
sc: Ident,
value: u64,
}
Expand All @@ -779,13 +780,16 @@ impl Variant {
anyhow!("EnumeratedValue {} has no `<value>` field", ev.name)
})?);

let nksc = ev.name.to_sanitized_not_keyword_snake_case();
let sc = util::sanitize_keyword(nksc.clone());
Ok(Variant {
doc: ev
.description
.clone()
.unwrap_or_else(|| format!("`{:b}`", value)),
pc: Ident::new(&ev.name.to_sanitized_upper_case(), span),
sc: Ident::new(&ev.name.to_sanitized_snake_case(), span),
nksc: Ident::new(&nksc, span),
sc: Ident::new(&sc, span),
value,
})
})
Expand Down
101 changes: 29 additions & 72 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,93 +47,50 @@ pub trait ToSanitizedUpperCase {
}

pub trait ToSanitizedSnakeCase {
fn to_sanitized_snake_case(&self) -> Cow<str>;
fn to_sanitized_not_keyword_snake_case(&self) -> Cow<str>;
fn to_sanitized_snake_case(&self) -> Cow<str> {
let s = self.to_sanitized_not_keyword_snake_case();
sanitize_keyword(s)
}
}

impl ToSanitizedSnakeCase for str {
fn to_sanitized_snake_case(&self) -> Cow<str> {
macro_rules! keywords {
($s:expr, $($kw:ident),+,) => {
Cow::from(match &$s.to_lowercase()[..] {
$(stringify!($kw) => concat!(stringify!($kw), "_")),+,
_ => return Cow::from($s.to_snake_case())
})
}
}
fn to_sanitized_not_keyword_snake_case(&self) -> Cow<str> {
const INTERNALS: [&str; 4] = ["set_bit", "clear_bit", "bit", "bits"];

let s = self.replace(BLACKLIST_CHARS, "");

match s.chars().next().unwrap_or('\0') {
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
Cow::from(format!("_{}", s.to_snake_case()))
format!("_{}", s.to_snake_case()).into()
}
_ => {
keywords! {
s,
abstract,
alignof,
as,
async,
await,
become,
box,
break,
const,
continue,
crate,
do,
else,
enum,
extern,
false,
final,
fn,
for,
if,
impl,
in,
let,
loop,
macro,
match,
mod,
move,
mut,
offsetof,
override,
priv,
proc,
pub,
pure,
ref,
return,
self,
sizeof,
static,
struct,
super,
trait,
true,
try,
type,
typeof,
unsafe,
unsized,
use,
virtual,
where,
while,
yield,
set_bit,
clear_bit,
bit,
bits,
let s = Cow::from(s.to_snake_case());
if INTERNALS.contains(&s.as_ref()) {
s + "_"
} else {
s
}
}
}
}
}

pub fn sanitize_keyword(sc: Cow<str>) -> Cow<str> {
const KEYWORDS: [&str; 54] = [
"abstract", "alignof", "as", "async", "await", "become", "box", "break", "const",
"continue", "crate", "do", "else", "enum", "extern", "false", "final", "fn", "for", "if",
"impl", "in", "let", "loop", "macro", "match", "mod", "move", "mut", "offsetof",
"override", "priv", "proc", "pub", "pure", "ref", "return", "self", "sizeof", "static",
"struct", "super", "trait", "true", "try", "type", "typeof", "unsafe", "unsized", "use",
"virtual", "where", "while", "yield",
];
if KEYWORDS.contains(&sc.as_ref()) {
sc + "_"
} else {
sc
}
}

impl ToSanitizedUpperCase for str {
fn to_sanitized_upper_case(&self) -> Cow<str> {
let s = self.replace(BLACKLIST_CHARS, "");
Expand Down