forked from jupyter-xeus/xeus-cling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmanager.hpp
189 lines (168 loc) · 6.22 KB
/
xmanager.hpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/***************************************************************************
* Copyright (c) 2016, Johan Mabille, Loic Gouarin and Sylvain Corlay *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#ifndef XCPP_MANAGER_HPP
#define XCPP_MANAGER_HPP
#include <map>
#include <memory>
#include <regex>
#include <string>
#include <type_traits>
#include "xholder_cling.hpp"
#include "xmagics.hpp"
#include "xpreamble.hpp"
namespace xcpp
{
struct xpreamble_manager
{
std::map<std::string, xholder_preamble> preamble;
template <typename preamble_type>
void register_preamble(const std::string& name, preamble_type* pre)
{
preamble[name] = xholder_preamble(pre);
}
void unregister_preamble(const std::string& name)
{
preamble.erase(name);
}
xholder_preamble& operator[](const std::string& name)
{
return preamble[name];
}
};
class xmagics_manager : public xpreamble
{
public:
using xpreamble::pattern;
xmagics_manager()
{
pattern = R"(^(?:\%{2}|\%)(\w+))";
}
template <typename xmagic_type>
void register_magic(const std::string& magic_name, xmagic_type magic)
{
auto shared = std::make_shared<xmagic_type>(magic);
if (std::is_base_of<xmagic_line, xmagic_type>::value)
{
m_magic_line[magic_name] = std::dynamic_pointer_cast<xmagic_line>(shared);
}
if (std::is_base_of<xmagic_cell, xmagic_type>::value)
{
m_magic_cell[magic_name] = std::dynamic_pointer_cast<xmagic_cell>(shared);
}
}
void unregister_magic(const std::string& magic_name)
{
m_magic_cell.erase(magic_name);
m_magic_line.erase(magic_name);
}
bool contains(const std::string& magic_name, const xmagic_type type = xmagic_type::cell)
{
if (type == xmagic_type::cell)
{
return m_magic_cell.find(magic_name) != m_magic_cell.end();
}
if (type == xmagic_type::line)
{
return m_magic_line.find(magic_name) != m_magic_line.end();
}
return false;
}
void apply(const std::string& magic_name, const std::string& line, const std::string& cell)
{
if (cell.empty())
{
std::cerr << "UsageError: %%" << magic_name << " is a cell magic, but the cell body is empty.";
if (contains(magic_name, xmagic_type::line))
{
std::cerr << " Did you mean the line magic %" << magic_name << " (single %)?";
}
std::cerr << "\n";
return;
}
try
{
(*m_magic_cell[magic_name])(line, cell);
}
catch (const cxxopts::OptionException& e)
{
std::cerr << "UsageError: " << e.what() << "\n";
}
catch (...)
{
std::cerr << "Exception occurred. Recovering...\n";
}
}
void apply(const std::string& magic_name, const std::string& line)
{
try
{
(*m_magic_line[magic_name])(line);
}
catch (const cxxopts::OptionException& e)
{
std::cerr << "UsageError: " << e.what() << "\n";
}
catch (...)
{
std::cerr << "Exception occurred. Recovering...\n";
}
}
void apply(const std::string& code, xeus::xjson& kernel_res) override
{
std::regex re_magic_cell(R"(^\%{2}(\w+))");
std::smatch magic_name;
if (std::regex_search(code, magic_name, re_magic_cell))
{
if (!contains(magic_name.str(1)))
{
std::cerr << "Unknown magic cell function %%" << magic_name[1] << "\n";
std::cout << std::flush;
kernel_res["status"] = "error";
kernel_res["ename"] = "ename";
kernel_res["evalue"] = "evalue";
kernel_res["traceback"] = {};
return;
}
std::regex re_magic_cell(R"(^\%{2}(\w+(?:\s.*)?)\n((?:.*\n?)*))");
std::smatch split_code;
std::regex_search(code, split_code, re_magic_cell);
apply(magic_name[1], split_code[1], split_code[2]);
std::cout << std::flush;
kernel_res["status"] = "ok";
}
std::regex re_magic_line(R"(^\%(\w+))");
if (std::regex_search(code, magic_name, re_magic_line))
{
if (!contains(magic_name.str(1), xmagic_type::line))
{
std::cerr << "Unknown magic line function %" << magic_name[1] << "\n";
std::cout << std::flush;
kernel_res["status"] = "error";
kernel_res["ename"] = "ename";
kernel_res["evalue"] = "evalue";
kernel_res["traceback"] = {};
return;
}
std::regex re_magic_line(R"(^\%(\w+(?:\s.*)?))");
std::smatch split_code;
std::regex_search(code, split_code, re_magic_line);
apply(magic_name[1], split_code[1]);
std::cout << std::flush;
kernel_res["status"] = "ok";
}
}
virtual xpreamble* clone() const override
{
return new xmagics_manager(*this);
}
private:
std::map<std::string, std::shared_ptr<xmagic_cell>> m_magic_cell;
std::map<std::string, std::shared_ptr<xmagic_line>> m_magic_line;
};
}
#endif