forked from freeorion/freeorion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValueRefParser.h
353 lines (310 loc) · 15.6 KB
/
ValueRefParser.h
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
#ifndef _ValueRefParser_h_
#define _ValueRefParser_h_
#include "EnumParser.h"
#include "MovableEnvelope.h"
#include "../universe/ValueRefs.h"
#include "../universe/NamedValueRefManager.h"
#include "../universe/EnumsFwd.h"
#include <boost/spirit/include/qi.hpp>
#include <boost/phoenix.hpp>
namespace Condition {
struct Condition;
}
namespace parse::detail {
// TODO: Investigate refactoring ValueRef to use variant,
// for increased locality of reference.
template <typename T>
using value_ref_payload = MovableEnvelope<ValueRef::ValueRef<T>>;
template <typename T>
using value_ref_signature = value_ref_payload<T> ();
template <typename T>
using value_ref_rule = detail::rule<value_ref_signature<T>>;
template <typename T>
using value_ref_grammar = detail::grammar<value_ref_signature<T>>;
using condition_payload = MovableEnvelope<Condition::Condition>;
using condition_signature = condition_payload ();
using condition_parser_grammar = grammar<condition_signature>;
using name_token_rule = rule<std::string ()>;
using reference_token_rule = rule<ValueRef::ReferenceType ()>;
using container_token_rule = rule<ValueRef::ContainerType ()>;
const parse::detail::reference_token_rule variable_scope(const parse::lexer& tok);
const parse::detail::container_token_rule container(const parse::lexer& tok);
template <typename T>
using variable_payload = MovableEnvelope<ValueRef::Variable<T>>;
template <typename T>
using variable_signature = variable_payload<T> ();
template <typename T>
using variable_rule = rule<
variable_signature<T>,
boost::spirit::qi::locals<
std::string,
ValueRef::ReferenceType,
ValueRef::ContainerType
>
>;
template <typename T>
struct simple_variable_rules
{
simple_variable_rules(const std::string& type_name, const parse::lexer& tok);
name_token_rule bound_variable_name;
name_token_rule free_variable_name;
detail::value_ref_rule<T> constant;
variable_rule<T> free_variable;
variable_rule<T> bound_variable;
variable_rule<T> unwrapped_bound_variable;
variable_rule<T> value_wrapped_bound_variable;
detail::value_ref_rule<T> simple;
reference_token_rule variable_scope_rule;
container_token_rule container_type_rule;
};
template <typename T>
using statistic_payload = MovableEnvelope<ValueRef::ValueRef<T>>;
template <typename T>
using statistic_signature = statistic_payload<T> ();
template <typename T>
using statistic_rule = rule<
statistic_signature<T>,
boost::spirit::qi::locals<
value_ref_payload<T>,
ValueRef::StatisticType,
value_ref_payload<std::string>
>
>;
template <typename T>
using expression_rule = rule<
value_ref_payload<T> (),
boost::spirit::qi::locals<
value_ref_payload<T>,
value_ref_payload<T>,
ValueRef::OpType,
std::vector<value_ref_payload<T>>
>
>;
template <typename T>
struct arithmetic_rules {
arithmetic_rules(const std::string& type_name,
const parse::lexer& tok,
Labeller& label,
const condition_parser_grammar& condition_parser,
const value_ref_grammar<std::string>& string_grammar);
parse::statistic_enum_grammar statistic_type_enum;
expression_rule<T> functional_expr;
expression_rule<T> exponential_expr;
expression_rule<T> multiplicative_expr;
expression_rule<T> additive_expr;
detail::value_ref_rule<T> named_lookup_expr;
detail::value_ref_rule<T> primary_expr;
detail::value_ref_rule<T> statistic_value_ref_expr;
statistic_rule<T> statistic_collection_expr;
statistic_rule<T> statistic_value_expr;
statistic_rule<T> statistic_expr;
detail::value_ref_rule<T> expr;
};
struct simple_int_parser_rules : public simple_variable_rules<int> {
simple_int_parser_rules(const parse::lexer& tok);
};
struct simple_double_parser_rules : public simple_variable_rules<double> {
simple_double_parser_rules(const parse::lexer& tok);
};
template <typename T>
using complex_variable_payload = MovableEnvelope<ValueRef::ComplexVariable<T>>;
template <typename T>
using complex_variable_signature = complex_variable_payload<T> ();
template <typename T>
using complex_variable_rule = rule<complex_variable_signature<T>>;
template <typename T>
using complex_variable_grammar = grammar<complex_variable_signature<T>>;
struct string_complex_parser_grammar : public complex_variable_grammar<std::string> {
string_complex_parser_grammar(const parse::lexer& tok,
Labeller& label,
const value_ref_grammar<std::string>& string_grammar);
simple_int_parser_rules simple_int_rules;
complex_variable_rule<std::string> game_rule;
complex_variable_rule<std::string> empire_ref;
complex_variable_rule<std::string> empire_empire_ref;
complex_variable_rule<std::string> start;
};
template <typename T>
void initialize_bound_variable_parser(
variable_rule<T>& bound_variable,
variable_rule<T>& unwrapped_bound_variable,
variable_rule<T>& value_wrapped_bound_variable,
const name_token_rule& variable_name,
const reference_token_rule& variable_scope_rule,
const container_token_rule& container_type_rule,
const parse::lexer& tok)
{
using boost::phoenix::construct;
using boost::phoenix::new_;
boost::spirit::qi::_1_type _1{};
boost::spirit::qi::_a_type _a{}; // string
boost::spirit::qi::_b_type _b{}; // ReferenceType
boost::spirit::qi::_c_type _c{}; // ContainerType
boost::spirit::qi::_val_type _val{};
boost::spirit::qi::omit_type omit_{};
boost::spirit::qi::eps_type eps{};
const boost::phoenix::function<construct_movable> construct_movable_{};
unwrapped_bound_variable
= (
variable_scope_rule [ _b = _1 ] >> '.'
>> (
(container_type_rule [ _c = _1 ] >> '.') |
eps [ _c = ValueRef::ContainerType::NONE ]
)
>> (variable_name [ _a = construct<std::string>(_1) ])
) [ _val = construct_movable_(new_<ValueRef::Variable<T>>(
_b, _a, _c, ValueRef::ValueToReturn::Initial)) ];
value_wrapped_bound_variable
= (
omit_[tok.Value_] >> '('
>> variable_scope_rule [ _b = _1 ] >> '.'
>> (
(container_type_rule [ _c = _1 ] >> '.') |
eps [ _c = ValueRef::ContainerType::NONE ]
)
>> (variable_name [ _a = construct<std::string>(_1) ]) > ')'
) [ _val = construct_movable_(new_<ValueRef::Variable<T>>(
_b, _a, _c, ValueRef::ValueToReturn::Immediate)) ];
bound_variable
=
value_wrapped_bound_variable [ _val = _1 ]
| unwrapped_bound_variable [ _val = _1 ]
;
}
template <typename T>
void open_and_register_as_string(const std::string& nameref,
::parse::detail::MovableEnvelope<ValueRef::ValueRef<T>>& obj,
bool& pass)
{
if (obj.IsEmptiedEnvelope()) {
ErrorLogger() << "The parser attempted to extract the unique_ptr from a MovableEnvelope more than once "
<< "- while looking at a valueref envelope for use in ValueRef registration ";
pass = false;
} else {
::RegisterValueRef<T>(nameref, obj.OpenEnvelope(pass));
}
}
BOOST_PHOENIX_ADAPT_FUNCTION(void, open_and_register_as_string_, open_and_register_as_string, 3)
}
#include "EnumValueRefRules.h"
namespace parse {
struct string_parser_grammar : public detail::value_ref_grammar<std::string> {
string_parser_grammar(const lexer& tok,
detail::Labeller& label,
const detail::condition_parser_grammar& condition_parser);
detail::string_complex_parser_grammar string_var_complex;
detail::name_token_rule bound_variable_name;
detail::value_ref_rule<std::string> constant;
detail::value_ref_rule<std::string> free_variable;
detail::variable_rule<std::string> bound_variable;
detail::variable_rule<std::string> unwrapped_bound_variable;
detail::variable_rule<std::string> value_wrapped_bound_variable;
detail::value_ref_rule<std::string> statistic_sub_value_ref;
detail::statistic_rule<std::string> statistic;
detail::value_ref_rule<std::string> named_string_valueref;
detail::value_ref_rule<std::string> named_lookup_expr;
detail::expression_rule<std::string> function_expr;
detail::expression_rule<std::string> operated_expr;
detail::value_ref_rule<std::string> expr;
detail::value_ref_rule<std::string> primary_expr;
detail::reference_token_rule variable_scope_rule;
detail::container_token_rule container_type_rule;
};
struct int_arithmetic_rules;
struct int_complex_parser_grammar : public detail::complex_variable_grammar<int> {
int_complex_parser_grammar(const lexer& tok,
detail::Labeller& label,
const int_arithmetic_rules& _int_arith_rules,
const detail::condition_parser_grammar& condition_parser,
const detail::value_ref_grammar<std::string>& string_grammar);
// imported grammars directly used in int_complex_parser_grammar
const int_arithmetic_rules& int_rules;
ship_part_class_enum_grammar ship_part_class_enum;
detail::planet_type_parser_rules planet_type_rules;
// grammars defined by int_complex_parser_grammar
detail::complex_variable_rule<int> game_rule;
detail::complex_variable_rule<int> empire_name_ref;
detail::complex_variable_rule<int> empire_id_ref;
detail::complex_variable_rule<int> empire_ships_destroyed;
detail::complex_variable_rule<int> jumps_between;
//complex_variable_rule<int> jumps_between_by_empire_supply;
detail::complex_variable_rule<int> outposts_owned;
detail::complex_variable_rule<int> parts_in_ship_design;
detail::complex_variable_rule<int> part_class_in_ship_design;
detail::value_ref_rule<int> part_class_as_int;
detail::complex_variable_rule<int> ship_parts_owned;
detail::complex_variable_rule<int> empire_design_ref;
detail::complex_variable_rule<int> slots_in_hull;
detail::complex_variable_rule<int> slots_in_ship_design;
detail::complex_variable_rule<int> special_added_on_turn;
detail::complex_variable_rule<int> planet_type_difference;
detail::complex_variable_rule<int> start;
};
struct int_arithmetic_rules : public detail::arithmetic_rules<int> {
int_arithmetic_rules(
const lexer& tok,
detail::Labeller& label,
const detail::condition_parser_grammar& condition_parser,
const detail::value_ref_grammar<std::string>& string_grammar);
detail::simple_int_parser_rules simple_int_rules;
int_complex_parser_grammar int_complex_grammar;
detail::value_ref_rule<int> named_int_valueref;
detail::value_ref_rule<int> total_fighter_shots;
};
struct double_complex_parser_grammar : public detail::complex_variable_grammar<double> {
double_complex_parser_grammar(const lexer& tok,
detail::Labeller& label,
const detail::condition_parser_grammar& condition_parser,
const detail::value_ref_grammar<std::string>& string_grammar);
detail::simple_int_parser_rules simple_int_rules;
detail::complex_variable_rule<double> name_property_rule;
detail::complex_variable_rule<double> id_empire_location_rule;
detail::complex_variable_rule<double> name_empire_location_rule;
detail::complex_variable_rule<double> empire_meter_value;
detail::complex_variable_rule<double> empire_stockpile;
detail::complex_variable_rule<double> direct_distance;
detail::complex_variable_rule<double> shortest_path;
detail::complex_variable_rule<double> species_content_opinion;
detail::complex_variable_rule<double> species_empire_opinion;
detail::complex_variable_rule<double> species_species_opinion;
detail::complex_variable_rule<double> special_capacity;
detail::complex_variable_rule<double> unwrapped_part_meter;
detail::complex_variable_rule<double> value_wrapped_part_meter;
detail::complex_variable_rule<double> start;
parse::ship_part_meter_enum_grammar ship_part_meter_type_enum;
};
struct double_parser_rules : public detail::arithmetic_rules<double> {
double_parser_rules(
const lexer& tok,
detail::Labeller& label,
const detail::condition_parser_grammar& condition_parser,
const detail::value_ref_grammar<std::string>& string_grammar);
int_arithmetic_rules int_rules;
detail::simple_int_parser_rules simple_int_rules;
detail::simple_double_parser_rules simple_double_rules;
int_complex_parser_grammar int_complex_grammar;
double_complex_parser_grammar double_complex_grammar;
detail::value_ref_rule<double> int_constant_cast;
detail::value_ref_rule<double> int_bound_variable_cast;
detail::value_ref_rule<double> int_free_variable_cast;
detail::value_ref_rule<double> int_statistic_cast;
detail::value_ref_rule<double> int_complex_variable_cast;
detail::value_ref_rule<double> int_total_fighter_shots_cast;
detail::value_ref_rule<double> named_real_valueref;
detail::value_ref_rule<double> named_int_valueref_cast;
};
struct castable_as_int_parser_rules {
castable_as_int_parser_rules(const lexer& tok,
detail::Labeller& label,
const detail::condition_parser_grammar& condition_parser,
const detail::value_ref_grammar<std::string>& string_grammar);
int_arithmetic_rules int_rules;
double_parser_rules double_rules;
detail::planet_type_parser_rules planet_type_rules;
detail::value_ref_rule<int> castable_expr;
detail::value_ref_rule<int> enum_expr;
detail::value_ref_rule<int> flexible_int;
detail::value_ref_rule<int> enum_or_int;
};
}
#endif