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

Method parse_escaped_char

crates/ruff_python_parser/src/string.rs:235–273  ·  view source on GitHub ↗

Parse an escaped character, returning the new character.

(&mut self)

Source from the content-addressed store, hash-verified

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

Callers 3

parse_bytesMethod · 0.80
parse_stringMethod · 0.80

Calls 11

OkClass · 0.85
EscapeInterface · 0.85
LiteralEnum · 0.85
next_charMethod · 0.80
parse_octetMethod · 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