Skip to content

Commit ae89a86

Browse files
committed
Auto merge of #85 - whitequark:uppercase, r=japaric
Normalize SVD names to upper case where they were to pascal case Fixes #76. Depends on calebmer/inflections#2.
2 parents 633403b + 601cc70 commit ae89a86

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

Cargo.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ cast = "0.2.0"
1515
clap = "2.20.5"
1616
either = "1.0.3"
1717
error-chain = "0.10.0"
18-
inflections = "1.0.0"
18+
inflections = "1.1.0"
1919
quote = "0.3.15"
2020
svd-parser = "0.5.1"
2121
syn = "0.11.9"

src/generate.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use svd::{Access, BitRange, Defaults, Device, EnumeratedValues, Field,
99
use syn::{Ident, Lit};
1010

1111
use errors::*;
12-
use util::{self, ToSanitizedPascalCase, ToSanitizedSnakeCase, U32Ext};
12+
use util::{self, ToSanitizedPascalCase, ToSanitizedSnakeCase,
13+
ToSanitizedUpperCase, U32Ext};
1314

1415
/// Whole device generation
1516
pub fn device(d: &Device, items: &mut Vec<Tokens>) -> Result<()> {
@@ -26,6 +27,7 @@ pub fn device(d: &Device, items: &mut Vec<Tokens>) -> Result<()> {
2627
#![doc = #doc]
2728
#![deny(missing_docs)]
2829
#![deny(warnings)]
30+
#![allow(non_camel_case_types)]
2931
#![feature(const_fn)]
3032
#![feature(optin_builtin_traits)]
3133
#![no_std]
@@ -130,7 +132,7 @@ pub fn interrupt(peripherals: &[Peripheral], items: &mut Vec<Tokens>) {
130132
);
131133
}
132134

133-
let name_pc = Ident::new(interrupt.name.to_sanitized_pascal_case());
135+
let name_pc = Ident::new(interrupt.name.to_sanitized_upper_case());
134136
let description = format!(
135137
"{} - {}",
136138
interrupt.value,
@@ -242,7 +244,7 @@ pub fn peripheral(
242244
defaults: &Defaults,
243245
) -> Result<()> {
244246
let name = Ident::new(&*p.name.to_uppercase());
245-
let name_pc = Ident::new(&*p.name.to_sanitized_pascal_case());
247+
let name_pc = Ident::new(&*p.name.to_sanitized_upper_case());
246248
let address = util::unsuffixed(u64(p.base_address));
247249
let description = util::respace(p.description.as_ref().unwrap_or(&p.name));
248250

@@ -425,7 +427,7 @@ pub fn register(
425427
) -> Result<()> {
426428
let access = util::access_of(register);
427429
let name = util::name_of(register);
428-
let name_pc = Ident::new(&*name.to_sanitized_pascal_case());
430+
let name_pc = Ident::new(&*name.to_sanitized_upper_case());
429431
let name_sc = Ident::new(&*name.to_sanitized_snake_case());
430432
let rsize = register.size
431433
.or(defs.size)
@@ -657,7 +659,7 @@ pub fn fields(
657659
fn from(f: &'a Field) -> Result<Self> {
658660
let BitRange { offset, width } = f.bit_range;
659661
let sc = f.name.to_sanitized_snake_case();
660-
let pc = f.name.to_sanitized_pascal_case();
662+
let pc = f.name.to_sanitized_upper_case();
661663
let pc_r = Ident::new(&*format!("{}R", pc));
662664
let pc_w = Ident::new(&*format!("{}W", pc));
663665
let _pc_w = Ident::new(&*format!("_{}W", pc));
@@ -761,15 +763,15 @@ pub fn fields(
761763
description: description,
762764
sc: sc,
763765
pc: Ident::new(&*ev.name
764-
.to_sanitized_pascal_case()),
766+
.to_sanitized_upper_case()),
765767
value: value,
766768
})
767769
})
768770
.collect::<Result<Vec<_>>>()?;
769771

770772
let pc_r = &f.pc_r;
771773
if let Some(ref base) = base {
772-
let pc = base.field.to_sanitized_pascal_case();
774+
let pc = base.field.to_sanitized_upper_case();
773775
let base_pc_r = Ident::new(&*format!("{}R", pc));
774776
let description =
775777
format!("Possible values of the field `{}`", f.name);
@@ -1075,7 +1077,7 @@ pub fn fields(
10751077
let base_pc_w = base.as_ref()
10761078
.map(
10771079
|base| {
1078-
let pc = base.field.to_sanitized_pascal_case();
1080+
let pc = base.field.to_sanitized_upper_case();
10791081
let base_pc_w = Ident::new(&*format!("{}W", pc));
10801082

10811083
if let (Some(ref peripheral), Some(ref register)) = (base.peripheral, base.register) {
@@ -1142,7 +1144,7 @@ pub fn fields(
11421144
format!("`{:b}`", value)
11431145
}),
11441146
pc: Ident::new(&*ev.name
1145-
.to_sanitized_pascal_case()),
1147+
.to_sanitized_upper_case()),
11461148
sc: Ident::new(&*ev.name
11471149
.to_sanitized_snake_case()),
11481150
value: value,

src/util.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ pub trait ToSanitizedPascalCase {
1717
fn to_sanitized_pascal_case(&self) -> Cow<str>;
1818
}
1919

20+
pub trait ToSanitizedUpperCase {
21+
fn to_sanitized_upper_case(&self) -> Cow<str>;
22+
}
23+
2024
pub trait ToSanitizedSnakeCase {
2125
fn to_sanitized_snake_case(&self) -> Cow<str>;
2226
}
@@ -98,6 +102,19 @@ impl ToSanitizedSnakeCase for str {
98102
}
99103
}
100104

105+
impl ToSanitizedUpperCase for str {
106+
fn to_sanitized_upper_case(&self) -> Cow<str> {
107+
let s = self.replace(BLACKLIST_CHARS, "");
108+
109+
match s.chars().next().unwrap_or('\0') {
110+
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
111+
Cow::from(format!("_{}", s.to_upper_case()))
112+
}
113+
_ => Cow::from(s.to_upper_case()),
114+
}
115+
}
116+
}
117+
101118
impl ToSanitizedPascalCase for str {
102119
fn to_sanitized_pascal_case(&self) -> Cow<str> {
103120
let s = self.replace(BLACKLIST_CHARS, "");
@@ -138,7 +155,7 @@ pub fn expand(registers: &[Register]) -> Vec<ExpandedRegister> {
138155
offset: info.address_offset,
139156
ty: Either::Left(
140157
info.name
141-
.to_sanitized_pascal_case()
158+
.to_sanitized_upper_case()
142159
.into_owned(),
143160
),
144161
},
@@ -153,7 +170,7 @@ pub fn expand(registers: &[Register]) -> Vec<ExpandedRegister> {
153170
info.name.replace("%s", "")
154171
};
155172

156-
let ty = Rc::new(ty.to_sanitized_pascal_case().into_owned());
173+
let ty = Rc::new(ty.to_sanitized_upper_case().into_owned());
157174

158175
let indices = array_info
159176
.dim_index

0 commit comments

Comments
 (0)