From 95a86143fc4104b50118ab9f83bf25cd3bb7f062 Mon Sep 17 00:00:00 2001 From: everpcpc Date: Tue, 9 May 2023 12:29:16 +0800 Subject: [PATCH 1/2] fix(cli): support comment block --- cli/Cargo.toml | 2 +- cli/src/ast/tokenizer.rs | 8 ++++++-- cli/src/session.rs | 28 +++++++++++++++++++++++++++- cli/tests/00-base.result | 1 + cli/tests/00-base.sql | 10 ++++++++++ 5 files changed, 45 insertions(+), 4 deletions(-) diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 57e7642e2..ab096ed59 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bendsql" -version = "0.3.8" +version = "0.3.9" edition = "2021" license = "Apache-2.0" description = "Databend Native Command Line Tool" diff --git a/cli/src/ast/tokenizer.rs b/cli/src/ast/tokenizer.rs index 300bee54f..79e3255d0 100644 --- a/cli/src/ast/tokenizer.rs +++ b/cli/src/ast/tokenizer.rs @@ -101,8 +101,12 @@ pub enum TokenKind { #[regex(r"--[^\t\n\f]*", logos::skip)] Comment, - #[regex(r"/\*([^\*]|(\*[^/]))*\*/", logos::skip)] - CommentBlock, + // #[regex(r"/\*([^\*]|(\*[^/]))*\*/")] + // CommentBlock, + #[regex(r"/\*")] + CommentBlockStart, + #[regex(r"\*/")] + CommentBlockEnd, #[regex(r"[\n]+")] Newline, diff --git a/cli/src/session.rs b/cli/src/session.rs index eecf2ac2e..cec6bda9d 100644 --- a/cli/src/session.rs +++ b/cli/src/session.rs @@ -42,6 +42,7 @@ pub struct Session { settings: Settings, query: String, + comment_block_level: usize, } impl Session { @@ -65,6 +66,7 @@ impl Session { is_repl, settings, query: String::new(), + comment_block_level: 0, }) } @@ -196,6 +198,27 @@ impl Session { let mut start = 0; let mut in_comment = false; while let Some(Ok(token)) = tokenizer.next() { + match token.kind { + TokenKind::CommentBlockStart => { + self.comment_block_level += 1; + start = token.span.end; + continue; + } + TokenKind::CommentBlockEnd => { + if self.comment_block_level == 0 { + panic!("Unexpected comment block end"); + } + self.comment_block_level -= 1; + start = token.span.end; + continue; + } + _ => { + if self.comment_block_level > 0 { + start = token.span.end; + continue; + } + } + } match token.kind { TokenKind::SemiColon => { let mut sql = self.query.trim().to_owned(); @@ -212,8 +235,11 @@ impl Session { TokenKind::Newline | TokenKind::EOI => { in_comment = false; } + TokenKind::CommentBlockStart | TokenKind::CommentBlockEnd => { + unreachable!("Comment block should be handled before") + } _ => { - if !in_comment { + if !in_comment && self.comment_block_level == 0 { self.query.push_str(&line[start..token.span.end]); } } diff --git a/cli/tests/00-base.result b/cli/tests/00-base.result index 0a53a9f6d..3a6b33f85 100644 --- a/cli/tests/00-base.result +++ b/cli/tests/00-base.result @@ -4,4 +4,5 @@ a 1 true 1 2 3 +with comment bye diff --git a/cli/tests/00-base.sql b/cli/tests/00-base.sql index 482a2f432..ded161928 100644 --- a/cli/tests/00-base.sql +++ b/cli/tests/00-base.sql @@ -11,5 +11,15 @@ select '1';select 2; select 1+2; -- ignore this line +select /* ignore this block */ 'with comment'; + +/* ignore this block +select 'in comment block'; +*/ + +/* ignore this block /* nested comment */ +select 'in nested comment block'; +*/ + select 'bye'; drop table test; From 6ec89f45d73422f695d0423ea641c4661350facf Mon Sep 17 00:00:00 2001 From: everpcpc Date: Tue, 9 May 2023 12:51:35 +0800 Subject: [PATCH 2/2] z --- cli/src/session.rs | 14 +++++++------- cli/tests/00-base.sql | 6 +----- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/cli/src/session.rs b/cli/src/session.rs index cec6bda9d..3bc34ae6b 100644 --- a/cli/src/session.rs +++ b/cli/src/session.rs @@ -42,7 +42,7 @@ pub struct Session { settings: Settings, query: String, - comment_block_level: usize, + in_comment_block: bool, } impl Session { @@ -66,7 +66,7 @@ impl Session { is_repl, settings, query: String::new(), - comment_block_level: 0, + in_comment_block: false, }) } @@ -200,20 +200,20 @@ impl Session { while let Some(Ok(token)) = tokenizer.next() { match token.kind { TokenKind::CommentBlockStart => { - self.comment_block_level += 1; + self.in_comment_block = true; start = token.span.end; continue; } TokenKind::CommentBlockEnd => { - if self.comment_block_level == 0 { + if !self.in_comment_block { panic!("Unexpected comment block end"); } - self.comment_block_level -= 1; + self.in_comment_block = false; start = token.span.end; continue; } _ => { - if self.comment_block_level > 0 { + if self.in_comment_block { start = token.span.end; continue; } @@ -239,7 +239,7 @@ impl Session { unreachable!("Comment block should be handled before") } _ => { - if !in_comment && self.comment_block_level == 0 { + if !in_comment && !self.in_comment_block { self.query.push_str(&line[start..token.span.end]); } } diff --git a/cli/tests/00-base.sql b/cli/tests/00-base.sql index ded161928..a932ae7c7 100644 --- a/cli/tests/00-base.sql +++ b/cli/tests/00-base.sql @@ -13,13 +13,9 @@ select '1';select 2; select 1+2; select /* ignore this block */ 'with comment'; -/* ignore this block +/* ignore this block /* /* select 'in comment block'; */ -/* ignore this block /* nested comment */ -select 'in nested comment block'; -*/ - select 'bye'; drop table test;