MCPcopy Create free account
hub / github.com/astral-sh/ruff / parse_escaped_char

Method parse_escaped_char

crates/ruff_python_parser/src/string.rs:240–278  ·  view source on GitHub ↗

Parse an escaped character, returning the new character.

(&mut self)

Source from the content-addressed store, hash-verified

238
239 /// Parse an escaped character, returning the new character.
240 fn parse_escaped_char(&mut self) -> Result<Option<EscapedChar>, LexicalError> {
241 let Some(first_char) = self.next_char() else {
242 // TODO: check when this error case happens
243 return Err(LexicalError::new(
244 LexicalErrorType::StringError,
245 TextRange::empty(self.position()),
246 ));
247 };
248
249 let new_char = match first_char {
250 '\\' => '\\',
251 '\'' => '\'',
252 '\"' => '"',
253 'a' => '\x07',
254 'b' => '\x08',
255 'f' => '\x0c',
256 'n' => '\n',
257 'r' => '\r',
258 't' => '\t',
259 'v' => '\x0b',
260 o @ '0'..='7' => self.parse_octet(o as u8),
261 'x' => self.parse_unicode_literal(2)?,
262 'u' if !self.flags.is_byte_string() => self.parse_unicode_literal(4)?,
263 'U' if !self.flags.is_byte_string() => self.parse_unicode_literal(8)?,
264 'N' if !self.flags.is_byte_string() => self.parse_unicode_name()?,
265 // Special cases where the escape sequence is not a single character
266 '\n' => return Ok(None),
267 '\r' => {
268 if self.peek_byte() == Some(b'\n') {
269 self.next_byte();
270 }
271
272 return Ok(None);
273 }
274 _ => return Ok(Some(EscapedChar::Escape(first_char))),
275 };
276
277 Ok(Some(EscapedChar::Literal(new_char)))
278 }
279
280 fn parse_interpolated_string_middle(
281 mut self,

Callers 3

parse_bytesMethod · 0.80
parse_stringMethod · 0.80

Calls 12

OkClass · 0.85
EscapeInterface · 0.85
LiteralEnum · 0.85
next_charMethod · 0.80
parse_octetMethod · 0.80
parse_unicode_literalMethod · 0.80
is_byte_stringMethod · 0.80
parse_unicode_nameMethod · 0.80
peek_byteMethod · 0.80
next_byteMethod · 0.80
emptyFunction · 0.50
positionMethod · 0.45

Tested by

no test coverage detected