Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e532e68

Browse files
committed
Allow replacement fields in format specs
1 parent 234f750 commit e532e68

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

parser/src/fstring.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,19 @@ impl<'a> FStringParser<'a> {
100100
'{' if in_nested => return Err(ExpressionNestedTooDeeply),
101101
'}' if in_nested => {
102102
in_nested = false;
103-
spec_constructor.push(self.expr(ExprKind::FormattedValue {
104-
value:
105-
Box::new(
106-
parse_fstring_expr(&formatted_value_piece).map_err(
107-
|e| InvalidExpression(Box::new(e.error)),
108-
)?,
103+
spec_constructor.push(
104+
self.expr(ExprKind::FormattedValue {
105+
value: Box::new(
106+
FStringParser::new(
107+
&format!("{{{}}}", formatted_value_piece),
108+
Location::default(),
109+
)
110+
.parse()?,
109111
),
110-
conversion: None,
111-
format_spec: None,
112-
}));
112+
conversion: None,
113+
format_spec: None,
114+
}),
115+
);
113116
formatted_value_piece.clear();
114117
}
115118
_ if in_nested => {

parser/src/snapshots/rustpython_parser__fstring__tests__parse_fstring_nested_spec.snap

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,35 @@ Located {
6161
node: FormattedValue {
6262
value: Located {
6363
location: Location {
64-
row: 1,
65-
column: 2,
64+
row: 0,
65+
column: 0,
6666
},
6767
custom: (),
68-
node: Name {
69-
id: "spec",
70-
ctx: Load,
68+
node: JoinedStr {
69+
values: [
70+
Located {
71+
location: Location {
72+
row: 0,
73+
column: 0,
74+
},
75+
custom: (),
76+
node: FormattedValue {
77+
value: Located {
78+
location: Location {
79+
row: 1,
80+
column: 2,
81+
},
82+
custom: (),
83+
node: Name {
84+
id: "spec",
85+
ctx: Load,
86+
},
87+
},
88+
conversion: None,
89+
format_spec: None,
90+
},
91+
},
92+
],
7193
},
7294
},
7395
conversion: None,

0 commit comments

Comments
 (0)