#pragma once #include "lexer.h" #include #include #include 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, create_table, insert_into, select_from, column_name, column_value, column_def, not_implemented_yet, error }; struct Node { NodeType node_type; Node(const NodeType type) : node_type(type) {} }; struct ColNameNode : Node { std::string name; ColNameNode(const std::string col_name) : Node(NodeType::column_name), name(col_name) {} }; struct ColValueNode : Node { std::string 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; 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) {} }; struct TrueNode : Node { TrueNode() : Node(NodeType::true_node) {} }; struct IntValueNode : Node { int value; IntValueNode(int value) : Node(NodeType::int_value), value(value) {} }; struct FloatValueNode : Node { double value; FloatValueNode(double value) : Node(NodeType::float_value), value(value) {} }; struct StringValueNode : Node { std::string value; StringValueNode(std::string value) : Node(NodeType::string_value), value(value) {} }; struct DatabaseValueNode : Node { std::string col_name; DatabaseValueNode(std::string name) : Node(NodeType::database_value), col_name(name) {} }; struct LogicalOperatorNode : Node { // and_operator, // or_operator, // not_operator, // and / or / not std::unique_ptr left; std::unique_ptr right; }; enum class RelationalOperatorType { equal, greater // =, !=, >, >=, <, <=, like }; struct RelationalOperatorNode : Node { RelationalOperatorType op; std::unique_ptr left; std::unique_ptr right; RelationalOperatorNode(RelationalOperatorType op, std::unique_ptr left, std::unique_ptr right) : Node(NodeType::relational_operator), op(op), left(std::move(left)), right(std::move(right)) {}; }; struct CreateTableNode : Node { std::string table_name; std::vector cols_defs; CreateTableNode(const std::string name, std::vector defs) : Node(NodeType::create_table), table_name(name), cols_defs(defs) {} }; struct InsertIntoTableNode : Node { std::string table_name; std::vector cols_names; std::vector cols_values; InsertIntoTableNode(const std::string name, std::vector names, std::vector values) : Node(NodeType::insert_into), table_name(name), cols_names(names), cols_values(values) {} }; struct SelectFromTableNode : Node { std::string table_name; std::vector cols_names; std::unique_ptr where; SelectFromTableNode(const std::string name, std::vector names, std::unique_ptr where_clause) : Node(NodeType::select_from), table_name(name), cols_names(names), where(std::move(where_clause)) {} }; struct UpdateTableNode : Node { }; struct DeleteFromTableNode : Node { }; class Parser { private: public: Parser(); std::unique_ptr parse(const std::string &code); private: std::unique_ptr parse_create_table(); std::unique_ptr parse_insert_into_table(); std::unique_ptr parse_select_from_table(); std::unique_ptr parse_where_clause(); std::unique_ptr parse_operand_node(); RelationalOperatorType parse_operator(); private: Lexer lexer; };