added an error statement type

This commit is contained in:
Pedro 2014-11-04 00:02:40 +01:00
parent 71e80cc17e
commit 9002abe81b
5 changed files with 16 additions and 7 deletions

1
src/.gitignore vendored
View File

@ -4,4 +4,5 @@ flex_lexer.h
bison_parser.c* bison_parser.c*
bison_parser.h bison_parser.h
*.o *.o
*.output
copy*.sh copy*.sh

View File

@ -12,6 +12,7 @@
namespace hsql { namespace hsql {
typedef enum { typedef enum {
kStmtError,
kStmtSelect, kStmtSelect,
kStmtJoin, kStmtJoin,
kStmtDelete, kStmtDelete,
@ -54,7 +55,9 @@ typedef enum {
struct Statement { struct Statement {
Statement(StatementType type) : type(type) {}; Statement(StatementType type) : type(type) {};
StatementType type; StatementType type;
const char* parser_msg;
}; };

View File

@ -27,9 +27,8 @@ Statement* SQLParser::parseSQLString(const char *text) {
state = hsql__scan_string(text, scanner); state = hsql__scan_string(text, scanner);
if (hsql_parse(&stmt, scanner)) { if (hsql_parse(&stmt, scanner)) {
// error parsing // Returns an error stmt object
// fprintf(stderr, "Error when parsing!\n"); return stmt;
return NULL;
} }
hsql__delete_buffer(state, scanner); hsql__delete_buffer(state, scanner);

View File

@ -19,8 +19,9 @@
using namespace hsql; using namespace hsql;
int yyerror(Statement **expression, yyscan_t scanner, const char *msg) { int yyerror(Statement **stmt, yyscan_t scanner, const char *msg) {
// fprintf(stderr, "[Error] SQL Parser: %s\n", msg); *stmt = new Statement(kStmtError);
(*stmt)->parser_msg = msg;
return 0; return 0;
} }

View File

@ -28,11 +28,16 @@ int main(int argc, char *argv[]) {
end = std::chrono::system_clock::now(); end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start; std::chrono::duration<double> elapsed_seconds = end-start;
if (expectFalse != (stmt == NULL)) { if (expectFalse != (stmt->type == kStmtError)) {
fprintf(stderr, "-> Failed (%.3fms)! \"%s\"\n", elapsed_seconds.count()*1000, sql); fprintf(stderr, "-> Failed (%.3fms)! \"%s\"\n", elapsed_seconds.count()*1000, sql);
continue; continue;
} else { } 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);
}
} }
} }