HyriseSQLParser/src/SQLParser.cpp

63 lines
1.4 KiB
C++
Raw Normal View History

2014-10-09 01:30:22 +02:00
#include "SQLParser.h"
#include "parser/bison_parser.h"
#include "parser/flex_lexer.h"
2014-10-09 01:30:22 +02:00
#include <stdio.h>
#include <string>
2014-10-09 01:30:22 +02:00
namespace hsql {
SQLParser::SQLParser() {
fprintf(stderr, "SQLParser only has static methods atm! Do not initialize!\n");
}
2014-10-09 01:30:22 +02:00
// static
bool SQLParser::parseSQLString(const char* text, SQLParserResult* result) {
yyscan_t scanner;
YY_BUFFER_STATE state;
2014-10-09 01:30:22 +02:00
if (hsql_lex_init(&scanner)) {
// Couldn't initialize the lexer.
fprintf(stderr, "[Error] SQLParser: Error when initializing lexer!\n");
return false;
}
2014-10-09 01:30:22 +02:00
state = hsql__scan_string(text, scanner);
2014-10-09 01:30:22 +02:00
// Parse the tokens.
// If parsing fails, the result will contain an error object.
int ret = hsql_parse(result, scanner);
bool success = (ret == 0);
result->setIsValid(success);
2014-10-09 01:30:22 +02:00
hsql__delete_buffer(state, scanner);
hsql_lex_destroy(scanner);
return true;
}
2015-01-07 13:24:39 +01:00
// static
bool SQLParser::parseSQLString(const std::string& text, SQLParserResult* result) {
return parseSQLString(text.c_str(), result);
}
2016-02-27 14:45:59 +01:00
// static
SQLParserResult* SQLParser::parseSQLString(const char* text) {
SQLParserResult* result = new SQLParserResult();
if (!SQLParser::parseSQLString(text, result)) {
delete result;
return NULL;
}
return result;
}
// static
SQLParserResult* SQLParser::parseSQLString(const std::string& text) {
return parseSQLString(text.c_str());
}
2016-02-27 15:01:06 +01:00
} // namespace hsql