@@ -94,43 +94,50 @@ fn parse_unicode(input: &mut &[u8], digits: u8) -> Option<char> {
9494 char:: from_u32 ( ret)
9595}
9696
97- pub fn parse_escape_code ( rest : & mut & [ u8 ] ) -> EscapedChar {
97+ /// Represents an invalid escape sequence.
98+ #[ derive( Debug ) ]
99+ pub struct EscapeError { }
100+
101+ /// Parse an escape sequence, like `\n` or `\xff`, etc.
102+ pub fn parse_escape_code ( rest : & mut & [ u8 ] ) -> Result < EscapedChar , EscapeError > {
98103 if let [ c, new_rest @ ..] = rest {
99104 // This is for the \NNN syntax for octal sequences.
100105 // Note that '0' is intentionally omitted because that
101106 // would be the \0NNN syntax.
102107 if let b'1' ..=b'7' = c {
103108 if let Some ( parsed) = parse_code ( rest, Base :: Oct ) {
104- return EscapedChar :: Byte ( parsed) ;
109+ return Ok ( EscapedChar :: Byte ( parsed) ) ;
105110 }
106111 }
107112
108113 * rest = new_rest;
109114 match c {
110- b'\\' => EscapedChar :: Byte ( b'\\' ) ,
111- b'"' => EscapedChar :: Byte ( b'"' ) ,
112- b'a' => EscapedChar :: Byte ( b'\x07' ) ,
113- b'b' => EscapedChar :: Byte ( b'\x08' ) ,
114- b'c' => EscapedChar :: End ,
115- b'e' => EscapedChar :: Byte ( b'\x1b' ) ,
116- b'f' => EscapedChar :: Byte ( b'\x0c' ) ,
117- b'n' => EscapedChar :: Byte ( b'\n' ) ,
118- b'r' => EscapedChar :: Byte ( b'\r' ) ,
119- b't' => EscapedChar :: Byte ( b'\t' ) ,
120- b'v' => EscapedChar :: Byte ( b'\x0b' ) ,
115+ b'\\' => Ok ( EscapedChar :: Byte ( b'\\' ) ) ,
116+ b'"' => Ok ( EscapedChar :: Byte ( b'"' ) ) ,
117+ b'a' => Ok ( EscapedChar :: Byte ( b'\x07' ) ) ,
118+ b'b' => Ok ( EscapedChar :: Byte ( b'\x08' ) ) ,
119+ b'c' => Ok ( EscapedChar :: End ) ,
120+ b'e' => Ok ( EscapedChar :: Byte ( b'\x1b' ) ) ,
121+ b'f' => Ok ( EscapedChar :: Byte ( b'\x0c' ) ) ,
122+ b'n' => Ok ( EscapedChar :: Byte ( b'\n' ) ) ,
123+ b'r' => Ok ( EscapedChar :: Byte ( b'\r' ) ) ,
124+ b't' => Ok ( EscapedChar :: Byte ( b'\t' ) ) ,
125+ b'v' => Ok ( EscapedChar :: Byte ( b'\x0b' ) ) ,
121126 b'x' => {
122127 if let Some ( c) = parse_code ( rest, Base :: Hex ) {
123- EscapedChar :: Byte ( c)
128+ Ok ( EscapedChar :: Byte ( c) )
124129 } else {
125- EscapedChar :: Backslash ( b'x' )
130+ Err ( EscapeError { } )
126131 }
127132 }
128- b'0' => EscapedChar :: Byte ( parse_code ( rest, Base :: Oct ) . unwrap_or ( b'\0' ) ) ,
129- b'u' => EscapedChar :: Char ( parse_unicode ( rest, 4 ) . unwrap_or ( '\0' ) ) ,
130- b'U' => EscapedChar :: Char ( parse_unicode ( rest, 8 ) . unwrap_or ( '\0' ) ) ,
131- c => EscapedChar :: Backslash ( * c) ,
133+ b'0' => Ok ( EscapedChar :: Byte (
134+ parse_code ( rest, Base :: Oct ) . unwrap_or ( b'\0' ) ,
135+ ) ) ,
136+ b'u' => Ok ( EscapedChar :: Char ( parse_unicode ( rest, 4 ) . unwrap_or ( '\0' ) ) ) ,
137+ b'U' => Ok ( EscapedChar :: Char ( parse_unicode ( rest, 8 ) . unwrap_or ( '\0' ) ) ) ,
138+ c => Ok ( EscapedChar :: Backslash ( * c) ) ,
132139 }
133140 } else {
134- EscapedChar :: Byte ( b'\\' )
141+ Ok ( EscapedChar :: Byte ( b'\\' ) )
135142 }
136143}
0 commit comments