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");
}
SQLParserResult* SQLParser::parseSQLString(const char* text) {
SQLParserResult* result = NULL;
yyscan_t scanner;
YY_BUFFER_STATE state;
if (hsql_lex_init(&scanner)) {
// couldn't initialize
// Couldn't initialize the lexer.
fprintf(stderr, "[Error] SQLParser: Error when initializing lexer!\n");
return NULL;
}
state = hsql__scan_string(text, scanner);
// Parser and return early if it failed.
if (hsql_parse(&result, scanner)) {
// Returns an error stmt object
// Returns an error stmt object.
return result;
}

View File

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

View File

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

View File

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