Do not include newline for unterminated string range (#12017)

## Summary

This PR updates the unterminated string error range to not include the
final newline character.

This is a follow-up to #12016 and required for #12019

This is not done for when the unterminated string goes till the end of
file (not a newline character). The unterminated f-string range is
correct.

### Why is this required for #12019 ?

Because otherwise the token ranges will overlap. For example:
```py
f"{"
f"{foo!r"
```

Here, the re-lexing logic recovers from an unterminated f-string and
thus emitting a `Newline` token for the one at the end of the first
line. But, currently the `Unknown` and the `Newline` token would overlap
because the `Unknown` token (unterminated string literal) range would
include the newline character.

## Test Plan

Update and validate the snapshot.
This commit is contained in:
Dhruv Manilawala 2024-06-25 13:40:07 +05:30 committed by GitHub
parent 9c1b6ec411
commit d930e97212
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 53 additions and 65 deletions

View file

@ -973,10 +973,10 @@ impl<'src> Lexer<'src> {
} }
match ch { match ch {
Some('\r' | '\n') => { Some(newline @ ('\r' | '\n')) => {
return self.push_error(LexicalError::new( return self.push_error(LexicalError::new(
LexicalErrorType::UnclosedStringError, LexicalErrorType::UnclosedStringError,
self.token_range(), self.token_range().sub_end(newline.text_len()),
)); ));
} }
Some(ch) if ch == quote => { Some(ch) if ch == quote => {

View file

@ -11,15 +11,15 @@ Module(
body: [ body: [
Expr( Expr(
StmtExpr { StmtExpr {
range: 0..5, range: 0..4,
value: FString( value: FString(
ExprFString { ExprFString {
range: 0..5, range: 0..4,
value: FStringValue { value: FStringValue {
inner: Single( inner: Single(
FString( FString(
FString { FString {
range: 0..5, range: 0..4,
elements: [ elements: [
Expression( Expression(
FStringExpressionElement { FStringExpressionElement {
@ -52,19 +52,19 @@ Module(
), ),
Expr( Expr(
StmtExpr { StmtExpr {
range: 5..15, range: 5..14,
value: FString( value: FString(
ExprFString { ExprFString {
range: 5..15, range: 5..14,
value: FStringValue { value: FStringValue {
inner: Single( inner: Single(
FString( FString(
FString { FString {
range: 5..15, range: 5..14,
elements: [ elements: [
Expression( Expression(
FStringExpressionElement { FStringExpressionElement {
range: 7..15, range: 7..14,
expression: Name( expression: Name(
ExprName { ExprName {
range: 8..11, range: 8..11,
@ -93,15 +93,15 @@ Module(
), ),
Expr( Expr(
StmtExpr { StmtExpr {
range: 15..24, range: 15..23,
value: FString( value: FString(
ExprFString { ExprFString {
range: 15..24, range: 15..23,
value: FStringValue { value: FStringValue {
inner: Single( inner: Single(
FString( FString(
FString { FString {
range: 15..24, range: 15..23,
elements: [ elements: [
Expression( Expression(
FStringExpressionElement { FStringExpressionElement {
@ -148,7 +148,7 @@ Module(
[ [
FString( FString(
FString { FString {
range: 24..29, range: 24..28,
elements: [ elements: [
Expression( Expression(
FStringExpressionElement { FStringExpressionElement {
@ -213,19 +213,9 @@ Module(
``` ```
## Errors ## Errors
|
1 | f"{"
| ____^
2 | | f"{foo!r"
| |_^ Syntax Error: missing closing quote in string literal
3 | f"{foo="
4 | f"{"
|
| |
1 | f"{" 1 | f"{"
| Syntax Error: f-string: unterminated string | ^ Syntax Error: missing closing quote in string literal
2 | f"{foo!r" 2 | f"{foo!r"
3 | f"{foo=" 3 | f"{foo="
| |
@ -240,13 +230,19 @@ Module(
| |
1 | f"{" 1 | f"{"
2 | f"{foo!r" | Syntax Error: f-string: unterminated string
| ________^ 2 | f"{foo!r"
3 | | f"{foo=" 3 | f"{foo="
| |_^ Syntax Error: missing closing quote in string literal |
4 | f"{"
5 | f"""{"""
|
1 | f"{"
2 | f"{foo!r"
| ^^ Syntax Error: missing closing quote in string literal
3 | f"{foo="
4 | f"{"
| |
@ -288,13 +284,12 @@ Module(
| |
1 | f"{" 1 | f"{"
2 | f"{foo!r" 2 | f"{foo!r"
3 | f"{foo=" 3 | f"{foo="
| ________^ | ^ Syntax Error: missing closing quote in string literal
4 | | f"{" 4 | f"{"
| |_^ Syntax Error: missing closing quote in string literal 5 | f"""{"""
5 | f"""{"""
| |
@ -319,12 +314,11 @@ Module(
| |
2 | f"{foo!r" 2 | f"{foo!r"
3 | f"{foo=" 3 | f"{foo="
4 | f"{" 4 | f"{"
| ____^ | ^ Syntax Error: missing closing quote in string literal
5 | | f"""{""" 5 | f"""{"""
| |_^ Syntax Error: missing closing quote in string literal
| |

View file

@ -153,12 +153,10 @@ Module(
## Errors ## Errors
| |
1 | 'hello' 'world 1 | 'hello' 'world
| _________^ | ^^^^^^ Syntax Error: missing closing quote in string literal
2 | | 1 + 1 2 | 1 + 1
| |_^ Syntax Error: missing closing quote in string literal 3 | 'hello' f'world {x}
3 | 'hello' f'world {x}
4 | 2 + 2
| |

View file

@ -216,14 +216,12 @@ Module(
| |
6 | ( 6 | (
7 | 'first' 7 | 'first'
8 | 'second 8 | 'second
| _____^ | ^^^^^^^ Syntax Error: missing closing quote in string literal
9 | | f'third' 9 | f'third'
| |_^ Syntax Error: missing closing quote in string literal 10 | )
10 | )
11 | 2 + 2
| |

View file

@ -324,13 +324,11 @@ Module(
| |
5 | f'middle {'string':\ 5 | f'middle {'string':\
6 | 'format spec'} 6 | 'format spec'}
| _____________________^ | ^^ Syntax Error: missing closing quote in string literal
7 | | 7 |
| |_^ Syntax Error: missing closing quote in string literal 8 | f'middle {'string':\\
8 | f'middle {'string':\\
9 | 'format spec'}
| |