m8
/
src
/
m8
/
m8.hh
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#ifndef M8_HH
#define M8_HH
#include "ob/ordered_map.hh"
#include "ob/scoped_map.hh"
#include "m8/ast.hh"
#include "m8/reader.hh"
#include "m8/writer.hh"
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <regex>
#include <functional>
#include <utility>
#include <deque>
#include <optional>
class M8
{
struct Core_Ctx
{
Core_Ctx(std::string& buf_, Reader& r_, Writer& w_, std::string ifile_, std::string ofile_):
buf {buf_},
r {r_},
w {w_},
ifile {ifile_},
ofile {ofile_}
{
}
std::string& buf;
Reader& r;
Writer& w;
std::string ifile;
std::string ofile;
}; // struct Core_Ctx
using Args = std::vector<std::string>;
struct Ctx
{
std::string& str;
Args const& args;
std::string err_msg;
std::unique_ptr<Core_Ctx> core;
// Cache& cache;
}; // struct Ctx
public:
using macro_fn = std::function<int(Ctx& ctx)>;
struct macro_t
{
macro_t(std::string const& usage_, std::string const& regex_, macro_fn const& func_) :
usage {usage_},
regex {regex_},
func {func_}
{
}
std::string usage;
std::string regex;
macro_fn func;
};
private:
enum class Mtype
{
core,
internal,
external,
remote
};
struct Macro
{
Mtype type;
std::string name;
std::string info;
// std::string usage;
// std::vector<std::pair<std::string, macro_fn>> rx_fn;
std::vector<macro_t> impl;
std::string url;
}; // struct Macro
OB::Scoped_Map<std::string, Macro> macros_;
public:
M8();
~M8();
// set external macro
void set_macro(std::string const& name, std::string const& info,
std::string const& usage, std::string regex);
// set remote macro
void set_macro(std::string const& name, std::string const& info,
std::string const& usage, std::string regex, std::string const& url);
// set internal macro
void set_macro(std::string const& name, std::string const& info,
std::string const& usage, std::string regex, macro_fn func);
// set internal macro
void set_macro(std::string const& name, std::string const& info, std::vector<M8::macro_t> impl);
// unset internal macro
void unset_macro(std::string const& name);
// unset internal macro
void unset_macro(std::string const& name, std::string regex);
// set core macro
void set_core(std::string const& name, std::string const& info,
std::string const& usage, std::string regex, macro_fn func);
// plain macro hooks
enum class Htype
{
begin,
macro,
res,
end
};
struct Hook
{
std::string key;
std::string val;
};
using Hooks = std::deque<Hook>;
void set_hook(Htype t, Hook h);
std::optional<Hooks> get_hooks(Htype t) const;
void rm_hook(Htype t, std::string key);
void set_debug(bool val);
void set_comment(std::string str);
void set_ignore(std::string str);
void set_copy(bool val);
void set_config(std::string file_name);
void set_delimits(std::string const& delim_start, std::string const& delim_end);
void set_readline(bool val);
std::string summary() const;
std::string list_macros() const;
std::string macro_info(std::string const& name) const;
void parse(std::string const& _ifile = {}, std::string const& _ofile = {});
private:
enum class error_t
{
missing_opening_delimiter,
missing_closing_delimiter,
invalid_format,
undefined_name,
invalid_arg,
failed,
};
std::string error(error_t type, Tmacro const& macro, std::string const& ifile, std::string const& line = {}) const;
struct Settings
{
bool readline {false};
bool use_stdout {false};
bool debug {false};
bool copy {false};
bool summary {false};
bool ignore {false};
}; // struct Settings
Settings settings_;
struct Stats
{
// general stats
int macro {0};
int ignored {0};
int warning {0};
int error {0};
int pass {0};
// macro stats
int core {0};
int internal {0};
int external {0};
int remote {0};
}; // struct Stats
Stats stats_;
std::string delim_start_ {"[M8["};
std::string delim_end_ {"]8M]"};
std::string ignore_;
std::string comment_;
std::unordered_set<std::string> includes_;
std::unordered_map<std::string, std::string> rx_grammar_ {
{"b", "^"},
{"e", "$"},
{"ws", "\\s+"},
{"empty", "^$"},
{"void", "^$"},
{"!all", "([^\\r]*?)"},
{"!wrd", "([^\\s]+?)"},
{"!num", "([\\-+]{0,1}[0-9]+(?:\\.[0-9]+)?(?:e[\\-+]{0,1}[0-9]+)?)"},
// {"!int", "([0-9]+)"},
// {"!dec", "([0-9]+\.[0-9]+)"},
{"!str_s", "'([^'\\\\]*(?:\\\\.[^'\\\\]*)*)'"},
{"!str_d", "\"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\""},
};
// abstract syntax tree
Ast ast_;
// hooks
Hooks h_begin_;
Hooks h_macro_;
Hooks h_res_;
Hooks h_end_;
void core_macros();
void run_hooks(Hooks const& h, std::string& s);
int run_internal(macro_fn const& func, Ctx& ctx);
int run_external(Macro const& macro, Ctx& ctx);
int run_remote(Macro const& macro, Ctx& ctx);
std::string env_var(std::string const& str) const;
std::vector<std::string> suggest_macro(std::string const& name) const;
}; // class M8
#endif // M8_HH