Stan  2.13.1
probability, sampling & optimization
var_decls_grammar_def.hpp
Go to the documentation of this file.
1 #ifndef STAN_LANG_GRAMMARS_VAR_DECLS_GRAMMAR_DEF_HPP
2 #define STAN_LANG_GRAMMARS_VAR_DECLS_GRAMMAR_DEF_HPP
3 
4 #include <stan/lang/ast.hpp>
8 #include <boost/spirit/include/qi.hpp>
9 #include <boost/spirit/include/phoenix_core.hpp>
10 #include <set>
11 #include <string>
12 #include <vector>
13 
15  (stan::lang::range, range_)
16  (std::string, name_)
17  (std::vector<stan::lang::expression>, dims_)
18  (stan::lang::expression, def_) )
19 
20 BOOST_FUSION_ADAPT_STRUCT(stan::lang::double_var_decl,
21  (stan::lang::range, range_)
22  (std::string, name_)
23  (std::vector<stan::lang::expression>, dims_)
24  (stan::lang::expression, def_) )
25 
26 BOOST_FUSION_ADAPT_STRUCT(stan::lang::vector_var_decl,
27  (stan::lang::range, range_)
28  (stan::lang::expression, M_)
29  (std::string, name_)
30  (std::vector<stan::lang::expression>, dims_)
31  (stan::lang::expression, def_) )
32 
33 BOOST_FUSION_ADAPT_STRUCT(stan::lang::row_vector_var_decl,
34  (stan::lang::range, range_)
35  (stan::lang::expression, N_)
36  (std::string, name_)
37  (std::vector<stan::lang::expression>, dims_)
38  (stan::lang::expression, def_) )
39 
40 BOOST_FUSION_ADAPT_STRUCT(stan::lang::matrix_var_decl,
41  (stan::lang::range, range_)
42  (stan::lang::expression, M_)
43  (stan::lang::expression, N_)
44  (std::string, name_)
45  (std::vector<stan::lang::expression>, dims_)
46  (stan::lang::expression, def_) )
47 
48 BOOST_FUSION_ADAPT_STRUCT(stan::lang::unit_vector_var_decl,
49  (stan::lang::expression, K_)
50  (std::string, name_)
51  (std::vector<stan::lang::expression>, dims_) )
52 
53 BOOST_FUSION_ADAPT_STRUCT(stan::lang::simplex_var_decl,
54  (stan::lang::expression, K_)
55  (std::string, name_)
56  (std::vector<stan::lang::expression>, dims_) )
57 
58 BOOST_FUSION_ADAPT_STRUCT(stan::lang::ordered_var_decl,
59  (stan::lang::expression, K_)
60  (std::string, name_)
61  (std::vector<stan::lang::expression>, dims_) )
62 
63 BOOST_FUSION_ADAPT_STRUCT(stan::lang::positive_ordered_var_decl,
64  (stan::lang::expression, K_)
65  (std::string, name_)
66  (std::vector<stan::lang::expression>, dims_) )
67 
68 BOOST_FUSION_ADAPT_STRUCT(stan::lang::cholesky_factor_var_decl,
69  (stan::lang::expression, M_)
70  (stan::lang::expression, N_)
71  (std::string, name_)
72  (std::vector<stan::lang::expression>, dims_) )
73 
74 BOOST_FUSION_ADAPT_STRUCT(stan::lang::cholesky_corr_var_decl,
75  (stan::lang::expression, K_)
76  (std::string, name_)
77  (std::vector<stan::lang::expression>, dims_) )
78 
79 BOOST_FUSION_ADAPT_STRUCT(stan::lang::cov_matrix_var_decl,
80  (stan::lang::expression, K_)
81  (std::string, name_)
82  (std::vector<stan::lang::expression>, dims_) )
83 
84 BOOST_FUSION_ADAPT_STRUCT(stan::lang::corr_matrix_var_decl,
85  (stan::lang::expression, K_)
86  (std::string, name_)
87  (std::vector<stan::lang::expression>, dims_) )
88 
89 namespace stan {
90 
91  namespace lang {
92 
93  template <typename Iterator>
94  var_decls_grammar<Iterator>::var_decls_grammar(variable_map& var_map,
95  std::stringstream& error_msgs)
96  : var_decls_grammar::base_type(var_decls_r),
97  var_map_(var_map),
98  error_msgs_(error_msgs),
99  expression_g(var_map, error_msgs),
100  expression07_g(var_map, error_msgs, expression_g) {
101  using boost::spirit::qi::_1;
102  using boost::spirit::qi::_3;
103  using boost::spirit::qi::char_;
104  using boost::spirit::qi::eps;
105  using boost::spirit::qi::lexeme;
106  using boost::spirit::qi::lit;
107  using boost::spirit::qi::no_skip;
108  using boost::spirit::qi::_pass;
109  using boost::spirit::qi::_val;
110  using boost::spirit::qi::labels::_a;
111  using boost::spirit::qi::labels::_r1;
112  using boost::spirit::qi::labels::_r2;
113 
114  var_decls_r.name("variable declarations");
116  %= *(var_decl_r(_r1, _r2));
117 
118  // _a = error state local,
119  // _r1 constraints allowed inherited,
120  // _r2 var_origin
121  var_decl_r.name("variable declaration");
122  var_decl_r
123  = (int_decl_r(_r2)
124  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
125  boost::phoenix::ref(error_msgs))]
126  | double_decl_r(_r2)
127  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
128  boost::phoenix::ref(error_msgs_))]
129  | vector_decl_r(_r2)
130  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
131  boost::phoenix::ref(error_msgs_))]
132  | row_vector_decl_r(_r2)
133  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
134  boost::phoenix::ref(error_msgs_))]
135  | matrix_decl_r(_r2)
136  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
137  boost::phoenix::ref(error_msgs_))]
138  | unit_vector_decl_r(_r2)
139  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
140  boost::phoenix::ref(error_msgs_))]
141  | simplex_decl_r(_r2)
142  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
143  boost::phoenix::ref(error_msgs_))]
144  | ordered_decl_r(_r2)
145  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
146  boost::phoenix::ref(error_msgs_))]
148  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
149  boost::phoenix::ref(error_msgs_))]
151  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
152  boost::phoenix::ref(error_msgs_))]
153  | cholesky_corr_decl_r(_r2)
154  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
155  boost::phoenix::ref(error_msgs_))]
156  | cov_matrix_decl_r(_r2)
157  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
158  boost::phoenix::ref(error_msgs_))]
159  | corr_matrix_decl_r(_r2)
160  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
161  boost::phoenix::ref(error_msgs_))]
162  )
163  > eps
164  [validate_decl_constraints_f(_r1, _a, _val, _pass,
165  boost::phoenix::ref(error_msgs_)),
166  validate_definition_f(_r2, _val, _pass,
167  boost::phoenix::ref(error_msgs_))]
168  > lit(';');
169 
170  int_decl_r.name("integer declaration");
171  int_decl_r
172  %= (lit("int")
173  >> no_skip[!char_("a-zA-Z0-9_")])
174  > -range_brackets_int_r(_r1)
175  > identifier_r
176  > opt_dims_r(_r1)
177  > opt_def_r(_r1);
178 
179 
180  double_decl_r.name("real declaration");
182  %= (lit("real")
183  >> no_skip[!char_("a-zA-Z0-9_")])
185  > identifier_r
186  > opt_dims_r(_r1)
187  > opt_def_r(_r1);
188 
189  vector_decl_r.name("vector declaration");
191  %= (lit("vector")
192  >> no_skip[!char_("a-zA-Z0-9_")])
194  > dim1_r(_r1)
195  > identifier_r
196  > opt_dims_r(_r1)
197  > opt_def_r(_r1);
198 
199  row_vector_decl_r.name("row vector declaration");
201  %= (lit("row_vector")
202  >> no_skip[!char_("a-zA-Z0-9_")])
204  > dim1_r(_r1)
205  > identifier_r
206  > opt_dims_r(_r1)
207  > opt_def_r(_r1);
208 
209  matrix_decl_r.name("matrix declaration");
211  %= (lit("matrix")
212  >> no_skip[!char_("a-zA-Z0-9_")])
214  > lit('[')
215  > int_data_expr_r(_r1) > lit(',') > int_data_expr_r(_r1)
216  > lit(']')
217  > identifier_r
218  > opt_dims_r(_r1)
219  > opt_def_r(_r1);
220 
221  unit_vector_decl_r.name("unit_vector declaration");
223  %= (lit("unit_vector")
224  >> no_skip[!char_("a-zA-Z0-9_")])
225  > dim1_r(_r1)
226  > identifier_r
227  > opt_dims_r(_r1);
228 
229  simplex_decl_r.name("simplex declaration");
231  %= (lit("simplex")
232  >> no_skip[!char_("a-zA-Z0-9_")])
233  > dim1_r(_r1)
234  > identifier_r
235  > opt_dims_r(_r1);
236 
237  ordered_decl_r.name("ordered declaration");
239  %= (lit("ordered")
240  >> no_skip[!char_("a-zA-Z0-9_")])
241  > dim1_r(_r1)
242  > identifier_r
243  > opt_dims_r(_r1);
244 
245  positive_ordered_decl_r.name("positive_ordered declaration");
247  %= (lit("positive_ordered")
248  >> no_skip[!char_("a-zA-Z0-9_")])
249  > dim1_r(_r1)
250  > identifier_r
251  > opt_dims_r(_r1);
252 
253  cholesky_factor_decl_r.name("cholesky factor for symmetric,"
254  " positive-def declaration");
256  %= (lit("cholesky_factor_cov")
257  >> no_skip[!char_("a-zA-Z0-9_")])
258  > lit('[')
259  > int_data_expr_r(_r1)
260  > -(lit(',') > int_data_expr_r(_r1))
261  > lit(']')
262  > identifier_r
263  > opt_dims_r(_r1)
264  > eps
266 
267  cholesky_corr_decl_r.name("cholesky factor for"
268  " correlation matrix declaration");
270  %= (lit("cholesky_factor_corr")
271  >> no_skip[!char_("a-zA-Z0-9_")])
272  > dim1_r(_r1)
273  > identifier_r
274  > opt_dims_r(_r1);
275 
276  cov_matrix_decl_r.name("covariance matrix declaration");
278  %= (lit("cov_matrix")
279  >> no_skip[!char_("a-zA-Z0-9_")])
280  > dim1_r(_r1)
281  > identifier_r
282  > opt_dims_r(_r1);
283 
284  corr_matrix_decl_r.name("correlation matrix declaration");
286  %= (lit("corr_matrix")
287  >> no_skip[!char_("a-zA-Z0-9_")])
288  > dim1_r(_r1)
289  > identifier_r
290  > opt_dims_r(_r1);
291 
292  int_data_expr_r.name("integer data expression");
294  %= expression_g(_r1)
295  [validate_int_data_expr_f(_1, _r1, _pass,
296  boost::phoenix::ref(var_map_),
297  boost::phoenix::ref(error_msgs_))];
298 
299  dim1_r.name("size declaration: integer (data-only) in square brackets");
300  dim1_r %= lit('[') > int_data_expr_r(_r1) > lit(']');
301 
302  dims_r.name("array dimensions");
303  dims_r %= lit('[') > (int_data_expr_r(_r1) % ',') > lit(']');
304 
305  opt_dims_r.name("array dimensions (optional)");
306  opt_dims_r %= -dims_r(_r1);
307 
308  opt_def_r.name("variable definition (optional)");
309  opt_def_r %= -def_r(_r1);
310 
311  def_r.name("variable definition");
312  def_r %= lit('=') > expression_g(_r1);
313 
314  range_brackets_int_r.name("integer range expression pair, brackets");
316  = lit('<') [empty_range_f(_val, boost::phoenix::ref(error_msgs_))]
317  >> (
318  ((lit("lower")
319  >> lit('=')
320  >> expression07_g(_r1)
321  [set_int_range_lower_f(_val, _1, _pass,
322  boost::phoenix::ref(error_msgs_))])
323  >> -(lit(',')
324  >> lit("upper")
325  >> lit('=')
326  >> expression07_g(_r1)
327  [set_int_range_upper_f(_val, _1, _pass,
328  boost::phoenix::ref(error_msgs_))]))
329  |
330  (lit("upper")
331  >> lit('=')
332  >> expression07_g(_r1)
333  [set_int_range_upper_f(_val, _1, _pass,
334  boost::phoenix::ref(error_msgs_))])
335  )
336  >> lit('>');
337 
338  range_brackets_double_r.name("real range expression pair, brackets");
340  = lit('<')[empty_range_f(_val, boost::phoenix::ref(error_msgs_))]
341  > (
342  ((lit("lower")
343  > lit('=')
344  > expression07_g(_r1)
345  [set_double_range_lower_f(_val, _1, _pass,
346  boost::phoenix::ref(error_msgs_))])
347  > -(lit(',')
348  > lit("upper")
349  > lit('=')
350  > expression07_g(_r1)
351  [set_double_range_upper_f(_val, _1, _pass,
352  boost::phoenix::ref(error_msgs_))]))
353  |
354  (lit("upper")
355  > lit('=')
356  > expression07_g(_r1)
357  [set_double_range_upper_f(_val, _1, _pass,
358  boost::phoenix::ref(error_msgs_))])
359  )
360  > lit('>');
361 
362  identifier_r.name("identifier");
365  [validate_identifier_f(_val, _pass,
366  boost::phoenix::ref(error_msgs_))];
367 
368  identifier_name_r.name("identifier subrule");
370  %= lexeme[char_("a-zA-Z")
371  >> *char_("a-zA-Z0-9_.")];
372  }
373  }
374 
375 
376 }
377 #endif
378 
boost::spirit::qi::rule< Iterator, row_vector_var_decl(var_origin), whitespace_grammar< Iterator > > row_vector_decl_r
boost::phoenix::function< set_int_range_upper > set_int_range_upper_f
boost::phoenix::function< set_int_range_lower > set_int_range_lower_f
boost::spirit::qi::rule< Iterator, std::string(), whitespace_grammar< Iterator > > identifier_name_r
boost::spirit::qi::rule< Iterator, ordered_var_decl(var_origin), whitespace_grammar< Iterator > > ordered_decl_r
Probability, optimization and sampling library.
boost::spirit::qi::rule< Iterator, positive_ordered_var_decl(var_origin), whitespace_grammar< Iterator > > positive_ordered_decl_r
expression_grammar< Iterator > expression_g
boost::spirit::qi::rule< Iterator, matrix_var_decl(var_origin), whitespace_grammar< Iterator > > matrix_decl_r
boost::spirit::qi::rule< Iterator, unit_vector_var_decl(var_origin), whitespace_grammar< Iterator > > unit_vector_decl_r
boost::spirit::qi::rule< Iterator, cholesky_corr_var_decl(var_origin), whitespace_grammar< Iterator > > cholesky_corr_decl_r
Template specification of functions in std for Stan.
boost::phoenix::function< empty_range > empty_range_f
boost::spirit::qi::rule< Iterator, std::string(), whitespace_grammar< Iterator > > identifier_r
boost::phoenix::function< add_var > add_var_f
boost::spirit::qi::rule< Iterator, cholesky_factor_var_decl(var_origin), whitespace_grammar< Iterator > > cholesky_factor_decl_r
boost::phoenix::function< set_double_range_lower > set_double_range_lower_f
boost::spirit::qi::rule< Iterator, expression(var_origin), whitespace_grammar< Iterator > > int_data_expr_r
boost::spirit::qi::rule< Iterator, expression(var_origin), whitespace_grammar< Iterator > > def_r
boost::spirit::qi::rule< Iterator, expression(var_origin), whitespace_grammar< Iterator > > dim1_r
boost::phoenix::function< validate_definition > validate_definition_f
boost::spirit::qi::rule< Iterator, range(var_origin), whitespace_grammar< Iterator > > range_brackets_int_r
boost::spirit::qi::rule< Iterator, simplex_var_decl(var_origin), whitespace_grammar< Iterator > > simplex_decl_r
boost::phoenix::function< validate_identifier > validate_identifier_f
BOOST_FUSION_ADAPT_STRUCT(stan::lang::expr_type,(stan::lang::base_expr_type, base_type_)(size_t, num_dims_)) namespace stan
boost::phoenix::function< validate_decl_constraints > validate_decl_constraints_f
boost::spirit::qi::rule< Iterator, int_var_decl(var_origin), whitespace_grammar< Iterator > > int_decl_r
boost::phoenix::function< validate_int_data_expr > validate_int_data_expr_f
boost::spirit::qi::rule< Iterator, double_var_decl(var_origin), whitespace_grammar< Iterator > > double_decl_r
expression07_grammar< Iterator > expression07_g
boost::spirit::qi::rule< Iterator, std::vector< expression >var_origin), whitespace_grammar< Iterator > > dims_r
boost::spirit::qi::rule< Iterator, expression(var_origin), whitespace_grammar< Iterator > > opt_def_r
boost::spirit::qi::rule< Iterator, boost::spirit::qi::locals< bool >, var_decl(bool, var_origin), whitespace_grammar< Iterator > > var_decl_r
boost::spirit::qi::rule< Iterator, range(var_origin), whitespace_grammar< Iterator > > range_brackets_double_r
boost::spirit::qi::rule< Iterator, vector_var_decl(var_origin), whitespace_grammar< Iterator > > vector_decl_r
boost::spirit::qi::rule< Iterator, cov_matrix_var_decl(var_origin), whitespace_grammar< Iterator > > cov_matrix_decl_r
boost::phoenix::function< set_double_range_upper > set_double_range_upper_f
boost::phoenix::function< copy_square_cholesky_dimension_if_necessary > copy_square_cholesky_dimension_if_necessary_f
boost::spirit::qi::rule< Iterator, corr_matrix_var_decl(var_origin), whitespace_grammar< Iterator > > corr_matrix_decl_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

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