Calculator  Step 6
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 #include "number.hpp"
13 
26 class parser
27 {
28 public:
33  plus='+', minus='-', times='*', slash='/', lparen = '(', rparen=')', equal='=',
34  comma=','};
35 
39  parser(std::istream& input);
40 
55  bool get_statement(std::ostream& output);
56 
57 private:
62  std::string charify(char c);
68  bool get_float(std::string const& token, node& result);
74  bool get_integer(std::string const& token, node& result);
79  bool get_expr(node& result);
87  bool get_add_expr(node& result);
95  bool get_mul_expr(node& result);
106  bool get_primary(node& result);
114  bool get_unary(node& result);
127  void get_definition(std::string& name, identifier_list& parameters, node& definition);
141  kind get_token(std::string& token);
146  void get_identifier(std::string& identifier);
150  void get_expr_list(node_list& result);
151 
160  template<class OutputIterator>
161  OutputIterator get_namelist(OutputIterator output);
166  void get_escape(std::string& str);
172  void get_string(std::string& result, char delimiter);
173 
179  void push_back(std::string const& token, kind k);
180 
185  bool isalpha(char c) const { return ctype_.is(ctype_.alpha, c); }
190  bool isalnum(char c) const { return ctype_.is(ctype_.alnum, c); }
195  bool isdigit(char c) const { return ctype_.is(ctype_.digit, c); }
200  bool isprint(char c) const { return ctype_.is(ctype_.print, c); }
201 
202  std::istream& input_;
203  std::ctype<char> const& ctype_;
204  std::string token_;
206 };
207 
213 void parse_loop(std::istream& input, std::ostream& output);
214 
215 template<class OutputIterator>
216 OutputIterator parser::get_namelist(OutputIterator output)
217 {
218  std::string token{};
219  while (kind k = get_token(token)) {
220  if (k == ')')
221  return output;
222  else if (k != identifier)
223  throw syntax_error{"expected function parameter, got " + token};
224  else {
225  *output = token;
226  ++output;
227 
228  k = get_token(token);
229  if (k == ')')
230  return output;
231  if (k != ',')
232  throw syntax_error{"expected comma in function paramter list, got " + token};
233  }
234  }
235  throw syntax_error{"unexpected end of line in function parameter list"};
236 }
237 
238 
239 #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:444
bool isprint(char c) const
Definition: parse.hpp:200
bool get_expr(node &result)
Definition: parse.cpp:307
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:28
void get_definition(std::string &name, identifier_list &parameters, node &definition)
Definition: parse.cpp:241
void push_back(std::string const &token, kind k)
Definition: parse.cpp:121
bool get_statement(std::ostream &output)
Definition: parse.cpp:262
bool get_integer(std::string const &token, node &result)
Definition: parse.cpp:219
Definition: parse.hpp:26
bool get_primary(node &result)
Definition: parse.cpp:392
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
Definition: node.hpp:21
OutputIterator get_namelist(OutputIterator output)
Definition: parse.hpp:216
bool get_unary(node &result)
Definition: parse.cpp:350
bool isalpha(char c) const
Definition: parse.hpp:185
kind get_token(std::string &token)
Definition: parse.cpp:130
std::string token_
One token push-back.
Definition: parse.hpp:204
bool get_float(std::string const &token, node &result)
Definition: parse.cpp:230
std::istream & input_
Share the input stream.
Definition: parse.hpp:202
kind kind_
The kind of token that was pushed back.
Definition: parse.hpp:205
kind
Definition: parse.hpp:32
std::vector< node > node_list
A sequence of nodes.
Definition: node.hpp:15
std::ctype< char > const & ctype_
Cache the ctype facet for checking character categories.
Definition: parse.hpp:203
bool get_mul_expr(node &result)
Definition: parse.cpp:331
bool get_add_expr(node &result)
Definition: parse.cpp:312
void get_expr_list(node_list &result)
Definition: parse.cpp:371
bool isalnum(char c) const
Definition: parse.hpp:190
void get_escape(std::string &str)
Definition: parse.cpp:41
bool isdigit(char c) const
Definition: parse.hpp:195