Calculator Step 6

- 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 <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:
32 enum kind : int { eof, identifier, integer, floating_point, string,
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_;
205 kind kind_;
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
parser::slash
Definition: parse.hpp:33
parser::comma
Definition: parse.hpp:34
parser::get_string
void get_string(std::string &result, char delimiter)
Definition: parse.cpp:86
parse_loop
void parse_loop(std::istream &input, std::ostream &output)
Definition: parse.cpp:444
parser::isprint
bool isprint(char c) const
Definition: parse.hpp:200
parser::times
Definition: parse.hpp:33
parser::get_expr
bool get_expr(node &result)
Definition: parse.cpp:307
parser::lparen
Definition: parse.hpp:33
parser::rparen
Definition: parse.hpp:33
parser::parser
parser(std::istream &input)
Definition: parse.cpp:10
parser::plus
Definition: parse.hpp:33
parser::get_identifier
void get_identifier(std::string &identifier)
Definition: parse.cpp:102
parser::charify
std::string charify(char c)
Definition: parse.cpp:17
node
Definition: node.hpp:28
parser::get_definition
void get_definition(std::string &name, identifier_list &parameters, node &definition)
Definition: parse.cpp:241
calc_error.hpp
parser::push_back
void push_back(std::string const &token, kind k)
Definition: parse.cpp:121
parser::get_statement
bool get_statement(std::ostream &output)
Definition: parse.cpp:262
parser::get_integer
bool get_integer(std::string const &token, node &result)
Definition: parse.cpp:219
parser::integer
Definition: parse.hpp:32
parser
Definition: parse.hpp:26
parser::get_primary
bool get_primary(node &result)
Definition: parse.cpp:392
syntax_error
Definition: calc_error.hpp:24
identifier_list
std::vector< std::string > identifier_list
A sequence of identifiers (e.g., parameter names).
Definition: node.hpp:21
parser::get_namelist
OutputIterator get_namelist(OutputIterator output)
Definition: parse.hpp:216
parser::get_unary
bool get_unary(node &result)
Definition: parse.cpp:350
parser::isalpha
bool isalpha(char c) const
Definition: parse.hpp:185
parser::get_token
kind get_token(std::string &token)
Definition: parse.cpp:130
parser::token_
std::string token_
One token push-back.
Definition: parse.hpp:204
parser::minus
Definition: parse.hpp:33
parser::get_float
bool get_float(std::string const &token, node &result)
Definition: parse.cpp:230
parser::floating_point
Definition: parse.hpp:32
parser::equal
Definition: parse.hpp:33
parser::input_
std::istream & input_
Share the input stream.
Definition: parse.hpp:202
parser::kind_
kind kind_
The kind of token that was pushed back.
Definition: parse.hpp:205
node.hpp
parser::eof
Definition: parse.hpp:32
number.hpp
parser::kind
kind
Definition: parse.hpp:32
node_list
std::vector< node > node_list
A sequence of nodes.
Definition: node.hpp:15
parser::ctype_
std::ctype< char > const & ctype_
Cache the ctype facet for checking character categories.
Definition: parse.hpp:203
parser::get_mul_expr
bool get_mul_expr(node &result)
Definition: parse.cpp:331
parser::get_add_expr
bool get_add_expr(node &result)
Definition: parse.cpp:312
parser::get_expr_list
void get_expr_list(node_list &result)
Definition: parse.cpp:371
parser::isalnum
bool isalnum(char c) const
Definition: parse.hpp:190
parser::string
Definition: parse.hpp:32
parser::get_escape
void get_escape(std::string &str)
Definition: parse.cpp:41
parser::identifier
Definition: parse.hpp:32
parser::isdigit
bool isdigit(char c) const
Definition: parse.hpp:195