16
16
17
17
use std:: array;
18
18
use std:: collections:: HashMap ;
19
+ use std:: ops:: Deref ;
19
20
use std:: fmt;
20
21
use std:: slice;
21
22
@@ -423,16 +424,28 @@ impl<R: RuleType> StringLiteralParser<R> {
423
424
}
424
425
425
426
/// Map of symbol and function aliases.
426
- #[ derive( Clone , Debug , Default ) ]
427
- pub struct AliasesMap < P > {
428
- symbol_aliases : HashMap < String , String > ,
427
+ #[ derive( Clone , Debug ) ]
428
+ pub struct AliasesMap < P , V : Deref > {
429
+ symbol_aliases : HashMap < String , V > ,
429
430
// name: [(params, defn)] (sorted by arity)
430
- function_aliases : HashMap < String , Vec < ( Vec < String > , String ) > > ,
431
+ function_aliases : HashMap < String , Vec < ( Vec < String > , V ) > > ,
431
432
// Parser type P helps prevent misuse of AliasesMap of different language.
432
433
parser : P ,
433
434
}
434
435
435
- impl < P > AliasesMap < P > {
436
+ // Unfortunately, #[derive(Default)] doesn't work correctly since V isn't
437
+ // Default. https://stackoverflow.com/questions/59538071/the-trait-bound-t-stddefaultdefault-is-not-satisfied-when-using-phantomda
438
+ impl < P : Default , V : Deref > Default for AliasesMap < P , V > {
439
+ fn default ( ) -> Self {
440
+ Self {
441
+ symbol_aliases : Default :: default ( ) ,
442
+ function_aliases : Default :: default ( ) ,
443
+ parser : Default :: default ( ) ,
444
+ }
445
+ }
446
+ }
447
+
448
+ impl < P , V : Deref > AliasesMap < P , V > {
436
449
/// Creates an empty aliases map with default-constructed parser.
437
450
pub fn new ( ) -> Self
438
451
where
@@ -445,7 +458,7 @@ impl<P> AliasesMap<P> {
445
458
///
446
459
/// Returns error if `decl` is invalid. The `defn` part isn't checked. A bad
447
460
/// `defn` will be reported when the alias is substituted.
448
- pub fn insert ( & mut self , decl : impl AsRef < str > , defn : impl Into < String > ) -> Result < ( ) , P :: Error >
461
+ pub fn insert ( & mut self , decl : impl AsRef < str > , defn : impl Into < V > ) -> Result < ( ) , P :: Error >
449
462
where
450
463
P : AliasDeclarationParser ,
451
464
{
@@ -475,46 +488,53 @@ impl<P> AliasesMap<P> {
475
488
}
476
489
477
490
/// Looks up symbol alias by name. Returns identifier and definition text.
478
- pub fn get_symbol ( & self , name : & str ) -> Option < ( AliasId < ' _ > , & str ) > {
491
+ pub fn get_symbol ( & self , name : & str ) -> Option < ( AliasId < ' _ > , & < V as Deref > :: Target ) > {
479
492
self . symbol_aliases
480
493
. get_key_value ( name)
481
- . map ( |( name, defn) | ( AliasId :: Symbol ( name) , defn. as_ref ( ) ) )
494
+ . map ( |( name, defn) | ( AliasId :: Symbol ( name) , defn. deref ( ) ) )
482
495
}
483
496
484
497
/// Looks up function alias by name and arity. Returns identifier, list of
485
498
/// parameter names, and definition text.
486
- pub fn get_function ( & self , name : & str , arity : usize ) -> Option < ( AliasId < ' _ > , & [ String ] , & str ) > {
499
+ pub fn get_function (
500
+ & self ,
501
+ name : & str ,
502
+ arity : usize ,
503
+ ) -> Option < ( AliasId < ' _ > , & [ String ] , & <V as Deref >:: Target ) > {
487
504
let overloads = self . get_function_overloads ( name) ?;
488
505
overloads. find_by_arity ( arity)
489
506
}
490
507
491
508
/// Looks up function aliases by name.
492
- fn get_function_overloads ( & self , name : & str ) -> Option < AliasFunctionOverloads < ' _ > > {
509
+ fn get_function_overloads ( & self , name : & str ) -> Option < AliasFunctionOverloads < ' _ , V > > {
493
510
let ( name, overloads) = self . function_aliases . get_key_value ( name) ?;
494
511
Some ( AliasFunctionOverloads { name, overloads } )
495
512
}
496
513
}
497
514
498
515
#[ derive( Clone , Copy , Debug ) ]
499
- struct AliasFunctionOverloads < ' a > {
516
+ struct AliasFunctionOverloads < ' a , V : Deref > {
500
517
name : & ' a String ,
501
- overloads : & ' a Vec < ( Vec < String > , String ) > ,
518
+ overloads : & ' a Vec < ( Vec < String > , V ) > ,
502
519
}
503
520
504
- impl < ' a > AliasFunctionOverloads < ' a > {
505
- fn arities ( self ) -> impl DoubleEndedIterator < Item = usize > + ExactSizeIterator + ' a {
521
+ impl < ' a , V : Deref > AliasFunctionOverloads < ' a , V > {
522
+ fn arities ( & self ) -> impl DoubleEndedIterator < Item = usize > + ExactSizeIterator + ' a {
506
523
self . overloads . iter ( ) . map ( |( params, _) | params. len ( ) )
507
524
}
508
525
509
- fn min_arity ( self ) -> usize {
526
+ fn min_arity ( & self ) -> usize {
510
527
self . arities ( ) . next ( ) . unwrap ( )
511
528
}
512
529
513
- fn max_arity ( self ) -> usize {
530
+ fn max_arity ( & self ) -> usize {
514
531
self . arities ( ) . next_back ( ) . unwrap ( )
515
532
}
516
533
517
- fn find_by_arity ( self , arity : usize ) -> Option < ( AliasId < ' a > , & ' a [ String ] , & ' a str ) > {
534
+ fn find_by_arity (
535
+ & self ,
536
+ arity : usize ,
537
+ ) -> Option < ( AliasId < ' a > , & ' a [ String ] , & ' a <V as Deref >:: Target ) > {
518
538
let index = self
519
539
. overloads
520
540
. binary_search_by_key ( & arity, |( params, _) | params. len ( ) )
@@ -609,7 +629,7 @@ pub trait AliasExpandError: Sized {
609
629
#[ derive( Debug ) ]
610
630
struct AliasExpander < ' i , T , P > {
611
631
/// Alias symbols and functions that are globally available.
612
- aliases_map : & ' i AliasesMap < P > ,
632
+ aliases_map : & ' i AliasesMap < P , String > ,
613
633
/// Stack of aliases and local parameters currently expanding.
614
634
states : Vec < AliasExpandingState < ' i , T > > ,
615
635
}
@@ -708,7 +728,7 @@ where
708
728
/// Expands aliases recursively.
709
729
pub fn expand_aliases < ' i , T , P > (
710
730
node : ExpressionNode < ' i , T > ,
711
- aliases_map : & ' i AliasesMap < P > ,
731
+ aliases_map : & ' i AliasesMap < P , String > ,
712
732
) -> Result < ExpressionNode < ' i , T > , P :: Error >
713
733
where
714
734
T : AliasExpandableExpression < ' i > + Clone ,
0 commit comments