Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
var_decls_grammar_def.hpp
Go to the documentation of this file.
1 #ifndef STAN__GM__PARSER__VAR_DECLS_GRAMMAR_DEF__HPP
2 #define STAN__GM__PARSER__VAR_DECLS_GRAMMAR_DEF__HPP
3 
4 #include <boost/spirit/include/qi.hpp>
5 // FIXME: get rid of unused include
6 #include <boost/spirit/include/phoenix_core.hpp>
7 #include <boost/spirit/include/phoenix_function.hpp>
8 #include <boost/spirit/include/phoenix_fusion.hpp>
9 #include <boost/spirit/include/phoenix_object.hpp>
10 #include <boost/spirit/include/phoenix_operator.hpp>
11 #include <boost/spirit/include/phoenix_stl.hpp>
12 
13 #include <boost/lexical_cast.hpp>
14 #include <boost/fusion/include/adapt_struct.hpp>
15 #include <boost/fusion/include/std_pair.hpp>
16 #include <boost/config/warning_disable.hpp>
17 #include <boost/spirit/include/qi_numeric.hpp>
18 #include <stan/gm/ast.hpp>
21 
23  (stan::gm::range, range_)
24  (std::string, name_)
25  (std::vector<stan::gm::expression>, dims_) )
26 
27 BOOST_FUSION_ADAPT_STRUCT(stan::gm::double_var_decl,
28  (stan::gm::range, range_)
29  (std::string, name_)
30  (std::vector<stan::gm::expression>, dims_) )
31 
32 BOOST_FUSION_ADAPT_STRUCT(stan::gm::vector_var_decl,
33  (stan::gm::range, range_)
34  (stan::gm::expression, M_)
35  (std::string, name_)
36  (std::vector<stan::gm::expression>, dims_) )
37 
38 BOOST_FUSION_ADAPT_STRUCT(stan::gm::row_vector_var_decl,
39  (stan::gm::range, range_)
40  (stan::gm::expression, N_)
41  (std::string, name_)
42  (std::vector<stan::gm::expression>, dims_) )
43 
44 BOOST_FUSION_ADAPT_STRUCT(stan::gm::matrix_var_decl,
45  (stan::gm::range, range_)
46  (stan::gm::expression, M_)
47  (stan::gm::expression, N_)
48  (std::string, name_)
49  (std::vector<stan::gm::expression>, dims_) )
50 
51 BOOST_FUSION_ADAPT_STRUCT(stan::gm::unit_vector_var_decl,
52  (stan::gm::expression, K_)
53  (std::string, name_)
54  (std::vector<stan::gm::expression>, dims_) )
55 
56 BOOST_FUSION_ADAPT_STRUCT(stan::gm::simplex_var_decl,
57  (stan::gm::expression, K_)
58  (std::string, name_)
59  (std::vector<stan::gm::expression>, dims_) )
60 
61 BOOST_FUSION_ADAPT_STRUCT(stan::gm::ordered_var_decl,
62  (stan::gm::expression, K_)
63  (std::string, name_)
64  (std::vector<stan::gm::expression>, dims_) )
65 
66 BOOST_FUSION_ADAPT_STRUCT(stan::gm::positive_ordered_var_decl,
67  (stan::gm::expression, K_)
68  (std::string, name_)
69  (std::vector<stan::gm::expression>, dims_) )
70 
71 BOOST_FUSION_ADAPT_STRUCT(stan::gm::cholesky_factor_var_decl,
72  (stan::gm::expression, M_)
73  (stan::gm::expression, N_)
74  (std::string, name_)
75  (std::vector<stan::gm::expression>, dims_) )
76 
77 BOOST_FUSION_ADAPT_STRUCT(stan::gm::cholesky_corr_var_decl,
78  (stan::gm::expression, K_)
79  (std::string, name_)
80  (std::vector<stan::gm::expression>, dims_) )
81 
82 BOOST_FUSION_ADAPT_STRUCT(stan::gm::cov_matrix_var_decl,
83  (stan::gm::expression, K_)
84  (std::string, name_)
85  (std::vector<stan::gm::expression>, dims_) )
86 
87 BOOST_FUSION_ADAPT_STRUCT(stan::gm::corr_matrix_var_decl,
88  (stan::gm::expression, K_)
89  (std::string, name_)
90  (std::vector<stan::gm::expression>, dims_) )
91 
92 namespace stan {
93 
94  namespace gm {
95 
96  struct validate_no_constraints_vis : public boost::static_visitor<bool> {
97  std::stringstream& error_msgs_;
98  validate_no_constraints_vis(std::stringstream& error_msgs)
99  : error_msgs_(error_msgs) {
100  }
101  bool operator()(const nil& /*x*/) const {
102  error_msgs_ << "nil declarations not allowed";
103  return false; // fail if arises
104  }
105  bool operator()(const int_var_decl& x) const {
106  if (x.range_.has_low() || x.range_.has_high()) {
107  error_msgs_ << "require unconstrained."
108  << " found range constraint." << std::endl;
109  return false;
110  }
111  return true;
112  }
113  bool operator()(const double_var_decl& x) const {
114  if (x.range_.has_low() || x.range_.has_high()) {
115  error_msgs_ << "require unconstrained."
116  << " found range constraint." << std::endl;
117  return false;
118  }
119  return true;
120  }
121  bool operator()(const vector_var_decl& /*x*/) const {
122  return true;
123  }
124  bool operator()(const row_vector_var_decl& /*x*/) const {
125  return true;
126  }
127  bool operator()(const matrix_var_decl& /*x*/) const {
128  return true;
129  }
130  bool operator()(const unit_vector_var_decl& /*x*/) const {
131  error_msgs_ << "require unconstrained variable declaration."
132  << " found unit_vector." << std::endl;
133  return false;
134  }
135  bool operator()(const simplex_var_decl& /*x*/) const {
136  error_msgs_ << "require unconstrained variable declaration."
137  << " found simplex." << std::endl;
138  return false;
139  }
140  bool operator()(const ordered_var_decl& /*x*/) const {
141  error_msgs_ << "require unconstrained variable declaration."
142  << " found ordered." << std::endl;
143  return false;
144  }
145  bool operator()(const positive_ordered_var_decl& /*x*/) const {
146  error_msgs_ << "require unconstrained variable declaration."
147  << " found positive_ordered." << std::endl;
148  return false;
149  }
150  bool operator()(const cholesky_factor_var_decl& /*x*/) const {
151  error_msgs_ << "require unconstrained variable declaration."
152  << " found cholesky_factor." << std::endl;
153  return false;
154  }
155  bool operator()(const cholesky_corr_var_decl& /*x*/) const {
156  error_msgs_ << "require unconstrained variable declaration."
157  << " found cholesky_factor_corr." << std::endl;
158  return false;
159  }
160  bool operator()(const cov_matrix_var_decl& /*x*/) const {
161  error_msgs_ << "require unconstrained variable declaration."
162  << " found cov_matrix." << std::endl;
163  return false;
164  }
165  bool operator()(const corr_matrix_var_decl& /*x*/) const {
166  error_msgs_ << "require unconstrained variable declaration."
167  << " found corr_matrix." << std::endl;
168  return false;
169  }
170  };
171 
172  struct data_only_expression : public boost::static_visitor<bool> {
173  std::stringstream& error_msgs_;
174  variable_map& var_map_;
175  data_only_expression(std::stringstream& error_msgs,
176  variable_map& var_map)
177  : error_msgs_(error_msgs),
178  var_map_(var_map) {
179  }
180  bool operator()(const nil& /*e*/) const {
181  return true;
182  }
183  bool operator()(const int_literal& /*x*/) const {
184  return true;
185  }
186  bool operator()(const double_literal& /*x*/) const {
187  return true;
188  }
189  bool operator()(const array_literal& x) const {
190  for (size_t i = 0; i < x.args_.size(); ++i)
191  if (!boost::apply_visitor(*this,x.args_[i].expr_))
192  return false;
193  return true;
194  }
195  bool operator()(const variable& x) const {
196  var_origin origin = var_map_.get_origin(x.name_);
197  bool is_data = (origin == data_origin)
198  || (origin == transformed_data_origin)
199  || (origin == local_origin);
200  if (!is_data) {
201  error_msgs_ << "non-data variables not allowed in dimension declarations."
202  << std::endl
203  << " found variable=" << x.name_
204  << "; declared in block=";
205  print_var_origin(error_msgs_,origin);
206  error_msgs_ << std::endl;
207  }
208  return is_data;
209  }
210  bool operator()(const integrate_ode& x) const {
211  return boost::apply_visitor(*this, x.y0_.expr_)
212  && boost::apply_visitor(*this, x.theta_.expr_);
213  }
214  bool operator()(const fun& x) const {
215  for (size_t i = 0; i < x.args_.size(); ++i)
216  if (!boost::apply_visitor(*this,x.args_[i].expr_))
217  return false;
218  return true;
219  }
220  bool operator()(const index_op& x) const {
221  if (!boost::apply_visitor(*this,x.expr_.expr_))
222  return false;
223  for (size_t i = 0; i < x.dimss_.size(); ++i)
224  for (size_t j = 0; j < x.dimss_[i].size(); ++j)
225  if (!boost::apply_visitor(*this,x.dimss_[i][j].expr_))
226  return false;
227  return true;
228  }
229  bool operator()(const binary_op& x) const {
230  return boost::apply_visitor(*this,x.left.expr_)
231  && boost::apply_visitor(*this,x.right.expr_);
232  }
233  bool operator()(const unary_op& x) const {
234  return boost::apply_visitor(*this,x.subject.expr_);
235  }
236  };
237 
238 
239  struct add_var {
240  template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
241  struct result { typedef void type; };
242  // each type derived from base_var_decl gets own instance
243  template <typename R, typename T>
244  void operator()(R& var_decl_result,
245  const T& var_decl,
246  variable_map& vm,
247  bool& pass,
248  const var_origin& vo,
249  std::ostream& error_msgs) const {
250  if (vm.exists(var_decl.name_)) {
251  // variable already exists
252  pass = false;
253  error_msgs << "duplicate declaration of variable, name="
254  << var_decl.name_;
255 
256  error_msgs << "; attempt to redeclare as ";
257  print_var_origin(error_msgs,vo); // FIXME -- need original vo
258 
259  error_msgs << "; original declaration as ";
260  print_var_origin(error_msgs,vm.get_origin(var_decl.name_));
261 
262  error_msgs << std::endl;
263  var_decl_result = var_decl;
264  return;
265  }
267  && var_decl.base_type_ == INT_T) {
268  pass = false;
269  error_msgs << "integer parameters or transformed parameters are not allowed; "
270  << " found declared type int, parameter name=" << var_decl.name_
271  << std::endl;
272  var_decl_result = var_decl;
273  return;
274  }
275  pass = true; // probably don't need to set true
276  vm.add(var_decl.name_,var_decl,vo);
277  var_decl_result = var_decl;
278  }
279  };
280  boost::phoenix::function<add_var> add_var_f;
281 
282 
284  template <typename T1, typename T2, typename T3, typename T4>
285  struct result { typedef bool type; };
286 
287  bool operator()(const bool& allow_constraints,
288  const bool& declaration_ok,
289  const var_decl& var_decl,
290  std::stringstream& error_msgs) const {
291  if (!declaration_ok) {
292  error_msgs << "Problem with declaration." << std::endl;
293  return false; // short-circuits test of constraints
294  }
295  if (allow_constraints)
296  return true;
297  validate_no_constraints_vis vis(error_msgs);
298  bool constraints_ok = boost::apply_visitor(vis,var_decl.decl_);
299  return constraints_ok;
300  }
301  };
302  boost::phoenix::function<validate_decl_constraints>
304 
306  std::set<std::string> reserved_word_set_;
307  std::set<std::string> const_fun_name_set_;
308 
309  template <typename T1, typename T2>
310  struct result { typedef bool type; };
311 
312  void reserve(const std::string& w) {
313  reserved_word_set_.insert(w);
314  }
315 
316  template <typename S, typename T>
317  static bool contains(const S& s,
318  const T& x) {
319  return s.find(x) != s.end();
320  }
321 
322  bool identifier_exists(const std::string& identifier) const {
323  return contains(reserved_word_set_, identifier)
324  || ( contains(function_signatures::instance().key_set(), identifier)
325  && !contains(const_fun_name_set_, identifier) );
326  }
327 
329  // Constant functions which can be used as identifiers
330  const_fun_name_set_.insert("pi");
331  const_fun_name_set_.insert("e");
332  const_fun_name_set_.insert("sqrt2");
333  const_fun_name_set_.insert("log2");
334  const_fun_name_set_.insert("log10");
335  const_fun_name_set_.insert("not_a_number");
336  const_fun_name_set_.insert("positive_infinity");
337  const_fun_name_set_.insert("negative_infinity");
338  const_fun_name_set_.insert("epsilon");
339  const_fun_name_set_.insert("negative_epsilon");
340 
341  // illegal identifiers
342  reserve("for");
343  reserve("in");
344  reserve("while");
345  reserve("repeat");
346  reserve("until");
347  reserve("if");
348  reserve("then");
349  reserve("else");
350  reserve("true");
351  reserve("false");
352 
353  reserve("int");
354  reserve("real");
355  reserve("vector");
356  reserve("unit_vector");
357  reserve("simplex");
358  reserve("ordered");
359  reserve("positive_ordered");
360  reserve("row_vector");
361  reserve("matrix");
362  reserve("cholesky_factor_cov");
363  reserve("cholesky_factor_corr");
364  reserve("cov_matrix");
365  reserve("corr_matrix");
366 
367 
368  reserve("model");
369  reserve("data");
370  reserve("parameters");
371  reserve("quantities");
372  reserve("transformed");
373  reserve("generated");
374 
375  reserve("var");
376 
377  reserve("alignas");
378  reserve("alignof");
379  reserve("and");
380  reserve("and_eq");
381  reserve("asm");
382  reserve("auto");
383  reserve("bitand");
384  reserve("bitor");
385  reserve("bool");
386  reserve("break");
387  reserve("case");
388  reserve("catch");
389  reserve("char");
390  reserve("char16_t");
391  reserve("char32_t");
392  reserve("class");
393  reserve("compl");
394  reserve("const");
395  reserve("constexpr");
396  reserve("const_cast");
397  reserve("continue");
398  reserve("decltype");
399  reserve("default");
400  reserve("delete");
401  reserve("do");
402  reserve("double");
403  reserve("dynamic_cast");
404  reserve("else");
405  reserve("enum");
406  reserve("explicit");
407  reserve("export");
408  reserve("extern");
409  reserve("false");
410  reserve("float");
411  reserve("for");
412  reserve("friend");
413  reserve("goto");
414  reserve("if");
415  reserve("inline");
416  reserve("int");
417  reserve("long");
418  reserve("mutable");
419  reserve("namespace");
420  reserve("new");
421  reserve("noexcept");
422  reserve("not");
423  reserve("not_eq");
424  reserve("nullptr");
425  reserve("operator");
426  reserve("or");
427  reserve("or_eq");
428  reserve("private");
429  reserve("protected");
430  reserve("public");
431  reserve("register");
432  reserve("reinterpret_cast");
433  reserve("return");
434  reserve("short");
435  reserve("signed");
436  reserve("sizeof");
437  reserve("static");
438  reserve("static_assert");
439  reserve("static_cast");
440  reserve("struct");
441  reserve("switch");
442  reserve("template");
443  reserve("this");
444  reserve("thread_local");
445  reserve("throw");
446  reserve("true");
447  reserve("try");
448  reserve("typedef");
449  reserve("typeid");
450  reserve("typename");
451  reserve("union");
452  reserve("unsigned");
453  reserve("using");
454  reserve("virtual");
455  reserve("void");
456  reserve("volatile");
457  reserve("wchar_t");
458  reserve("while");
459  reserve("xor");
460  reserve("xor_eq");
461 
462  // function names declared in signatures
464  using std::set;
465  using std::string;
466  const function_signatures& sigs = function_signatures::instance();
467 
468  set<string> fun_names = sigs.key_set();
469  for (set<string>::iterator it = fun_names.begin(); it != fun_names.end(); ++it)
470  if (!contains(const_fun_name_set_, *it))
471  reserve(*it);
472  }
473 
474  bool operator()(const std::string& identifier,
475  std::stringstream& error_msgs) const {
476  int len = identifier.size();
477  if (len >= 2
478  && identifier[len-1] == '_'
479  && identifier[len-2] == '_') {
480  error_msgs << "variable identifier (name) may not end in double underscore (__)"
481  << std::endl
482  << " found identifer=" << identifier << std::endl;
483  return false;
484  }
485  size_t period_position = identifier.find('.');
486  if (period_position != std::string::npos) {
487  error_msgs << "variable identifier may not contain a period (.)"
488  << std::endl
489  << " found period at position (indexed from 0)=" << period_position
490  << std::endl
491  << " found identifier=" << identifier
492  << std::endl;
493  return false;
494  }
495  if (identifier_exists(identifier)) {
496  error_msgs << "variable identifier (name) may not be reserved word"
497  << std::endl
498  << " found identifier=" << identifier
499  << std::endl;
500  return false;
501  }
502  return true;
503  }
504  };
505  boost::phoenix::function<validate_identifier> validate_identifier_f;
506 
507  // copies single dimension from M to N if only M declared
509  template <typename T1>
510  struct result { typedef void type; };
511  void operator()(cholesky_factor_var_decl& var_decl) const {
512  if (is_nil(var_decl.N_))
513  var_decl.N_ = var_decl.M_;
514  }
515  };
516  boost::phoenix::function<copy_square_cholesky_dimension_if_necessary>
518 
519  struct empty_range {
520  template <typename T1>
521  struct result { typedef range type; };
522  range operator()(std::stringstream& /*error_msgs*/) const {
523  return range();
524  }
525  };
526  boost::phoenix::function<empty_range> empty_range_f;
527 
529  template <typename T1, typename T2, typename T3>
530  struct result { typedef void type; };
531 
532  void operator()(const expression& expr,
533  bool& pass,
534  std::stringstream& error_msgs) const {
535  if (!expr.expression_type().is_primitive_int()) {
536  error_msgs << "expression denoting integer required; found type="
537  << expr.expression_type() << std::endl;
538 
539  pass = false;
540  return;
541  }
542  pass = true;
543  }
544  };
545  boost::phoenix::function<validate_int_expr> validate_int_expr_f;
546 
548  template <typename T1, typename T2, typename T3, typename T4>
549  struct result { typedef void type; };
550  void operator()(range& range,
551  const expression& expr,
552  bool& pass,
553  std::stringstream& error_msgs) const {
554  range.low_ = expr;
555  validate_int_expr validator;
556  validator(expr,pass,error_msgs);
557  }
558  };
559  boost::phoenix::function<set_int_range_lower> set_int_range_lower_f;
560 
562  template <typename T1, typename T2, typename T3, typename T4>
563  struct result { typedef void type; };
564  void operator()(range& range,
565  const expression& expr,
566  bool& pass,
567  std::stringstream& error_msgs) const {
568  range.high_ = expr;
569  validate_int_expr validator;
570  validator(expr,pass,error_msgs);
571  }
572  };
573  boost::phoenix::function<set_int_range_upper> set_int_range_upper_f;
574 
575 
576 
578  template <typename T1, typename T2, typename T3, typename T4, typename T5>
579  struct result { typedef void type; };
580 
581  void operator()(const expression& expr,
582  int var_origin,
583  bool& pass,
584  variable_map& var_map,
585  std::stringstream& error_msgs) const {
586  if (!expr.expression_type().is_primitive_int()) {
587  error_msgs << "dimension declaration requires expression denoting integer;"
588  << " found type="
589  << expr.expression_type()
590  << std::endl;
591  pass = false;
592  } else if (var_origin != local_origin) {
593  data_only_expression vis(error_msgs,var_map);
594  bool only_data_dimensions = boost::apply_visitor(vis,expr.expr_);
595  pass = only_data_dimensions;
596  } else {
597  // don't need to check data vs. parameter in dimensions for
598  // local variable declarations
599  pass = true;
600  }
601  }
602  };
603  boost::phoenix::function<validate_int_data_expr> validate_int_data_expr_f;
604 
606  template <typename T1, typename T2>
607  struct result { typedef bool type; };
608 
609  bool operator()(const expression& expr,
610  std::stringstream& error_msgs) const {
611  if (!expr.expression_type().is_primitive_double()
612  && !expr.expression_type().is_primitive_int()) {
613  error_msgs << "expression denoting real required; found type="
614  << expr.expression_type() << std::endl;
615  return false;
616  }
617  return true;
618  }
619  };
620  boost::phoenix::function<validate_double_expr> validate_double_expr_f;
621 
622 
624  template <typename T1, typename T2, typename T3, typename T4>
625  struct result { typedef void type; };
626  void operator()(range& range,
627  const expression& expr,
628  bool& pass,
629  std::stringstream& error_msgs) const {
630  range.low_ = expr;
631  validate_double_expr validator;
632  pass = validator(expr,error_msgs);
633  }
634  };
635  boost::phoenix::function<set_double_range_lower> set_double_range_lower_f;
636 
638  template <typename T1, typename T2, typename T3, typename T4>
639  struct result { typedef void type; };
640  void operator()(range& range,
641  const expression& expr,
642  bool& pass,
643  std::stringstream& error_msgs) const {
644  range.high_ = expr;
645  validate_double_expr validator;
646  pass = validator(expr,error_msgs);
647  }
648  };
649  boost::phoenix::function<set_double_range_upper> set_double_range_upper_f;
650 
651 
652  template <typename Iterator>
654  std::stringstream& error_msgs)
655  : var_decls_grammar::base_type(var_decls_r),
656  var_map_(var_map),
657  error_msgs_(error_msgs),
658  // expression_g allows full recursion
659  expression_g(var_map,error_msgs),
660  // expression07_g disallows comparisons
661  expression07_g(var_map,error_msgs,expression_g)
662  {
663 
664  using boost::spirit::qi::_1;
665  using boost::spirit::qi::_3;
666  using boost::spirit::qi::char_;
667  using boost::spirit::qi::eps;
668  using boost::spirit::qi::lexeme;
669  using boost::spirit::qi::lit;
670  using boost::spirit::qi::no_skip;
671  using boost::spirit::qi::_pass;
672  using boost::spirit::qi::_val;
673  using boost::spirit::qi::labels::_a;
674  using boost::spirit::qi::labels::_r1;
675  using boost::spirit::qi::labels::_r2;
676 
677  var_decls_r.name("variable declarations");
678  var_decls_r
679  %= *(var_decl_r(_r1,_r2))
680  ;
681 
682  // _a = error state local,
683  // _r1 constraints allowed inherited,
684  // _r2 var_origin
685  var_decl_r.name("variable declaration");
686  var_decl_r
687  %= (int_decl_r(_r2)
688  [add_var_f(_val,_1,boost::phoenix::ref(var_map_),_a,_r2,
689  boost::phoenix::ref(error_msgs))]
690  | double_decl_r(_r2)
691  [add_var_f(_val,_1,boost::phoenix::ref(var_map_),_a,_r2,
692  boost::phoenix::ref(error_msgs_))]
693  | vector_decl_r(_r2)
694  [add_var_f(_val,_1,boost::phoenix::ref(var_map_),_a,_r2,
695  boost::phoenix::ref(error_msgs_))]
696  | row_vector_decl_r(_r2)
697  [add_var_f(_val,_1,boost::phoenix::ref(var_map_),_a,_r2,
698  boost::phoenix::ref(error_msgs_))]
699  | matrix_decl_r(_r2)
700  [add_var_f(_val,_1,boost::phoenix::ref(var_map_),_a,_r2,
701  boost::phoenix::ref(error_msgs_))]
702  | unit_vector_decl_r(_r2)
703  [add_var_f(_val,_1,boost::phoenix::ref(var_map_),_a,_r2,
704  boost::phoenix::ref(error_msgs_))]
705  | simplex_decl_r(_r2)
706  [add_var_f(_val,_1,boost::phoenix::ref(var_map_),_a,_r2,
707  boost::phoenix::ref(error_msgs_))]
708  | ordered_decl_r(_r2)
709  [add_var_f(_val,_1,boost::phoenix::ref(var_map_),_a,_r2,
710  boost::phoenix::ref(error_msgs_))]
712  [add_var_f(_val,_1,boost::phoenix::ref(var_map_),_a,_r2,
713  boost::phoenix::ref(error_msgs_))]
714  | cholesky_factor_decl_r(_r2)
715  [add_var_f(_val,_1,boost::phoenix::ref(var_map_),_a,_r2,
716  boost::phoenix::ref(error_msgs_))]
717  | cholesky_corr_decl_r(_r2)
718  [add_var_f(_val,_1,boost::phoenix::ref(var_map_),_a,_r2,
719  boost::phoenix::ref(error_msgs_))]
720  | cov_matrix_decl_r(_r2)
721  [add_var_f(_val,_1,boost::phoenix::ref(var_map_),_a,_r2,
722  boost::phoenix::ref(error_msgs_))]
723  | corr_matrix_decl_r(_r2)
724  [add_var_f(_val,_1,boost::phoenix::ref(var_map_),_a,_r2,
725  boost::phoenix::ref(error_msgs_))]
726  )
727  > lit(';')
728  [_pass
729  = validate_decl_constraints_f(_r1,_a,_val,
730  boost::phoenix::ref(error_msgs_))]
731  ;
732 
733  int_decl_r.name("integer declaration");
734  int_decl_r
735  %= ( lit("int")
736  >> no_skip[!char_("a-zA-Z0-9_")] )
737  > -range_brackets_int_r(_r1)
738  > identifier_r
739  > opt_dims_r(_r1)
740  ;
741 
742  double_decl_r.name("real declaration");
744  %= ( lit("real")
745  >> no_skip[!char_("a-zA-Z0-9_")] )
747  > identifier_r
748  > opt_dims_r(_r1)
749  ;
750 
751  vector_decl_r.name("vector declaration");
753  %= ( lit("vector")
754  >> no_skip[!char_("a-zA-Z0-9_")] )
756  > lit('[')
757  > expression_g(_r1)
758  [validate_int_expr_f(_1,_pass,boost::phoenix::ref(error_msgs_))]
759  > lit(']')
760  > identifier_r
761  > opt_dims_r(_r1)
762  ;
763 
764  row_vector_decl_r.name("row vector declaration");
766  %= ( lit("row_vector")
767  >> no_skip[!char_("a-zA-Z0-9_")] )
769  > lit('[')
770  > expression_g(_r1)
771  [validate_int_expr_f(_1,_pass,boost::phoenix::ref(error_msgs_))]
772  > lit(']')
773  > identifier_r
774  > opt_dims_r(_r1)
775  ;
776 
777  matrix_decl_r.name("matrix declaration");
779  %= ( lit("matrix")
780  >> no_skip[!char_("a-zA-Z0-9_")] )
782  > lit('[')
783  > expression_g(_r1)
784  [validate_int_expr_f(_1,_pass,boost::phoenix::ref(error_msgs_))]
785  > lit(',')
786  > expression_g(_r1)
787  [validate_int_expr_f(_1,_pass,boost::phoenix::ref(error_msgs_))]
788  > lit(']')
789  > identifier_r
790  > opt_dims_r(_r1)
791  ;
792 
793  unit_vector_decl_r.name("unit_vector declaration");
795  %= ( lit("unit_vector")
796  >> no_skip[!char_("a-zA-Z0-9_")] )
797  > lit('[')
798  > expression_g(_r1)
799  [validate_int_expr_f(_1,_pass,boost::phoenix::ref(error_msgs_))]
800  > lit(']')
801  > identifier_r
802  > opt_dims_r(_r1)
803  ;
804 
805  simplex_decl_r.name("simplex declaration");
807  %= ( lit("simplex")
808  >> no_skip[!char_("a-zA-Z0-9_")] )
809  > lit('[')
810  > expression_g(_r1)
811  [validate_int_expr_f(_1,_pass,boost::phoenix::ref(error_msgs_))]
812  > lit(']')
813  > identifier_r
814  > opt_dims_r(_r1)
815  ;
816 
817  ordered_decl_r.name("ordered declaration");
819  %= ( lit("ordered")
820  >> no_skip[!char_("a-zA-Z0-9_")] )
821  > lit('[')
822  > expression_g(_r1)
823  [validate_int_expr_f(_1,_pass,boost::phoenix::ref(error_msgs_))]
824  > lit(']')
825  > identifier_r
826  > opt_dims_r(_r1)
827  ;
828 
829  positive_ordered_decl_r.name("positive_ordered declaration");
831  %= ( lit("positive_ordered")
832  >> no_skip[!char_("a-zA-Z0-9_")] )
833  > lit('[')
834  > expression_g(_r1)
835  [validate_int_expr_f(_1,_pass,boost::phoenix::ref(error_msgs_))]
836  > lit(']')
837  > identifier_r
838  > opt_dims_r(_r1)
839  ;
840 
841  cholesky_factor_decl_r.name("cholesky factor for symmetric, positive-def declaration");
843  %= ( lit("cholesky_factor_cov")
844  >> no_skip[!char_("a-zA-Z0-9_")] )
845  > lit('[')
846  > expression_g(_r1)
847  [validate_int_expr_f(_1,_pass,boost::phoenix::ref(error_msgs_))]
848  > -( lit(',')
849  > expression_g(_r1)
850  [validate_int_expr_f(_1,_pass,boost::phoenix::ref(error_msgs_))]
851  )
852  > lit(']')
853  > identifier_r
854  > opt_dims_r(_r1)
855  > eps
857  ;
858 
859  cholesky_corr_decl_r.name("cholesky factor for correlation matrix declaration");
861  %= ( lit("cholesky_factor_corr")
862  >> no_skip[!char_("a-zA-Z0-9_")] )
863  > lit('[')
864  > expression_g(_r1)
865  [validate_int_expr_f(_1,_pass,boost::phoenix::ref(error_msgs_))]
866  > lit(']')
867  > identifier_r
868  > opt_dims_r(_r1)
869  ;
870 
871  cov_matrix_decl_r.name("covariance matrix declaration");
873  %= ( lit("cov_matrix")
874  >> no_skip[!char_("a-zA-Z0-9_")] )
875  > lit('[')
876  > expression_g(_r1)
877  [validate_int_expr_f(_1,_pass,boost::phoenix::ref(error_msgs_))]
878  > lit(']')
879  > identifier_r
880  > opt_dims_r(_r1)
881  ;
882 
883  corr_matrix_decl_r.name("correlation matrix declaration");
885  %= ( lit("corr_matrix")
886  >> no_skip[!char_("a-zA-Z0-9_")] )
887  > lit('[')
888  > expression_g(_r1)
889  [validate_int_expr_f(_1,_pass,boost::phoenix::ref(error_msgs_))]
890  > lit(']')
891  > identifier_r
892  > opt_dims_r(_r1)
893  ;
894 
895  opt_dims_r.name("array dimensions (optional)");
896  opt_dims_r
897  %= - dims_r(_r1);
898 
899  dims_r.name("array dimensions");
900  dims_r
901  %= lit('[')
902  > (expression_g(_r1)
903  [validate_int_data_expr_f(_1,_r1,_pass,
904  boost::phoenix::ref(var_map_),
905  boost::phoenix::ref(error_msgs_))]
906  % ',')
907  > lit(']')
908  ;
909 
910  range_brackets_int_r.name("integer range expression pair, brackets");
912  = lit('<') [_val = empty_range_f(boost::phoenix::ref(error_msgs_))]
913  >> (
914  ( (lit("lower")
915  >> lit('=')
916  >> expression07_g(_r1)
917  [set_int_range_lower_f(_val,_1,_pass,
918  boost::phoenix::ref(error_msgs_)) ])
919  >> -( lit(',')
920  >> lit("upper")
921  >> lit('=')
922  >> expression07_g(_r1)
923  [set_int_range_upper_f(_val,_1,_pass,
924  boost::phoenix::ref(error_msgs_)) ] ) )
925  |
926  ( lit("upper")
927  >> lit('=')
928  >> expression07_g(_r1)
929  [set_int_range_upper_f(_val,_1,_pass,
930  boost::phoenix::ref(error_msgs_)) ])
931  )
932  >> lit('>');
933 
934  range_brackets_double_r.name("real range expression pair, brackets");
936  = lit('<') [_val = empty_range_f(boost::phoenix::ref(error_msgs_))]
937  > (
938  ( (lit("lower")
939  > lit('=')
940  > expression07_g(_r1)
941  [set_double_range_lower_f(_val,_1,_pass,
942  boost::phoenix::ref(error_msgs_)) ])
943  > -( lit(',')
944  > lit("upper")
945  > lit('=')
946  > expression07_g(_r1)
947  [set_double_range_upper_f(_val,_1,_pass,
948  boost::phoenix::ref(error_msgs_)) ] ) )
949  |
950  ( lit("upper")
951  > lit('=')
952  > expression07_g(_r1)
953  [set_double_range_upper_f(_val,_1,_pass,
954  boost::phoenix::ref(error_msgs_)) ])
955  )
956  > lit('>');
957 
958  identifier_r.name("identifier");
961  [_pass = validate_identifier_f(_val,boost::phoenix::ref(error_msgs_))]
962  ;
963 
964  identifier_name_r.name("identifier subrule");
966  %= lexeme[char_("a-zA-Z")
967  >> *char_("a-zA-Z0-9_.")]
968  ;
969 
970  }
971  }
972 
973 
974 }
975 #endif
range operator()(std::stringstream &) const
bool operator()(const bool &allow_constraints, const bool &declaration_ok, const var_decl &var_decl, std::stringstream &error_msgs) const
void operator()(cholesky_factor_var_decl &var_decl) const
boost::spirit::qi::rule< Iterator, std::string(), whitespace_grammar< Iterator > > identifier_name_r
boost::spirit::qi::rule< Iterator, matrix_var_decl(var_origin), whitespace_grammar< Iterator > > matrix_decl_r
int N_
const int data_origin
Definition: ast.hpp:413
boost::phoenix::function< empty_range > empty_range_f
boost::phoenix::function< set_int_range_lower > set_int_range_lower_f
BOOST_FUSION_ADAPT_STRUCT(stan::gm::int_var_decl,(stan::gm::range, range_)(std::string, name_)(std::vector< stan::gm::expression >, dims_)) BOOST_FUSION_ADAPT_STRUCT(stan
const int parameter_origin
Definition: ast.hpp:415
void operator()(range &range, const expression &expr, bool &pass, std::stringstream &error_msgs) const
boost::spirit::qi::rule< Iterator, row_vector_var_decl(var_origin), whitespace_grammar< Iterator > > row_vector_decl_r
const int local_origin
Definition: ast.hpp:418
void operator()(range &range, const expression &expr, bool &pass, std::stringstream &error_msgs) const
bool operator()(const index_op &x) const
bool identifier_exists(const std::string &identifier) const
bool operator()(const std::string &identifier, std::stringstream &error_msgs) const
boost::spirit::qi::rule< Iterator, cholesky_factor_var_decl(var_origin), whitespace_grammar< Iterator > > cholesky_factor_decl_r
boost::spirit::qi::rule< Iterator, std::vector< expression >var_origin), whitespace_grammar< Iterator > > dims_r
void print_var_origin(std::ostream &o, const var_origin &vo)
Definition: ast_def.cpp:944
expression07_grammar< Iterator > expression07_g
bool operator()(const variable &x) const
bool operator()(const nil &) const
std::stringstream & error_msgs_
bool operator()(const int_literal &) const
boost::phoenix::function< copy_square_cholesky_dimension_if_necessary > copy_square_cholesky_dimension_if_necessary_f
bool operator()(const array_literal &x) const
static bool contains(const S &s, const T &x)
boost::phoenix::function< validate_double_expr > validate_double_expr_f
boost::spirit::qi::rule< Iterator, unit_vector_var_decl(var_origin), whitespace_grammar< Iterator > > unit_vector_decl_r
bool operator()(const expression &expr, std::stringstream &error_msgs) const
std::vector< std::vector< typename stan::return_type< T1, T2 >::type > > integrate_ode(const F &f, const std::vector< T1 > y0, const double t0, const std::vector< double > &ts, const std::vector< T2 > &theta, const std::vector< double > &x, const std::vector< int > &x_int, std::ostream *msgs)
Return the solutions for the specified system of ordinary differential equations given the specified ...
boost::spirit::qi::rule< Iterator, vector_var_decl(var_origin), whitespace_grammar< Iterator > > vector_decl_r
bool operator()(const unary_op &x) const
boost::spirit::qi::rule< Iterator, corr_matrix_var_decl(var_origin), whitespace_grammar< Iterator > > corr_matrix_decl_r
boost::phoenix::function< validate_int_expr > validate_int_expr_f
boost::phoenix::function< validate_decl_constraints > validate_decl_constraints_f
boost::spirit::qi::rule< Iterator, std::string(), whitespace_grammar< Iterator > > identifier_r
boost::spirit::qi::rule< Iterator, boost::spirit::qi::locals< bool >, std::vector< var_decl >bool, var_origin), whitespace_grammar< Iterator > > var_decls_r
boost::spirit::qi::rule< Iterator, std::vector< expression >var_origin), whitespace_grammar< Iterator > > opt_dims_r
boost::spirit::qi::rule< Iterator, cholesky_corr_var_decl(var_origin), whitespace_grammar< Iterator > > cholesky_corr_decl_r
boost::spirit::qi::rule< Iterator, ordered_var_decl(var_origin), whitespace_grammar< Iterator > > ordered_decl_r
bool operator()(const integrate_ode &x) const
boost::spirit::qi::rule< Iterator, double_var_decl(var_origin), whitespace_grammar< Iterator > > double_decl_r
std::set< std::string > const_fun_name_set_
boost::spirit::qi::rule< Iterator, boost::spirit::qi::locals< bool >, var_decl(bool, var_origin), whitespace_grammar< Iterator > > var_decl_r
bool operator()(const fun &x) const
void operator()(const expression &expr, bool &pass, std::stringstream &error_msgs) const
boost::phoenix::function< validate_identifier > validate_identifier_f
void operator()(range &range, const expression &expr, bool &pass, std::stringstream &error_msgs) const
boost::spirit::qi::rule< Iterator, range(var_origin), whitespace_grammar< Iterator > > range_brackets_double_r
data_only_expression(std::stringstream &error_msgs, variable_map &var_map)
boost::phoenix::function< set_int_range_upper > set_int_range_upper_f
boost::phoenix::function< set_double_range_lower > set_double_range_lower_f
bool operator()(const binary_op &x) const
void operator()(range &range, const expression &expr, bool &pass, std::stringstream &error_msgs) const
boost::spirit::qi::rule< Iterator, range(var_origin), whitespace_grammar< Iterator > > range_brackets_int_r
const int transformed_data_origin
Definition: ast.hpp:414
bool is_nil(const expression &e)
Definition: ast_def.cpp:768
boost::spirit::qi::rule< Iterator, positive_ordered_var_decl(var_origin), whitespace_grammar< Iterator > > positive_ordered_decl_r
boost::phoenix::function< set_double_range_upper > set_double_range_upper_f
std::set< std::string > reserved_word_set_
std::stringstream & error_msgs_
boost::spirit::qi::rule< Iterator, cov_matrix_var_decl(var_origin), whitespace_grammar< Iterator > > cov_matrix_decl_r
boost::spirit::qi::rule< Iterator, int_var_decl(var_origin), whitespace_grammar< Iterator > > int_decl_r
const int INT_T
Definition: ast.hpp:65
expression_grammar< Iterator > expression_g
const int transformed_parameter_origin
Definition: ast.hpp:416
bool operator()(const double_literal &) const
void operator()(const expression &expr, int var_origin, bool &pass, variable_map &var_map, std::stringstream &error_msgs) const
boost::phoenix::function< validate_int_data_expr > validate_int_data_expr_f
void reserve(const std::string &w)
boost::phoenix::function< add_var > add_var_f
boost::spirit::qi::rule< Iterator, simplex_var_decl(var_origin), whitespace_grammar< Iterator > > simplex_decl_r
int var_origin
Definition: ast.hpp:411
void operator()(R &var_decl_result, const T &var_decl, variable_map &vm, bool &pass, const var_origin &vo, std::ostream &error_msgs) const
int M_

     [ Stan Home Page ] © 2011–2014, Stan Development Team.