315
315
//! gpioa.dir.write(|w| w.pin0().output());
316
316
//! ```
317
317
//!
318
- //! The `bits` method is still available but will become safe if it's impossible to
319
- //! write a reserved bit pattern into the register
318
+ //! The `bits` method is still available but will become safe if it's impossible
319
+ //! to write a reserved bit pattern into the register
320
320
//!
321
321
//! ```
322
322
//! // safe because there are only two options: `0` or `1`
@@ -345,6 +345,10 @@ use svd::{Access, BitRange, Defaults, EnumeratedValues, Field, Peripheral,
345
345
Register , RegisterInfo , Usage } ;
346
346
use syn:: * ;
347
347
348
+ /// List of chars that some vendors use in their peripheral/field names but
349
+ /// that are not valid in Rust ident
350
+ const BLACKLIST_CHARS : & [ char ] = & [ '(' , ')' ] ;
351
+
348
352
trait ToSanitizedPascalCase {
349
353
fn to_sanitized_pascal_case ( & self ) -> Cow < str > ;
350
354
}
@@ -356,84 +360,89 @@ trait ToSanitizedSnakeCase {
356
360
impl ToSanitizedSnakeCase for str {
357
361
fn to_sanitized_snake_case ( & self ) -> Cow < str > {
358
362
macro_rules! keywords {
359
- ( $( $kw: ident) ,+, ) => {
360
- Cow :: from( match & self . to_lowercase( ) [ ..] {
363
+ ( $s : expr , $ ( $kw: ident) ,+, ) => {
364
+ Cow :: from( match & $s . to_lowercase( ) [ ..] {
361
365
$( stringify!( $kw) => concat!( stringify!( $kw) , "_" ) ) ,+,
362
- _ => return Cow :: from( self . to_snake_case( ) )
366
+ _ => return Cow :: from( $s . to_snake_case( ) )
363
367
} )
364
368
}
365
369
}
366
370
367
- match self . chars ( ) . next ( ) . unwrap_or ( '\0' ) {
371
+ let s = self . replace ( BLACKLIST_CHARS , "" ) ;
372
+
373
+ match s. chars ( ) . next ( ) . unwrap_or ( '\0' ) {
368
374
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
369
- Cow :: from ( format ! ( "_{}" , self . to_snake_case( ) ) )
375
+ Cow :: from ( format ! ( "_{}" , s . to_snake_case( ) ) )
370
376
}
371
377
_ => {
372
- keywords ! {
373
- abstract,
374
- alignof,
375
- as ,
376
- become,
377
- box,
378
- break ,
379
- const ,
380
- continue ,
381
- crate ,
382
- do,
383
- else,
384
- enum ,
385
- extern,
386
- false ,
387
- final,
388
- fn ,
389
- for ,
390
- if ,
391
- impl ,
392
- in,
393
- let ,
394
- loop ,
395
- macro,
396
- match ,
397
- mod ,
398
- move,
399
- mut ,
400
- offsetof,
401
- override,
402
- priv,
403
- proc,
404
- pub ,
405
- pure,
406
- ref,
407
- return ,
408
- self ,
409
- sizeof,
410
- static ,
411
- struct ,
412
- super ,
413
- trait ,
414
- true ,
415
- type ,
416
- typeof,
417
- unsafe ,
418
- unsized,
419
- use ,
420
- virtual,
421
- where ,
422
- while ,
423
- yield,
424
- }
378
+ keywords ! {
379
+ s,
380
+ abstract,
381
+ alignof,
382
+ as ,
383
+ become,
384
+ box,
385
+ break ,
386
+ const ,
387
+ continue ,
388
+ crate ,
389
+ do,
390
+ else,
391
+ enum ,
392
+ extern,
393
+ false ,
394
+ final,
395
+ fn ,
396
+ for ,
397
+ if ,
398
+ impl ,
399
+ in,
400
+ let ,
401
+ loop ,
402
+ macro,
403
+ match ,
404
+ mod ,
405
+ move,
406
+ mut ,
407
+ offsetof,
408
+ override,
409
+ priv,
410
+ proc,
411
+ pub ,
412
+ pure,
413
+ ref,
414
+ return ,
415
+ self ,
416
+ sizeof,
417
+ static ,
418
+ struct ,
419
+ super ,
420
+ trait ,
421
+ true ,
422
+ type ,
423
+ typeof,
424
+ unsafe ,
425
+ unsized,
426
+ use ,
427
+ virtual,
428
+ where ,
429
+ while ,
430
+ yield,
431
+ }
425
432
}
426
433
}
427
434
}
428
435
}
429
436
430
437
impl ToSanitizedPascalCase for str {
431
438
fn to_sanitized_pascal_case ( & self ) -> Cow < str > {
432
- match self . chars ( ) . next ( ) . unwrap_or ( '\0' ) {
439
+ let s = self . replace ( BLACKLIST_CHARS , "" ) ;
440
+
441
+ match s. chars ( ) . next ( ) . unwrap_or ( '\0' ) {
433
442
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
434
- Cow :: from ( format ! ( "_{}" , self . to_pascal_case( ) ) )
443
+ Cow :: from ( format ! ( "_{}" , s . to_pascal_case( ) ) )
435
444
}
436
- _ => Cow :: from ( self . to_pascal_case ( ) ) ,
445
+ _ => Cow :: from ( s . to_pascal_case ( ) ) ,
437
446
}
438
447
}
439
448
}
@@ -770,9 +779,11 @@ pub fn gen_register(r: &Register,
770
779
771
780
let field_name = Ident :: new ( & * field. name
772
781
. to_sanitized_snake_case ( ) ) ;
773
- let _field_name = Ident :: new ( & * format ! ( "_{}" ,
774
- field. name
775
- . to_snake_case( ) ) ) ;
782
+ let _field_name =
783
+ Ident :: new ( & * format ! ( "_{}" ,
784
+ field. name
785
+ . replace( BLACKLIST_CHARS , "" )
786
+ . to_snake_case( ) ) ) ;
776
787
let width = field. bit_range . width ;
777
788
let mask = Lit :: Int ( ( 1u64 << width) - 1 , IntTy :: Unsuffixed ) ;
778
789
let offset = Lit :: Int ( u64:: from ( field. bit_range . offset ) ,
@@ -807,7 +818,9 @@ pub fn gen_register(r: &Register,
807
818
if let Some ( ev) = evs. values
808
819
. iter ( )
809
820
. find ( |ev| ev. value == Some ( i) ) {
810
- let sc = Ident :: new ( & * ev. name . to_snake_case ( ) ) ;
821
+ let sc = Ident :: new ( & * ev. name
822
+ . replace ( BLACKLIST_CHARS , "" )
823
+ . to_snake_case ( ) ) ;
811
824
let doc = Cow :: from ( ev. description
812
825
. clone ( )
813
826
. unwrap_or_else ( || {
0 commit comments