Calculator  Step 5
parse.hpp
Go to the documentation of this file.
1 #ifndef PARSE_HPP_
2 #define PARSE_HPP_
3 
4 #include <cstdlib>
5 #include <istream>
6 #include <locale>
7 #include <ostream>
8 #include <string>
9 
10 #include "calc_error.hpp"
11 #include "node.hpp"
12 
25 class parser
26 {
27 public:
31  enum kind : int { eof, identifier, number, string,
32  plus='+', minus='-', times='*', slash='/', lparen = '(', rparen=')', equal='=',
33  comma=','};
34 
38  parser(std::istream& input);
39 
54  bool get_statement(std::ostream& output);
55 
56 private:
61  std::string charify(char c);
67  bool get_number(std::string const& token, node& result);
72  bool get_expr(node& result);
80  bool get_add_expr(node& result);
88  bool get_mul_expr(node& result);
99  bool get_primary(node& result);
107  bool get_unary(node& result);
120  void get_definition(std::string& name, identifier_list& parameters, node& definition);
132  kind get_token(std::string& token);
137  void get_identifier(std::string& identifier);
141  void get_expr_list(node_list& result);
142 
151  template<class OutputIterator>
152  OutputIterator get_namelist(OutputIterator output);
157  void get_escape(std::string& str);
163  void get_string(std::string& result, char delimiter);
164 
170  void push_back(std::string const& token, kind k);
171 
176  bool isalpha(char c) const { return ctype_.is(ctype_.alpha, c); }
181  bool isalnum(char c) const { return ctype_.is(ctype_.alnum, c); }
186  bool isdigit(char c) const { return ctype_.is(ctype_.digit, c); }
191  bool isprint(char c) const { return ctype_.is(ctype_.print, c); }
192 
193  std::istream& input_;
194  std::ctype<char> const& ctype_;
195  std::string token_;
197 };
198 
204 void parse_loop(std::istream& input, std::ostream& output);
205 
206 template<class OutputIterator>
207 OutputIterator parser::get_namelist(OutputIterator output)
208 {
209  std::string token{};
210  while (kind k = get_token(token)) {
211  if (k == ')')
212  return output;
213  else if (k != identifier)
214  throw syntax_error{"expected function parameter, got " + token};
215  else {
216  *output = token;
217  ++output;
218 
219  k = get_token(token);
220  if (k == ')')
221  return output;
222  if (k != ',')
223  throw syntax_error{"expected comma in function paramter list, got " + token};
224  }
225  }
226  throw syntax_error{"unexpected end of line in function parameter list"};
227 }
228 
229 
230 #endif
void get_string(std::string &result, char delimiter)
Definition: parse.cpp:86
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:421
bool isprint(char c) const
Definition: parse.hpp:191
bool get_expr(node &result)
Definition: parse.cpp:291
parser(std::istream &input)
Definition: parse.cpp:10
void get_identifier(std::string &identifier)
Definition: parse.cpp:102
std::string charify(char c)
Definition: parse.cpp:17
Definition: node.hpp:26
void get_definition(std::string &name, identifier_list &parameters, node &definition)
Definition: parse.cpp:225
void push_back(std::string const &token, kind k)
Definition: parse.cpp:121
bool get_statement(std::ostream &output)
Definition: parse.cpp:246
Definition: parse.hpp:25
bool get_primary(node &result)
Definition: parse.cpp:376
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
Definition: node.hpp:19
OutputIterator get_namelist(OutputIterator output)
Definition: parse.hpp:207
bool get_unary(node &result)
Definition: parse.cpp:334
bool isalpha(char c) const
Definition: parse.hpp:176
kind get_token(std::string &token)
Definition: parse.cpp:130
std::string token_
One token push-back.
Definition: parse.hpp:195
std::istream & input_
Share the input stream.
Definition: parse.hpp:193
kind kind_
The kind of token that was pushed back.
Definition: parse.hpp:196
kind
Definition: parse.hpp:31
std::vector< node > node_list
A sequence of nodes.
Definition: node.hpp:13
bool get_number(std::string const &token, node &result)
Definition: parse.cpp:214
std::ctype< char > const & ctype_
Cache the ctype facet for checking character categories.
Definition: parse.hpp:194
bool get_mul_expr(node &result)
Definition: parse.cpp:315
bool get_add_expr(node &result)
Definition: parse.cpp:296
void get_expr_list(node_list &result)
Definition: parse.cpp:355
bool isalnum(char c) const
Definition: parse.hpp:181
void get_escape(std::string &str)
Definition: parse.cpp:41
bool isdigit(char c) const
Definition: parse.hpp:186