Stan  2.10.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 
19 BOOST_FUSION_ADAPT_STRUCT(stan::lang::double_var_decl,
20  (stan::lang::range, range_)
21  (std::string, name_)
22  (std::vector<stan::lang::expression>, dims_) )
23 
24 BOOST_FUSION_ADAPT_STRUCT(stan::lang::vector_var_decl,
25  (stan::lang::range, range_)
26  (stan::lang::expression, M_)
27  (std::string, name_)
28  (std::vector<stan::lang::expression>, dims_) )
29 
30 BOOST_FUSION_ADAPT_STRUCT(stan::lang::row_vector_var_decl,
31  (stan::lang::range, range_)
32  (stan::lang::expression, N_)
33  (std::string, name_)
34  (std::vector<stan::lang::expression>, dims_) )
35 
36 BOOST_FUSION_ADAPT_STRUCT(stan::lang::matrix_var_decl,
37  (stan::lang::range, range_)
38  (stan::lang::expression, M_)
39  (stan::lang::expression, N_)
40  (std::string, name_)
41  (std::vector<stan::lang::expression>, dims_) )
42 
43 BOOST_FUSION_ADAPT_STRUCT(stan::lang::unit_vector_var_decl,
44  (stan::lang::expression, K_)
45  (std::string, name_)
46  (std::vector<stan::lang::expression>, dims_) )
47 
48 BOOST_FUSION_ADAPT_STRUCT(stan::lang::simplex_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::ordered_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::positive_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::cholesky_factor_var_decl,
64  (stan::lang::expression, M_)
65  (stan::lang::expression, N_)
66  (std::string, name_)
67  (std::vector<stan::lang::expression>, dims_) )
68 
69 BOOST_FUSION_ADAPT_STRUCT(stan::lang::cholesky_corr_var_decl,
70  (stan::lang::expression, K_)
71  (std::string, name_)
72  (std::vector<stan::lang::expression>, dims_) )
73 
74 BOOST_FUSION_ADAPT_STRUCT(stan::lang::cov_matrix_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::corr_matrix_var_decl,
80  (stan::lang::expression, K_)
81  (std::string, name_)
82  (std::vector<stan::lang::expression>, dims_) )
83 
84 namespace stan {
85 
86  namespace lang {
87 
88  template <typename Iterator>
89  var_decls_grammar<Iterator>::var_decls_grammar(variable_map& var_map,
90  std::stringstream& error_msgs)
91  : var_decls_grammar::base_type(var_decls_r),
92  var_map_(var_map),
93  error_msgs_(error_msgs),
94  expression_g(var_map, error_msgs),
95  expression07_g(var_map, error_msgs, expression_g) {
96  using boost::spirit::qi::_1;
97  using boost::spirit::qi::_3;
98  using boost::spirit::qi::char_;
99  using boost::spirit::qi::eps;
100  using boost::spirit::qi::lexeme;
101  using boost::spirit::qi::lit;
102  using boost::spirit::qi::no_skip;
103  using boost::spirit::qi::_pass;
104  using boost::spirit::qi::_val;
105  using boost::spirit::qi::labels::_a;
106  using boost::spirit::qi::labels::_r1;
107  using boost::spirit::qi::labels::_r2;
108 
109  var_decls_r.name("variable declarations");
111  %= *(var_decl_r(_r1, _r2));
112 
113  // _a = error state local,
114  // _r1 constraints allowed inherited,
115  // _r2 var_origin
116  var_decl_r.name("variable declaration");
117  var_decl_r
118  %= (int_decl_r(_r2)
119  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
120  boost::phoenix::ref(error_msgs))]
121  | double_decl_r(_r2)
122  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
123  boost::phoenix::ref(error_msgs_))]
124  | vector_decl_r(_r2)
125  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
126  boost::phoenix::ref(error_msgs_))]
127  | row_vector_decl_r(_r2)
128  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
129  boost::phoenix::ref(error_msgs_))]
130  | matrix_decl_r(_r2)
131  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
132  boost::phoenix::ref(error_msgs_))]
133  | unit_vector_decl_r(_r2)
134  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
135  boost::phoenix::ref(error_msgs_))]
136  | simplex_decl_r(_r2)
137  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
138  boost::phoenix::ref(error_msgs_))]
139  | ordered_decl_r(_r2)
140  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
141  boost::phoenix::ref(error_msgs_))]
143  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
144  boost::phoenix::ref(error_msgs_))]
146  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
147  boost::phoenix::ref(error_msgs_))]
148  | cholesky_corr_decl_r(_r2)
149  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
150  boost::phoenix::ref(error_msgs_))]
151  | cov_matrix_decl_r(_r2)
152  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
153  boost::phoenix::ref(error_msgs_))]
154  | corr_matrix_decl_r(_r2)
155  [add_var_f(_val, _1, boost::phoenix::ref(var_map_), _a, _r2,
156  boost::phoenix::ref(error_msgs_))]
157  )
158  > eps
159  [validate_decl_constraints_f(_r1, _a, _val, _pass,
160  boost::phoenix::ref(error_msgs_))]
161 
162  > lit(';');
163 
164  int_decl_r.name("integer declaration");
165  int_decl_r
166  %= (lit("int")
167  >> no_skip[!char_("a-zA-Z0-9_")])
168  > -range_brackets_int_r(_r1)
169  > identifier_r
170  > opt_dims_r(_r1);
171 
172  double_decl_r.name("real declaration");
174  %= (lit("real")
175  >> no_skip[!char_("a-zA-Z0-9_")])
177  > identifier_r
178  > opt_dims_r(_r1);
179 
180  vector_decl_r.name("vector declaration");
182  %= (lit("vector")
183  >> no_skip[!char_("a-zA-Z0-9_")])
185  > lit('[')
186  > expression_g(_r1)
187  [validate_int_expr_f(_1, _pass, boost::phoenix::ref(error_msgs_))]
188  > lit(']')
189  > identifier_r
190  > opt_dims_r(_r1);
191 
192  row_vector_decl_r.name("row vector declaration");
194  %= (lit("row_vector")
195  >> no_skip[!char_("a-zA-Z0-9_")])
197  > lit('[')
198  > expression_g(_r1)
199  [validate_int_expr_f(_1, _pass, boost::phoenix::ref(error_msgs_))]
200  > lit(']')
201  > identifier_r
202  > opt_dims_r(_r1);
203 
204  matrix_decl_r.name("matrix declaration");
206  %= (lit("matrix")
207  >> no_skip[!char_("a-zA-Z0-9_")])
209  > lit('[')
210  > expression_g(_r1)
211  [validate_int_expr_f(_1, _pass, boost::phoenix::ref(error_msgs_))]
212  > lit(',')
213  > expression_g(_r1)
214  [validate_int_expr_f(_1, _pass, boost::phoenix::ref(error_msgs_))]
215  > lit(']')
216  > identifier_r
217  > opt_dims_r(_r1);
218 
219  unit_vector_decl_r.name("unit_vector declaration");
221  %= (lit("unit_vector")
222  >> no_skip[!char_("a-zA-Z0-9_")])
223  > lit('[')
224  > expression_g(_r1)
225  [validate_int_expr_f(_1, _pass, boost::phoenix::ref(error_msgs_))]
226  > lit(']')
227  > identifier_r
228  > opt_dims_r(_r1);
229 
230  simplex_decl_r.name("simplex declaration");
232  %= (lit("simplex")
233  >> no_skip[!char_("a-zA-Z0-9_")])
234  > lit('[')
235  > expression_g(_r1)
236  [validate_int_expr_f(_1, _pass, boost::phoenix::ref(error_msgs_))]
237  > lit(']')
238  > identifier_r
239  > opt_dims_r(_r1);
240 
241  ordered_decl_r.name("ordered declaration");
243  %= (lit("ordered")
244  >> no_skip[!char_("a-zA-Z0-9_")])
245  > lit('[')
246  > expression_g(_r1)
247  [validate_int_expr_f(_1, _pass, boost::phoenix::ref(error_msgs_))]
248  > lit(']')
249  > identifier_r
250  > opt_dims_r(_r1);
251 
252  positive_ordered_decl_r.name("positive_ordered declaration");
254  %= (lit("positive_ordered")
255  >> no_skip[!char_("a-zA-Z0-9_")])
256  > lit('[')
257  > expression_g(_r1)
258  [validate_int_expr_f(_1, _pass, boost::phoenix::ref(error_msgs_))]
259  > lit(']')
260  > identifier_r
261  > opt_dims_r(_r1);
262 
263  cholesky_factor_decl_r.name("cholesky factor for symmetric,"
264  " positive-def declaration");
266  %= (lit("cholesky_factor_cov")
267  >> no_skip[!char_("a-zA-Z0-9_")])
268  > lit('[')
269  > expression_g(_r1)
270  [validate_int_expr_f(_1, _pass, boost::phoenix::ref(error_msgs_))]
271  > -(lit(',')
272  > expression_g(_r1)
273  [validate_int_expr_f(_1, _pass,
274  boost::phoenix::ref(error_msgs_))])
275  > lit(']')
276  > identifier_r
277  > opt_dims_r(_r1)
278  > eps
280 
281  cholesky_corr_decl_r.name("cholesky factor for"
282  " correlation matrix declaration");
284  %= (lit("cholesky_factor_corr")
285  >> no_skip[!char_("a-zA-Z0-9_")])
286  > lit('[')
287  > expression_g(_r1)
288  [validate_int_expr_f(_1, _pass, boost::phoenix::ref(error_msgs_))]
289  > lit(']')
290  > identifier_r
291  > opt_dims_r(_r1);
292 
293  cov_matrix_decl_r.name("covariance matrix declaration");
295  %= (lit("cov_matrix")
296  >> no_skip[!char_("a-zA-Z0-9_")])
297  > lit('[')
298  > expression_g(_r1)
299  [validate_int_expr_f(_1, _pass, boost::phoenix::ref(error_msgs_))]
300  > lit(']')
301  > identifier_r
302  > opt_dims_r(_r1);
303 
304  corr_matrix_decl_r.name("correlation matrix declaration");
306  %= (lit("corr_matrix")
307  >> no_skip[!char_("a-zA-Z0-9_")])
308  > lit('[')
309  > expression_g(_r1)
310  [validate_int_expr_f(_1, _pass, boost::phoenix::ref(error_msgs_))]
311  > lit(']')
312  > identifier_r
313  > opt_dims_r(_r1);
314 
315  opt_dims_r.name("array dimensions (optional)");
316  opt_dims_r
317  %= - dims_r(_r1);
318 
319  dims_r.name("array dimensions");
320  dims_r
321  %= lit('[')
322  > (expression_g(_r1)
323  [validate_int_data_expr_f(_1, _r1, _pass,
324  boost::phoenix::ref(var_map_),
325  boost::phoenix::ref(error_msgs_))]
326  % ',')
327  > lit(']');
328 
329  range_brackets_int_r.name("integer range expression pair, brackets");
331  = lit('<') [empty_range_f(_val, boost::phoenix::ref(error_msgs_))]
332  >> (
333  ((lit("lower")
334  >> lit('=')
335  >> expression07_g(_r1)
336  [set_int_range_lower_f(_val, _1, _pass,
337  boost::phoenix::ref(error_msgs_))])
338  >> -(lit(',')
339  >> lit("upper")
340  >> lit('=')
341  >> expression07_g(_r1)
342  [set_int_range_upper_f(_val, _1, _pass,
343  boost::phoenix::ref(error_msgs_))]))
344  |
345  (lit("upper")
346  >> lit('=')
347  >> expression07_g(_r1)
348  [set_int_range_upper_f(_val, _1, _pass,
349  boost::phoenix::ref(error_msgs_))])
350  )
351  >> lit('>');
352 
353  range_brackets_double_r.name("real range expression pair, brackets");
355  = lit('<')[empty_range_f(_val, boost::phoenix::ref(error_msgs_))]
356  > (
357  ((lit("lower")
358  > lit('=')
359  > expression07_g(_r1)
360  [set_double_range_lower_f(_val, _1, _pass,
361  boost::phoenix::ref(error_msgs_))])
362  > -(lit(',')
363  > lit("upper")
364  > lit('=')
365  > expression07_g(_r1)
366  [set_double_range_upper_f(_val, _1, _pass,
367  boost::phoenix::ref(error_msgs_))]))
368  |
369  (lit("upper")
370  > lit('=')
371  > expression07_g(_r1)
372  [set_double_range_upper_f(_val, _1, _pass,
373  boost::phoenix::ref(error_msgs_))])
374  )
375  > lit('>');
376 
377  identifier_r.name("identifier");
380  [validate_identifier_f(_val, _pass,
381  boost::phoenix::ref(error_msgs_))];
382 
383  identifier_name_r.name("identifier subrule");
385  %= lexeme[char_("a-zA-Z")
386  >> *char_("a-zA-Z0-9_.")];
387  }
388  }
389 
390 
391 }
392 #endif
393 
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, 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_int_expr > validate_int_expr_f
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, 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.