Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e22aa78

Browse files
authoredNov 30, 2022
fix: handle nested comments (#726)
1 parent 3163597 commit e22aa78

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed
 

‎src/tokenizer.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -832,22 +832,23 @@ impl<'a> Tokenizer<'a> {
832832
chars: &mut Peekable<Chars<'_>>,
833833
) -> Result<Option<Token>, TokenizerError> {
834834
let mut s = String::new();
835-
let mut maybe_closing_comment = false;
836-
// TODO: deal with nested comments
835+
let mut nested = 1;
836+
let mut last_ch = ' ';
837+
837838
loop {
838839
match chars.next() {
839840
Some(ch) => {
840-
if maybe_closing_comment {
841-
if ch == '/' {
841+
if last_ch == '/' && ch == '*' {
842+
nested += 1;
843+
} else if last_ch == '*' && ch == '/' {
844+
nested -= 1;
845+
if nested == 0 {
846+
s.pop();
842847
break Ok(Some(Token::Whitespace(Whitespace::MultiLineComment(s))));
843-
} else {
844-
s.push('*');
845848
}
846849
}
847-
maybe_closing_comment = ch == '*';
848-
if !maybe_closing_comment {
849-
s.push(ch);
850-
}
850+
s.push(ch);
851+
last_ch = ch;
851852
}
852853
None => break self.tokenizer_error("Unexpected EOF while in a multi-line comment"),
853854
}
@@ -1355,6 +1356,23 @@ mod tests {
13551356
compare(expected, tokens);
13561357
}
13571358

1359+
#[test]
1360+
fn tokenize_nested_multiline_comment() {
1361+
let sql = String::from("0/*multi-line\n* \n/* comment \n /*comment*/*/ */ /comment*/1");
1362+
1363+
let dialect = GenericDialect {};
1364+
let mut tokenizer = Tokenizer::new(&dialect, &sql);
1365+
let tokens = tokenizer.tokenize().unwrap();
1366+
let expected = vec![
1367+
Token::Number("0".to_string(), false),
1368+
Token::Whitespace(Whitespace::MultiLineComment(
1369+
"multi-line\n* \n/* comment \n /*comment*/*/ */ /comment".to_string(),
1370+
)),
1371+
Token::Number("1".to_string(), false),
1372+
];
1373+
compare(expected, tokens);
1374+
}
1375+
13581376
#[test]
13591377
fn tokenize_multiline_comment_with_even_asterisks() {
13601378
let sql = String::from("\n/** Comment **/\n");

0 commit comments

Comments
 (0)
Please sign in to comment.