LILAC
Language to language Interop LAyer Compiler
Loading...
Searching...
No Matches
interfacevisitor.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024 Yeong-won Seo
3 *
4 * This file is part of LILAC.
5 *
6 * LILAC is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3, or (at your option) any later
9 * version.
10 *
11 * LILAC is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <functional>
23#include <utility>
24
25#include "exception.h"
26#include "frxml.h"
27
28namespace lilac::shared
29{
38 template<typename TContext>
40 {
41 public:
42 virtual ~InterfaceVisitor() = default;
43
54 [[nodiscard]]
55 virtual std::string GetName() const = 0;
56
63 virtual void Begin(TContext& ctx, const frxml::dom& parent, const frxml::dom& current, int depth) = 0;
64
71 virtual void End(TContext& ctx, const frxml::dom& parent, const frxml::dom& current, int depth) = 0;
72 };
73
74 template<typename T, typename TContext>
75 concept TVisitor = std::is_base_of_v<InterfaceVisitor<TContext>, T>;
76
83 template<typename TContext, TVisitor<TContext>... TVisitor>
84 class GenericInterfaceVisitor final : public InterfaceVisitor<TContext>
85 {
86 std::map<std::string, std::shared_ptr<InterfaceVisitor<TContext>>> m_Visitors;
87
88 public:
90 {
91 for (std::shared_ptr<InterfaceVisitor<TContext>> visitor: {
92 static_cast<std::shared_ptr<InterfaceVisitor<TContext>>>(std::make_shared<TVisitor>())...
93 })
94 m_Visitors.emplace(visitor->GetName(), std::move(visitor));
95 }
96
97 [[nodiscard]]
98 std::string GetName() const override
99 {
100 return "__generic__";
101 }
102
103 void Begin(TContext& ctx, const frxml::dom& parent, const frxml::dom& current, int depth) override
104 {
105 auto v = static_cast<std::string>(current.tag().view());
106 if (!m_Visitors.contains(v))
107 throw exception(std::format("undefined tag '{}'", v), current);
108
109 m_Visitors.at(v)->Begin(ctx, parent, current, depth);
110 }
111
112 void End(TContext& ctx, const frxml::dom& parent, const frxml::dom& current, int depth) override
113 {
114 auto v = static_cast<std::string>(current.tag().view());
115 if (!m_Visitors.contains(v))
116 throw exception(std::format("undefined tag '{}'", v), current);
117
118 m_Visitors.at(v)->End(ctx, parent, current, depth);
119 }
120 };
121
133 template<typename TContext>
134 class AssemblyVisitor : public InterfaceVisitor<TContext>
135 {
136 public:
137 [[nodiscard]]
138 std::string GetName() const final
139 {
140 return "assembly";
141 }
142
143 void Begin(TContext& ctx, const frxml::dom& parent, const frxml::dom& current, int depth) override = 0;
144
145 void End(TContext& ctx, const frxml::dom& parent, const frxml::dom& current, int depth) override = 0;
146 };
147}
An interface visitor for a whole assembly.
void Begin(TContext &ctx, const frxml::dom &parent, const frxml::dom &current, int depth) override=0
void End(TContext &ctx, const frxml::dom &parent, const frxml::dom &current, int depth) override=0
std::string GetName() const final
Gets the name of this interface visitor.
A composite ASTVisitor wrapping other ASTVisitor(s)
void End(TContext &ctx, const frxml::dom &parent, const frxml::dom &current, int depth) override
std::string GetName() const override
Gets the name of this interface visitor.
void Begin(TContext &ctx, const frxml::dom &parent, const frxml::dom &current, int depth) override
A helper class to traverse interface graph.
virtual ~InterfaceVisitor()=default
virtual std::string GetName() const =0
Gets the name of this interface visitor.
virtual void Begin(TContext &ctx, const frxml::dom &parent, const frxml::dom &current, int depth)=0
virtual void End(TContext &ctx, const frxml::dom &parent, const frxml::dom &current, int depth)=0