Skip to content

Commit

Permalink
fix(cli): support comment block (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc authored May 9, 2023
1 parent 8088208 commit 027f639
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
8 changes: 6 additions & 2 deletions cli/src/ast/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 27 additions & 1 deletion cli/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct Session {

settings: Settings,
query: String,
in_comment_block: bool,
}

impl Session {
Expand All @@ -65,6 +66,7 @@ impl Session {
is_repl,
settings,
query: String::new(),
in_comment_block: false,
})
}

Expand Down Expand Up @@ -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();
Expand All @@ -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]);
}
}
Expand Down
1 change: 1 addition & 0 deletions cli/tests/00-base.result
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ a 1 true
1
2
3
with comment
bye
6 changes: 6 additions & 0 deletions cli/tests/00-base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 027f639

Please sign in to comment.