-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.hpp
397 lines (326 loc) · 14.9 KB
/
expression.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
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
#ifndef SCHLAZICONTROL_EXPRESSION_HPP
#define SCHLAZICONTROL_EXPRESSION_HPP
#include <cstdint>
#include <algorithm>
#include <chrono>
#include <iterator>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#include <boost/fusion/container/set.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/variant/static_visitor.hpp>
#include <boost/variant/variant.hpp>
#include <typestring.hh>
#include "typeinfo.hpp"
#include "utility_stream.hpp"
#include "utility_string.hpp"
namespace sc {
namespace expression {
template< typename Derived, typename ...Args >
struct Factory
{
Factory( std::string const& name )
: name_( name ) {}
std::string const& name() const { return name_; }
private:
std::string name_;
};
namespace diagnostics {
template< typename ...Args >
std::runtime_error invalidArgument( std::string const& function, std::size_t index, Args&&... args )
{
return std::runtime_error( str(
"invalid argument ", index + 1, " in call to ", function, ": ", std::forward< Args >( args )... ) );
}
namespace detail {
template< typename Type >
struct ValueHolder
{
ValueHolder( Type const& value ) : value_( &value ) {}
Type const* value_;
};
template< typename Type >
struct Value : ValueHolder< Type >
{
using ValueHolder< Type >::ValueHolder;
friend std::ostream& operator<<( std::ostream& os, Value const& value )
{
return os << *value.value_;
}
};
template< typename Rep, typename Period >
struct Value< std::chrono::duration< Rep, Period > >
: ValueHolder< std::chrono::duration< Rep, Period > >
{
using ValueHolder< std::chrono::duration< Rep, Period > >::ValueHolder;
friend std::ostream& operator<<( std::ostream& os, Value const& value )
{
return os << std::chrono::duration_cast< std::chrono::nanoseconds >( *value.value_ ).count() << "ns";
}
};
} // namespace detail
template< typename Type >
detail::Value< Type > value( Type const& value )
{
return detail::Value< Type >( value );
}
namespace detail {
template< typename ...Values >
struct Strings
{
Strings( char const* quote, char const* delim )
: quote_( quote ), delim_( delim ) {}
friend std::ostream& operator<<( std::ostream& os, Strings const& strings )
{
write( os, strings.quote_, strings.delim_, Values()... );
return os;
}
private:
static void write( std::ostream& os, char const* quote, char const* delim )
{
}
template< typename Arg0, typename ...Args >
static void write(
std::ostream& os, char const* quote, char const* delim, Arg0 value0, Args... values )
{
os << quote;
std::copy( Arg0::cbegin(), Arg0::cend(), OutputStreamIterator( os ) );
os << quote;
if ( sizeof...( Args ) > 0 ) {
os << delim;
}
write( os, quote, delim, values... );
}
char const* quote_;
char const* delim_;
};
} // namespace detail
template< typename ...Values >
detail::Strings< Values... > strings( char const* quote, char const* delim )
{
return detail::Strings< Values... >( quote, delim );
}
} // namespace diagnostics
template< typename Result, Result Minimum, Result Maximum >
struct Range
{
using Type = Result;
static void validate( std::string const& function, std::size_t index, Type const& value )
{
if ( value < Minimum || value > Maximum ) {
throw diagnostics::invalidArgument(
function, index, "expected numeric value between ", Minimum, " and ", Maximum,
" instead of ", value );
}
}
};
template< typename Result, typename ...Values >
struct Enumeration
{
using Type = Result;
template< typename ...Args >
static void validate( std::string const& function, std::size_t index, Result const& value, Args... )
{
throw diagnostics::invalidArgument(
function, index, "expected one out of enumeration ",
diagnostics::strings< Args... >( "\"", ", " ), " instead of \"", value, "\"" );
}
};
template< typename Result, typename Value0, typename ...Values >
struct Enumeration< Result, Value0, Values... >
{
using Type = Result;
static void validate( std::string const& function, std::size_t index, Result const& value )
{
validate( function, index, value, Value0(), Values()... );
}
template< typename ...Args >
static void validate(
std::string const& function, std::size_t index, Result const& value, Args... args )
{
if ( value.size() != Value0::size() ||
!std::equal( Value0::cbegin(), Value0::cend(), value.cbegin() ) ) {
Enumeration< Result, Values... >::validate( function, index, value, args... );
}
}
};
namespace detail {
template< typename Derived, typename Types >
struct ArgParserInvoker
{
template< typename Other >
static Derived parse( std::string const& function, std::size_t index, Other&& value )
{
throw std::runtime_error( "TODO: no parser for this type" );
}
};
template< typename Derived, typename Type0, typename ...Types >
struct ArgParserInvoker< Derived, boost::fusion::set< Type0, Types... > >
: ArgParserInvoker< Derived, boost::fusion::set< Types... > >
{
using ArgParserInvoker< Derived, boost::fusion::set< Types... > >::parse;
static Derived parse( std::string const& function, std::size_t index, Type0 const& value )
{
Derived::validate( function, index, value );
return Derived( value );
}
};
template< typename Derived, typename ...Types >
struct ArgParserValidator
{
template< typename Other >
static void validate( std::string const& function, std::size_t index, Other&& value ) {}
};
template< typename Derived, typename Type0, typename ...Types >
struct ArgParserValidator< Derived, Type0, Types... >
: ArgParserValidator< Derived, Types... >
{
using ArgParserValidator< Derived, Types... >::validate;
static void validate( std::string const& function, std::size_t index, typename Type0::Type const& value )
{
Type0::validate( function, index, value );
ArgParserValidator< Derived, Types... >::validate( function, index, value );
}
};
struct ArgParserTag {};
} // namespace detail
template< typename Derived, typename Result, typename ...Args >
struct ArgParser : detail::ArgParserTag
{
using BaseType = ArgParser< Derived, Result, Args... >;
template< typename Type >
static Derived parse( std::string const& function, std::size_t index, Type&& value )
{
return detail::ArgParserInvoker< Derived, boost::fusion::set< typename Args::Type... > >::parse(
function, index, std::forward< Type >( value ) );
}
template< typename Type >
static void validate( std::string const& function, std::size_t index, Type&& value )
{
detail::ArgParserValidator< Derived, Args... >::validate(
function, index, std::forward< Type >( value ) );
}
ArgParser( Result&& result ) : result_( std::move( result ) ) {}
operator Result&&() { return std::move( result_ ); }
private:
Result result_;
};
template< typename Base, typename ...Factories >
std::unique_ptr< Base > parseExpression( std::string const& text, Factories&& ... factories );
namespace detail {
using CallArgument = boost::variant< std::string, std::intmax_t, std::chrono::nanoseconds >;
struct Call
{
std::string function;
std::vector< CallArgument > arguments;
};
template< typename Type >
struct ArgumentVisitorFallback
{
template< typename Other >
static Type invoke( std::string const& function, std::size_t index, Other&& value )
{
throw diagnostics::invalidArgument(
function, index, "got ", typeName< Other >(), " \"",
diagnostics::value( std::forward< Other >( value ) ), "\" instead of ",
typeName< Type >() );
}
};
template< typename Type, typename Enable = void >
struct ArgumentVisitorImpl;
template<>
struct ArgumentVisitorImpl< std::string >
: ArgumentVisitorFallback< std::string >
{
static std::string invoke( std::string const& function, std::size_t index, std::string const& value )
{
return value;
}
using ArgumentVisitorFallback< std::string >::invoke;
};
template< typename Type >
struct ArgumentVisitorImpl< Type, std::enable_if_t< std::is_integral< Type >::value > >
: ArgumentVisitorFallback< Type >
{
static Type invoke( std::string const& function, std::size_t index, std::intmax_t value )
{
return value;
}
using ArgumentVisitorFallback< Type >::invoke;
};
template< typename Rep, typename Period >
struct ArgumentVisitorImpl< std::chrono::duration< Rep, Period > >
: ArgumentVisitorFallback< std::chrono::duration< Rep, Period > >
{
static std::chrono::duration< Rep, Period > invoke(
std::string const& function, std::size_t index, std::chrono::nanoseconds value )
{
return std::chrono::duration_cast< std::chrono::duration< Rep, Period > >( value );
}
using ArgumentVisitorFallback< std::chrono::duration< Rep, Period > >::invoke;
};
template< typename Type >
struct ArgumentVisitorImpl< Type, std::enable_if_t< std::is_base_of< ArgParserTag, Type >::value > >
{
template< typename Other >
static Type invoke( std::string const& function, std::size_t index, Other&& value )
{
return Type::parse( function, index, std::forward< Other >( value ) );
}
};
template< typename Type >
struct ArgumentVisitor : boost::static_visitor< Type >
{
ArgumentVisitor( std::string const& function, std::size_t index )
: function_( function )
, index_( index )
{
}
template< typename Other >
Type operator()( Other&& value ) const
{
return ArgumentVisitorImpl< Type >::invoke( function_, index_, std::forward< Other >( value ) );
}
private:
std::string const& function_;
std::size_t index_;
};
template< typename Type >
Type expand( Call const& call, size_t index )
{
return boost::apply_visitor( ArgumentVisitor< Type >( call.function, index ), call.arguments[ index ] );
}
template< typename Base >
std::unique_ptr< Base > create( Call const& call )
{
throw std::runtime_error( str( "unknown function ", call.function, " in expression" ));
}
template< typename Base, typename Derived, typename ...Args, typename ...Factories >
std::unique_ptr< Base > create(
Call const& call, Factory< Derived, Args... > const& factory, Factories&& ... factories )
{
if ( factory.name() != call.function ) {
return create< Base >( call, std::forward< Factories >( factories )... );
}
if ( sizeof...( Args ) != call.arguments.size()) {
throw std::runtime_error( str(
"invalid number of arguments to function ", factory.name(), " (expected ",
sizeof...( Args ), " but was ", call.arguments.size(), ")" ) );
}
size_t index = 0;
return std::unique_ptr< Derived >(
new Derived( expand< Args >( call, index++ )... ) );
}
Call parseExpression( std::string const& text );
} // namespace detail
template< typename Base, typename ...Factories >
std::unique_ptr< Base > parseExpression( std::string const& text, Factories&& ... factories )
{
return detail::create< Base >( detail::parseExpression( text ), std::forward< Factories >( factories )... );
}
std::chrono::nanoseconds parseDuration( std::string const& text );
} // namespace expression
} // namespace sc
#endif //SCHLAZICONTROL_EXPRESSION_HPP