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.h
*.o
*.output
copy*.sh

View File

@ -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;
};

View File

@ -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);

View File

@ -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;
}

View File

@ -28,11 +28,16 @@ int main(int argc, char *argv[]) {
end = std::chrono::system_clock::now();
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);
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);
}
}
}