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

Avoid consuming newline for unterminated string #12067

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 12 additions & 7 deletions crates/ruff_python_parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -962,25 +962,30 @@ impl<'src> Lexer<'src> {

// Skip up to the current character.
self.cursor.skip_bytes(index);
let ch = self.cursor.bump();

// Lookahead because we want to bump only if it's a quote or being escaped.
let quote_or_newline = self.cursor.first();

// If the character is escaped, continue scanning.
if num_backslashes % 2 == 1 {
if ch == Some('\r') {
self.cursor.bump();
if quote_or_newline == '\r' {
self.cursor.eat_char('\n');
}
continue;
}

match ch {
Some(newline @ ('\r' | '\n')) => {
match quote_or_newline {
'\r' | '\n' => {
return self.push_error(LexicalError::new(
LexicalErrorType::UnclosedStringError,
self.token_range().sub_end(newline.text_len()),
self.token_range(),
));
}
Some(ch) if ch == quote => {
break self.offset() - TextSize::new(1);
ch if ch == quote => {
let value_end = self.offset();
self.cursor.bump();
break value_end;
}
_ => unreachable!("memchr2 returned an index that is not a quote or a newline"),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ Module(
|


|
1 | 'hello' 'world
| ^ Syntax Error: Expected a statement
2 | 1 + 1
3 | 'hello' f'world {x}
4 | 2 + 2
|


Comment on lines +163 to +171
Copy link
Member Author

Choose a reason for hiding this comment

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

The reason for this "Expected a statement" error is because the current token is Newline which doesn't start a module statement nor ends a list of module statements.

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 think this might get fixed if we start emitting String tokens instead of Unknown token because currently the parser thinks that the statement ended after 'hello' but then it encountered an Unknown token (for unterminated string) and a Newline token both of which doesn't start or end a statement.

|
1 | 'hello' 'world
2 | 1 + 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ Module(
|
5 | f'middle {'string':\
6 | 'format spec'}
| ^ Syntax Error: Expected a statement
7 |
| ^ Syntax Error: Expected a statement
8 | f'middle {'string':\\
9 | 'format spec'}
|
Expand Down
Loading