-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.hpp
1231 lines (1010 loc) · 32 KB
/
rules.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 (C) 2015 Jiří Fatka <[email protected]> */
/* */
/* This program is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU Lesser General Public License as published */
/* by the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/* ************************************************************************ */
#pragma once
/* ************************************************************************ */
/**
* @file rules.hpp
*
* This header contains multiple structures that can be used to match input
* within iterator range. Those rules can be aggregate into more complex
* rules that are equivalent to regular expressions (for characters) or
* EBNF rules (for grammar).
*
* Rules are constructed in compile-time and they do not have any runtime
* footprint. All rules' functions must be called statically.
*
* @todo Test for out-of-range in `val` and `range`.
* @todo Allow to matching not from input begin (i.e. remove implicit ^...)
*/
/* ************************************************************************ */
// C++
#include <iterator>
#include <type_traits>
#include <tuple>
#include <utility>
#include <functional>
/* ************************************************************************ */
namespace template_regex {
namespace rules {
/* ************************************************************************ */
/**
* @brief Sequence of integers.
*
* @tparam I A list of integers.
*
* @todo Replace with std::integer_sequence in C++14
*/
template<int... I>
struct int_seq { };
/* ************************************************************************ */
/**
* @brief Sequence number range generator. The generated range is [S, E - 1].
*
* @tparam S Starting number.
* @tparam E Number after last generated.
* @tparam I... Helper list for recursive generating numbers.
*
* @see make_int_seq
*
* The idea is based on link below.
*
* @link http://stackoverflow.com/questions/7858817/unpacking-a-tuple-to-call-a-matching-function-pointer/7858971#7858971
*/
template<int S, int E, int... I>
struct sequence_generator
{
static_assert(S <= E, "S <= E !!!");
/// Generated `int_seq` type.
using type = typename std::conditional<
S >= E,
// This is required for prevent instantization of the second type
// that causes recursive instantizations
std::conditional<true, int_seq<I...>, void>,
sequence_generator<S, E - 1, E - 1, I...>
>::type::type;
};
/* ************************************************************************ */
// Just some tests
static_assert(std::is_same<typename sequence_generator<0, 0>::type, int_seq<>>::value, "Fail [0, 0]");
static_assert(std::is_same<typename sequence_generator<0, 1>::type, int_seq<0>>::value, "Fail [0, 1]");
static_assert(std::is_same<typename sequence_generator<5, 10>::type, int_seq<5, 6, 7, 8, 9>>::value, "Fail [5, 10]");
/* ************************************************************************ */
/**
* @brief Alias type for generating numbers range.
*
* @tparam S Starting number.
* @tparam E Number after last generated.
*/
template<int S, int E>
using make_int_seq = typename sequence_generator<S, E>::type;
/* ************************************************************************ */
/**
* @brief Base class for all rules.
*
* @tparam Rule Matching rule or derived class.
*/
template<typename Rule>
struct matcher
{
/**
* @brief Match the input range with Rule.
*
* @tparam Iterator Type of the input iterator. Requires InputIterator.
* @tparam Outputs A list of output iterators used by `capture`.
*
* @param it An iterator to the first value.
* @param end An iterator to the value following the last valid
* value.
* @param out An optional list of output iterators.
*
* @return If input was matched.
*/
template<typename Iterator, typename... Outputs>
static bool match(Iterator it, const Iterator end, Outputs... outs)
{
static_assert(Rule::output_count == sizeof...(Outputs),
"Outputs count must match with output_count");
return match_ref(it, end, outs...);
}
/**
* @brief Match the input range with Rule.
*
* @tparam Iterator Type of the input iterator. Requires InputIterator.
* @tparam Outputs A list of output iterators used by `capture`.
*
* @param it An iterator to the first value.
* @param end An iterator to the value following the last valid
* value.
* @param out An optional list of output iterators.
*
* @return If input was matched.
*/
template<typename Range, typename... Outputs>
static bool match(Range&& r, Outputs... outs)
{
auto it = std::begin(r);
return match_ref(it, std::end(r), outs...);
}
/**
* @brief Match the input range with Rule.
*
* @tparam Iterator Type of the input iterator. Requires InputIterator.
* @tparam Outputs A list of output iterators used by `capture`.
*
* @param it A reference to starting iterator. At the end this iterator
* refers to the last unmatched value or is equivalent to the end
* iterator.
* @param end An iterator to the value following the last valid
* value.
* @param out An optional list of output iterators.
*
* @return If input was matched.
*/
template<typename Iterator, typename... Outputs>
static bool match_ref(Iterator& it, const Iterator end, Outputs... outs)
{
static_assert(Rule::output_count == sizeof...(Outputs),
"Outputs count must match with output_count");
return Rule::match_impl(it, end, outs...);
}
/**
* @brief Match the whole input range with Rule.
*
* @tparam Iterator Type of the input iterator. Requires InputIterator.
* @tparam Outputs A list of output iterators used by `capture`.
*
* @param it An iterator to the first value.
* @param end An iterator to the value following the last valid
* value.
* @param out An optional list of output iterators.
*
* @return If the whole input was matched.
*/
template<typename Iterator, typename... Outputs>
static bool match_all(Iterator it, const Iterator end, Outputs... outs)
{
return match_all_ref(it, end, outs...);
}
/**
* @brief Match the whole input range with Rule.
*
* @tparam Iterator Type of the input iterator. Requires InputIterator.
* @tparam Outputs A list of output iterators used by `capture`.
*
* @param it An iterator to the first value.
* @param end An iterator to the value following the last valid
* value.
* @param out An optional list of output iterators.
*
* @return If the whole input was matched.
*/
template<typename Range, typename... Outputs>
static bool match_all(Range&& r, Outputs... outs)
{
auto it = std::begin(r);
return match_all_ref(it, std::end(r), outs...);
}
/**
* @brief Match the whole input range with Rule.
*
* @tparam Iterator Type of the input iterator. Requires InputIterator.
* @tparam Output A list of output iterators used by `capture`.
*
* @param it A reference to starting iterator. At the end this iterator
* refers to the last unmatched value or is equivalent to the end
* iterator.
* @param end An iterator to the value following the last valid
* value.
* @param out An optional list of output iterators.
*
* @return If the whole input was matched.
*/
template<typename Iterator, typename... Outputs>
static bool match_all_ref(Iterator& it, const Iterator end, Outputs... outs)
{
static_assert(Rule::output_count == sizeof...(Outputs),
"Outputs count must match with output_count");
return Rule::match_impl(it, end, outs...) && (it == end);
}
};
/* ************************************************************************ */
/**
* @brief List of rules.
*/
template<typename Rule, typename... Rules>
struct list
{
template<typename Out, typename Iterator>
static Out match(Iterator& it, const Iterator end, Out def)
{
if (Rule::match(it, end))
{
return static_cast<Out>(Rule::value);
}
return list<Rules...>::match(it, end, def);
}
};
/* ************************************************************************ */
/**
* @brief List of rules.
*/
template<typename Rule>
struct list<Rule>
{
template<typename Out, typename Iterator>
static Out match(Iterator& it, const Iterator end, Out def)
{
if (Rule::match(it, end))
{
return static_cast<Out>(Rule::value);
}
return def;
}
};
/* ************************************************************************ */
/**
* @brief Rule list item.
*
* @param Rule Matching rule.
* @param Value Return value.
*/
template<typename Rule, int Value>
struct item
{
/// Return value.
static constexpr int value = Value;
template<typename Iterator>
static bool match(Iterator& it, const Iterator end)
{
return Rule::match(it, end);
}
};
/* ************************************************************************ */
template<typename InputIterator, typename OutputIterator>
struct capture_iterator
{
using difference_type = typename std::iterator_traits<InputIterator>::difference_type;
using value_type = typename std::iterator_traits<InputIterator>::value_type;
using pointer = typename std::iterator_traits<InputIterator>::pointer;
using reference = typename std::iterator_traits<InputIterator>::reference;
using iterator_category = typename std::iterator_traits<InputIterator>::iterator_category;
capture_iterator(InputIterator& in, OutputIterator out)
: in_(in), out_(out), ref_(in)
{}
capture_iterator(const capture_iterator& rhs)
: in_(rhs.ref_), out_(rhs.out_), ref_(in_)
{}
capture_iterator operator=(const capture_iterator& rhs)
{
in_ = rhs.ref_;
out_ = rhs.out_;
ref_ = in_;
}
capture_iterator(capture_iterator&& rhs)
: in_(std::move(rhs.ref_)), out_(std::move(rhs.out_)), ref_(std::move(in_))
{}
capture_iterator operator=(capture_iterator&& rhs)
{
in_ = std::move(rhs.ref_);
out_ = std::move(rhs.out_);
ref_ = std::move(in_);
}
bool operator==(const capture_iterator& rhs) const
{
return ref_ == rhs.ref_;
}
bool operator!=(const capture_iterator& rhs) const
{
return ref_ != rhs.ref_;
}
reference operator*() const
{
return ref_.operator*();
}
pointer operator->() const
{
return ref_.operator->();
}
capture_iterator& operator++()
{
*out_ = *ref_;
++ref_;
++out_;
return *this;
}
InputIterator in_;
OutputIterator out_;
InputIterator& ref_;
};
/* ************************************************************************ */
/**
* @brief Captures content matched by given rule.
*
* Regular Expression: "(Rule)"
*/
template<typename Rule>
struct capture : matcher<capture<Rule>>
{
static const unsigned output_count = Rule::output_count + 1;
template<typename Iterator, typename OutputIterator, typename... Output>
static bool match_impl(Iterator& it, Iterator end, OutputIterator out_it, Output... out)
{
capture_iterator<Iterator, OutputIterator> cit{it, out_it};
const capture_iterator<Iterator, OutputIterator> cend{end, out_it};
return Rule::match_ref(cit, cend, out...);
}
};
/* ************************************************************************ */
/**
* @brief Usefull struct for rule specialization (recursive stop rule).
*/
template<typename Rule>
struct forward
{
/// A number of outputs in the rule.
static const unsigned output_count = Rule::output_count;
template<typename Iterator, typename... Output>
static bool match_impl(Iterator& it, const Iterator end, Output... out)
{
return Rule::match_impl(it, end, out...);
}
};
/* ************************************************************************ */
template<typename Parent>
struct value_matcher : matcher<Parent>
{
/// A number of outputs in the rule.
static const unsigned output_count = 0;
template<typename Iterator, typename... Output>
static bool match_impl(Iterator& it, const Iterator end, Output... out)
{
return Parent::is(*it) ? (++it, true) : false;
}
};
/* ************************************************************************ */
/**
* @brief Value matching.
*
* @tparam Value Matched integer value constant.
*/
template<int Value>
struct val : value_matcher<val<Value>>
{
/// Stored value.
static const int value = Value;
/**
* @brief Check if given value match class rule.
*
* @tparam Val Value type.
*
* @param val Tested value.
* @param pos Tested value position - must be 0.
*/
template<typename Val>
static constexpr bool is(Val value)
{
return static_cast<int>(value) == Value;
}
};
/* ************************************************************************ */
/**
* @brief Value matching not.
*
* @param Value
*/
template<int Value>
struct val_not : value_matcher<val_not<Value>>
{
/**
* @brief Check if given value match class rule.
*
* @tparam Val Value type.
*
* @param val Tested value.
*/
template<typename Val>
static constexpr bool is(Val value)
{
return static_cast<int>(value) != Value;
}
};
/* ************************************************************************ */
/**
* @brief Match value in range: "{Low}-{High}"
*
* @param Low Lower value.
* @param High High value.
*/
template<int Low, int High>
struct range : value_matcher<range<Low, High>>
{
/**
* @brief Check if given value match class rule.
*
* @tparam Val Value type.
*
* @param val Tested value.
*/
template<typename Val>
static constexpr bool is(Val value)
{
return
(static_cast<int>(value) >= Low) &&
(static_cast<int>(value) <= High)
;
}
};
/* ************************************************************************ */
/**
* @brief Rule that does nothing.
*/
struct null_rule : matcher<null_rule>
{
/// A number of outputs in the rule.
static const unsigned output_count = 0;
template<typename Iterator, typename... Output>
static bool match_impl(Iterator& it, const Iterator end, Output... out)
{
return true;
}
};
/* ************************************************************************ */
/**
* @brief Match any value.
*/
struct any : value_matcher<any>
{
/**
* @brief Check if given value match class rule.
*
* @tparam Val Value type.
*
* @param val Tested value.
*/
template<typename Val>
static constexpr bool is(Val value)
{
return true;
}
};
/* ************************************************************************ */
/**
* @brief Selects from multiple values {Rule1}|{Rule2}|{Rules...}
*
* @param Rule1 First rule.
* @param Rules Next rules.
*/
template<typename Rule, typename... Rules>
struct alternative : matcher<alternative<Rule, Rules...>>
{
/// A number of outputs in the rule.
static const unsigned output_count = Rule::output_count;
template<typename Iterator, typename... Output>
static bool match_impl(Iterator& it, const Iterator end, Output... out)
{
return
Rule::match_impl(it, end, out...) ||
alternative<Rules...>::match_impl(it, end, out...)
;
}
};
/* ************************************************************************ */
/**
* @brief Stop specialization for alternative.
*
* @tparam Rule
*/
template<typename Rule>
struct alternative<Rule> : forward<Rule>, matcher<alternative<Rule>> { };
/* ************************************************************************ */
/**
* @brief Selects from multiple values {Rule1}|{Rule2}|{Rules...}
*
* @param Rule1 First rule.
* @param Rules Next rules.
*/
template<typename Rule, typename... Rules>
struct alternative_not : matcher<alternative_not<Rule, Rules...>>
{
/// A number of outputs in the rule.
static const unsigned output_count = Rule::output_count;
template<typename Iterator, typename... Output>
static bool match_impl(Iterator& it, const Iterator end, Output... out)
{
static_assert(!std::is_same<
typename std::iterator_traits<Iterator>::iterator_category,
std::input_iterator_tag
>::value, "alternative_not rule require forward_iterator at least");
// Store current position for ability to return
Iterator tmp = it;
// Rule matched, so result is: not matched.
if (Rule::match_impl(tmp, end, out...))
return false;
return alternative_not<Rules...>::match_impl(it, end, out...);
}
};
/* ************************************************************************ */
/**
* @brief Stop specialization for alternative.
*
* @tparam Rule
*/
template<typename Rule>
struct alternative_not<Rule> : matcher<alternative_not<Rule>>
{
/// A number of outputs in the rule.
static const unsigned output_count = Rule::output_count;
template<typename Iterator, typename... Output>
static bool match_impl(Iterator& it, const Iterator end, Output... out)
{
static_assert(!std::is_same<
typename std::iterator_traits<Iterator>::iterator_category,
std::input_iterator_tag
>::value, "alternative_not rule require forward_iterator at least");
// Store current position for ability to return
Iterator tmp = it;
// Use temporary iterator
return Rule::match_impl(tmp, end, out...) ? false : (++it, true);
}
};
/* ************************************************************************ */
/**
* @brief Optional repeat - none or more.
*
* - Regular expressions: {Rule}*
* - EBNF: { <Rule> }
*
* @param Rule Base rule
*/
template<typename Rule>
struct repeat_optional : matcher<repeat_optional<Rule>>
{
/// A number of outputs in the rule.
static const unsigned output_count = Rule::output_count;
template<typename Iterator, typename... Output>
static bool match_impl(Iterator& it, const Iterator end, Output... out)
{
while (Rule::match_ref(it, end, out...))
continue;
return true;
}
};
/* ************************************************************************ */
/**
* @brief Repeat rule - one or more.
*
* - Regular expressions: {Rule}+
* - EBNF: <Rule> { <Rule> }
*
* @param Rule Base rule
*/
template<typename Rule>
struct repeat : matcher<repeat<Rule>>
{
/// A number of outputs in the rule.
static const unsigned output_count = Rule::output_count;
template<typename Iterator, typename... Output>
static bool match_impl(Iterator& it, const Iterator end, Output... out)
{
unsigned int count = 0;
while ((it != end) && Rule::match_impl(it, end, out...))
++count;
return (count != 0);
}
};
/* ************************************************************************ */
/**
* @brief Optional rule: {Rule}?
*
* @param Rule Base rule
*/
template<typename Rule>
struct optional : matcher<optional<Rule>>
{
/// A number of outputs in the rule.
static const unsigned output_count = Rule::output_count;
template<typename Iterator, typename... Output>
static bool match_impl(Iterator& it, const Iterator end, Output... out)
{
Rule::match_ref(it, end, out...);
return true;
}
};
/* ************************************************************************ */
template<typename... Rules>
struct output_counter { };
/* ************************************************************************ */
template<typename Rule, typename... Rules>
struct output_counter<Rule, Rules...>
{
/// A number of outputs in the rule.
static const unsigned value = Rule::output_count + output_counter<Rules...>::value;
};
/* ************************************************************************ */
template<typename Rule>
struct output_counter<Rule>
{
static const unsigned value = Rule::output_count;
};
/* ************************************************************************ */
template<typename... Rules>
struct sequence;
/* ************************************************************************ */
/**
* @brief Match sequence of rules: {Rule1}{Rule2}{Rules...}
*
* @param Rule1 First rule.
* @param Rules Next rules.
*/
template<typename Rule, typename... Rules>
struct sequence<Rule, Rules...> : matcher<sequence<Rule, Rules...>>
{
/// A number of outputs in the rule.
static const unsigned output_count = output_counter<Rule, Rules...>::value;
template<typename Iterator, typename... Outputs>
static bool match_impl(Iterator& it, const Iterator end, Outputs... outs)
{
// Split output by numbers of outputs
// Make a tuple
auto outputs = std::tuple<Outputs...>{outs...};
return
apply<Rule>(it, end, outputs, make_int_seq<0, Rule::output_count>()) &&
apply<sequence<Rules...>>(it, end, outputs, make_int_seq<Rule::output_count, sizeof...(Outputs)>())
// Rule::match_impl(it, end, outs...) &&
// sequence<Rules...>::match_impl(it, end, outs...)
;
}
template<typename IRule, typename Iterator, typename Tuple, int... I>
static auto apply(Iterator& it, const Iterator end, Tuple&& args, int_seq<I...>)
-> decltype(IRule::match_impl(it, end, std::get<I>(std::forward<Tuple>(args))...))
{
return IRule::match_impl(it, end, std::get<I>(std::forward<Tuple>(args))...);
}
};
/* ************************************************************************ */
/**
* @brief Match sequence of rules: {Rule1}{Rule2}
*
* @param Rule2 Second rule.
*/
template<typename Rule>
struct sequence<Rule> : forward<Rule>, matcher<sequence<Rule>> { };
/* ************************************************************************ */
/**
* @brief Structure used for merging `sequence`.
*
* @tparam Seq...
*/
template<typename... Seq>
struct sequence_merge;
/* ************************************************************************ */
/**
* @brief Structure used for merging `sequence`.
*
* @tparam Seq1...
* @tparam Seq2...
*/
template<typename... Seq1, typename... Seq2>
struct sequence_merge<sequence<Seq1...>, sequence<Seq2...>>
{
using type = sequence<Seq1..., Seq2...>;
};
/* ************************************************************************ */
/**
* @brief Structure used for merging `sequence`.
*
* @tparam Seq...
*/
template<typename... Seq1>
struct sequence_merge<void, sequence<Seq1...>>
{
using type = sequence<Seq1...>;
};
/* ************************************************************************ */
/**
* @brief Structure used for merging `sequence`.
*
* @tparam Seq...
*/
template<typename... Seq1>
struct sequence_merge<sequence<Seq1...>, void>
{
using type = sequence<Seq1...>;
};
/* ************************************************************************ */
/**
* @brief Structure used for merging `sequence`.
*
* @tparam Rule
* @tparam Seq...
*/
template<typename Rule, typename... Seq1>
struct sequence_merge<Rule, sequence<Seq1...>>
{
using type = sequence<Rule, Seq1...>;
};
/* ************************************************************************ */
/**
* @brief Structure used for merging `sequence`.
*
* @tparam Rule
* @tparam Seq...
*/
template<typename Rule, typename... Seq1>
struct sequence_merge<sequence<Seq1...>, Rule>
{
using type = sequence<Seq1..., Rule>;
};
/* ************************************************************************ */
/**
* @brief Removes sequences that contains only one rule.
*
* @param Seq Sequence.
*/
template<typename Seq>
struct sequence_remove
{
using type = Seq;
};
/* ************************************************************************ */
/**
* @brief Removes sequences that contains only one rule.
*
* @param Seq Sequence.
*/
template<typename Rule>
struct sequence_remove<sequence<Rule>>
{
using type = Rule;
};
/* ************************************************************************ */
/**
* @brief Removes `sequence` from alternative list.
*
* @param Rules A list of rules.
*/
template<typename... Rules>
struct sequence_remove<alternative<sequence<Rules...>>>
{
using type = alternative<Rules...>;
};
/* ************************************************************************ */
/**
* @brief Removes sequence from alternative list.
*
* @param Rules A list of rules.
*/
template<typename... Rules>
struct sequence_remove<alternative_not<sequence<Rules...>>>
{
using type = alternative_not<Rules...>;
};
/* ************************************************************************ */
/**
* @brief Special type for ignoring store rules.
*/
struct null_variable { };
/* ************************************************************************ */
/**
* @brief Tests if T is variable type.
*
* @tparam T Tested type.
*/
template<typename T>
struct is_variable : std::false_type {};
/* ************************************************************************ */
/**
* @brief Specialization for std::reference_wrapper.
*
* @tparam T Tested type.
*/
template<typename T>
struct is_variable<std::reference_wrapper<T>> : std::true_type {};
static_assert(is_variable<std::reference_wrapper<int>>::value, "Nop");