Fix nested-fstring unbalanced brace pairs handling

This commit is contained in:
Jeong YunWon
2022-08-19 05:48:38 +09:00
parent ca11fb4476
commit 598be6e5ea
2 changed files with 11 additions and 12 deletions

View File

@@ -91,20 +91,19 @@ impl<'a> FStringParser<'a> {
}
':' if delims.is_empty() => {
let mut in_nested = false;
let mut nested = 0;
let mut spec_constructor = Vec::new();
let mut constant_piece = String::new();
let mut formatted_value_piece = String::new();
let mut spec_delims = Vec::new();
while let Some(&next) = self.chars.peek() {
match next {
'{' if in_nested => {
spec_delims.push(next);
'{' if nested > 0 => {
nested += 1;
formatted_value_piece.push(next);
}
'}' if in_nested => {
if spec_delims.is_empty() {
in_nested = false;
'}' if nested > 0 => {
nested -= 1;
if nested == 0 {
formatted_value_piece.push(next);
spec_constructor.push(
self.expr(ExprKind::FormattedValue {
@@ -122,21 +121,21 @@ impl<'a> FStringParser<'a> {
);
formatted_value_piece.clear();
} else {
spec_delims.pop();
formatted_value_piece.push(next);
}
}
_ if in_nested => {
_ if nested > 0 => {
formatted_value_piece.push(next);
}
'{' => {
in_nested = true;
nested += 1;
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(' ');
}
'}' => break,
_ => {
@@ -150,7 +149,7 @@ impl<'a> FStringParser<'a> {
kind: None,
}));
constant_piece.clear();
if in_nested {
if nested > 0 {
return Err(UnclosedLbrace);
}
spec = Some(Box::new(self.expr(ExprKind::JoinedStr {

View File

@@ -76,7 +76,7 @@ Located {
value: Located {
location: Location {
row: 1,
column: 2,
column: 3,
},
custom: (),
node: Name {