-
Notifications
You must be signed in to change notification settings - Fork 0
/
infix_t.hpp
278 lines (252 loc) · 4.98 KB
/
infix_t.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#ifndef __infix_t_hpp__
#define __infix_t_hpp__
#include <string>
#include <vector>
#include <map>
#include <tuple>
#include <functional>
#include "debug.hpp"
#include "postfix_t.hpp"
/**
* An infix expression parser.
*/
struct infix_t
{
/**
* @param[in] p the computation engine
*/
infix_t(postfix_t& p)
:p_(p)
{
}
/**
* @param[in] s the infix expression candidate
* @return a tuple whose first member is an iterator
* revealing where the parse ended. On a successful
* parse, this value will be equal to s.cend(). When the
* parse is not successful, the second member is a
* description of how the parse failed.
*/
std::tuple
<
std::string::const_iterator,
const char*
>
parse(const std::string& s)
{
const char* response_message = "";
i_ = begin_ = s.cbegin();
end_ = s.cend();
try
{
descend(0);
}
catch (const char* e)
{
response_message = e;
}
return std::make_tuple(i_, response_message);
}
private:
enum types_t
{
infix,
prefix,
postfix
};
struct level_t
{
std::vector<std::string> glyphs;
types_t type;
};
std::vector<level_t> levels_ =
{
{{"<<", ">>"}, types_t::infix},
{{"+", "-"}, types_t::infix},
{{"*", "/", "%"}, types_t::infix},
{{"^"}, types_t::infix},
{{"~", "-"}, types_t::prefix},
{{"!"}, types_t::postfix},
};
std::string consume_one_of(
const std::vector<std::string>& glyphs
)
{
for (auto g : glyphs)
{
int len = g.size();
if (len <= end_ - i_)
{
std::string candidate(i_, i_ + len);
if (candidate == g)
{
i_ += len;
return g;
}
}
}
return "";
}
void do_infix(std::size_t level)
{
advance();
descend(level + 1);
std::string g;
while
(
advance()
&&
!(g = consume_one_of(levels_[level].glyphs)).empty()
)
{
if (g.empty())
{
throw "missing infix operator";
}
descend(level + 1);
p_.push(g);
}
}
void do_prefix(std::size_t level)
{
advance();
auto g = consume_one_of(levels_[level].glyphs);
descend(level + 1);
if (!g.empty())
{
if (g == "-")
{
std::string whole_thing(begin_, end_);
boost::trim(whole_thing);
if (whole_thing == "-")
{
p_.push("-");
}
else
{
p_.push("neg");
}
}
else
{
p_.push(g);
}
}
}
void do_postfix(std::size_t level)
{
advance();
descend(level + 1);
auto g = consume_one_of(levels_[level].glyphs);
if (!g.empty())
{
p_.push(g);
}
}
void do_terminal()
{
advance();
if (*i_ == '(')
{
++i_;
descend(0);
advance();
if (i_ < end_)
{
ensure(*i_ == ')');
++i_;
}
}
else
{
auto saved = i_;
while (i_ < end_ && is_terminal_glyph())
{
++i_;
}
if (saved < i_)
{
std::string terminal(saved, i_);
p_.push(terminal);
}
}
}
void descend(std::size_t level)
{
if (levels_.size() <= level)
{
do_terminal();
return;
}
switch (levels_[level].type)
{
case types_t::infix:
do_infix(level);
break;
case types_t::prefix:
do_prefix(level);
break;
case types_t::postfix:
do_postfix(level);
break;
default:
throw "invalid type";
}
}
bool advance()
{
while (is_whitespace() && i_ < end_)
{
++i_;
}
bool result = i_ != end_;
return result;
}
bool is_point() const
{
bool result = *i_ == '.';
return result;
}
bool is_comma() const
{
bool result = *i_ == ',';
return result;
}
bool is_decimal_digit() const
{
bool result =
'0' <= *i_ && *i_ <= '9';
return result;
}
bool is_terminal_glyph() const
{
bool result =
is_decimal_digit()
||
is_point()
||
is_comma()
||
is_alpha()
;
return result;
}
bool is_alpha() const
{
bool result =
('A' <= *i_ && *i_ <= 'Z')
||
('a' <= *i_ && *i_ <= 'z');
return result;
}
bool is_whitespace() const
{
bool result =*i_ <= ' ' || *i_ > '~';
return result;
}
postfix_t& p_;
std::string::const_iterator begin_;
std::string::const_iterator i_;
std::string::const_iterator end_;
};
#endif