memgraph/cypher/parser.cpp

60 lines
1.7 KiB
C++
Raw Normal View History

#include <cstdlib>
#include <vector>
#include <vector>
2015-10-19 01:44:00 +08:00
#include "compiler.hpp"
2015-10-30 08:24:01 +08:00
#include "debug/tree_print.hpp"
#include "codegen/cppgen.hpp"
#include "utils/command_line/arguments.hpp"
#include "utils/string/filereader.hpp"
2015-10-30 08:24:01 +08:00
using std::cout;
using std::endl;
2015-10-30 08:24:01 +08:00
// * QUERY EXAMPLES *
// "CREATE (n { name: 'Dominik', age: 24, role: 'CEO' }) return n"
// "MATCH (user:User { name: 'Dominik', age: 8 + 4})-[has:HAS|IS|CAN { duration: 'PERMANENT'}]->(item:Item)--(shop)"
// "MATCH (user:User { name: 'Dominik', age: 24})-[has:HAS]->(item:Item) WHERE item.name = 'XPS 13' AND item.price = 11999.99 RETURN user, has, item"
2015-10-30 08:24:01 +08:00
// * INPUT ARGUMENTS *
// -q -> query
// -v -> visitor
// -f -> file
std::string extract_query(const vector_str& arguments)
{
if (contain_argument(arguments, "-q"))
return get_argument(arguments, "-q", "CREATE (n {a:1, b:2}) RETURN n");
auto default_file = "query/read/match/match-where.cypher";
auto file = get_argument(arguments, "-f", default_file);
// TODO: error handling
return read_file(file.c_str());
}
2015-10-30 08:24:01 +08:00
int main(int argc, char *argv[])
{
// arguments parsing
auto arguments = all_arguments(argc, argv);
// query extraction
auto cypher_query = extract_query(arguments);
2015-10-30 08:24:01 +08:00
// traversers
auto traverser = get_argument(arguments, "-t", "code");
2015-10-30 08:24:01 +08:00
auto print_traverser = Traverser::sptr(new PrintVisitor(cout));
auto cppgen_traverser = Traverser::sptr(new CppGen());
std::map<std::string, Traverser::sptr> traversers = {
{"print", print_traverser},
{"code", cppgen_traverser}
};
2015-10-30 08:24:01 +08:00
cypher::Compiler compiler;
auto tree = compiler.syntax_tree(cypher_query);
2015-10-19 01:44:00 +08:00
2015-10-30 08:24:01 +08:00
auto t = traversers[traverser];
tree.root->accept(*t);
return 0;
}