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..3bc34ae6b 100644 --- a/cli/src/session.rs +++ b/cli/src/session.rs @@ -42,6 +42,7 @@ pub struct Session { settings: Settings, query: String, + in_comment_block: bool, } impl Session { @@ -65,6 +66,7 @@ impl Session { is_repl, settings, query: String::new(), + in_comment_block: false, }) } @@ -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.in_comment_block = true; + start = token.span.end; + continue; + } + TokenKind::CommentBlockEnd => { + if !self.in_comment_block { + panic!("Unexpected comment block end"); + } + self.in_comment_block = false; + start = token.span.end; + continue; + } + _ => { + if self.in_comment_block { + 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.in_comment_block { 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..a932ae7c7 100644 --- a/cli/tests/00-base.sql +++ b/cli/tests/00-base.sql @@ -11,5 +11,11 @@ select '1';select 2; select 1+2; -- ignore this line +select /* ignore this block */ 'with comment'; + +/* ignore this block /* /* +select 'in comment block'; +*/ + select 'bye'; drop table test;