-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
crates/ruff_python_parser/resources/invalid/re_lexing/fstring_format_spec_1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'} |
5 changes: 5 additions & 0 deletions
5
crates/ruff_python_parser/resources/invalid/re_lexing/fstring_format_spec_2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
...uff_python_parser/tests/snapshots/invalid_syntax@re_lexing__fstring_format_spec_1.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
| |
134 changes: 134 additions & 0 deletions
134
...uff_python_parser/tests/snapshots/invalid_syntax@re_lexing__fstring_format_spec_2.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
| |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 :(There was a problem hiding this comment.
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.