Calculator Step 5

- 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
25 class parser
26 {
27 public:
31 enum kind : int { eof, identifier, number, string,
32 plus='+', minus='-', times='*', slash='/', lparen = '(', rparen=')', equal='=',
33 comma=','};
34
38 parser(std::istream& input);
39
54 bool get_statement(std::ostream& output);
55
56 private:
61 std::string charify(char c);
67 bool get_number(std::string const& token, node& result);
72 bool get_expr(node& result);
80 bool get_add_expr(node& result);
88 bool get_mul_expr(node& result);
99 bool get_primary(node& result);
107 bool get_unary(node& result);
120 void get_definition(std::string& name, identifier_list& parameters, node& definition);
132 kind get_token(std::string& token);
137 void get_identifier(std::string& identifier);
141 void get_expr_list(node_list& result);
142
151 template<class OutputIterator>
152 OutputIterator get_namelist(OutputIterator output);
157 void get_escape(std::string& str);
163 void get_string(std::string& result, char delimiter);
164
170 void push_back(std::string const& token, kind k);
171
176 bool isalpha(char c) const { return ctype_.is(ctype_.alpha, c); }
181 bool isalnum(char c) const { return ctype_.is(ctype_.alnum, c); }
186 bool isdigit(char c) const { return ctype_.is(ctype_.digit, c); }
191 bool isprint(char c) const { return ctype_.is(ctype_.print, c); }
192
193 std::istream& input_;
194 std::ctype<char> const& ctype_;
195 std::string token_;
196 kind kind_;
197 };
198
204 void parse_loop(std::istream& input, std::ostream& output);
205
206 template<class OutputIterator>
207 OutputIterator parser::get_namelist(OutputIterator output)
208 {
209 std::string token{};
210 while (kind k = get_token(token)) {
211 if (k == ')')
212 return output;
213 else if (k != identifier)
214 throw syntax_error{"expected function parameter, got " + token};
215 else {
216 *output = token;
217 ++output;
218
219 k = get_token(token);
220 if (k == ')')
221 return output;
222 if (k != ',')
223 throw syntax_error{"expected comma in function paramter list, got " + token};
224 }
225 }
226 throw syntax_error{"unexpected end of line in function parameter list"};
227 }
228
229
230 #endif
parser::slash
Definition: parse.hpp:32
parser::comma
Definition: parse.hpp:33
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:421
parser::isprint
bool isprint(char c) const
Definition: parse.hpp:191
parser::times
Definition: parse.hpp:32
parser::get_expr
bool get_expr(node &result)
Definition: parse.cpp:291
parser::lparen
Definition: parse.hpp:32
parser::rparen
Definition: parse.hpp:32
parser::parser
parser(std::istream &input)
Definition: parse.cpp:10
parser::plus
Definition: parse.hpp:32
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:26
parser::get_definition
void get_definition(std::string &name, identifier_list &parameters, node &definition)
Definition: parse.cpp:225
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:246
parser
Definition: parse.hpp:25
parser::get_primary
bool get_primary(node &result)
Definition: parse.cpp:376
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:19
parser::get_namelist
OutputIterator get_namelist(OutputIterator output)
Definition: parse.hpp:207
parser::get_unary
bool get_unary(node &result)
Definition: parse.cpp:334
parser::isalpha
bool isalpha(char c) const
Definition: parse.hpp:176
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:195
parser::minus
Definition: parse.hpp:32
parser::number
Definition: parse.hpp:31
parser::equal
Definition: parse.hpp:32
parser::input_
std::istream & input_
Share the input stream.
Definition: parse.hpp:193
parser::kind_
kind kind_
The kind of token that was pushed back.
Definition: parse.hpp:196
node.hpp
parser::eof
Definition: parse.hpp:31
parser::kind
kind
Definition: parse.hpp:31
node_list
std::vector< node > node_list
A sequence of nodes.
Definition: node.hpp:13
parser::get_number
bool get_number(std::string const &token, node &result)
Definition: parse.cpp:214
parser::ctype_
std::ctype< char > const & ctype_
Cache the ctype facet for checking character categories.
Definition: parse.hpp:194
parser::get_mul_expr
bool get_mul_expr(node &result)
Definition: parse.cpp:315
parser::get_add_expr
bool get_add_expr(node &result)
Definition: parse.cpp:296
parser::get_expr_list
void get_expr_list(node_list &result)
Definition: parse.cpp:355
parser::isalnum
bool isalnum(char c) const
Definition: parse.hpp:181
parser::string
Definition: parse.hpp:31
parser::get_escape
void get_escape(std::string &str)
Definition: parse.cpp:41
parser::identifier
Definition: parse.hpp:31
parser::isdigit
bool isdigit(char c) const
Definition: parse.hpp:186