Skip to content

Commit 2345bc8

Browse files
author
Charles Marsh
committed
Avoid creating unused JoinedStr in FStringParser
1 parent 53c48bf commit 2345bc8

14 files changed

Lines changed: 567 additions & 744 deletions

‎parser/src/fstring.rs‎

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,13 @@ impl<'a> FStringParser<'a> {
105105
nested -= 1;
106106
if nested == 0 {
107107
formatted_value_piece.push(next);
108-
spec_constructor.push(
109-
self.expr(ExprKind::FormattedValue {
110-
value: Box::new(
111-
FStringParser::new(
112-
&formatted_value_piece,
113-
Location::default(),
114-
&self.recurse_lvl + 1,
115-
)
116-
.parse()?,
117-
),
118-
conversion: ConversionFlag::None as _,
119-
format_spec: None,
120-
}),
108+
spec_constructor.extend(
109+
FStringParser::new(
110+
&format!("{{{}}}", formatted_value_piece),
111+
Location::default(),
112+
&self.recurse_lvl + 1,
113+
)
114+
.parse()?,
121115
);
122116
formatted_value_piece.clear();
123117
} else {
@@ -129,11 +123,13 @@ impl<'a> FStringParser<'a> {
129123
}
130124
'{' => {
131125
nested += 1;
132-
spec_constructor.push(self.expr(ExprKind::Constant {
133-
value: constant_piece.to_owned().into(),
134-
kind: None,
135-
}));
136-
constant_piece.clear();
126+
if !constant_piece.is_empty() {
127+
spec_constructor.push(self.expr(ExprKind::Constant {
128+
value: constant_piece.to_owned().into(),
129+
kind: None,
130+
}));
131+
constant_piece.clear();
132+
}
137133
formatted_value_piece.push(next);
138134
formatted_value_piece.push(' ');
139135
}
@@ -144,11 +140,13 @@ impl<'a> FStringParser<'a> {
144140
}
145141
self.chars.next();
146142
}
147-
spec_constructor.push(self.expr(ExprKind::Constant {
148-
value: constant_piece.to_owned().into(),
149-
kind: None,
150-
}));
151-
constant_piece.clear();
143+
if !constant_piece.is_empty() {
144+
spec_constructor.push(self.expr(ExprKind::Constant {
145+
value: constant_piece.to_owned().into(),
146+
kind: None,
147+
}));
148+
constant_piece.clear();
149+
}
152150
if nested > 0 {
153151
return Err(UnclosedLbrace);
154152
}
@@ -241,7 +239,7 @@ impl<'a> FStringParser<'a> {
241239
Err(UnclosedLbrace)
242240
}
243241

244-
fn parse(mut self) -> Result<Expr, FStringErrorType> {
242+
fn parse(mut self) -> Result<Vec<Expr>, FStringErrorType> {
245243
if self.recurse_lvl >= 2 {
246244
return Err(ExpressionNestedTooDeeply);
247245
}
@@ -287,7 +285,7 @@ impl<'a> FStringParser<'a> {
287285
}))
288286
}
289287

290-
Ok(self.expr(ExprKind::JoinedStr { values }))
288+
Ok(values)
291289
}
292290
}
293291

@@ -298,7 +296,7 @@ fn parse_fstring_expr(source: &str) -> Result<Expr, ParseError> {
298296

299297
/// Parse an fstring from a string, located at a certain position in the sourcecode.
300298
/// In case of errors, we will get the location and the error returned.
301-
pub fn parse_located_fstring(source: &str, location: Location) -> Result<Expr, FStringError> {
299+
pub fn parse_located_fstring(source: &str, location: Location) -> Result<Vec<Expr>, FStringError> {
302300
FStringParser::new(source, location, 0)
303301
.parse()
304302
.map_err(|error| FStringError { error, location })
@@ -308,7 +306,7 @@ pub fn parse_located_fstring(source: &str, location: Location) -> Result<Expr, F
308306
mod tests {
309307
use super::*;
310308

311-
fn parse_fstring(source: &str) -> Result<Expr, FStringErrorType> {
309+
fn parse_fstring(source: &str) -> Result<Vec<Expr>, FStringErrorType> {
312310
FStringParser::new(source, Location::default(), 0).parse()
313311
}
314312

‎parser/src/snapshots/rustpython_parser__fstring__tests__fstring_parse_selfdocumenting_base.snap‎

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,53 @@
22
source: parser/src/fstring.rs
33
expression: parse_ast
44
---
5-
Located {
6-
location: Location {
7-
row: 0,
8-
column: 0,
5+
[
6+
Located {
7+
location: Location {
8+
row: 0,
9+
column: 0,
10+
},
11+
custom: (),
12+
node: Constant {
13+
value: Str(
14+
"user=",
15+
),
16+
kind: None,
17+
},
918
},
10-
custom: (),
11-
node: JoinedStr {
12-
values: [
13-
Located {
14-
location: Location {
15-
row: 0,
16-
column: 0,
17-
},
18-
custom: (),
19-
node: Constant {
20-
value: Str(
21-
"user=",
22-
),
23-
kind: None,
24-
},
25-
},
26-
Located {
27-
location: Location {
28-
row: 0,
29-
column: 0,
30-
},
31-
custom: (),
32-
node: Constant {
33-
value: Str(
34-
"",
35-
),
36-
kind: None,
37-
},
38-
},
39-
Located {
19+
Located {
20+
location: Location {
21+
row: 0,
22+
column: 0,
23+
},
24+
custom: (),
25+
node: Constant {
26+
value: Str(
27+
"",
28+
),
29+
kind: None,
30+
},
31+
},
32+
Located {
33+
location: Location {
34+
row: 0,
35+
column: 0,
36+
},
37+
custom: (),
38+
node: FormattedValue {
39+
value: Located {
4040
location: Location {
41-
row: 0,
42-
column: 0,
41+
row: 1,
42+
column: 2,
4343
},
4444
custom: (),
45-
node: FormattedValue {
46-
value: Located {
47-
location: Location {
48-
row: 1,
49-
column: 2,
50-
},
51-
custom: (),
52-
node: Name {
53-
id: "user",
54-
ctx: Load,
55-
},
56-
},
57-
conversion: 114,
58-
format_spec: None,
45+
node: Name {
46+
id: "user",
47+
ctx: Load,
5948
},
6049
},
61-
],
50+
conversion: 114,
51+
format_spec: None,
52+
},
6253
},
63-
}
54+
]

0 commit comments

Comments
 (0)