Skip to content

Commit

Permalink
Return UnclosedLbrace error for correct condition
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Sep 2, 2023
1 parent 2de7565 commit 15887c9
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions crates/ruff_python_parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,20 +671,6 @@ impl<'source> Lexer<'source> {
// string; consume those two characters and ensure that we require a triple-quote to close
let triple_quoted = self.cursor.eat_char2(quote, quote);

if let Some(fstring_context) = self.fstring_stack.last() {
// When we are in an f-string, check whether does the initial quote
// matches with f-strings quotes and if it is, then this must be a
// missing '}' token so raise the proper error.
if fstring_context.quote_char() == quote
&& fstring_context.is_triple_quoted() == triple_quoted
{
return Err(LexicalError {
error: LexicalErrorType::FStringError(FStringErrorType::UnclosedLbrace),
location: self.offset() - fstring_context.quote_size(),
});
}
}

let value_start = self.offset();

let value_end = loop {
Expand All @@ -697,6 +683,21 @@ impl<'source> Lexer<'source> {
}
}
Some('\r' | '\n') if !triple_quoted => {
if let Some(fstring_context) = self.fstring_stack.last() {
// When we are in an f-string, check whether does the initial quote
// matches with f-strings quotes and if it is, then this must be a
// missing '}' token so raise the proper error.
if fstring_context.quote_char() == quote
&& !fstring_context.is_triple_quoted()
{
return Err(LexicalError {
error: LexicalErrorType::FStringError(
FStringErrorType::UnclosedLbrace,
),
location: self.offset() - fstring_context.quote_size(),
});
}
}
return Err(LexicalError {
error: LexicalErrorType::OtherError(
"EOL while scanning string literal".to_owned(),
Expand All @@ -716,6 +717,21 @@ impl<'source> Lexer<'source> {

Some(_) => {}
None => {
if let Some(fstring_context) = self.fstring_stack.last() {
// When we are in an f-string, check whether does the initial quote
// matches with f-strings quotes and if it is, then this must be a
// missing '}' token so raise the proper error.
if fstring_context.quote_char() == quote
&& fstring_context.is_triple_quoted() == triple_quoted
{
return Err(LexicalError {
error: LexicalErrorType::FStringError(
FStringErrorType::UnclosedLbrace,
),
location: self.offset() - fstring_context.quote_size(),
});
}
}
return Err(LexicalError {
error: if triple_quoted {
LexicalErrorType::Eof
Expand Down

0 comments on commit 15887c9

Please sign in to comment.