Skip to content

Commit 3d2c79f

Browse files
bors[bot]burrbull
andauthored
Merge #501
501: don't add underscore to end of "is_keyword" r=therealprof a=burrbull fixes #497 not tested yet Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents 26baf2c + 0d14b95 commit 3d2c79f

File tree

3 files changed

+36
-75
lines changed

3 files changed

+36
-75
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2020

2121
### Fixed
2222

23-
- Keyword sanitizing (`async`)
23+
- Keyword sanitizing (`async` and unneeded underscores)
2424

2525
- Expand derived clusters.
2626

src/generate/register.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ pub fn fields(
494494

495495
for v in &variants {
496496
let pc = &v.pc;
497-
let sc = &v.sc;
497+
let sc = &v.nksc;
498498

499499
let is_variant = Ident::new(
500500
&if sc.to_string().starts_with('_') {
@@ -762,6 +762,7 @@ fn unsafety(write_constraint: Option<&WriteConstraint>, width: u32) -> Option<Id
762762
struct Variant {
763763
doc: String,
764764
pc: Ident,
765+
nksc: Ident,
765766
sc: Ident,
766767
value: u64,
767768
}
@@ -779,13 +780,16 @@ impl Variant {
779780
anyhow!("EnumeratedValue {} has no `<value>` field", ev.name)
780781
})?);
781782

783+
let nksc = ev.name.to_sanitized_not_keyword_snake_case();
784+
let sc = util::sanitize_keyword(nksc.clone());
782785
Ok(Variant {
783786
doc: ev
784787
.description
785788
.clone()
786789
.unwrap_or_else(|| format!("`{:b}`", value)),
787790
pc: Ident::new(&ev.name.to_sanitized_upper_case(), span),
788-
sc: Ident::new(&ev.name.to_sanitized_snake_case(), span),
791+
nksc: Ident::new(&nksc, span),
792+
sc: Ident::new(&sc, span),
789793
value,
790794
})
791795
})

src/util.rs

+29-72
Original file line numberDiff line numberDiff line change
@@ -47,93 +47,50 @@ pub trait ToSanitizedUpperCase {
4747
}
4848

4949
pub trait ToSanitizedSnakeCase {
50-
fn to_sanitized_snake_case(&self) -> Cow<str>;
50+
fn to_sanitized_not_keyword_snake_case(&self) -> Cow<str>;
51+
fn to_sanitized_snake_case(&self) -> Cow<str> {
52+
let s = self.to_sanitized_not_keyword_snake_case();
53+
sanitize_keyword(s)
54+
}
5155
}
5256

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

6461
let s = self.replace(BLACKLIST_CHARS, "");
65-
6662
match s.chars().next().unwrap_or('\0') {
6763
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
68-
Cow::from(format!("_{}", s.to_snake_case()))
64+
format!("_{}", s.to_snake_case()).into()
6965
}
7066
_ => {
71-
keywords! {
72-
s,
73-
abstract,
74-
alignof,
75-
as,
76-
async,
77-
await,
78-
become,
79-
box,
80-
break,
81-
const,
82-
continue,
83-
crate,
84-
do,
85-
else,
86-
enum,
87-
extern,
88-
false,
89-
final,
90-
fn,
91-
for,
92-
if,
93-
impl,
94-
in,
95-
let,
96-
loop,
97-
macro,
98-
match,
99-
mod,
100-
move,
101-
mut,
102-
offsetof,
103-
override,
104-
priv,
105-
proc,
106-
pub,
107-
pure,
108-
ref,
109-
return,
110-
self,
111-
sizeof,
112-
static,
113-
struct,
114-
super,
115-
trait,
116-
true,
117-
try,
118-
type,
119-
typeof,
120-
unsafe,
121-
unsized,
122-
use,
123-
virtual,
124-
where,
125-
while,
126-
yield,
127-
set_bit,
128-
clear_bit,
129-
bit,
130-
bits,
67+
let s = Cow::from(s.to_snake_case());
68+
if INTERNALS.contains(&s.as_ref()) {
69+
s + "_"
70+
} else {
71+
s
13172
}
13273
}
13374
}
13475
}
13576
}
13677

78+
pub fn sanitize_keyword(sc: Cow<str>) -> Cow<str> {
79+
const KEYWORDS: [&str; 54] = [
80+
"abstract", "alignof", "as", "async", "await", "become", "box", "break", "const",
81+
"continue", "crate", "do", "else", "enum", "extern", "false", "final", "fn", "for", "if",
82+
"impl", "in", "let", "loop", "macro", "match", "mod", "move", "mut", "offsetof",
83+
"override", "priv", "proc", "pub", "pure", "ref", "return", "self", "sizeof", "static",
84+
"struct", "super", "trait", "true", "try", "type", "typeof", "unsafe", "unsized", "use",
85+
"virtual", "where", "while", "yield",
86+
];
87+
if KEYWORDS.contains(&sc.as_ref()) {
88+
sc + "_"
89+
} else {
90+
sc
91+
}
92+
}
93+
13794
impl ToSanitizedUpperCase for str {
13895
fn to_sanitized_upper_case(&self) -> Cow<str> {
13996
let s = self.replace(BLACKLIST_CHARS, "");

0 commit comments

Comments
 (0)