Stan  2.10.0
probability, sampling & optimization
functions_grammar_def.hpp
Go to the documentation of this file.
1 #ifndef STAN_LANG_GRAMMARS_FUNCTIONS_GRAMMAR_DEF_HPP
2 #define STAN_LANG_GRAMMARS_FUNCTIONS_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 <string>
11 #include <vector>
12 
14  (stan::lang::expr_type, return_type_)
15  (std::string, name_)
16  (std::vector<stan::lang::arg_decl>, arg_decls_)
17  (stan::lang::statement, body_) )
18 
19 BOOST_FUSION_ADAPT_STRUCT(stan::lang::arg_decl,
20  (stan::lang::expr_type, arg_type_)
21  (std::string, name_)
22  (stan::lang::statement, body_) )
23 
24 namespace stan {
25 
26  namespace lang {
27 
28  template <typename Iterator>
29  functions_grammar<Iterator>::functions_grammar(variable_map& var_map,
30  std::stringstream& error_msgs)
31  : functions_grammar::base_type(functions_r),
32  var_map_(var_map),
33  functions_declared_(),
34  functions_defined_(),
35  error_msgs_(error_msgs),
36  statement_g(var_map_, error_msgs_),
37  bare_type_g(var_map_, error_msgs_) {
38  using boost::spirit::qi::_1;
39  using boost::spirit::qi::char_;
40  using boost::spirit::qi::eps;
41  using boost::spirit::qi::lexeme;
42  using boost::spirit::qi::lit;
43  using boost::spirit::qi::_pass;
44  using boost::spirit::qi::_val;
45  using boost::spirit::qi::labels::_a;
46  using boost::spirit::qi::labels::_b;
47 
48  functions_r.name("function declarations and definitions");
50  %= (lit("functions") > lit("{"))
51  >> *function_r
52  > lit('}')
53  > eps[validate_declarations_f(_pass,
54  boost::phoenix::ref(functions_declared_),
55  boost::phoenix::ref(functions_defined_),
56  boost::phoenix::ref(error_msgs_))];
57 
58  // locals: _a = allow sampling, _b = origin (function, rng/lp)
59  function_r.name("function declaration or definition");
61  %= bare_type_g[set_void_function_f(_1, _b, _pass,
62  boost::phoenix::ref(error_msgs_))]
63  > identifier_r
64  [set_allows_sampling_origin_f(_1, _a, _b)]
65  [validate_prob_fun_f(_1, _pass, boost::phoenix::ref(error_msgs_))]
66  > lit('(')
67  > arg_decls_r
69  > eps
70  [validate_pmf_pdf_variate_f(_val, _pass,
71  boost::phoenix::ref(error_msgs_))]
72  > eps[scope_lp_f(boost::phoenix::ref(var_map_))]
73  > statement_g(_a, _b, true)
74  > eps[unscope_variables_f(_val, boost::phoenix::ref(var_map_))]
75  > eps[validate_return_type_f(_val, _pass,
76  boost::phoenix::ref(error_msgs_))]
77  > eps[add_function_signature_f(_val, _pass,
78  boost::phoenix::ref(functions_declared_),
79  boost::phoenix::ref(functions_defined_),
80  boost::phoenix::ref(error_msgs_))];
81 
82  close_arg_decls_r.name("argument declaration or close paren )"
83  " to end argument declarations");
84  close_arg_decls_r %= lit(')');
85 
86  arg_decls_r.name("function argument declaration sequence");
88  %= arg_decl_r % ','
89  | eps;
90 
91  arg_decl_r.name("function argument declaration");
94  boost::phoenix::ref(error_msgs_))]
95  > identifier_r
96  > eps[add_fun_var_f(_val, _pass,
97  boost::phoenix::ref(var_map_),
98  boost::phoenix::ref(error_msgs_))];
99 
100  identifier_r.name("identifier");
102  %= lexeme[char_("a-zA-Z")
103  >> *char_("a-zA-Z0-9_.")];
104  }
105 
106  }
107 }
108 #endif
109 
boost::phoenix::function< set_void_function > set_void_function_f
boost::spirit::qi::rule< Iterator, std::vector< arg_decl >), whitespace_grammar< Iterator > > arg_decls_r
boost::spirit::qi::rule< Iterator, boost::spirit::qi::locals< bool, int >, function_decl_def(), whitespace_grammar< Iterator > > function_r
boost::spirit::qi::rule< Iterator, arg_decl(), whitespace_grammar< Iterator > > arg_decl_r
boost::spirit::qi::rule< Iterator, boost::spirit::qi::unused_type, whitespace_grammar< Iterator > > close_arg_decls_r
Probability, optimization and sampling library.
Template specification of functions in std for Stan.
bare_type_grammar< Iterator > bare_type_g
boost::spirit::qi::rule< Iterator, std::string(), whitespace_grammar< Iterator > > identifier_r
boost::phoenix::function< validate_non_void_arg_function > validate_non_void_arg_f
boost::spirit::qi::rule< Iterator, std::vector< function_decl_def >), whitespace_grammar< Iterator > > functions_r
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_prob_fun > validate_prob_fun_f
boost::phoenix::function< validate_pmf_pdf_variate > validate_pmf_pdf_variate_f
std::set< std::pair< std::string, function_signature_t > > functions_defined_
boost::phoenix::function< set_allows_sampling_origin > set_allows_sampling_origin_f
statement_grammar< Iterator > statement_g
boost::phoenix::function< add_function_signature > add_function_signature_f
boost::phoenix::function< unscope_variables > unscope_variables_f
std::set< std::pair< std::string, function_signature_t > > functions_declared_
boost::phoenix::function< validate_return_type > validate_return_type_f
boost::phoenix::function< add_fun_var > add_fun_var_f
boost::phoenix::function< validate_declarations > validate_declarations_f
boost::phoenix::function< scope_lp > scope_lp_f

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