parser.hpp
Go to the documentation of this file.
1 #ifndef STAN_LANG_PARSER_HPP
2 #define STAN_LANG_PARSER_HPP
3 
4 #include <stan/lang/ast.hpp>
7 #include <boost/spirit/home/support/iterators/line_pos_iterator.hpp>
8 #include <boost/spirit/include/qi.hpp>
9 #include <istream>
10 #include <ostream>
11 #include <sstream>
12 #include <string>
13 #include <stdexcept>
14 
15 namespace stan {
16  namespace lang {
17 
18  /**
19  * Parse the program from the specified input stream, writing
20  * warning messages to the specified output stream, with the
21  * specified model, setting the specified program to the result,
22  * with a flag indicating whether undefined function declarations
23  * are allowed.
24  *
25  * @param out stream to which error messages and warnings are
26  * written
27  * @param in stream from which the program is read
28  * @param name name of program
29  * @param reader program reader with include structure
30  * @param prog program into which result is written
31  * @param allow_undefined true if functions may be declared but
32  * not defined in the functions block
33  * @return true if parse succeeds
34  */
35  bool parse(std::ostream* out, std::istream& in, const std::string& name,
36  const io::program_reader& reader, program& prog,
37  const bool allow_undefined = false) {
38  using boost::spirit::qi::expectation_failure;
39  using boost::spirit::qi::phrase_parse;
40 
42 
43  std::ostringstream buf;
44  buf << in.rdbuf();
45  std::string stan_string = buf.str() + "\n";
46  if (!is_nonempty(stan_string))
47  *out << std::endl << "WARNING: empty program" << std::endl;
48 
49  typedef std::string::const_iterator input_iterator;
50  typedef boost::spirit::line_pos_iterator<input_iterator> lp_iterator;
51 
52  lp_iterator fwd_begin = lp_iterator(stan_string.begin());
53  lp_iterator fwd_end = lp_iterator(stan_string.end());
54 
55  program_grammar<lp_iterator> prog_grammar(name, reader, allow_undefined);
56  whitespace_grammar<lp_iterator> whitesp_grammar(prog_grammar.error_msgs_);
57 
58  bool parse_succeeded = false;
59  try {
60  parse_succeeded = phrase_parse(fwd_begin, fwd_end, prog_grammar,
61  whitesp_grammar, prog);
62  std::string diagnostics = prog_grammar.error_msgs_.str();
63  if (out && is_nonempty(diagnostics))
64  *out << "DIAGNOSTIC(S) FROM PARSER:" << std::endl
65  << diagnostics << std::endl;
66  } catch (const expectation_failure<lp_iterator>& e) {
67  std::stringstream msg;
68  std::string diagnostics = prog_grammar.error_msgs_.str();
69  if (out && is_nonempty(diagnostics))
70  msg << "SYNTAX ERROR, MESSAGE(S) FROM PARSER:" << std::endl
71  << std::endl << diagnostics;
72  if (out) {
73  std::stringstream ss;
74  ss << e.what_;
75  std::string e_what = ss.str();
76  std::string angle_eps("<eps>");
77  if (e_what != angle_eps)
78  msg << "PARSER EXPECTED: "
79  << e.what_
80  << std::endl;
81  }
82  throw std::invalid_argument(msg.str());
83  } catch (const std::exception& e) {
84  std::stringstream msg;
85  msg << "PROGRAM ERROR, MESSAGE(S) FROM PARSER:"
86  << std::endl
87  << prog_grammar.error_msgs_.str()
88  << std::endl;
89 
90  throw std::invalid_argument(msg.str());
91  }
92  bool consumed_all_input = (fwd_begin == fwd_end);
93  bool success = parse_succeeded && consumed_all_input;
94  if (!success) {
95  std::stringstream msg;
96  if (!parse_succeeded)
97  msg << "PARSE FAILED." << std::endl;
98  if (!consumed_all_input) {
99  std::basic_stringstream<char> unparsed_non_ws;
100  unparsed_non_ws << boost::make_iterator_range(fwd_begin, fwd_end);
101  msg << "PARSER EXPECTED: whitespace to end of file."
102  << std::endl
103  << "FOUND AT line "
104  << get_line(fwd_begin)
105  << ": "
106  << std::endl
107  << unparsed_non_ws.str()
108  << std::endl;
109  }
110  msg << std::endl << prog_grammar.error_msgs_.str() << std::endl;
111  throw std::invalid_argument(msg.str());
112  }
113  return true;
114  }
115 
116  }
117 }
118 #endif
const XML_Char * name
Definition: expat.h:151
::xsd::cxx::tree::diagnostics< char > diagnostics
Definition: Database.h:221
Float_t ss
Definition: plot.C:24
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
bool is_nonempty(const std::string &s)
bool parse(std::ostream *out, std::istream &in, const std::string &name, const io::program_reader &reader, program &prog, const bool allow_undefined=false)
Definition: parser.hpp:35
double lang
Definition: runWimpSim.h:113
def success(message)
Definition: log.py:5
void invalid_argument(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
ifstream in
Definition: comparison.C:7
Float_t e
Definition: plot.C:35
enum BeamMode string