@@ -2,11 +2,14 @@ use std::marker::PhantomData;
2
2
3
3
use nom;
4
4
use nom:: { IResult , Err , Context , ErrorKind } ;
5
+ //use unicode_names;
5
6
6
7
use helpers;
7
8
use helpers:: { StrSpan , name} ;
8
9
use helpers:: { AreNewlinesSpaces , NewlinesAreSpaces } ;
9
10
use functions:: varargslist;
11
+ use bytes:: bytes;
12
+ use strings:: string;
10
13
use ast:: * ;
11
14
12
15
#[ derive( Clone , Debug , PartialEq ) ]
@@ -244,92 +247,6 @@ named!(atom_expr<StrSpan, Box<Expression>>,
244
247
)
245
248
) ;
246
249
247
- named ! ( escapedchar<StrSpan , Option <char >>,
248
- preceded!( char !( '\\' ) ,
249
- alt!(
250
- char !( '\n' ) => { |_| None }
251
- | char !( '\\' ) => { |_| Some ( '\\' ) }
252
- | char !( '\'' ) => { |_| Some ( '\'' ) }
253
- | char !( '"' ) => { |_| Some ( '"' ) }
254
- | char !( 'b' ) => { |_| Some ( '\x07' ) } // BEL
255
- | char !( 'f' ) => { |_| Some ( '\x0c' ) } // FF
256
- | char !( 'n' ) => { |_| Some ( '\n' ) }
257
- | char !( 'r' ) => { |_| Some ( '\r' ) }
258
- | char !( 't' ) => { |_| Some ( '\t' ) }
259
- | char !( 'v' ) => { |_| Some ( '\x0b' ) } // VT
260
- | tuple!( one_of!( "01234567" ) , opt!( one_of!( "01234567" ) ) , opt!( one_of!( "01234567" ) ) ) => { |( c1, c2, c3) : ( char , Option <char >, Option <char >) |
261
- match ( c1. to_digit( 8 ) , c2. and_then( |c| c. to_digit( 2 ) ) , c3. and_then( |c| c. to_digit( 2 ) ) ) {
262
- ( Some ( d1) , Some ( d2) , Some ( d3) ) => :: std:: char :: from_u32( ( d1 << 6 ) + ( d2 << 3 ) + d3) ,
263
- ( Some ( d1) , Some ( d2) , None ) => :: std:: char :: from_u32( ( d1 << 3 ) + d2) ,
264
- ( Some ( d1) , None , None ) => :: std:: char :: from_u32( d1) ,
265
- _ => unreachable!( ) ,
266
- }
267
- }
268
- | preceded!( char !( 'x' ) , tuple!( one_of!( "0123456789abcdef" ) , one_of!( "0123456789abcdef" ) ) ) => { |( c1, c2) : ( char , char ) |
269
- match ( c1. to_digit( 16 ) , c2. to_digit( 16 ) ) {
270
- ( Some ( d1) , Some ( d2) ) => :: std:: char :: from_u32( ( d1 << 8 ) + d2) ,
271
- _ => unreachable!( ) ,
272
- }
273
- }
274
- | char !( 'N' ) => { |_| unimplemented!( ) } // TODO
275
- | char !( 'u' ) => { |_| unimplemented!( ) } // TODO
276
- | char !( 'U' ) => { |_| unimplemented!( ) } // TODO
277
- )
278
- )
279
- ) ;
280
-
281
- named_args ! ( shortstring( quote: char ) <StrSpan , String >,
282
- fold_many0!(
283
- alt!(
284
- call!( Self :: escapedchar)
285
- | verify!( none_of!( "\\ " ) , |c: char | c != quote) => { |c: char | Some ( c) }
286
- ) ,
287
- String :: new( ) ,
288
- |mut acc: String , c: Option <char >| { match c { Some ( c) => acc. push_str( & c. to_string( ) ) , None => ( ) } ; acc }
289
- )
290
- ) ;
291
-
292
- named_args ! ( longstring( quote: char ) <StrSpan , String >,
293
- fold_many0!(
294
- alt!(
295
- call!( Self :: escapedchar)
296
- | verify!( tuple!( peek!( take!( 3 ) ) , none_of!( "\\ " ) ) , |( s, _) : ( StrSpan , _) | { s. fragment. 0 . chars( ) . collect:: <Vec <char >>( ) != vec![ quote, quote, quote] } ) => { |( _, c) | Some ( c) }
297
- ) ,
298
- String :: new( ) ,
299
- |mut acc: String , c: Option <char >| { match c { Some ( c) => acc. push_str( & c. to_string( ) ) , None => ( ) } ; acc }
300
- )
301
- ) ;
302
-
303
- named ! ( string<StrSpan , PyString >,
304
- do_parse!(
305
- prefix: alt!( tag!( "fr" ) |tag!( "Fr" ) |tag!( "fR" ) |tag!( "FR" ) |tag!( "rf" ) |tag!( "rF" ) |tag!( "Rf" ) |tag!( "RF" ) |tag!( "r" ) |tag!( "u" ) |tag!( "R" ) |tag!( "U" ) |tag!( "f" ) |tag!( "F" ) |tag!( "" ) ) >>
306
- content: alt!(
307
- delimited!(
308
- tag!( "'''" ) ,
309
- call!( Self :: longstring, '\'' ) ,
310
- tag!( "'''" )
311
- )
312
- | delimited!(
313
- tag!( "\" \" \" " ) ,
314
- call!( Self :: longstring, '"' ) ,
315
- tag!( "\" \" \" " )
316
- )
317
- | delimited!(
318
- char !( '\'' ) ,
319
- call!( Self :: shortstring, '\'' ) ,
320
- char !( '\'' )
321
- )
322
- | delimited!(
323
- char !( '"' ) ,
324
- call!( Self :: shortstring, '"' ) ,
325
- char !( '"' )
326
- )
327
- ) >> (
328
- PyString { prefix: prefix. to_string( ) , content: content. to_string( ) }
329
- )
330
- )
331
- ) ;
332
-
333
250
// atom: ('(' [yield_expr|testlist_comp] ')' |
334
251
// '[' [testlist_comp] ']' |
335
252
// '{' [dictorsetmaker] '}' |
@@ -340,8 +257,13 @@ named!(atom<StrSpan, Box<Expression>>,
340
257
| tag!( "None" ) => { |_| Expression :: None }
341
258
| tag!( "True" ) => { |_| Expression :: True }
342
259
| tag!( "False" ) => { |_| Expression :: False }
260
+ | separated_nonempty_list!( spaces!( ) , string) => { |s| Expression :: String ( s) }
261
+ | separated_nonempty_list!( spaces!( ) , bytes) => { |v| {
262
+ let mut v2 = Vec :: new( ) ;
263
+ for b in v { v2. extend( b) }
264
+ Expression :: Bytes ( v2)
265
+ } }
343
266
| name => { |n| Expression :: Name ( n) }
344
- | separated_nonempty_list!( spaces!( ) , call!( Self :: string) ) => { |s| Expression :: String ( s) }
345
267
| ws3!( tuple!( char !( '[' ) , opt!( ws!( char !( ' ' ) ) ) , char !( ']' ) ) ) => { |_| Expression :: ListLiteral ( vec![ ] ) }
346
268
| ws3!( tuple!( char !( '{' ) , opt!( ws!( char !( ' ' ) ) ) , char !( '}' ) ) ) => { |_| Expression :: DictLiteral ( vec![ ] ) }
347
269
| ws3!( tuple!( char !( '(' ) , opt!( ws!( char !( ' ' ) ) ) , char !( ')' ) ) ) => { |_| Expression :: TupleLiteral ( vec![ ] ) }
@@ -675,25 +597,59 @@ mod tests {
675
597
use super :: * ;
676
598
677
599
#[ test]
678
- fn test_atom ( ) {
600
+ fn test_string ( ) {
679
601
let atom = ExpressionParser :: < NewlinesAreNotSpaces > :: atom;
680
602
let new_pystring = |s : & str | PyString { prefix : "" . to_string ( ) , content : s. to_string ( ) } ;
681
- assert_parse_eq ( atom ( make_strspan ( "foo " ) ) , Ok ( ( make_strspan ( " " ) , Box :: new ( Expression :: Name ( "foo" . to_string ( ) ) ) ) ) ) ;
682
- assert_parse_eq ( atom ( make_strspan ( r#""foo" "# ) ) , Ok ( ( make_strspan ( " " ) , Box :: new ( Expression :: String ( vec ! [ new_pystring( "foo" ) ] ) ) ) ) ) ;
683
- assert_parse_eq ( atom ( make_strspan ( r#""foo" "bar""# ) ) , Ok ( ( make_strspan ( "" ) , Box :: new ( Expression :: String ( vec ! [ new_pystring( "foo" ) , new_pystring( "bar" ) ] ) ) ) ) ) ;
684
- assert_parse_eq ( atom ( make_strspan ( r#""fo\"o" "# ) ) , Ok ( ( make_strspan ( " " ) , Box :: new ( Expression :: String ( vec ! [ new_pystring( "fo\" o" ) ] ) ) ) ) ) ;
685
- assert_parse_eq ( atom ( make_strspan ( r#""fo"o" "# ) ) , Ok ( ( make_strspan ( r#"o" "# ) , Box :: new ( Expression :: String ( vec ! [ new_pystring( "fo" ) ] ) ) ) ) ) ;
686
- assert_parse_eq ( atom ( make_strspan ( r#""fo \" o" "# ) ) , Ok ( ( make_strspan ( " " ) , Box :: new ( Expression :: String ( vec ! [ new_pystring( "fo \" o" ) ] ) ) ) ) ) ;
687
- assert_parse_eq ( atom ( make_strspan ( r#"'fo \' o' "# ) ) , Ok ( ( make_strspan ( " " ) , Box :: new ( Expression :: String ( vec ! [ new_pystring( "fo ' o" ) ] ) ) ) ) ) ;
603
+ assert_parse_eq ( atom ( make_strspan ( r#""foo" "# ) ) , Ok ( ( make_strspan ( " " ) ,
604
+ Box :: new ( Expression :: String ( vec ! [ new_pystring( "foo" ) ] ) ) )
605
+ ) ) ;
606
+ assert_parse_eq ( atom ( make_strspan ( r#""foo" "bar""# ) ) , Ok ( ( make_strspan ( "" ) ,
607
+ Box :: new ( Expression :: String ( vec ! [ new_pystring( "foo" ) , new_pystring( "bar" ) ] ) ) )
608
+ ) ) ;
609
+ assert_parse_eq ( atom ( make_strspan ( r#""fo\"o" "# ) ) , Ok ( ( make_strspan ( " " ) ,
610
+ Box :: new ( Expression :: String ( vec ! [ new_pystring( "fo\" o" ) ] ) ) )
611
+ ) ) ;
612
+ assert_parse_eq ( atom ( make_strspan ( r#""fo"o" "# ) ) , Ok ( ( make_strspan ( r#"o" "# ) ,
613
+ Box :: new ( Expression :: String ( vec ! [ new_pystring( "fo" ) ] ) ) )
614
+ ) ) ;
615
+ assert_parse_eq ( atom ( make_strspan ( r#""fo \" o" "# ) ) , Ok ( ( make_strspan ( " " ) ,
616
+ Box :: new ( Expression :: String ( vec ! [ new_pystring( "fo \" o" ) ] ) ) )
617
+ ) ) ;
618
+ assert_parse_eq ( atom ( make_strspan ( r#"'fo \' o' "# ) ) , Ok ( ( make_strspan ( " " ) ,
619
+ Box :: new ( Expression :: String ( vec ! [ new_pystring( "fo ' o" ) ] ) ) )
620
+ ) ) ;
688
621
}
689
622
690
623
#[ test]
691
- fn test_triple_quotes ( ) {
624
+ fn test_triple_quotes_string ( ) {
692
625
let new_pystring = |s : & str | PyString { prefix : "" . to_string ( ) , content : s. to_string ( ) } ;
693
626
let atom = ExpressionParser :: < NewlinesAreNotSpaces > :: atom;
694
627
assert_parse_eq ( atom ( make_strspan ( r#"'''fo ' o''' "# ) ) , Ok ( ( make_strspan ( " " ) , Box :: new ( Expression :: String ( vec ! [ new_pystring( "fo ' o" ) ] ) ) ) ) ) ;
695
628
}
696
629
630
+ #[ test]
631
+ fn test_bytes ( ) {
632
+ let atom = ExpressionParser :: < NewlinesAreNotSpaces > :: atom;
633
+ assert_parse_eq ( atom ( make_strspan ( r#"b"foo" "# ) ) , Ok ( ( make_strspan ( " " ) ,
634
+ Box :: new ( Expression :: Bytes ( b"foo" . to_vec ( ) ) ) )
635
+ ) ) ;
636
+ assert_parse_eq ( atom ( make_strspan ( r#"b"foo" "bar""# ) ) , Ok ( ( make_strspan ( "" ) ,
637
+ Box :: new ( Expression :: Bytes ( b"foobar" . to_vec ( ) ) ) )
638
+ ) ) ;
639
+ assert_parse_eq ( atom ( make_strspan ( r#"b"fo\"o" "# ) ) , Ok ( ( make_strspan ( " " ) ,
640
+ Box :: new ( Expression :: Bytes ( b"fo\" o" . to_vec ( ) ) ) )
641
+ ) ) ;
642
+ assert_parse_eq ( atom ( make_strspan ( r#"b"fo"o" "# ) ) , Ok ( ( make_strspan ( r#"o" "# ) ,
643
+ Box :: new ( Expression :: Bytes ( b"fo" . to_vec ( ) ) ) )
644
+ ) ) ;
645
+ assert_parse_eq ( atom ( make_strspan ( r#"b"fo \" o" "# ) ) , Ok ( ( make_strspan ( " " ) ,
646
+ Box :: new ( Expression :: Bytes ( b"fo \" o" . to_vec ( ) ) ) )
647
+ ) ) ;
648
+ assert_parse_eq ( atom ( make_strspan ( r#"b'fo \' o' "# ) ) , Ok ( ( make_strspan ( " " ) ,
649
+ Box :: new ( Expression :: Bytes ( b"fo ' o" . to_vec ( ) ) ) )
650
+ ) ) ;
651
+ }
652
+
697
653
#[ test]
698
654
fn test_ternary ( ) {
699
655
let test = ExpressionParser :: < NewlinesAreNotSpaces > :: test;
0 commit comments