changes here and there
This commit is contained in:
349
parser.h
349
parser.h
@@ -6,223 +6,276 @@
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace usql {
|
||||
|
||||
enum class ColumnType {
|
||||
|
||||
enum class ColumnType {
|
||||
integer_type,
|
||||
float_type,
|
||||
varchar_type
|
||||
};
|
||||
};
|
||||
|
||||
enum class NodeType {
|
||||
true_node,
|
||||
int_value,
|
||||
float_value,
|
||||
string_value,
|
||||
database_value,
|
||||
logical_operator,
|
||||
relational_operator,
|
||||
arithmetical_operator,
|
||||
enum class NodeType {
|
||||
true_node,
|
||||
int_value,
|
||||
float_value,
|
||||
string_value,
|
||||
database_value,
|
||||
logical_operator,
|
||||
relational_operator,
|
||||
arithmetical_operator,
|
||||
create_table,
|
||||
insert_into,
|
||||
select_from,
|
||||
delete_from,
|
||||
update_table,
|
||||
column_name,
|
||||
delete_from,
|
||||
update_table,
|
||||
load_table,
|
||||
column_name,
|
||||
column_value,
|
||||
column_def,
|
||||
not_implemented_yet,
|
||||
error
|
||||
};
|
||||
};
|
||||
|
||||
struct Node {
|
||||
struct Node {
|
||||
NodeType node_type;
|
||||
|
||||
Node(const NodeType type) : node_type(type) {}
|
||||
};
|
||||
};
|
||||
|
||||
struct ColNameNode : Node {
|
||||
std::string name;
|
||||
struct ColNameNode : Node {
|
||||
std::string name;
|
||||
|
||||
ColNameNode(const std::string col_name) :
|
||||
Node(NodeType::column_name), name(col_name) {}
|
||||
};
|
||||
ColNameNode(const std::string col_name) :
|
||||
Node(NodeType::column_name), name(col_name) {}
|
||||
};
|
||||
|
||||
struct ColValueNode : Node {
|
||||
std::string value;
|
||||
struct ColValueNode : Node {
|
||||
std::string value;
|
||||
|
||||
ColValueNode(const std::string col_value) :
|
||||
Node(NodeType::column_value), value(col_value) {}
|
||||
};
|
||||
ColValueNode(const std::string col_value) :
|
||||
Node(NodeType::column_value), value(col_value) {}
|
||||
};
|
||||
|
||||
// TODO add order in row
|
||||
struct ColDefNode : Node {
|
||||
std::string name;
|
||||
ColumnType type;
|
||||
int order;
|
||||
int length;
|
||||
bool null;
|
||||
// TODO add order in row
|
||||
struct ColDefNode : Node {
|
||||
std::string name;
|
||||
ColumnType type;
|
||||
int order;
|
||||
int length;
|
||||
bool null;
|
||||
|
||||
ColDefNode(const std::string col_name, const ColumnType col_type, int col_order, int col_len, bool nullable) :
|
||||
Node(NodeType::column_def), name(col_name), type(col_type), order(col_order), length(col_len), null(nullable) {}
|
||||
};
|
||||
Node(NodeType::column_def), name(col_name), type(col_type), order(col_order), length(col_len),
|
||||
null(nullable) {}
|
||||
};
|
||||
|
||||
|
||||
struct TrueNode : Node {
|
||||
TrueNode() : Node(NodeType::true_node) {}
|
||||
};
|
||||
|
||||
struct TrueNode : Node {
|
||||
TrueNode() : Node(NodeType::true_node) {}
|
||||
};
|
||||
struct ValueNode : Node {
|
||||
ValueNode(NodeType type) : Node(type) {}
|
||||
|
||||
struct IntValueNode : Node {
|
||||
int value;
|
||||
virtual int getIntValue() = 0;
|
||||
|
||||
IntValueNode(int value) : Node(NodeType::int_value), value(value) {}
|
||||
};
|
||||
virtual double getDoubleValue() = 0;
|
||||
|
||||
struct FloatValueNode : Node {
|
||||
double value;
|
||||
virtual std::string getStringValue() = 0;
|
||||
|
||||
FloatValueNode(double value) : Node(NodeType::float_value), value(value) {}
|
||||
};
|
||||
virtual ~ValueNode() {};
|
||||
};
|
||||
|
||||
struct StringValueNode : Node {
|
||||
std::string value;
|
||||
struct IntValueNode : ValueNode {
|
||||
int value;
|
||||
|
||||
StringValueNode(std::string value) : Node(NodeType::string_value), value(value) {}
|
||||
};
|
||||
IntValueNode(int value) : ValueNode(NodeType::int_value), value(value) {}
|
||||
|
||||
struct DatabaseValueNode : Node {
|
||||
std::string col_name;
|
||||
int getIntValue() { return value; };
|
||||
|
||||
DatabaseValueNode(std::string name) : Node(NodeType::database_value), col_name(name) {}
|
||||
};
|
||||
double getDoubleValue() { return (double) value; };
|
||||
|
||||
enum class LogicalOperatorType {
|
||||
and_operator,
|
||||
or_operator,
|
||||
not_operator
|
||||
};
|
||||
std::string getStringValue() { return std::to_string(value); }
|
||||
};
|
||||
|
||||
struct LogicalOperatorNode : Node {
|
||||
LogicalOperatorType op;
|
||||
std::unique_ptr<Node> left;
|
||||
std::unique_ptr<Node> right;
|
||||
struct FloatValueNode : ValueNode {
|
||||
double value;
|
||||
|
||||
LogicalOperatorNode(LogicalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
|
||||
Node(NodeType::logical_operator), op(op), left(std::move(left)), right(std::move(right)) {};
|
||||
};
|
||||
FloatValueNode(double value) : ValueNode(NodeType::float_value), value(value) {}
|
||||
|
||||
enum class RelationalOperatorType {
|
||||
equal,
|
||||
greater,
|
||||
greater_equal,
|
||||
lesser,
|
||||
lesser_equal,
|
||||
not_equal
|
||||
// like
|
||||
};
|
||||
int getIntValue() { return (int) value; };
|
||||
|
||||
struct RelationalOperatorNode : Node {
|
||||
RelationalOperatorType op;
|
||||
double getDoubleValue() { return value; };
|
||||
|
||||
std::unique_ptr<Node> left;
|
||||
std::unique_ptr<Node> right;
|
||||
std::string getStringValue() { return std::to_string(value); }
|
||||
};
|
||||
|
||||
RelationalOperatorNode(RelationalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
|
||||
Node(NodeType::relational_operator), op(op), left(std::move(left)), right(std::move(right)) {};
|
||||
};
|
||||
struct StringValueNode : ValueNode {
|
||||
std::string value;
|
||||
|
||||
enum class ArithmeticalOperatorType {
|
||||
copy_value, // just copy lef value and do nothing with it
|
||||
plus_operator,
|
||||
minus_operator,
|
||||
multiply_operator,
|
||||
divide_operator
|
||||
};
|
||||
StringValueNode(std::string value) : ValueNode(NodeType::string_value), value(value) {}
|
||||
|
||||
struct ArithmeticalOperatorNode : Node {
|
||||
ArithmeticalOperatorType op;
|
||||
int getIntValue() { return std::stoi(value); };
|
||||
|
||||
std::unique_ptr<Node> left;
|
||||
std::unique_ptr<Node> right;
|
||||
double getDoubleValue() { return std::stod(value); };
|
||||
|
||||
ArithmeticalOperatorNode(ArithmeticalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
|
||||
Node(NodeType::arithmetical_operator), op(op), left(std::move(left)), right(std::move(right)) {};
|
||||
};
|
||||
std::string getStringValue() { return value; };
|
||||
};
|
||||
|
||||
struct DatabaseValueNode : Node {
|
||||
std::string col_name;
|
||||
|
||||
DatabaseValueNode(std::string name) : Node(NodeType::database_value), col_name(name) {}
|
||||
};
|
||||
|
||||
enum class LogicalOperatorType {
|
||||
and_operator,
|
||||
or_operator,
|
||||
not_operator
|
||||
};
|
||||
|
||||
struct LogicalOperatorNode : Node {
|
||||
LogicalOperatorType op;
|
||||
std::unique_ptr<Node> left;
|
||||
std::unique_ptr<Node> right;
|
||||
|
||||
LogicalOperatorNode(LogicalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
|
||||
Node(NodeType::logical_operator), op(op), left(std::move(left)), right(std::move(right)) {};
|
||||
};
|
||||
|
||||
enum class RelationalOperatorType {
|
||||
equal,
|
||||
greater,
|
||||
greater_equal,
|
||||
lesser,
|
||||
lesser_equal,
|
||||
not_equal
|
||||
// like
|
||||
};
|
||||
|
||||
struct RelationalOperatorNode : Node {
|
||||
RelationalOperatorType op;
|
||||
|
||||
std::unique_ptr<Node> left;
|
||||
std::unique_ptr<Node> right;
|
||||
|
||||
RelationalOperatorNode(RelationalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
|
||||
Node(NodeType::relational_operator), op(op), left(std::move(left)), right(std::move(right)) {};
|
||||
};
|
||||
|
||||
enum class ArithmeticalOperatorType {
|
||||
copy_value, // just copy lef value and do nothing with it
|
||||
plus_operator,
|
||||
minus_operator,
|
||||
multiply_operator,
|
||||
divide_operator
|
||||
};
|
||||
|
||||
struct ArithmeticalOperatorNode : Node {
|
||||
ArithmeticalOperatorType op;
|
||||
|
||||
std::unique_ptr<Node> left;
|
||||
std::unique_ptr<Node> right;
|
||||
|
||||
ArithmeticalOperatorNode(ArithmeticalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
|
||||
Node(NodeType::arithmetical_operator), op(op), left(std::move(left)), right(std::move(right)) {};
|
||||
};
|
||||
|
||||
|
||||
struct CreateTableNode : Node {
|
||||
struct CreateTableNode : Node {
|
||||
std::string table_name;
|
||||
std::vector<ColDefNode> cols_defs;
|
||||
|
||||
CreateTableNode(const std::string name, std::vector<ColDefNode> defs) :
|
||||
Node(NodeType::create_table), table_name(name), cols_defs(defs) {}
|
||||
};
|
||||
Node(NodeType::create_table), table_name(name), cols_defs(defs) {}
|
||||
};
|
||||
|
||||
struct InsertIntoTableNode : Node {
|
||||
std::string table_name;
|
||||
std::vector<ColNameNode> cols_names;
|
||||
std::vector<ColValueNode> cols_values;
|
||||
struct InsertIntoTableNode : Node {
|
||||
std::string table_name;
|
||||
std::vector<ColNameNode> cols_names;
|
||||
std::vector<ColValueNode> cols_values;
|
||||
|
||||
InsertIntoTableNode(const std::string name, std::vector<ColNameNode> names, std::vector<ColValueNode> values) :
|
||||
Node(NodeType::insert_into), table_name(name), cols_names(names), cols_values(values) {}
|
||||
};
|
||||
InsertIntoTableNode(const std::string name, std::vector<ColNameNode> names, std::vector<ColValueNode> values) :
|
||||
Node(NodeType::insert_into), table_name(name), cols_names(names), cols_values(values) {}
|
||||
};
|
||||
|
||||
struct SelectFromTableNode : Node {
|
||||
std::string table_name;
|
||||
std::vector<ColNameNode> cols_names;
|
||||
std::unique_ptr<Node> where;
|
||||
struct SelectFromTableNode : Node {
|
||||
std::string table_name;
|
||||
std::vector<ColNameNode> cols_names;
|
||||
std::unique_ptr<Node> where;
|
||||
|
||||
SelectFromTableNode(std::string name, std::vector<ColNameNode> names, std::unique_ptr<Node> where_clause) :
|
||||
Node(NodeType::select_from), table_name(name), cols_names(names), where(std::move(where_clause)) {}
|
||||
};
|
||||
SelectFromTableNode(std::string name, std::vector<ColNameNode> names, std::unique_ptr<Node> where_clause) :
|
||||
Node(NodeType::select_from), table_name(name), cols_names(names), where(std::move(where_clause)) {}
|
||||
};
|
||||
|
||||
struct UpdateTableNode : Node {
|
||||
std::string table_name;
|
||||
std::vector<ColNameNode> cols_names;
|
||||
std::vector<std::unique_ptr<Node>> values;
|
||||
std::unique_ptr<Node> where;
|
||||
struct UpdateTableNode : Node {
|
||||
std::string table_name;
|
||||
std::vector<ColNameNode> cols_names;
|
||||
std::vector<std::unique_ptr<Node>> values;
|
||||
std::unique_ptr<Node> where;
|
||||
|
||||
UpdateTableNode(std::string name, std::vector<ColNameNode> names, std::vector<std::unique_ptr<Node>> vals,
|
||||
std::unique_ptr<Node> where_clause) :
|
||||
Node(NodeType::update_table), table_name(name), cols_names(names), values(std::move(vals)), where(std::move(where_clause)) {}
|
||||
};
|
||||
UpdateTableNode(std::string name, std::vector<ColNameNode> names, std::vector<std::unique_ptr<Node>> vals,
|
||||
std::unique_ptr<Node> where_clause) :
|
||||
Node(NodeType::update_table), table_name(name), cols_names(names), values(std::move(vals)),
|
||||
where(std::move(where_clause)) {}
|
||||
};
|
||||
|
||||
struct DeleteFromTableNode : Node {
|
||||
std::string table_name;
|
||||
std::unique_ptr<Node> where;
|
||||
struct LoadIntoTableNode : Node {
|
||||
std::string table_name;
|
||||
std::string filename;
|
||||
|
||||
DeleteFromTableNode(const std::string name, std::unique_ptr<Node> where_clause) :
|
||||
Node(NodeType::delete_from), table_name(name), where(std::move(where_clause)) {}
|
||||
LoadIntoTableNode(const std::string name, std::string file) :
|
||||
Node(NodeType::load_table), table_name(name), filename(file) {}
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
struct DeleteFromTableNode : Node {
|
||||
std::string table_name;
|
||||
std::unique_ptr<Node> where;
|
||||
|
||||
DeleteFromTableNode(const std::string name, std::unique_ptr<Node> where_clause) :
|
||||
Node(NodeType::delete_from), table_name(name), where(std::move(where_clause)) {}
|
||||
|
||||
};
|
||||
|
||||
|
||||
class Parser {
|
||||
private:
|
||||
|
||||
|
||||
class Parser {
|
||||
private:
|
||||
|
||||
public:
|
||||
public:
|
||||
Parser();
|
||||
|
||||
std::unique_ptr<Node> parse(const std::string &code);
|
||||
|
||||
private:
|
||||
std::unique_ptr<Node> parse_create_table();
|
||||
std::unique_ptr<Node> parse_insert_into_table();
|
||||
std::unique_ptr<Node> parse_select_from_table();
|
||||
std::unique_ptr<Node> parse_delete_from_table();
|
||||
std::unique_ptr<Node> parse_update_table();
|
||||
private:
|
||||
std::unique_ptr<Node> parse_create_table();
|
||||
|
||||
std::unique_ptr<Node> parse_where_clause();
|
||||
std::unique_ptr<Node> parse_operand_node();
|
||||
RelationalOperatorType parse_relational_operator();
|
||||
LogicalOperatorType parse_logical_operator();
|
||||
ArithmeticalOperatorType parse_arithmetical_operator();
|
||||
std::unique_ptr<Node> parse_insert_into_table();
|
||||
|
||||
private:
|
||||
std::unique_ptr<Node> parse_select_from_table();
|
||||
|
||||
std::unique_ptr<Node> parse_delete_from_table();
|
||||
|
||||
std::unique_ptr<Node> parse_update_table();
|
||||
|
||||
std::unique_ptr<Node> parse_load_table();
|
||||
|
||||
std::unique_ptr<Node> parse_where_clause();
|
||||
|
||||
std::unique_ptr<Node> parse_operand_node();
|
||||
|
||||
RelationalOperatorType parse_relational_operator();
|
||||
|
||||
LogicalOperatorType parse_logical_operator();
|
||||
|
||||
ArithmeticalOperatorType parse_arithmetical_operator();
|
||||
|
||||
private:
|
||||
Lexer lexer;
|
||||
|
||||
std::unique_ptr<Node> parse_relational_expression();
|
||||
std::unique_ptr<Node> parse_relational_expression();
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user