@@ -21,6 +21,7 @@ use std::gc::Gc;
21
21
use std:: io:: File ;
22
22
use std:: rc:: Rc ;
23
23
use std:: str;
24
+ use std:: iter;
24
25
25
26
pub mod lexer;
26
27
pub mod parser;
@@ -327,7 +328,7 @@ pub fn str_lit(lit: &str) -> String {
327
328
let error = |i| format ! ( "lexer should have rejected {} at {}" , lit, i) ;
328
329
329
330
/// Eat everything up to a non-whitespace
330
- fn eat < ' a > ( it : & mut :: std :: iter:: Peekable < ( uint , char ) , :: std :: str:: CharOffsets < ' a > > ) {
331
+ fn eat < ' a > ( it : & mut iter:: Peekable < ( uint , char ) , str:: CharOffsets < ' a > > ) {
331
332
loop {
332
333
match it. peek ( ) . map ( |x| x. val1 ( ) ) {
333
334
Some ( ' ' ) | Some ( '\n' ) | Some ( '\r' ) | Some ( '\t' ) => {
@@ -471,35 +472,54 @@ pub fn binary_lit(lit: &str) -> Rc<Vec<u8>> {
471
472
// FIXME #8372: This could be a for-loop if it didn't borrow the iterator
472
473
let error = |i| format ! ( "lexer should have rejected {} at {}" , lit, i) ;
473
474
475
+ /// Eat everything up to a non-whitespace
476
+ fn eat < ' a , I : Iterator < ( uint , u8 ) > > ( it : & mut iter:: Peekable < ( uint , u8 ) , I > ) {
477
+ loop {
478
+ match it. peek ( ) . map ( |x| x. val1 ( ) ) {
479
+ Some ( b' ' ) | Some ( b'\n' ) | Some ( b'\r' ) | Some ( b'\t' ) => {
480
+ it. next ( ) ;
481
+ } ,
482
+ _ => { break ; }
483
+ }
484
+ }
485
+ }
486
+
474
487
// binary literals *must* be ASCII, but the escapes don't have to be
475
- let mut chars = lit. as_bytes ( ) . iter ( ) . enumerate ( ) . peekable ( ) ;
488
+ let mut chars = lit. bytes ( ) . enumerate ( ) . peekable ( ) ;
476
489
loop {
477
490
match chars. next ( ) {
478
- Some ( ( i, & c) ) => {
479
- if c == b'\\' {
480
- if * chars. peek ( ) . expect ( error ( i) . as_slice ( ) ) . val1 ( ) == b'\n' {
481
- loop {
482
- // eat everything up to a non-whitespace
483
- match chars. peek ( ) . map ( |x| * x. val1 ( ) ) {
484
- Some ( b' ' ) | Some ( b'\n' ) | Some ( b'\r' ) | Some ( b'\t' ) => {
485
- chars. next ( ) ;
486
- } ,
487
- _ => { break ; }
488
- }
491
+ Some ( ( i, b'\\' ) ) => {
492
+ let em = error ( i) ;
493
+ match chars. peek ( ) . expect ( em. as_slice ( ) ) . val1 ( ) {
494
+ b'\n' => eat ( & mut chars) ,
495
+ b'\r' => {
496
+ chars. next ( ) ;
497
+ if chars. peek ( ) . expect ( em. as_slice ( ) ) . val1 ( ) != b'\n' {
498
+ fail ! ( "lexer accepted bare CR" ) ;
489
499
}
490
- } else {
500
+ eat ( & mut chars) ;
501
+ }
502
+ _ => {
491
503
// otherwise, a normal escape
492
504
let ( c, n) = byte_lit ( lit. slice_from ( i) ) ;
493
- for _ in range ( 0 , n - 1 ) { // we don't need to move past the first \
505
+ // we don't need to move past the first \
506
+ for _ in range ( 0 , n - 1 ) {
494
507
chars. next ( ) ;
495
508
}
496
509
res. push ( c) ;
497
510
}
498
- } else {
499
- res. push ( c) ;
500
511
}
501
512
} ,
502
- None => { break ; }
513
+ Some ( ( i, b'\r' ) ) => {
514
+ let em = error ( i) ;
515
+ if chars. peek ( ) . expect ( em. as_slice ( ) ) . val1 ( ) != b'\n' {
516
+ fail ! ( "lexer accepted bare CR" ) ;
517
+ }
518
+ chars. next ( ) ;
519
+ res. push ( b'\n' ) ;
520
+ }
521
+ Some ( ( _, c) ) => res. push ( c) ,
522
+ None => break ,
503
523
}
504
524
}
505
525
0 commit comments