From 9002abe81b026ead35ac66bfc3df3608b66a8c88 Mon Sep 17 00:00:00 2001 From: Pedro Date: Tue, 4 Nov 2014 00:02:40 +0100 Subject: [PATCH] added an error statement type --- src/.gitignore | 1 + src/lib/Statement.h | 3 +++ src/parser/SQLParser.cpp | 5 ++--- src/parser/bison_parser.y | 5 +++-- src/sql_grammar_test.cpp | 9 +++++++-- 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/.gitignore b/src/.gitignore index d21e6e3..5e65d69 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -4,4 +4,5 @@ flex_lexer.h bison_parser.c* bison_parser.h *.o +*.output copy*.sh diff --git a/src/lib/Statement.h b/src/lib/Statement.h index fa503bc..74f2cef 100644 --- a/src/lib/Statement.h +++ b/src/lib/Statement.h @@ -12,6 +12,7 @@ namespace hsql { typedef enum { + kStmtError, kStmtSelect, kStmtJoin, kStmtDelete, @@ -54,7 +55,9 @@ typedef enum { struct Statement { Statement(StatementType type) : type(type) {}; + StatementType type; + const char* parser_msg; }; diff --git a/src/parser/SQLParser.cpp b/src/parser/SQLParser.cpp index 91951d1..578193f 100644 --- a/src/parser/SQLParser.cpp +++ b/src/parser/SQLParser.cpp @@ -27,9 +27,8 @@ Statement* SQLParser::parseSQLString(const char *text) { state = hsql__scan_string(text, scanner); if (hsql_parse(&stmt, scanner)) { - // error parsing - // fprintf(stderr, "Error when parsing!\n"); - return NULL; + // Returns an error stmt object + return stmt; } hsql__delete_buffer(state, scanner); diff --git a/src/parser/bison_parser.y b/src/parser/bison_parser.y index 0d8fc71..317f2e4 100644 --- a/src/parser/bison_parser.y +++ b/src/parser/bison_parser.y @@ -19,8 +19,9 @@ using namespace hsql; -int yyerror(Statement **expression, yyscan_t scanner, const char *msg) { - // fprintf(stderr, "[Error] SQL Parser: %s\n", msg); +int yyerror(Statement **stmt, yyscan_t scanner, const char *msg) { + *stmt = new Statement(kStmtError); + (*stmt)->parser_msg = msg; return 0; } diff --git a/src/sql_grammar_test.cpp b/src/sql_grammar_test.cpp index 56c65d5..511d7c3 100644 --- a/src/sql_grammar_test.cpp +++ b/src/sql_grammar_test.cpp @@ -28,11 +28,16 @@ int main(int argc, char *argv[]) { end = std::chrono::system_clock::now(); std::chrono::duration elapsed_seconds = end-start; - if (expectFalse != (stmt == NULL)) { + if (expectFalse != (stmt->type == kStmtError)) { fprintf(stderr, "-> Failed (%.3fms)! \"%s\"\n", elapsed_seconds.count()*1000, sql); continue; } else { - printf("Success (%.3fms%s)! \"%s\"\n", elapsed_seconds.count()*1000, (expectFalse) ? ", expected error" : "", sql); + if (expectFalse) { + printf("Success (%.3fms)! %s: \"%s\"\n", elapsed_seconds.count()*1000, stmt->parser_msg, sql); + + } else { + printf("Success (%.3fms)! \"%s\"\n", elapsed_seconds.count()*1000, sql); + } } }