@@ -5,8 +5,8 @@ use std::collections::HashMap;
5
5
use std:: marker:: PhantomData ;
6
6
7
7
use combine:: { Parser , ParseResult , Stream } ;
8
- use combine:: char:: { tab, char, crlf, string, letter, alpha_num} ;
9
- use combine:: combinator:: { between, many, none_of, or, optional, value} ;
8
+ use combine:: char:: { tab, char, crlf, string, letter, alpha_num, digit } ;
9
+ use combine:: combinator:: { between, many, none_of, one_of , or, optional, value} ;
10
10
11
11
pub type Name = String ;
12
12
pub type SelectionSet = Vec < Selection > ;
@@ -309,8 +309,9 @@ make_parser!(
309
309
. skip( char ( ':' ) )
310
310
. skip( many:: <Vec <_>, _>( or( WhiteSpace :: new( ) , LineTerminator :: new( & true ) ) ) )
311
311
. and( TypeParser :: new( ) )
312
- . map( |( variable, var_type) | {
313
- VariableDefinition :: new( variable, var_type, None )
312
+ . and( optional( DefaultValue :: new( ) ) )
313
+ . map( |( ( variable, var_type) , opt_type) | {
314
+ VariableDefinition :: new( variable, var_type, opt_type)
314
315
} )
315
316
. skip( many:: <Vec <_>, _>( or( WhiteSpace :: new( ) , LineTerminator :: new( & true ) ) ) )
316
317
. parse_stream( input)
@@ -330,7 +331,7 @@ make_parser!(
330
331
let named_type = NameParser :: new( ) . map( Type :: Named ) ;
331
332
let list_type = between( char ( '[' ) , char ( ']' ) , TypeParser :: new( ) ) . map( |t| Type :: List ( Box :: new( t) ) ) ;
332
333
333
- // i can't clone for some reason -.-
334
+ // TODO i can't clone for some reason -.-
334
335
let non_null_type = char ( '!' )
335
336
. with( or(
336
337
NameParser :: new( ) . map( Type :: Named ) ,
@@ -344,6 +345,60 @@ make_parser!(
344
345
}
345
346
) ;
346
347
348
+ make_parser ! (
349
+ DefaultValue ( input: char ) -> Value {
350
+ char ( '=' )
351
+ . skip( many:: <Vec <_>, _>( or( WhiteSpace :: new( ) , LineTerminator :: new( & true ) ) ) )
352
+ . with( ValueParser :: new( & false ) )
353
+ . parse_stream( input)
354
+ }
355
+ ) ;
356
+
357
+ make_parser ! (
358
+ ValueParser ( input: char , variable: & bool ) -> Value {
359
+
360
+ let mut constants = IntValue :: new( ) ;
361
+
362
+ if * variable {
363
+ VariableParser :: new( ) . with( constants) . parse_stream( input)
364
+ } else {
365
+ constants. parse_stream( input)
366
+ }
367
+ }
368
+ ) ;
369
+
370
+ make_parser ! (
371
+ IntValue ( input: char ) -> Value {
372
+ optional( char ( '-' ) )
373
+ . and(
374
+ or(
375
+ char ( '0' ) . map( |c| {
376
+ let mut result = String :: new( ) ;
377
+ result. push( c) ;
378
+ result
379
+ } ) ,
380
+ one_of( "123456789" . chars( ) )
381
+ . and( many:: <String , _>( digit( ) ) )
382
+ . map( |( c, rest) | {
383
+ let mut result = String :: new( ) ;
384
+ result. push( c) ;
385
+ result. push_str( & rest) ;
386
+ result
387
+ } )
388
+ )
389
+ . map( |number| number. parse:: <i32 >( ) . unwrap( ) )
390
+ )
391
+ . map( |( neg, number) | {
392
+ match neg {
393
+ Some ( _) => -number,
394
+ None => number
395
+ }
396
+ } )
397
+ . map( Value :: Int )
398
+ . parse_stream( input)
399
+ }
400
+ ) ;
401
+
347
402
make_parser ! (
348
403
Alias ( input: char ) -> Name {
349
404
NameParser :: new( )
@@ -431,6 +486,27 @@ mod tests {
431
486
fn test_parse_variabledefinition_nodefaultvalue ( ) {
432
487
assert_successful_parse ! ( VariableDefinitionParser ,
433
488
"$devicePicSize: Int" ,
434
- VariableDefinition :: new( String :: from( "devicePicSize" ) , Type :: Named ( String :: from( "Int" ) ) , None ) ) ;
489
+ VariableDefinition :: new( String :: from( "devicePicSize" ) ,
490
+ Type :: Named ( String :: from( "Int" ) ) ,
491
+ None ) ) ;
492
+ }
493
+
494
+ #[ test]
495
+ fn test_parse_variabledefinition_defaultvalue ( ) {
496
+ assert_successful_parse ! ( VariableDefinitionParser ,
497
+ "$devicePicSize: Int = 10" ,
498
+ VariableDefinition :: new( String :: from( "devicePicSize" ) ,
499
+ Type :: Named ( String :: from( "Int" ) ) ,
500
+ Some ( Value :: Int ( 10 ) ) ) ) ;
501
+ }
502
+
503
+ #[ test]
504
+ fn test_parse_value ( ) {
505
+ assert_successful_parse ! ( IntValue , "0" , Value :: Int ( 0 ) ) ;
506
+ assert_successful_parse ! ( IntValue , "-0" , Value :: Int ( 0 ) ) ;
507
+ assert_successful_parse ! ( IntValue , "1" , Value :: Int ( 1 ) ) ;
508
+ assert_successful_parse ! ( IntValue , "-1" , Value :: Int ( -1 ) ) ;
509
+ assert_successful_parse ! ( IntValue , "10" , Value :: Int ( 10 ) ) ;
510
+ assert_successful_parse ! ( IntValue , "-10" , Value :: Int ( -10 ) ) ;
435
511
}
436
512
}
0 commit comments