2018-12-06 15:29:55 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-02-22 13:33:56 +00:00
|
|
|
#include <typeinfo>
|
2018-12-07 15:14:50 +00:00
|
|
|
#include <vector>
|
2018-12-17 16:20:15 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2022-10-01 15:47:58 +00:00
|
|
|
#include <Common/checkStackSize.h>
|
2018-12-06 15:29:55 +00:00
|
|
|
#include <Parsers/DumpASTNode.h>
|
|
|
|
|
2022-10-01 15:47:58 +00:00
|
|
|
|
2018-12-06 15:29:55 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-12-10 13:49:36 +00:00
|
|
|
/// Visits AST tree in depth, call functions for nodes according to Matcher type data.
|
2019-02-22 13:33:56 +00:00
|
|
|
/// You need to define Data, visit() and needChildVisit() in Matcher class.
|
2021-09-14 11:13:07 +00:00
|
|
|
template <typename Matcher, bool _top_to_bottom, bool need_child_accept_data = false, typename T = ASTPtr>
|
2019-10-21 16:22:54 +00:00
|
|
|
class InDepthNodeVisitor
|
2018-12-06 15:29:55 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Data = typename Matcher::Data;
|
|
|
|
|
2021-08-18 21:54:55 +00:00
|
|
|
explicit InDepthNodeVisitor(Data & data_, WriteBuffer * ostr_ = nullptr)
|
2018-12-06 15:29:55 +00:00
|
|
|
: data(data_),
|
|
|
|
visit_depth(0),
|
|
|
|
ostr(ostr_)
|
|
|
|
{}
|
|
|
|
|
2019-07-23 19:49:15 +00:00
|
|
|
void visit(T & ast)
|
2018-12-06 15:29:55 +00:00
|
|
|
{
|
2022-10-01 15:47:58 +00:00
|
|
|
checkStackSize();
|
2019-02-22 13:33:56 +00:00
|
|
|
DumpASTNode dump(*ast, ostr, visit_depth, typeid(Matcher).name());
|
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);
|
|
|
|
|
2020-10-23 12:20:07 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Matcher::visit(ast, data);
|
|
|
|
}
|
|
|
|
catch (Exception & e)
|
|
|
|
{
|
|
|
|
e.addMessage("While processing {}", ast->formatForErrorMessage());
|
|
|
|
throw;
|
|
|
|
}
|
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;
|
2020-11-09 16:05:40 +00:00
|
|
|
WriteBuffer * ostr;
|
2018-12-06 15:29:55 +00:00
|
|
|
|
2019-07-23 19:49:15 +00:00
|
|
|
void visitChildren(T & ast)
|
2018-12-06 15:29:55 +00:00
|
|
|
{
|
|
|
|
for (auto & child : ast->children)
|
2021-09-14 11:13:07 +00:00
|
|
|
{
|
|
|
|
bool need_visit_child = false;
|
|
|
|
if constexpr (need_child_accept_data)
|
|
|
|
need_visit_child = Matcher::needChildVisit(ast, child, data);
|
|
|
|
else
|
|
|
|
need_visit_child = Matcher::needChildVisit(ast, child);
|
|
|
|
|
|
|
|
if (need_visit_child)
|
2018-12-06 15:29:55 +00:00
|
|
|
visit(child);
|
2021-09-14 11:13:07 +00:00
|
|
|
}
|
2018-12-06 15:29:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-14 11:13:07 +00:00
|
|
|
template <typename Matcher, bool top_to_bottom, bool need_child_accept_data = false>
|
|
|
|
using ConstInDepthNodeVisitor = InDepthNodeVisitor<Matcher, top_to_bottom, need_child_accept_data, const ASTPtr>;
|
2019-07-23 19:49:15 +00:00
|
|
|
|
2020-04-14 18:50:00 +00:00
|
|
|
struct NeedChild
|
|
|
|
{
|
|
|
|
using Condition = bool (*)(const ASTPtr & node, const ASTPtr & child);
|
|
|
|
|
|
|
|
static bool all(const ASTPtr &, const ASTPtr &) { return true; }
|
|
|
|
static bool none(const ASTPtr &, const ASTPtr &) { return false; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Simple matcher for one node type. Use need_child function for complex traversal logic.
|
2021-01-21 09:01:35 +00:00
|
|
|
template <typename DataImpl, NeedChild::Condition need_child = NeedChild::all, typename T = ASTPtr>
|
2018-12-17 16:20:15 +00:00
|
|
|
class OneTypeMatcher
|
|
|
|
{
|
|
|
|
public:
|
2021-01-21 09:01:35 +00:00
|
|
|
using Data = DataImpl;
|
2018-12-17 16:20:15 +00:00
|
|
|
using TypeToVisit = typename Data::TypeToVisit;
|
|
|
|
|
2020-04-14 18:50:00 +00:00
|
|
|
static bool needChildVisit(const ASTPtr & node, const ASTPtr & child) { return need_child(node, child); }
|
2018-12-17 16:20:15 +00:00
|
|
|
|
2019-10-21 16:22:54 +00:00
|
|
|
static void visit(T & ast, Data & data)
|
2018-12-17 16:20:15 +00:00
|
|
|
{
|
|
|
|
if (auto * t = typeid_cast<TypeToVisit *>(ast.get()))
|
|
|
|
data.visit(*t, ast);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-14 18:50:00 +00:00
|
|
|
template <typename Data, NeedChild::Condition need_child = NeedChild::all>
|
|
|
|
using ConstOneTypeMatcher = OneTypeMatcher<Data, need_child, const ASTPtr>;
|
2018-12-17 16:20:15 +00:00
|
|
|
|
2022-06-16 13:29:56 +00:00
|
|
|
template <typename Visitor, typename T = ASTPtr>
|
|
|
|
struct InDepthNodeVisitorWithChildInfo : Visitor
|
|
|
|
{
|
|
|
|
using ChildInfo = typename Visitor::ChildInfo;
|
|
|
|
|
|
|
|
ChildInfo visit(T & ast, const T & parent = {})
|
|
|
|
{
|
|
|
|
ChildInfo all_children_info;
|
|
|
|
for (auto & child : ast->children)
|
|
|
|
{
|
|
|
|
if (Visitor::needVisitChild(ast, child))
|
|
|
|
{
|
|
|
|
ChildInfo child_info = visit(child, ast);
|
|
|
|
all_children_info.update(child_info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return Visitor::visitNode(ast, parent, all_children_info);
|
|
|
|
}
|
|
|
|
catch (Exception & e)
|
|
|
|
{
|
|
|
|
e.addMessage("While processing {}", ast->formatForErrorMessage());
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-06 15:29:55 +00:00
|
|
|
}
|