8 ctype_(std::use_facet<std::ctype<char>>(input.getloc()))
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) <<
'\'';
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);
116 if (c !=
'+' and c !=
'-') {
122 throw parse_error{
"syntax error: unterminated expression. Expected a multiplicative-exprssion after " + std::string(c,1)};
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)
172 throw parse_error{
"syntax error: EOF when expecting ')'"};
189 for (output <<
"> "; std::getline(input, line); output <<
"> ") {
190 std::istringstream input{std::move(line)};
194 while (p.get_expr(x))
197 output << ex.what() <<
'\n';
198 }
catch(std::exception
const& ex) {
199 output <<
"exception: " << ex.what() <<
'\n';
bool get_primary(double &result)
void parse_loop(std::istream &input, std::ostream &output)
parser(std::istream &input)
std::string charify(char c)
bool get_number(double &result)
bool get_expr(double &result)
bool get_mult_expr(double &result)