2018-12-06 15:29:55 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-12-07 15:14:50 +00:00
|
|
|
#include <vector>
|
2018-12-17 16:20:15 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2018-12-06 15:29:55 +00:00
|
|
|
#include <Parsers/DumpASTNode.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-12-10 13:49:36 +00:00
|
|
|
/// Visits AST tree in depth, call functions for nodes according to Matcher type data.
|
2018-12-06 15:29:55 +00:00
|
|
|
/// You need to define Data, label, visit() and needChildVisit() in Matcher class.
|
2018-12-17 16:20:15 +00:00
|
|
|
template <typename Matcher, bool _top_to_bottom>
|
2018-12-06 15:29:55 +00:00
|
|
|
class InDepthNodeVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Data = typename Matcher::Data;
|
|
|
|
|
|
|
|
InDepthNodeVisitor(Data & data_, std::ostream * ostr_ = nullptr)
|
|
|
|
: data(data_),
|
|
|
|
visit_depth(0),
|
|
|
|
ostr(ostr_)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void visit(ASTPtr & ast)
|
|
|
|
{
|
|
|
|
DumpASTNode dump(*ast, ostr, visit_depth, Matcher::label);
|
|
|
|
|
2018-12-17 16:20:15 +00:00
|
|
|
if constexpr (!_top_to_bottom)
|
2018-12-06 15:29:55 +00:00
|
|
|
visitChildren(ast);
|
|
|
|
|
2018-12-07 14:24:47 +00:00
|
|
|
/// It operates with ASTPtr * cause we may want to rewrite ASTPtr in visit().
|
|
|
|
std::vector<ASTPtr *> additional_nodes = Matcher::visit(ast, data);
|
|
|
|
|
2018-12-06 15:29:55 +00:00
|
|
|
/// visit additional nodes (ex. only part of children)
|
2018-12-07 14:24:47 +00:00
|
|
|
for (ASTPtr * node : additional_nodes)
|
|
|
|
visit(*node);
|
2018-12-06 15:29:55 +00:00
|
|
|
|
2018-12-17 16:20:15 +00:00
|
|
|
if constexpr (_top_to_bottom)
|
2018-12-06 15:29:55 +00:00
|
|
|
visitChildren(ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-12-06 19:02:42 +00:00
|
|
|
Data & data;
|
2018-12-06 15:29:55 +00:00
|
|
|
size_t visit_depth;
|
|
|
|
std::ostream * ostr;
|
|
|
|
|
|
|
|
void visitChildren(ASTPtr & ast)
|
|
|
|
{
|
|
|
|
for (auto & child : ast->children)
|
|
|
|
if (Matcher::needChildVisit(ast, child))
|
|
|
|
visit(child);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-17 16:20:15 +00:00
|
|
|
/// Simple matcher for one node type without complex traversal logic.
|
2019-02-04 18:45:31 +00:00
|
|
|
template <typename _Data, bool _visit_children = true>
|
2018-12-17 16:20:15 +00:00
|
|
|
class OneTypeMatcher
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Data = _Data;
|
|
|
|
using TypeToVisit = typename Data::TypeToVisit;
|
|
|
|
|
|
|
|
static constexpr const char * label = "";
|
|
|
|
|
2019-02-04 18:45:31 +00:00
|
|
|
static bool needChildVisit(ASTPtr &, const ASTPtr &) { return _visit_children; }
|
2018-12-17 16:20:15 +00:00
|
|
|
|
|
|
|
static std::vector<ASTPtr *> visit(ASTPtr & ast, Data & data)
|
|
|
|
{
|
|
|
|
if (auto * t = typeid_cast<TypeToVisit *>(ast.get()))
|
|
|
|
data.visit(*t, ast);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Links two simple matches into resulting one. There's no complex traversal logic: all the children would be visited.
|
|
|
|
template <typename First, typename Second>
|
|
|
|
class LinkedMatcher
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Data = std::pair<typename First::Data, typename Second::Data>;
|
|
|
|
|
|
|
|
static constexpr const char * label = "";
|
|
|
|
|
|
|
|
static bool needChildVisit(ASTPtr &, const ASTPtr &) { return true; }
|
|
|
|
|
|
|
|
static std::vector<ASTPtr *> visit(ASTPtr & ast, Data & data)
|
|
|
|
{
|
|
|
|
First::visit(ast, data.first);
|
|
|
|
Second::visit(ast, data.second);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-06 15:29:55 +00:00
|
|
|
}
|