-
Notifications
You must be signed in to change notification settings - Fork 605
Correctly tokenize nested comments #1629
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b56b9be
Correctly handle nested comments
hansott 5c6d3ed
Fix comment
hansott f068f5e
Use loop with test cases
hansott d7c8b3b
Add character after nested comment
hansott e1bd5c3
Add option for nested comments to dialects
hansott 4a6ab2b
Move if statements to match inside loop and avoid unwrap
hansott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1855,22 +1855,31 @@ impl<'a> Tokenizer<'a> { | |
) -> Result<Option<Token>, TokenizerError> { | ||
let mut s = String::new(); | ||
let mut nested = 1; | ||
let mut last_ch = ' '; | ||
let supports_nested_comments = self.dialect.supports_nested_comments(); | ||
|
||
loop { | ||
match chars.next() { | ||
Some(ch) => { | ||
if last_ch == '/' && ch == '*' { | ||
if ch == '/' && matches!(chars.peek(), Some('*')) && supports_nested_comments { | ||
s.push(ch); | ||
s.push(chars.next().unwrap()); // consume the '*' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we rewrite this to return an error in order to avoid the unwrap? |
||
nested += 1; | ||
} else if last_ch == '*' && ch == '/' { | ||
continue; | ||
} | ||
|
||
if ch == '*' && matches!(chars.peek(), Some('/')) { | ||
s.push(ch); | ||
let slash = chars.next(); | ||
nested -= 1; | ||
if nested == 0 { | ||
s.pop(); | ||
s.pop(); // remove the last '*' | ||
break Ok(Some(Token::Whitespace(Whitespace::MultiLineComment(s)))); | ||
} | ||
s.push(slash.unwrap()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, it would be nice if we can avoid unwraping in code |
||
continue; | ||
} | ||
|
||
s.push(ch); | ||
last_ch = ch; | ||
} | ||
None => { | ||
break self.tokenizer_error( | ||
|
@@ -2718,18 +2727,90 @@ mod tests { | |
|
||
#[test] | ||
fn tokenize_nested_multiline_comment() { | ||
let sql = String::from("0/*multi-line\n* \n/* comment \n /*comment*/*/ */ /comment*/1"); | ||
let dialect = GenericDialect {}; | ||
let test_cases = vec![ | ||
( | ||
"0/*multi-line\n* \n/* comment \n /*comment*/*/ */ /comment*/1", | ||
vec![ | ||
Token::Number("0".to_string(), false), | ||
Token::Whitespace(Whitespace::MultiLineComment( | ||
"multi-line\n* \n/* comment \n /*comment*/*/ ".into(), | ||
)), | ||
Token::Whitespace(Whitespace::Space), | ||
Token::Div, | ||
Token::Word(Word { | ||
value: "comment".to_string(), | ||
quote_style: None, | ||
keyword: Keyword::COMMENT, | ||
}), | ||
Token::Mul, | ||
Token::Div, | ||
Token::Number("1".to_string(), false), | ||
], | ||
), | ||
( | ||
"0/*multi-line\n* \n/* comment \n /*comment/**/ */ /comment*/*/1", | ||
vec![ | ||
Token::Number("0".to_string(), false), | ||
Token::Whitespace(Whitespace::MultiLineComment( | ||
"multi-line\n* \n/* comment \n /*comment/**/ */ /comment*/".into(), | ||
)), | ||
Token::Number("1".to_string(), false), | ||
], | ||
), | ||
( | ||
"SELECT 1/* a /* b */ c */0", | ||
vec![ | ||
Token::make_keyword("SELECT"), | ||
Token::Whitespace(Whitespace::Space), | ||
Token::Number("1".to_string(), false), | ||
Token::Whitespace(Whitespace::MultiLineComment(" a /* b */ c ".to_string())), | ||
Token::Number("0".to_string(), false), | ||
], | ||
), | ||
]; | ||
|
||
for (sql, expected) in test_cases { | ||
let tokens = Tokenizer::new(&dialect, sql).tokenize().unwrap(); | ||
compare(expected, tokens); | ||
} | ||
} | ||
|
||
#[test] | ||
fn tokenize_nested_multiline_comment_empty() { | ||
let sql = "select 1/*/**/*/0"; | ||
|
||
let dialect = GenericDialect {}; | ||
let tokens = Tokenizer::new(&dialect, &sql).tokenize().unwrap(); | ||
let tokens = Tokenizer::new(&dialect, sql).tokenize().unwrap(); | ||
let expected = vec![ | ||
Token::make_keyword("select"), | ||
Token::Whitespace(Whitespace::Space), | ||
Token::Number("1".to_string(), false), | ||
Token::Whitespace(Whitespace::MultiLineComment("/**/".to_string())), | ||
Token::Number("0".to_string(), false), | ||
]; | ||
|
||
compare(expected, tokens); | ||
} | ||
|
||
#[test] | ||
fn tokenize_nested_comments_if_not_supported() { | ||
let dialect = SQLiteDialect {}; | ||
let sql = "SELECT 1/*/* nested comment */*/0"; | ||
let tokens = Tokenizer::new(&dialect, sql).tokenize(); | ||
let expected = vec![ | ||
Token::make_keyword("SELECT"), | ||
Token::Whitespace(Whitespace::Space), | ||
Token::Number("1".to_string(), false), | ||
Token::Whitespace(Whitespace::MultiLineComment( | ||
"multi-line\n* \n/* comment \n /*comment*/*/ */ /comment".to_string(), | ||
"/* nested comment ".to_string(), | ||
)), | ||
Token::Number("1".to_string(), false), | ||
Token::Mul, | ||
Token::Div, | ||
Token::Number("0".to_string(), false), | ||
]; | ||
compare(expected, tokens); | ||
|
||
compare(expected, tokens.unwrap()); | ||
} | ||
|
||
#[test] | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we move some of the condition out to the match statement? thinking that way we avoid extra nesting + continue. And the different cases are clearer. e.g.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, will do!