forked from freeorion/freeorion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumValueRefRules.h
239 lines (205 loc) · 8.6 KB
/
EnumValueRefRules.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
#ifndef _EnumValueRefRules_h_
#define _EnumValueRefRules_h_
#include "ValueRefParser.h"
#include "MovableEnvelope.h"
namespace parse::detail {
template <typename T>
void initialize_nonnumeric_statistic_parser(
parse::detail::statistic_rule<T>& statistic,
const parse::lexer& tok,
parse::detail::Labeller& label,
const parse::detail::condition_parser_grammar& condition_parser,
const typename parse::detail::value_ref_rule<T>& value_ref);
template <typename T>
struct enum_value_ref_rules {
enum_value_ref_rules(const std::string& type_name,
const lexer& tok,
Labeller& label,
const condition_parser_grammar& condition_parser);
rule<ValueRef::OpType ()> selection_operator;
value_ref_rule<T> selection_expr;
name_token_rule variable_name;
enum_rule<T> enum_expr;
value_ref_rule<T> constant_expr;
value_ref_rule<T> free_variable_expr;
variable_rule<T> bound_variable_expr;
variable_rule<T> unwrapped_bound_variable_expr;
variable_rule<T> value_wrapped_bound_variable_expr;
expression_rule<T> functional_expr;
value_ref_rule<T> named_lookup_expr;
value_ref_rule<T> primary_expr;
value_ref_rule<T> statistic_sub_value_ref;
statistic_rule<T> statistic_expr;
complex_variable_rule<T> complex_expr;
value_ref_rule<T> expr;
reference_token_rule variable_scope_rule;
container_token_rule container_type_rule;
};
template <typename T>
void initialize_nonnumeric_statistic_parser(
statistic_rule<T>& statistic,
const parse::lexer& tok,
Labeller& label,
const condition_parser_grammar& condition_parser,
const value_ref_rule<T>& value_ref)
{
using boost::phoenix::construct;
using boost::phoenix::new_;
using boost::phoenix::push_back;
boost::spirit::qi::_1_type _1;
boost::spirit::qi::_2_type _2;
boost::spirit::qi::_val_type _val;
boost::spirit::qi::_pass_type _pass;
boost::spirit::qi::omit_type omit_;
const boost::phoenix::function<construct_movable> construct_movable_;
const boost::phoenix::function<deconstruct_movable> deconstruct_movable_;
statistic
= ( (omit_[tok.Statistic_] >> omit_[tok.Mode_])
> label(tok.value_) > value_ref
> label(tok.condition_) > condition_parser)
[ _val = construct_movable_(new_<ValueRef::Statistic<T>>(
deconstruct_movable_(_1, _pass),
ValueRef::StatisticType::MODE,
deconstruct_movable_(_2, _pass))) ]
;
}
template <typename T>
enum_value_ref_rules<T>::enum_value_ref_rules(
const std::string& type_name,
const parse::lexer& tok,
Labeller& label,
const condition_parser_grammar& condition_parser)
{
using boost::phoenix::new_;
using boost::phoenix::push_back;
boost::spirit::qi::_1_type _1;
boost::spirit::qi::_2_type _2;
boost::spirit::qi::_4_type _4;
boost::spirit::qi::_val_type _val;
boost::spirit::qi::_pass_type _pass;
boost::spirit::qi::lit_type lit;
const boost::phoenix::function<construct_movable> construct_movable_;
//const boost::phoenix::function<deconstruct_movable> deconstruct_movable_;
const boost::phoenix::function<deconstruct_movable_vector> deconstruct_movable_vector_;
constant_expr
= enum_expr [ _val = construct_movable_(new_<ValueRef::Constant<T>>(_1)) ]
;
variable_scope_rule = variable_scope(tok);
container_type_rule = container(tok);
initialize_bound_variable_parser<T>(
bound_variable_expr, unwrapped_bound_variable_expr,
value_wrapped_bound_variable_expr, variable_name,
variable_scope_rule, container_type_rule, tok);
statistic_sub_value_ref
= constant_expr
| bound_variable_expr
| free_variable_expr
| complex_expr
;
selection_operator
= tok.OneOf_ [ _val = ValueRef::OpType::RANDOM_PICK ]
| tok.Min_ [ _val = ValueRef::OpType::MINIMUM ]
| tok.Max_ [ _val = ValueRef::OpType::MAXIMUM ];
selection_expr
= (selection_operator > '(' > (expr % ',') > ')')
[ _val = construct_movable_(new_<ValueRef::Operation<T>>(_1, deconstruct_movable_vector_(_2, _pass))) ];
named_lookup_expr
= (
tok.Named_ >> tok.Value_ >> tok.Lookup_
>> label(tok.name_)
>> tok.string
) [ _val = construct_movable_(new_<ValueRef::NamedRef<T>>(_4)) ]
;
functional_expr %= selection_expr | primary_expr;
expr
= functional_expr
;
initialize_nonnumeric_statistic_parser<T>(
statistic_expr, tok, label, condition_parser, statistic_sub_value_ref);
primary_expr
= constant_expr
| bound_variable_expr
| free_variable_expr
| statistic_expr
| complex_expr
| named_lookup_expr
;
#if DEBUG_VALUEREF_PARSERS
debug(variable_name);
debug(enum_expr);
debug(constant_expr);
debug(free_variable_expr);
debug(bound_variable_expr);
debug(statistic_value_ref_expr);
debug(statistic_expr);
debug(functional_expr);
debug(named_lookup_expr);
debug(primary_expr);
debug(expr);
#endif
variable_name.name(type_name + " variable name");
enum_expr.name(type_name);
constant_expr.name(type_name + " constant");
free_variable_expr.name(type_name + " free variable");
bound_variable_expr.name(type_name + " variable");
statistic_sub_value_ref.name(type_name + " statistic subvalue");
statistic_expr.name(type_name + " statistic");
named_lookup_expr.name(type_name + " named valueref");
primary_expr.name(type_name + " expression");
expr.name(type_name + " expression");
}
/* The following parsers are defined in separate compilation units to
avoid MSVC running out of memory and throwing:
fatal error C1060: compiler is out of heap space
*/
struct planet_environment_parser_rules :
public enum_value_ref_rules<PlanetEnvironment>
{
planet_environment_parser_rules(const lexer& tok,
Labeller& label,
const condition_parser_grammar& condition_parser);
};
struct planet_size_parser_rules :
public enum_value_ref_rules<PlanetSize>
{
planet_size_parser_rules(const lexer& tok,
Labeller& label,
const condition_parser_grammar& condition_parser);
};
struct planet_type_parser_rules :
public enum_value_ref_rules<PlanetType>
{
planet_type_parser_rules(const lexer& tok,
Labeller& label,
const condition_parser_grammar& condition_parser);
};
struct star_type_parser_rules :
public enum_value_ref_rules<StarType>
{
star_type_parser_rules(const lexer& tok,
Labeller& label,
const condition_parser_grammar& condition_parser);
};
struct visibility_complex_parser_grammar : public complex_variable_grammar<Visibility> {
visibility_complex_parser_grammar(const lexer& tok, Labeller& label);
simple_int_parser_rules simple_int_rules;
complex_variable_rule<Visibility> empire_object_visibility;
complex_variable_rule<Visibility> start;
};
struct visibility_parser_rules :
public enum_value_ref_rules<Visibility>
{
visibility_parser_rules(const lexer& tok,
Labeller& label,
const condition_parser_grammar& condition_parser);
visibility_complex_parser_grammar visibility_var_complex_grammar;
};
struct universe_object_type_parser_rules :
public enum_value_ref_rules<UniverseObjectType>
{
universe_object_type_parser_rules(const lexer& tok,
Labeller& label,
const condition_parser_grammar& condition_parser);
};
}
#endif