#include <parse.hpp>
Parser class template.
Definition at line 17 of file parse.hpp.
parser::parser |
( |
std::istream & |
input | ) |
|
Constructor. Save the input
stream.
- Parameters
-
Definition at line 6 of file parse.cpp.
8 ctype_(std::use_facet<std::ctype<char>>(input.getloc()))
std::ctype< char > const & ctype_
std::string parser::charify |
( |
char |
c | ) |
|
|
private |
Convert a characer to a readable form.
- Parameters
-
- Returns
- A C++-style character literal that ensures
c
is readable.
Definition at line 11 of file parse.cpp.
Referenced by get_number(), and get_primary().
13 if (c ==
'\a')
return R
"('\a')";
14 if (c ==
'\b')
return R
"('\b')";
15 if (c ==
'\f')
return R
"('\f')";
16 if (c ==
'\n')
return R
"('\n')";
17 if (c ==
'\r')
return R
"('\r')";
18 if (c ==
'\t')
return R
"('\t')";
19 if (c ==
'\v')
return R
"('\v')";
20 if (c ==
'\'')
return R
"('\'')";
21 if (c ==
'\\')
return R
"('\\')";
24 return std::string{
"\'"} + std::string(1,c) +
"\'";
26 std::ostringstream stream{};
27 stream <<
"'\\x" << std::hex;
30 stream << (std::char_traits<char>::to_int_type(c) & 0xFF) <<
'\'';
bool parser::get_expr |
( |
double & |
result | ) |
|
Read one expression and store the result in result
.
- Parameters
-
result | Where to store the result of the expression. |
- Returns
- true to continue or false to end the loop
- Exceptions
-
Definition at line 110 of file parse.cpp.
References get_mult_expr(), and input_.
Referenced by get_primary().
116 if (c !=
'+' and c !=
'-') {
122 throw parse_error{
"syntax error: unterminated expression. Expected a multiplicative-exprssion after " + std::string(c,1)};
bool get_mult_expr(double &result)
bool parser::get_mult_expr |
( |
double & |
result | ) |
|
|
private |
Parse a multiplicative expression.
- Parameters
-
result | Store the number here |
- Returns
- true to continue parsing or false to stop (end of file or error)
Definition at line 135 of file parse.cpp.
References get_primary(), and input_.
Referenced by get_expr().
141 if (c !=
'*' and c !=
'/') {
147 throw parse_error{
"syntax error: unterminated expression. Expected a primary after " + std::string(c,1)};
150 else if (right == 0.0)
bool get_primary(double &result)
bool parser::get_number |
( |
double & |
result | ) |
|
|
private |
Parse a number.
- Parameters
-
result | Store the number here |
- Returns
- true to continue parsing or false to stop (end of file or error)
Definition at line 38 of file parse.cpp.
References charify(), and input_.
Referenced by get_primary().
44 if (c ==
'+' or c ==
'-') {
47 throw parse_error{
"unterminated number: expected a digit after the sign"};
49 if (c < '0' or c >
'9') {
53 while (c >=
'0' and c <=
'9') {
56 std::istringstream tmp{std::move(token)};
58 return (tmp >> result);
64 throw parse_error{
"unterminated number: expected digit after the decimal point"};
65 if (c < '0' or c >
'9') {
67 throw parse_error{
"syntax error: expected digit after decimal point, got " +
charify(c)};
69 while (c >=
'0' and c <=
'9') {
72 std::istringstream tmp{std::move(token)};
74 return (tmp >> result);
78 if (c ==
'e' or c ==
'E') {
81 throw parse_error{
"unterminated number: expected digit in the exponent"};
82 if (c ==
'-' or c ==
'+') {
85 throw parse_error{
"unterminated number: expected digit after sign in the exponent"};
87 if (c < '0' or c >
'9') {
91 while (c >=
'0' and c <=
'9') {
94 std::istringstream tmp{std::move(token)};
96 return (tmp >> result);
102 std::istringstream tmp{std::move(token)};
104 return (tmp >> result);
std::string charify(char c)
bool parser::get_primary |
( |
double & |
result | ) |
|
|
private |
Parse a primary expression. A primary is a parenthesized expression or a numeric literal.
- Parameters
-
result | Store the number here |
- Returns
- true to continue parsing or false to stop (end of file or error)
Definition at line 162 of file parse.cpp.
References charify(), get_expr(), get_number(), and input_.
Referenced by get_mult_expr().
172 throw parse_error{
"syntax error: EOF when expecting ')'"};
std::string charify(char c)
bool get_number(double &result)
bool get_expr(double &result)
std::ctype<char> const& parser::ctype_ |
|
private |
std::istream& parser::input_ |
|
private |
The documentation for this class was generated from the following files: