1 #ifndef STAN_COMMAND_STANC_HELPER_HPP 2 #define STAN_COMMAND_STANC_HELPER_HPP 22 if (!out_stream)
return;
23 *out_stream <<
"stanc version " 40 if (!out_stream)
return;
46 *out_stream <<
"USAGE: " <<
"stanc [options] <model_file>" <<
std::endl;
58 "default = \"$model_filename_model\"");
61 "Output file for generated C++ code",
62 "default = \"$name.cpp\"");
65 "Do not fail if a function is declared but not defined");
80 if (file_name.size() == 0)
85 *err_stream <<
"Could not remove output file=" << file_name
95 size_t slashInd = in_file_name.rfind(
'/');
96 size_t ptInd = in_file_name.rfind(
'.');
97 if (ptInd == std::string::npos)
98 ptInd = in_file_name.length();
99 if (slashInd == std::string::npos) {
100 slashInd = in_file_name.rfind(
'\\');
102 if (slashInd == std::string::npos) {
108 in_file_name.substr(slashInd, ptInd - slashInd);
109 for (std::string::iterator strIt = result.begin();
110 strIt != result.end(); strIt++) {
111 if (!isalnum(*strIt) && *strIt !=
'_') {
129 if (file_name.length() >= extension.length() + 1) {
130 if (0 == file_name.compare (file_name.length() - extension.length(),
131 extension.length(), extension)
132 && file_name[file_name.length() - extension.length() - 1] ==
'.')
150 if (!isalpha(identifier[0]) && identifier[0] !=
'_') {
152 "number or symbol other than _");
155 for (std::string::const_iterator strIt = identifier.begin();
156 strIt != identifier.end(); strIt++) {
157 if (!isalnum(*strIt) && *strIt !=
'_') {
159 +
" must contain only letters, numbers and _");
181 std::ostream* out_stream, std::ostream* err_stream) {
182 enum CompilationType {
186 static const int SUCCESS_RC = 0;
187 static const int EXCEPTION_RC = -1;
188 static const int PARSE_FAIL_RC = -2;
189 static const int INVALID_ARGUMENT_RC = -3;
211 cmd.
bare(0, in_file_name);
213 CompilationType compilation_type;
215 compilation_type = kStandaloneFunctions;
217 compilation_type = kModel;
220 std::ifstream
in(in_file_name.c_str());
222 std::stringstream
msg;
223 msg <<
"Failed to open model file " 224 << in_file_name.c_str();
228 std::vector<std::string> include_paths;
229 include_paths.push_back(
"");
231 bool allow_undefined = cmd.
has_flag(
"allow_undefined");
233 bool valid_input =
false;
235 switch (compilation_type) {
239 cmd.
val(
"name", model_name);
247 cmd.
val(
"o", out_file_name);
249 out_file_name = model_name;
251 out_file_name +=
".cpp";
258 *out_stream <<
"Model name=" << model_name <<
std::endl;
259 *out_stream <<
"Input file=" << in_file_name <<
std::endl;
260 *out_stream <<
"Output file=" << out_file_name <<
std::endl;
262 if (!
out.is_open()) {
263 std::stringstream
msg;
264 msg <<
"Failed to open output file " 265 << out_file_name.c_str();
270 allow_undefined, in_file_name,
275 case kStandaloneFunctions: {
277 cmd.
val(
"o", out_file_name);
280 out_file_name +=
".hpp";
285 std::vector<std::string> namespaces;
286 if (cmd.
has_key(
"namespace")) {
288 cmd.
val(
"namespace", ns);
289 namespaces.push_back(ns);
297 for (
size_t namespace_i = 0;
298 namespace_i < namespaces.size(); ++namespace_i) {
304 *out_stream <<
"Parsing a fuctions-only file" <<
std::endl;
306 *out_stream <<
"Target namespace= ";
307 for (
size_t namespace_i = 0;
308 namespace_i < namespaces.size(); ++namespace_i) {
309 *out_stream <<
"::" << namespaces[namespace_i];
313 *out_stream <<
"Input file=" << in_file_name <<
std::endl;
314 *out_stream <<
"Output file=" << out_file_name <<
std::endl;
318 namespaces, allow_undefined);
330 *err_stream <<
"PARSING FAILED." <<
std::endl;
333 return PARSE_FAIL_RC;
342 return INVALID_ARGUMENT_RC;
bool compile(std::ostream *msgs, std::istream &in, std::ostream &out, const std::string &name, const bool allow_undefined=false, const std::string &filename="unknown file name", const std::vector< std::string > &include_paths=std::vector< std::string >())
std::string identifier_from_file_name(const std::string &in_file_name)
void check_identifier(const std::string &identifier, const std::string &identifier_type)
bool val(const std::string &key, T &x) const
::xsd::cxx::tree::exception< char > exception
void delete_file(std::ostream *err_stream, const std::string &file_name)
bool has_key(const std::string &key) const
bool has_extension(const std::string &file_name, const std::string &extension)
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
const std::string MINOR_VERSION
bool compile_functions(std::ostream *msgs, std::istream &stan_funcs_in, std::ostream &cpp_out, const std::vector< std::string > &namespaces, const bool allow_undefined=false, const std::string &filename="unknown file name", const std::vector< std::string > &include_paths=std::vector< std::string >())
void invalid_argument(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
static const double ns
Module that plots metrics from reconstructed cosmic ray data.
const std::string PATCH_VERSION
void print_stanc_help(std::ostream *out_stream)
assert(nhit_max >=nhit_nbins)
bool bare(size_t n, T &x) const
void print_help_option(std::ostream *o, const std::string &key, const std::string &value_type, const std::string &msg, const std::string ¬e="")
const std::string MAJOR_VERSION