Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider line continuation character for re-lexing #12008

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The newline character is being escaped which means that the lexer shouldn't be moved
# back to that position.
# https://github.com/astral-sh/ruff/issues/12004

f'middle {'string':\
'format spec'}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Similar to `./fstring_format_spec_1.py` except that the continuation character itself
# is escaped. So, the lexer should be moved back.

f'hello {x:\\
'world'}'
22 changes: 19 additions & 3 deletions crates/ruff_python_parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1373,15 +1373,31 @@ impl<'src> Lexer<'src> {
}

let mut current_position = self.current_range().start();
let reverse_chars = self.source[..current_position.to_usize()].chars().rev();
let mut reverse_chars = self.source[..current_position.to_usize()]
.chars()
.rev()
.peekable();
let mut newline_position = None;

for ch in reverse_chars {
while let Some(ch) = reverse_chars.next() {
if is_python_whitespace(ch) {
current_position -= ch.text_len();
} else if matches!(ch, '\n' | '\r') {
current_position -= ch.text_len();
newline_position = Some(current_position);
if let Some(first_slash) = reverse_chars.next_if_eq(&'\\') {
if let Some(second_slash) = reverse_chars.next_if_eq(&'\\') {
// Line continuation character has been escaped: `\\\n`
newline_position = Some(current_position);
// Set the newline position before updating the current position.
current_position -= first_slash.text_len() - second_slash.text_len();
} else {
Copy link
Member

Choose a reason for hiding this comment

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

I think it's still more complicate than this. What about \\\ Here, we have an escaped backslash followed by a continuation :(

Copy link
Member Author

Choose a reason for hiding this comment

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

I see. I guess we'd need to count the number of backslashes and make a decision based on whether it's odd or even.

// Newline has been escaped: `\\n`
current_position -= first_slash.text_len();
}
} else {
// No escapes: `\n`
newline_position = Some(current_position);
}
} else {
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/invalid/re_lexing/fstring_format_spec_1.py
---
## AST

```
Module(
ModModule {
range: 0..205,
body: [
Expr(
StmtExpr {
range: 162..192,
value: FString(
ExprFString {
range: 162..192,
value: FStringValue {
inner: Single(
FString(
FString {
range: 162..192,
elements: [
Literal(
FStringLiteralElement {
range: 164..171,
value: "middle ",
},
),
Expression(
FStringExpressionElement {
range: 171..191,
expression: StringLiteral(
ExprStringLiteral {
range: 172..180,
value: StringLiteralValue {
inner: Single(
StringLiteral {
range: 172..180,
value: "string",
flags: StringLiteralFlags {
quote_style: Single,
prefix: Empty,
triple_quoted: false,
},
},
),
},
},
),
debug_text: None,
conversion: None,
format_spec: Some(
FStringFormatSpec {
range: 181..191,
elements: [
Literal(
FStringLiteralElement {
range: 181..191,
value: " ",
},
),
],
},
),
},
),
],
flags: FStringFlags {
quote_style: Single,
prefix: Regular,
triple_quoted: false,
},
},
),
),
},
},
),
},
),
Expr(
StmtExpr {
range: 192..198,
value: Name(
ExprName {
range: 192..198,
id: "format",
ctx: Load,
},
),
},
),
Expr(
StmtExpr {
range: 199..203,
value: Name(
ExprName {
range: 199..203,
id: "spec",
ctx: Load,
},
),
},
),
],
},
)
```
## Errors

|
5 | f'middle {'string':\
6 | 'format spec'}
| ^ Syntax Error: f-string: expecting '}'
|


|
5 | f'middle {'string':\
6 | 'format spec'}
| ^^^^^^ Syntax Error: Simple statements must be separated by newlines or semicolons
|


|
5 | f'middle {'string':\
6 | 'format spec'}
| ^^^^ Syntax Error: Simple statements must be separated by newlines or semicolons
|


|
5 | f'middle {'string':\
6 | 'format spec'}
| ^^ Syntax Error: Got unexpected string
|


|
5 | f'middle {'string':\
6 | 'format spec'}
| Syntax Error: Expected a statement
|
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/invalid/re_lexing/fstring_format_spec_2.py
---
## AST

```
Module(
ModModule {
range: 0..166,
body: [
Expr(
StmtExpr {
range: 139..152,
value: FString(
ExprFString {
range: 139..152,
value: FStringValue {
inner: Single(
FString(
FString {
range: 139..152,
elements: [
Literal(
FStringLiteralElement {
range: 141..147,
value: "hello ",
},
),
Expression(
FStringExpressionElement {
range: 147..152,
expression: Name(
ExprName {
range: 148..149,
id: "x",
ctx: Load,
},
),
debug_text: None,
conversion: None,
format_spec: Some(
FStringFormatSpec {
range: 150..152,
elements: [
Literal(
FStringLiteralElement {
range: 150..152,
value: "\\",
},
),
],
},
),
},
),
],
flags: FStringFlags {
quote_style: Single,
prefix: Regular,
triple_quoted: false,
},
},
),
),
},
},
),
},
),
Expr(
StmtExpr {
range: 157..164,
value: StringLiteral(
ExprStringLiteral {
range: 157..164,
value: StringLiteralValue {
inner: Single(
StringLiteral {
range: 157..164,
value: "world",
flags: StringLiteralFlags {
quote_style: Single,
prefix: Empty,
triple_quoted: false,
},
},
),
},
},
),
},
),
],
},
)
```
## Errors

|
2 | # is escaped. So, the lexer should be moved back.
3 |
4 | f'hello {x:\\
| Syntax Error: f-string: unterminated string
5 | 'world'}'
|


|
4 | f'hello {x:\\
5 | 'world'}'
| ^^^^ Syntax Error: Unexpected indentation
|


|
4 | f'hello {x:\\
5 | 'world'}'
| ^ Syntax Error: Expected a statement
|


|
4 | f'hello {x:\\
5 | 'world'}'
| ^ Syntax Error: Got unexpected string
|


|
4 | f'hello {x:\\
5 | 'world'}'
| Syntax Error: Expected a statement
|
Loading