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

Method parse_interpolated_string_middle

crates/ruff_python_parser/src/string.rs:275–369  ·  view source on GitHub ↗
(
        mut self,
    )

Source from the content-addressed store, hash-verified

273 }
274
275 fn parse_interpolated_string_middle(
276 mut self,
277 ) -> Result<ast::InterpolatedStringLiteralElement, LexicalError> {
278 // Fast-path: if the f-string or t-string doesn't contain any escape sequences, return the literal.
279 let Some(mut index) = memchr::memchr3(b'{', b'}', b'\\', self.source.as_bytes()) else {
280 return Ok(ast::InterpolatedStringLiteralElement {
281 value: self.source.into(),
282 range: self.range,
283 node_index: AtomicNodeIndex::NONE,
284 });
285 };
286
287 let mut value = String::with_capacity(self.source.len());
288 loop {
289 // Add the characters before the escape sequence (or curly brace) to the string.
290 let before_with_slash_or_brace = self.skip_bytes(index + 1);
291 let before = &before_with_slash_or_brace[..before_with_slash_or_brace.len() - 1];
292 value.push_str(before);
293
294 // Add the escaped character to the string.
295 match self.source.as_bytes()[self.cursor - 1] {
296 // If there are any curly braces inside a `F/TStringMiddle` token,
297 // then they were escaped (i.e. `{{` or `}}`). This means that
298 // the raw source contains a doubled brace, but the literal value only
299 // contains one brace.
300 brace @ (b'{' | b'}') => {
301 if self.peek_byte() == Some(brace) {
302 self.next_byte();
303 }
304 value.push(char::from(brace));
305 }
306 // We can encounter a `\` as the last character in a `F/TStringMiddle`
307 // token which is valid in this context. For example,
308 //
309 // ```python
310 // f"\{foo} \{bar:\}"
311 // # ^ ^^ ^
312 // ```
313 //
314 // Here, the `F/TStringMiddle` token content will be "\" and " \"
315 // which is invalid if we look at the content in isolation:
316 //
317 // ```python
318 // "\"
319 // ```
320 //
321 // However, the content is syntactically valid in the context of
322 // the f/t-string because it's a substring of the entire f/t-string.
323 // This is still an invalid escape sequence, but we don't want to
324 // raise a syntax error as is done by the CPython parser. It might
325 // be supported in the future, refer to point 3: https://peps.python.org/pep-0701/#rejected-ideas
326 b'\\' => {
327 if !self.flags.is_raw_string() && self.peek_byte().is_some() {
328 if let Some(brace @ (b'{' | b'}')) = self.peek_byte()
329 && self.source.as_bytes().get(self.cursor + 1).copied() == Some(brace)
330 {
331 // Leave the doubled brace for the next iteration to collapse.
332 value.push('\\');

Calls 11

OkClass · 0.85
push_strMethod · 0.80
peek_byteMethod · 0.80
next_byteMethod · 0.80
parse_escaped_charMethod · 0.80
as_bytesMethod · 0.45
lenMethod · 0.45
skip_bytesMethod · 0.45
pushMethod · 0.45
is_raw_stringMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected