diff --git a/crates/ruff_python_parser/src/error.rs b/crates/ruff_python_parser/src/error.rs index 0088d9bc8d9ad..143c50e86f725 100644 --- a/crates/ruff_python_parser/src/error.rs +++ b/crates/ruff_python_parser/src/error.rs @@ -404,7 +404,7 @@ impl std::fmt::Display for LexicalErrorType { write!(f, "Got unexpected token {tok}") } LexicalErrorType::LineContinuationError => { - write!(f, "unexpected character after line continuation character") + write!(f, "Expected a newline after line continuation character") } LexicalErrorType::Eof => write!(f, "unexpected EOF while parsing"), LexicalErrorType::OtherError(msg) => write!(f, "{msg}"), diff --git a/crates/ruff_python_parser/src/lexer.rs b/crates/ruff_python_parser/src/lexer.rs index 5a7fbc7bdb1e9..1aeafd3487923 100644 --- a/crates/ruff_python_parser/src/lexer.rs +++ b/crates/ruff_python_parser/src/lexer.rs @@ -247,7 +247,7 @@ impl<'src> Lexer<'src> { } else if !self.cursor.eat_char('\n') { return Some(self.push_error(LexicalError::new( LexicalErrorType::LineContinuationError, - TextRange::at(self.offset(), self.cursor.first().text_len()), + TextRange::at(self.offset() - '\\'.text_len(), '\\'.text_len()), ))); } indentation = Indentation::root(); @@ -339,7 +339,7 @@ impl<'src> Lexer<'src> { } else if !self.cursor.eat_char('\n') { return Err(LexicalError::new( LexicalErrorType::LineContinuationError, - TextRange::at(self.offset(), self.cursor.first().text_len()), + TextRange::at(self.offset() - '\\'.text_len(), '\\'.text_len()), )); } } diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__line_continuation_1.py.snap b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__line_continuation_1.py.snap index b544d9158d39c..55113bd113f12 100644 --- a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__line_continuation_1.py.snap +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__line_continuation_1.py.snap @@ -11,10 +11,10 @@ Module( body: [ Expr( StmtExpr { - range: 0..14, + range: 0..13, value: Call( ExprCall { - range: 0..14, + range: 0..13, func: Name( ExprName { range: 0..4, @@ -23,7 +23,7 @@ Module( }, ), arguments: Arguments { - range: 4..14, + range: 4..13, args: [ Name( ExprName { @@ -82,7 +82,7 @@ Module( | 1 | call(a, b, \\\ - | ^ Syntax Error: unexpected character after line continuation character + | ^ Syntax Error: Expected a newline after line continuation character 2 | 3 | def bar(): | @@ -90,7 +90,7 @@ Module( | 1 | call(a, b, \\\ - | ^ Syntax Error: unexpected character after line continuation character + | ^ Syntax Error: Expected a newline after line continuation character 2 | 3 | def bar(): |