-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.hpp
More file actions
604 lines (457 loc) · 13.9 KB
/
parse.hpp
File metadata and controls
604 lines (457 loc) · 13.9 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#ifndef HELPER_PARSER_HPP
#define HELPER_PARSER_HPP
#include <deque>
#include <array>
#include <type_traits>
#include <iosfwd>
#include <functional>
#include <cassert>
#include "maybe.hpp"
#include <sstream>
// a small parser combinator library, loosely based on "Monadic Parser
// Combinators" by Hutton & Meijer.
// each parser call is either:
// 1. successful and the stream has the failbit **not** set
// 2. unsuccessful and the stream state (pos/rdstate) must be restored as if
// before the call. use the stream_state RAII for this.
namespace parser {
namespace {
template<class Parser>
using result_type = typename detail::require_maybe<
typename std::result_of<Parser(std::istream&)>::type
>::type;
template<class Parser>
using value_type = typename result_type<Parser>::value_type;
// RAII for saving/restoring/discarding stream state
class stream_state {
std::istream* stream;
public:
const std::ios::iostate state;
const std::ios::pos_type pos;
inline stream_state(std::istream& stream)
: stream(&stream),
state(stream.rdstate()),
pos(stream.good() ? stream.tellg() : std::ios::pos_type() ){
}
inline ~stream_state() {
if(!stream) return;
stream->clear(state);
if(stream->good()) {
stream->seekg(pos);
}
}
inline stream_state(stream_state&& other)
: stream(other.stream),
state(other.state),
pos(other.pos) {
other.stream = nullptr;
}
inline void discard() { stream = nullptr; }
};
////////////////////////////////////////////////////////////////////////////////
// parser combinators
// type erasure
template<class T>
using any = std::function< maybe<T> (std::istream& ) >;
// reference
template<class Parser>
struct ref_type {
const Parser& parser;
result_type<Parser> operator()(std::istream& in) const {
return parser(in);
}
};
template<class Parser>
static ref_type<Parser> ref(const Parser& parser) { return {parser}; }
// monadic bind (parser sequencing with binding)
template<class Parser, class Func>
struct bind_type {
const Parser parser;
const Func func;
using func_result_type = typename std::result_of<Func(value_type<Parser>)>::type;
result_type<func_result_type> operator()(std::istream& in) const {
stream_state backup(in);
assert(!in.fail());
if(auto value = parser(in)) {
if(auto res = func(std::move(value.get()))(in)) {
backup.discard();
return std::move(res.get());
}
}
return {};
}
};
template<class Parser, class Func, class = value_type<Parser> >
static bind_type<Parser, Func> operator>>(const Parser& parser, const Func& func) {
return {parser, func};
}
// monadic pure (lift value as a parser)
template<class T>
struct pure_type {
const T value;
maybe<T> operator()(std::istream& ) const { return value; }
};
template<class T>
static pure_type< typename std::decay<T>::type> pure(T&& value = {}) {
return {std::forward<T>(value)};
}
// monadic zero
template<class T>
struct fail {
maybe<T> operator()(std::istream& ) const { return {}; }
};
// functor map
template<class Parser, class Func>
struct map_type {
const Parser parser;
const Func func;
using type = typename std::result_of<Func(value_type<Parser>)>::type;
maybe<type> operator()(std::istream& in) const {
return map(parser(in), func);
}
};
template<class Parser, class Func>
static map_type<Parser, Func> map(const Parser& parser, const Func& func) {
return {parser, func};
}
// sequencing without binding
template<class Parser>
struct then_type {
const Parser parser;
template<class T>
Parser operator()(const T&) const { return parser; }
};
template<class Parser>
static then_type<Parser> then(const Parser& parser) { return {parser}; }
template<class LHS, class RHS>
struct sequence_type {
using impl_type = bind_type<LHS, then_type<RHS>>;
const impl_type impl;
sequence_type(const LHS& lhs, const RHS& rhs)
: impl( lhs >> then(rhs) ) {
}
result_type<RHS> operator()(std::istream& in) const {
return impl(in);
}
};
template<class LHS, class RHS, class = value_type<LHS>, class = value_type<RHS>>
static sequence_type<LHS, RHS> operator>>=(const LHS& lhs, const RHS& rhs) {
return {lhs, rhs};
}
// sequencing with result drop
template<class Parser>
struct drop_type {
const Parser parser;
template<class T>
sequence_type<Parser, pure_type<typename std::decay<T>::type>>
operator()(T&& value) const {
return parser >>= pure(std::forward<T>(value));
}
};
template<class Parser>
static drop_type<Parser> drop(const Parser& parser) { return {parser}; }
// sequencing with result cast
template<class U>
struct cast {
template<class T>
pure_type<U> operator()(T&& value) const {
return pure<U>(std::forward<T>(value));
}
};
// parser either LHS or RHS (usual short-circuit applies)
// TODO use coproduct type for values and relax check
template<class LHS, class RHS>
struct coproduct_type {
const LHS lhs;
const RHS rhs;
static_assert(std::is_same< result_type<LHS>, result_type<RHS> >::value,
"parser value types must agree");
result_type<LHS> operator()(std::istream& in) const {
stream_state backup(in);
if(auto res = lhs(in)) {
backup.discard();
return std::move(res.get());
} else if(auto res = rhs(in)) {
backup.discard();
return std::move(res.get());
} else {
return {};
}
}
};
template<class LHS, class RHS, class = value_type<LHS>, class = value_type<RHS>>
static coproduct_type<LHS, RHS> operator|(const LHS& lhs, const RHS& rhs) {
return {lhs, rhs};
}
// kleene star
template<class Parser>
struct kleene_type {
const Parser parser;
using type = std::deque< value_type<Parser> >;
maybe<type> operator()(std::istream& in) const {
type res;
while(auto value = parser(in)) {
res.emplace_back(std::move(value.get()));
}
return res;
}
};
template<class Parser, class = value_type<Parser> >
static kleene_type<Parser> operator*(const Parser& parser) {
return {parser};
}
// (non-empty) kleene star
template<class Parser>
struct plus_type {
const Parser parser;
using type = std::deque< value_type<Parser> >;
maybe<type> operator()(std::istream& in) const {
const auto impl = ref(parser) >> [&](value_type<Parser>&& first) {
return *ref(parser) >> [&](std::deque<value_type<Parser>>&& rest) {
rest.emplace_front(std::move(first));
return pure(rest);
};
};
return impl(in);
}
};
template<class Parser, class = value_type<Parser> >
static plus_type<Parser> operator+(const Parser& parser) {
return {parser};
}
// repetition
template<std::size_t N, class Parser>
struct repeat_type {
const Parser parser;
using type = std::array<value_type<Parser>, N>;
maybe<type> operator()(std::istream& in) const {
stream_state backup(in);
type res;
for(std::size_t i = 0; i < N; ++i) {
if(auto ith = parser(in)) {
res[i] = std::move(ith.get());
} else {
return {};
}
}
return res;
}
};
template<std::size_t N, class Parser>
static repeat_type<N, Parser> repeat(const Parser& parser) { return {parser}; }
template<class T, class Exception = std::runtime_error>
struct error : Exception {
using Exception::Exception;
maybe<T> operator()(std::istream&) const {
throw *this;
}
};
// *non-empty* list with separator
template<class Parser, class Separator>
struct list {
const Parser parser;
const Separator separator;
using type = std::deque<value_type<Parser>>;
maybe<type> operator()(std::istream& in) const {
const auto impl = ref(parser)
>> [&](value_type<Parser>&& first) {
return *(ref(separator) >> then(ref(parser)))
>> [&](type&& rest) {
rest.emplace_front(std::move(first));
return pure(std::move(rest));
};
};
return impl(in);
}
};
template<class Parser, class Separator,
class = value_type<Parser>, class = value_type<Separator>>
static list<Parser, Separator> operator%(const Parser& parser,
const Separator& separator) {
return {parser, separator};
};
struct stream_format {
std::istream* in;
const std::istream::fmtflags fmt;
inline stream_format(std::istream& in)
: in(&in), fmt(in.flags()) { }
inline ~stream_format() {
if(in) in->flags(fmt);
}
};
// unformatted parsing
template<class Parser>
struct noskip_type {
const Parser parser;
result_type<Parser> operator()(std::istream& in) const {
const stream_format backup(in);
return parser(in >> std::noskipws);
}
};
template<class Parser>
static noskip_type<Parser> noskip(const Parser& parser) {
return {parser};
}
////////////////////////////////////////////////////////////////////////////////
// actual parsers
// formatted for first char, unformatted for the rest
struct token {
inline token(const char* value) : expected(value) { }
const char* const expected;
inline maybe<const char*> operator()(std::istream& in) const {
stream_state backup(in);
const auto fmt = in.flags();
// first read is formatted
// in >> std::skipws;
for(const char* c = expected; *c; ++c) {
char i = 0;
if(!(in >> i) || i != *c) {
in.flags(fmt); // restore flags
return {};
}
// next reads are unformatted
if(c == expected) in >> std::noskipws;
}
backup.discard();
in.flags(fmt); // restore flags
return expected;
}
};
// parse a value using standard stream input
template<class T>
struct value {
maybe<T> operator()(std::istream& in) const {
stream_state backup(in);
T res;
if(in >> res) {
backup.discard();
return res;
} else {
return {};
}
}
};
// parse true if the next read would yield eof
struct eof {
inline maybe<bool> operator()(std::istream& in) const {
stream_state backup(in);
char c;
if( !(in >> c) && in.eof() ) return true;
return {};
}
};
// optional parser. value type must be default constructible in case parse
// failed
template<class Parser>
struct option_type {
const Parser parser;
result_type<Parser> operator()(std::istream& in) const {
if(auto res = parser(in)) {
return res;
}
return {{}};
}
};
template<class Parser, class = result_type<Parser>>
static option_type<Parser> operator~(const Parser& parser) { return {parser}; }
// character parser from predicate. warning: parsing is **formatted** unless
// explicitely disabled with 'noskip'
template<int (*f) (int)>
struct chr {
maybe<char> operator()(std::istream& in) const {
stream_state backup(in);
char c;
if((in >> c) && f(c)) {
backup.discard();
return c;
}
return {};
};
};
// TODO character range/set/predicates
template<class Parser>
struct debug_type {
const Parser parser;
const char* const trace;
result_type<Parser> operator()(std::istream& in) const;
};
namespace {
struct debug {
const char* trace;
inline debug(const char* trace) : trace(trace) { }
template<class Parser>
inline debug_type<Parser> operator|=(const Parser& parser) const {
return {parser, trace};
}
static std::ostream* stream;
static std::size_t depth;
};
std::ostream* debug::stream = nullptr;
std::size_t debug::depth = 0;
}
static std::string istream_slice(std::istream& in,
std::istream::streampos first,
std::istream::streampos last) {
if(first == -1) return {};
in.seekg(first);
if(last == -1) {
std::stringstream ss;
in >> ss.rdbuf();
return ss.str();
}
const std::size_t size = last - first;
std::string buffer;
buffer.resize(size);
in.read(&buffer[0], size);
return buffer;
}
template<class Parser>
inline result_type<Parser> debug_type<Parser>::operator()(std::istream& in) const {
const std::size_t depth = debug::depth++;
const std::istream::streampos prev = in.eof() ?
std::istream::streampos(-1) : in.tellg();
const auto indent = [depth] () -> std::ostream& {
assert(debug::stream);
for(std::size_t i = 0; i < depth; ++i) {
*debug::stream << " ";
}
return *debug::stream;
};
if(debug::stream) {
indent() << ">> " << trace << std::endl;
}
assert(!in.fail());
auto res = parser(in);
assert(!in.fail());
if(debug::stream) {
const stream_state backup(in);
if(res) {
const std::istream::streampos curr = in.eof() ?
std::istream::streampos(-1) : in.tellg();
indent() << "<< " << trace
<< " success \"" << istream_slice(in, prev, curr)
<< "\"" << std::endl;
} else {
std::string next;
in >> next;
indent() << "<< " << trace
<< " failed on \"" << next << "\"" << std::endl;
}
}
--debug::depth;
return res;
}
// adaptor to standard c++ parsing api
template<class Parser>
static std::istream& parse(value_type<Parser>& self, const Parser& parser,
std::istream& in) {
if(auto res = parser(in)) {
self = std::move(res.get());
} else {
in.setstate(std::ios::failbit);
}
return in;
}
}
}
#endif