Stan  2.14.0
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  (stan::lang::expression, def_) )
53 
54 BOOST_FUSION_ADAPT_STRUCT(stan::lang::simplex_var_decl,
55  (stan::lang::expression, K_)
56  (std::string, name_)
57  (std::vector<stan::lang::expression>, dims_)
58  (stan::lang::expression, def_) )
59 
60 BOOST_FUSION_ADAPT_STRUCT(stan::lang::ordered_var_decl,
61  (stan::lang::expression, K_)
62  (std::string, name_)
63  (std::vector<stan::lang::expression>, dims_)
64  (stan::lang::expression, def_) )
65 
66 BOOST_FUSION_ADAPT_STRUCT(stan::lang::positive_ordered_var_decl,
67  (stan::lang::expression, K_)
68  (std::string, name_)
69  (std::vector<stan::lang::expression>, dims_)
70  (stan::lang::expression, def_) )
71 
72 BOOST_FUSION_ADAPT_STRUCT(stan::lang::cholesky_factor_var_decl,
73  (stan::lang::expression, M_)
74  (stan::lang::expression, N_)
75  (std::string, name_)
76  (std::vector<stan::lang::expression>, dims_)
77  (stan::lang::expression, def_) )
78 
79 BOOST_FUSION_ADAPT_STRUCT(stan::lang::cholesky_corr_var_decl,
80  (stan::lang::expression, K_)
81  (std::string, name_)
82  (std::vector<stan::lang::expression>, dims_)
83  (stan::lang::expression, def_) )
84 
85 BOOST_FUSION_ADAPT_STRUCT(stan::lang::cov_matrix_var_decl,
86  (stan::lang::expression, K_)
87  (std::string, name_)
88  (std::vector<stan::lang::expression>, dims_)
89  (stan::lang::expression, def_) )
90 
91 BOOST_FUSION_ADAPT_STRUCT(stan::lang::corr_matrix_var_decl,
92  (stan::lang::expression, K_)
93  (std::string, name_)
94  (std::vector<stan::lang::expression>, dims_)
95  (stan::lang::expression, def_) )
96 
97 namespace stan {
98 
99  namespace lang {
100 
101  template <typename Iterator>
102  var_decls_grammar<Iterator>::var_decls_grammar(variable_map& var_map,
103  std::stringstream& error_msgs)
104  : var_decls_grammar::base_type(var_decls_r),
105  var_map_(var_map),
106  error_msgs_(error_msgs),
107  expression_g(var_map, error_msgs),
108  expression07_g(var_map, error_msgs, expression_g) {
109  using boost::spirit::qi::_1;
110  using boost::spirit::qi::_3;
111  using boost::spirit::qi::char_;
112  using boost::spirit::qi::eps;
113  using boost::spirit::qi::lexeme;
114  using boost::spirit::qi::lit;
115  using boost::spirit::qi::no_skip;
116  using boost::spirit::qi::_pass;
117  using boost::spirit::qi::_val;
118  using boost::spirit::qi::labels::_a;
119  using boost::spirit::qi::labels::_r1;
120  using boost::spirit::qi::labels::_r2;
121 
122  var_decls_r.name("variable declarations");
124  %= *(var_decl_r(_r1, _r2));
125 
126  // _a = error state local,
127  // _r1 constraints allowed inherited,
128  // _r2 var_origin
129  var_decl_r.name("variable declaration");
130  var_decl_r
131  = (int_decl_r(_r2)
132  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
133  boost::phoenix::ref(error_msgs))]
134  | double_decl_r(_r2)
135  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
136  boost::phoenix::ref(error_msgs_))]
137  | vector_decl_r(_r2)
138  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
139  boost::phoenix::ref(error_msgs_))]
140  | row_vector_decl_r(_r2)
141  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
142  boost::phoenix::ref(error_msgs_))]
143  | matrix_decl_r(_r2)
144  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
145  boost::phoenix::ref(error_msgs_))]
146  | unit_vector_decl_r(_r2)
147  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
148  boost::phoenix::ref(error_msgs_))]
149  | simplex_decl_r(_r2)
150  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
151  boost::phoenix::ref(error_msgs_))]
152  | ordered_decl_r(_r2)
153  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
154  boost::phoenix::ref(error_msgs_))]
156  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
157  boost::phoenix::ref(error_msgs_))]
159  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
160  boost::phoenix::ref(error_msgs_))]
161  | cholesky_corr_decl_r(_r2)
162  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
163  boost::phoenix::ref(error_msgs_))]
164  | cov_matrix_decl_r(_r2)
165  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
166  boost::phoenix::ref(error_msgs_))]
167  | corr_matrix_decl_r(_r2)
168  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
169  boost::phoenix::ref(error_msgs_))]
170  )
171  > eps
172  [validate_decl_constraints_f(_r1, _a, _val, _pass,
173  boost::phoenix::ref(error_msgs_)),
174  validate_definition_f(_r2, _val, _pass,
175  boost::phoenix::ref(error_msgs_))]
176  > lit(';');
177 
178  int_decl_r.name("integer declaration");
179  int_decl_r
180  %= (lit("int")
181  >> no_skip[!char_("a-zA-Z0-9_")])
182  > -range_brackets_int_r(_r1)
183  > identifier_r
184  > opt_dims_r(_r1)
185  > opt_def_r(_r1);
186 
187 
188  double_decl_r.name("real declaration");
190  %= (lit("real")
191  >> no_skip[!char_("a-zA-Z0-9_")])
193  > identifier_r
194  > opt_dims_r(_r1)
195  > opt_def_r(_r1);
196 
197  vector_decl_r.name("vector declaration");
199  %= (lit("vector")
200  >> no_skip[!char_("a-zA-Z0-9_")])
202  > dim1_r(_r1)
203  > identifier_r
204  > opt_dims_r(_r1)
205  > opt_def_r(_r1);
206 
207  row_vector_decl_r.name("row vector declaration");
209  %= (lit("row_vector")
210  >> no_skip[!char_("a-zA-Z0-9_")])
212  > dim1_r(_r1)
213  > identifier_r
214  > opt_dims_r(_r1)
215  > opt_def_r(_r1);
216 
217  matrix_decl_r.name("matrix declaration");
219  %= (lit("matrix")
220  >> no_skip[!char_("a-zA-Z0-9_")])
222  > lit('[')
223  > int_data_expr_r(_r1) > lit(',') > int_data_expr_r(_r1)
224  > lit(']')
225  > identifier_r
226  > opt_dims_r(_r1)
227  > opt_def_r(_r1);
228 
229  unit_vector_decl_r.name("unit_vector declaration");
231  %= (lit("unit_vector")
232  >> no_skip[!char_("a-zA-Z0-9_")])
233  > dim1_r(_r1)
234  > identifier_r
235  > opt_dims_r(_r1)
236  > opt_def_r(_r1);
237 
238  simplex_decl_r.name("simplex declaration");
240  %= (lit("simplex")
241  >> no_skip[!char_("a-zA-Z0-9_")])
242  > dim1_r(_r1)
243  > identifier_r
244  > opt_dims_r(_r1)
245  > opt_def_r(_r1);
246 
247  ordered_decl_r.name("ordered declaration");
249  %= (lit("ordered")
250  >> no_skip[!char_("a-zA-Z0-9_")])
251  > dim1_r(_r1)
252  > identifier_r
253  > opt_dims_r(_r1)
254  > opt_def_r(_r1);
255 
256  positive_ordered_decl_r.name("positive_ordered declaration");
258  %= (lit("positive_ordered")
259  >> no_skip[!char_("a-zA-Z0-9_")])
260  > dim1_r(_r1)
261  > identifier_r
262  > opt_dims_r(_r1)
263  > opt_def_r(_r1);
264 
265  cholesky_factor_decl_r.name("cholesky factor for symmetric,"
266  " positive-def declaration");
268  %= (lit("cholesky_factor_cov")
269  >> no_skip[!char_("a-zA-Z0-9_")])
270  > lit('[')
271  > int_data_expr_r(_r1)
272  > -(lit(',') > int_data_expr_r(_r1))
273  > lit(']')
274  > identifier_r
275  > opt_dims_r(_r1)
276  > opt_def_r(_r1)
277  > eps
279 
280  cholesky_corr_decl_r.name("cholesky factor for"
281  " correlation matrix declaration");
283  %= (lit("cholesky_factor_corr")
284  >> no_skip[!char_("a-zA-Z0-9_")])
285  > dim1_r(_r1)
286  > identifier_r
287  > opt_dims_r(_r1)
288  > opt_def_r(_r1);
289 
290  cov_matrix_decl_r.name("covariance matrix declaration");
292  %= (lit("cov_matrix")
293  >> no_skip[!char_("a-zA-Z0-9_")])
294  > dim1_r(_r1)
295  > identifier_r
296  > opt_dims_r(_r1)
297  > opt_def_r(_r1);
298 
299  corr_matrix_decl_r.name("correlation matrix declaration");
301  %= (lit("corr_matrix")
302  >> no_skip[!char_("a-zA-Z0-9_")])
303  > dim1_r(_r1)
304  > identifier_r
305  > opt_dims_r(_r1)
306  > opt_def_r(_r1);
307 
308  int_data_expr_r.name("integer data expression");
310  %= expression_g(_r1)
311  [validate_int_data_expr_f(_1, _r1, _pass,
312  boost::phoenix::ref(var_map_),
313  boost::phoenix::ref(error_msgs_))];
314 
315  dim1_r.name("size declaration: integer (data-only) in square brackets");
316  dim1_r %= lit('[') > int_data_expr_r(_r1) > lit(']');
317 
318  dims_r.name("array dimensions");
319  dims_r %= lit('[') > (int_data_expr_r(_r1) % ',') > lit(']');
320 
321  opt_dims_r.name("array dimensions (optional)");
322  opt_dims_r %= -dims_r(_r1);
323 
324  opt_def_r.name("variable definition (optional)");
325  opt_def_r %= -def_r(_r1);
326 
327  def_r.name("variable definition");
328  def_r %= lit('=') > expression_g(_r1);
329 
330  range_brackets_int_r.name("integer range expression pair, brackets");
332  = lit('<') [empty_range_f(_val, boost::phoenix::ref(error_msgs_))]
333  >> (
334  ((lit("lower")
335  >> lit('=')
336  >> expression07_g(_r1)
337  [set_int_range_lower_f(_val, _1, _pass,
338  boost::phoenix::ref(error_msgs_))])
339  >> -(lit(',')
340  >> lit("upper")
341  >> lit('=')
342  >> expression07_g(_r1)
343  [set_int_range_upper_f(_val, _1, _pass,
344  boost::phoenix::ref(error_msgs_))]))
345  |
346  (lit("upper")
347  >> lit('=')
348  >> expression07_g(_r1)
349  [set_int_range_upper_f(_val, _1, _pass,
350  boost::phoenix::ref(error_msgs_))])
351  )
352  >> lit('>');
353 
354  range_brackets_double_r.name("real range expression pair, brackets");
356  = lit('<')[empty_range_f(_val, boost::phoenix::ref(error_msgs_))]
357  > (
358  ((lit("lower")
359  > lit('=')
360  > expression07_g(_r1)
361  [set_double_range_lower_f(_val, _1, _pass,
362  boost::phoenix::ref(error_msgs_))])
363  > -(lit(',')
364  > lit("upper")
365  > lit('=')
366  > expression07_g(_r1)
367  [set_double_range_upper_f(_val, _1, _pass,
368  boost::phoenix::ref(error_msgs_))]))
369  |
370  (lit("upper")
371  > lit('=')
372  > expression07_g(_r1)
373  [set_double_range_upper_f(_val, _1, _pass,
374  boost::phoenix::ref(error_msgs_))])
375  )
376  > lit('>');
377 
378  identifier_r.name("identifier");
381  [validate_identifier_f(_val, _pass,
382  boost::phoenix::ref(error_msgs_))];
383 
384  identifier_name_r.name("identifier subrule");
386  %= lexeme[char_("a-zA-Z")
387  >> *char_("a-zA-Z0-9_.")];
388  }
389  }
390 
391 
392 }
393 #endif
394 
An integer variable declaration and optional definition.
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
AST structure for a range object with a low and high value.
Definition: range.hpp:14
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.