diff --git a/parser/src/fstring.rs b/parser/src/fstring.rs index 2869306..0d5e260 100644 --- a/parser/src/fstring.rs +++ b/parser/src/fstring.rs @@ -105,19 +105,13 @@ impl<'a> FStringParser<'a> { nested -= 1; if nested == 0 { formatted_value_piece.push(next); - spec_constructor.push( - self.expr(ExprKind::FormattedValue { - value: Box::new( - FStringParser::new( - &formatted_value_piece, - Location::default(), - &self.recurse_lvl + 1, - ) - .parse()?, - ), - conversion: ConversionFlag::None as _, - format_spec: None, - }), + spec_constructor.extend( + FStringParser::new( + &format!("{{{}}}", formatted_value_piece), + Location::default(), + &self.recurse_lvl + 1, + ) + .parse()?, ); formatted_value_piece.clear(); } else { @@ -129,11 +123,13 @@ impl<'a> FStringParser<'a> { } '{' => { nested += 1; - spec_constructor.push(self.expr(ExprKind::Constant { - value: constant_piece.to_owned().into(), - kind: None, - })); - constant_piece.clear(); + if !constant_piece.is_empty() { + spec_constructor.push(self.expr(ExprKind::Constant { + value: constant_piece.to_owned().into(), + kind: None, + })); + constant_piece.clear(); + } formatted_value_piece.push(next); formatted_value_piece.push(' '); } @@ -144,11 +140,13 @@ impl<'a> FStringParser<'a> { } self.chars.next(); } - spec_constructor.push(self.expr(ExprKind::Constant { - value: constant_piece.to_owned().into(), - kind: None, - })); - constant_piece.clear(); + if !constant_piece.is_empty() { + spec_constructor.push(self.expr(ExprKind::Constant { + value: constant_piece.to_owned().into(), + kind: None, + })); + constant_piece.clear(); + } if nested > 0 { return Err(UnclosedLbrace); } @@ -241,7 +239,7 @@ impl<'a> FStringParser<'a> { Err(UnclosedLbrace) } - fn parse(mut self) -> Result { + fn parse(mut self) -> Result, FStringErrorType> { if self.recurse_lvl >= 2 { return Err(ExpressionNestedTooDeeply); } @@ -287,7 +285,7 @@ impl<'a> FStringParser<'a> { })) } - Ok(self.expr(ExprKind::JoinedStr { values })) + Ok(values) } } @@ -298,7 +296,7 @@ fn parse_fstring_expr(source: &str) -> Result { /// Parse an fstring from a string, located at a certain position in the sourcecode. /// In case of errors, we will get the location and the error returned. -pub fn parse_located_fstring(source: &str, location: Location) -> Result { +pub fn parse_located_fstring(source: &str, location: Location) -> Result, FStringError> { FStringParser::new(source, location, 0) .parse() .map_err(|error| FStringError { error, location }) @@ -308,7 +306,7 @@ pub fn parse_located_fstring(source: &str, location: Location) -> Result Result { + fn parse_fstring(source: &str) -> Result, FStringErrorType> { FStringParser::new(source, Location::default(), 0).parse() } diff --git a/parser/src/snapshots/rustpython_parser__fstring__tests__fstring_parse_selfdocumenting_base.snap b/parser/src/snapshots/rustpython_parser__fstring__tests__fstring_parse_selfdocumenting_base.snap index b2322b9..ac99159 100644 --- a/parser/src/snapshots/rustpython_parser__fstring__tests__fstring_parse_selfdocumenting_base.snap +++ b/parser/src/snapshots/rustpython_parser__fstring__tests__fstring_parse_selfdocumenting_base.snap @@ -2,62 +2,53 @@ source: parser/src/fstring.rs expression: parse_ast --- -Located { - location: Location { - row: 0, - column: 0, +[ + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + "user=", + ), + kind: None, + }, }, - custom: (), - node: JoinedStr { - values: [ - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - "user=", - ), - kind: None, - }, - }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - "", - ), - kind: None, - }, - }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: FormattedValue { - value: Located { - location: Location { - row: 1, - column: 2, - }, - custom: (), - node: Name { - id: "user", - ctx: Load, - }, - }, - conversion: 114, - format_spec: None, - }, - }, - ], + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + "", + ), + kind: None, + }, }, -} + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: FormattedValue { + value: Located { + location: Location { + row: 1, + column: 2, + }, + custom: (), + node: Name { + id: "user", + ctx: Load, + }, + }, + conversion: 114, + format_spec: None, + }, + }, +] diff --git a/parser/src/snapshots/rustpython_parser__fstring__tests__fstring_parse_selfdocumenting_base_more.snap b/parser/src/snapshots/rustpython_parser__fstring__tests__fstring_parse_selfdocumenting_base_more.snap index 32d6581..6ebbcca 100644 --- a/parser/src/snapshots/rustpython_parser__fstring__tests__fstring_parse_selfdocumenting_base_more.snap +++ b/parser/src/snapshots/rustpython_parser__fstring__tests__fstring_parse_selfdocumenting_base_more.snap @@ -2,136 +2,127 @@ source: parser/src/fstring.rs expression: parse_ast --- -Located { - location: Location { - row: 0, - column: 0, +[ + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + "mix ", + ), + kind: None, + }, }, - custom: (), - node: JoinedStr { - values: [ - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - "mix ", - ), - kind: None, - }, - }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - "user=", - ), - kind: None, - }, - }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - "", - ), - kind: None, - }, - }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: FormattedValue { - value: Located { - location: Location { - row: 1, - column: 2, - }, - custom: (), - node: Name { - id: "user", - ctx: Load, - }, - }, - conversion: 114, - format_spec: None, - }, - }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - " with text and ", - ), - kind: None, - }, - }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - "second=", - ), - kind: None, - }, - }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - "", - ), - kind: None, - }, - }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: FormattedValue { - value: Located { - location: Location { - row: 1, - column: 2, - }, - custom: (), - node: Name { - id: "second", - ctx: Load, - }, - }, - conversion: 114, - format_spec: None, - }, - }, - ], + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + "user=", + ), + kind: None, + }, }, -} + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + "", + ), + kind: None, + }, + }, + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: FormattedValue { + value: Located { + location: Location { + row: 1, + column: 2, + }, + custom: (), + node: Name { + id: "user", + ctx: Load, + }, + }, + conversion: 114, + format_spec: None, + }, + }, + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + " with text and ", + ), + kind: None, + }, + }, + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + "second=", + ), + kind: None, + }, + }, + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + "", + ), + kind: None, + }, + }, + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: FormattedValue { + value: Located { + location: Location { + row: 1, + column: 2, + }, + custom: (), + node: Name { + id: "second", + ctx: Load, + }, + }, + conversion: 114, + format_spec: None, + }, + }, +] diff --git a/parser/src/snapshots/rustpython_parser__fstring__tests__fstring_parse_selfdocumenting_format.snap b/parser/src/snapshots/rustpython_parser__fstring__tests__fstring_parse_selfdocumenting_format.snap index 4ab56f0..71b86cb 100644 --- a/parser/src/snapshots/rustpython_parser__fstring__tests__fstring_parse_selfdocumenting_format.snap +++ b/parser/src/snapshots/rustpython_parser__fstring__tests__fstring_parse_selfdocumenting_format.snap @@ -2,87 +2,78 @@ source: parser/src/fstring.rs expression: parse_ast --- -Located { - location: Location { - row: 0, - column: 0, +[ + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + "user=", + ), + kind: None, + }, }, - custom: (), - node: JoinedStr { - values: [ - Located { + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + "", + ), + kind: None, + }, + }, + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: FormattedValue { + value: Located { location: Location { - row: 0, - column: 0, + row: 1, + column: 2, }, custom: (), - node: Constant { - value: Str( - "user=", - ), - kind: None, + node: Name { + id: "user", + ctx: Load, }, }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - "", - ), - kind: None, - }, - }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: FormattedValue { - value: Located { - location: Location { - row: 1, - column: 2, - }, - custom: (), - node: Name { - id: "user", - ctx: Load, - }, + conversion: 0, + format_spec: Some( + Located { + location: Location { + row: 0, + column: 0, }, - conversion: 0, - format_spec: Some( - Located { - location: Location { - row: 0, - column: 0, + custom: (), + node: JoinedStr { + values: [ + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + ">10", + ), + kind: None, + }, }, - custom: (), - node: JoinedStr { - values: [ - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - ">10", - ), - kind: None, - }, - }, - ], - }, - }, - ), + ], + }, }, - }, - ], + ), + }, }, -} +] diff --git a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_empty_fstring.snap b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_empty_fstring.snap index 0da814c..4156710 100644 --- a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_empty_fstring.snap +++ b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_empty_fstring.snap @@ -1,15 +1,5 @@ --- source: parser/src/fstring.rs expression: "parse_fstring(\"\").unwrap()" - --- -Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: JoinedStr { - values: [], - }, -} +[] diff --git a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring.snap b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring.snap index 26af1d8..3a05c32 100644 --- a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring.snap +++ b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring.snap @@ -2,71 +2,62 @@ source: parser/src/fstring.rs expression: parse_ast --- -Located { - location: Location { - row: 0, - column: 0, +[ + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: FormattedValue { + value: Located { + location: Location { + row: 1, + column: 2, + }, + custom: (), + node: Name { + id: "a", + ctx: Load, + }, + }, + conversion: 0, + format_spec: None, + }, }, - custom: (), - node: JoinedStr { - values: [ - Located { + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: FormattedValue { + value: Located { location: Location { - row: 0, - column: 0, + row: 1, + column: 3, }, custom: (), - node: FormattedValue { - value: Located { - location: Location { - row: 1, - column: 2, - }, - custom: (), - node: Name { - id: "a", - ctx: Load, - }, - }, - conversion: 0, - format_spec: None, + node: Name { + id: "b", + ctx: Load, }, }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: FormattedValue { - value: Located { - location: Location { - row: 1, - column: 3, - }, - custom: (), - node: Name { - id: "b", - ctx: Load, - }, - }, - conversion: 0, - format_spec: None, - }, - }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - "{foo}", - ), - kind: None, - }, - }, - ], + conversion: 0, + format_spec: None, + }, }, -} + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + "{foo}", + ), + kind: None, + }, + }, +] diff --git a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_equals.snap b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_equals.snap index 5a98ebd..c7a6ddb 100644 --- a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_equals.snap +++ b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_equals.snap @@ -2,65 +2,56 @@ source: parser/src/fstring.rs expression: parse_ast --- -Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: JoinedStr { - values: [ - Located { +[ + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: FormattedValue { + value: Located { location: Location { - row: 0, - column: 0, + row: 1, + column: 5, }, custom: (), - node: FormattedValue { - value: Located { + node: Compare { + left: Located { location: Location { row: 1, - column: 5, + column: 2, }, custom: (), - node: Compare { - left: Located { - location: Location { - row: 1, - column: 2, - }, - custom: (), - node: Constant { - value: Int( - 42, - ), - kind: None, - }, - }, - ops: [ - Eq, - ], - comparators: [ - Located { - location: Location { - row: 1, - column: 8, - }, - custom: (), - node: Constant { - value: Int( - 42, - ), - kind: None, - }, - }, - ], + node: Constant { + value: Int( + 42, + ), + kind: None, }, }, - conversion: 0, - format_spec: None, + ops: [ + Eq, + ], + comparators: [ + Located { + location: Location { + row: 1, + column: 8, + }, + custom: (), + node: Constant { + value: Int( + 42, + ), + kind: None, + }, + }, + ], }, }, - ], + conversion: 0, + format_spec: None, + }, }, -} +] 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 3bf74c0..e71cb7c 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 @@ -2,118 +2,52 @@ source: parser/src/fstring.rs expression: parse_ast --- -Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: JoinedStr { - values: [ - Located { +[ + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: FormattedValue { + value: Located { location: Location { - row: 0, - column: 0, + row: 1, + column: 2, }, custom: (), - node: FormattedValue { - value: Located { - location: Location { - row: 1, - column: 2, - }, - custom: (), - node: Name { - id: "foo", - ctx: Load, - }, - }, - conversion: 0, - format_spec: Some( - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: JoinedStr { - values: [ - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - "", - ), - kind: None, - }, - }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: FormattedValue { - value: Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: JoinedStr { - values: [ - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: FormattedValue { - value: Located { - location: Location { - row: 1, - column: 3, - }, - custom: (), - node: Name { - id: "spec", - ctx: Load, - }, - }, - conversion: 0, - format_spec: None, - }, - }, - ], - }, - }, - conversion: 0, - format_spec: None, - }, - }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - "", - ), - kind: None, - }, - }, - ], - }, - }, - ), + node: Name { + id: "foo", + ctx: Load, }, }, - ], + conversion: 0, + format_spec: Some( + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: JoinedStr { + values: [ + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + "{ spec}", + ), + kind: None, + }, + }, + ], + }, + }, + ), + }, }, -} +] diff --git a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_not_equals.snap b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_not_equals.snap index 655bf1b..d3ca5d3 100644 --- a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_not_equals.snap +++ b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_not_equals.snap @@ -2,65 +2,56 @@ source: parser/src/fstring.rs expression: parse_ast --- -Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: JoinedStr { - values: [ - Located { +[ + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: FormattedValue { + value: Located { location: Location { - row: 0, - column: 0, + row: 1, + column: 4, }, custom: (), - node: FormattedValue { - value: Located { + node: Compare { + left: Located { location: Location { row: 1, - column: 4, + column: 2, }, custom: (), - node: Compare { - left: Located { - location: Location { - row: 1, - column: 2, - }, - custom: (), - node: Constant { - value: Int( - 1, - ), - kind: None, - }, - }, - ops: [ - NotEq, - ], - comparators: [ - Located { - location: Location { - row: 1, - column: 7, - }, - custom: (), - node: Constant { - value: Int( - 2, - ), - kind: None, - }, - }, - ], + node: Constant { + value: Int( + 1, + ), + kind: None, }, }, - conversion: 0, - format_spec: None, + ops: [ + NotEq, + ], + comparators: [ + Located { + location: Location { + row: 1, + column: 7, + }, + custom: (), + node: Constant { + value: Int( + 2, + ), + kind: None, + }, + }, + ], }, }, - ], + conversion: 0, + format_spec: None, + }, }, -} +] diff --git a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_not_nested_spec.snap b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_not_nested_spec.snap index 8950ebb..a5760e2 100644 --- a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_not_nested_spec.snap +++ b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_not_nested_spec.snap @@ -2,61 +2,52 @@ source: parser/src/fstring.rs expression: parse_ast --- -Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: JoinedStr { - values: [ - Located { +[ + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: FormattedValue { + value: Located { location: Location { - row: 0, - column: 0, + row: 1, + column: 2, }, custom: (), - node: FormattedValue { - value: Located { - location: Location { - row: 1, - column: 2, - }, - custom: (), - node: Name { - id: "foo", - ctx: Load, - }, - }, - conversion: 0, - format_spec: Some( - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: JoinedStr { - values: [ - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - "spec", - ), - kind: None, - }, - }, - ], - }, - }, - ), + node: Name { + id: "foo", + ctx: Load, }, }, - ], + conversion: 0, + format_spec: Some( + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: JoinedStr { + values: [ + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + "spec", + ), + kind: None, + }, + }, + ], + }, + }, + ), + }, }, -} +] diff --git a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_selfdoc_prec_space.snap b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_selfdoc_prec_space.snap index 33f62ee..0e92a12 100644 --- a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_selfdoc_prec_space.snap +++ b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_selfdoc_prec_space.snap @@ -2,62 +2,53 @@ source: parser/src/fstring.rs expression: parse_ast --- -Located { - location: Location { - row: 0, - column: 0, +[ + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + "x =", + ), + kind: None, + }, }, - custom: (), - node: JoinedStr { - values: [ - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - "x =", - ), - kind: None, - }, - }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - "", - ), - kind: None, - }, - }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: FormattedValue { - value: Located { - location: Location { - row: 1, - column: 2, - }, - custom: (), - node: Name { - id: "x", - ctx: Load, - }, - }, - conversion: 114, - format_spec: None, - }, - }, - ], + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + "", + ), + kind: None, + }, }, -} + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: FormattedValue { + value: Located { + location: Location { + row: 1, + column: 2, + }, + custom: (), + node: Name { + id: "x", + ctx: Load, + }, + }, + conversion: 114, + format_spec: None, + }, + }, +] diff --git a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_selfdoc_trailing_space.snap b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_selfdoc_trailing_space.snap index deb7b02..22bb47f 100644 --- a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_selfdoc_trailing_space.snap +++ b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_selfdoc_trailing_space.snap @@ -2,62 +2,53 @@ source: parser/src/fstring.rs expression: parse_ast --- -Located { - location: Location { - row: 0, - column: 0, +[ + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + "x=", + ), + kind: None, + }, }, - custom: (), - node: JoinedStr { - values: [ - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - "x=", - ), - kind: None, - }, - }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: Constant { - value: Str( - " ", - ), - kind: None, - }, - }, - Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: FormattedValue { - value: Located { - location: Location { - row: 1, - column: 2, - }, - custom: (), - node: Name { - id: "x", - ctx: Load, - }, - }, - conversion: 114, - format_spec: None, - }, - }, - ], + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: Constant { + value: Str( + " ", + ), + kind: None, + }, }, -} + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: FormattedValue { + value: Located { + location: Location { + row: 1, + column: 2, + }, + custom: (), + node: Name { + id: "x", + ctx: Load, + }, + }, + conversion: 114, + format_spec: None, + }, + }, +] diff --git a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_yield_expr.snap b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_yield_expr.snap index 37e0d05..a963739 100644 --- a/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_yield_expr.snap +++ b/parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_yield_expr.snap @@ -2,35 +2,26 @@ source: parser/src/fstring.rs expression: parse_ast --- -Located { - location: Location { - row: 0, - column: 0, - }, - custom: (), - node: JoinedStr { - values: [ - Located { +[ + Located { + location: Location { + row: 0, + column: 0, + }, + custom: (), + node: FormattedValue { + value: Located { location: Location { - row: 0, - column: 0, + row: 1, + column: 2, }, custom: (), - node: FormattedValue { - value: Located { - location: Location { - row: 1, - column: 2, - }, - custom: (), - node: Yield { - value: None, - }, - }, - conversion: 0, - format_spec: None, + node: Yield { + value: None, }, }, - ], + conversion: 0, + format_spec: None, + }, }, -} +] diff --git a/parser/src/string.rs b/parser/src/string.rs index 02a5514..ac4d222 100644 --- a/parser/src/string.rs +++ b/parser/src/string.rs @@ -34,19 +34,10 @@ pub fn parse_strings(values: Vec<(Location, (String, StringKind))>) -> Result current.push(string), StringKind::F => { has_fstring = true; - let values = if let ExprKind::JoinedStr { values } = - parse_located_fstring(&string, location) - .map_err(|e| LexicalError { - location, - error: LexicalErrorType::FStringError(e.error), - })? - .node - { - values - } else { - unreachable!("parse_located_fstring returned a non-JoinedStr.") - }; - for value in values { + for value in parse_located_fstring(&string, location).map_err(|e| LexicalError { + location, + error: LexicalErrorType::FStringError(e.error), + })? { match value.node { ExprKind::FormattedValue { .. } => { if !current.is_empty() {