13 if (!out_stream)
return;
14 *out_stream <<
"stanc version "
29 if (!out_stream)
return;
31 *out_stream << std::endl;
33 *out_stream << std::endl;
35 *out_stream <<
"USAGE: " <<
"stanc [options] <model_file>" << std::endl;
36 *out_stream << std::endl;
38 *out_stream <<
"OPTIONS:" << std::endl;
39 *out_stream << std::endl;
47 "default = \"$model_filename_model\"");
50 "Output file for generated C++ code",
51 "default = \"$name.cpp\"");
56 const std::string& file_name) {
57 int deleted = std::remove(file_name.c_str());
58 if (deleted != 0 && file_name.size() > 0)
60 std::cerr <<
"Could not remove output file=" << file_name
66 std::ostream* out_stream, std::ostream* err_stream) {
67 static const int SUCCESS_RC = 0;
68 static const int EXCEPTION_RC = -1;
69 static const int PARSE_FAIL_RC = -2;
70 static const int INVALID_ARGUMENT_RC = -3;
72 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 number or symbol other than _");
130 throw std::invalid_argument(msg);
132 for (std::string::iterator strIt = model_name.begin();
133 strIt != model_name.end(); strIt++) {
134 if (!isalnum(*strIt) && *strIt !=
'_') {
135 std::string msg(
"model_name must contain only letters, numbers and _");
136 throw std::invalid_argument(msg);
141 std::fstream out(out_file_name.c_str(),
144 *out_stream <<
"Model name=" << model_name << std::endl;
145 *out_stream <<
"Input file=" << in_file_name << std::endl;
146 *out_stream <<
"Output file=" << out_file_name << std::endl;
153 *err_stream <<
"PARSING FAILED." << std::endl;
155 return PARSE_FAIL_RC;
157 }
catch (
const std::invalid_argument&
e) {
159 *err_stream << std::endl
162 *err_stream <<
"Execute \"stanc --help\" for more information"
166 return INVALID_ARGUMENT_RC;
167 }
catch (
const std::exception& e) {
169 *err_stream << std::endl
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.
double e()
Return the base of the natural logarithm.
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.
bool compile(std::ostream *msgs, std::istream &stan_gm_in, std::ostream &cpp_out, const std::string &model_name, const std::string &in_file_name="input")
Read a Stan model specification from the specified input, parse it, and write the C++ code for it to ...