Public Types | Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
stan::io::program_reader Class Reference

#include "stan/io/program_reader.hpp"

Public Types

typedef std::pair< std::string, intpath_line_t
 
typedef std::vector< path_line_ttrace_t
 

Public Member Functions

 program_reader (std::istream &in, const std::string &name, const std::vector< std::string > &search_path)
 
 program_reader (const program_reader &r)
 
 program_reader ()
 
std::string program () const
 
trace_t trace (int target) const
 
const std::vector< preproc_event > & history () const
 
void add_event (int concat_line_num, int line_num, const std::string &action, const std::string &path)
 

Private Member Functions

void read (std::istream &in, const std::string &path, const std::vector< std::string > &search_path, int &concat_line_num, bool is_nested, std::set< std::string > &visited_paths)
 
void read (std::istream &in, const std::string &path, const std::vector< std::string > &search_path, int &concat_line_num)
 

Static Private Member Functions

static std::string include_path (const std::string &line)
 

Private Attributes

std::stringstream program_
 
std::vector< preproc_eventhistory_
 

Detailed Description

A program_reader reads a Stan program and unpacks the include statements relative to a search path in such a way that error messages can reproduce the include path.

Definition at line 48 of file program_reader.hpp.

Member Typedef Documentation

typedef std::pair<std::string, int> stan::io::program_reader::path_line_t

A pair for holding a path and a line number.

Definition at line 53 of file program_reader.hpp.

Ordered sequence of path and line number pairs.

Definition at line 58 of file program_reader.hpp.

Constructor & Destructor Documentation

stan::io::program_reader::program_reader ( std::istream &  in,
const std::string &  name,
const std::vector< std::string > &  search_path 
)
inline

Construct a program reader from the specified stream derived from the specified name or path, and a sequence of paths to search for include files. The paths should be directories.

Calling this method does not close the specified input stream.

Parameters
[in]instream from which to start reading
[in]namename path or name attached to stream
[in]search_pathordered sequence of directory names to search for included files

Definition at line 72 of file program_reader.hpp.

73  {
74  int concat_line_num = 0;
75  read(in, name, search_path, concat_line_num);
76  }
const XML_Char * name
Definition: expat.h:151
void read(std::istream &in, const std::string &path, const std::vector< std::string > &search_path, int &concat_line_num, bool is_nested, std::set< std::string > &visited_paths)
ifstream in
Definition: comparison.C:7
stan::io::program_reader::program_reader ( const program_reader r)
inline

Construct a copy of the specified reader. Both the underlying program string and history will be copied.

Parameters
rreader to copy

Definition at line 84 of file program_reader.hpp.

85  : program_(r.program_.str()), history_(r.history_) { }
std::stringstream program_
std::vector< preproc_event > history_
TRandom3 r(0)
stan::io::program_reader::program_reader ( )
inline

Construct a program reader with an empty program and history.

Definition at line 91 of file program_reader.hpp.

91 : program_(""), history_() { }
std::stringstream program_
std::vector< preproc_event > history_

Member Function Documentation

void stan::io::program_reader::add_event ( int  concat_line_num,
int  line_num,
const std::string &  action,
const std::string &  path 
)
inline

Adds preprocessing event with specified components to the back of the history sequence.

Parameters
[in]concat_line_numposition in concatenated program
[in]line_numposition in current file
[in]actionpurpose of preprocessing event
[in]pathlocation of current file

Definition at line 159 of file program_reader.hpp.

References e.

Referenced by model_to_cpp(), stub_reader(), and TEST().

160  {
161  preproc_event e(concat_line_num, line_num, action, path);
162  history_.push_back(e);
163  }
const std::string path
Definition: plot_BEN.C:43
std::vector< preproc_event > history_
Float_t e
Definition: plot.C:35
const std::vector<preproc_event>& stan::io::program_reader::history ( ) const
inline

Return the record of the files and includes used to build up this program.

Returns
I/O history of the program

Definition at line 146 of file program_reader.hpp.

Referenced by stan::lang::compile(), stan::lang::compile_functions(), model_to_cpp(), and TEST().

146  {
147  return history_;
148  }
std::vector< preproc_event > history_
static std::string stan::io::program_reader::include_path ( const std::string &  line)
inlinestaticprivate

Returns the characters following #include on the line, trimming whitespace characters. Assumes that #include" is line initial.

Parameters
lineline of text beginning with #include
Returns
text after #include with whitespace trimmed

Definition at line 178 of file program_reader.hpp.

References febshutoff_auto::end, and febshutoff_auto::start.

178  {
179  int start = std::string("#include").size();
180  while (line[start] == ' ') ++start;
181  int end = line.size() - 1;
182  while (line[end] == ' ') --end;
183  return line.substr(start, end - start);
184  }
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
std::string stan::io::program_reader::program ( ) const
inline

Return a string representing the concatenated program. This string may be wrapped in a std::stringstream for reading.

Returns
stream for program

Definition at line 100 of file program_reader.hpp.

Referenced by stan::lang::compile(), stan::lang::compile_functions(), is_parsable(), and TEST().

100  {
101  return program_.str();
102  }
std::stringstream program_
void stan::io::program_reader::read ( std::istream &  in,
const std::string &  path,
const std::vector< std::string > &  search_path,
int concat_line_num,
bool  is_nested,
std::set< std::string > &  visited_paths 
)
inlineprivate

Definition at line 186 of file program_reader.hpp.

References MakeMiniprodValidationCuts::f, MECModelEnuComparisons::i, make_syst_table_plots::line, stan::io::preproc_event::preproc_event(), stan::io::read_line(), and stan::io::starts_with().

189  {
190  if (visited_paths.find(path) != visited_paths.end())
191  return; // avoids recursive visitation
192  visited_paths.insert(path);
193  history_.push_back(preproc_event(concat_line_num, 0, "start", path));
194  for (int line_num = 1; ; ++line_num) {
196  if (line.empty()) {
197  // ends initial out of loop start event
198  if (!is_nested) {
199  // pad end concat_line_num of outermost file in order to properly
200  // report end-of-file parse error - else trace throws exception
201  history_.push_back(preproc_event(concat_line_num + 2,
202  line_num - 1, "end", path));
203  } else {
204  history_.push_back(preproc_event(concat_line_num,
205  line_num - 1, "end", path));
206  }
207  break;
208  } else if (starts_with("#include ", line)) {
209  std::string incl_path = include_path(line);
210  history_.push_back(preproc_event(concat_line_num, line_num - 1,
211  "include", incl_path));
212  bool found_path = false;
213  for (size_t i = 0; i < search_path.size(); ++i) {
214  std::string f = search_path[i] + incl_path;
215  std::ifstream include_in(f.c_str());
216  if (!include_in.good()) {
217  include_in.close();
218  continue;
219  }
220  try {
221  read(include_in, incl_path, search_path, concat_line_num, true,
222  visited_paths);
223  } catch (...) {
224  include_in.close();
225  throw;
226  }
227  include_in.close();
228  history_.push_back(preproc_event(concat_line_num, line_num,
229  "restart", path));
230  found_path = true;
231  break;
232  }
233  if (!found_path)
234  throw std::runtime_error("could not find include file: " + line);
235  } else {
236  ++concat_line_num;
237  program_ << line;
238  }
239  }
240  visited_paths.erase(path); // allow multiple, just not nested
241  }
void read(std::istream &in, const std::string &path, const std::vector< std::string > &search_path, int &concat_line_num, bool is_nested, std::set< std::string > &visited_paths)
std::string read_line(std::istream &in)
Definition: read_line.hpp:22
std::stringstream program_
bool starts_with(const std::string &p, const std::string &s)
Definition: starts_with.hpp:17
static std::string include_path(const std::string &line)
const std::string path
Definition: plot_BEN.C:43
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
ifstream in
Definition: comparison.C:7
std::vector< preproc_event > history_
void stan::io::program_reader::read ( std::istream &  in,
const std::string &  path,
const std::vector< std::string > &  search_path,
int concat_line_num 
)
inlineprivate

Read the rest of a program from the specified input stream in the specified path, with the specified search path for include files, and incrementing the specified concatenated line number. This method is called recursively for included files. If a file is included recursively, the second include is ignored.

Parameters
[in]instream from which to read
[in]pathname of stream
[in]search_pathsequence of path names to search for include files
[in,out]concat_line_numposition in concatenated file to be updated
Exceptions
std::runtime_errorif an included file cannot be found

Definition at line 260 of file program_reader.hpp.

262  {
263  std::set<std::string> visited_paths;
264  read(in, path, search_path, concat_line_num, false, visited_paths);
265  }
void read(std::istream &in, const std::string &path, const std::vector< std::string > &search_path, int &concat_line_num, bool is_nested, std::set< std::string > &visited_paths)
const std::string path
Definition: plot_BEN.C:43
ifstream in
Definition: comparison.C:7
trace_t stan::io::program_reader::trace ( int  target) const
inline

Return the include trace of the path and line numbers leading to the specified line of text in the concatenated program. The top of the stack is the most recently read path.

Parameters
[in]targetline number in concatenated program file
Returns
sequence of files and positions for includes

Definition at line 112 of file program_reader.hpp.

References stan::io::preproc_event::action_, stan::io::preproc_event::concat_line_num_, file, MECModelEnuComparisons::i, make_syst_table_plots::line, stan::io::preproc_event::line_num_, and fillBadChanDBTables::result.

Referenced by stan::lang::dump_program_line(), expect_trace(), stan::lang::program_error::operator()(), and TEST().

112  {
113  if (target < 1)
114  throw std::runtime_error("trace() argument target must be"
115  " greater than 1");
116  trace_t result;
117  std::string file = "ERROR: UNINITIALIZED";
118  int file_start = -1;
119  int concat_start = -1;
120  for (size_t i = 0; i < history_.size(); ++i) {
121  if (target <= history_[i].concat_line_num_) {
122  int line = file_start + target - concat_start;
123  result.push_back(path_line_t(file, line));
124  return result;
125  } else if (history_[i].action_ == "start"
126  || history_[i].action_ == "restart" ) {
127  file = history_[i].path_;
128  file_start = history_[i].line_num_;
129  concat_start = history_[i].concat_line_num_;
130  } else if (history_[i].action_ == "end") {
131  if (result.size() == 0) break;
132  result.pop_back();
133  } else if (history_[i].action_ == "include") {
134  result.push_back(path_line_t(file, history_[i].line_num_ + 1));
135  }
136  }
137  throw std::runtime_error("ran beyond end of program in trace()");
138  }
const XML_Char * target
Definition: expat.h:268
std::pair< std::string, int > path_line_t
std::vector< path_line_t > trace_t
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
std::vector< preproc_event > history_
TFile * file
Definition: cellShifts.C:17

Member Data Documentation

std::vector<preproc_event> stan::io::program_reader::history_
private

Definition at line 167 of file program_reader.hpp.

std::stringstream stan::io::program_reader::program_
private

Definition at line 166 of file program_reader.hpp.


The documentation for this class was generated from the following file: