Calculator Step 3

- 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
10 class node;
11
13 class parse_error : public std::runtime_error {
14 public:
15 parse_error(std::string const& msg) : runtime_error{msg} {}
16 };
17
30 class parser
31 {
32 public:
36 enum kind : int { eof, identifier, number,
37 plus='+', minus='-', times='*', slash='/', lparen = '(', rparen=')', equal='=' };
38
42 parser(std::istream& input);
43
49 bool get_expr(node& result);
50
51 private:
56 std::string charify(char c);
62 bool get_number(std::string const& token, node& result);
70 bool get_add_expr(node& result);
78 bool get_mul_expr(node& result);
86 bool get_primary(node& result);
94 bool get_unary(node& result);
106 kind get_token(std::string& token);
111 void get_identifier(std::string& identifier);
112
118 void push_back(std::string const& token, kind k) { token_ = token; kind_ = k; }
119
124 bool isalpha(char c) const { return ctype_.is(ctype_.alpha, c); }
129 bool isalnum(char c) const { return ctype_.is(ctype_.alnum, c); }
134 bool isdigit(char c) const { return ctype_.is(ctype_.digit, c); }
139 bool isprint(char c) const { return ctype_.is(ctype_.print, c); }
140
141 std::istream& input_;
142 std::ctype<char> const& ctype_;
143 std::string token_;
144 kind kind_;
145 };
146
152 void parse_loop(std::istream& input, std::ostream& output);
153
154 #endif
parser::slash
Definition: parse.hpp:37
parse_loop
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:272
parser::isprint
bool isprint(char c) const
Definition: parse.hpp:139
parser::times
Definition: parse.hpp:37
parser::get_expr
bool get_expr(node &result)
Definition: parse.cpp:143
parser::lparen
Definition: parse.hpp:37
parser::rparen
Definition: parse.hpp:37
parser::parser
parser(std::istream &input)
Definition: parse.cpp:6
parser::plus
Definition: parse.hpp:37
parse_error::parse_error
parse_error(std::string const &msg)
Definition: parse.hpp:15
parser::get_identifier
void get_identifier(std::string &identifier)
Definition: parse.cpp:37
parser::charify
std::string charify(char c)
Definition: parse.cpp:13
node
Definition: node.hpp:16
parser::push_back
void push_back(std::string const &token, kind k)
Definition: parse.hpp:118
parser
Definition: parse.hpp:30
parser::get_primary
bool get_primary(node &result)
Definition: parse.cpp:244
parser::get_unary
bool get_unary(node &result)
Definition: parse.cpp:222
parser::isalpha
bool isalpha(char c) const
Definition: parse.hpp:124
parser::get_token
kind get_token(std::string &token)
Definition: parse.cpp:56
parser::token_
std::string token_
One token push-back.
Definition: parse.hpp:143
parser::minus
Definition: parse.hpp:37
parse_error
Definition: parse.hpp:13
parser::number
Definition: parse.hpp:36
parser::equal
Definition: parse.hpp:37
parser::input_
std::istream & input_
Share the input stream.
Definition: parse.hpp:141
parser::kind_
kind kind_
The kind of token that was pushed back.
Definition: parse.hpp:144
parser::eof
Definition: parse.hpp:36
parser::kind
kind
Definition: parse.hpp:36
parser::get_number
bool get_number(std::string const &token, node &result)
Definition: parse.cpp:129
parser::ctype_
std::ctype< char > const & ctype_
Cache the ctype facet for checking character categories.
Definition: parse.hpp:142
parser::get_mul_expr
bool get_mul_expr(node &result)
Definition: parse.cpp:200
parser::get_add_expr
bool get_add_expr(node &result)
Definition: parse.cpp:178
parser::isalnum
bool isalnum(char c) const
Definition: parse.hpp:129
parser::identifier
Definition: parse.hpp:36
parser::isdigit
bool isdigit(char c) const
Definition: parse.hpp:134