initial commit
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
#ifndef RSQL_PARSER_BINARY_CONDITION_NODE_HPP
|
||||
#define RSQL_PARSER_BINARY_CONDITION_NODE_HPP
|
||||
|
||||
#include "node.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
namespace matador::rsql {
|
||||
enum class binary_operator {
|
||||
EQUALS,
|
||||
NOT_EQUALS,
|
||||
GREATER_THAN,
|
||||
GREATER_THAN_OR_EQUAL,
|
||||
LESS_THAN,
|
||||
LESS_THAN_OR_EQUAL,
|
||||
};
|
||||
|
||||
static const std::map<std::string, binary_operator> binary_operators = {
|
||||
{ "==", binary_operator::EQUALS },
|
||||
{ "!=", binary_operator::NOT_EQUALS },
|
||||
{ ">", binary_operator::GREATER_THAN },
|
||||
{ "=gt*", binary_operator::GREATER_THAN },
|
||||
{ ">=", binary_operator::GREATER_THAN_OR_EQUAL },
|
||||
{ "=ge=", binary_operator::GREATER_THAN_OR_EQUAL },
|
||||
{ "<", binary_operator::LESS_THAN },
|
||||
{ "=lt=", binary_operator::LESS_THAN },
|
||||
{ "<=", binary_operator::LESS_THAN_OR_EQUAL },
|
||||
{ "=le=", binary_operator::LESS_THAN_OR_EQUAL }
|
||||
};
|
||||
|
||||
class node_visitor;
|
||||
class binary_condition_node final : public node {
|
||||
public:
|
||||
binary_condition_node(std::string field, binary_operator op, std::string value);
|
||||
|
||||
void accept(node_visitor& visitor) const override;
|
||||
|
||||
const std::string& field() const;
|
||||
binary_operator operand() const;
|
||||
const std::string& value() const;
|
||||
|
||||
private:
|
||||
std::string field_;
|
||||
binary_operator op_;
|
||||
std::string value_;
|
||||
};
|
||||
|
||||
};
|
||||
#endif //RSQL_PARSER_BINARY_CONDITION_NODE_HPP
|
||||
@@ -0,0 +1,38 @@
|
||||
#ifndef RSQL_PARSER_COLLECTION_CONDITION_NODE_HPP
|
||||
#define RSQL_PARSER_COLLECTION_CONDITION_NODE_HPP
|
||||
|
||||
#include "node.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace matador::rsql {
|
||||
enum class collection_operator {
|
||||
IN,
|
||||
OUT
|
||||
};
|
||||
|
||||
static const std::map<std::string, collection_operator> collection_operators = {
|
||||
{ "=in=", collection_operator::IN },
|
||||
{ "=out=", collection_operator::OUT }
|
||||
};
|
||||
|
||||
|
||||
class collection_condition_node final : public node {
|
||||
public:
|
||||
collection_condition_node(std::string field, collection_operator op, std::vector<std::string> value);
|
||||
|
||||
void accept(node_visitor& visitor) const override;
|
||||
|
||||
const std::string& field() const;
|
||||
collection_operator operand() const;
|
||||
const std::vector<std::string>& values() const;
|
||||
|
||||
private:
|
||||
std::string field_;
|
||||
collection_operator op_;
|
||||
std::vector<std::string> value_;
|
||||
};
|
||||
}
|
||||
#endif //RSQL_PARSER_COLLECTION_CONDITION_NODE_HPP
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef RSQL_PARSER_LEXER_HPP
|
||||
#define RSQL_PARSER_LEXER_HPP
|
||||
|
||||
#include "token.hpp"
|
||||
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace matador::rsql::lexer {
|
||||
std::vector<token> tokenize(const std::string& input);
|
||||
}
|
||||
#endif //RSQL_PARSER_LEXER_HPP
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef RSQL_PARSER_LOGICAL_NODE_HPP
|
||||
#define RSQL_PARSER_LOGICAL_NODE_HPP
|
||||
|
||||
#include "node.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace matador::rsql {
|
||||
enum class logical_operator {
|
||||
AND,
|
||||
OR,
|
||||
};
|
||||
|
||||
class node_visitor;
|
||||
|
||||
class logical_node final : public node {
|
||||
public:
|
||||
explicit logical_node(logical_operator op);
|
||||
void accept(node_visitor& visitor) const override;
|
||||
|
||||
const std::vector<std::shared_ptr<node>>& children() const;
|
||||
logical_operator operand() const;
|
||||
|
||||
private:
|
||||
friend class parser;
|
||||
logical_operator op_; // ";" for AND, "," for OR
|
||||
std::vector<std::shared_ptr<node>> children_;
|
||||
};
|
||||
|
||||
}
|
||||
#endif //RSQL_PARSER_LOGICAL_NODE_HPP
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef RSQL_PARSER_NODE_HPP
|
||||
#define RSQL_PARSER_NODE_HPP
|
||||
|
||||
namespace matador::rsql {
|
||||
class node_visitor;
|
||||
class node {
|
||||
public:
|
||||
virtual ~node() = default;
|
||||
virtual void accept(node_visitor& visitor) const = 0;
|
||||
};
|
||||
}
|
||||
#endif //RSQL_PARSER_NODE_HPP
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef RSQL_PARSER_NODE_VISITOR_HPP
|
||||
#define RSQL_PARSER_NODE_VISITOR_HPP
|
||||
|
||||
namespace matador::rsql {
|
||||
|
||||
class binary_condition_node;
|
||||
class collection_condition_node;
|
||||
class logical_node;
|
||||
|
||||
class node_visitor {
|
||||
public:
|
||||
virtual ~node_visitor() = default;
|
||||
virtual void visit(const binary_condition_node& node) = 0;
|
||||
virtual void visit(const collection_condition_node& node) = 0;
|
||||
virtual void visit(const logical_node& node) = 0;
|
||||
|
||||
};
|
||||
}
|
||||
#endif //RSQL_PARSER_NODE_VISITOR_HPP
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef RSQL_PARSER_PARSER_HPP
|
||||
#define RSQL_PARSER_PARSER_HPP
|
||||
|
||||
#include "token.hpp"
|
||||
#include "token_type.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace matador::rsql {
|
||||
class node;
|
||||
|
||||
class parser {
|
||||
public:
|
||||
explicit parser(std::vector<token> t);
|
||||
|
||||
std::shared_ptr<node> parse();
|
||||
|
||||
private:
|
||||
// OR level
|
||||
std::shared_ptr<node> parse_or_expression();
|
||||
|
||||
// AND level
|
||||
std::shared_ptr<node> parse_and_expression();
|
||||
|
||||
// Primary: parenthesis or condition
|
||||
std::shared_ptr<node> parse_primary();
|
||||
std::shared_ptr<node> parse_condition();
|
||||
|
||||
bool match(const std::vector<token_type>& types);
|
||||
|
||||
const token& previous() const;
|
||||
|
||||
private:
|
||||
std::vector<token> tokens_;
|
||||
size_t current_size_ = 0;
|
||||
};
|
||||
}
|
||||
#endif //RSQL_PARSER_PARSER_HPP
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef RSQL_PARSER_TOKEN_HPP
|
||||
#define RSQL_PARSER_TOKEN_HPP
|
||||
|
||||
#include "token_type.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::rsql {
|
||||
struct token {
|
||||
token_type type;
|
||||
std::string value;
|
||||
};
|
||||
}
|
||||
#endif //RSQL_PARSER_TOKEN_HPP
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef RSQL_PARSER_TOKEN_TYPE_HPP
|
||||
#define RSQL_PARSER_TOKEN_TYPE_HPP
|
||||
|
||||
namespace matador::rsql {
|
||||
enum class token_type {
|
||||
IDENTIFIER,
|
||||
OPERATOR,
|
||||
VALUE,
|
||||
LOGICAL_AND,
|
||||
LOGICAL_OR,
|
||||
OPEN_PAREN,
|
||||
CLOSE_PAREN,
|
||||
UNKNOWN
|
||||
};
|
||||
}
|
||||
|
||||
#endif //RSQL_PARSER_TOKEN_TYPE_HPP
|
||||
Reference in New Issue
Block a user