Avoid creating unused JoinedStr in FStringParser

This commit is contained in:
Charles Marsh 2022-08-19 10:47:37 -04:00
parent 53c48bf6b9
commit 2345bc895d
14 changed files with 598 additions and 775 deletions

View file

@ -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(
spec_constructor.extend(
FStringParser::new(
&formatted_value_piece,
&format!("{{{}}}", formatted_value_piece),
Location::default(),
&self.recurse_lvl + 1,
)
.parse()?,
),
conversion: ConversionFlag::None as _,
format_spec: None,
}),
);
formatted_value_piece.clear();
} else {
@ -129,11 +123,13 @@ impl<'a> FStringParser<'a> {
}
'{' => {
nested += 1;
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();
}
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<Expr, FStringErrorType> {
fn parse(mut self) -> Result<Vec<Expr>, 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<Expr, ParseError> {
/// 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<Expr, FStringError> {
pub fn parse_located_fstring(source: &str, location: Location) -> Result<Vec<Expr>, 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<Expr, F
mod tests {
use super::*;
fn parse_fstring(source: &str) -> Result<Expr, FStringErrorType> {
fn parse_fstring(source: &str) -> Result<Vec<Expr>, FStringErrorType> {
FStringParser::new(source, Location::default(), 0).parse()
}

View file

@ -2,14 +2,7 @@
source: parser/src/fstring.rs
expression: parse_ast
---
Located {
location: Location {
row: 0,
column: 0,
},
custom: (),
node: JoinedStr {
values: [
[
Located {
location: Location {
row: 0,
@ -58,6 +51,4 @@ Located {
format_spec: None,
},
},
],
},
}
]

View file

@ -2,14 +2,7 @@
source: parser/src/fstring.rs
expression: parse_ast
---
Located {
location: Location {
row: 0,
column: 0,
},
custom: (),
node: JoinedStr {
values: [
[
Located {
location: Location {
row: 0,
@ -132,6 +125,4 @@ Located {
format_spec: None,
},
},
],
},
}
]

View file

@ -2,14 +2,7 @@
source: parser/src/fstring.rs
expression: parse_ast
---
Located {
location: Location {
row: 0,
column: 0,
},
custom: (),
node: JoinedStr {
values: [
[
Located {
location: Location {
row: 0,
@ -83,6 +76,4 @@ Located {
),
},
},
],
},
}
]

View file

@ -1,15 +1,5 @@
---
source: parser/src/fstring.rs
expression: "parse_fstring(\"\").unwrap()"
---
Located {
location: Location {
row: 0,
column: 0,
},
custom: (),
node: JoinedStr {
values: [],
},
}
[]

View file

@ -2,14 +2,7 @@
source: parser/src/fstring.rs
expression: parse_ast
---
Located {
location: Location {
row: 0,
column: 0,
},
custom: (),
node: JoinedStr {
values: [
[
Located {
location: Location {
row: 0,
@ -67,6 +60,4 @@ Located {
kind: None,
},
},
],
},
}
]

View file

@ -2,14 +2,7 @@
source: parser/src/fstring.rs
expression: parse_ast
---
Located {
location: Location {
row: 0,
column: 0,
},
custom: (),
node: JoinedStr {
values: [
[
Located {
location: Location {
row: 0,
@ -61,6 +54,4 @@ Located {
format_spec: None,
},
},
],
},
}
]

View file

@ -2,14 +2,7 @@
source: parser/src/fstring.rs
expression: parse_ast
---
Located {
location: Location {
row: 0,
column: 0,
},
custom: (),
node: JoinedStr {
values: [
[
Located {
location: Location {
row: 0,
@ -46,64 +39,7 @@ Located {
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(
"",
"{ spec}",
),
kind: None,
},
@ -114,6 +50,4 @@ Located {
),
},
},
],
},
}
]

View file

@ -2,14 +2,7 @@
source: parser/src/fstring.rs
expression: parse_ast
---
Located {
location: Location {
row: 0,
column: 0,
},
custom: (),
node: JoinedStr {
values: [
[
Located {
location: Location {
row: 0,
@ -61,6 +54,4 @@ Located {
format_spec: None,
},
},
],
},
}
]

View file

@ -2,14 +2,7 @@
source: parser/src/fstring.rs
expression: parse_ast
---
Located {
location: Location {
row: 0,
column: 0,
},
custom: (),
node: JoinedStr {
values: [
[
Located {
location: Location {
row: 0,
@ -57,6 +50,4 @@ Located {
),
},
},
],
},
}
]

View file

@ -2,14 +2,7 @@
source: parser/src/fstring.rs
expression: parse_ast
---
Located {
location: Location {
row: 0,
column: 0,
},
custom: (),
node: JoinedStr {
values: [
[
Located {
location: Location {
row: 0,
@ -58,6 +51,4 @@ Located {
format_spec: None,
},
},
],
},
}
]

View file

@ -2,14 +2,7 @@
source: parser/src/fstring.rs
expression: parse_ast
---
Located {
location: Location {
row: 0,
column: 0,
},
custom: (),
node: JoinedStr {
values: [
[
Located {
location: Location {
row: 0,
@ -58,6 +51,4 @@ Located {
format_spec: None,
},
},
],
},
}
]

View file

@ -2,14 +2,7 @@
source: parser/src/fstring.rs
expression: parse_ast
---
Located {
location: Location {
row: 0,
column: 0,
},
custom: (),
node: JoinedStr {
values: [
[
Located {
location: Location {
row: 0,
@ -31,6 +24,4 @@ Located {
format_spec: None,
},
},
],
},
}
]

View file

@ -34,19 +34,10 @@ pub fn parse_strings(values: Vec<(Location, (String, StringKind))>) -> Result<Ex
StringKind::Normal | StringKind::U => current.push(string),
StringKind::F => {
has_fstring = true;
let values = if let ExprKind::JoinedStr { values } =
parse_located_fstring(&string, location)
.map_err(|e| LexicalError {
for value in 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 {
})? {
match value.node {
ExprKind::FormattedValue { .. } => {
if !current.is_empty() {