Skip to content

Normalize SVD names to upper case where they were to pascal case #85

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 2 commits into from
Jun 5, 2017
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
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ cast = "0.2.0"
clap = "2.20.5"
either = "1.0.3"
error-chain = "0.10.0"
inflections = "1.0.0"
inflections = "1.1.0"
quote = "0.3.15"
svd-parser = "0.5.1"
syn = "0.11.9"
20 changes: 11 additions & 9 deletions src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use svd::{Access, BitRange, Defaults, Device, EnumeratedValues, Field,
use syn::{Ident, Lit};

use errors::*;
use util::{self, ToSanitizedPascalCase, ToSanitizedSnakeCase, U32Ext};
use util::{self, ToSanitizedPascalCase, ToSanitizedSnakeCase,
ToSanitizedUpperCase, U32Ext};

/// Whole device generation
pub fn device(d: &Device, items: &mut Vec<Tokens>) -> Result<()> {
Expand All @@ -24,6 +25,7 @@ pub fn device(d: &Device, items: &mut Vec<Tokens>) -> Result<()> {
#![doc = #doc]
#![deny(missing_docs)]
#![deny(warnings)]
#![allow(non_camel_case_types)]
#![feature(const_fn)]
#![no_std]

Expand Down Expand Up @@ -127,7 +129,7 @@ pub fn interrupt(peripherals: &[Peripheral], items: &mut Vec<Tokens>) {
);
}

let name_pc = Ident::new(interrupt.name.to_sanitized_pascal_case());
let name_pc = Ident::new(interrupt.name.to_sanitized_upper_case());
let description = format!(
"{} - {}",
interrupt.value,
Expand Down Expand Up @@ -237,7 +239,7 @@ pub fn peripheral(
defaults: &Defaults,
) -> Result<()> {
let name = Ident::new(&*p.name.to_uppercase());
let name_pc = Ident::new(&*p.name.to_sanitized_pascal_case());
let name_pc = Ident::new(&*p.name.to_sanitized_upper_case());
let address = util::unsuffixed(u64(p.base_address));
let description = util::respace(p.description.as_ref().unwrap_or(&p.name));

Expand Down Expand Up @@ -413,7 +415,7 @@ pub fn register(
) -> Result<()> {
let access = util::access_of(register);
let name = util::name_of(register);
let name_pc = Ident::new(&*name.to_sanitized_pascal_case());
let name_pc = Ident::new(&*name.to_sanitized_upper_case());
let name_sc = Ident::new(&*name.to_sanitized_snake_case());
let rsize = register.size
.or(defs.size)
Expand Down Expand Up @@ -637,7 +639,7 @@ pub fn fields(
fn from(f: &'a Field) -> Result<Self> {
let BitRange { offset, width } = f.bit_range;
let sc = f.name.to_sanitized_snake_case();
let pc = f.name.to_sanitized_pascal_case();
let pc = f.name.to_sanitized_upper_case();
let pc_r = Ident::new(&*format!("{}R", pc));
let pc_w = Ident::new(&*format!("{}W", pc));
let _pc_w = Ident::new(&*format!("_{}W", pc));
Expand Down Expand Up @@ -726,15 +728,15 @@ pub fn fields(
description: description,
sc: sc,
pc: Ident::new(&*ev.name
.to_sanitized_pascal_case()),
.to_sanitized_upper_case()),
value: value,
})
})
.collect::<Result<Vec<_>>>()?;

let pc_r = &f.pc_r;
if let Some(ref base) = base {
let pc = base.field.to_sanitized_pascal_case();
let pc = base.field.to_sanitized_upper_case();
let base_pc_r = Ident::new(&*format!("{}R", pc));
let description =
format!("Possible values of the field `{}`", f.name);
Expand Down Expand Up @@ -988,7 +990,7 @@ pub fn fields(
let base_pc_w = base.as_ref()
.map(
|base| {
let pc = base.field.to_sanitized_pascal_case();
let pc = base.field.to_sanitized_upper_case();
let base_pc_w = Ident::new(&*format!("{}W", pc));

if let Some(ref register) = base.register {
Expand Down Expand Up @@ -1036,7 +1038,7 @@ pub fn fields(
format!("`{:b}`", value)
}),
pc: Ident::new(&*ev.name
.to_sanitized_pascal_case()),
.to_sanitized_upper_case()),
sc: Ident::new(&*ev.name
.to_sanitized_snake_case()),
value: value,
Expand Down
21 changes: 19 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub trait ToSanitizedPascalCase {
fn to_sanitized_pascal_case(&self) -> Cow<str>;
}

pub trait ToSanitizedUpperCase {
fn to_sanitized_upper_case(&self) -> Cow<str>;
}

pub trait ToSanitizedSnakeCase {
fn to_sanitized_snake_case(&self) -> Cow<str>;
}
Expand Down Expand Up @@ -98,6 +102,19 @@ impl ToSanitizedSnakeCase for str {
}
}

impl ToSanitizedUpperCase for str {
fn to_sanitized_upper_case(&self) -> Cow<str> {
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_upper_case()))
}
_ => Cow::from(s.to_upper_case()),
}
}
}

impl ToSanitizedPascalCase for str {
fn to_sanitized_pascal_case(&self) -> Cow<str> {
let s = self.replace(BLACKLIST_CHARS, "");
Expand Down Expand Up @@ -138,7 +155,7 @@ pub fn expand(registers: &[Register]) -> Vec<ExpandedRegister> {
offset: info.address_offset,
ty: Either::Left(
info.name
.to_sanitized_pascal_case()
.to_sanitized_upper_case()
.into_owned(),
),
},
Expand All @@ -153,7 +170,7 @@ pub fn expand(registers: &[Register]) -> Vec<ExpandedRegister> {
info.name.replace("%s", "")
};

let ty = Rc::new(ty.to_sanitized_pascal_case().into_owned());
let ty = Rc::new(ty.to_sanitized_upper_case().into_owned());

let indices = array_info
.dim_index
Expand Down