Raise syntax error when \ is at end of file (#17409)

## Summary

This PR fixes a bug in the lexer specifically around line continuation
character at end of file.

The reason this was occurring is because the lexer wouldn't check for
EOL _after_ consuming the escaped newline but only if the EOL was right
after the line continuation character.

fixes: #17398 

## Test Plan

Add tests for the scenarios where this should occur mainly (a) when the
state is `AfterNewline` and (b) when the state is `Other`.
This commit is contained in:
Dhruv Manilawala 2025-04-15 21:26:12 +05:30 committed by GitHub
parent 942cb9e3ad
commit bfc17fecaa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 223 additions and 7 deletions

View file

@ -0,0 +1,22 @@
---
source: crates/ruff_python_parser/src/lexer.rs
expression: line_continuation_at_eof_after_newline(MAC_EOL)
---
## Tokens
```
[
(
Unknown,
0..2,
),
]
```
## Errors
```
[
LexicalError {
error: Eof,
location: 0..2,
},
]
```

View file

@ -0,0 +1,22 @@
---
source: crates/ruff_python_parser/src/lexer.rs
expression: line_continuation_at_eof_after_newline(UNIX_EOL)
---
## Tokens
```
[
(
Unknown,
0..2,
),
]
```
## Errors
```
[
LexicalError {
error: Eof,
location: 0..2,
},
]
```

View file

@ -0,0 +1,22 @@
---
source: crates/ruff_python_parser/src/lexer.rs
expression: line_continuation_at_eof_after_newline(WINDOWS_EOL)
---
## Tokens
```
[
(
Unknown,
0..3,
),
]
```
## Errors
```
[
LexicalError {
error: Eof,
location: 0..3,
},
]
```

View file

@ -0,0 +1,36 @@
---
source: crates/ruff_python_parser/src/lexer.rs
expression: line_continuation_at_eof(MAC_EOL)
---
## Tokens
```
[
(
Int(
1,
),
0..1,
),
(
Comma,
1..2,
),
(
Unknown,
2..5,
),
(
Newline,
5..5,
),
]
```
## Errors
```
[
LexicalError {
error: Eof,
location: 2..5,
},
]
```

View file

@ -0,0 +1,36 @@
---
source: crates/ruff_python_parser/src/lexer.rs
expression: line_continuation_at_eof(UNIX_EOL)
---
## Tokens
```
[
(
Int(
1,
),
0..1,
),
(
Comma,
1..2,
),
(
Unknown,
2..5,
),
(
Newline,
5..5,
),
]
```
## Errors
```
[
LexicalError {
error: Eof,
location: 2..5,
},
]
```

View file

@ -0,0 +1,36 @@
---
source: crates/ruff_python_parser/src/lexer.rs
expression: line_continuation_at_eof(WINDOWS_EOL)
---
## Tokens
```
[
(
Int(
1,
),
0..1,
),
(
Comma,
1..2,
),
(
Unknown,
2..6,
),
(
Newline,
6..6,
),
]
```
## Errors
```
[
LexicalError {
error: Eof,
location: 2..6,
},
]
```