bux API Reference
1.12.6
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.
Toggle main menu visibility
Loading...
Searching...
No Matches
include
bux
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
12
namespace
bux
{
13
14
//
15
// Types
16
//
17
using
T_StateID
= unsigned;
18
19
template
<
class
T,
template
<
class
>
class
C_Ptr>
20
using
F_GetProducedT
= std::function<C_LexInfoT<T,C_Ptr> &(size_t)>;
21
22
template
<
class
T,
template
<
class
>
class
C_Ptr>
23
class
FC_GetRelLexT
24
{
25
public
:
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
33
private
:
34
35
// Data
36
const
F_GetProducedT<T,C_Ptr>
m_from;
37
const
int
m_baseInd;
38
};
39
40
template
<
class
T_Key,
class
T_Value>
41
struct
C_KVPair
42
{
43
T_Key
m_key
;
44
T_Value
m_value
;
45
};
46
47
template
<
class
T_Key,
class
T_Value>
48
union
U_K2V
49
{
50
const
C_KVPair<T_Key,T_Value>
*
m_table
;
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
57
template
<
class
T,
bool
>
58
struct
T_CoConst_
;
59
template
<
class
T_Data,
class
T_Dep>
60
using
T_CoConst
=
typename
T_CoConst_<T_Data,std::is_const<T_Dep>::value
>
::type
;
61
62
template
<
class
T>
63
struct
T_CoConst_
<T,true>
64
{
65
using
type
= std::add_const_t<T>;
66
};
67
template
<
class
T>
68
struct
T_CoConst_
<T,false>
69
{
70
using
type
= T;
71
};
72
73
class
C_ParserLogCount
74
{
75
public
:
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
81
protected
:
82
83
// Data
84
std::array<unsigned,LL_VERBOSE+1>
m_count
{};
// Presumably LL_VERBOSE is the highest log level
85
86
// Virtuals
87
virtual
std::string
toStr
(
const
C_SourcePos
&pos)
const
;
88
virtual
void
println
(
const
std::string &line) = 0;
89
};
90
91
class
C_ParserOStreamCount
:
public
C_ParserLogCount
92
{
93
public
:
94
95
// Ctor
96
C_ParserOStreamCount
(std::ostream &out): m_out(out) {}
97
98
private
:
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
//
110
template
<
class
T_Key,
class
T_Value,
class
T_ValueOrSize,
class
T_Traits>
111
T_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
138
template
<
class
T>
139
auto
&
unlex
(
I_LexAttr
&lex)
140
{
141
return
dynamic_cast<
C_LexDataT<T>
&
>
(lex).m_data;
142
}
143
144
template
<
class
T>
145
auto
&
unlex
(
const
I_LexAttr
&lex)
146
{
147
return
dynamic_cast<
const
C_LexDataT<T>
&
>
(lex).m_data;
148
}
149
150
template
<
class
T_Data,
class
T_Lex,
template
<
class
>
class
C_Ptr>
151
auto
&
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
156
template
<
class
T_Data,
class
T_Lex,
template
<
class
>
class
C_Ptr>
157
auto
&
unlex
(
const
C_LexInfoT<T_Lex,C_Ptr>
&lex)
158
{
159
return
unlex<T_Data>
(lex.
m_attr
);
160
}
161
162
template
<
class
T_Data,
class
T_Lex,
template
<
class
>
class
C_Ptr>
163
T_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
171
template
<
class
T_Data,
class
T_Lex,
template
<
class
>
class
C_Ptr>
172
auto
tryUnlex
(
const
C_LexInfoT<T_Lex,C_Ptr>
&lex)
173
{
174
return
tryUnlex<T_Data>
(lex.
m_attr
);
175
}
176
177
template
<
class
T_Lex,
template
<
class
>
class
C_Ptr>
178
int
toInt
(
const
C_LexInfoT<T_Lex,C_Ptr>
&lex)
179
{
180
auto
&t =
dynamic_cast<
const
C_IntegerLex
&
>
(*lex);
181
return
t.
value
<
int
>();
182
}
183
184
}
//namespace bux
LexBase.h
LogLevel.h
bux::C_IntegerLex
Definition
LexBase.h:159
bux::C_IntegerLex::value
auto value() const
Definition
LexBase.h:169
bux::C_ParserLogCount
Definition
ParserBase.h:74
bux::C_ParserLogCount::println
virtual void println(const std::string &line)=0
bux::C_ParserLogCount::m_count
std::array< unsigned, LL_VERBOSE+1 > m_count
Definition
ParserBase.h:84
bux::C_ParserLogCount::log
void log(E_LogLevel ll, const C_SourcePos &pos, std::string_view message)
Definition
ParserBase.cpp:11
bux::C_ParserLogCount::getCount
auto getCount(E_LogLevel ll) const
Definition
ParserBase.h:78
bux::C_ParserLogCount::toStr
virtual std::string toStr(const C_SourcePos &pos) const
Definition
ParserBase.cpp:18
bux::C_ParserOStreamCount::C_ParserOStreamCount
C_ParserOStreamCount(std::ostream &out)
Definition
ParserBase.h:96
bux::FC_GetRelLexT::operator()
auto & operator()(size_t n)
Definition
ParserBase.h:30
bux::FC_GetRelLexT::FC_GetRelLexT
FC_GetRelLexT(const F_GetProducedT< T, C_Ptr > &from, int baseInd)
Definition
ParserBase.h:28
int
type
bux
THE common namespace of bux library.
Definition
AtomiX.cpp:3
bux::toInt
int toInt(const C_LexInfoT< T_Lex, C_Ptr > &lex)
Definition
ParserBase.h:178
bux::F_GetProducedT
std::function< C_LexInfoT< T, C_Ptr > &(size_t)> F_GetProducedT
Definition
ParserBase.h:20
bux::E_LogLevel
E_LogLevel
Definition
LogLevel.h:9
bux::unlex
auto & unlex(I_LexAttr &lex)
Definition
ParserBase.h:139
bux::tryUnlex
T_CoConst< T_Data, T_Lex > * tryUnlex(const C_Ptr< T_Lex > &lex)
Definition
ParserBase.h:163
bux::T_LexID
uint32_t T_LexID
Definition
LexBase.h:36
bux::T_CoConst
typename T_CoConst_< T_Data, std::is_const< T_Dep >::value >::type T_CoConst
Definition
ParserBase.h:60
bux::index2value
T_Value index2value(U_K2V< T_Key, T_Value > k2v, T_ValueOrSize valOrSz, T_LexID key)
Definition
ParserBase.h:111
bux::T_StateID
unsigned T_StateID
Definition
ParserBase.h:17
bux::C_KVPair
Definition
ParserBase.h:42
bux::C_KVPair::m_key
T_Key m_key
Definition
ParserBase.h:43
bux::C_KVPair::m_value
T_Value m_value
Definition
ParserBase.h:44
bux::C_LexDataT
Render any copyable "type" token attribute on the fly.
Definition
LexBase.h:147
bux::C_LexInfoT
Definition
LexBase.h:54
bux::C_LexInfoT::m_attr
C_Ptr< T > m_attr
Definition
LexBase.h:56
bux::C_SourcePos
Definition
LexBase.h:39
bux::I_LexAttr
Definition
LexBase.h:93
bux::T_CoConst_< T, false >::type
T type
Definition
ParserBase.h:70
bux::T_CoConst_< T, true >::type
std::add_const_t< T > type
Definition
ParserBase.h:65
bux::T_CoConst_
Definition
ParserBase.h:58
bux::U_K2V
Definition
ParserBase.h:49
bux::U_K2V::m_table
const C_KVPair< T_Key, T_Value > * m_table
Definition
ParserBase.h:50
bux::U_K2V::U_K2V
constexpr U_K2V(int(*conv)(T_Key))
Definition
ParserBase.h:54
bux::U_K2V::m_conv
int(*) m_conv(T_Key)
Definition
ParserBase.h:51
bux::U_K2V::U_K2V
constexpr U_K2V(const C_KVPair< T_Key, T_Value > *table)
Definition
ParserBase.h:53
Generated by
1.18.0