2015-10-19 01:44:00 +08:00
|
|
|
#ifndef MEMGRAPH_CYPHER_TREE_PRINT_HPP
|
|
|
|
#define MEMGRAPH_CYPHER_TREE_PRINT_HPP
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <stack>
|
|
|
|
|
2015-10-28 03:21:28 +08:00
|
|
|
#include "cypher/visitor/traverser.hpp"
|
2015-10-19 01:44:00 +08:00
|
|
|
|
2015-10-28 03:21:28 +08:00
|
|
|
class PrintVisitor : public Traverser
|
2015-10-19 01:44:00 +08:00
|
|
|
{
|
2015-10-28 03:21:28 +08:00
|
|
|
public:
|
2015-10-19 01:44:00 +08:00
|
|
|
class Printer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Printer(std::ostream& stream, const std::string& header)
|
|
|
|
: stream(stream)
|
|
|
|
{
|
|
|
|
stream << header;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Printer()
|
|
|
|
{
|
|
|
|
stream << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Entry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Entry(Printer& printer) : printer(printer), valid(true)
|
|
|
|
{
|
|
|
|
printer.level++;
|
|
|
|
|
|
|
|
for(size_t i = 1; i < printer.level; ++i)
|
|
|
|
printer.stream << "| ";
|
|
|
|
|
|
|
|
printer.stream << "+--";
|
|
|
|
}
|
|
|
|
|
|
|
|
Entry(const Entry&) = delete;
|
|
|
|
|
|
|
|
Entry(Entry&& other) : printer(other.printer), valid(true)
|
|
|
|
{
|
|
|
|
other.valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Entry()
|
|
|
|
{
|
|
|
|
if(valid)
|
|
|
|
printer.level--;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
friend Entry& operator<<(Entry& entry, const T& item)
|
|
|
|
{
|
|
|
|
entry.printer.stream << item;
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Printer& printer;
|
|
|
|
bool valid;
|
|
|
|
};
|
|
|
|
|
|
|
|
Entry advance()
|
|
|
|
{
|
|
|
|
stream << std::endl;
|
|
|
|
return std::move(Entry(*this));
|
|
|
|
}
|
|
|
|
|
|
|
|
Entry advance(const std::string& text)
|
|
|
|
{
|
|
|
|
stream << std::endl;
|
|
|
|
auto entry = Entry(*this);
|
|
|
|
entry << text;
|
|
|
|
return std::move(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::ostream& stream;
|
|
|
|
size_t level = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
PrintVisitor(std::ostream& stream)
|
|
|
|
: printer(stream, "Printing AST") {}
|
|
|
|
|
|
|
|
void visit(ast::Start& start) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Start");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(start);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::ReadQuery& read_query) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Read Query");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(read_query);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Match& match) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Match");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(match);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Pattern& pattern) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Pattern");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(pattern);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Node& node) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Node");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(node);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Identifier& idn) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance();
|
|
|
|
entry << "Identifier '" << idn.name << "'";
|
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Return& return_clause) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Return");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(return_clause);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Accessor& accessor) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Accessor");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(accessor);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Boolean& boolean) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance();
|
|
|
|
entry << "Boolean " << boolean.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Float& floating) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance();
|
|
|
|
entry << "Float " << floating.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Integer& integer) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance();
|
|
|
|
entry << "Integer " << integer.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::String& string) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance();
|
|
|
|
entry << "String " << string.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Property& property) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Property");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(property);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::And& and_expr) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("And");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(and_expr);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Or& or_expr) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Or");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(or_expr);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Lt& lt_expr) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Less Than");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(lt_expr);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Gt& gt_expr) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Greater Than");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(gt_expr);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Ge& ge_expr) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Greater od Equal");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(ge_expr);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Le& le_expr) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Less or Equal");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(le_expr);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Eq& eq_expr) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Equal");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(eq_expr);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Ne& ne_expr) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Not Equal");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(ne_expr);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Plus& plus) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Plus");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(plus);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Minus& minus) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Minus");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(minus);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Star& star) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Star");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(star);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Slash& slash) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Slash");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(slash);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Rem& rem) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Rem (%)");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(rem);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::PropertyList& prop_list) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Property List");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(prop_list);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::RelationshipList& rel_list) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Relationship List");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(rel_list);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Relationship& rel) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Relationship");
|
|
|
|
entry << " direction: " << rel.direction;
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(rel);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::RelationshipSpecs& rel_specs) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Relationship Specs");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(rel_specs);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::LabelList& labels) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Label List");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(labels);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::ReturnList& return_list) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Return List");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(return_list);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Where& where) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Where");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(where);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::WriteQuery& write_query) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Write Query");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(write_query);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visit(ast::Create& create) override
|
|
|
|
{
|
|
|
|
auto entry = printer.advance("Create");
|
2015-10-28 03:21:28 +08:00
|
|
|
Traverser::visit(create);
|
2015-10-19 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Printer printer;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|