-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmatch.hpp
More file actions
467 lines (379 loc) · 11.9 KB
/
match.hpp
File metadata and controls
467 lines (379 loc) · 11.9 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
cpp-pattern-matching - Code covered by the MIT License
Author: mutouyun (http://orzz.org)
*/
#pragma once
#include "capo/preprocessor.hpp"
#include <utility> // std::forward
#include <regex> // std::regex, std::regex_match
#include <string> // std::string
#include <tuple> // std::tuple
#include <type_traits> // std::add_pointer, std::remove_reference, ...
#include <cstddef> // size_t
namespace match {
// To remove reference and cv qualification from a type.
template <typename T>
using underlying = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
// The helper meta-predicate capable of distinguishing all our patterns.
template <typename T>
struct is_pattern : std::false_type {};
template <typename T>
struct pattern_checker : is_pattern<underlying<T>> {};
/*
* Constant pattern
*/
template <typename T>
struct constant
{
const T& t_;
template <typename U>
bool operator()(U&& tar) const
{
return (std::forward<U>(tar) == t_);
}
};
template <typename T>
struct is_pattern<constant<T>> : std::true_type {};
/*
* Variable pattern
*/
template <typename T>
struct variable
{
T& t_;
template <typename U>
bool operator()(U&& tar) const
{
t_ = std::forward<U>(tar);
return (t_ == std::forward<U>(tar));
}
};
template <typename T>
struct is_pattern<variable<T>> : std::true_type {};
/*
* Wildcard pattern
*/
struct wildcard
{
constexpr wildcard(void) {}
template <typename U>
bool operator()(U&&) const
{
return true;
}
};
constexpr wildcard _;
template <>
struct is_pattern<wildcard> : std::true_type{};
/*
* Predicate pattern, for lambda-expressions or other callable objects.
*/
template <typename F>
struct predicate
{
F judge_;
template <typename U>
bool operator()(U&& tar) const
{
return !!(this->judge_(std::forward<U>(tar)));
}
};
template <typename T>
struct is_pattern<predicate<T>> : std::true_type {};
struct is_functor_checker_
{
template <typename T> static std::true_type check(decltype(&T::operator())*);
template <typename T> static std::false_type check(...);
};
template <typename T>
using is_functor = decltype(is_functor_checker_::check<T>(nullptr));
template <typename T, bool = std::is_function<typename std::remove_pointer<T>::type>::value ||
is_functor<T>::value>
struct is_closure_;
template <typename T> struct is_closure_<T, true> : std::true_type {};
template <typename T> struct is_closure_<T, false> : std::false_type {};
template <typename T>
struct is_closure : is_closure_<underlying<T>> {};
template <typename T>
inline auto converter(T&& arg)
-> typename std::enable_if<is_closure<T>::value, predicate<T&&>>::type
{
return { std::forward<T>(arg) };
}
/*
* Regular expression pattern
*/
struct regex
{
std::regex r_;
template <typename T>
regex(T&& r)
: r_(std::forward<T>(r))
{}
template <typename U>
bool operator()(U&& tar) const
{
return std::regex_match(std::forward<U>(tar), r_);
}
};
template <>
struct is_pattern<regex> : std::true_type{};
#define Regex(...) match::regex { __VA_ARGS__ }
/*
* Type pattern
*/
template <typename T> inline const T* addr(const T* t) { return t; }
template <typename T> inline T* addr( T* t) { return t; }
template <typename T> inline const T* addr(const T& t) { return std::addressof(t); }
template <typename T> inline T* addr( T& t) { return std::addressof(t); }
template <typename T, bool = std::is_polymorphic<underlying<T>>::value>
struct type;
template <>
struct type<wildcard, false>
{
template <typename U>
bool operator()(U&&) const
{
return true;
}
};
template <typename T>
struct type<T, false>
{
template <typename U>
bool operator()(const volatile U&) const
{
return std::is_same<underlying<T>, U>::value;
}
};
template <typename T>
struct type<T, true>
{
template <typename U>
bool operator()(U&& tar) const
{
using p_t = underlying<T> const volatile *;
return (dynamic_cast<p_t>(addr(tar)) != nullptr);
}
};
template <typename T, bool Cond>
struct is_pattern<type<T, Cond>> : std::true_type{};
#define Type(...) match::type<__VA_ARGS__> {}
/*
* Constructor pattern
*/
template <typename...>
struct model;
template <>
struct model<> {};
template <typename T1, typename... T>
struct model<T1, T...> : model<T...> { T1 m_; };
template <typename Tp, size_t N>
struct model_strip;
template <typename T1, typename... T, size_t N>
struct model_strip<model<T1, T...>, N> : model_strip<model<T...>, N - 1> {};
template <typename T1, typename... T>
struct model_strip<model<T1, T...>, 0> { using type = model<T1, T...>; };
template <typename Tp>
struct model_strip<Tp, 0> { using type = Tp; };
template <typename Tp>
struct model_length : std::integral_constant<size_t, 0> {};
template <typename... T>
struct model_length<model<T...>> : std::integral_constant<size_t, sizeof...(T)> {};
template <typename T, typename U>
struct model_append { using type = model<T, U>; };
template <typename... T, typename U>
struct model_append<model<T...>, U> { using type = model<T..., U>; };
template <typename Tp>
struct model_reverse { using type = Tp; };
template <typename T1, typename... T>
struct model_reverse<model<T1, T...>>
{
using head = typename model_reverse<model<T...>>::type;
using type = typename model_append<head, T1>::type;
};
template <typename... T>
struct layout
{
using model_t = typename model_reverse<model<T...>>::type;
template <typename U, typename V>
struct rep;
template <typename U, typename V>
struct rep<U&, V> { using type = V&; };
template <typename U, typename V>
struct rep<U&&, V> { using type = V&&; };
template <typename U, typename V>
struct rep<const U&, V> { using type = const V&; };
template <typename U, typename V>
struct rep<const U&&, V> { using type = const V&&; };
template <size_t N, typename U>
static auto & get(U&& tar)
{
decltype(auto) mm = reinterpret_cast<typename rep<U&&, model_t>::type>(tar);
return static_cast<typename model_strip<model_t, model_length<model_t>::value - N - 1>::type &>(mm).m_;
}
};
template <class Bind>
struct bindings_base
{
template <size_t N, typename T, typename U>
static auto apply(const T&, U&&)
-> typename std::enable_if<(std::tuple_size<T>::value <= N), bool>::type
{
return true;
}
template <size_t N, typename T, typename U>
static auto apply(const T& tp, U&& tar)
-> typename std::enable_if<(std::tuple_size<T>::value > N), bool>::type
{
using layout_t = typename Bind::layout_t;
if ( std::get<N>(tp)(layout_t::template get<N>(tar)) )
{
return apply<N + 1>(tp, std::forward<U>(tar));
}
return false;
}
template <typename T, typename U>
static auto apply(const T& tp, U&& tar)
-> typename std::enable_if<std::is_pointer<underlying<U>>::value, bool>::type
{
return apply<0>(tp, *std::forward<U>(tar));
}
template <typename T, typename U>
static auto apply(const T& tp, U&& tar)
-> typename std::enable_if<!std::is_pointer<underlying<U>>::value, bool>::type
{
return apply<0>(tp, std::forward<U>(tar));
}
};
template <class C>
struct bindings;
template <typename C, typename... T>
struct constructor : type<C>
{
std::tuple<T...> tp_;
template <typename... U>
constructor(U&&... args)
: tp_(std::forward<U>(args)...)
{}
template <typename U>
bool operator()(U&& tar) const
{
if ( type<C>::operator()(std::forward<U>(tar)) )
{
return bindings<underlying<U>>::apply(tp_, std::forward<U>(tar));
}
return false;
}
};
template <typename C, typename... T>
struct is_pattern<constructor<C, T...>> : std::true_type{};
#define MATCH_REGIST_TYPE(TYPE, ...) \
namespace match \
{ \
template <> struct bindings<TYPE> : bindings_base<bindings<TYPE>> \
{ \
using layout_t = layout<__VA_ARGS__>; \
}; \
template <> struct bindings<TYPE*> : bindings<TYPE> {}; \
}
/*
* Sequence pattern
*/
template <typename... T>
struct sequence
{
std::tuple<T...> tp_;
template <typename... U>
sequence(U&&... args)
: tp_(std::forward<U>(args)...)
{}
template <size_t N, typename U, typename It>
auto apply(U&&, It&&) const
-> typename std::enable_if<(sizeof...(T) <= N), bool>::type
{
return true;
}
template <size_t N, typename U, typename It>
auto apply(U&& tar, It&& it) const
-> typename std::enable_if<(sizeof...(T) > N), bool>::type
{
if ( it == tar.end() ) return false;
if ( std::get<N>(tp_)(*it) ) return apply<N + 1>(std::forward<U>(tar), ++it);
return false;
}
template <typename U>
bool operator()(U&& tar) const
{
return apply<0>(std::forward<U>(tar), tar.begin());
}
};
template <typename... T>
struct is_pattern<sequence<T...>> : std::true_type{};
/*
* "filter" is a common function used to provide convenience to the users by converting
* constant values into constant patterns and regular variables into variable patterns.
* If the users defined a suitable converter for a specific type, the filter would choose
* the custom converter for wrapping that variable of the type.
*/
void converter(...);
template <typename T>
inline auto filter(T&& arg)
-> typename std::enable_if<pattern_checker<T>::value, T&&>::type
{
return std::forward<T>(arg);
}
template <typename T>
inline auto filter(const T& arg)
-> typename std::enable_if<!pattern_checker<T>::value &&
std::is_same<decltype(converter(arg)), void>::value,
constant<T>>::type
{
return { arg };
}
template <typename T>
inline auto filter(T& arg)
-> typename std::enable_if<!pattern_checker<T>::value &&
std::is_same<decltype(converter(arg)), void>::value,
variable<T>>::type
{
return { arg };
}
template <typename T>
inline auto filter(T&& arg)
-> typename std::enable_if<!pattern_checker<T>::value &&
!std::is_same<decltype(converter(std::forward<T>(arg))), void>::value,
decltype(converter(std::forward<T>(arg)))>::type
{
return converter(std::forward<T>(arg));
}
/*
* Here is a part of the constructor & sequence pattern.
* I have to implement it here, because gcc needs the filter be declared before it.
*/
template <typename T = wildcard, typename... P>
inline auto C(P&&... args)
-> constructor<T, decltype(filter(std::forward<P>(args)))...>
{
return { filter(std::forward<P>(args))... };
}
template <typename... P>
inline auto S(P&&... args)
-> sequence<decltype(filter(std::forward<P>(args)))...>
{
return { filter(std::forward<P>(args))... };
}
} // namespace match
#define Match(...) \
{ \
auto target_ = std::forward_as_tuple(__VA_ARGS__); \
if (false)
#define MATCH_CASE_ARG_(N, ...) && ( match::filter( CAPO_PP_A_(N, __VA_ARGS__) )( std::get<N - 1>(std::move(target_)) ) )
#define P(...) ( true CAPO_PP_REPEAT_(CAPO_PP_COUNT_(__VA_ARGS__), MATCH_CASE_ARG_, __VA_ARGS__) )
#define With(...) \
} else if (__VA_ARGS__) {
#define Case(...) With( P(__VA_ARGS__) )
#define Otherwise() \
} else {
#define EndMatch \
}