1 #ifndef STAN_COMMAND_STANC_HELPER_HPP 2 #define STAN_COMMAND_STANC_HELPER_HPP 19 if (!out_stream)
return;
20 *out_stream <<
"stanc version " 37 if (!out_stream)
return;
39 *out_stream << std::endl;
41 *out_stream << std::endl;
43 *out_stream <<
"USAGE: " <<
"stanc [options] <model_file>" << std::endl;
44 *out_stream << std::endl;
46 *out_stream <<
"OPTIONS:" << std::endl;
47 *out_stream << std::endl;
55 "default = \"$model_filename_model\"");
58 "Output file for generated C++ code",
59 "default = \"$name.cpp\"");
62 "Do not fail if a function is declared but not defined");
75 const std::string& file_name) {
76 if (file_name.size() == 0)
78 int return_code = std::remove(file_name.c_str());
81 *err_stream <<
"Could not remove output file=" << file_name
101 std::ostream* out_stream, std::ostream* err_stream) {
102 static const int SUCCESS_RC = 0;
103 static const int EXCEPTION_RC = -1;
104 static const int PARSE_FAIL_RC = -2;
105 static const int INVALID_ARGUMENT_RC = -3;
107 std::string out_file_name;
123 std::string msg(
"Require model file as argument. ");
124 throw std::invalid_argument(msg);
126 std::string in_file_name;
127 cmd.
bare(0, in_file_name);
128 std::ifstream in(in_file_name.c_str());
130 std::string model_name;
132 cmd.
val(
"name", model_name);
134 size_t slashInd = in_file_name.rfind(
'/');
135 size_t ptInd = in_file_name.rfind(
'.');
136 if (ptInd == std::string::npos)
137 ptInd = in_file_name.length();
138 if (slashInd == std::string::npos) {
139 slashInd = in_file_name.rfind(
'\\');
141 if (slashInd == std::string::npos) {
146 model_name = in_file_name.substr(slashInd, ptInd - slashInd) +
"_model";
147 for (std::string::iterator strIt = model_name.begin();
148 strIt != model_name.end(); strIt++) {
149 if (!isalnum(*strIt) && *strIt !=
'_') {
156 cmd.
val(
"o", out_file_name);
158 out_file_name = model_name;
160 out_file_name +=
".cpp";
163 if (!isalpha(model_name[0]) && model_name[0] !=
'_') {
164 std::string msg(
"model_name must not start with a " 165 "number or symbol other than _");
166 throw std::invalid_argument(msg);
168 for (std::string::iterator strIt = model_name.begin();
169 strIt != model_name.end(); strIt++) {
170 if (!isalnum(*strIt) && *strIt !=
'_') {
171 std::string msg(
"model_name must contain only letters, numbers and _");
172 throw std::invalid_argument(msg);
176 bool allow_undefined = cmd.
has_flag(
"allow_undefined");
178 std::fstream out(out_file_name.c_str(), std::fstream::out);
180 *out_stream <<
"Model name=" << model_name << std::endl;
181 *out_stream <<
"Input file=" << in_file_name << std::endl;
182 *out_stream <<
"Output file=" << out_file_name << std::endl;
191 *err_stream <<
"PARSING FAILED." << std::endl;
194 return PARSE_FAIL_RC;
196 }
catch (
const std::invalid_argument& e) {
198 *err_stream << std::endl
203 return INVALID_ARGUMENT_RC;
204 }
catch (
const std::exception& e) {
206 *err_stream << std::endl
bool compile(std::ostream *msgs, std::istream &stan_lang_in, std::ostream &cpp_out, const std::string &model_name, const bool allow_undefined=false)
Read a Stan model specification from the specified input, parse it, and write the C++ code for it to ...
void delete_file(std::ostream *err_stream, const std::string &file_name)
Delte the file at the specified path, writing messages to error stream if not possible.
Parses and stores command-line arguments.
void print_version(std::ostream *out_stream)
Print the version of stanc with major, minor and patch.
bool has_flag(const std::string &flag) const
Return true if the specified flag is defined.
bool bare(size_t n, T &x) const
Returns the bare argument.
int stanc_helper(int argc, const char *argv[], std::ostream *out_stream, std::ostream *err_stream)
Invoke the stanc command on the specified argument list, writing output and error messages to the spe...
const std::string MINOR_VERSION
Minor version number for Stan package.
bool has_key(const std::string &key) const
Return true if the specified key is defined.
bool val(const std::string &key, T &x) const
Returns the value for the key provided.
const std::string PATCH_VERSION
Patch version for Stan package.
void print_stanc_help(std::ostream *out_stream)
Prints the Stan compiler (stanc) help.
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.
size_t bare_size() const
Return the number of bare arguments.
const std::string MAJOR_VERSION
Major version number for Stan package.