aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/jinja2cpp/src/ast_visitor.h
blob: 01b519829b1a1a04a5d8bb531f3513e9bb1451a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef JINJA2CPP_SRC_AST_VISITOR_H
#define JINJA2CPP_SRC_AST_VISITOR_H

namespace jinja2
{
class IRendererBase;
class ExpressionEvaluatorBase;
class Statement;
class ForStatement;
class IfStatement;
class ElseBranchStatement;
class SetStatement;
class ParentBlockStatement;
class BlockStatement;
class ExtendsStatement;
class IncludeStatement;
class ImportStatement;
class MacroStatement;
class MacroCallStatement;
class ComposedRenderer;
class RawTextRenderer;
class ExpressionRenderer;

class StatementVisitor;

class VisitableStatement
{
public:
    virtual ~VisitableStatement() = default;

    virtual void ApplyVisitor(StatementVisitor* visitor) = 0;
    virtual void ApplyVisitor(StatementVisitor* visitor) const = 0;
};

#define VISITABLE_STATEMENT() \
    void ApplyVisitor(StatementVisitor* visitor) override {visitor->DoVisit(this);} \
    void ApplyVisitor(StatementVisitor* visitor) const override {visitor->DoVisit(this);} \

namespace detail
{
template<typename Base, typename Type>
class VisitorIfaceImpl : public Base
{
public:
    using Base::DoVisit;

    virtual void DoVisit(Type*) {}
    virtual void DoVisit(const Type*) {}
};

template<typename Type>
class VisitorIfaceImpl<void, Type>
{
public:
    virtual void DoVisit(Type*) {}
    virtual void DoVisit(const Type*) {}
};

template<typename Base, typename ... Types>
struct VisitorBaseImpl;

template<typename Base, typename T, typename ... Types>
struct VisitorBaseImpl<Base, T, Types...>
{
    using current_base = VisitorIfaceImpl<Base, T>;
    using base_type = typename VisitorBaseImpl<current_base, Types...>::base_type;
};

template<typename Base, typename T>
struct VisitorBaseImpl<Base, T>
{
    using base_type = VisitorIfaceImpl<Base, T>;
};


template<typename ... Types>
struct VisitorBase
{
    using type = typename VisitorBaseImpl<void, Types ...>::base_type;
};
} // namespace detail

template<typename ... Types>
using VisitorBase = typename detail::VisitorBase<Types...>::type;

class StatementVisitor : public VisitorBase<
    IRendererBase,
    Statement,
    ForStatement,
    IfStatement,
    ElseBranchStatement,
    SetStatement,
    ParentBlockStatement,
    BlockStatement,
    ExtendsStatement,
    IncludeStatement,
    ImportStatement,
    MacroStatement,
    MacroCallStatement,
    ComposedRenderer,
    RawTextRenderer,
    ExpressionRenderer>
{
public:
    void Visit(VisitableStatement* stmt)
    {
        stmt->ApplyVisitor(this);
    }
    void Visit(const VisitableStatement* stmt)
    {
        stmt->ApplyVisitor(this);
    }
};
} // namespace jinja2


#endif