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
ParserBase.h
Go to the documentation of this file.
1#pragma once
2
3#include "LexBase.h" // bux::T_LexID, bux::C_LexInfoT<>, ...
4#include "LogLevel.h" // bux::E_LogLevel
5#include <algorithm> // std::lower_bound()
6#include <array> // std::array<>
7#include <functional> // std::function<>
8#include <limits> // std::numeric_limits<>
9#include <string_view> // std::string_view
10#include <type_traits> // std::is_const<>, std::is_pointer<>, std::add_const_t<>
11
12namespace bux {
13
14//
15// Types
16//
17using T_StateID = unsigned;
18
19template<class T, template<class> class C_Ptr>
20using F_GetProducedT = std::function<C_LexInfoT<T,C_Ptr> &(size_t)>;
21
22template<class T, template<class> class C_Ptr>
24{
25public:
26
27 // Nonvirtuals
28 FC_GetRelLexT(const F_GetProducedT<T,C_Ptr> &from, int baseInd): m_from(from), m_baseInd(baseInd)
29 {}
30 auto &operator()(size_t n)
31 { return m_from(size_t(n + m_baseInd)); }
32
33private:
34
35 // Data
36 const F_GetProducedT<T,C_Ptr> m_from;
37 const int m_baseInd;
38};
39
40template<class T_Key, class T_Value>
42{
43 T_Key m_key;
44 T_Value m_value;
45};
46
47template<class T_Key, class T_Value>
48union U_K2V
49{
51 int (*m_conv)(T_Key);
52
53 constexpr U_K2V(const C_KVPair<T_Key,T_Value> *table): m_table(table) {}
54 constexpr U_K2V(int (*conv)(T_Key)): m_conv(conv) {}
55};
56
57template<class T, bool>
59template<class T_Data, class T_Dep>
61
62template<class T>
63struct T_CoConst_<T,true>
64{
65 using type = std::add_const_t<T>;
66};
67template<class T>
68struct T_CoConst_<T,false>
69{
70 using type = T;
71};
72
74{
75public:
76
77 // Nonvirtuals
78 auto getCount(E_LogLevel ll) const { return m_count.at(ll); }
79 void log(E_LogLevel ll, const C_SourcePos &pos, std::string_view message);
80
81protected:
82
83 // Data
84 std::array<unsigned,5> m_count{}; static_assert(LL_VERBOSE+1 == 5);
85
86 // Virtuals
87 virtual std::string toStr(const C_SourcePos &pos) const;
88 virtual void println(const std::string &line) = 0;
89};
90
92{
93public:
94
95 // Ctor
96 C_ParserOStreamCount(std::ostream &out): m_out(out) {}
97
98private:
99
100 // Data
101 std::ostream &m_out;
102
103 // Implement C_ParserLogCount
104 void println(const std::string &line) override;
105};
106
107//
108// Function Templates
109//
110template<class T_Key, class T_Value, class T_ValueOrSize, class T_Traits>
111T_Value index2value(U_K2V<T_Key,T_Value> k2v, T_ValueOrSize valOrSz, T_LexID key)
112{
113 // Constraints
114 static_assert(std::numeric_limits<T_ValueOrSize>::is_signed && sizeof(T_Value) <= sizeof(T_ValueOrSize),
115 "T_Value/T_ValueOrSize criteria");
116
117 if (valOrSz < 0)
118 // (-valOrSz) is size of index array
119 {
120 auto end = k2v.m_table - valOrSz;
121 auto found = std::lower_bound(k2v.m_table, end, key,
122 [](C_KVPair<T_Key,T_Value> a, T_LexID b)->bool {
123 return a.m_key < b;
124 });
125 if (found != end && found->m_key == key)
126 return found->m_value;
127 }
128 else
129 // valOrSz is THE value
130 {
131 const auto ind = k2v.m_conv(T_Key(key));
132 if (ind >= 0)
133 return T_Traits::map(valOrSz, ind);
134 }
135 return T_Traits::valueError();
136}
137
138template<class T>
139auto &unlex(I_LexAttr &lex)
140{
141 return dynamic_cast<C_LexDataT<T>&>(lex).m_data;
142}
143
144template<class T>
145auto &unlex(const I_LexAttr &lex)
146{
147 return dynamic_cast<const C_LexDataT<T>&>(lex).m_data;
148}
149
150template<class T_Data, class T_Lex, template<class> class C_Ptr>
151auto &unlex(const C_Ptr<T_Lex> &lex)
152{
153 return dynamic_cast<T_CoConst<C_LexDataT<T_Data>,T_Lex>&>(*lex).m_data;
154}
155
156template<class T_Data, class T_Lex, template<class> class C_Ptr>
158{
159 return unlex<T_Data>(lex.m_attr);
160}
161
162template<class T_Data, class T_Lex, template<class> class C_Ptr>
163T_CoConst<T_Data,T_Lex> *tryUnlex(const C_Ptr<T_Lex> &lex)
164{
165 if (auto p = dynamic_cast<T_CoConst<C_LexDataT<T_Data>,T_Lex>*>(lex.get()))
166 return &p->m_data;
167
168 return nullptr;
169}
170
171template<class T_Data, class T_Lex, template<class> class C_Ptr>
173{
174 return tryUnlex<T_Data>(lex.m_attr);
175}
176
177template<class T_Lex, template<class> class C_Ptr>
179{
180 auto &t = dynamic_cast<const C_IntegerLex&>(*lex);
181 return t.value<int>();
182}
183
184} //namespace bux
auto value() const
Definition LexBase.h:168
std::array< unsigned, 5 > m_count
Definition ParserBase.h:84
virtual void println(const std::string &line)=0
void log(E_LogLevel ll, const C_SourcePos &pos, std::string_view message)
auto getCount(E_LogLevel ll) const
Definition ParserBase.h:78
virtual std::string toStr(const C_SourcePos &pos) const
C_ParserOStreamCount(std::ostream &out)
Definition ParserBase.h:96
auto & operator()(size_t n)
Definition ParserBase.h:30
FC_GetRelLexT(const F_GetProducedT< T, C_Ptr > &from, int baseInd)
Definition ParserBase.h:28
THE common namespace of bux library.
Definition AtomiX.cpp:3
int toInt(const C_LexInfoT< T_Lex, C_Ptr > &lex)
Definition ParserBase.h:178
std::function< C_LexInfoT< T, C_Ptr > &(size_t)> F_GetProducedT
Definition ParserBase.h:20
E_LogLevel
Definition LogLevel.h:9
auto & unlex(I_LexAttr &lex)
Definition ParserBase.h:139
T_CoConst< T_Data, T_Lex > * tryUnlex(const C_Ptr< T_Lex > &lex)
Definition ParserBase.h:163
uint32_t T_LexID
Definition LexBase.h:35
typename T_CoConst_< T_Data, std::is_const< T_Dep >::value >::type T_CoConst
Definition ParserBase.h:60
T_Value index2value(U_K2V< T_Key, T_Value > k2v, T_ValueOrSize valOrSz, T_LexID key)
Definition ParserBase.h:111
unsigned T_StateID
Definition ParserBase.h:17
T_Value m_value
Definition ParserBase.h:44
Render any copyable "type" token attribute on the fly.
Definition LexBase.h:146
value_type m_data
Definition LexBase.h:150
C_Ptr< T > m_attr
Definition LexBase.h:55
std::add_const_t< T > type
Definition ParserBase.h:65
const C_KVPair< T_Key, T_Value > * m_table
Definition ParserBase.h:50
constexpr U_K2V(int(*conv)(T_Key))
Definition ParserBase.h:54
int(* m_conv)(T_Key)
Definition ParserBase.h:51
constexpr U_K2V(const C_KVPair< T_Key, T_Value > *table)
Definition ParserBase.h:53