Skip to content

Commit e1bd5c3

Browse files
committed
Add option for nested comments to dialects
1 parent d7c8b3b commit e1bd5c3

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

src/dialect/generic.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,8 @@ impl Dialect for GenericDialect {
131131
fn supports_empty_projections(&self) -> bool {
132132
true
133133
}
134+
135+
fn supports_nested_comments(&self) -> bool {
136+
true
137+
}
134138
}

src/dialect/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,12 @@ pub trait Dialect: Debug + Any {
682682
false
683683
}
684684

685+
/// Returns true if the dialect supports nested comments
686+
/// e.g. `/* /* nested */ */`
687+
fn supports_nested_comments(&self) -> bool {
688+
false
689+
}
690+
685691
/// Returns true if this dialect supports treating the equals operator `=` within a `SelectItem`
686692
/// as an alias assignment operator, rather than a boolean expression.
687693
/// For example: the following statements are equivalent for such a dialect:

src/dialect/postgresql.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ impl Dialect for PostgreSqlDialect {
241241
fn supports_empty_projections(&self) -> bool {
242242
true
243243
}
244+
245+
fn supports_nested_comments(&self) -> bool {
246+
true
247+
}
244248
}
245249

246250
pub fn parse_create(parser: &mut Parser) -> Option<Result<Statement, ParserError>> {

src/tokenizer.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1855,11 +1855,12 @@ impl<'a> Tokenizer<'a> {
18551855
) -> Result<Option<Token>, TokenizerError> {
18561856
let mut s = String::new();
18571857
let mut nested = 1;
1858+
let supports_nested_comments = self.dialect.supports_nested_comments();
18581859

18591860
loop {
18601861
match chars.next() {
18611862
Some(ch) => {
1862-
if ch == '/' && matches!(chars.peek(), Some('*')) {
1863+
if ch == '/' && matches!(chars.peek(), Some('*')) && supports_nested_comments {
18631864
s.push(ch);
18641865
s.push(chars.next().unwrap()); // consume the '*'
18651866
nested += 1;
@@ -2792,6 +2793,26 @@ mod tests {
27922793
compare(expected, tokens);
27932794
}
27942795

2796+
#[test]
2797+
fn tokenize_nested_comments_if_not_supported() {
2798+
let dialect = SQLiteDialect {};
2799+
let sql = "SELECT 1/*/* nested comment */*/0";
2800+
let tokens = Tokenizer::new(&dialect, sql).tokenize();
2801+
let expected = vec![
2802+
Token::make_keyword("SELECT"),
2803+
Token::Whitespace(Whitespace::Space),
2804+
Token::Number("1".to_string(), false),
2805+
Token::Whitespace(Whitespace::MultiLineComment(
2806+
"/* nested comment ".to_string(),
2807+
)),
2808+
Token::Mul,
2809+
Token::Div,
2810+
Token::Number("0".to_string(), false),
2811+
];
2812+
2813+
compare(expected, tokens.unwrap());
2814+
}
2815+
27952816
#[test]
27962817
fn tokenize_multiline_comment_with_even_asterisks() {
27972818
let sql = String::from("\n/** Comment **/\n");

0 commit comments

Comments
 (0)