8 ctype_(std::use_facet<std::ctype<char> >(input.getloc())),
15 if (c ==
'\a')
return R
"('\a')";
16 if (c ==
'\b')
return R
"('\b')";
17 if (c ==
'\f')
return R
"('\f')";
18 if (c ==
'\n')
return R
"('\n')";
19 if (c ==
'\r')
return R
"('\r')";
20 if (c ==
'\t')
return R
"('\t')";
21 if (c ==
'\v')
return R
"('\v')";
22 if (c ==
'\'')
return R
"('\'')";
23 if (c ==
'\\')
return R
"('\\')";
26 return std::string{
"\'"} + std::string(1,c) +
"\'";
28 std::ostringstream stream{};
29 stream <<
"'\\x" << std::hex;
32 stream << (std::char_traits<char>::to_int_type(c) & 0xFF) <<
'\'';
78 if (c ==
'+' or c ==
'-' or c ==
'*' or c ==
'/' or c ==
'%' or c ==
'(' or c ==
')' or c ==
'=') {
80 return static_cast<kind>(c);
83 if (c < '0' or c >
'9') {
87 while (c >=
'0' and c <=
'9') {
95 throw parse_error{
"unterminated number: expected digit after the decimal point"};
96 if (c < '0' or c >
'9') {
98 throw parse_error{
"syntax error: expected digit after decimal point, got " +
charify(c)};
100 while (c >=
'0' and c <=
'9') {
106 if (c ==
'e' or c ==
'E') {
109 throw parse_error{
"unterminated number: expected digit in the exponent"};
110 if (c ==
'-' or c ==
'+') {
113 throw parse_error{
"unterminated number: expected digit after sign in the exponent"};
115 if (c < '0' or c >
'9') {
119 while (c >=
'0' and c <=
'9') {
131 std::istringstream stream{token};
134 if (not (stream >> value))
136 result =
node(value);
155 throw parse_error{
"syntax error: expected IDENTIFIER, but got " + name};
158 throw parse_error{
"syntax error: expected =, but got " + token};
160 throw parse_error{
"syntax error: expected additive-exprssion in assignment"};
161 result =
node(
node{std::move(name)}, result);
170 throw parse_error{
"syntax error: expected an additive-expression"};
184 if (k !=
'+' and k !=
'-') {
190 throw parse_error{
"syntax error: unterminated expression. Expected a multiplicative-expression after " + token};
191 result =
node(result, k, right);
206 if (k !=
'*' and k !=
'/') {
212 throw parse_error{
"syntax error: unterminated expression. Expected a unary-expression after " + token};
213 result =
node(result, k, right);
229 result =
node(k, result);
231 }
else if (k ==
'+') {
253 throw parse_error{
"syntax error: EOF when expecting ')'"};
255 throw parse_error{
"syntax error: expected ')', but got " + token};
260 throw parse_error{
"Invalid numeric literal: " + token};
263 result =
node{std::move(token)};
266 throw parse_error{
"syntax error: expected a primary, but got " + token};
278 for (output <<
"> "; std::getline(input, line); output <<
"> ") {
279 std::istringstream input{std::move(line)};
283 while (p.get_expr(n))
286 output << ex.what() <<
'\n';
287 }
catch(std::exception
const& ex) {
288 output <<
"exception: " << ex.what() <<
'\n';
void parse_loop(std::istream &input, std::ostream &output)
bool isprint(char c) const
bool get_expr(node &result)
parser(std::istream &input)
void get_identifier(std::string &identifier)
std::string charify(char c)
void push_back(std::string const &token, kind k)
bool get_primary(node &result)
bool get_unary(node &result)
bool isalpha(char c) const
kind get_token(std::string &token)
std::string token_
One token push-back.
std::istream & input_
Share the input stream.
kind kind_
The kind of token that was pushed back.
bool get_number(std::string const &token, node &result)
bool get_mul_expr(node &result)
bool get_add_expr(node &result)
bool isalnum(char c) const