@@ -21,6 +21,7 @@ use std::gc::Gc;
2121use std:: io:: File ;
2222use std:: rc:: Rc ;
2323use std:: str;
24+ use std:: iter;
2425
2526pub mod lexer;
2627pub mod parser;
@@ -327,7 +328,7 @@ pub fn str_lit(lit: &str) -> String {
327328 let error = |i| format ! ( "lexer should have rejected {} at {}" , lit, i) ;
328329
329330 /// 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 > > ) {
331332 loop {
332333 match it. peek ( ) . map ( |x| x. val1 ( ) ) {
333334 Some ( ' ' ) | Some ( '\n' ) | Some ( '\r' ) | Some ( '\t' ) => {
@@ -471,35 +472,54 @@ pub fn binary_lit(lit: &str) -> Rc<Vec<u8>> {
471472 // FIXME #8372: This could be a for-loop if it didn't borrow the iterator
472473 let error = |i| format ! ( "lexer should have rejected {} at {}" , lit, i) ;
473474
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+
474487 // 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 ( ) ;
476489 loop {
477490 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" ) ;
489499 }
490- } else {
500+ eat ( & mut chars) ;
501+ }
502+ _ => {
491503 // otherwise, a normal escape
492504 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 ) {
494507 chars. next ( ) ;
495508 }
496509 res. push ( c) ;
497510 }
498- } else {
499- res. push ( c) ;
500511 }
501512 } ,
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 ,
503523 }
504524 }
505525
0 commit comments