Skip to content

Commit

Permalink
Move if statements to match inside loop and avoid unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Jan 5, 2025
1 parent e1bd5c3 commit 4a6ab2b
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1859,33 +1859,29 @@ impl<'a> Tokenizer<'a> {

loop {
match chars.next() {
Some(ch) => {
if ch == '/' && matches!(chars.peek(), Some('*')) && supports_nested_comments {
s.push(ch);
s.push(chars.next().unwrap()); // consume the '*'
nested += 1;
continue;
}

if ch == '*' && matches!(chars.peek(), Some('/')) {
s.push(ch);
let slash = chars.next();
nested -= 1;
if nested == 0 {
s.pop(); // remove the last '*'
break Ok(Some(Token::Whitespace(Whitespace::MultiLineComment(s))));
}
s.push(slash.unwrap());
continue;
Some('/') if matches!(chars.peek(), Some('*')) && supports_nested_comments => {
chars.next(); // consume the '*'
s.push('/');
s.push('*');
nested += 1;
}
Some('*') if matches!(chars.peek(), Some('/')) => {
chars.next(); // consume the '/'
nested -= 1;
if nested == 0 {
break Ok(Some(Token::Whitespace(Whitespace::MultiLineComment(s))));
}

s.push('*');
s.push('/');
}
Some(ch) => {
s.push(ch);
}
None => {
break self.tokenizer_error(
chars.location(),
"Unexpected EOF while in a multi-line comment",
)
);
}
}
}
Expand Down

0 comments on commit 4a6ab2b

Please sign in to comment.