Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions extra_tests/snippets/syntax_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
assert fr'x={4*10}\n' == 'x=40\\n'
assert f'{16:0>+#10x}' == '00000+0x10'
assert f"{{{(lambda x: f'hello, {x}')('world}')}" == '{hello, world}'
assert f"""{f'''{f"{f'{foo}'}"}'''}""" == 'bar'
assert f"""{1:{1}{f'n'}}""" == '1'
assert f'{ {1}.pop()}' == '1'
assert f"""{1:{{1}.pop()}{f'n'}}""" == '1'


# base test of self documenting strings
Expand Down
25 changes: 13 additions & 12 deletions parser/src/fstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,25 @@ 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 {
value: Box::new(
FStringParser::new(
&format!("{{{}}}", formatted_value_piece),
&formatted_value_piece,
Location::default(),
&self.recurse_lvl + 1,
)
Expand All @@ -121,20 +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,
_ => {
Expand All @@ -148,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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Located {
value: Located {
location: Location {
row: 1,
column: 2,
column: 3,
},
custom: (),
node: Name {
Expand Down