-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParserCombinators.hpp
More file actions
390 lines (315 loc) · 10.6 KB
/
ParserCombinators.hpp
File metadata and controls
390 lines (315 loc) · 10.6 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#ifndef PARSERCOMBINATORS_HPP
#define PARSERCOMBINATORS_HPP
#include <iostream>
#include <string>
#include <boost/regex.hpp>
#include <boost/foreach.hpp>
#include <boost/optional.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <algorithm>
#include <functional>
#include <cassert>
#include <utility>
#include <deque>
namespace ParserCombinators {
using std::deque;
using std::pair;
using std::string;
using std::vector;
using boost::regex;
using boost::optional;
using boost::shared_ptr;
using boost::weak_ptr;
class ParserFailure {
string msg;
public:
virtual ~ParserFailure() {}
ParserFailure(const string& msg): msg(msg) {}
ParserFailure(): msg("Unknown error") {}
string get_msg() const { return msg; }
};
class MatchFailure: public ParserFailure {
public:
MatchFailure(const string& msg): ParserFailure(msg) {}
};
class EndOfBufferFailure: public ParserFailure {
public:
EndOfBufferFailure(const string& msg): ParserFailure(msg) {}
};
template <typename Iterator>
void end_of_buffer_p(Iterator* begin, Iterator end) {
if (*begin == end) throw EndOfBufferFailure("Unexpected end of buffer");
}
typedef shared_ptr<vector<string> > StringVecPtr;
template <typename Iterator, typename Res>
class Parser {
public:
typedef shared_ptr<Parser<Iterator, Res> > Ptr;
virtual ~Parser() {}
virtual Res parse(Iterator* begin, Iterator end) const = 0;
};
template <typename Iterator>
class RegexParser: public Parser<Iterator, StringVecPtr> {
string str;
regex our_regex;
public:
typedef StringVecPtr result_type;
typedef typename Parser<Iterator, StringVecPtr>::Ptr Ptr;
static Ptr Instantiate(const string& regex) {
return Ptr(new RegexParser<Iterator>(regex));
}
RegexParser(const string& regex): str(regex), our_regex(boost::regex(regex)) {}
StringVecPtr parse(Iterator* begin, Iterator end) const {
boost::match_results<Iterator> m;
if (!regex_search(*begin, end, m, our_regex, boost::regex_constants::match_continuous)) {
throw MatchFailure("Regex: " + str);
}
StringVecPtr res = StringVecPtr(new vector<string>(m.size()));
for (size_t i = 0; i < m.size(); ++i) {
(*res)[i] = m.str(i);
}
advance(*begin, m.length());
return res;
}
};
using boost::enable_shared_from_this;
template <typename ParserType>
class ParserList: public enable_shared_from_this<ParserList<ParserType> > {
public:
typedef shared_ptr<ParserType> ParserTypePtr;
typedef shared_ptr<ParserList<ParserType> > Ptr;
private:
ParserTypePtr parser;
Ptr next;
public:
ParserList(ParserTypePtr parser): parser(parser), next() {}
ParserList(ParserTypePtr parser, Ptr pl): parser(parser), next(pl) {}
Ptr get_shared() {
return enable_shared_from_this<ParserList<ParserType> >::shared_from_this();
}
Ptr get_next() const { return next; }
ParserTypePtr get_parser() const { return parser; }
Ptr cons(ParserTypePtr pt) {
return Ptr(new ParserList<ParserType>(pt, get_shared()));
}
Ptr reverse() const {
Ptr lst = Ptr(new ParserList<ParserType>(parser));
Ptr next = this->next;
while(next) {
lst = lst->cons(next->get_parser());
next = next->get_next();
}
return lst;
}
};
template <typename Iterator, typename Res>
class ParseOr: public Parser<Iterator, Res> {
public:
typedef shared_ptr< ParseOr<Iterator, Res> > Ptr;
typedef Parser<Iterator, Res> OurParser;
typedef shared_ptr<OurParser> OurParserPtr;
typedef typename ParserList<OurParser>::Ptr ParserListPtr;
static Ptr Instantiate() {
return Ptr(new ParseOr<Iterator, Res>());
}
private:
mutable ParserListPtr reverse_list;
ParserListPtr parser_list;
ParseOr(ParserListPtr parser_list): parser_list(parser_list) {}
ParserListPtr get_reverse_list() const {
if (!parser_list) return parser_list;
if (!reverse_list) {
reverse_list = parser_list->reverse();
}
return reverse_list;
}
public:
ParseOr(): reverse_list(), parser_list() {}
ParseOr(OurParserPtr parser): reverse_list(), parser_list(new ParserList<OurParser>(parser)) {}
Res parse(Iterator *begin, Iterator end) const {
Iterator cached_begin = *begin;
ParserListPtr ptr = get_reverse_list();
while(ptr) {
try {
return ptr->get_parser()->parse(begin, end);
} catch (MatchFailure& m) {
*begin = cached_begin;
ptr = ptr->get_next();
}
}
throw MatchFailure("ParseOr failed");
}
Ptr add_or(OurParserPtr parser) const {
if (parser_list)
return Ptr(new ParseOr<Iterator, Res>(parser_list->cons(parser)));
return Ptr(new ParseOr<Iterator, Res>(parser));
}
};
template <typename Iterator, typename Res, typename InterRes = void>
class InterspersedParser: public Parser<Iterator, shared_ptr<deque<Res> > > {
public:
typedef shared_ptr<deque<Res> > result_type;
private:
typename Parser<Iterator, Res>::Ptr parser;
typename Parser<Iterator, InterRes>::Ptr inter_parser;
public:
InterspersedParser(typename Parser<Iterator, Res>::Ptr parser,
typename Parser<Iterator, InterRes>::Ptr inter_parser):
parser(parser), inter_parser(inter_parser) {}
result_type parse(Iterator* begin, Iterator end) const {
result_type output(new deque<Res>());
Iterator cached_begin = *begin;
try {
output->push_back(parser->parse(begin, end));
} catch (MatchFailure& f) {
*begin = cached_begin;
return output;
}
while (*begin != end) {
Iterator cached_begin = *begin;
try {
inter_parser->parse(begin, end);
output->push_back(parser->parse(begin, end));
} catch (MatchFailure& m) {
*begin = cached_begin;
return output;
}
};
return output;
}
};
template <typename T>
struct VecPtr {
typedef shared_ptr<vector<T> > type;
};
template <typename Iterator, typename Res, typename InterRes = void>
class InterspersedParser1: public Parser<Iterator,
typename InterspersedParser<Iterator, Res, InterRes>::result_type> {
InterspersedParser<Iterator, Res, InterRes> parser;
public:
typedef typename InterspersedParser<Iterator, Res, InterRes>::result_type result_type;
InterspersedParser1(typename Parser<Iterator, Res>::Ptr parser,
typename Parser<Iterator, InterRes>::Ptr inter_parser):
parser(parser, inter_parser) {}
result_type parse(Iterator* begin, Iterator end) const {
Iterator cached_begin = *begin;
result_type res(parser.parse(begin, end));
if (res->size() == 0) {
throw MatchFailure("Failed to match InterspersedParser1");
} else {
return res;
}
}
};
template <typename Iterator, typename Res, typename Start = void, typename End = void>
class DelimitedExpressionParser: public Parser<Iterator, Res> {
typename Parser<Iterator, Start>::Ptr start_parser;
typename Parser<Iterator, End>::Ptr end_parser;
typename Parser<Iterator, Res>::Ptr parser;
private:
typedef typename Parser<Iterator, Res>::Ptr Ptr;
public:
static Ptr Instantiate(typename Parser<Iterator, Start>::Ptr start_parser,
typename Parser<Iterator, End>::Ptr end_parser,
typename Parser<Iterator, Res>::Ptr parser) {
return Ptr(new DelimitedExpressionParser<Iterator, Res, Start, End>(start_parser, end_parser, parser));
}
DelimitedExpressionParser(typename Parser<Iterator, Start>::Ptr start_parser,
typename Parser<Iterator, End>::Ptr end_parser,
typename Parser<Iterator, Res>::Ptr parser):
start_parser(start_parser),
end_parser(end_parser),
parser(parser) {}
Res parse(Iterator* begin, Iterator end) const {
start_parser->parse(begin, end);
Res r(parser->parse(begin, end));
end_parser->parse(begin, end);
return r;
}
};
template <typename Iterator, typename Res>
class RecursiveParser: public Parser<Iterator, Res> {
weak_ptr<Parser<Iterator, Res> > parser;
public:
typedef typename Parser<Iterator, Res>::Ptr Ptr;
static Ptr Instantiate(Ptr p) {
return Ptr(new RecursiveParser<Iterator, Res>(p));
}
RecursiveParser(Ptr p): parser(p) {}
Res parse(Iterator* begin, Iterator end) const {
Ptr p(parser.lock());
assert(p);
return p->parse(begin, end);
}
};
template <typename Iterator>
class WhitespaceParser: public Parser<Iterator, void> {
public:
static typename Parser<Iterator, void>::Ptr Instantiate() {
return typename Parser<Iterator, void>::Ptr(new WhitespaceParser<Iterator>());
}
private:
RegexParser<Iterator> regexp;
public:
WhitespaceParser(): regexp("\\s*") {}
void parse(Iterator* begin, Iterator end) const {
regexp.parse(begin, end);
}
};
template <typename Iterator, typename Res, typename Res1 = void>
class ComposeParser: public Parser<Iterator, Res> {
typedef typename Parser<Iterator, Res1>::Ptr first_parser_type;
typedef typename Parser<Iterator, Res>::Ptr second_parser_type;
first_parser_type first_parser;
second_parser_type second_parser;
public:
typedef typename Parser<Iterator, Res>::Ptr Ptr;
static Ptr Instantiate(first_parser_type first_parser, second_parser_type second_parser) {
return Ptr(new ComposeParser<Iterator, Res>(first_parser, second_parser));
}
ComposeParser(first_parser_type first_parser, second_parser_type second_parser):
first_parser(first_parser), second_parser(second_parser) {}
Res parse(Iterator* begin, Iterator end) const {
first_parser->parse(begin, end);
return second_parser->parse(begin, end);
}
};
template <typename Iterator>
class ParseConstant: public Parser<Iterator, void> {
string str;
public:
typedef typename Parser<Iterator, void>::Ptr Ptr;
static Ptr Instantiate(const string& str) {
return Ptr(new ParseConstant<Iterator>(str));
}
ParseConstant(const string& str): str(str) {}
void parse(Iterator* begin, Iterator end) const {
if (distance(*begin, end) < distance(str.begin(), str.end()) ||
!equal(str.begin(), str.end(), *begin)) {
throw MatchFailure("Expected: " + str);
}
advance(*begin, distance(str.begin(), str.end()));
}
};
template <typename Iterator, typename Res>
class ConstantParser: public Parser<Iterator, Res> {
Res ret;
public:
typedef typename Parser<Iterator, Res>::Ptr Ptr;
static Ptr Instantiate(const Res& ret) {
return Ptr(new ConstantParser<Iterator, Res>(ret));
}
ConstantParser(const Res& ret): ret(ret) {}
Res parse(Iterator*, Iterator) const {
return ret;
}
};
template <typename Iterator, typename Res, typename Res1>
typename Parser<Iterator, Res>::Ptr operator>>(shared_ptr<Parser<Iterator, Res1> > first,
shared_ptr<Parser<Iterator, Res> > second) {
return ComposeParser<Iterator, Res, Res1>::Instantiate(first, second);
}
}
#endif