-
Notifications
You must be signed in to change notification settings - Fork 0
/
messer.cpp
2992 lines (2923 loc) · 131 KB
/
messer.cpp
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
#include<type_traits>
#include<utility>
#include<iterator>
#include<string>
#include<string_view>
#include<optional>
#include<variant>
#include<filesystem>
#include<fstream>
#include<veiler/hastur.hpp>
#include<veiler/lampads.hpp>
#include<veiler/pegasus.hpp>
#include<boost/preprocessor/cat.hpp>
#include<boost/preprocessor/seq/for_each.hpp>
#include<boost/range/adaptor/indexed.hpp>
#include<boost/range/adaptor/reversed.hpp>
#include<boost/coroutine2/all.hpp>
namespace messer{
template<typename Iterator>
class output_range{
Iterator be, en;
public:
output_range() = default;
output_range(const output_range&)noexcept = default;
output_range(output_range&&)noexcept = default;
output_range& operator=(const output_range&)noexcept = default;
output_range& operator=(output_range&&)noexcept = default;
template<typename I, typename J>
output_range(I&& b, J&& e):be(std::forward<I>(b)), en(std::forward<J>(e)){}
const Iterator& begin()const{return be;}
const Iterator& end ()const{return en;}
friend std::ostream& operator<<(std::ostream& os, const output_range& r){
for(auto&& x : r)
os << x;
return os;
}
};
namespace detail{
using has_get_raw = VEILER_HASTUR_TAG_CREATE(get_raw);
template<typename T>class pointer_iterator;
template<typename T>class pointer_iterator<T*>{
T* ptr;
using self = pointer_iterator<T*>;
public:
pointer_iterator() = default;
pointer_iterator(T* ptr):ptr(ptr){}
pointer_iterator(const pointer_iterator&) = default;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
using iterator_category = std::random_access_iterator_tag;
value_type operator*()const{return *ptr;}
self& operator++(){++ptr;return *this;}
self operator++(int){self t = *this; ++(*this); return t;}
self& operator+=(std::size_t n){ptr += n;return *this;}
self& operator--(){--ptr;return *this;}
self operator--(int){self t = *this;--(*this);return t;}
self operator-=(std::size_t n){ptr -= n;return *this;}
friend self operator+ (const self& lhs, std::size_t n){self t = lhs; t += n; return t;}
friend self operator- (const self& lhs, std::size_t n){self t = lhs; t -= n; return t;}
friend std::ptrdiff_t operator- (const self& lhs, const self& rhs){return lhs.ptr-rhs.ptr;}
friend bool operator==(const self& lhs, const self& rhs)noexcept{return lhs.ptr == rhs.ptr;}
friend bool operator!=(const self& lhs, const self& rhs)noexcept{return !(lhs == rhs);}
};
template<typename T>
using to_inheritable_iterator = std::conditional_t<std::is_pointer<T>::value, pointer_iterator<T>, T>;
}//namespace detail
template<typename FilterIterator, std::enable_if_t<veiler::hastur<detail::has_get_raw>::func<FilterIterator>{}>* = nullptr>
inline auto&& get_raw(FilterIterator&& it){
return std::forward<FilterIterator>(it).get_raw();
}
template<typename Iterator, std::enable_if_t<!veiler::hastur<detail::has_get_raw>::func<Iterator>{}>* = nullptr>
inline auto&& get_raw(Iterator&& it){
return std::forward<Iterator>(it);
}
struct annotation_type{
std::string_view filename;
std::size_t line;
std::size_t column;
};
template<typename T>
class annotation_range{
class annotation_iterator:public detail::to_inheritable_iterator<decltype(std::begin(std::declval<T>()))>{
using self = annotation_iterator;
using parent = detail::to_inheritable_iterator<decltype(std::begin(std::declval<T>()))>;
annotation_type data;
public:
annotation_iterator() = default;
annotation_iterator(const parent& it, std::string_view fn, std::size_t l = 1, std::size_t c = 1):parent{it}, data{fn, l, c}{}
annotation_iterator(const parent& it, const annotation_type& an):parent{it}, data{an}{}
annotation_iterator(const self&) = default;
std::string_view get_filename()const{return data.filename;}
std::size_t get_line()const{return data.line;}
std::size_t get_column()const{return data.column;}
self& operator++(){
if(**this == '\n'){
++data.line;
data.column = 1;
}
else
++data.column;
++*static_cast<parent*>(this);
return *this;
}
self operator++(int){self t = *this; ++(*this); return t;}
const annotation_type& get_annotate()const{return data;}
const parent& get_inner()const{return *this;}
};
annotation_type annot_data;
T&& t;
public:
using iterator = annotation_iterator;
using const_iterator = iterator;
using value_type = decltype(*std::declval<decltype(std::begin(std::declval<T>()))>());
annotation_range(annotation_type anno, T&& data):annot_data(std::move(anno)), t(std::forward<T>(data)){}
annotation_iterator begin()const{return annotation_iterator{t.begin(), annot_data};}
annotation_iterator end()const{return annotation_iterator{t.end(), annot_data};}
};
class annotation{
annotation_type data;
public:
annotation(const std::string_view& filename, std::size_t l = 1, std::size_t c = 1):data{filename, l, c}{}
annotation(const annotation_type& anno):data{anno}{}
template<typename T>
friend constexpr auto operator|(T&& t, annotation&& a)noexcept{
return annotation_range<T>{std::move(a.data), std::forward<T>(t)};
}
};
template<typename T>
class filter_iterator : T{
using self = filter_iterator<T>;
std::optional<typename T::value_type> t;
public:
using typename T::value_type;
template<typename... Args>
explicit filter_iterator(Args&&... args):T{std::forward<Args>(args)...}{}
filter_iterator(const filter_iterator& other):T{other}{}
filter_iterator& operator=(const filter_iterator& other){static_cast<T&>(*this) = other; return *this;}
value_type& operator*(){t = this->dereference();return *t;}
value_type operator*()const{return this->dereference();}
value_type* operator->(){return &**this;}
self& operator++(){this->next();return *this;}
self operator++(int){self t = *this; ++(*this); return t;}
self& operator+=(std::size_t n){while(n-->0)++(*this);return *this;}
auto&& get_raw(){return this->get_raw_iterator();}
auto&& get_raw()const{return this->get_raw_iterator();}
friend self operator+(const self& lhs, std::size_t n){self t = lhs; t += n; return t;}
friend bool operator==(const self& lhs, const self& rhs)noexcept{return lhs.is_equal(rhs);}
friend bool operator!=(const self& lhs, const self& rhs)noexcept{return !(lhs == rhs);}
};
}
namespace std{
template<typename T>struct iterator_traits<messer::filter_iterator<T>>{
using value_type = typename messer::filter_iterator<T>::value_type;
using iterator = messer::filter_iterator<T>;
using iterator_category = forward_iterator_tag;
using reference = value_type&;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
};
}
namespace veiler{
template<typename T>
struct hash<messer::filter_iterator<T>> : hash<typename messer::filter_iterator<T>::value_type>{
constexpr hash() = default;
constexpr hash(const hash&) = default;
constexpr hash(hash&&) = default;
~hash() = default;
hash& operator=(const hash&) = default;
hash& operator=(hash&&) = default;
using result_type = std::size_t;
using argument_type = messer::filter_iterator<T>;
std::size_t operator()(const argument_type& key)const noexcept{
return veiler::hash<typename messer::filter_iterator<T>::value_type>::operator()(*key);
}
};
}
namespace messer{
template<typename T, template<typename>class IteratorImpl>
class filter_range{
T t;
public:
using iterator = filter_iterator<IteratorImpl<std::decay_t<T>>>;
using const_iterator = iterator;
using value_type = typename iterator::value_type;
filter_range(T&& t):t{std::forward<T>(t)}{}
iterator begin()const{using std::begin;using std::end;return iterator{begin(t), end(t)};}
iterator end ()const{ using std::end;return iterator{end (t), end(t)};}
friend std::ostream& operator<<(std::ostream& os, const filter_range<T, IteratorImpl>& rhs){
for(auto&& x : rhs)
os << x;
return os;
}
};
class phase1_t{
template<typename T>
struct crlf_to_lf_iterator_impl{
using iterator = typename T::const_iterator;
private:
iterator b;
iterator e;
bool is_target()const noexcept{
if(*b != '\r')
return false;
const auto next = std::next(b);
return next != e && *next == '\n';
}
public:
using value_type = std::enable_if_t<std::is_convertible<typename T::value_type, char>::value, char>;
crlf_to_lf_iterator_impl() = default;
crlf_to_lf_iterator_impl(const iterator& b, const iterator& e):b{b}, e{e}{}
crlf_to_lf_iterator_impl(const crlf_to_lf_iterator_impl&) = default;
crlf_to_lf_iterator_impl& operator=(const crlf_to_lf_iterator_impl& other){b = other.b; e = other.e;return *this;}
value_type dereference()const{
return *b;
}
void next(){
++b;
if(is_target())
++b;
}
bool is_equal(const crlf_to_lf_iterator_impl& other)const noexcept{return b == other.b;}
auto& get_raw_iterator() {return get_raw(b);}
const auto& get_raw_iterator()const{return get_raw(b);}
};
template<typename T>
class trigraph_iterator_impl{
static char trigraph_convert(char c){
switch(c){
case '=' : return '#';
case '(' : return '[';
case '/' : return '\\';
case ')' : return ']';
case '\'': return '^';
case '<' : return '{';
case '!' : return '|';
case '>' : return '}';
case '-' : return '~';
}
return -1;
}
public:
using iterator = typename T::const_iterator;
private:
iterator b;
iterator e;
bool is_trigraph()const noexcept{
if(*b != '?')
return false;
auto next = std::next(b);
if(next == e || *next != '?')
return false;
next = std::next(next);
return next != e && trigraph_convert(*next) != -1;
}
public:
using value_type = std::enable_if_t<std::is_convertible<typename T::value_type, char>::value, char>;
trigraph_iterator_impl() = default;
trigraph_iterator_impl(const iterator& b, const iterator& e):b{b}, e{e}{}
trigraph_iterator_impl(const trigraph_iterator_impl&) = default;
trigraph_iterator_impl& operator=(const trigraph_iterator_impl& other){b = other.b;return *this;}
value_type dereference()const{
if(is_trigraph())
return trigraph_convert(*std::next(b,2));
return *b;
}
void next(){
if(is_trigraph())
std::advance(b, 3);
else
++b;
}
bool is_equal(const trigraph_iterator_impl& other)const noexcept{return b == other.b;}
auto& get_raw_iterator() {return get_raw(b);}
const auto& get_raw_iterator()const{return get_raw(b);}
};
template<typename T>
using trigraph = filter_range<T, trigraph_iterator_impl>;
template<typename T>
using crlf_to_lf = filter_range<T, crlf_to_lf_iterator_impl>;
public:
template<typename T>
friend constexpr auto operator|(T&& t, const phase1_t&)noexcept{
return /*trigraph<*/crlf_to_lf<T>/*>*/{{std::forward<T>(t)}};
}
};
class phase2_t{
template<typename T>
struct backslash_eol_iterator_impl{
using iterator = typename T::const_iterator;
private:
iterator b;
iterator e;
bool is_target()const noexcept{
if(*b != '\\')
return false;
const auto next = std::next(b);
return next != e && *next == '\n';
}
void skip(){
while(is_target())
std::advance(b, 2);
}
public:
using value_type = std::enable_if_t<std::is_convertible<typename T::value_type, char>::value, char>;
backslash_eol_iterator_impl() = default;
backslash_eol_iterator_impl(const iterator& b, const iterator& e):b{b}, e{e}{skip();}
backslash_eol_iterator_impl(const backslash_eol_iterator_impl&) = default;
backslash_eol_iterator_impl& operator=(const backslash_eol_iterator_impl& other){b = other.b;return *this;}
value_type dereference()const{
return *b;
}
void next(){
++b;
skip();
}
bool is_equal(const backslash_eol_iterator_impl& other)const noexcept{return b == other.b;}
auto& get_raw_iterator() {return get_raw(b);}
const auto& get_raw_iterator()const{return get_raw(b);}
};
template<typename T>
using backslash_eol = filter_range<T, backslash_eol_iterator_impl>;
public:
template<typename T>
friend constexpr auto operator|(T&& t, const phase2_t&)noexcept{
return backslash_eol<T>{std::forward<T>(t)};
}
};
#define MESSER_DECL_TOKEN_TYPE(sequence) \
enum class token_type{\
BOOST_PP_SEQ_ENUM(sequence)\
, END\
};\
inline std::ostream& operator<<(std::ostream& os, token_type t){\
switch(t){\
BOOST_PP_SEQ_FOR_EACH(MESSER_DECL_TOKEN_TYPE_I, _, sequence)\
default:os<<"unknown("<<static_cast<std::underlying_type_t<token_type>>(t)<<')';\
}\
return os;\
}
#define MESSER_DECL_TOKEN_TYPE_I(r, _, name) case token_type::name: os << BOOST_PP_STRINGIZE(name);break;
#define PUNCTUATORS \
(punctuator)(punctuator_hash)(punctuator_hashhash)(punctuator_left_parenthesis)(punctuator_comma)(punctuator_ellipsis)(punctuator_right_parenthesis)(punctuator_left_shift)(punctuator_right_shift)(punctuator_less_equal)(punctuator_greater_equal)(punctuator_less)(punctuator_greater)(punctuator_equalequal)(punctuator_not_equal)(punctuator_logical_or)(punctuator_logical_and)(punctuator_logical_not)(punctuator_ampersand)(punctuator_asterisk)(punctuator_plus)(punctuator_minus)(punctuator_division)(punctuator_modulo)(punctuator_bitwise_or)(punctuator_bitwise_xor)(punctuator_bitwise_not)(punctuator_question)(punctuator_colon)
#define IDENTIFIERS \
(identifier)(identifier_include)(identifier_define)(identifier_undef)(identifier_line)(identifier_error)(identifier_pragma)(identifier_if)(identifier_ifdef)(identifier_ifndef)(identifier_elif)(identifier_else)(identifier_endif)(identifier_defined)(identifier_time_)(identifier_date_)(identifier_file_)(identifier_line_)(identifier_pragma_op)(identifier_has_include)(identifier_true)(identifier_false)
MESSER_DECL_TOKEN_TYPE(
(empty)
(white_space)
(eol)
(header_name)
PUNCTUATORS
(character_literal)
(string_literal)
(pp_number)
IDENTIFIERS
(unclassified_character)
)
#undef MESSER_DECL_TOKEN_TYPE_I
#undef MESSER_DECL_TOKEN_TYPE
inline bool is_white_spaces(const token_type& t)noexcept{switch(t){case token_type::white_space: case token_type::eol: return true;default:return false;}}
#define MESSER_DECL_TOKEN_TYPE(r, _, name) case token_type::name:
inline bool is_punctuator(const token_type& t)noexcept{switch(t){BOOST_PP_SEQ_FOR_EACH(MESSER_DECL_TOKEN_TYPE, _, PUNCTUATORS)return true; default: return false;}}
inline bool is_identifier(const token_type& t)noexcept{switch(t){BOOST_PP_SEQ_FOR_EACH(MESSER_DECL_TOKEN_TYPE, _, IDENTIFIERS)return true; default: return false;}}
template<typename StrT>
class token{
StrT str;
token_type tt;
public:
template<typename T>
token(T&& str, token_type tok):str{std::forward<T>(str)}, tt{tok}{}
token_type type()const{return tt;}
token_type& type(){return tt;}
const StrT& get()const{return str;}
friend std::ostream& operator<<(std::ostream& os, const token& tkn){return os << tkn.str;}
};
#define RULE VEILER_PEGASUS_RULE
#define AUTO_RULE VEILER_PEGASUS_AUTO_RULE
#define INLINE_RULE VEILER_PEGASUS_INLINE_RULE
class phase3_t{
struct lexer : veiler::pegasus::parsers<lexer>{
static auto entrypoint(){
return instance().preprocessing_token;
}
static auto inner_include(){
return instance().rule_inner_include;
}
private:
static constexpr auto location = veiler::pegasus::semantic_actions::location;
static constexpr auto omit = veiler::pegasus::semantic_actions::omit;
static constexpr auto read = veiler::pegasus::read;
static constexpr auto value = veiler::pegasus::semantic_actions::value;
template<typename T>
static constexpr auto lit(T&& t){return veiler::pegasus::lit(std::forward<T>(t));}
template<typename T, typename U>
static constexpr auto range(T&& t, U&& u){return veiler::pegasus::range(std::forward<T>(t), std::forward<U>(u));}
static constexpr auto set_token(token_type t){
return [t](auto&& loc, [[maybe_unused]] auto&&... unused){return std::make_pair(loc, t);};
}
AUTO_RULE(rule_inner_include, veiler::pegasus::transient(
rules.header_name
| rules.preprocessing_token
))
AUTO_RULE(header_name, veiler::pegasus::transient(
( lit('<') >> +rules.h_char >> lit('>')
| lit('"') >> +rules.q_char >> lit('"')
)[location][set_token(token_type::header_name)]
))
AUTO_RULE(h_char, veiler::pegasus::transient(
omit[read - lit('\n') - lit('>')]
))
AUTO_RULE(q_char, veiler::pegasus::transient(
omit[read - lit('\n') - lit('"')]
))
AUTO_RULE(preprocessing_token, veiler::pegasus::transient(
rules.white_spaces[location][set_token(token_type::white_space)]
| lit('\n')[location][set_token(token_type::eol)]
| rules.pp_number[location][set_token(token_type::pp_number)]
| rules.punctuators
| rules.character_literal[location][set_token(token_type::character_literal)]
| rules.string_literal
| rules.directive
| (lit("true") >> !rules.identifier)[location][set_token(token_type::identifier_true)]
| (lit("false") >> !rules.identifier)[location][set_token(token_type::identifier_false)]
| (lit("defined") >> !rules.identifier)[location][set_token(token_type::identifier_defined)]
| (lit("__TIME__") >> !rules.identifier)[location][set_token(token_type::identifier_time_)]
| (lit("__DATE__") >> !rules.identifier)[location][set_token(token_type::identifier_date_)]
| (lit("__FILE__") >> !rules.identifier)[location][set_token(token_type::identifier_file_)]
| (lit("__LINE__") >> !rules.identifier)[location][set_token(token_type::identifier_line_)]
| (lit("_Pragma") >> !rules.identifier)[location][set_token(token_type::identifier_pragma_op)]
| (lit("__has_include") >> !rules.identifier)[location][set_token(token_type::identifier_has_include)]
| rules.identifier[location][set_token(token_type::identifier)]
| read[location][set_token(token_type::unclassified_character)]
))
AUTO_RULE(punctuators, veiler::pegasus::transient(
lit("%:%:" )[location][set_token(token_type::punctuator_hashhash)]
| lit("..." )[location][set_token(token_type::punctuator_ellipsis)]
| lit("<<=" )[location][set_token(token_type::punctuator)]
| lit(">>=" )[location][set_token(token_type::punctuator)]
| lit("->*" )[location][set_token(token_type::punctuator)]//C++
| lit(".*" )[location][set_token(token_type::punctuator)]//C++
| lit("%:" )[location][set_token(token_type::punctuator_hash)]
| lit("<:" )[location][set_token(token_type::punctuator)]
| lit(":>" )[location][set_token(token_type::punctuator)]
| lit("<%" )[location][set_token(token_type::punctuator)]
| lit("%>" )[location][set_token(token_type::punctuator)]
| lit("##" )[location][set_token(token_type::punctuator_hashhash)]
| lit("<<" )[location][set_token(token_type::punctuator_left_shift)]
| lit(">>" )[location][set_token(token_type::punctuator_right_shift)]
| lit("->" )[location][set_token(token_type::punctuator)]
| lit("++" )[location][set_token(token_type::punctuator)]
| lit("--" )[location][set_token(token_type::punctuator)]
| lit("<=" )[location][set_token(token_type::punctuator_less_equal)]
| lit(">=" )[location][set_token(token_type::punctuator_greater_equal)]
| lit("==" )[location][set_token(token_type::punctuator_equalequal)]
| lit("!=" )[location][set_token(token_type::punctuator_not_equal)]
| lit("&&" )[location][set_token(token_type::punctuator_logical_and)]
| lit("||" )[location][set_token(token_type::punctuator_logical_or)]
| lit("*=" )[location][set_token(token_type::punctuator)]
| lit("/=" )[location][set_token(token_type::punctuator)]
| lit("%=" )[location][set_token(token_type::punctuator)]
| lit("+=" )[location][set_token(token_type::punctuator)]
| lit("-=" )[location][set_token(token_type::punctuator)]
| lit("&=" )[location][set_token(token_type::punctuator)]
| lit("^=" )[location][set_token(token_type::punctuator)]
| lit("|=" )[location][set_token(token_type::punctuator)]
| lit("::" )[location][set_token(token_type::punctuator)]//C++
| lit('#' )[location][set_token(token_type::punctuator_hash)]
| lit('[' )[location][set_token(token_type::punctuator)]
| lit(']' )[location][set_token(token_type::punctuator)]
| lit('(' )[location][set_token(token_type::punctuator_left_parenthesis)]
| lit(')' )[location][set_token(token_type::punctuator_right_parenthesis)]
| lit('{' )[location][set_token(token_type::punctuator)]
| lit('}' )[location][set_token(token_type::punctuator)]
| lit('.' )[location][set_token(token_type::punctuator)]
| lit('&' )[location][set_token(token_type::punctuator_ampersand)]
| lit('*' )[location][set_token(token_type::punctuator_asterisk)]
| lit('+' )[location][set_token(token_type::punctuator_plus)]
| lit('-' )[location][set_token(token_type::punctuator_minus)]
| lit('~' )[location][set_token(token_type::punctuator_bitwise_not)]
| lit('!' )[location][set_token(token_type::punctuator_logical_not)]
| lit('/' )[location][set_token(token_type::punctuator_division)]
| lit('%' )[location][set_token(token_type::punctuator_modulo)]
| lit('<' )[location][set_token(token_type::punctuator_less)]
| lit('>' )[location][set_token(token_type::punctuator_greater)]
| lit('^' )[location][set_token(token_type::punctuator_bitwise_xor)]
| lit('|' )[location][set_token(token_type::punctuator_bitwise_or)]
| lit('?' )[location][set_token(token_type::punctuator_question)]
| lit(':' )[location][set_token(token_type::punctuator_colon)]
| lit(';' )[location][set_token(token_type::punctuator)]
| lit('=' )[location][set_token(token_type::punctuator)]
| lit(',' )[location][set_token(token_type::punctuator_comma)]
| lit("bitand")[location][set_token(token_type::punctuator_ampersand)] >> omit[!rules.identifier]
| lit("and_eq")[location][set_token(token_type::punctuator)] >> omit[!rules.identifier]
| lit("xor_eq")[location][set_token(token_type::punctuator)] >> omit[!rules.identifier]
| lit("not_eq")[location][set_token(token_type::punctuator_not_equal)] >> omit[!rules.identifier]
| lit("bitor" )[location][set_token(token_type::punctuator_bitwise_or)] >> omit[!rules.identifier]
| lit("compl" )[location][set_token(token_type::punctuator_bitwise_not)] >> omit[!rules.identifier]
| lit("or_eq" )[location][set_token(token_type::punctuator)] >> omit[!rules.identifier]
| lit("and" )[location][set_token(token_type::punctuator_logical_and)] >> omit[!rules.identifier]
| lit("xor" )[location][set_token(token_type::punctuator_bitwise_xor)] >> omit[!rules.identifier]
| lit("not" )[location][set_token(token_type::punctuator_logical_not)] >> omit[!rules.identifier]
| lit("or" )[location][set_token(token_type::punctuator_logical_or)] >> omit[!rules.identifier]
))
AUTO_RULE(pp_number, veiler::pegasus::transient(
( rules.digit
| lit('.') >> rules.digit
)
>> *( -lit('\'') >> rules.digit
| lit('e') >> rules.sign
| lit('E') >> rules.sign
| lit('p') >> rules.sign
| lit('P') >> rules.sign
| lit('.')
| -lit('\'') >> rules.ident_nondigit
)
))
AUTO_RULE(character_literal, veiler::pegasus::transient(
-( lit("u8")
| rules.encoding_prefix
)
>> lit('\'')
>> +rules.c_char
>> lit('\'')
>> -rules.identifier//UDL
))
AUTO_RULE(encoding_prefix, veiler::pegasus::transient(
lit('u')
| lit('U')
| lit('L')
))
AUTO_RULE(c_char, veiler::pegasus::transient(
( read - lit('\'') - lit('\\') - lit('\n') )
| rules.escape_sequence
))
AUTO_RULE(escape_sequence, veiler::pegasus::transient(
lit('\\')
>> ( lit('\'')
| lit('"')
| lit('?')
| lit('\\')
| lit('a')
| lit('b')
| lit('f')
| lit('n')
| lit('r')
| lit('t')
| lit('v')
| rules.octo >> rules.octo >> rules.octo
| rules.octo >> rules.octo
| rules.octo
| lit('x') >> +rules.hex
)
))
AUTO_RULE(string_literal, veiler::pegasus::transient(
( -( lit("u8")
| rules.encoding_prefix
)
>> lit('"')
>> *rules.s_char
>> lit('"')
>> -rules.identifier
)[location][set_token(token_type::string_literal)]
|
( -( lit("u8")
| rules.encoding_prefix
)
>> lit("R\"")
>> veiler::pegasus::filter([&rules](auto&& v, auto&& end, auto&&... args){
const auto raw_string_literal_parser =
(*rules.d_char)[location][([](auto&& loc, auto&&, auto&& delim, [[maybe_unused]] auto&&... unused){delim.assign(loc.begin(), loc.end());return veiler::pegasus::unit{};})]
>> lit('(')
>> *(veiler::pegasus::read
- ( lit(')')
>> veiler::pegasus::loop(
veiler::pegasus::eps[([](auto&&, auto&&, auto&& delim, [[maybe_unused]] auto&&... unused){return delim.size();})],
rules.d_char[value])[veiler::pegasus::semantic_actions::change_container<std::string>]
[([](auto&& v, auto&& loc, auto&& delim, [[maybe_unused]] auto&&... unused)
->veiler::expected<veiler::pegasus::unit, veiler::pegasus::parse_error<decltype(loc.begin())>>{
if(v != delim)return veiler::make_unexpected(veiler::pegasus::error_type::semantic_check_failed{"raw string literal: unmatch delimiter"});
else return veiler::pegasus::unit{};
})]
>> lit('"')
))
>> lit(')')
>> veiler::pegasus::loop(
veiler::pegasus::eps[([](auto&&, auto&&, auto&& delim, [[maybe_unused]] auto&&... unused){return delim.size();})],
rules.d_char)
>> &lit('"');
auto it = messer::get_raw(v);
auto ret = raw_string_literal_parser[omit](it, messer::get_raw(end), args...);
messer::get_raw(v) = it;
++v;
return ret.valid();
})
>> -rules.identifier//UDL
)[location][set_token(token_type::string_literal)]
))
AUTO_RULE(s_char, veiler::pegasus::transient(
( read - lit('"') - lit('\\') - lit('\n') )
| rules.escape_sequence
))
AUTO_RULE(d_char, veiler::pegasus::transient(
read
- lit(' ')
- lit('(')
- lit(')')
- lit('\\')
- lit('\t')
- lit('\v')
- lit('\f')
- lit('\n')
))
AUTO_RULE(directive, veiler::pegasus::transient(
( lit("include") >> &( rules.white_spaces
| lit('<')
| lit('"')
)
)[location][set_token(token_type::identifier_include)]
|
( lit("define") >> & rules.white_spaces
)[location][set_token(token_type::identifier_define)]
|
( lit("undef") >> & rules.white_spaces
)[location][set_token(token_type::identifier_undef)]
|
( lit("line") >> & rules.white_spaces
)[location][set_token(token_type::identifier_line)]
|
( lit("error") >> &( rules.white_spaces
| lit('\n')
)
)[location][set_token(token_type::identifier_error)]
|
( lit("pragma") >> &( rules.white_spaces
| lit('\n')
)
)[location][set_token(token_type::identifier_pragma)]
|
( lit("if") >> &( rules.white_spaces
| lit('(')
)
)[location][set_token(token_type::identifier_if)]
|
( lit("ifdef") >> & rules.white_spaces
)[location][set_token(token_type::identifier_ifdef)]
|
( lit("ifndef") >> & rules.white_spaces
)[location][set_token(token_type::identifier_ifndef)]
|
( lit("elif") >> &( rules.white_spaces
| lit('(')
)
)[location][set_token(token_type::identifier_elif)]
|
( lit("else") >> &( rules.white_spaces
| lit('\n')
)
)[location][set_token(token_type::identifier_else)]
|
( lit("endif") >> &( rules.white_spaces
| lit('\n')
)
)[location][set_token(token_type::identifier_endif)]
))
AUTO_RULE(identifier, veiler::pegasus::transient(
(
rules.ident_nondigit
>> *( rules.ident_nondigit
| rules.digit
)
)
))
AUTO_RULE(white_spaces, veiler::pegasus::transient(
+( lit(' ')
| lit('\t')
| lit("/*") >> *( read - lit("*/") ) >> lit("*/")
| lit("//") >> *( read - lit('\n') )
| lit('\v')
| lit('\f')
)
))
RULE(ident_nondigit, char, veiler::pegasus::transient(
rules.nondigit
))
RULE(nondigit, char, veiler::pegasus::transient(
range('A', 'Z')[value]
| range('a', 'z')[value]
| lit('_')[value]
))
RULE(digit, char, veiler::pegasus::transient(
range('0', '9')[value]
))
RULE(hex, char, veiler::pegasus::transient(
rules.digit
| range('A', 'F')[value]
| range('a', 'f')[value]
))
RULE(octo, char, veiler::pegasus::transient(
range('0', '7')[value]
))
RULE(sign, char, veiler::pegasus::transient(
lit('+')[value]
| lit('-')[value]
))
};
public:
class value_type : public token<std::string>{
using parent = token<std::string>;
annotation_type data;
public:
value_type(parent&& tk, const annotation_type& anno):parent{std::move(tk)}, data{anno}{}
std::string_view filename()const{return data.filename;}
std::size_t line()const{return data.line;}
std::size_t column()const{return data.column;}
const annotation_type& annotation()const{return data;}
annotation_type& annotation(){return data;}
value_type& filename(std::string_view sv){data.filename = std::move(sv); return *this;}
value_type& line(std::size_t ln){data.line = ln; return *this;}
friend std::ostream& operator<<(std::ostream& os, const value_type& v){return os << *static_cast<const parent*>(&v);}
};
private:
template<typename T>
class lexer_iterator_impl{
using impl = typename T::iterator;
impl it;
impl end;
enum class status{
first,
pp_directive,
include,
none
}st = status::first;
token_type tkt = token_type::eol;
impl beg;
bool result;
public:
using value_type = phase3_t::value_type;
using iterator = filter_iterator<lexer_iterator_impl<T>>;
lexer_iterator_impl(const impl& b, const impl& e):it(b), end(e), beg(b), result(b != e){}
lexer_iterator_impl(const lexer_iterator_impl&) = default;
lexer_iterator_impl& operator=(const lexer_iterator_impl&) = default;
value_type dereference()const{
if(!result)
throw std::runtime_error("can't dereference it");
const auto& i = get_raw(beg);
const auto anno = annotation_type{i.get_filename(), i.get_line(), i.get_column()};
if(tkt != token_type::string_literal || *beg == '"')
return value_type{token<std::string>{std::string{beg, it}, tkt}, anno};
auto itt = beg;
while(std::next(itt) != end && *std::next(itt) != '"')
++itt;
if(*itt != 'R')
return value_type{token<std::string>{std::string{beg, it}, tkt}, anno};
++itt;
auto edit = itt;
auto edit_raw = messer::get_raw(edit);
std::string delimiter;
for(auto itr = std::next(edit_raw); *itr != '('; ++itr)
delimiter.push_back(*itr);
std::advance(edit_raw, delimiter.size());
while(true){
while(*edit_raw++ != ')');
auto itr = edit_raw;
for(auto&& x : delimiter){
if(*itr++ != x)
break;
}
if(*itr == '"'){
messer::get_raw(edit) = itr;
break;
}
}
return value_type{token<std::string>{std::string{beg, itt} + std::string{messer::get_raw(itt).get_inner(), messer::get_raw(edit).get_inner()} + std::string{edit, it} , tkt}, anno};
}
void next(){
if(!result)
return;
beg = it;
auto ret = (st == status::include ?
lexer::inner_include()(it, end, std::string{}):
lexer::entrypoint ()(it, end, std::string{}));
result = ret && it != beg;
if(!result)
return;
tkt = ret--->second;
if(st == status::first && tkt == token_type::punctuator_hash)
st = status::pp_directive;
else if(st == status::pp_directive && tkt == token_type::identifier_include)
st = status::include;
else if(tkt == token_type::eol)
st = status::first;
else if(tkt != token_type::white_space)
st = status::none;
}
bool is_equal(const lexer_iterator_impl& other)const noexcept{return result == other.result && it == other.it;}
auto& get_raw_iterator() {return it;}
const auto& get_raw_iterator()const{return it;}
};
template<typename T>
using lexer_range = filter_range<T, lexer_iterator_impl>;
public:
template<typename T>
friend constexpr auto operator|(T&& t, const phase3_t&)noexcept{
return lexer_range<T>{std::forward<T>(t)};
}
};
}
namespace veiler{
template<>
struct hash<messer::phase3_t::value_type> : hash<std::underlying_type_t<messer::token_type>>, hash<std::string>, hash<std::string_view>, hash<std::size_t>{
constexpr hash() = default;
constexpr hash(const hash&) = default;
constexpr hash(hash&&) = default;
~hash() = default;
hash& operator=(const hash&) = default;
hash& operator=(hash&&) = default;
using result_type = std::size_t;
using argument_type = messer::phase3_t::value_type;
std::size_t operator()(const argument_type& key)const noexcept{
return detail::fool::hash_combine(hash<std::underlying_type_t<messer::token_type>>::operator()(static_cast<std::underlying_type_t<messer::token_type>>(key.type())), hash<std::string>::operator()(key.get()), hash<std::string_view>::operator()(key.filename()), hash<std::size_t>::operator()(key.line()), hash<std::size_t>::operator()(key.column()));
}
};
}
namespace veiler::pegasus{
template<>
struct member_accessor<std::string_view, messer::phase3_t::value_type>{
static const std::string& access(const messer::phase3_t::value_type& t){return t.get();}
};
template<>
struct member_accessor<std::string, messer::phase3_t::value_type>{
static const std::string& access(const messer::phase3_t::value_type& t){return t.get();}
};
template<>
struct member_accessor<const char*, messer::phase3_t::value_type>{
static const std::string& access(const messer::phase3_t::value_type& t){return t.get();}
};
template<>
struct member_accessor<messer::token_type, messer::phase3_t::value_type>{
static messer::token_type access(const messer::phase3_t::value_type& t){return t.type();}
};
}
#include<iostream>
#include<vector>
#include<list>
namespace messer{
template<typename T, typename U>
inline bool tokens_equal(const T& t, const U& u){
if(t.size() != u.size())
return false;
for(auto t_it = t.begin(), u_it = u.begin(); t_it != t.end(); ++t_it, ++u_it)
if(t_it->get() != u_it->get())
return false;
return true;
}
template<typename T>
inline std::vector<T> vector_product(std::vector<T> x, std::vector<T> y){
std::sort(x.begin(), x.end());
std::sort(y.begin(), y.end());
std::vector<T> product;
product.reserve(x.size() + y.size());
std::set_intersection(x.begin(), x.end(), y.begin(), y.end(), std::back_inserter(product));
return product;
}
namespace detail{
template<typename T>
struct list_replace_1{
output_range<typename std::list<T>::const_iterator> r;
std::list<T> data;
output_range<typename std::list<T>::iterator> operator()(std::list<T>& l){
if(data.empty()){
const auto it = l.erase(r.begin(), r.end());
return output_range<typename std::list<T>::iterator>{it, it};
}
const auto inserted_begin = data.begin();
l.splice(r.begin(), std::move(data));
return output_range<typename std::list<T>::iterator>{inserted_begin, l.erase(r.begin(), r.end())};
}
friend output_range<typename std::list<T>::iterator> operator|(std::list<T>& l, list_replace_1&& rep){return std::move(rep)(l);}
list_replace_1(output_range<typename std::list<T>::const_iterator> r, std::list<T>&& data):r{std::move(r)}, data{std::move(data)}{}
list_replace_1(output_range<typename std::list<T>::iterator> r, std::list<T>&& data):r{r.begin(), r.end()}, data{std::move(data)}{}
};
template<typename T>
struct list_replace_2{
output_range<typename std::list<T>::iterator> r;
T data;
output_range<typename std::list<T>::iterator> operator()(std::list<T>& l){
const auto inserted_begin = l.insert(r.begin(), std::move(data));
return {inserted_begin, l.erase(r.begin(), r.end())};
}
friend output_range<typename std::list<T>::iterator> operator|(std::list<T>& l, list_replace_2&& rep){return std::move(rep)(l);}
};
template<typename T, typename U>
struct list_replace_3{
output_range<typename std::list<T>::iterator> r;
output_range<U> data;
output_range<typename std::list<T>::iterator> operator()(std::list<T>& l){
const auto inserted_begin = l.insert(r.begin(), data.begin(), data.end());
return {inserted_begin, l.erase(r.begin(), r.end())};
}
friend output_range<typename std::list<T>::iterator> operator|(std::list<T>& l, list_replace_3&& rep){return std::move(rep)(l);}
};
}
template<typename T>
inline detail::list_replace_1<typename std::iterator_traits<T>::value_type> replacer(const T& rb, const T& re, std::list<typename std::iterator_traits<T>::value_type> l){return detail::list_replace_1<typename std::iterator_traits<T>::value_type>(output_range<T>{rb, re}, std::move(l));}
template<typename T>
inline detail::list_replace_2<typename std::iterator_traits<T>::value_type> replacer(const T& rb, const T& re, typename std::iterator_traits<T>::value_type t){return {output_range<T>{rb, re}, std::move(t)};}
template<typename T, typename U>
inline detail::list_replace_3<typename std::iterator_traits<T>::value_type, U> replacer(const T& rb, const T& re, const output_range<U>& r2){return {output_range<T>{rb, re}, r2};}
template<typename T, typename U, typename Y>
inline detail::list_replace_3<typename std::iterator_traits<T>::value_type, std::common_type_t<U, Y>> replacer(const T& rb, const T& re, const U& b, const Y& e){return replacer(rb, re, output_range<std::common_type_t<U, Y>>{b, e});}
inline std::string escape(std::string_view str){
std::string ret;
for(auto&& x : str)
switch(x){
case '\t':ret += "\\t";break;
case '\'':ret += "\\\'";break;
case '"': ret += "\\\"";break;
case '\\':ret += "\\\\";break;
case '\a':ret += "\\a";break;
case '\b':ret += "\\b";break;
case '\f':ret += "\\f";break;
case '\n':ret += "\\n";break;
case '\r':ret += "\\r";break;
case '\v':ret += "\\v";break;
default: ret += x;break;
}
return ret;
}
inline std::string unescape(std::string_view str){
static constexpr auto oct = veiler::pegasus::filter([](auto&& x, auto&&){return '0' <= *x && *x <= '8';})[([](auto&& x, [[maybe_unused]] auto&&... unused)->int{return *x - '0';})] >> veiler::pegasus::read[veiler::pegasus::semantic_actions::omit];
static constexpr auto hex = veiler::pegasus::filter([](auto&& x, auto&&){return '0' <= *x && *x <= '9';})[([](auto&& x, [[maybe_unused]] auto&&... unused)->int{return *x - '0';})] >> veiler::pegasus::read[veiler::pegasus::semantic_actions::omit]
| veiler::pegasus::filter([](auto&& x, auto&&){return 'a' <= *x && *x <= 'f';})[([](auto&& x, [[maybe_unused]] auto&&... unused)->int{return *x - 'a'+10;})] >> veiler::pegasus::read[veiler::pegasus::semantic_actions::omit]
| veiler::pegasus::filter([](auto&& x, auto&&){return 'A' <= *x && *x <= 'F';})[([](auto&& x, [[maybe_unused]] auto&&... unused)->int{return *x - 'A'+10;})] >> veiler::pegasus::read[veiler::pegasus::semantic_actions::omit];
std::string ret;
ret.reserve(str.size());
for(auto it = str.begin(); it != str.end();)
if(*it != '\\')
ret += *it++;
else if(it+1 != str.end())switch(*(it+1)){
case 't': ret += '\t';it += 2;break;
case '\'':ret += '\'';it += 2;break;
case '"':ret += '"';it += 2;break;
case '\\':ret += '\\';it += 2;break;
case '?': ret += '?';it += 2;break;
case 'a': ret += '\a';it += 2;break;
case 'b': ret += '\b';it += 2;break;
case 'f': ret += '\f';it += 2;break;
case 'n': ret += '\n';it += 2;break;
case 'r': ret += '\r';it += 2;break;