@@ -97,12 +97,30 @@ pub struct Directive {
97
97
arguments : Vec < Argument > ,
98
98
}
99
99
100
+ impl Directive {
101
+ fn new ( name : Name , arguments : Vec < Argument > ) -> Directive {
102
+ Directive {
103
+ name : name,
104
+ arguments : arguments,
105
+ }
106
+ }
107
+ }
108
+
100
109
#[ derive( Debug , PartialEq , Clone ) ]
101
110
pub struct Argument {
102
111
name : Name ,
103
112
value : Value ,
104
113
}
105
114
115
+ impl Argument {
116
+ fn new ( name : Name , value : Value ) -> Argument {
117
+ Argument {
118
+ name : name,
119
+ value : value,
120
+ }
121
+ }
122
+ }
123
+
106
124
#[ derive( Debug , PartialEq , Clone ) ]
107
125
pub enum Selection {
108
126
Field ( Field ) ,
@@ -544,9 +562,9 @@ make_parser!(
544
562
545
563
fn hex_digit_to_u8 ( input : char ) -> u8 {
546
564
match input {
547
- '0' ... '9' => ( input as u8 ) - ( '0' as u8 ) ,
548
- 'a' ... 'f' => ( input as u8 ) - ( 'a' as u8 ) + 10 ,
549
- 'A' ... 'F' => ( input as u8 ) - ( 'A' as u8 ) + 10 ,
565
+ '0' ...'9' => ( input as u8 ) - ( '0' as u8 ) ,
566
+ 'a' ...'f' => ( input as u8 ) - ( 'a' as u8 ) + 10 ,
567
+ 'A' ...'F' => ( input as u8 ) - ( 'A' as u8 ) + 10 ,
550
568
_ => 0 ,
551
569
}
552
570
}
@@ -674,6 +692,47 @@ make_parser!(
674
692
}
675
693
) ;
676
694
695
+ make_parser ! (
696
+ Directives ( input: char ) -> Vec <Directive > {
697
+ many:: <Vec <_>, _>( DirectiveParser :: new( ) )
698
+ . parse_lazy( input)
699
+ }
700
+ ) ;
701
+
702
+ make_parser ! (
703
+ DirectiveParser ( input: char ) -> Directive {
704
+ char ( '@' )
705
+ . with( NameParser :: new( ) )
706
+ . and( optional( Arguments :: new( ) ) )
707
+ . map( |( name, opt_args) | {
708
+ Directive :: new( name, match opt_args {
709
+ Some ( args) => args,
710
+ None => Vec :: new( ) ,
711
+ } )
712
+ } )
713
+ . skip( many:: <Vec <_>, _>( or( WhiteSpace :: new( ) , LineTerminator :: new( & true ) ) ) )
714
+ . parse_lazy( input)
715
+ }
716
+ ) ;
717
+
718
+ make_parser ! (
719
+ Arguments ( input: char ) -> Vec <Argument > {
720
+ between( char ( '(' ) , char ( ')' ) , many:: <Vec <_>, _>( ArgumentParser :: new( ) ) )
721
+ . parse_lazy( input)
722
+ }
723
+ ) ;
724
+
725
+ make_parser ! (
726
+ ArgumentParser ( input: char ) -> Argument {
727
+ NameParser :: new( )
728
+ . skip( char ( ':' ) )
729
+ . skip( many:: <Vec <_>, _>( or( WhiteSpace :: new( ) , LineTerminator :: new( & true ) ) ) )
730
+ . and( ValueParser :: new( & false ) )
731
+ . map( |( name, value) | Argument :: new( name, value) )
732
+ . parse_lazy( input)
733
+ }
734
+ ) ;
735
+
677
736
// TODO .skip(many::<Vec<_>,_>(or(WhiteSpace::new(), LineTerminator::new(&true))))
678
737
// TODO add commas to this thing
679
738
@@ -918,8 +977,44 @@ mod tests {
918
977
assert_successful_parse ! ( StringValue , r#""""# , Value :: String ( String :: from( "" ) ) ) ;
919
978
920
979
// strings with random stuff in it
921
- assert_successful_parse ! ( StringValue , r#""hello world""# , Value :: String ( String :: from( "hello world" ) ) ) ;
922
- assert_successful_parse ! ( StringValue , r#""hello \u0025""# , Value :: String ( String :: from( "hello %" ) ) ) ;
923
- assert_successful_parse ! ( StringValue , r#""hello\n\u0025""# , Value :: String ( String :: from( "hello\n %" ) ) ) ;
980
+ assert_successful_parse ! ( StringValue ,
981
+ r#""hello world""# ,
982
+ Value :: String ( String :: from( "hello world" ) ) ) ;
983
+ assert_successful_parse ! ( StringValue ,
984
+ r#""hello \u0025""# ,
985
+ Value :: String ( String :: from( "hello %" ) ) ) ;
986
+ assert_successful_parse ! ( StringValue ,
987
+ r#""hello\n\u0025""# ,
988
+ Value :: String ( String :: from( "hello\n %" ) ) ) ;
989
+ }
990
+
991
+ #[ test]
992
+ fn test_parse_argument ( ) {
993
+ assert_successful_parse ! ( ArgumentParser ,
994
+ "x:1" ,
995
+ Argument :: new( String :: from( "x" ) , Value :: Int ( 1 ) ) ) ;
996
+ }
997
+
998
+ #[ test]
999
+ fn test_parse_directive ( ) {
1000
+ // directive no arguments
1001
+ assert_successful_parse ! ( DirectiveParser ,
1002
+ "@dir" ,
1003
+ Directive :: new( String :: from( "dir" ) , Vec :: new( ) ) ) ;
1004
+
1005
+ // directive with arguments
1006
+ assert_successful_parse ! ( DirectiveParser ,
1007
+ "@dir(x:1)" ,
1008
+ Directive :: new( String :: from( "dir" ) ,
1009
+ vec![ Argument :: new( String :: from( "x" ) , Value :: Int ( 1 ) ) ] ) ) ;
1010
+ }
1011
+
1012
+ #[ test]
1013
+ fn test_parse_directives ( ) {
1014
+ assert_successful_parse ! ( Directives ,
1015
+ "@dir\n @dir2(x:1)" ,
1016
+ vec![ Directive :: new( String :: from( "dir" ) , Vec :: new( ) ) ,
1017
+ Directive :: new( String :: from( "dir2" ) ,
1018
+ vec![ Argument :: new( String :: from( "x" ) , Value :: Int ( 1 ) ) ] ) ] ) ;
924
1019
}
925
1020
}
0 commit comments