From a1dffe671e4199f2604d47fefe0516ef317098ef Mon Sep 17 00:00:00 2001 From: Padraic Fanning Date: Tue, 1 Feb 2022 20:20:11 -0500 Subject: [PATCH] Allow replacement fields in format specs --- parser/src/fstring.rs | 21 ++++++------ ...ing__tests__parse_fstring_nested_spec.snap | 32 ++++++++++++++++--- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/parser/src/fstring.rs b/parser/src/fstring.rs index 85428505d..ec861e377 100644 --- a/parser/src/fstring.rs +++ b/parser/src/fstring.rs @@ -100,16 +100,19 @@ impl<'a> FStringParser<'a> { '{' if in_nested => return Err(ExpressionNestedTooDeeply), '}' if in_nested => { in_nested = false; - spec_constructor.push(self.expr(ExprKind::FormattedValue { - value: - Box::new( - parse_fstring_expr(&formatted_value_piece).map_err( - |e| InvalidExpression(Box::new(e.error)), - )?, + spec_constructor.push( + self.expr(ExprKind::FormattedValue { + value: Box::new( + FStringParser::new( + &format!("{{{}}}", formatted_value_piece), + Location::default(), + ) + .parse()?, ), - conversion: None, - format_spec: None, - })); + conversion: None, + format_spec: None, + }), + ); formatted_value_piece.clear(); } _ if in_nested => { diff --git a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_nested_spec.snap b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_nested_spec.snap index 3656ef8b0..6bcd55732 100644 --- a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_nested_spec.snap +++ b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_nested_spec.snap @@ -61,13 +61,35 @@ Located { node: FormattedValue { value: Located { location: Location { - row: 1, - column: 2, + row: 0, + column: 0, }, custom: (), - node: Name { - id: "spec", - ctx: Load, + node: JoinedStr { + values: [ + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: FormattedValue { + value: Located { + location: Location { + row: 1, + column: 2, + }, + custom: (), + node: Name { + id: "spec", + ctx: Load, + }, + }, + conversion: None, + format_spec: None, + }, + }, + ], }, }, conversion: None,