1 #ifndef __STAN__GM__PARSER__VAR_DECLS_GRAMMAR_DEF__HPP__
2 #define __STAN__GM__PARSER__VAR_DECLS_GRAMMAR_DEF__HPP__
16 #include <boost/spirit/include/qi.hpp>
18 #include <boost/spirit/include/phoenix_core.hpp>
19 #include <boost/spirit/include/phoenix_function.hpp>
20 #include <boost/spirit/include/phoenix_fusion.hpp>
21 #include <boost/spirit/include/phoenix_object.hpp>
22 #include <boost/spirit/include/phoenix_operator.hpp>
23 #include <boost/spirit/include/phoenix_stl.hpp>
25 #include <boost/lexical_cast.hpp>
26 #include <boost/fusion/include/adapt_struct.hpp>
27 #include <boost/fusion/include/std_pair.hpp>
28 #include <boost/config/warning_disable.hpp>
29 #include <boost/spirit/include/qi.hpp>
30 #include <boost/spirit/include/qi_numeric.hpp>
31 #include <boost/spirit/include/classic_position_iterator.hpp>
32 #include <boost/spirit/include/phoenix_core.hpp>
33 #include <boost/spirit/include/phoenix_function.hpp>
34 #include <boost/spirit/include/phoenix_fusion.hpp>
35 #include <boost/spirit/include/phoenix_object.hpp>
36 #include <boost/spirit/include/phoenix_operator.hpp>
37 #include <boost/spirit/include/phoenix_stl.hpp>
38 #include <boost/spirit/include/support_multi_pass.hpp>
39 #include <boost/tuple/tuple.hpp>
40 #include <boost/variant/apply_visitor.hpp>
41 #include <boost/variant/recursive_variant.hpp>
52 (std::vector<stan::gm::expression>, dims_) )
55 (stan::gm::range, range_)
57 (std::vector<stan::gm::expression>, dims_) )
60 (stan::gm::range, range_)
61 (stan::gm::expression, M_)
63 (std::vector<stan::gm::expression>, dims_) )
66 (stan::gm::range, range_)
67 (stan::gm::expression, N_)
69 (std::vector<stan::gm::expression>, dims_) )
72 (stan::gm::range, range_)
73 (stan::gm::expression, M_)
74 (stan::gm::expression, N_)
76 (std::vector<stan::gm::expression>, dims_) )
79 (stan::gm::expression, K_)
81 (std::vector<stan::gm::expression>, dims_) )
84 (stan::gm::expression, K_)
86 (std::vector<stan::gm::expression>, dims_) )
89 (stan::gm::expression, K_)
91 (std::vector<stan::gm::expression>, dims_) )
94 (stan::gm::expression, K_)
96 (std::vector<stan::gm::expression>, dims_) )
99 (stan::gm::expression, K_)
101 (std::vector<stan::gm::expression>, dims_) )
107 struct validate_no_constraints_vis :
public boost::static_visitor<bool> {
108 std::stringstream& error_msgs_;
109 validate_no_constraints_vis(std::stringstream& error_msgs)
110 : error_msgs_(error_msgs) {
112 bool operator()(
const nil& x)
const {
113 error_msgs_ <<
"nil declarations not allowed";
116 bool operator()(
const int_var_decl& x)
const {
117 if (x.range_.has_low() || x.range_.has_high()) {
118 error_msgs_ <<
"require unconstrained."
119 <<
" found range constraint." << std::endl;
124 bool operator()(
const double_var_decl& x)
const {
125 if (x.range_.has_low() || x.range_.has_high()) {
126 error_msgs_ <<
"require unconstrained."
127 <<
" found range constraint." << std::endl;
132 bool operator()(
const vector_var_decl& x)
const {
135 bool operator()(
const row_vector_var_decl& x)
const {
138 bool operator()(
const matrix_var_decl& x)
const {
141 bool operator()(
const simplex_var_decl& x)
const {
142 error_msgs_ <<
"require unconstrained variable declaration."
143 <<
" found simplex." << std::endl;
146 bool operator()(
const ordered_var_decl& x)
const {
147 error_msgs_ <<
"require unconstrained variable declaration."
148 <<
" found ordered." << std::endl;
151 bool operator()(
const positive_ordered_var_decl& x)
const {
152 error_msgs_ <<
"require unconstrained variable declaration."
153 <<
" found positive_ordered." << std::endl;
156 bool operator()(
const cov_matrix_var_decl& x)
const {
157 error_msgs_ <<
"require unconstrained variable declaration."
158 <<
" found cov_matrix." << std::endl;
161 bool operator()(
const corr_matrix_var_decl& x)
const {
162 error_msgs_ <<
"require unconstrained variable declaration."
163 <<
" found corr_matrix." << std::endl;
168 struct data_only_expression :
public boost::static_visitor<bool> {
169 std::stringstream& error_msgs_;
170 variable_map& var_map_;
171 data_only_expression(std::stringstream& error_msgs,
172 variable_map& var_map)
173 : error_msgs_(error_msgs),
176 bool operator()(
const nil&
e)
const {
179 bool operator()(
const int_literal& x)
const {
182 bool operator()(
const double_literal& x)
const {
185 bool operator()(
const variable& x)
const {
186 var_origin origin = var_map_.get_origin(x.name_);
189 error_msgs_ <<
"non-data variables not allowed in dimension declarations."
191 <<
" found variable=" << x.name_
192 <<
"; declared in block=";
194 error_msgs_ << std::endl;
198 bool operator()(
const fun& x)
const {
199 for (
size_t i = 0; i < x.args_.size(); ++i)
200 if (!boost::apply_visitor(*
this,x.args_[i].expr_))
204 bool operator()(
const index_op& x)
const {
205 if (!boost::apply_visitor(*
this,x.expr_.expr_))
207 for (
size_t i = 0; i < x.dimss_.size(); ++i)
208 for (
size_t j = 0; j < x.dimss_[i].size(); ++j)
209 if (!boost::apply_visitor(*
this,x.dimss_[i][j].expr_))
213 bool operator()(
const binary_op& x)
const {
214 return boost::apply_visitor(*
this,x.left.expr_)
215 && boost::apply_visitor(*
this,x.right.expr_);
217 bool operator()(
const unary_op& x)
const {
218 return boost::apply_visitor(*
this,x.subject.expr_);
224 template <
typename T1,
typename T2,
typename T3,
typename T4,
typename T5>
225 struct result {
typedef T1 type; };
227 template <
typename T>
228 T operator()(
const T& var_decl,
232 std::ostream& error_msgs)
const {
233 if (vm.exists(var_decl.name_)) {
236 error_msgs <<
"variable already declared, name="
242 && var_decl.base_type_ ==
INT_T) {
244 error_msgs <<
"integer parameters or transformed parameters are not allowed; "
245 <<
" found declared type int, parameter name=" << var_decl.name_
250 vm.add(var_decl.name_,var_decl,vo);
254 boost::phoenix::function<add_var> add_var_f;
256 struct validate_decl_constraints {
257 template <
typename T1,
typename T2,
typename T3,
typename T4>
258 struct result {
typedef bool type; };
260 bool operator()(
const bool& allow_constraints,
261 const bool& declaration_ok,
262 const var_decl& var_decl,
263 std::stringstream& error_msgs)
const {
264 if (!declaration_ok) {
265 error_msgs <<
"Problem with declaration." << std::endl;
268 if (allow_constraints)
270 validate_no_constraints_vis vis(error_msgs);
271 bool constraints_ok = boost::apply_visitor(vis,var_decl.decl_);
272 return constraints_ok;
275 boost::phoenix::function<validate_decl_constraints>
276 validate_decl_constraints_f;
278 struct validate_identifier {
279 template <
typename T1,
typename T2>
280 struct result {
typedef bool type; };
282 bool operator()(
const std::string& identifier,
283 std::stringstream& error_msgs)
const {
284 int len = identifier.size();
286 && identifier[len-1] ==
'_'
287 && identifier[len-2] ==
'_') {
288 error_msgs <<
"identifiers cannot end in double underscore (__)"
289 <<
"; found identifer=" << identifier;
295 boost::phoenix::function<validate_identifier> validate_identifier_f;
298 template <
typename T1>
299 struct result {
typedef range type; };
300 range operator()(std::stringstream& error_msgs)
const {
304 boost::phoenix::function<empty_range> empty_range_f;
306 struct validate_int_expr {
307 template <
typename T1,
typename T2>
308 struct result {
typedef bool type; };
310 bool operator()(
const expression& expr,
311 std::stringstream& error_msgs)
const {
312 if (!expr.expression_type().is_primitive_int()) {
313 error_msgs <<
"expression denoting integer required; found type="
314 << expr.expression_type() << std::endl;
320 boost::phoenix::function<validate_int_expr> validate_int_expr_f;
322 struct set_int_range_lower {
323 template <
typename T1,
typename T2,
typename T3>
324 struct result {
typedef bool type; };
325 bool operator()(range& range,
326 const expression& expr,
327 std::stringstream& error_msgs)
const {
329 validate_int_expr validator;
330 return validator(expr,error_msgs);
333 boost::phoenix::function<set_int_range_lower> set_int_range_lower_f;
335 struct set_int_range_upper {
336 template <
typename T1,
typename T2,
typename T3>
337 struct result {
typedef bool type; };
338 bool operator()(range& range,
339 const expression& expr,
340 std::stringstream& error_msgs)
const {
342 validate_int_expr validator;
343 return validator(expr,error_msgs);
346 boost::phoenix::function<set_int_range_upper> set_int_range_upper_f;
350 struct validate_int_data_expr {
351 template <
typename T1,
typename T2,
typename T3>
352 struct result {
typedef bool type; };
354 bool operator()(
const expression& expr,
355 variable_map& var_map,
356 std::stringstream& error_msgs)
const {
357 if (!expr.expression_type().is_primitive_int()) {
358 error_msgs <<
"dimension declaration requires expression denoting integer;"
360 << expr.expression_type()
364 data_only_expression vis(error_msgs,var_map);
365 bool only_data_dimensions = boost::apply_visitor(vis,expr.expr_);
366 return only_data_dimensions;
369 boost::phoenix::function<validate_int_data_expr> validate_int_data_expr_f;
371 struct validate_double_expr {
372 template <
typename T1,
typename T2>
373 struct result {
typedef bool type; };
375 bool operator()(
const expression& expr,
376 std::stringstream& error_msgs)
const {
377 if (!expr.expression_type().is_primitive_double()
378 && !expr.expression_type().is_primitive_int()) {
379 error_msgs <<
"expression denoting double required; found type="
380 << expr.expression_type() << std::endl;
386 boost::phoenix::function<validate_double_expr> validate_double_expr_f;
389 struct set_double_range_lower {
390 template <
typename T1,
typename T2,
typename T3>
391 struct result {
typedef bool type; };
392 bool operator()(range& range,
393 const expression& expr,
394 std::stringstream& error_msgs)
const {
396 validate_double_expr validator;
397 return validator(expr,error_msgs);
400 boost::phoenix::function<set_double_range_lower> set_double_range_lower_f;
402 struct set_double_range_upper {
403 template <
typename T1,
typename T2,
typename T3>
404 struct result {
typedef bool type; };
405 bool operator()(range& range,
406 const expression& expr,
407 std::stringstream& error_msgs)
const {
409 validate_double_expr validator;
410 return validator(expr,error_msgs);
413 boost::phoenix::function<set_double_range_upper> set_double_range_upper_f;
416 template <
typename Iterator>
417 var_decls_grammar<Iterator>::var_decls_grammar(variable_map& var_map,
418 std::stringstream& error_msgs)
419 : var_decls_grammar::base_type(var_decls_r),
421 error_msgs_(error_msgs),
422 expression_g(var_map,error_msgs)
425 using boost::spirit::qi::_1;
426 using boost::spirit::qi::char_;
427 using boost::spirit::qi::eps;
428 using boost::spirit::qi::lexeme;
429 using boost::spirit::qi::lit;
430 using boost::spirit::qi::no_skip;
431 using boost::spirit::qi::_pass;
432 using boost::spirit::qi::_val;
433 using boost::spirit::qi::labels::_a;
434 using boost::spirit::qi::labels::_r1;
435 using boost::spirit::qi::labels::_r2;
437 var_decls_r.name(
"variable declarations");
439 %= *var_decl_r(_r1,_r2);
442 var_decl_r.name(
"variable declaration");
445 [_val = add_var_f(_1,boost::phoenix::ref(var_map_),_a,_r2,
446 boost::phoenix::ref(error_msgs))]
448 [_val = add_var_f(_1,boost::phoenix::ref(var_map_),_a,_r2,
449 boost::phoenix::ref(error_msgs_))]
451 [_val = add_var_f(_1,boost::phoenix::ref(var_map_),_a,_r2,
452 boost::phoenix::ref(error_msgs_))]
454 [_val = add_var_f(_1,boost::phoenix::ref(var_map_),_a,_r2,
455 boost::phoenix::ref(error_msgs_))]
457 [_val = add_var_f(_1,boost::phoenix::ref(var_map_),_a,_r2,
458 boost::phoenix::ref(error_msgs_))]
460 [_val = add_var_f(_1,boost::phoenix::ref(var_map_),_a,_r2,
461 boost::phoenix::ref(error_msgs_))]
463 [_val = add_var_f(_1,boost::phoenix::ref(var_map_),_a,_r2,
464 boost::phoenix::ref(error_msgs_))]
465 | positive_ordered_decl_r
466 [_val = add_var_f(_1,boost::phoenix::ref(var_map_),_a,_r2,
467 boost::phoenix::ref(error_msgs_))]
469 [_val = add_var_f(_1,boost::phoenix::ref(var_map_),_a,_r2,
470 boost::phoenix::ref(error_msgs_))]
472 [_val = add_var_f(_1,boost::phoenix::ref(var_map_),_a,_r2,
473 boost::phoenix::ref(error_msgs_))]
477 = validate_decl_constraints_f(_r1,_a,_val,
478 boost::phoenix::ref(error_msgs_))]
481 int_decl_r.name(
"integer declaration");
484 >> no_skip[!char_(
"a-zA-Z0-9_")]
485 > -range_brackets_int_r
491 double_decl_r.name(
"real declaration");
494 >> no_skip[!char_(
"a-zA-Z0-9_")]
495 > -range_brackets_double_r
500 vector_decl_r.name(
"vector declaration");
503 > -range_brackets_double_r
506 [_pass = validate_int_expr_f(_1,boost::phoenix::ref(error_msgs_))]
512 row_vector_decl_r.name(
"row vector declaration");
515 > -range_brackets_double_r
518 [_pass = validate_int_expr_f(_1,boost::phoenix::ref(error_msgs_))]
524 matrix_decl_r.name(
"matrix declaration");
527 > -range_brackets_double_r
530 [_pass = validate_int_expr_f(_1,boost::phoenix::ref(error_msgs_))]
533 [_pass = validate_int_expr_f(_1,boost::phoenix::ref(error_msgs_))]
539 simplex_decl_r.name(
"simplex declaration");
544 [_pass = validate_int_expr_f(_1,boost::phoenix::ref(error_msgs_))]
550 ordered_decl_r.name(
"ordered declaration");
555 [_pass = validate_int_expr_f(_1,boost::phoenix::ref(error_msgs_))]
561 positive_ordered_decl_r.name(
"positive_ordered declaration");
562 positive_ordered_decl_r
563 %= lit(
"positive_ordered")
566 [_pass = validate_int_expr_f(_1,boost::phoenix::ref(error_msgs_))]
572 corr_matrix_decl_r.name(
"correlation matrix declaration");
574 %= lit(
"corr_matrix")
577 [_pass = validate_int_expr_f(_1,boost::phoenix::ref(error_msgs_))]
583 cov_matrix_decl_r.name(
"covariance matrix declaration");
588 [_pass = validate_int_expr_f(_1,boost::phoenix::ref(error_msgs_))]
594 opt_dims_r.name(
"array dimensions (optional)");
598 dims_r.name(
"array dimensions");
602 [_pass = validate_int_data_expr_f(_1,
603 boost::phoenix::ref(var_map_),
604 boost::phoenix::ref(error_msgs_))]
609 range_brackets_int_r.name(
"integer range expression pair, brackets");
611 = lit(
'<') [_val = empty_range_f(boost::phoenix::ref(error_msgs_))]
616 [ _pass = set_int_range_lower_f(_val,_1,
617 boost::phoenix::ref(error_msgs_)) ])
622 [ _pass = set_int_range_upper_f(_val,_1,
623 boost::phoenix::ref(error_msgs_)) ] ) )
628 [ _pass = set_int_range_upper_f(_val,_1,
629 boost::phoenix::ref(error_msgs_)) ])
633 range_brackets_double_r.name(
"real range expression pair, brackets");
634 range_brackets_double_r
635 = lit(
'<') [_val = empty_range_f(boost::phoenix::ref(error_msgs_))]
640 [ _pass = set_double_range_lower_f(_val,_1,
641 boost::phoenix::ref(error_msgs_)) ])
646 [ _pass = set_double_range_upper_f(_val,_1,
647 boost::phoenix::ref(error_msgs_)) ] ) )
652 [ _pass = set_double_range_upper_f(_val,_1,
653 boost::phoenix::ref(error_msgs_)) ])
657 identifier_r.name(
"identifier");
660 [_pass = validate_identifier_f(_1,boost::phoenix::ref(error_msgs_))]
663 identifier_name_r.name(
"identifier subrule");
665 %= lexeme[char_(
"a-zA-Z")
666 >> *char_(
"a-zA-Z0-9_.")]
670 range_r.name(
"range expression pair, colon");
673 [_pass = validate_int_expr_f(_1,boost::phoenix::ref(error_msgs_))]
676 [_pass = validate_int_expr_f(_1,boost::phoenix::ref(error_msgs_))];