Stan  2.14.0
probability, sampling & optimization
program_grammar_def.hpp
Go to the documentation of this file.
1 #ifndef STAN_LANG_GRAMMARS_PROGRAM_GRAMMAR_DEF_HPP
2 #define STAN_LANG_GRAMMARS_PROGRAM_GRAMMAR_DEF_HPP
3 
4 #include <stan/lang/ast.hpp>
7 #include <boost/format.hpp>
8 #include <boost/fusion/include/std_pair.hpp>
9 #include <boost/spirit/home/support/iterators/line_pos_iterator.hpp>
10 #include <boost/spirit/include/phoenix_core.hpp>
11 #include <boost/spirit/include/qi.hpp>
12 #include <iomanip>
13 #include <sstream>
14 #include <string>
15 #include <utility>
16 #include <vector>
17 
18 namespace {
19  // hack to pass pair into macro below to adapt; in namespace to hide
20  struct DUMMY_STRUCT {
21  typedef std::pair<std::vector<stan::lang::var_decl>,
22  std::vector<stan::lang::statement> > type;
23  };
24 }
25 
27  (std::vector<stan::lang::function_decl_def>,
28  function_decl_defs_)
29  (std::vector<stan::lang::var_decl>, data_decl_)
30  (DUMMY_STRUCT::type, derived_data_decl_)
31  (std::vector<stan::lang::var_decl>, parameter_decl_)
32  (DUMMY_STRUCT::type, derived_decl_)
33  (stan::lang::statement, statement_)
34  (DUMMY_STRUCT::type, generated_decl_) )
35 
36 
37 namespace stan {
38 
39  namespace lang {
40 
41  template <typename Iterator>
42  program_grammar<Iterator>::program_grammar(const std::string& model_name,
43  bool allow_undefined)
44  : program_grammar::base_type(program_r),
45  model_name_(model_name),
46  var_map_(),
47  error_msgs_(),
48  expression_g(var_map_, error_msgs_),
49  var_decls_g(var_map_, error_msgs_),
50  statement_g(var_map_, error_msgs_),
51  functions_g(var_map_, error_msgs_, allow_undefined) {
52  using boost::spirit::qi::eps;
53  using boost::spirit::qi::lit;
54  using boost::spirit::qi::on_error;
55  using boost::spirit::qi::rethrow;
56  using boost::spirit::qi::_1;
57  using boost::spirit::qi::_2;
58  using boost::spirit::qi::_3;
59  using boost::spirit::qi::_4;
60 
61  // add model_name to var_map with special origin
62  var_map_.add(model_name, base_var_decl(), model_name_origin);
63 
64  program_r.name("program");
65  program_r
66  %= -functions_g
67  > -data_var_decls_r
68  > -derived_data_var_decls_r
69  > -param_var_decls_r
70  > eps[add_lp_var_f(boost::phoenix::ref(var_map_))]
71  > -derived_var_decls_r
72  > model_r
73  > eps[remove_lp_var_f(boost::phoenix::ref(var_map_))]
74  > -generated_var_decls_r;
75 
76  model_r.name("model declaration (or perhaps an earlier block)");
77  model_r
78  %= lit("model")
79  > statement_g(true, local_origin, false, false);
80 
81  end_var_decls_r.name(
82  "one of the following:\n"
83  " a variable declaration, beginning with type,\n"
84  " (int, real, vector, row_vector, matrix, unit_vector,\n"
85  " simplex, ordered, positive_ordered,\n"
86  " corr_matrix, cov_matrix,\n"
87  " cholesky_corr, cholesky_cov\n"
88  " or '}' to close variable declarations");
89  end_var_decls_r %= lit('}');
90 
91  end_var_decls_statements_r.name(
92  "one of the following:\n"
93  " a variable declaration, beginning with type\n"
94  " (int, real, vector, row_vector, matrix, unit_vector,\n"
95  " simplex, ordered, positive_ordered,\n"
96  " corr_matrix, cov_matrix,\n"
97  " cholesky_corr, cholesky_cov\n"
98  " or a <statement>\n"
99  " or '}' to close variable declarations and definitions");
100  end_var_decls_statements_r %= lit('}');
101 
102  end_var_definitions_r.name("expected another statement or '}'"
103  " to close declarations");
104  end_var_definitions_r %= lit('}');
105 
106  data_var_decls_r.name("data variable declarations");
107  data_var_decls_r
108  %= (lit("data")
109  > lit('{'))
110  > var_decls_g(true, data_origin) // +constraints
111  > end_var_decls_r;
112 
113  derived_data_var_decls_r.name("transformed data block");
114  derived_data_var_decls_r
115  %= ((lit("transformed")
116  >> lit("data"))
117  > lit('{'))
118  > var_decls_g(true, transformed_data_origin) // -constraints
119  > ((statement_g(false, transformed_data_origin, false, false)
120  > *statement_g(false, transformed_data_origin, false, false)
121  > end_var_definitions_r)
122  | (*statement_g(false, transformed_data_origin, false, false)
123  > end_var_decls_statements_r));
124 
125  param_var_decls_r.name("parameter variable declarations");
126  param_var_decls_r
127  %= (lit("parameters")
128  > lit('{'))
129  > var_decls_g(true, parameter_origin) // +constraints
130  > end_var_decls_r;
131 
132  derived_var_decls_r.name("derived variable declarations");
133  derived_var_decls_r
134  %= (lit("transformed")
135  > lit("parameters")
136  > lit('{'))
137  > var_decls_g(true, transformed_parameter_origin)
138  > *statement_g(false, transformed_parameter_origin, false, false)
139  > end_var_decls_statements_r;
140 
141  generated_var_decls_r.name("generated variable declarations");
142  generated_var_decls_r
143  %= (lit("generated")
144  > lit("quantities")
145  > lit('{'))
146  > var_decls_g(true, derived_origin)
147  > *statement_g(false, derived_origin, false, false)
148  > end_var_decls_statements_r;
149 
150  on_error<rethrow>(program_r,
151  program_error_f(_1, _2, _3,
152  boost::phoenix::ref(var_map_),
153  boost::phoenix::ref(error_msgs_)));
154  }
155 
156  }
157 }
158 #endif
Structure to wrap the variant type of statements.
Definition: statement.hpp:29
const int derived_origin
The origin of the variable is ???.
Definition: var_origin.hpp:42
const int parameter_origin
The origin of the variable is the parameter block.
Definition: var_origin.hpp:32
Probability, optimization and sampling library.
boost::phoenix::function< program_error > program_error_f
const int transformed_parameter_origin
The origin of the variable is the transformed parameter block.
Definition: var_origin.hpp:37
boost::phoenix::function< remove_lp_var > remove_lp_var_f
const int model_name_origin
Origin of variable is the name of the model.
Definition: var_origin.hpp:17
const int data_origin
The origin of the variable is the data block.
Definition: var_origin.hpp:22
boost::phoenix::function< add_lp_var > add_lp_var_f
BOOST_FUSION_ADAPT_STRUCT(stan::lang::program,(std::vector< stan::lang::function_decl_def >, function_decl_defs_)(std::vector< stan::lang::var_decl >, data_decl_)(DUMMY_STRUCT::type, derived_data_decl_)(std::vector< stan::lang::var_decl >, parameter_decl_)(DUMMY_STRUCT::type, derived_decl_)(stan::lang::statement, statement_)(DUMMY_STRUCT::type, generated_decl_)) namespace stan
const int local_origin
The origin of the variable is as a local variable.
Definition: var_origin.hpp:47
AST node for a complete Stan program.
Definition: program.hpp:17
const int transformed_data_origin
The origin of the variable is the transformed data block.
Definition: var_origin.hpp:27

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