Calculator  Step 2
parse.hpp
Go to the documentation of this file.
1 #ifndef PARSE_HPP_
2 #define PARSE_HPP_
3 
4 #include <istream>
5 #include <locale>
6 #include <ostream>
7 #include <stdexcept>
8 #include <string>
9 #include <vector>
10 
11 #include "variables.hpp"
12 
14 class parse_error : public std::runtime_error {
15 public:
16  parse_error(std::string const& msg) : runtime_error{msg} {}
17 };
18 
31 class parser
32 {
33 public:
37  enum kind : int { eof, identifier, number,
38  plus='+', minus='-', times='*', slash='/', lparen = '(', rparen=')', equal='=' };
39 
43  parser(std::istream& input);
44 
50  bool get_expr(double& result);
51 
52 private:
57  std::string charify(char c);
63  bool get_number(std::string const& token, double& result);
71  bool get_add_expr(double& result);
79  bool get_mul_expr(double& result);
87  bool get_primary(double& result);
95  bool get_unary(double& result);
107  kind get_token(std::string& token);
112  void get_identifier(std::string& identifier);
113 
119  void push_back(std::string const& token, kind k) { token_ = token; kind_ = k; }
120 
125  bool isalpha(char c) const { return ctype_.is(ctype_.alpha, c); }
130  bool isalnum(char c) const { return ctype_.is(ctype_.alnum, c); }
135  bool isdigit(char c) const { return ctype_.is(ctype_.digit, c); }
140  bool isprint(char c) const { return ctype_.is(ctype_.print, c); }
141 
142  std::istream& input_;
143  std::ctype<char> const& ctype_;
144  std::string token_;
146 };
147 
153 void parse_loop(std::istream& input, std::ostream& output);
154 
155 #endif
bool get_primary(double &result)
Definition: parse.cpp:239
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:267
bool isprint(char c) const
Definition: parse.hpp:140
parser(std::istream &input)
Definition: parse.cpp:5
parse_error(std::string const &msg)
Definition: parse.hpp:16
void get_identifier(std::string &identifier)
Definition: parse.cpp:36
std::string charify(char c)
Definition: parse.cpp:12
void push_back(std::string const &token, kind k)
Definition: parse.hpp:119
Definition: parse.hpp:31
bool get_mul_expr(double &result)
Definition: parse.cpp:196
bool isalpha(char c) const
Definition: parse.hpp:125
kind get_token(std::string &token)
Definition: parse.cpp:62
std::string token_
One token push-back.
Definition: parse.hpp:144
bool get_number(std::string const &token, double &result)
Definition: parse.cpp:135
bool get_expr(double &result)
Definition: parse.cpp:142
bool get_add_expr(double &result)
Definition: parse.cpp:174
std::istream & input_
Share the input stream.
Definition: parse.hpp:142
bool get_unary(double &result)
Definition: parse.cpp:220
kind kind_
The kind of token that was pushed back.
Definition: parse.hpp:145
kind
Definition: parse.hpp:37
std::ctype< char > const & ctype_
Cache the ctype facet for checking character categories.
Definition: parse.hpp:143
bool isalnum(char c) const
Definition: parse.hpp:130
bool isdigit(char c) const
Definition: parse.hpp:135