Skip to content
This repository was archived by the owner on Jul 27, 2023. It is now read-only.

fix the ranges of constants inside f-strings #33

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 31 additions & 8 deletions parser/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
token::{StringKind, Tok},
};
use itertools::Itertools;
use rustpython_ast::Ranged;
use rustpython_parser_core::{
text_size::{TextLen, TextSize},
ConversionFlag,
Expand Down Expand Up @@ -452,6 +453,7 @@ impl<'a> StringParser<'a> {
}

let mut content = String::new();
let mut content_start = self.location;
let mut values = vec![];

while let Some(&ch) = self.peek() {
Expand All @@ -477,7 +479,10 @@ impl<'a> StringParser<'a> {
ast::ExprConstant {
value: content.drain(..).collect::<String>().into(),
kind: None,
range: self.range(),
range: TextRange::new(
std::mem::replace(&mut content_start, self.location),
self.location - TextSize::from(1),
),
}
.into(),
),
Expand All @@ -486,6 +491,7 @@ impl<'a> StringParser<'a> {

let parsed_values = self.parse_formatted_value(nested)?;
values.extend(parsed_values);
content_start = self.location;
}
'}' => {
if nested > 0 {
Expand Down Expand Up @@ -516,7 +522,7 @@ impl<'a> StringParser<'a> {
ast::ExprConstant {
value: content.into(),
kind: None,
range: self.range(),
range: TextRange::new(content_start, self.location),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The code so far consistently uses self.get_pos() over location

Suggested change
range: TextRange::new(content_start, self.location),
range: TextRange::new(content_start, self.get_pos()),

}
.into(),
),
Expand Down Expand Up @@ -674,34 +680,43 @@ pub(crate) fn parse_strings(
// De-duplicate adjacent constants.
let mut deduped: Vec<Expr> = vec![];
let mut current: Vec<String> = vec![];
let mut current_start = initial_start;
let mut current_end = last_end;

let take_current = |current: &mut Vec<String>| -> Expr {
let take_current = |current: &mut Vec<String>, start, end| -> Expr {
Expr::Constant(ast::ExprConstant {
value: Constant::Str(current.drain(..).join("")),
kind: initial_kind.clone(),
range: TextRange::new(initial_start, last_end),
range: TextRange::new(start, end),
})
};

for (start, (source, kind, triple_quoted), end) in values {
for value in parse_string(&source, kind, triple_quoted, start, end)? {
let value_range = value.range();
match value {
Expr::FormattedValue { .. } => {
if !current.is_empty() {
deduped.push(take_current(&mut current));
deduped.push(take_current(&mut current, current_start, current_end));
}
deduped.push(value)
}
Expr::Constant(ast::ExprConstant {
value: Constant::Str(value),
value: Constant::Str(inner),
..
}) => current.push(value),
}) => {
if current.is_empty() {
current_start = value_range.start();
}
current_end = value_range.end();
current.push(inner);
}
_ => unreachable!("Unexpected non-string expression."),
}
}
}
if !current.is_empty() {
deduped.push(take_current(&mut current));
deduped.push(take_current(&mut current, current_start, current_end));
}

Ok(Expr::JoinedStr(ast::ExprJoinedStr {
Expand Down Expand Up @@ -1069,6 +1084,14 @@ mod tests {
insta::assert_debug_snapshot!(parse_ast);
}

#[test]
fn test_fstring_constant_range() {
let source = r#"f"aaa{bbb}ccc{ddd}eee""#;
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
// assert!(false);
insta::assert_debug_snapshot!(parse_ast);
}

#[test]
fn test_fstring_unescaped_newline() {
let source = r#"f"""
Expand Down