bux API Reference 1.12.3
Static library of whatever are seen required in general purpose but not directly supported from Modern C++. Or whatever reusable originated from my side projects.
Loading...
Searching...
No Matches
LexBase.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef __BORLANDC__
4# pragma warn -8058
5# pragma warn -8027
6#endif
7
8#include <cstdint> // uint32_t
9#include <stdexcept> // std::runtime_error
10#include <string> // std::string, std::to_string()
11#include <string_view> // std::string_view
12#include <type_traits> // std::is_base_of_v<>, std::is_arithmetic_v<>
13
14namespace bux {
15
16//
17// Constants
18//
19constinit const uint32_t MIN_TOKEN_ID = 0x70000000;
20// MIN_TOKEN_ID is moved out of the enum scope down below due to
21// warning: arithmetic between different enumeration types is deprecated [-Wdeprecated-enum-enum-conversion]
22
23enum: uint32_t
24{
25 // Must-have non-lexical symbols
27 ROOT_NID, // NID for the starting symbol <@>
28
29 // Base of parser-generated identifiers
31};
32
33//
34// Types
35//
36using T_LexID = uint32_t;
37
39{
40 // Data
41 std::string_view m_Source;
42 unsigned m_Line;
43 unsigned m_Col;
44
45 // Nonvirtuals
46 C_SourcePos() = default;
47 C_SourcePos(std::string_view source, unsigned line, unsigned col) noexcept;
48 C_SourcePos(const C_SourcePos &a) = default;
49 C_SourcePos &operator=(const C_SourcePos &a) = default;
50};
51
52template<class T, template<class> class C_Ptr>
54{
55 // Data
56 C_Ptr<T> m_attr;
58
59 // Nonvirtuals
60 C_LexInfoT() = default;
61 C_LexInfoT(C_LexInfoT &another): m_attr(another.m_attr), m_pos(another.m_pos) {}
62 C_LexInfoT &operator=(C_LexInfoT &another) { m_attr = another.m_attr; m_pos = another.m_pos; return *this; }
63 //--------------------------------------------------------
64 operator bool() const { return bool{m_attr}; }
65 operator C_Ptr<T>&() { return m_attr; }
66 operator const C_SourcePos&() const { return m_pos; }
67 template<class T2>
68 operator T2*() const { return dynamic_cast<T2*>(get()); }
69 //--------------------------------------------------------
70 T &operator *() const { return *m_attr; }
71 T *get() const { return m_attr.get(); }
72};
73
74template<class T, template<class> class C_Ptr>
76{
77 // Data
78 C_Ptr<T> &m_lval;
79
80 // Nonvirtuals
81 C_RetLvalT(C_Ptr<T> &lval): m_lval(lval) {}
82 template<class T2>
83 void operator =(C_LexInfoT<T2,C_Ptr> &rval) const { m_lval = rval.m_attr; }
84 template<class T2>
85 void operator =(C_Ptr<T2> &rval) const { m_lval = rval; }
86 void operator =(T *ptr) const { m_lval.reset(ptr); }
87 T &operator *() const { return *m_lval; }
88};
89
93{
94 virtual ~I_LexAttr() = 0;
95};
96
98{
99 // Pure virtuals
100 virtual ~I_Parser() = default;
101 virtual void add(T_LexID token, unsigned line, unsigned col, I_LexAttr *unownedAttr) = 0;
102 virtual std::string_view setSource(std::string_view src) = 0;
103};
104
105template<class F_Pred>
106class C_Screener: public I_Parser
107{
108public:
109
110 // Nonvirtuals
111 C_Screener(I_Parser &parser, F_Pred &&ignore): m_parser(parser), m_ignore(std::move(ignore)) {}
112
113 // Implement I_Parser
114 void add(T_LexID token, unsigned line, unsigned col, I_LexAttr *unownedAttr) override
115 {
116 if (m_ignore(token))
117 // Skip this one
118 delete unownedAttr;
119 else
120 m_parser.add(token, line, col, unownedAttr);
121 }
122 std::string_view setSource(std::string_view src) override
123 {
124 return m_parser.setSource(src);
125 }
126
127private:
128
129 // Data
130 I_Parser &m_parser;
131 const F_Pred m_ignore;
132};
133
134template<T_LexID IGNORED>
135class C_ScreenerNo: public C_Screener<bool(*)(T_LexID)>
136{
137public:
138 constexpr explicit C_ScreenerNo(I_Parser &parser): C_Screener<bool(*)(T_LexID)>(parser, predicate) {}
139private:
140 static bool predicate(T_LexID token) { return token == IGNORED; }
141};
142
143template<class T_Data> requires (!std::is_base_of_v<I_LexAttr,T_Data>)
147{
148 using value_type = std::remove_cv_t<std::remove_reference_t<T_Data>>;
149
150 // Data
152
153 // Ctor
154 template<class...T_Args>
155 explicit constexpr C_LexDataT(T_Args&&...args): m_data(std::forward<T_Args>(args)...) {}
156};
157
159{
160public:
161
162 // Nonvirtuals
163 C_IntegerLex(std::string_view numstr, int _radix) noexcept;
164 void negate();
165 void prependPlus();
166 auto radix() const noexcept { return m_radix; }
167 auto &str() const noexcept { return m_numStr; }
168 template<class T> requires std::is_arithmetic_v<T>
169 auto value() const
170 {
171 const auto t = value_();
172 const auto ret = static_cast<T>(t);
173 if (ret != t)
174 throw std::runtime_error{"Cast overflow " + std::to_string(ret) + " != " + std::to_string(t)};
175
176 return ret;
177 }
178
179private:
180
181 // Data
182 std::string m_numStr;
183 const int m_radix;
184
185 // Nonvirtuals
186 long long value_() const;
187};
188
189//
190// Externals
191//
192bool operator==(const C_SourcePos &a, const C_SourcePos &b) noexcept;
194bool operator<(const C_SourcePos &a, const C_SourcePos &b) noexcept;
196
197std::string asciiLiteral(uint32_t utf32);
198std::string asciiLiteral(std::string_view utf8);
199
200void addAsHex(std::string &dst, uint32_t utf32);
201
202//
203// Function Templates
204//
205template<class T>
206auto createLex(const T &t)
207{
208 return new C_LexDataT<T>(t);
209}
210
211template<class T>
212auto createLex(T &t)
213{
214 return new C_LexDataT<T>(std::move(t));
215}
216
217template<class T, class...T_Args>
218auto createLex(T_Args &&...args)
219{
220 return new C_LexDataT<T>(std::forward<T_Args>(args)...);
221}
222
223} //namespace bux
C_IntegerLex(std::string_view numstr, int _radix) noexcept
Definition LexBase.cpp:118
auto & str() const noexcept
Definition LexBase.h:167
auto radix() const noexcept
Definition LexBase.h:166
auto value() const
Definition LexBase.h:169
constexpr C_ScreenerNo(I_Parser &parser)
Definition LexBase.h:138
C_Screener(I_Parser &parser, F_Pred &&ignore)
Definition LexBase.h:111
void add(T_LexID token, unsigned line, unsigned col, I_LexAttr *unownedAttr) override
Definition LexBase.h:114
std::string_view setSource(std::string_view src) override
Definition LexBase.h:122
THE common namespace of bux library.
Definition AtomiX.cpp:3
@ TOKENGEN_LB
Definition LexBase.h:30
@ TID_EOF
Definition LexBase.h:26
@ ROOT_NID
Definition LexBase.h:27
bool operator==(const C_SourcePos &a, const C_SourcePos &b) noexcept
Equivalence relation.
Definition LexBase.cpp:63
auto createLex(const T &t)
Definition LexBase.h:206
constinit const uint32_t MIN_TOKEN_ID
Definition LexBase.h:19
bool operator<(const C_SourcePos &a, const C_SourcePos &b) noexcept
Partial relation.
Definition LexBase.cpp:70
std::string asciiLiteral(uint32_t utf32)
Definition LexBase.cpp:78
uint32_t T_LexID
Definition LexBase.h:36
void addAsHex(std::string &dst, uint32_t ch32)
Definition LexBase.cpp:101
Render any copyable "type" token attribute on the fly.
Definition LexBase.h:147
std::remove_cv_t< std::remove_reference_t< T_Data > > value_type
Definition LexBase.h:148
constexpr C_LexDataT(T_Args &&...args)
Definition LexBase.h:155
value_type m_data
Definition LexBase.h:151
C_LexInfoT & operator=(C_LexInfoT &another)
Definition LexBase.h:62
std::shared_ptr< const I_LexAttr > m_attr
Definition LexBase.h:56
T & operator*() const
Definition LexBase.h:70
C_LexInfoT(C_LexInfoT &another)
Definition LexBase.h:61
T * get() const
Definition LexBase.h:71
C_LexInfoT()=default
std::shared_ptr< const I_LexAttr > & m_lval
Definition LexBase.h:78
void operator=(C_LexInfoT< T2, C_Ptr > &rval) const
Definition LexBase.h:83
T & operator*() const
Definition LexBase.h:87
C_RetLvalT(C_Ptr< T > &lval)
Definition LexBase.h:81
unsigned m_Col
Definition LexBase.h:43
std::string_view m_Source
Definition LexBase.h:41
unsigned m_Line
Definition LexBase.h:42
C_SourcePos()=default
C_SourcePos & operator=(const C_SourcePos &a)=default
C_SourcePos(const C_SourcePos &a)=default
virtual ~I_LexAttr()=0
Definition LexBase.cpp:111
virtual void add(T_LexID token, unsigned line, unsigned col, I_LexAttr *unownedAttr)=0
virtual std::string_view setSource(std::string_view src)=0
virtual ~I_Parser()=default