Skip to content

Commit 8a929a5

Browse files
authored
Merge pull request #240 from therealprof/fixes/invalid-characters
Added util::escape_brackets function to escape unescpad brackets Travis successful, merging manually.
2 parents cb4bc38 + 35fc5ad commit 8a929a5

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

src/generate/interrupt.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ pub fn render(
5050
.description
5151
.as_ref()
5252
.map(|s| util::respace(s))
53+
.as_ref()
54+
.map(|s| util::escape_brackets(s))
5355
.unwrap_or_else(|| interrupt.name.clone())
5456
);
5557

src/generate/peripheral.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn render(
2121

2222
let name_pc = Ident::new(&*p.name.to_sanitized_upper_case());
2323
let address = util::hex(p.base_address);
24-
let description = util::respace(p.description.as_ref().unwrap_or(&p.name));
24+
let description = util::escape_brackets(util::respace(p.description.as_ref().unwrap_or(&p.name)).as_ref());
2525

2626
let name_sc = Ident::new(&*p.name.to_sanitized_snake_case());
2727
let (base, derived) = if let Some(base) = p.derived_from.as_ref() {
@@ -95,7 +95,7 @@ pub fn render(
9595
)?);
9696
}
9797

98-
let description = util::respace(p.description.as_ref().unwrap_or(&p.name));
98+
let description = util::escape_brackets(util::respace(p.description.as_ref().unwrap_or(&p.name)).as_ref());
9999
out.push(quote! {
100100
#[doc = #description]
101101
pub mod #name_sc {
@@ -396,7 +396,7 @@ fn register_or_cluster_block_stable(
396396
let comment = &format!(
397397
"0x{:02x} - {}",
398398
reg_block_field.offset,
399-
util::respace(&reg_block_field.description),
399+
util::escape_brackets(util::respace(&reg_block_field.description).as_ref()),
400400
)[..];
401401

402402
fields.append(quote! {
@@ -473,14 +473,13 @@ fn register_or_cluster_block_nightly(
473473
let comment = &format!(
474474
"0x{:02x} - {}",
475475
reg_block_field.offset,
476-
util::respace(&reg_block_field.description),
476+
util::escape_brackets(util::respace(&reg_block_field.description).as_ref()),
477477
)[..];
478478

479479
region_fields.append(quote! {
480480
#[doc = #comment]
481481
});
482482

483-
484483
reg_block_field.field.to_tokens(&mut region_fields);
485484
Ident::new(",").to_tokens(&mut region_fields);
486485
}
@@ -718,7 +717,7 @@ fn cluster_block(
718717
let mut mod_items: Vec<Tokens> = vec![];
719718

720719
// name_sc needs to take into account array type.
721-
let description = util::respace(&c.description);
720+
let description = util::escape_brackets(util::respace(&c.description).as_ref());
722721

723722
// Generate the register block.
724723
let mod_name = match *c {

src/generate/register.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn render(
3131
rsize.next_power_of_two()
3232
};
3333
let rty = rsize.to_ty()?;
34-
let description = util::respace(&register.description);
34+
let description = util::escape_brackets(util::respace(&register.description).as_ref());
3535

3636
let unsafety = unsafety(register.write_constraint.as_ref(), rsize);
3737

@@ -728,7 +728,7 @@ pub fn fields(
728728
let pc = &v.pc;
729729
let sc = &v.sc;
730730

731-
let doc = util::respace(&v.doc);
731+
let doc = util::escape_brackets(util::respace(&v.doc).as_ref());
732732
if let Some(enum_) = base_pc_w.as_ref() {
733733
proxy_items.push(quote! {
734734
#[doc = #doc]

src/util.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,33 @@ pub fn respace(s: &str) -> String {
137137
s.split_whitespace().collect::<Vec<_>>().join(" ")
138138
}
139139

140+
pub fn escape_brackets(s: &str) -> String {
141+
s.split('[')
142+
.fold("".to_string(), |acc, x| {
143+
if acc == "" {
144+
x.to_string()
145+
} else {
146+
if acc.ends_with("\\") {
147+
acc.to_owned() + "[" + &x.to_string()
148+
} else {
149+
acc.to_owned() + "\\[" + &x.to_string()
150+
}
151+
}
152+
})
153+
.split(']')
154+
.fold("".to_string(), |acc, x| {
155+
if acc == "" {
156+
x.to_string()
157+
} else {
158+
if acc.ends_with("\\") {
159+
acc.to_owned() + "[" + &x.to_string()
160+
} else {
161+
acc.to_owned() + "\\[" + &x.to_string()
162+
}
163+
}
164+
})
165+
}
166+
140167
pub fn name_of(register: &Register) -> Cow<str> {
141168
match *register {
142169
Register::Single(ref info) => Cow::from(&*info.name),

0 commit comments

Comments
 (0)