Stan  2.14.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  bool allow_undefined)
32  : functions_grammar::base_type(functions_r),
33  var_map_(var_map),
34  functions_declared_(),
35  functions_defined_(),
36  error_msgs_(error_msgs),
37  statement_g(var_map_, error_msgs_),
38  bare_type_g(var_map_, error_msgs_) {
39  using boost::spirit::qi::_1;
40  using boost::spirit::qi::char_;
41  using boost::spirit::qi::eps;
42  using boost::spirit::qi::lexeme;
43  using boost::spirit::qi::lit;
44  using boost::spirit::qi::_pass;
45  using boost::spirit::qi::_val;
46  using boost::spirit::qi::labels::_a;
47  using boost::spirit::qi::labels::_b;
48 
49  functions_r.name("function declarations and definitions");
51  %= (lit("functions") > lit("{"))
52  >> *function_r
53  > lit('}')
54  > eps[validate_declarations_f(_pass,
55  boost::phoenix::ref(functions_declared_),
56  boost::phoenix::ref(functions_defined_),
57  boost::phoenix::ref(error_msgs_),
58  allow_undefined)];
59 
60  // locals: _a = allow sampling, _b = origin (function, rng/lp)
61  function_r.name("function declaration or definition");
63  %= bare_type_g[set_void_function_f(_1, _b, _pass,
64  boost::phoenix::ref(error_msgs_))]
65  > identifier_r
66  [set_allows_sampling_origin_f(_1, _a, _b)]
67  [validate_prob_fun_f(_1, _pass, boost::phoenix::ref(error_msgs_))]
68  > lit('(')
69  > arg_decls_r
71  > eps
72  [validate_pmf_pdf_variate_f(_val, _pass,
73  boost::phoenix::ref(error_msgs_))]
74  > eps[scope_lp_f(boost::phoenix::ref(var_map_))]
75  > statement_g(_a, _b, true, false)
76  > eps[unscope_variables_f(_val, boost::phoenix::ref(var_map_))]
77  > eps[validate_return_type_f(_val, _pass,
78  boost::phoenix::ref(error_msgs_))]
79  > eps[add_function_signature_f(_val, _pass,
80  boost::phoenix::ref(functions_declared_),
81  boost::phoenix::ref(functions_defined_),
82  boost::phoenix::ref(error_msgs_))];
83 
84  close_arg_decls_r.name("argument declaration or close paren )"
85  " to end argument declarations");
86  close_arg_decls_r %= lit(')');
87 
88  arg_decls_r.name("function argument declaration sequence");
90  %= arg_decl_r % ','
91  | eps;
92 
93  arg_decl_r.name("function argument declaration");
96  boost::phoenix::ref(error_msgs_))]
97  > identifier_r
98  > eps[add_fun_var_f(_val, _pass,
99  boost::phoenix::ref(var_map_),
100  boost::phoenix::ref(error_msgs_))];
101 
102  identifier_r.name("identifier");
104  %= lexeme[char_("a-zA-Z")
105  >> *char_("a-zA-Z0-9_.")];
106  }
107 
108  }
109 }
110 #endif
111 
boost::phoenix::function< set_void_function > set_void_function_f
Structure to wrap the variant type of statements.
Definition: statement.hpp:29
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
Structure of the type of an expression, which consists of a base type and a number of dimensions...
Definition: expr_type.hpp:14
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_
AST node for a function declaration and definition including return type name, arguments, and body.
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.