move initialization of SQLParserResult to SQLParser from bison_parser

This commit is contained in:
Pedro Flemming 2017-04-07 16:07:14 +02:00
parent 9e558c7d22
commit 074c564cc4
2 changed files with 22 additions and 18 deletions

View File

@ -13,20 +13,21 @@ namespace hsql {
} }
SQLParserResult* SQLParser::parseSQLString(const char* text) { SQLParserResult* SQLParser::parseSQLString(const char* text) {
SQLParserResult* result = NULL; SQLParserResult* result = new SQLParserResult();
yyscan_t scanner; yyscan_t scanner;
YY_BUFFER_STATE state; YY_BUFFER_STATE state;
if (hsql_lex_init(&scanner)) { if (hsql_lex_init(&scanner)) {
// Couldn't initialize the lexer. // Couldn't initialize the lexer.
fprintf(stderr, "[Error] SQLParser: Error when initializing lexer!\n"); fprintf(stderr, "[Error] SQLParser: Error when initializing lexer!\n");
delete result;
return NULL; return NULL;
} }
state = hsql__scan_string(text, scanner); state = hsql__scan_string(text, scanner);
// Parser and return early if it failed. // Parser and return early if it failed.
if (hsql_parse(&result, scanner)) { if (hsql_parse(result, scanner)) {
// Returns an error stmt object. // Returns an error stmt object.
hsql__delete_buffer(state, scanner); hsql__delete_buffer(state, scanner);
hsql_lex_destroy(scanner); hsql_lex_destroy(scanner);

View File

@ -18,14 +18,9 @@
using namespace hsql; using namespace hsql;
int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const char *msg) { int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const char *msg) {
delete *result; result->setIsValid(false);
result->setErrorDetails(strdup(msg), llocp->first_line, llocp->first_column);
SQLParserResult* list = new SQLParserResult();
list->setIsValid(false);
list->setErrorDetails(strdup(msg), llocp->first_line, llocp->first_column);
*result = list;
return 0; return 0;
} }
@ -90,7 +85,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch
%lex-param { yyscan_t scanner } %lex-param { yyscan_t scanner }
// Define additional parameters for yyparse // Define additional parameters for yyparse
%parse-param { hsql::SQLParserResult** result } %parse-param { hsql::SQLParserResult* result }
%parse-param { yyscan_t scanner } %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::GroupByDescription* group_t;
hsql::UpdateClause* update_t; hsql::UpdateClause* update_t;
hsql::SQLParserResult* stmt_list; std::vector<hsql::SQLStatement*>* stmt_vec;
std::vector<char*>* str_vec; std::vector<char*>* str_vec;
std::vector<hsql::TableRef*>* table_vec; std::vector<hsql::TableRef*>* table_vec;
@ -147,7 +142,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch
} }
} }
delete ($$); delete ($$);
} <str_vec> <table_vec> <column_vec> <update_vec> <expr_vec> <order_vec> } <str_vec> <table_vec> <column_vec> <update_vec> <expr_vec> <order_vec> <stmt_vec>
%destructor { 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) ** Non-Terminal types (http://www.gnu.org/software/bison/manual/html_node/Type-Decl.html)
*********************************/ *********************************/
%type <stmt_list> statement_list %type <stmt_vec> statement_list
%type <statement> statement preparable_statement %type <statement> statement preparable_statement
%type <exec_stmt> execute_statement %type <exec_stmt> execute_statement
%type <prep_stmt> prepare_statement %type <prep_stmt> prepare_statement
@ -243,14 +238,18 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch
// Defines our general input. // Defines our general input.
input: input:
statement_list opt_semicolon { statement_list opt_semicolon {
*result = $1; for (SQLStatement* stmt : *$1) {
// Transfers ownership of the statement.
result->addStatement(stmt);
}
delete $1;
} }
; ;
statement_list: statement_list:
statement { $$ = new SQLParserResult($1); } statement { $$ = new std::vector<SQLStatement*>(); $$->push_back($1); }
| statement_list ';' statement { $1->addStatement($3); $$ = $1; } | statement_list ';' statement { $1->push_back($3); $$ = $1; }
; ;
statement: statement:
@ -288,7 +287,11 @@ prepare_statement:
| PREPARE IDENTIFIER '{' statement_list opt_semicolon '}' { | PREPARE IDENTIFIER '{' statement_list opt_semicolon '}' {
$$ = new PrepareStatement(); $$ = new PrepareStatement();
$$->name = $2; $$->name = $2;
$$->query = $4; $$->query = new SQLParserResult();
for (SQLStatement* stmt : *$4) {
$$->query->addStatement(stmt);
}
delete $4;
} }
; ;