1 #ifndef STAN_COMMAND_STANC_HELPER_HPP
2 #define STAN_COMMAND_STANC_HELPER_HPP
15 if (!out_stream)
return;
16 *out_stream <<
"stanc version "
31 if (!out_stream)
return;
33 *out_stream << std::endl;
35 *out_stream << std::endl;
37 *out_stream <<
"USAGE: " <<
"stanc [options] <model_file>" << std::endl;
38 *out_stream << std::endl;
40 *out_stream <<
"OPTIONS:" << std::endl;
41 *out_stream << std::endl;
49 "default = \"$model_filename_model\"");
52 "Output file for generated C++ code",
53 "default = \"$name.cpp\"");
57 const std::string& file_name) {
58 int deleted = std::remove(file_name.c_str());
59 if (deleted != 0 && file_name.size() > 0)
61 *err_stream <<
"Could not remove output file=" << file_name
67 std::ostream* out_stream, std::ostream* err_stream) {
68 static const int SUCCESS_RC = 0;
69 static const int EXCEPTION_RC = -1;
70 static const int PARSE_FAIL_RC = -2;
71 static const int INVALID_ARGUMENT_RC = -3;
73 std::string out_file_name;
89 std::string msg(
"Require model file as argument. ");
90 throw std::invalid_argument(msg);
92 std::string in_file_name;
93 cmd.
bare(0, in_file_name);
94 std::fstream in(in_file_name.c_str());
96 std::string model_name;
98 cmd.
val(
"name", model_name);
100 size_t slashInd = in_file_name.rfind(
'/');
101 size_t ptInd = in_file_name.rfind(
'.');
102 if (ptInd == std::string::npos)
103 ptInd = in_file_name.length();
104 if (slashInd == std::string::npos) {
105 slashInd = in_file_name.rfind(
'\\');
107 if (slashInd == std::string::npos) {
112 model_name = in_file_name.substr(slashInd, ptInd - slashInd) +
"_model";
113 for (std::string::iterator strIt = model_name.begin();
114 strIt != model_name.end(); strIt++) {
115 if (!isalnum(*strIt) && *strIt !=
'_') {
122 cmd.
val(
"o", out_file_name);
124 out_file_name = model_name;
125 out_file_name +=
".cpp";
128 if (!isalpha(model_name[0]) && model_name[0] !=
'_') {
129 std::string msg(
"model_name must not start with a "
130 "number or symbol other than _");
131 throw std::invalid_argument(msg);
133 for (std::string::iterator strIt = model_name.begin();
134 strIt != model_name.end(); strIt++) {
135 if (!isalnum(*strIt) && *strIt !=
'_') {
136 std::string msg(
"model_name must contain only letters, numbers and _");
137 throw std::invalid_argument(msg);
143 std::fstream out(out_file_name.c_str(),
146 *out_stream <<
"Model name=" << model_name << std::endl;
147 *out_stream <<
"Input file=" << in_file_name << std::endl;
148 *out_stream <<
"Output file=" << out_file_name << std::endl;
157 *err_stream <<
"PARSING FAILED." << std::endl;
160 return PARSE_FAIL_RC;
162 }
catch (
const std::invalid_argument& e) {
164 *err_stream << std::endl
169 return INVALID_ARGUMENT_RC;
170 }
catch (
const std::exception& e) {
172 *err_stream << std::endl
bool compile(std::ostream *msgs, std::istream &stan_lang_in, std::ostream &cpp_out, const std::string &model_name)
Read a Stan model specification from the specified input, parse it, and write the C++ code for it to ...
bool val(const std::string &key, T &x) const
Returns the value for the key provided.
void delete_file(std::ostream *err_stream, const std::string &file_name)
bool has_key(const std::string &key) const
Return true if the specified key is defined.
Parses and stores command-line arguments.
void print_version(std::ostream *out_stream)
int stanc_helper(int argc, const char *argv[], std::ostream *out_stream, std::ostream *err_stream)
bool has_flag(const std::string &flag) const
Return true if the specified flag is defined.
const std::string MINOR_VERSION
Minor version number for Stan package.
size_t bare_size() const
Return the number of bare arguments.
const std::string PATCH_VERSION
Patch version for Stan package.
void print_stanc_help(std::ostream *out_stream)
Prints the Stan compiler (stanc) help.
bool bare(size_t n, T &x) const
Returns the bare argument.
void print_help_option(std::ostream *o, const std::string &key, const std::string &value_type, const std::string &msg, const std::string ¬e="")
Prints single print option to output ptr if non-null.
const std::string MAJOR_VERSION
Major version number for Stan package.