-
Notifications
You must be signed in to change notification settings - Fork 9
/
parser_combinators.hpp
1605 lines (1336 loc) · 48.2 KB
/
parser_combinators.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
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//============================================================================
// copyright 2012, 2013, 2014 Keean Schupke
// compile with -std=c++11
// parser_combinators.hpp
#ifndef PARSER_COMBINATORS_HPP
#define PARSER_COMBINATORS_HPP
#include <istream>
#include <sstream>
#include <stdexcept>
#include <vector>
#include <map>
#include <tuple>
#include <type_traits>
#include <memory>
#include <cassert>
#include <iterator>
#include <utility>
#include <type_traits>
#include "function_traits.hpp"
using namespace std;
//============================================================================
// Character Predicates
struct is_any {
using is_predicate_type = true_type;
static constexpr int rank = 0;
constexpr is_any() {};
bool operator() (int const c) const {
return c != EOF;
}
string name() const {
return "anything";
}
} constexpr is_any;
struct is_alnum {
using is_predicate_type = true_type;
static constexpr int rank = 0;
constexpr is_alnum() {}
bool operator() (int const c) const {
return ::isalnum(c) != 0;
}
string name() const {
return "alphanumeric";
}
} constexpr is_alnum;
struct is_alpha {
using is_predicate_type = true_type;
static constexpr int rank = 0;
constexpr is_alpha() {}
bool operator() (int const c) const {
return ::isalpha(c) != 0;
}
string name() const {
return "alphabetic";
}
} constexpr is_alpha;
struct is_blank {
using is_predicate_type = true_type;
static constexpr int rank = 0;
constexpr is_blank() {}
bool operator() (int const c) const {
return ::isblank(c) != 0;
}
string name() const {
return "blank";
}
} constexpr is_blank;
struct is_cntrl {
using is_predicate_type = true_type;
static constexpr int rank = 0;
constexpr is_cntrl() {}
bool operator() (int const c) const {
return ::iscntrl(c) != 0;
}
string name() const {
return "control";
}
} constexpr is_cntrl;
struct is_digit {
using is_predicate_type = true_type;
static constexpr int rank = 0;
constexpr is_digit() {}
bool operator() (int const c) const {
return ::isdigit(c) != 0;
}
string name() const {
return "digit";
}
} constexpr is_digit;
struct is_graph {
using is_predicate_type = true_type;
static constexpr int rank = 0;
constexpr is_graph() {}
bool operator() (int const c) const {
return ::isgraph(c) != 0;
}
string name() const {
return "graphic";
}
} constexpr is_graph;
struct is_lower {
using is_predicate_type = true_type;
static constexpr int rank = 0;
constexpr is_lower() {}
bool operator() (int const c) const {
return ::islower(c) != 0;
}
string name() const {
return "lowercase";
}
} constexpr is_lower;
struct is_print {
using is_predicate_type = true_type;
static constexpr int rank = 0;
constexpr is_print() {}
bool operator() (int const c) const {
return ::isprint(c) != 0;
}
string name() const {
return "printable";
}
} constexpr is_print;
struct is_punct {
using is_predicate_type = true_type;
static constexpr int rank = 0;
constexpr is_punct() {}
bool operator() (int const c) const {
return ::ispunct(c) != 0;
}
string name() const {
return "punctuation";
}
} constexpr is_punct;
struct is_space {
using is_predicate_type = true_type;
static constexpr int rank = 0;
constexpr is_space() {}
bool operator() (int const c) const {
return ::isspace(c) != 0;
}
string name() const {
return "space";
}
} constexpr is_space;
struct is_upper {
using is_predicate_type = true_type;
static constexpr int rank = 0;
constexpr is_upper() {}
bool operator() (int const c) const {
return ::isupper(c) != 0;
}
string name() const {
return "uppercase";
}
} constexpr is_upper;
struct is_xdigit {
using is_predicate_type = true_type;
static constexpr int rank = 0;
constexpr is_xdigit() {}
bool operator() (int const c) const {
return ::isxdigit(c) != 0;
}
string name() const {
return "hexdigit";
}
} constexpr is_xdigit;
struct is_eol {
using is_predicate_type = true_type;
static constexpr int rank = 0;
constexpr is_eol() {}
bool operator() (int const c) const {
return c == '\n';
}
string name() const {
return "EOL";
}
} constexpr is_eol;
//----------------------------------------------------------------------------
// Any single character
class is_char {
int const k;
public:
using is_predicate_type = true_type;
static constexpr int rank = 0;
constexpr explicit is_char(char const c)
: k(c) {}
bool operator() (int const c) const {
return k == c;
}
string name() const {
return "'" + string(1, k) + "'";
}
};
is_char constexpr is_eof(EOF);
//----------------------------------------------------------------------------
// Combining character predicates
template <typename P, typename = typename P::is_predicate_type>
string format_name(P const& p, int const rank) {
if (p.rank > rank) {
return "(" + p.name() + ")";
} else {
return p.name();
}
}
template <typename P1, typename P2> class is_either {
P1 const p1;
P2 const p2;
public:
using is_predicate_type = true_type;
static constexpr int rank = 1;
constexpr is_either(P1 const& p1, P2 const& p2)
: p1(p1), p2(p2) {}
bool operator() (int const c) const {
return p1(c) || p2(c);
}
string name() const {
return p1.name() + " | " + p2.name();
}
};
template <typename P1, typename P2,
typename = typename P1::is_predicate_type,
typename = typename P2::is_predicate_type>
constexpr is_either<P1, P2> const operator|| (P1 const& p1, P2 const& p2) {
return is_either<P1, P2>(p1, p2);
}
template <typename P1, typename P2> class is_except {
P1 const p1;
P2 const p2;
public:
using is_predicate_type = true_type;
static constexpr int rank = 0;
constexpr explicit is_except(P1 const& p1, P2 const& p2)
: p1(p1), p2(p2) {}
bool operator() (int const c) const {
return p1(c) && !p2(c);
}
string name() const {
return format_name(p1, rank) + " - " + format_name(p2, rank);
}
};
template <typename P1, typename P2,
typename = typename P1::is_predicate_type,
typename = typename P2::is_predicate_type>
constexpr is_except<P1, P2> const operator- (P1 const& p1, P2 const& p2) {
return is_except<P1, P2>(p1, p2);
}
//===========================================================================
// Default Inherited Attribute
struct default_inherited {};
//===========================================================================
// Parsing Errors
using unique_defs = map<string, string>;
struct parse_error : public runtime_error {
template <typename Parser, typename Iterator, typename Range>
static string message(string const& what, Parser const& p,
Iterator const &f, Iterator const &l, Range const &r
) {
stringstream err;
Iterator i(r.first);
Iterator line_start(r.first);
int row = 1;
while ((i != r.last) && (i != f)) {
if (*i == '\n') {
++row;
line_start = ++i;
} else {
++i;
}
}
err << what << " at line: " << row
<< " column: " << f - line_start + 1 << endl;
bool in = true;
for (Iterator i(line_start); (i != r.last) && (in || *i != '\n'); ++i) {
if (i == l) {
in = false;
}
if (is_space(*i)) {
err << ' ';
} else {
err << static_cast<char>(*i);
}
}
err << endl;
i = line_start;
while (i != f) {
err << ' ';
++i;
}
err << '^';
++i;
if (i != l) {
++i;
while (i != l) {
err << '-';
++i;
}
err << "^";
}
err << endl << "expecting: ";
unique_defs defs;
err << p.ebnf(&defs) << endl << "where:" << endl;
for (auto const& d : defs) {
err << "\t" << d.first << " = " << d.second << ";" << endl;
}
return err.str();
}
template <typename Parser, typename Iterator, typename Range>
parse_error(string const& what, Parser const& p,
Iterator const &f, Iterator const &l, Range const &r
) : runtime_error(message(what, p, f, l, r)) {}
};
//============================================================================
// Type Helpers
//----------------------------------------------------------------------------
// Make tuples of integer sequences from begining to end - 1 of range
template <size_t... Is> struct size_sequence {};
template <size_t Begin, size_t End, size_t... Is> struct range : range<Begin, End - 1, End - 1, Is...> {};
template <size_t Begin, size_t... Is> struct range<Begin, Begin, Is...> : size_sequence<Is...> {};
template <typename F, typename A, typename T, size_t I0, size_t... Is>
A fold_tuple2(F f, A a, T&& t, size_t, size_t...) {
return fold_tuple2<F, A, T, Is...>(f, f(a, get<I0>(t)), t, Is...);
}
template <typename F, typename A, typename T, size_t I0>
A fold_tuple2(F f, A a, T&& t, size_t) {
return f(a, get<I0>(t));
}
template <typename F, typename A, typename T, size_t... Is>
A fold_tuple1(F f, A a, T&& t, size_sequence<Is...>) {
return fold_tuple2<F, A, T, Is...>(f, a, t, Is...);
}
template <typename F, typename A, typename... Ts>
A fold_tuple(F f, A a, tuple<Ts...> const& t) {
return fold_tuple1(f, a, t, range<0, sizeof...(Ts)>());
}
//----------------------------------------------------------------------------
// Can a pointer to one type be implicitly converted into a pointer to the other.
template <typename A, typename B> struct is_compat {
using PA = typename add_pointer<A>::type;
using PB = typename add_pointer<B>::type;
static constexpr bool value = is_convertible<PA, PB>::value;
};
template <typename A, typename B> struct is_compatible {
static constexpr bool value = is_compat<A, B>::value || is_compat<B, A>::value;
};
//----------------------------------------------------------------------------
// Choose the result type of two parsers which a pointer the other result type
// can be conveted into a pointer to. For example given a parsers with result types
// void and int, the least general is int, because (void*) can be implicitely converted
// to (int*). If there is no possible implicit converstion result_type is undefined.
template <typename P1, typename P2, typename = void> struct least_general {};
template <typename P1, typename P2> struct least_general <P1, P2,
typename enable_if<is_compat<typename P2::result_type, typename P1::result_type>::value
&& !is_same<typename P1::result_type, typename P2::result_type>::value>::type> {
using result_type = typename P2::result_type;
};
template <typename P1, typename P2> struct least_general <P1, P2,
typename enable_if<is_compat<typename P1::result_type, typename P2::result_type>::value
|| is_same<typename P1::result_type, typename P2::result_type>::value>::type> {
using result_type = typename P1::result_type;
};
//----------------------------------------------------------------------------
// Concatenate multiple string template arguments into a single string.
string concat(string const& sep, string const& str) {
return str;
}
template <typename... Strs> string concat(string const& sep, string const& str, Strs const&... strs) {
string s = str;
int const unpack[] {0, (s += sep + strs, 0)...};
return s;
}
//----------------------------------------------------------------------------
// Use EBNF precedence for parser naming.
template <typename P, typename = typename P::is_parser_type>
string format_name(P const& p, int const rank, unique_defs* defs = nullptr) {
if (p.rank > rank) {
return "(" + p.ebnf(defs) + ")";
} else {
return p.ebnf(defs);
}
}
//============================================================================
// Primitive String Recognisers: accept, accept_str
//----------------------------------------------------------------------------
// Stream is advanced if symbol matches, and symbol is appended to result.
template <typename Predicate> class recogniser_accept {
Predicate const p;
public:
using is_parser_type = true_type;
using is_handle_type = false_type;
using has_side_effects = false_type;
using result_type = string;
int const rank;
constexpr explicit recogniser_accept(Predicate const& p) : p(p), rank(p.rank) {}
template <typename Iterator, typename Range, typename Inherit = default_inherited>
bool operator() (
Iterator &i,
Range const &r,
string *result = nullptr,
Inherit* st = nullptr
) const {
int sym;
if (i == r.last) {
sym = EOF;
} else {
sym = *i;
}
if (!p(sym)) {
return false;
}
++i;
if (result != nullptr) {
result->push_back(sym);
}
return true;
}
string ebnf(unique_defs* defs = nullptr) const {
return p.name();
}
};
template <typename P, typename = typename P::is_predicate_type>
constexpr recogniser_accept<P> accept(P const &p) {
return recogniser_accept<P>(p);
}
//-----------------------------------------------------------------------------
// String Parser.
class accept_str {
char const* s;
public:
using is_parser_type = true_type;
using is_handle_type = false_type;
using has_side_effects = false_type;
using result_type = string;
int const rank = 0;
constexpr explicit accept_str(char const* s) : s(s) {}
template <typename Iterator, typename Range, typename Inherit = default_inherited>
bool operator() (
Iterator &i,
Range const &r,
string *result = nullptr,
Inherit* st = nullptr
) const {
for (auto j = s; *j != 0; ++j) {
if (i == r.last || *i != *j) {
return false;
}
++i;
}
if (result != nullptr) {
result->append(s);
}
return true;
}
string ebnf(unique_defs* defs = nullptr) const {
return "\"" + string(s) + "\"";
}
};
//============================================================================
// Constant Parsers: succ, fail
//----------------------------------------------------------------------------
// Always succeeds.
struct parser_succ {
using is_parser_type = true_type;
using is_handle_type = false_type;
using has_side_effects = false_type;
using result_type = void;
int const rank = 0;
constexpr parser_succ() {}
template <typename Iterator, typename Range, typename Inherit = default_inherited>
bool operator() (
Iterator &i,
Range const &r,
void *result = nullptr,
Inherit* st = nullptr
) const {
return true;
}
string ebnf(unique_defs* defs = nullptr) const {
return "succ";
}
} constexpr succ;
//----------------------------------------------------------------------------
// Always fails.
struct parser_fail {
using is_parser_type = true_type;
using is_handle_type = false_type;
using has_side_effects = false_type;
using result_type = void;
int const rank = 0;
constexpr parser_fail() {}
template <typename Iterator, typename Range, typename Inherit = default_inherited>
bool operator() (
Iterator &i,
Range const &r,
void *result = nullptr,
Inherit* st = nullptr
) const {
return false;
}
string ebnf(unique_defs* defs = nullptr) const {
return "fail";
}
} constexpr fail;
//============================================================================
// Lifting String Recognisers to Parsers, and Parsers up one level: any, all
//----------------------------------------------------------------------------
// as soon as one parser succeeds, pass result to user supplied functor
template <typename Functor, typename Inherit, typename = void> class call_any {};
template <typename Functor, typename Inherit>
class call_any<Functor, Inherit, typename enable_if<is_same<Inherit, default_inherited>::value>::type> {
Functor const f;
public:
explicit call_any(Functor const& f) : f(f) {}
template <typename Result, typename Rs, size_t... I>
void any(Result* r, int j, Rs& rs, Inherit* st, size_t...) {
f(r, j, get<I>(rs)...);
}
};
template <typename Functor, typename Inherit>
class call_any<Functor, Inherit, typename enable_if<!is_same<Inherit, default_inherited>::value>::type> {
Functor const f;
public:
explicit call_any(Functor const& f) : f(f) {}
template <typename Result, typename Rs, size_t... I>
void any(Result* r, int j, Rs& rs, Inherit* st, size_t...) {
f(r, j, get<I>(rs)..., st);
}
};
template <typename Functor, typename... Parsers> class fmap_choice {
using functor_traits = function_traits<Functor>;
using tuple_type = tuple<Parsers...>;
using tmp_type = tuple<typename Parsers::result_type...>;
public:
using is_parser_type = true_type;
using is_handle_type = false_type;
using has_side_effects = true_type;
using result_type = typename remove_pointer<typename functor_traits::template argument<0>::type>::type;
private:
tuple_type const ps;
Functor const f;
template <typename Iterator, typename Range, typename Inherit, typename Rs, size_t I0, size_t... Is>
int any_parsers(Iterator &i, Range const &r, Inherit* st, Rs &rs, size_t, size_t...) const {
if (get<I0>(ps)(i, r, &get<I0>(rs), st)) {
return I0;
}
return any_parsers<Iterator, Range, Inherit, Rs, Is...>(i, r, st, rs, Is...);
}
template <typename Iterator, typename Range, typename Inherit, typename Rs, size_t I0>
int any_parsers(Iterator &i, Range const &r, Inherit* st, Rs &rs, size_t) const {
if (get<I0>(ps)(i, r, &get<I0>(rs), st)) {
return I0;
}
return -1;
}
template <typename Iterator, typename Range, typename Inherit, size_t... I> bool fmap_any(
Iterator &i,
Range const &r,
size_sequence<I...> seq,
result_type *result,
Inherit* st
) const {
tmp_type tmp {};
Iterator const first = i;
int const j = any_parsers<Iterator, Range, Inherit, tmp_type, I...>(i, r, st, tmp, I...);
if (j >= 0) {
if (result != nullptr) {
call_any<Functor, Inherit> call_f(f);
try {
call_f.template any<result_type, tmp_type, I...>(result, j, tmp, st, I...);
} catch (runtime_error &e) {
throw parse_error(e.what(), *this, first, i, r);
}
}
return true;
}
return false;
}
public:
int const rank = 1;
constexpr explicit fmap_choice(Functor const& f, Parsers const&... ps)
: f(f), ps(ps...) {}
template <typename Iterator, typename Range, typename Inherit>
bool operator() (
Iterator &i,
Range const &r,
result_type *result = nullptr,
Inherit* st = nullptr
) const {
return fmap_any(i, r, range<0, sizeof...(Parsers)>(), result, st);
}
class choice_ebnf {
int const rank;
unique_defs* defs;
public:
choice_ebnf(int r, unique_defs* d) : rank(r), defs(d) {}
template <typename P>
string operator() (string const& s, P&& p) const {
if (s.length() == 0) {
return format_name(p, rank, defs);
}
return s + " | " + format_name(p, rank, defs);
}
};
string ebnf(unique_defs* defs = nullptr) const {
return fold_tuple(choice_ebnf(rank, defs), string(), ps);
}
};
template <typename F, typename... PS>
constexpr fmap_choice<F, PS...> const any(F const& f, PS const&... ps) {
return fmap_choice<F, PS...>(f, ps...);
}
//----------------------------------------------------------------------------
// If all parsers succeed, pass all results as arguments to user supplied functor
template <typename Functor, typename Inherit, typename = void> class call_all {};
template <typename Functor, typename Inherit>
class call_all<Functor, Inherit, typename enable_if<is_same<Inherit, default_inherited>::value>::type> {
Functor const f;
public:
explicit call_all(Functor const& f) : f(f) {}
template <typename Result, typename Rs, size_t... I>
void all(Result* r, Rs& rs, Inherit* st, size_t...) {
f(r, get<I>(rs)...);
}
};
template <typename Functor, typename Inherit>
class call_all<Functor, Inherit, typename enable_if<!is_same<Inherit, default_inherited>::value>::type> {
Functor const f;
public:
explicit call_all(Functor const& f) : f(f) {}
template <typename Result, typename Rs, size_t... I>
void all(Result* r, Rs& rs, Inherit* st, size_t...) {
f(r, get<I>(rs)..., st);
}
};
template <typename Functor, typename... Parsers> class fmap_sequence {
using functor_traits = function_traits<Functor>;
using tuple_type = tuple<Parsers...>;
using tmp_type = tuple<typename Parsers::result_type...>;
public:
using is_parser_type = true_type;
using is_handle_type = false_type;
using has_side_effects = true_type;
using result_type = typename remove_pointer<typename functor_traits::template argument<0>::type>::type;
private:
tuple_type const ps;
Functor const f;
template <typename Iterator, typename Range, typename Inherit, typename Rs, size_t I0, size_t... Is>
bool all_parsers(Iterator &i, Range const &r, Inherit* st, Rs &rs, size_t, size_t...) const {
if (get<I0>(ps)(i, r, &get<I0>(rs), st)) {
return all_parsers<Iterator, Range, Inherit, Rs, Is...>(i, r, st, rs, Is...);
}
return false;
}
template <typename Iterator, typename Range, typename Inherit, typename Rs, size_t I0>
bool all_parsers(Iterator &i, Range const &r, Inherit* st, Rs &rs, size_t) const {
return get<I0>(ps)(i, r, &get<I0>(rs), st);
}
template <typename Iterator, typename Range, typename Inherit, size_t... I>
bool fmap_all(Iterator &i, Range const &r, size_sequence<I...> seq, result_type *result, Inherit* st) const {
tmp_type tmp {};
Iterator const first = i;
if (all_parsers<Iterator, Range, Inherit, tmp_type, I...>(i, r, st, tmp, I...)) {
if (result != nullptr) {
call_all<Functor, Inherit> call_f(f);
try {
call_f.template all<result_type, tmp_type, I...>(result, tmp, st, I...);
} catch (runtime_error &e) {
throw parse_error(e.what(), *this, first, i, r);
}
}
return true;
}
return false;
}
public:
int const rank = 0;
constexpr fmap_sequence(Functor const& f, Parsers const&... ps)
: f(f), ps(ps...) {}
template <typename Iterator, typename Range, typename Inherit = default_inherited>
bool operator() (
Iterator &i,
Range const &r,
result_type *result = nullptr,
Inherit* st = nullptr
) const {
return fmap_all(i, r, range<0, sizeof...(Parsers)>(), result, st);
}
class sequence_ebnf {
int const rank;
unique_defs* defs;
public:
sequence_ebnf(int r, unique_defs* d) : rank(r), defs(d) {}
template <typename P>
string operator() (string const &s, P&& p) const {
if (s.size() == 0) {
return format_name(p, rank, defs);
}
return s + ", " + format_name(p, rank, defs);
}
};
string ebnf(unique_defs* defs = nullptr) const {
if (tuple_size<tuple_type>::value == 1) {
return format_name(get<0>(ps), rank, defs);
} else {
return fold_tuple(sequence_ebnf(rank, defs), string(), ps);
}
}
};
template <typename F, typename... PS>
constexpr fmap_sequence<F, PS...> const all(F const& f, PS const&... ps) {
return fmap_sequence<F, PS...>(f, ps...);
}
//============================================================================
// Combinators For Both Parsers and Recognisers: ||, &&, many
//----------------------------------------------------------------------------
// Run the second parser only if the first fails.
template <typename Parser1, typename Parser2> class combinator_choice {
Parser1 const p1;
Parser2 const p2;
public:
using is_parser_type = true_type;
using is_handle_type = false_type;
using has_side_effects =
integral_constant<bool, Parser1::has_side_effects::value || Parser2::has_side_effects::value>;
using result_type = typename least_general<Parser1, Parser2>::result_type;
int const rank = 1;
constexpr combinator_choice(Parser1 const& p1, Parser2 const& p2) : p1(p1), p2(p2) {}
template <typename Iterator, typename Range, typename Inherit = default_inherited>
bool operator() (
Iterator &i,
Range const &r,
result_type *result = nullptr,
Inherit* st = nullptr
) const {
Iterator const first = i;
if (p1(i, r, result, st)) {
return true;
}
if (first != i) {
throw parse_error("failed parser consumed input", p1, first, i, r);
}
if (p2(i, r, result, st)) {
return true;
}
return false;
}
string ebnf(unique_defs* defs = nullptr) const {
return format_name(p1, rank, defs) + " | " + format_name(p2, rank, defs);
}
};
template <typename P1, typename P2,
typename = typename enable_if<is_same<typename P1::is_parser_type, true_type>::value
|| is_same<typename P1::is_handle_type, true_type>::value>::type,
typename = typename enable_if<is_same<typename P2::is_parser_type, true_type>::value
|| is_same<typename P2::is_handle_type, true_type>::value>::type,
typename = typename enable_if<is_compatible<typename P1::result_type, typename P2::result_type>::value, pair<typename P1::result_type, typename P2::result_type>>::type>
constexpr combinator_choice<P1, P2> const operator|| (P1 const& p1, P2 const& p2) {
return combinator_choice<P1, P2>(p1, p2);
}
//----------------------------------------------------------------------------
// Run the second parser only if the first succeeds.
template <typename Parser1, typename Parser2> class combinator_sequence {
Parser1 const p1;
Parser2 const p2;
public:
using is_parser_type = true_type;
using is_handle_type = false_type;
using has_side_effects =
integral_constant<bool, Parser1::has_side_effects::value || Parser2::has_side_effects::value>;
using result_type = typename least_general<Parser1, Parser2>::result_type;
int const rank = 0;
constexpr combinator_sequence(Parser1 const& p1, Parser2 const& p2) : p1(p1), p2(p2) {}
template <typename Iterator, typename Range, typename Inherit = default_inherited>
bool operator() (
Iterator &i,
Range const &r,
result_type *result = nullptr,
Inherit* st = nullptr
) const {
return p1(i, r, result, st) && p2(i, r, result, st);
}
string ebnf(unique_defs* defs = nullptr) const {
return format_name(p1, rank, defs) + ", " + format_name(p2, rank, defs);
}
};
template <typename P1, typename P2,
typename = typename enable_if<is_same<typename P1::is_parser_type, true_type>::value
|| is_same<typename P1::is_handle_type, true_type>::value>::type,
typename = typename enable_if<is_same<typename P2::is_parser_type, true_type>::value
|| is_same<typename P2::is_handle_type, true_type>::value>::type,
typename = typename enable_if<is_compatible<typename P1::result_type, typename P2::result_type>::value>::type>
constexpr combinator_sequence<P1, P2> operator&& (P1 const& p1, P2 const& p2) {
return combinator_sequence<P1, P2>(p1, p2);
}
//----------------------------------------------------------------------------
// Accept the parser zero or more times.
template <typename Parser> class combinator_many {
Parser const p;
public:
using is_parser_type = true_type;
using is_handle_type = false_type;
using has_side_effects = typename Parser::has_side_effects;
using result_type = typename Parser::result_type;
int const rank = 0;
constexpr explicit combinator_many(Parser const& p) : p(p) {}
template <typename Iterator, typename Range, typename Inherit = default_inherited>
bool operator() (
Iterator &i,
Range const &r,
result_type *result = nullptr,
Inherit* st = nullptr
) const {
Iterator first = i;
while (p(i, r, result, st)) {
first = i;
}
if (first != i) {
throw parse_error("failed many-parser consumed input", p, first, i, r);
}
return true;
}
string ebnf(unique_defs* defs = nullptr) const {
return "{" + p.ebnf(defs) + "}";
}
};
template <typename P, typename = typename enable_if<is_same<typename P::is_parser_type, true_type>::value
|| is_same<typename P::is_handle_type, true_type>::value>::type>
constexpr combinator_many<P> const many(P const& p) {
return combinator_many<P>(p);
}
//----------------------------------------------------------------------------
// Exception parser
template <typename Parser> class combinator_except {
Parser const p;
char const* x;
public:
using is_parser_type = true_type;
using is_handle_type = false_type;
using has_side_effects = typename Parser::has_side_effects;
using result_type = typename Parser::result_type;
int const rank = 0;
constexpr combinator_except(char const* x, Parser const& p) : p(p), x(x) {}