add documentation to parser and parsing result

This commit is contained in:
Pedro 2017-02-08 02:33:42 +01:00
parent 28214e8043
commit 5cf62f6b1d
4 changed files with 31 additions and 21 deletions

View File

@ -12,22 +12,22 @@ namespace hsql {
fprintf(stderr, "SQLParser only has static methods atm! Do not initialize!\n"); fprintf(stderr, "SQLParser only has static methods atm! Do not initialize!\n");
} }
SQLParserResult* SQLParser::parseSQLString(const char* text) { SQLParserResult* SQLParser::parseSQLString(const char* text) {
SQLParserResult* result = NULL; SQLParserResult* result = NULL;
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 // Couldn't initialize the lexer.
fprintf(stderr, "[Error] SQLParser: Error when initializing lexer!\n"); fprintf(stderr, "[Error] SQLParser: Error when initializing lexer!\n");
return NULL; return NULL;
} }
state = hsql__scan_string(text, scanner); 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 // Returns an error stmt object.
return result; return result;
} }

View File

@ -5,15 +5,18 @@
#include "sql/statements.h" #include "sql/statements.h"
namespace hsql { namespace hsql {
/**
* Main class for parsing SQL strings // Static methods used to parse SQL strings.
*/
class SQLParser { class SQLParser {
public: public:
// Parses a given constant character SQL string.
static SQLParserResult* parseSQLString(const char* sql); static SQLParserResult* parseSQLString(const char* sql);
// Parses an SQL std::string.
static SQLParserResult* parseSQLString(const std::string& sql); static SQLParserResult* parseSQLString(const std::string& sql);
private: private:
// Static class can't be instatiated.
SQLParser(); SQLParser();
}; };

View File

@ -7,14 +7,12 @@ namespace hsql {
isValid(true), isValid(true),
errorMsg(NULL) {}; errorMsg(NULL) {};
SQLParserResult::SQLParserResult(SQLStatement* stmt) : SQLParserResult::SQLParserResult(SQLStatement* stmt) :
isValid(true), isValid(true),
errorMsg(NULL) { errorMsg(NULL) {
addStatement(stmt); addStatement(stmt);
}; };
SQLParserResult::~SQLParserResult() { SQLParserResult::~SQLParserResult() {
for (std::vector<SQLStatement*>::iterator it = statements.begin(); it != statements.end(); ++it) { for (std::vector<SQLStatement*>::iterator it = statements.begin(); it != statements.end(); ++it) {
delete *it; delete *it;
@ -23,17 +21,14 @@ namespace hsql {
delete errorMsg; delete errorMsg;
} }
void SQLParserResult::addStatement(SQLStatement* stmt) { void SQLParserResult::addStatement(SQLStatement* stmt) {
statements.push_back(stmt); statements.push_back(stmt);
} }
SQLStatement* SQLParserResult::getStatement(int id) { SQLStatement* SQLParserResult::getStatement(int id) {
return statements[id]; return statements[id];
} }
size_t SQLParserResult::size() { size_t SQLParserResult::size() {
return statements.size(); return statements.size();
} }

View File

@ -4,29 +4,41 @@
#include "sql/SQLStatement.h" #include "sql/SQLStatement.h"
namespace hsql { namespace hsql {
/** // Represents the result of the SQLParser.
* Represents the result of the SQLParser. // If parsing was successful it contains a list of SQLStatement.
* If parsing was successful it contains a list of SQLStatement.
*/
class SQLParserResult { class SQLParserResult {
public: public:
// Initialize with empty statement list.
SQLParserResult(); SQLParserResult();
// Initialize with a single statement.
SQLParserResult(SQLStatement* stmt); SQLParserResult(SQLStatement* stmt);
// Deletes all statements in the resul.
virtual ~SQLParserResult(); virtual ~SQLParserResult();
void addStatement(SQLStatement* stmt); // Returns the number of statements in the result.
SQLStatement* getStatement(int id);
size_t size(); size_t size();
// public properties // Gets the SQL statement with the given index.
SQLStatement* getStatement(int id);
// Adds a statement to the result list of statements.
void addStatement(SQLStatement* stmt);
// List of statements within the result.
std::vector<SQLStatement*> statements; std::vector<SQLStatement*> statements;
// Flag indicating the parsing was successful.
bool isValid; bool isValid;
// Error message, if an error occurred.
const char* errorMsg; const char* errorMsg;
// Line number of the occurrance of the error in the query.
int errorLine; int errorLine;
// Column number of the occurrance of the error in the query.
int errorColumn; int errorColumn;
}; };