Avoid creating unused JoinedStr in FStringParser

This commit is contained in:
Charles Marsh 2022-08-19 10:47:37 -04:00
parent 060d153bb3
commit fa2e69983f
14 changed files with 598 additions and 775 deletions

View file

@ -105,19 +105,13 @@ impl<'a> FStringParser<'a> {
nested -= 1; nested -= 1;
if nested == 0 { if nested == 0 {
formatted_value_piece.push(next); formatted_value_piece.push(next);
spec_constructor.push( spec_constructor.extend(
self.expr(ExprKind::FormattedValue { FStringParser::new(
value: Box::new( &format!("{{{}}}", formatted_value_piece),
FStringParser::new( Location::default(),
&formatted_value_piece, &self.recurse_lvl + 1,
Location::default(), )
&self.recurse_lvl + 1, .parse()?,
)
.parse()?,
),
conversion: ConversionFlag::None as _,
format_spec: None,
}),
); );
formatted_value_piece.clear(); formatted_value_piece.clear();
} else { } else {
@ -129,11 +123,13 @@ impl<'a> FStringParser<'a> {
} }
'{' => { '{' => {
nested += 1; nested += 1;
spec_constructor.push(self.expr(ExprKind::Constant { if !constant_piece.is_empty() {
value: constant_piece.to_owned().into(), spec_constructor.push(self.expr(ExprKind::Constant {
kind: None, value: constant_piece.to_owned().into(),
})); kind: None,
constant_piece.clear(); }));
constant_piece.clear();
}
formatted_value_piece.push(next); formatted_value_piece.push(next);
formatted_value_piece.push(' '); formatted_value_piece.push(' ');
} }
@ -144,11 +140,13 @@ impl<'a> FStringParser<'a> {
} }
self.chars.next(); self.chars.next();
} }
spec_constructor.push(self.expr(ExprKind::Constant { if !constant_piece.is_empty() {
value: constant_piece.to_owned().into(), spec_constructor.push(self.expr(ExprKind::Constant {
kind: None, value: constant_piece.to_owned().into(),
})); kind: None,
constant_piece.clear(); }));
constant_piece.clear();
}
if nested > 0 { if nested > 0 {
return Err(UnclosedLbrace); return Err(UnclosedLbrace);
} }
@ -241,7 +239,7 @@ impl<'a> FStringParser<'a> {
Err(UnclosedLbrace) Err(UnclosedLbrace)
} }
fn parse(mut self) -> Result<Expr, FStringErrorType> { fn parse(mut self) -> Result<Vec<Expr>, FStringErrorType> {
if self.recurse_lvl >= 2 { if self.recurse_lvl >= 2 {
return Err(ExpressionNestedTooDeeply); 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. /// 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. /// 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) FStringParser::new(source, location, 0)
.parse() .parse()
.map_err(|error| FStringError { error, location }) .map_err(|error| FStringError { error, location })
@ -308,7 +306,7 @@ pub fn parse_located_fstring(source: &str, location: Location) -> Result<Expr, F
mod tests { mod tests {
use super::*; 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() FStringParser::new(source, Location::default(), 0).parse()
} }

View file

@ -2,62 +2,53 @@
source: parser/src/fstring.rs source: parser/src/fstring.rs
expression: parse_ast expression: parse_ast
--- ---
Located { [
location: Location { Located {
row: 0, location: Location {
column: 0, row: 0,
column: 0,
},
custom: (),
node: Constant {
value: Str(
"user=",
),
kind: None,
},
}, },
custom: (), Located {
node: JoinedStr { location: Location {
values: [ row: 0,
Located { column: 0,
location: Location { },
row: 0, custom: (),
column: 0, node: Constant {
}, value: Str(
custom: (), "",
node: Constant { ),
value: Str( kind: None,
"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: FormattedValue {
value: Located {
location: Location {
row: 1,
column: 2,
},
custom: (),
node: Name {
id: "user",
ctx: Load,
},
},
conversion: 114,
format_spec: None,
},
},
]

View file

@ -2,136 +2,127 @@
source: parser/src/fstring.rs source: parser/src/fstring.rs
expression: parse_ast expression: parse_ast
--- ---
Located { [
location: Location { Located {
row: 0, location: Location {
column: 0, row: 0,
column: 0,
},
custom: (),
node: Constant {
value: Str(
"mix ",
),
kind: None,
},
}, },
custom: (), Located {
node: JoinedStr { location: Location {
values: [ row: 0,
Located { column: 0,
location: Location { },
row: 0, custom: (),
column: 0, node: Constant {
}, value: Str(
custom: (), "user=",
node: Constant { ),
value: Str( kind: None,
"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(
"",
),
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,
},
},
]

View file

@ -2,87 +2,78 @@
source: parser/src/fstring.rs source: parser/src/fstring.rs
expression: parse_ast expression: parse_ast
--- ---
Located { [
location: Location { Located {
row: 0, location: Location {
column: 0, row: 0,
column: 0,
},
custom: (),
node: Constant {
value: Str(
"user=",
),
kind: None,
},
}, },
custom: (), Located {
node: JoinedStr { location: Location {
values: [ row: 0,
Located { column: 0,
},
custom: (),
node: Constant {
value: Str(
"",
),
kind: None,
},
},
Located {
location: Location {
row: 0,
column: 0,
},
custom: (),
node: FormattedValue {
value: Located {
location: Location { location: Location {
row: 0, row: 1,
column: 0, column: 2,
}, },
custom: (), custom: (),
node: Constant { node: Name {
value: Str( id: "user",
"user=", ctx: Load,
),
kind: None,
}, },
}, },
Located { conversion: 0,
location: Location { format_spec: Some(
row: 0, Located {
column: 0, location: Location {
}, row: 0,
custom: (), column: 0,
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, custom: (),
format_spec: Some( node: JoinedStr {
Located { values: [
location: Location { Located {
row: 0, location: Location {
column: 0, 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,
},
},
],
},
},
),
}, },
}, ),
], },
}, },
} ]

View file

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

View file

@ -2,71 +2,62 @@
source: parser/src/fstring.rs source: parser/src/fstring.rs
expression: parse_ast expression: parse_ast
--- ---
Located { [
location: Location { Located {
row: 0, location: Location {
column: 0, 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: (), Located {
node: JoinedStr { location: Location {
values: [ row: 0,
Located { column: 0,
},
custom: (),
node: FormattedValue {
value: Located {
location: Location { location: Location {
row: 0, row: 1,
column: 0, column: 3,
}, },
custom: (), custom: (),
node: FormattedValue { node: Name {
value: Located { id: "b",
location: Location { ctx: Load,
row: 1,
column: 2,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
conversion: 0,
format_spec: None,
}, },
}, },
Located { conversion: 0,
location: Location { format_spec: None,
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,
},
},
],
}, },
} Located {
location: Location {
row: 0,
column: 0,
},
custom: (),
node: Constant {
value: Str(
"{foo}",
),
kind: None,
},
},
]

View file

@ -2,65 +2,56 @@
source: parser/src/fstring.rs source: parser/src/fstring.rs
expression: parse_ast expression: parse_ast
--- ---
Located { [
location: Location { Located {
row: 0, location: Location {
column: 0, row: 0,
}, column: 0,
custom: (), },
node: JoinedStr { custom: (),
values: [ node: FormattedValue {
Located { value: Located {
location: Location { location: Location {
row: 0, row: 1,
column: 0, column: 5,
}, },
custom: (), custom: (),
node: FormattedValue { node: Compare {
value: Located { left: Located {
location: Location { location: Location {
row: 1, row: 1,
column: 5, column: 2,
}, },
custom: (), custom: (),
node: Compare { node: Constant {
left: Located { value: Int(
location: Location { 42,
row: 1, ),
column: 2, kind: None,
},
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,
},
},
],
}, },
}, },
conversion: 0, ops: [
format_spec: None, Eq,
],
comparators: [
Located {
location: Location {
row: 1,
column: 8,
},
custom: (),
node: Constant {
value: Int(
42,
),
kind: None,
},
},
],
}, },
}, },
], conversion: 0,
format_spec: None,
},
}, },
} ]

View file

@ -2,118 +2,52 @@
source: parser/src/fstring.rs source: parser/src/fstring.rs
expression: parse_ast expression: parse_ast
--- ---
Located { [
location: Location { Located {
row: 0, location: Location {
column: 0, row: 0,
}, column: 0,
custom: (), },
node: JoinedStr { custom: (),
values: [ node: FormattedValue {
Located { value: Located {
location: Location { location: Location {
row: 0, row: 1,
column: 0, column: 2,
}, },
custom: (), custom: (),
node: FormattedValue { node: Name {
value: Located { id: "foo",
location: Location { ctx: Load,
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,
},
},
],
},
},
),
}, },
}, },
], 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,
},
},
],
},
},
),
},
}, },
} ]

View file

@ -2,65 +2,56 @@
source: parser/src/fstring.rs source: parser/src/fstring.rs
expression: parse_ast expression: parse_ast
--- ---
Located { [
location: Location { Located {
row: 0, location: Location {
column: 0, row: 0,
}, column: 0,
custom: (), },
node: JoinedStr { custom: (),
values: [ node: FormattedValue {
Located { value: Located {
location: Location { location: Location {
row: 0, row: 1,
column: 0, column: 4,
}, },
custom: (), custom: (),
node: FormattedValue { node: Compare {
value: Located { left: Located {
location: Location { location: Location {
row: 1, row: 1,
column: 4, column: 2,
}, },
custom: (), custom: (),
node: Compare { node: Constant {
left: Located { value: Int(
location: Location { 1,
row: 1, ),
column: 2, kind: None,
},
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,
},
},
],
}, },
}, },
conversion: 0, ops: [
format_spec: None, NotEq,
],
comparators: [
Located {
location: Location {
row: 1,
column: 7,
},
custom: (),
node: Constant {
value: Int(
2,
),
kind: None,
},
},
],
}, },
}, },
], conversion: 0,
format_spec: None,
},
}, },
} ]

View file

@ -2,61 +2,52 @@
source: parser/src/fstring.rs source: parser/src/fstring.rs
expression: parse_ast expression: parse_ast
--- ---
Located { [
location: Location { Located {
row: 0, location: Location {
column: 0, row: 0,
}, column: 0,
custom: (), },
node: JoinedStr { custom: (),
values: [ node: FormattedValue {
Located { value: Located {
location: Location { location: Location {
row: 0, row: 1,
column: 0, column: 2,
}, },
custom: (), custom: (),
node: FormattedValue { node: Name {
value: Located { id: "foo",
location: Location { ctx: Load,
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,
},
},
],
},
},
),
}, },
}, },
], 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,
},
},
],
},
},
),
},
}, },
} ]

View file

@ -2,62 +2,53 @@
source: parser/src/fstring.rs source: parser/src/fstring.rs
expression: parse_ast expression: parse_ast
--- ---
Located { [
location: Location { Located {
row: 0, location: Location {
column: 0, row: 0,
column: 0,
},
custom: (),
node: Constant {
value: Str(
"x =",
),
kind: None,
},
}, },
custom: (), Located {
node: JoinedStr { location: Location {
values: [ row: 0,
Located { column: 0,
location: Location { },
row: 0, custom: (),
column: 0, node: Constant {
}, value: Str(
custom: (), "",
node: Constant { ),
value: Str( kind: None,
"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: FormattedValue {
value: Located {
location: Location {
row: 1,
column: 2,
},
custom: (),
node: Name {
id: "x",
ctx: Load,
},
},
conversion: 114,
format_spec: None,
},
},
]

View file

@ -2,62 +2,53 @@
source: parser/src/fstring.rs source: parser/src/fstring.rs
expression: parse_ast expression: parse_ast
--- ---
Located { [
location: Location { Located {
row: 0, location: Location {
column: 0, row: 0,
column: 0,
},
custom: (),
node: Constant {
value: Str(
"x=",
),
kind: None,
},
}, },
custom: (), Located {
node: JoinedStr { location: Location {
values: [ row: 0,
Located { column: 0,
location: Location { },
row: 0, custom: (),
column: 0, node: Constant {
}, value: Str(
custom: (), " ",
node: Constant { ),
value: Str( kind: None,
"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: FormattedValue {
value: Located {
location: Location {
row: 1,
column: 2,
},
custom: (),
node: Name {
id: "x",
ctx: Load,
},
},
conversion: 114,
format_spec: None,
},
},
]

View file

@ -2,35 +2,26 @@
source: parser/src/fstring.rs source: parser/src/fstring.rs
expression: parse_ast expression: parse_ast
--- ---
Located { [
location: Location { Located {
row: 0, location: Location {
column: 0, row: 0,
}, column: 0,
custom: (), },
node: JoinedStr { custom: (),
values: [ node: FormattedValue {
Located { value: Located {
location: Location { location: Location {
row: 0, row: 1,
column: 0, column: 2,
}, },
custom: (), custom: (),
node: FormattedValue { node: Yield {
value: Located { value: None,
location: Location {
row: 1,
column: 2,
},
custom: (),
node: Yield {
value: None,
},
},
conversion: 0,
format_spec: None,
}, },
}, },
], conversion: 0,
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::Normal | StringKind::U => current.push(string),
StringKind::F => { StringKind::F => {
has_fstring = true; has_fstring = true;
let values = if let ExprKind::JoinedStr { values } = for value in parse_located_fstring(&string, location).map_err(|e| LexicalError {
parse_located_fstring(&string, location) location,
.map_err(|e| LexicalError { error: LexicalErrorType::FStringError(e.error),
location, })? {
error: LexicalErrorType::FStringError(e.error),
})?
.node
{
values
} else {
unreachable!("parse_located_fstring returned a non-JoinedStr.")
};
for value in values {
match value.node { match value.node {
ExprKind::FormattedValue { .. } => { ExprKind::FormattedValue { .. } => {
if !current.is_empty() { if !current.is_empty() {