From 34da5535fe488922be40f4eb532c509c35509b66 Mon Sep 17 00:00:00 2001 From: Pedro Date: Wed, 5 Nov 2014 15:54:41 +0100 Subject: [PATCH] added support for comments and like operator --- src/build_and_run_tests.sh | 6 ++++- src/lib/Expr.h | 1 + src/parser/bison_parser.y | 31 +++++++++++++------------ src/parser/flex_lexer.l | 47 ++++++++++++++++++++++++++------------ 4 files changed, 55 insertions(+), 30 deletions(-) diff --git a/src/build_and_run_tests.sh b/src/build_and_run_tests.sh index f46fea6..3227fa9 100644 --- a/src/build_and_run_tests.sh +++ b/src/build_and_run_tests.sh @@ -13,7 +13,8 @@ echo "\n\n" ./bin/grammar_test "SELECT col1, col2, 'test' FROM table, foo AS t WHERE age > 12 AND zipcode = 12345 GROUP BY col1;" ./bin/grammar_test "SELECT age FROM table AS t1, (SELECT * FROM table2) AS t2 ORDER BY age DESC" ./bin/grammar_test "SELECT * from table JOIN table2 ON a = b WHERE (b OR NOT a) AND a = 12.5" -./bin/grammar_test "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c = 3 LIMIT 10);" +./bin/grammar_test "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c LIKE 's%' LIMIT 10);" +./bin/grammar_test "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c NOT LIKE 's%' LIMIT 10);" ./bin/grammar_test "SELECT t1.a, t1.b, t2.c FROM table AS t1 JOIN (SELECT * FROM foo JOIN bar ON foo.id = bar.id) t2 ON t1.a = t2.b WHERE (t1.b OR NOT t1.a) AND t2.c = 12.5" # Error: Where clause in between join statement @@ -26,6 +27,9 @@ echo "\n\n" # ./bin/analysis "SELECT col1, col2, 'test' FROM table t1, foo WHERE age > 12 AND zipcode = 12345 GROUP BY col1 ORDER BY col2 DESC LIMIT 100;" # ./bin/analysis "SELECT * from table AS t1 JOIN table2 AS t2 ON t1.a = t2.b WHERE (b OR NOT a) AND a = 12.5" ./bin/analysis "SELECT t1.a, t1.b, t2.c FROM table AS t1 JOIN (SELECT * FROM foo JOIN bar ON foo.id = bar.id) t2 ON t1.a = t2.b WHERE (t1.b OR NOT t1.a) AND t2.c = 12.5" +./bin/analysis "-- test +SELECT * FROM table WHERE a NOT LIKE '%s' -- inline comment +--my comment" # ./bin/analysis "SELECT * from table WHERE (b OR NOT a) AND a = 12.5 JOIN table2 ON a = b" echo "\n\n" \ No newline at end of file diff --git a/src/lib/Expr.h b/src/lib/Expr.h index 879309a..e992cfc 100644 --- a/src/lib/Expr.h +++ b/src/lib/Expr.h @@ -33,6 +33,7 @@ struct Expr { LESS_EQ, GREATER_EQ, LIKE, + NOT_LIKE, AND, OR, // Unary diff --git a/src/parser/bison_parser.y b/src/parser/bison_parser.y index b8b78f7..b124bd4 100644 --- a/src/parser/bison_parser.y +++ b/src/parser/bison_parser.y @@ -92,9 +92,10 @@ typedef void* yyscan_t; /********************************* ** Token Definition *********************************/ -%token SELECT FROM WHERE GROUP BY HAVING ORDER ASC DESC LIMIT +%token SELECT FROM WHERE GROUP BY HAVING ORDER ASC DESC LIMIT DISTINCT OFFSET %token JOIN ON INNER OUTER LEFT RIGHT CROSS USING NATURAL %token CREATE TABLE DATABASE INDEX +%token DELETE INSERT %token AS NOT AND OR NULL LIKE %token NAME STRING COMPARISON %token FLOAT @@ -126,7 +127,7 @@ typedef void* yyscan_t; %left OR %left AND %right NOT -%right '=' EQUALS NOTEQUALS +%right '=' EQUALS NOTEQUALS LIKE %nonassoc '<' '>' LESS GREATER LESSEQ GREATEREQ %nonassoc NOTNULL @@ -249,22 +250,24 @@ unary_expr: binary_expr: comp_expr - | expr '-' expr { $$ = Expr::makeOpBinary($1, '-', $3); } - | expr '+' expr { $$ = Expr::makeOpBinary($1, '+', $3); } - | expr '/' expr { $$ = Expr::makeOpBinary($1, '/', $3); } - | expr '*' expr { $$ = Expr::makeOpBinary($1, '*', $3); } - | expr AND expr { $$ = Expr::makeOpBinary($1, Expr::AND, $3); } - | expr OR expr { $$ = Expr::makeOpBinary($1, Expr::OR, $3); } + | expr '-' expr { $$ = Expr::makeOpBinary($1, '-', $3); } + | expr '+' expr { $$ = Expr::makeOpBinary($1, '+', $3); } + | expr '/' expr { $$ = Expr::makeOpBinary($1, '/', $3); } + | expr '*' expr { $$ = Expr::makeOpBinary($1, '*', $3); } + | expr AND expr { $$ = Expr::makeOpBinary($1, Expr::AND, $3); } + | expr OR expr { $$ = Expr::makeOpBinary($1, Expr::OR, $3); } + | expr LIKE expr { $$ = Expr::makeOpBinary($1, Expr::LIKE, $3); } + | expr NOT LIKE expr { $$ = Expr::makeOpBinary($1, Expr::NOT_LIKE, $4); } ; comp_expr: - expr EQUALS expr { $$ = Expr::makeOpBinary($1, '=', $3); } - | expr NOTEQUALS expr { $$ = Expr::makeOpBinary($1, Expr::NOT_EQUALS, $3); } - | expr LESS expr { $$ = Expr::makeOpBinary($1, '<', $3); } - | expr GREATER expr { $$ = Expr::makeOpBinary($1, '>', $3); } - | expr LESSEQ expr { $$ = Expr::makeOpBinary($1, Expr::LESS_EQ, $3); } - | expr GREATEREQ expr { $$ = Expr::makeOpBinary($1, Expr::GREATER_EQ, $3); } + expr EQUALS expr { $$ = Expr::makeOpBinary($1, '=', $3); } + | expr NOTEQUALS expr { $$ = Expr::makeOpBinary($1, Expr::NOT_EQUALS, $3); } + | expr LESS expr { $$ = Expr::makeOpBinary($1, '<', $3); } + | expr GREATER expr { $$ = Expr::makeOpBinary($1, '>', $3); } + | expr LESSEQ expr { $$ = Expr::makeOpBinary($1, Expr::LESS_EQ, $3); } + | expr GREATEREQ expr { $$ = Expr::makeOpBinary($1, Expr::GREATER_EQ, $3); } ; function_expr: diff --git a/src/parser/flex_lexer.l b/src/parser/flex_lexer.l index b0807e5..fba200a 100644 --- a/src/parser/flex_lexer.l +++ b/src/parser/flex_lexer.l @@ -41,30 +41,47 @@ /* %option nodefault */ +%s COMMENT /*************************** ** Section 3: Rules ***************************/ %% +-- BEGIN(COMMENT); +[^\n]* /* skipping comment content until a end of line is read */; +\n BEGIN(INITIAL); + + [ \t\n]+ /* skip whitespace */; -SELECT TOKEN(SELECT) -FROM TOKEN(FROM) -GROUP TOKEN(GROUP) -BY TOKEN(BY) -WHERE TOKEN(WHERE) -NOT TOKEN(NOT) -AND TOKEN(AND) -OR TOKEN(OR) -AS TOKEN(AS) -LIMIT TOKEN(LIMIT) -ORDER TOKEN(ORDER) -ASC TOKEN(ASC) -DESC TOKEN(DESC) -JOIN TOKEN(JOIN) -ON TOKEN(ON) +DISTINCT TOKEN(DISTINCT) +OFFSET TOKEN(OFFSET) +SELECT TOKEN(SELECT) +INSERT TOKEN(INSERT) +CREATE TOKEN(CREATE) +DELETE TOKEN(DELETE) +HAVING TOKEN(HAVING) +GROUP TOKEN(GROUP) +WHERE TOKEN(WHERE) +LIMIT TOKEN(LIMIT) +ORDER TOKEN(ORDER) +INNER TOKEN(INNER) +OUTER TOKEN(OUTER) +CROSS TOKEN(CROSS) +FROM TOKEN(FROM) +LIKE TOKEN(LIKE) +JOIN TOKEN(JOIN) +DESC TOKEN(DESC) +ASC TOKEN(ASC) +NOT TOKEN(NOT) +AND TOKEN(AND) +BY TOKEN(BY) +OR TOKEN(OR) +AS TOKEN(AS) +ON TOKEN(ON) + "=" TOKEN(EQUALS) "<>" TOKEN(NOTEQUALS)