From 074c564cc424722c1dd53cb92a88c42c6922cf0f Mon Sep 17 00:00:00 2001 From: Pedro Flemming Date: Fri, 7 Apr 2017 16:07:14 +0200 Subject: [PATCH] move initialization of SQLParserResult to SQLParser from bison_parser --- src/SQLParser.cpp | 5 +++-- src/parser/bison_parser.y | 35 +++++++++++++++++++---------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/SQLParser.cpp b/src/SQLParser.cpp index 55ae5f7..5f89911 100644 --- a/src/SQLParser.cpp +++ b/src/SQLParser.cpp @@ -13,20 +13,21 @@ namespace hsql { } SQLParserResult* SQLParser::parseSQLString(const char* text) { - SQLParserResult* result = NULL; + SQLParserResult* result = new SQLParserResult(); yyscan_t scanner; YY_BUFFER_STATE state; if (hsql_lex_init(&scanner)) { // Couldn't initialize the lexer. fprintf(stderr, "[Error] SQLParser: Error when initializing lexer!\n"); + delete result; return NULL; } state = hsql__scan_string(text, scanner); // Parser and return early if it failed. - if (hsql_parse(&result, scanner)) { + if (hsql_parse(result, scanner)) { // Returns an error stmt object. hsql__delete_buffer(state, scanner); hsql_lex_destroy(scanner); diff --git a/src/parser/bison_parser.y b/src/parser/bison_parser.y index 4ecdb93..c0252d0 100644 --- a/src/parser/bison_parser.y +++ b/src/parser/bison_parser.y @@ -18,14 +18,9 @@ using namespace hsql; -int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const char *msg) { - delete *result; - - SQLParserResult* list = new SQLParserResult(); - list->setIsValid(false); - list->setErrorDetails(strdup(msg), llocp->first_line, llocp->first_column); - - *result = list; +int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const char *msg) { + result->setIsValid(false); + result->setErrorDetails(strdup(msg), llocp->first_line, llocp->first_column); return 0; } @@ -90,7 +85,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch %lex-param { yyscan_t scanner } // Define additional parameters for yyparse -%parse-param { hsql::SQLParserResult** result } +%parse-param { hsql::SQLParserResult* result } %parse-param { yyscan_t scanner } @@ -124,7 +119,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch hsql::GroupByDescription* group_t; hsql::UpdateClause* update_t; - hsql::SQLParserResult* stmt_list; + std::vector* stmt_vec; std::vector* str_vec; std::vector* table_vec; @@ -147,7 +142,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch } } delete ($$); -} +} %destructor { delete ($$); } <*> @@ -178,7 +173,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch /********************************* ** Non-Terminal types (http://www.gnu.org/software/bison/manual/html_node/Type-Decl.html) *********************************/ -%type statement_list +%type statement_list %type statement preparable_statement %type execute_statement %type prepare_statement @@ -243,14 +238,18 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch // Defines our general input. input: statement_list opt_semicolon { - *result = $1; + for (SQLStatement* stmt : *$1) { + // Transfers ownership of the statement. + result->addStatement(stmt); + } + delete $1; } ; statement_list: - statement { $$ = new SQLParserResult($1); } - | statement_list ';' statement { $1->addStatement($3); $$ = $1; } + statement { $$ = new std::vector(); $$->push_back($1); } + | statement_list ';' statement { $1->push_back($3); $$ = $1; } ; statement: @@ -288,7 +287,11 @@ prepare_statement: | PREPARE IDENTIFIER '{' statement_list opt_semicolon '}' { $$ = new PrepareStatement(); $$->name = $2; - $$->query = $4; + $$->query = new SQLParserResult(); + for (SQLStatement* stmt : *$4) { + $$->query->addStatement(stmt); + } + delete $4; } ;