-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluon-complete-v0.0.140.hpp
18811 lines (16274 loc) · 671 KB
/
cluon-complete-v0.0.140.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
// This is an auto-generated header-only single-file distribution of libcluon.
// Date: Thu, 04 Feb 2021 12:20:57 +0000
// Version: 0.0.140
//
//
// Implementation of N4562 std::experimental::any (merged into C++17) for C++11 compilers.
//
// See also:
// + http://en.cppreference.com/w/cpp/any
// + http://en.cppreference.com/w/cpp/experimental/any
// + http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4562.html#any
// + https://cplusplus.github.io/LWG/lwg-active.html#2509
//
//
// Copyright (c) 2016 Denilson das Mercês Amorim
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef LINB_ANY_HPP
#define LINB_ANY_HPP
//#pragma once
#include <typeinfo>
#include <type_traits>
#include <stdexcept>
#if defined(PARTICLE)
#if !defined(__cpp_exceptions) && !defined(ANY_IMPL_NO_EXCEPTIONS) && !defined(ANY_IMPL_EXCEPTIONS)
# define ANY_IMPL_NO_EXCEPTIONS
# endif
#else
// you can opt-out of exceptions by definining ANY_IMPL_NO_EXCEPTIONS,
// but you must ensure not to cast badly when passing an `any' object to any_cast<T>(any)
#endif
#if defined(PARTICLE)
#if !defined(__cpp_rtti) && !defined(ANY_IMPL_NO_RTTI) && !defined(ANY_IMPL_RTTI)
# define ANY_IMPL_NO_RTTI
# endif
#else
// you can opt-out of RTTI by defining ANY_IMPL_NO_RTTI,
// in order to disable functions working with the typeid of a type
#endif
namespace linb
{
class bad_any_cast : public std::bad_cast
{
public:
const char* what() const noexcept override
{
return "bad any cast";
}
};
class any final
{
public:
/// Constructs an object of type any with an empty state.
any() :
vtable(nullptr)
{
}
/// Constructs an object of type any with an equivalent state as other.
any(const any& rhs) :
vtable(rhs.vtable)
{
if(!rhs.empty())
{
rhs.vtable->copy(rhs.storage, this->storage);
}
}
/// Constructs an object of type any with a state equivalent to the original state of other.
/// rhs is left in a valid but otherwise unspecified state.
any(any&& rhs) noexcept :
vtable(rhs.vtable)
{
if(!rhs.empty())
{
rhs.vtable->move(rhs.storage, this->storage);
rhs.vtable = nullptr;
}
}
/// Same effect as this->clear().
~any()
{
this->clear();
}
/// Constructs an object of type any that contains an object of type T direct-initialized with std::forward<ValueType>(value).
///
/// T shall satisfy the CopyConstructible requirements, otherwise the program is ill-formed.
/// This is because an `any` may be copy constructed into another `any` at any time, so a copy should always be allowed.
template<typename ValueType, typename = typename std::enable_if<!std::is_same<typename std::decay<ValueType>::type, any>::value>::type>
any(ValueType&& value)
{
static_assert(std::is_copy_constructible<typename std::decay<ValueType>::type>::value,
"T shall satisfy the CopyConstructible requirements.");
this->construct(std::forward<ValueType>(value));
}
/// Has the same effect as any(rhs).swap(*this). No effects if an exception is thrown.
any& operator=(const any& rhs)
{
any(rhs).swap(*this);
return *this;
}
/// Has the same effect as any(std::move(rhs)).swap(*this).
///
/// The state of *this is equivalent to the original state of rhs and rhs is left in a valid
/// but otherwise unspecified state.
any& operator=(any&& rhs) noexcept
{
any(std::move(rhs)).swap(*this);
return *this;
}
/// Has the same effect as any(std::forward<ValueType>(value)).swap(*this). No effect if a exception is thrown.
///
/// T shall satisfy the CopyConstructible requirements, otherwise the program is ill-formed.
/// This is because an `any` may be copy constructed into another `any` at any time, so a copy should always be allowed.
template<typename ValueType, typename = typename std::enable_if<!std::is_same<typename std::decay<ValueType>::type, any>::value>::type>
any& operator=(ValueType&& value)
{
static_assert(std::is_copy_constructible<typename std::decay<ValueType>::type>::value,
"T shall satisfy the CopyConstructible requirements.");
any(std::forward<ValueType>(value)).swap(*this);
return *this;
}
/// If not empty, destroys the contained object.
void clear() noexcept
{
if(!empty())
{
this->vtable->destroy(storage);
this->vtable = nullptr;
}
}
/// Returns true if *this has no contained object, otherwise false.
bool empty() const noexcept
{
return this->vtable == nullptr;
}
#ifndef ANY_IMPL_NO_RTTI
/// If *this has a contained object of type T, typeid(T); otherwise typeid(void).
const std::type_info& type() const noexcept
{
return empty()? typeid(void) : this->vtable->type();
}
#endif
/// Exchange the states of *this and rhs.
void swap(any& rhs) noexcept
{
if(this->vtable != rhs.vtable)
{
any tmp(std::move(rhs));
// move from *this to rhs.
rhs.vtable = this->vtable;
if(this->vtable != nullptr)
{
this->vtable->move(this->storage, rhs.storage);
//this->vtable = nullptr; -- unneeded, see below
}
// move from tmp (previously rhs) to *this.
this->vtable = tmp.vtable;
if(tmp.vtable != nullptr)
{
tmp.vtable->move(tmp.storage, this->storage);
tmp.vtable = nullptr;
}
}
else // same types
{
if(this->vtable != nullptr)
this->vtable->swap(this->storage, rhs.storage);
}
}
private: // Storage and Virtual Method Table
union storage_union
{
using stack_storage_t = typename std::aligned_storage<2 * sizeof(void*), std::alignment_of<void*>::value>::type;
void* dynamic;
stack_storage_t stack; // 2 words for e.g. shared_ptr
};
/// Base VTable specification.
struct vtable_type
{
// Note: The caller is responssible for doing .vtable = nullptr after destructful operations
// such as destroy() and/or move().
#ifndef ANY_IMPL_NO_RTTI
/// The type of the object this vtable is for.
const std::type_info& (*type)() noexcept;
#endif
/// Destroys the object in the union.
/// The state of the union after this call is unspecified, caller must ensure not to use src anymore.
void(*destroy)(storage_union&) noexcept;
/// Copies the **inner** content of the src union into the yet unitialized dest union.
/// As such, both inner objects will have the same state, but on separate memory locations.
void(*copy)(const storage_union& src, storage_union& dest);
/// Moves the storage from src to the yet unitialized dest union.
/// The state of src after this call is unspecified, caller must ensure not to use src anymore.
void(*move)(storage_union& src, storage_union& dest) noexcept;
/// Exchanges the storage between lhs and rhs.
void(*swap)(storage_union& lhs, storage_union& rhs) noexcept;
};
/// VTable for dynamically allocated storage.
template<typename T>
struct vtable_dynamic
{
#ifndef ANY_IMPL_NO_RTTI
static const std::type_info& type() noexcept
{
return typeid(T);
}
#endif
static void destroy(storage_union& storage) noexcept
{
//assert(reinterpret_cast<T*>(storage.dynamic));
delete reinterpret_cast<T*>(storage.dynamic);
}
static void copy(const storage_union& src, storage_union& dest)
{
dest.dynamic = new T(*reinterpret_cast<const T*>(src.dynamic));
}
static void move(storage_union& src, storage_union& dest) noexcept
{
dest.dynamic = src.dynamic;
src.dynamic = nullptr;
}
static void swap(storage_union& lhs, storage_union& rhs) noexcept
{
// just exchage the storage pointers.
std::swap(lhs.dynamic, rhs.dynamic);
}
};
/// VTable for stack allocated storage.
template<typename T>
struct vtable_stack
{
#ifndef ANY_IMPL_NO_RTTI
static const std::type_info& type() noexcept
{
return typeid(T);
}
#endif
static void destroy(storage_union& storage) noexcept
{
reinterpret_cast<T*>(&storage.stack)->~T();
}
static void copy(const storage_union& src, storage_union& dest)
{
new (&dest.stack) T(reinterpret_cast<const T&>(src.stack));
}
static void move(storage_union& src, storage_union& dest) noexcept
{
// one of the conditions for using vtable_stack is a nothrow move constructor,
// so this move constructor will never throw a exception.
new (&dest.stack) T(std::move(reinterpret_cast<T&>(src.stack)));
destroy(src);
}
static void swap(storage_union& lhs, storage_union& rhs) noexcept
{
storage_union tmp_storage;
move(rhs, tmp_storage);
move(lhs, rhs);
move(tmp_storage, lhs);
}
};
/// Whether the type T must be dynamically allocated or can be stored on the stack.
template<typename T>
struct requires_allocation :
std::integral_constant<bool,
!(std::is_nothrow_move_constructible<T>::value // N4562 §6.3/3 [any.class]
&& sizeof(T) <= sizeof(storage_union::stack)
&& std::alignment_of<T>::value <= std::alignment_of<storage_union::stack_storage_t>::value)>
{};
/// Returns the pointer to the vtable of the type T.
template<typename T>
static vtable_type* vtable_for_type()
{
using VTableType = typename std::conditional<requires_allocation<T>::value, vtable_dynamic<T>, vtable_stack<T>>::type;
static vtable_type table = {
#ifndef ANY_IMPL_NO_RTTI
VTableType::type,
#endif
VTableType::destroy,
VTableType::copy, VTableType::move,
VTableType::swap,
};
return &table;
}
protected:
template<typename T>
friend const T* any_cast(const any* operand) noexcept;
template<typename T>
friend T* any_cast(any* operand) noexcept;
/// Casts (with no type_info checks) the storage pointer as const T*.
template<typename T>
const T* cast() const noexcept
{
return requires_allocation<typename std::decay<T>::type>::value?
reinterpret_cast<const T*>(storage.dynamic) :
reinterpret_cast<const T*>(&storage.stack);
}
/// Casts (with no type_info checks) the storage pointer as T*.
template<typename T>
T* cast() noexcept
{
return requires_allocation<typename std::decay<T>::type>::value?
reinterpret_cast<T*>(storage.dynamic) :
reinterpret_cast<T*>(&storage.stack);
}
private:
storage_union storage; // on offset(0) so no padding for align
vtable_type* vtable;
template<typename ValueType, typename T>
typename std::enable_if<requires_allocation<T>::value>::type
do_construct(ValueType&& value)
{
storage.dynamic = new T(std::forward<ValueType>(value));
}
template<typename ValueType, typename T>
typename std::enable_if<!requires_allocation<T>::value>::type
do_construct(ValueType&& value)
{
new (&storage.stack) T(std::forward<ValueType>(value));
}
/// Chooses between stack and dynamic allocation for the type decay_t<ValueType>,
/// assigns the correct vtable, and constructs the object on our storage.
template<typename ValueType>
void construct(ValueType&& value)
{
using T = typename std::decay<ValueType>::type;
this->vtable = vtable_for_type<T>();
do_construct<ValueType,T>(std::forward<ValueType>(value));
}
};
namespace detail
{
template<typename ValueType>
inline ValueType any_cast_move_if_true(typename std::remove_reference<ValueType>::type* p, std::true_type)
{
return std::move(*p);
}
template<typename ValueType>
inline ValueType any_cast_move_if_true(typename std::remove_reference<ValueType>::type* p, std::false_type)
{
return *p;
}
}
/// Performs *any_cast<add_const_t<remove_reference_t<ValueType>>>(&operand), or throws bad_any_cast on failure.
template<typename ValueType>
inline ValueType any_cast(const any& operand)
{
auto p = any_cast<typename std::add_const<typename std::remove_reference<ValueType>::type>::type>(&operand);
#ifndef ANY_IMPL_NO_EXCEPTIONS
if(p == nullptr) throw bad_any_cast();
#endif
return *p;
}
/// Performs *any_cast<remove_reference_t<ValueType>>(&operand), or throws bad_any_cast on failure.
template<typename ValueType>
inline ValueType any_cast(any& operand)
{
auto p = any_cast<typename std::remove_reference<ValueType>::type>(&operand);
#ifndef ANY_IMPL_NO_EXCEPTIONS
if(p == nullptr) throw bad_any_cast();
#endif
return *p;
}
///
/// If ValueType is MoveConstructible and isn't a lvalue reference, performs
/// std::move(*any_cast<remove_reference_t<ValueType>>(&operand)), otherwise
/// *any_cast<remove_reference_t<ValueType>>(&operand). Throws bad_any_cast on failure.
///
template<typename ValueType>
inline ValueType any_cast(any&& operand)
{
using can_move = std::integral_constant<bool,
std::is_move_constructible<ValueType>::value
&& !std::is_lvalue_reference<ValueType>::value>;
auto p = any_cast<typename std::remove_reference<ValueType>::type>(&operand);
#ifndef ANY_IMPL_NO_EXCEPTIONS
if(p == nullptr) throw bad_any_cast();
#endif
return detail::any_cast_move_if_true<ValueType>(p, can_move());
}
/// If operand != nullptr && operand->type() == typeid(ValueType), a pointer to the object
/// contained by operand, otherwise nullptr.
template<typename ValueType>
inline const ValueType* any_cast(const any* operand) noexcept
{
using T = typename std::decay<ValueType>::type;
if (operand && operand->vtable == any::vtable_for_type<T>())
return operand->cast<ValueType>();
else
return nullptr;
}
/// If operand != nullptr && operand->type() == typeid(ValueType), a pointer to the object
/// contained by operand, otherwise nullptr.
template<typename ValueType>
inline ValueType* any_cast(any* operand) noexcept
{
using T = typename std::decay<ValueType>::type;
if (operand && operand->vtable == any::vtable_for_type<T>())
return operand->cast<ValueType>();
else
return nullptr;
}
}
namespace std
{
inline void swap(linb::any& lhs, linb::any& rhs) noexcept
{
lhs.swap(rhs);
}
}
#endif
//
// peglib.h
//
// Copyright (c) 2020 Yuji Hirose. All rights reserved.
// MIT License
//
#ifndef CPPPEGLIB_PEGLIB_H
#define CPPPEGLIB_PEGLIB_H
#ifndef PEGLIB_USE_STD_ANY
#ifdef _MSVC_LANG
#define PEGLIB_USE_STD_ANY _MSVC_LANG >= 201703L
#elif defined(__cplusplus)
#define PEGLIB_USE_STD_ANY __cplusplus >= 201703L
#endif
#endif // PEGLIB_USE_STD_ANY
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstring>
#include <functional>
#include <initializer_list>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <mutex>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#if PEGLIB_USE_STD_ANY
#include <any>
#endif
// guard for older versions of VC++
#ifdef _MSC_VER
#if defined(_MSC_VER) && _MSC_VER < 1900 // Less than Visual Studio 2015
#error "Requires complete C+11 support"
#endif
#endif
namespace peg {
/*-----------------------------------------------------------------------------
* any
*---------------------------------------------------------------------------*/
#if PEGLIB_USE_STD_ANY
using any = std::any;
// Define a function alias to std::any_cast using perfect forwarding
template <typename T, typename... Args>
auto any_cast(Args &&... args)
-> decltype(std::any_cast<T>(std::forward<Args>(args)...)) {
return std::any_cast<T>(std::forward<Args>(args)...);
}
#else
class any {
public:
any() = default;
any(const any &rhs) : content_(rhs.clone()) {}
any(any &&rhs) : content_(rhs.content_) { rhs.content_ = nullptr; }
template <typename T> any(const T &value) : content_(new holder<T>(value)) {}
any &operator=(const any &rhs) {
if (this != &rhs) {
if (content_) { delete content_; }
content_ = rhs.clone();
}
return *this;
}
any &operator=(any &&rhs) {
if (this != &rhs) {
if (content_) { delete content_; }
content_ = rhs.content_;
rhs.content_ = nullptr;
}
return *this;
}
~any() { delete content_; }
bool has_value() const { return content_ != nullptr; }
template <typename T> friend T &any_cast(any &val);
template <typename T> friend const T &any_cast(const any &val);
private:
struct placeholder {
virtual ~placeholder() {}
virtual placeholder *clone() const = 0;
};
template <typename T> struct holder : placeholder {
holder(const T &value) : value_(value) {}
placeholder *clone() const override { return new holder(value_); }
T value_;
};
placeholder *clone() const { return content_ ? content_->clone() : nullptr; }
placeholder *content_ = nullptr;
};
template <typename T> T &any_cast(any &val) {
if (!val.content_) { throw std::bad_cast(); }
auto p = dynamic_cast<any::holder<T> *>(val.content_);
assert(p);
if (!p) { throw std::bad_cast(); }
return p->value_;
}
template <> inline any &any_cast<any>(any &val) { return val; }
template <typename T> const T &any_cast(const any &val) {
assert(val.content_);
auto p = dynamic_cast<any::holder<T> *>(val.content_);
assert(p);
if (!p) { throw std::bad_cast(); }
return p->value_;
}
template <> inline const any &any_cast<any>(const any &val) { return val; }
#endif
/*-----------------------------------------------------------------------------
* scope_exit
*---------------------------------------------------------------------------*/
// This is based on
// "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189".
template <typename EF> struct scope_exit {
explicit scope_exit(EF &&f)
: exit_function(std::move(f)), execute_on_destruction{true} {}
scope_exit(scope_exit &&rhs)
: exit_function(std::move(rhs.exit_function)),
execute_on_destruction{rhs.execute_on_destruction} {
rhs.release();
}
~scope_exit() {
if (execute_on_destruction) { this->exit_function(); }
}
void release() { this->execute_on_destruction = false; }
private:
scope_exit(const scope_exit &) = delete;
void operator=(const scope_exit &) = delete;
scope_exit &operator=(scope_exit &&) = delete;
EF exit_function;
bool execute_on_destruction;
};
template <typename EF>
auto make_scope_exit(EF &&exit_function) -> scope_exit<EF> {
return scope_exit<typename std::remove_reference<EF>::type>(
std::forward<EF>(exit_function));
}
/*-----------------------------------------------------------------------------
* UTF8 functions
*---------------------------------------------------------------------------*/
inline size_t codepoint_length(const char *s8, size_t l) {
if (l) {
auto b = static_cast<uint8_t>(s8[0]);
if ((b & 0x80) == 0) {
return 1;
} else if ((b & 0xE0) == 0xC0) {
return 2;
} else if ((b & 0xF0) == 0xE0) {
return 3;
} else if ((b & 0xF8) == 0xF0) {
return 4;
}
}
return 0;
}
inline size_t encode_codepoint(char32_t cp, char *buff) {
if (cp < 0x0080) {
buff[0] = static_cast<char>(cp & 0x7F);
return 1;
} else if (cp < 0x0800) {
buff[0] = static_cast<char>(0xC0 | ((cp >> 6) & 0x1F));
buff[1] = static_cast<char>(0x80 | (cp & 0x3F));
return 2;
} else if (cp < 0xD800) {
buff[0] = static_cast<char>(0xE0 | ((cp >> 12) & 0xF));
buff[1] = static_cast<char>(0x80 | ((cp >> 6) & 0x3F));
buff[2] = static_cast<char>(0x80 | (cp & 0x3F));
return 3;
} else if (cp < 0xE000) {
// D800 - DFFF is invalid...
return 0;
} else if (cp < 0x10000) {
buff[0] = static_cast<char>(0xE0 | ((cp >> 12) & 0xF));
buff[1] = static_cast<char>(0x80 | ((cp >> 6) & 0x3F));
buff[2] = static_cast<char>(0x80 | (cp & 0x3F));
return 3;
} else if (cp < 0x110000) {
buff[0] = static_cast<char>(0xF0 | ((cp >> 18) & 0x7));
buff[1] = static_cast<char>(0x80 | ((cp >> 12) & 0x3F));
buff[2] = static_cast<char>(0x80 | ((cp >> 6) & 0x3F));
buff[3] = static_cast<char>(0x80 | (cp & 0x3F));
return 4;
}
return 0;
}
inline std::string encode_codepoint(char32_t cp) {
char buff[4];
auto l = encode_codepoint(cp, buff);
return std::string(buff, l);
}
inline bool decode_codepoint(const char *s8, size_t l, size_t &bytes,
char32_t &cp) {
if (l) {
auto b = static_cast<uint8_t>(s8[0]);
if ((b & 0x80) == 0) {
bytes = 1;
cp = b;
return true;
} else if ((b & 0xE0) == 0xC0) {
if (l >= 2) {
bytes = 2;
cp = ((static_cast<char32_t>(s8[0] & 0x1F)) << 6) |
(static_cast<char32_t>(s8[1] & 0x3F));
return true;
}
} else if ((b & 0xF0) == 0xE0) {
if (l >= 3) {
bytes = 3;
cp = ((static_cast<char32_t>(s8[0] & 0x0F)) << 12) |
((static_cast<char32_t>(s8[1] & 0x3F)) << 6) |
(static_cast<char32_t>(s8[2] & 0x3F));
return true;
}
} else if ((b & 0xF8) == 0xF0) {
if (l >= 4) {
bytes = 4;
cp = ((static_cast<char32_t>(s8[0] & 0x07)) << 18) |
((static_cast<char32_t>(s8[1] & 0x3F)) << 12) |
((static_cast<char32_t>(s8[2] & 0x3F)) << 6) |
(static_cast<char32_t>(s8[3] & 0x3F));
return true;
}
}
}
return false;
}
inline size_t decode_codepoint(const char *s8, size_t l, char32_t &out) {
size_t bytes;
if (decode_codepoint(s8, l, bytes, out)) { return bytes; }
return 0;
}
inline char32_t decode_codepoint(const char *s8, size_t l) {
char32_t out = 0;
decode_codepoint(s8, l, out);
return out;
}
inline std::u32string decode(const char *s8, size_t l) {
std::u32string out;
size_t i = 0;
while (i < l) {
auto beg = i++;
while (i < l && (s8[i] & 0xc0) == 0x80) {
i++;
}
out += decode_codepoint(&s8[beg], (i - beg));
}
return out;
}
/*-----------------------------------------------------------------------------
* resolve_escape_sequence
*---------------------------------------------------------------------------*/
inline bool is_hex(char c, int &v) {
if ('0' <= c && c <= '9') {
v = c - '0';
return true;
} else if ('a' <= c && c <= 'f') {
v = c - 'a' + 10;
return true;
} else if ('A' <= c && c <= 'F') {
v = c - 'A' + 10;
return true;
}
return false;
}
inline bool is_digit(char c, int &v) {
if ('0' <= c && c <= '9') {
v = c - '0';
return true;
}
return false;
}
inline std::pair<int, size_t> parse_hex_number(const char *s, size_t n,
size_t i) {
int ret = 0;
int val;
while (i < n && is_hex(s[i], val)) {
ret = static_cast<int>(ret * 16 + val);
i++;
}
return std::make_pair(ret, i);
}
inline std::pair<int, size_t> parse_octal_number(const char *s, size_t n,
size_t i) {
int ret = 0;
int val;
while (i < n && is_digit(s[i], val)) {
ret = static_cast<int>(ret * 8 + val);
i++;
}
return std::make_pair(ret, i);
}
inline std::string resolve_escape_sequence(const char *s, size_t n) {
std::string r;
r.reserve(n);
size_t i = 0;
while (i < n) {
auto ch = s[i];
if (ch == '\\') {
i++;
if (i == n) { throw std::runtime_error("Invalid escape sequence..."); }
switch (s[i]) {
case 'n':
r += '\n';
i++;
break;
case 'r':
r += '\r';
i++;
break;
case 't':
r += '\t';
i++;
break;
case '\'':
r += '\'';
i++;
break;
case '"':
r += '"';
i++;
break;
case '[':
r += '[';
i++;
break;
case ']':
r += ']';
i++;
break;
case '\\':
r += '\\';
i++;
break;
case 'x':
case 'u': {
char32_t cp;
std::tie(cp, i) = parse_hex_number(s, n, i + 1);
r += encode_codepoint(cp);
break;
}
default: {
char32_t cp;
std::tie(cp, i) = parse_octal_number(s, n, i);
r += encode_codepoint(cp);
break;
}
}
} else {
r += ch;
i++;
}
}
return r;
}
/*-----------------------------------------------------------------------------
* Trie
*---------------------------------------------------------------------------*/
class Trie {
public:
Trie() = default;
Trie(const Trie &) = default;
Trie(const std::vector<std::string> &items) {
for (const auto &item : items) {
for (size_t len = 1; len <= item.size(); len++) {
auto last = len == item.size();
std::string s(item.c_str(), len);
auto it = dic_.find(s);
if (it == dic_.end()) {
dic_.emplace(s, Info{last, last});
} else if (last) {
it->second.match = true;
} else {
it->second.done = false;
}
}
}
}
size_t match(const char *text, size_t text_len) const {
size_t match_len = 0;
{
auto done = false;
size_t len = 1;
while (!done && len <= text_len) {
std::string s(text, len);
auto it = dic_.find(s);
if (it == dic_.end()) {
done = true;
} else {
if (it->second.match) { match_len = len; }
if (it->second.done) { done = true; }
}
len += 1;
}
}
return match_len;
}
private:
struct Info {
bool done;
bool match;
};
std::unordered_map<std::string, Info> dic_;
};
/*-----------------------------------------------------------------------------
* PEG
*---------------------------------------------------------------------------*/
/*
* Line information utility function
*/
inline std::pair<size_t, size_t> line_info(const char *start, const char *cur) {
auto p = start;
auto col_ptr = p;
auto no = 1;
while (p < cur) {
if (*p == '\n') {
no++;
col_ptr = p + 1;
}
p++;
}
auto col = p - col_ptr + 1;
return std::make_pair(no, col);
}
/*
* String tag
*/
inline constexpr unsigned int str2tag(const char *str, unsigned int h = 0) {
return (*str == '\0')
? h
: str2tag(str + 1, (h * 33) ^ static_cast<unsigned char>(*str));
}
namespace udl {
inline constexpr unsigned int operator"" _(const char *s, size_t) {
return str2tag(s);
}
} // namespace udl
/*
* Semantic values
*/
struct SemanticValues : protected std::vector<any> {
// Input text
const char *path = nullptr;
const char *ss = nullptr;
const std::vector<size_t> *source_line_index = nullptr;
// Matched string
const char *c_str() const { return s_; }
size_t length() const { return n_; }
std::string str() const { return std::string(s_, n_); }
// Definition name
const std::string &name() const { return name_; }
std::vector<unsigned int> tags;
// Line number and column at which the matched string is
std::pair<size_t, size_t> line_info() const {
const auto &idx = *source_line_index;