diff --git a/cypher/compiler.hpp b/cypher/compiler.hpp index c0d22b6ba..cd7e860c8 100644 --- a/cypher/compiler.hpp +++ b/cypher/compiler.hpp @@ -1,6 +1,6 @@ #pragma once -#include "cypher_lexer.hpp" +#include "tokenizer/cypher_lexer.hpp" #include "parser.hpp" namespace cypher diff --git a/cypher/parser.hpp b/cypher/parser.hpp index 3586079fd..9455dffd2 100644 --- a/cypher/parser.hpp +++ b/cypher/parser.hpp @@ -2,9 +2,8 @@ #include "cypher.h" #include "token.hpp" - -#include "cypher_lexer.hpp" #include "ast/tree.hpp" +#include "tokenizer/cypher_lexer.hpp" void* cypher_parserAlloc(void* (*allocProc)(size_t)); void cypher_parser(void*, int, Token*, ast::Ast* ast); diff --git a/cypher/token.hpp b/cypher/token.hpp index 3a3b30b7e..567187c05 100644 --- a/cypher/token.hpp +++ b/cypher/token.hpp @@ -10,6 +10,26 @@ struct Token unsigned long id; std::string value; + /* + * Token is "True" if it's id is bigger than zero. Because + * lexer ids are all bigger than zero. + * + * This object could be used in while loop as a condition. + * E.g.: + * while (auto token = ...) + * { + * } + */ + operator bool() const + { + return id > 0; + } + + /* + * Ostream operator + * + * Prints token id and value in single line. + */ friend std::ostream& operator<<(std::ostream& stream, const Token& token) { return stream << "TOKEN id = " << token.id diff --git a/cypher/cypher_lexer.hpp b/cypher/tokenizer/cypher_lexer.hpp similarity index 98% rename from cypher/cypher_lexer.hpp rename to cypher/tokenizer/cypher_lexer.hpp index 4e92b8ef1..cff8f6167 100644 --- a/cypher/cypher_lexer.hpp +++ b/cypher/tokenizer/cypher_lexer.hpp @@ -1,7 +1,6 @@ #pragma once -#include "cypher.h" - +#include "cypher/cypher.h" #include "lexer.hpp" class CypherLexer : public Lexer diff --git a/cypher/lexer.hpp b/cypher/tokenizer/lexer.hpp similarity index 89% rename from cypher/lexer.hpp rename to cypher/tokenizer/lexer.hpp index 82481f470..00f5fa8ac 100644 --- a/cypher/lexer.hpp +++ b/cypher/tokenizer/lexer.hpp @@ -7,12 +7,12 @@ // auto_ptr > is deprecated #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" -#include "lexertl/lexertl/generator.hpp" -#include "lexertl/lexertl/lookup.hpp" +#include "cypher/lexertl/lexertl/generator.hpp" +#include "cypher/lexertl/lexertl/lookup.hpp" #pragma GCC diagnostic pop -#include "errors.hpp" -#include "token.hpp" +#include "cypher/errors.hpp" +#include "cypher/token.hpp" class Lexer { diff --git a/query_engine/compile.sh b/query_engine/compile.sh index 2451f942a..4a0d99391 100755 --- a/query_engine/compile.sh +++ b/query_engine/compile.sh @@ -1,4 +1,4 @@ #!/bin/bash # TODO: create Makefile or cmake script -clang++ -std=c++1y -I../ main.cpp ../cypher/cypher.cpp -o engine +clang++ -std=c++1y -g -I../ main.cpp ../cypher/cypher.cpp -o engine diff --git a/query_engine/engine b/query_engine/engine index aab2a5edd..361ca1c2a 100755 Binary files a/query_engine/engine and b/query_engine/engine differ diff --git a/query_engine/query_engine.hpp b/query_engine/query_engine.hpp index 786e05187..a0a99c019 100644 --- a/query_engine/query_engine.hpp +++ b/query_engine/query_engine.hpp @@ -2,11 +2,13 @@ #include +#include "query_stripper.hpp" #include "query_traverser.hpp" #include "code_generator.hpp" #include "code_compiler.hpp" #include "query_executor.hpp" #include "query_result.hpp" +#include "utils/hashing/fnv.hpp" using std::cout; using std::endl; @@ -21,13 +23,19 @@ class QueryEngine public: QueryResult execute(const std::string& query) { - traverser.build_tree(query); - traverser.traverse(); + auto stripped = stripper.strip(query); + cout << "STRIPPED: " << stripped << endl; + auto stripped_hash = fnv(stripped); + cout << "STRIPPED HASH: " << stripped_hash << endl; + + // traverser.build_tree(query); + // traverser.traverse(); return QueryResult(); } private: // TODO: use IoC or something similar + QueryStripper stripper; QueryTraverser traverser; CodeGenerator generator; CodeCompiler compiler; diff --git a/query_engine/query_stripper.hpp b/query_engine/query_stripper.hpp new file mode 100644 index 000000000..155a48653 --- /dev/null +++ b/query_engine/query_stripper.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include +#include + +#include "cypher/tokenizer/cypher_lexer.hpp" +#include "cypher/cypher.h" + +class QueryStripper +{ +public: + + // TODO: extract parameters + + std::string strip(const std::string& query) + { + auto tokenizer = lexer.tokenize(query); + std::string stripped = ""; + int counter = 0; + while (auto token = tokenizer.lookup()) + { + // TODO: do this more generic via template metaprogramming + if (token.id == TK_STR || token.id == TK_INT || + token.id == TK_FLOAT) { + stripped += "@" + std::to_string(counter++); + } else { + stripped += token.value; + } + } + return stripped; + } + +private: + CypherLexer lexer; +};