Calculator Step 2

- Main Page
- Classes
- Files

- File List
- File Members

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_;
145 kind kind_;
146 };
147
153 void parse_loop(std::istream& input, std::ostream& output);
154
155 #endif
parser::slash
Definition: parse.hpp:38
parser::get_primary
bool get_primary(double &result)
Definition: parse.cpp:239
parse_loop
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:267
parser::isprint
bool isprint(char c) const
Definition: parse.hpp:140
parser::times
Definition: parse.hpp:38
parser::lparen
Definition: parse.hpp:38
parser::rparen
Definition: parse.hpp:38
parser::parser
parser(std::istream &input)
Definition: parse.cpp:5
parser::plus
Definition: parse.hpp:38
parse_error::parse_error
parse_error(std::string const &msg)
Definition: parse.hpp:16
parser::get_identifier
void get_identifier(std::string &identifier)
Definition: parse.cpp:36
parser::charify
std::string charify(char c)
Definition: parse.cpp:12
parser::push_back
void push_back(std::string const &token, kind k)
Definition: parse.hpp:119
parser
Definition: parse.hpp:31
parser::get_mul_expr
bool get_mul_expr(double &result)
Definition: parse.cpp:196
parser::isalpha
bool isalpha(char c) const
Definition: parse.hpp:125
parser::get_token
kind get_token(std::string &token)
Definition: parse.cpp:62
parser::token_
std::string token_
One token push-back.
Definition: parse.hpp:144
parser::minus
Definition: parse.hpp:38
parser::get_number
bool get_number(std::string const &token, double &result)
Definition: parse.cpp:135
parser::get_expr
bool get_expr(double &result)
Definition: parse.cpp:142
parse_error
Definition: parse.hpp:14
parser::get_add_expr
bool get_add_expr(double &result)
Definition: parse.cpp:174
parser::number
Definition: parse.hpp:37
parser::equal
Definition: parse.hpp:38
parser::input_
std::istream & input_
Share the input stream.
Definition: parse.hpp:142
parser::get_unary
bool get_unary(double &result)
Definition: parse.cpp:220
parser::kind_
kind kind_
The kind of token that was pushed back.
Definition: parse.hpp:145
parser::eof
Definition: parse.hpp:37
parser::kind
kind
Definition: parse.hpp:37
parser::ctype_
std::ctype< char > const & ctype_
Cache the ctype facet for checking character categories.
Definition: parse.hpp:143
variables.hpp
parser::isalnum
bool isalnum(char c) const
Definition: parse.hpp:130
parser::identifier
Definition: parse.hpp:37
parser::isdigit
bool isdigit(char c) const
Definition: parse.hpp:135