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