Skip to content

Commit c8085bb

Browse files
Add COMMENT to Scanner grammar rule (#26)
Supports line comments as per SQL-92 and nested block comments as per SQL-99. Co-authored-by: Reza Handzalah <reza.handzalah@mbiz.co.id>
1 parent 88ada3b commit c8085bb

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

partiql-parser/src/partiql.pest

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
WHITESPACE = _{ " " | "\t" | "\x0B" | "\x0C" | "\r" | "\n" }
22

3+
COMMENT = _{ LineComment | BlockComment }
4+
5+
BlockComment = { "/*" ~ ( BlockComment | (!"*/" ~ ANY) )* ~ "*/" }
6+
LineComment = { "--" ~ ( !"\n" ~ ANY )* ~ "\n" }
7+
38
// TODO implement a full grammar, this is a very primitive version to start
49
// working with Pest and its APIs.
510

partiql-parser/src/scanner.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,63 @@ mod test {
399399
};
400400
}
401401

402+
#[rstest]
403+
#[case::comment_single_keyword(
404+
scanner_test_case![
405+
"--",
406+
"SELECT",
407+
" \n ",
408+
]
409+
)]
410+
#[rstest]
411+
#[case::comment_mid_line(
412+
scanner_test_case![
413+
"SELECT" => keyword("SELECT"),
414+
" ",
415+
"FROM" => keyword("FROM"),
416+
" -- ",
417+
"WHERE",
418+
" \n ",
419+
]
420+
)]
421+
#[rstest]
422+
#[case::comment_until_eol(
423+
scanner_test_case![
424+
" -- ",
425+
"CASE",
426+
" ",
427+
"IN",
428+
" ",
429+
"WHERE",
430+
" \n ",
431+
"SELECT" => keyword("SELECT"),
432+
]
433+
)]
434+
#[rstest]
435+
#[case::comment_block(
436+
scanner_test_case![
437+
" /* ",
438+
"CASE",
439+
" ",
440+
"IN",
441+
" */ ",
442+
"SELECT" => keyword("SELECT"),
443+
]
444+
)]
445+
#[rstest]
446+
#[case::comment_block_nested(
447+
scanner_test_case![
448+
"employee" => identifier("employee"),
449+
" /*\n ",
450+
"CASE",
451+
" /* ",
452+
"WHERE",
453+
" \n */ ",
454+
"employee",
455+
" \n\n*/ ",
456+
"IN" => keyword("IN"),
457+
]
458+
)]
402459
#[rstest]
403460
#[case::single_keyword(
404461
scanner_test_case![
@@ -532,6 +589,11 @@ mod test {
532589

533590
#[rstest]
534591
#[case::bad_identifier("💩")]
592+
#[case::unterminated_line_comment("-- DROP")]
593+
#[case::unbalanced_block_comment("/*\n\n SELECT /* WHERE */")]
594+
#[case::unbalanced_block_comment("/* CASE do WHEN re THEN mi ELSE fa END /*")]
595+
#[case::unbalanced_block_comment("/*SELECT /* FROM /* FULL OUTER JOIN */ */ ")]
596+
#[case::unbalanced_block_comment("/*/*/*/*/*/*/*/*[ascii art here]*/*/*/*/*/*/*/ ")]
535597
fn bad_tokens(#[case] input: &str) -> ParserResult<()> {
536598
let expecteds = vec![syntax_error("IGNORED MESSAGE", Position::at(1, 1))];
537599
assert_input(input, expecteds)

0 commit comments

Comments
 (0)