Parse an escaped character, returning the new character.
(&mut self)
| 118 | |
| 119 | /// Parse an escaped character, returning the new character. |
| 120 | fn parse_escaped_char(&mut self) -> Result<Option<EscapedChar>, LexicalError> { |
| 121 | let Some(first_char) = self.next_char() else { |
| 122 | unreachable!() |
| 123 | }; |
| 124 | |
| 125 | let new_char = match first_char { |
| 126 | '\\' => '\\'.into(), |
| 127 | '\'' => '\''.into(), |
| 128 | '\"' => '"'.into(), |
| 129 | 'a' => '\x07'.into(), |
| 130 | 'b' => '\x08'.into(), |
| 131 | 'f' => '\x0c'.into(), |
| 132 | 'n' => '\n'.into(), |
| 133 | 'r' => '\r'.into(), |
| 134 | 't' => '\t'.into(), |
| 135 | 'v' => '\x0b'.into(), |
| 136 | o @ '0'..='7' => self.parse_octet(o as u8).into(), |
| 137 | 'x' => self.parse_unicode_literal(2)?, |
| 138 | 'u' if !self.flags.is_byte_string() => self.parse_unicode_literal(4)?, |
| 139 | 'U' if !self.flags.is_byte_string() => self.parse_unicode_literal(8)?, |
| 140 | 'N' if !self.flags.is_byte_string() => self.parse_unicode_name()?.into(), |
| 141 | // Special cases where the escape sequence is not a single character |
| 142 | '\n' => return Ok(None), |
| 143 | '\r' => { |
| 144 | if self.peek_byte() == Some(b'\n') { |
| 145 | self.next_byte(); |
| 146 | } |
| 147 | |
| 148 | return Ok(None); |
| 149 | } |
| 150 | _ => return Ok(Some(EscapedChar::Escape(first_char))), |
| 151 | }; |
| 152 | |
| 153 | Ok(Some(EscapedChar::Literal(new_char))) |
| 154 | } |
| 155 | |
| 156 | fn parse_fstring_middle(mut self) -> Result<Box<Wtf8>, LexicalError> { |
| 157 | // Fast-path: if the f-string doesn't contain any escape sequences, return the literal. |
no test coverage detected