small rewrite of QueryStripper, Lexer and CypherLexer

This commit is contained in:
Marko Budiselic 2016-02-06 13:45:45 +01:00
parent 499469aec2
commit a21fe854c2
6 changed files with 65 additions and 15 deletions

View File

@ -16,7 +16,7 @@ public:
auto parser = cypher::Parser();
auto tokenizer = lexer.tokenize(input);
auto tree = parser.parse(tokenizer);
return std::move(tree);
return tree;
}
private:

View File

@ -6,11 +6,10 @@
class CypherLexer : public Lexer
{
public:
CypherLexer()
{
// whitespace
rule("\\s+", sm.skip());
rule("\\s+", sm->skip());
// special characters
rule("\\.", TK_DOT);
@ -69,4 +68,6 @@ public:
build();
}
CypherLexer(CypherLexer& other) = delete;
CypherLexer(CypherLexer&& other) = default;
};

View File

@ -1,6 +1,7 @@
#pragma once
#include <cstdint>
#include <memory>
// unfortunatelly, lexertl uses some stuff deprecated in c++11 so we get some
// warnings during compile time, mainly for the auto_ptr
@ -17,6 +18,32 @@
class Lexer
{
public:
// public pointer declarations
using uptr = std::unique_ptr<Lexer>;
using sptr = std::shared_ptr<Lexer>;
// constructors
// default constructor creates unique pointers to object
// members
Lexer() :
rules(std::make_unique<lexertl::rules>()),
sm(std::make_unique<lexertl::state_machine>())
{
}
// copy constructor is deleted
Lexer(Lexer& other) = delete;
// move constructor has default implementation
Lexer(Lexer&& other) :
rules(std::move(other.rules)),
sm(std::move(other.sm))
{
}
// TODO take care of concurrnecy and moving the lexer object when
// some Tokenizer already uses the it (right now I'm not
// sure what is going to happen)
// check this ASAP
class Tokenizer
{
public:
@ -25,7 +52,7 @@ public:
Token lookup()
{
lexertl::lookup(lexer.sm, results);
lexertl::lookup(*lexer.sm, results);
auto token = Token {results.id, results.str()};
if(results.id == static_cast<decltype(results.id)>(-1))
@ -46,15 +73,19 @@ public:
void build()
{
lexertl::generator::build(rules, sm);
lexertl::generator::build(*rules, *sm);
}
void rule(const std::string& regex, uint64_t id)
{
rules.push(regex, id);
rules->push(regex, id);
}
protected:
lexertl::rules rules;
lexertl::state_machine sm;
using uptr_lexertl_rules = std::unique_ptr<lexertl::rules>;
using uptr_lexertl_sm = std::unique_ptr<lexertl::state_machine>;
uptr_lexertl_rules rules;
uptr_lexertl_sm sm;
};

BIN
query_engine/engine Executable file

Binary file not shown.

View File

@ -22,11 +22,13 @@ class QueryEngine
{
public:
QueryEngine()
: stripper(make_query_stripper(TK_INT, TK_FLOAT, TK_STR))
{
}
QueryResult execute(const std::string& query)
{
cout << "QUERY ENGINE EXECUTE" << endl;
auto stripped = stripper.strip(query);
cout << "STRIPPED: " << stripped << endl;
auto stripped_hash = fnv(stripped);
@ -39,7 +41,7 @@ public:
private:
// TODO: use IoC or something similar
QueryStripper<int, int, int> stripper{TK_INT, TK_FLOAT, TK_STR};
QueryStripper<int, int, int> stripper;
QueryTraverser traverser;
CodeGenerator generator;
CodeCompiler compiler;

View File

@ -8,17 +8,28 @@
#include "cypher/tokenizer/cypher_lexer.hpp"
#include "utils/variadic/variadic.hpp"
#include <iostream>
template<typename ...Ts>
class QueryStripper
{
public:
QueryStripper(Ts&&... strip_types)
: strip_types(std::make_tuple(std::forward<Ts>(strip_types)...)) {}
std::string strip(const std::string& query)
QueryStripper(Ts&&... strip_types) :
lexer(std::make_unique<CypherLexer>()),
strip_types(std::make_tuple(std::forward<Ts>(strip_types)...))
{
auto tokenizer = lexer.tokenize(query);
}
QueryStripper(QueryStripper& other) = delete;
QueryStripper(QueryStripper&& other) :
strip_types(std::move(other.strip_types)),
lexer(std::move(other.lexer))
{
}
decltype(auto) strip(const std::string& query)
{
auto tokenizer = lexer->tokenize(query);
std::string stripped = "";
int counter = 0;
constexpr auto size = std::tuple_size<decltype(strip_types)>::value;
@ -35,7 +46,7 @@ public:
private:
std::tuple<Ts...> strip_types;
CypherLexer lexer;
CypherLexer::uptr lexer;
template<typename Value, typename Tuple, std::size_t ...index>
bool _or(Value&& value, Tuple&& tuple, std::index_sequence<index...>)
@ -44,3 +55,8 @@ private:
std::get<index>(std::forward<Tuple>(tuple))...);
}
};
template<typename ...Ts>
decltype(auto) make_query_stripper(Ts&&... ts) {
return QueryStripper<Ts...>(std::forward<Ts>(ts)...);
}