From 027f639a68c33085ede45757d3a5003a4fd02728 Mon Sep 17 00:00:00 2001 From: everpcpc Date: Tue, 9 May 2023 12:56:56 +0800 Subject: [PATCH] fix(cli): support comment block (#111) --- 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 | 6 ++++++ 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 57e7642e..ab096ed5 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 300bee54..79e3255d 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 eecf2ac2..3bc34ae6 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 0a53a9f6..3a6b33f8 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 482a2f43..a932ae7c 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;