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

Use correct range to highlight line continuation error #12016

Merged
merged 1 commit into from
Jun 25, 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
23 changes: 14 additions & 9 deletions crates/ruff_python_parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,24 @@ impl<'src> Lexer<'src> {
std::mem::take(&mut self.current_value)
}

/// Helper function to push the given error, updating the current range with the error location
/// and return the [`TokenKind::Unknown`] token.
fn push_error(&mut self, error: LexicalError) -> TokenKind {
self.current_range = error.location();
self.errors.push(error);
TokenKind::Unknown
}

/// Lex the next token.
pub fn next_token(&mut self) -> TokenKind {
self.cursor.start_token();
self.current_value = TokenValue::None;
self.current_flags = TokenFlags::empty();
self.current_kind = self.lex_token();
self.current_range = self.token_range();
// For `Unknown` token, the `push_error` method updates the current range.
if !matches!(self.current_kind, TokenKind::Unknown) {
self.current_range = self.token_range();
}
Comment on lines +150 to +153
Copy link
Member

Choose a reason for hiding this comment

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

It's a bit unfortunate that we now have this branch in the very hot next_token function for the very rare case of an unknown token. But I don't see a better way of solving this that doesn't require a lot of repetition on the token range.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I agree. I thought of doing something like what you've suggested (computing the token range at the place where it's emitted) but wanted to see if this actually impacts the performance. It doesn't seem to be which is why I'm fine moving ahead with this.

self.current_kind
}

Expand Down Expand Up @@ -236,7 +247,7 @@ impl<'src> Lexer<'src> {
} else if !self.cursor.eat_char('\n') {
return Some(self.push_error(LexicalError::new(
LexicalErrorType::LineContinuationError,
self.token_range(),
TextRange::at(self.offset(), self.cursor.first().text_len()),
)));
}
indentation = Indentation::root();
Expand Down Expand Up @@ -328,7 +339,7 @@ impl<'src> Lexer<'src> {
} else if !self.cursor.eat_char('\n') {
return Err(LexicalError::new(
LexicalErrorType::LineContinuationError,
self.token_range(),
TextRange::at(self.offset(), self.cursor.first().text_len()),
));
}
}
Expand Down Expand Up @@ -1464,12 +1475,6 @@ impl<'src> Lexer<'src> {
self.token_range().start()
}

/// Helper function to push the given error and return the [`TokenKind::Unknown`] token.
fn push_error(&mut self, error: LexicalError) -> TokenKind {
self.errors.push(error);
TokenKind::Unknown
}

/// Creates a checkpoint to which the lexer can later return to using [`Self::rewind`].
pub(crate) fn checkpoint(&self) -> LexerCheckpoint {
LexerCheckpoint {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ Module(
body: [
Expr(
StmtExpr {
range: 0..13,
range: 0..14,
value: Call(
ExprCall {
range: 0..13,
range: 0..14,
func: Name(
ExprName {
range: 0..4,
Expand All @@ -23,7 +23,7 @@ Module(
},
),
arguments: Arguments {
range: 4..13,
range: 4..14,
args: [
Name(
ExprName {
Expand Down Expand Up @@ -82,15 +82,15 @@ Module(

|
1 | call(a, b, \\\
| ^^ Syntax Error: unexpected character after line continuation character
| ^ Syntax Error: unexpected character after line continuation character
2 |
3 | def bar():
|


|
1 | call(a, b, \\\
| ^ Syntax Error: unexpected character after line continuation character
| ^ Syntax Error: unexpected character after line continuation character
2 |
3 | def bar():
|
Expand Down
Loading