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
src
LexBase.cpp
Go to the documentation of this file.
1
#include "
LexBase.h
"
2
#include "
UnicodeCvt.h
"
// bux::C_UnicodeIn
3
#include <cctype>
// isprint()
4
#include <cstring>
// strchr()
5
6
namespace
{
7
8
using namespace
bux
;
9
10
//
11
// Functions
12
//
13
void
appendAsciiLiteral(uint32_t utf32, std::string &dst)
14
{
15
if
(utf32 < 0x100U)
16
{
17
if
(isprint(
int
(utf32)))
18
{
19
if
(strchr(
"\\\'\""
,
int
(utf32)))
20
dst +=
'\\'
;
21
22
dst += char(utf32);
23
}
24
else
switch
(utf32)
25
{
26
case
'\a'
:
27
dst +=
"\\a"
;
28
break
;
29
case
'\b'
:
30
dst +=
"\\b"
;
31
break
;
32
case
'\f'
:
33
dst +=
"\\f"
;
34
break
;
35
case
'\n'
:
36
dst +=
"\\n"
;
37
break
;
38
case
'\r'
:
39
dst +=
"\\r"
;
40
break
;
41
case
'\t'
:
42
dst +=
"\\t"
;
43
break
;
44
case
'\v'
:
45
dst +=
"\\v"
;
46
break
;
47
default
:
48
dst +=
"\\x"
;
49
addAsHex
(dst, utf32);
50
}
51
}
52
else
53
dst +=
to_utf8
(utf32);
54
}
55
56
}
57
58
namespace
bux
{
59
60
//
61
// Functions
62
//
63
bool
operator==
(
const
C_SourcePos
&a,
const
C_SourcePos
&b)
noexcept
64
{
65
return
a.m_Col == b.m_Col &&
66
a.m_Line == b.m_Line &&
67
a.m_Source == b.m_Source;
68
}
69
70
bool
operator<
(
const
C_SourcePos
&a,
const
C_SourcePos
&b)
noexcept
71
{
72
if
(a.m_Source == b.m_Source)
73
return
a.m_Line < b.m_Line || a.m_Line == b.m_Line && a.m_Col < b.m_Col;
74
75
return
false
;
76
}
77
78
std::string
asciiLiteral
(uint32_t utf32)
79
{
80
std::string ret;
81
appendAsciiLiteral(utf32, ret);
82
return
ret;
83
}
84
85
std::string
asciiLiteral
(std::string_view utf8)
86
{
87
C_UnicodeIn
uin{utf8};
88
std::string ret;
89
T_Utf32
u32;
90
while
(
const
auto
t = uin.
get
(u32))
91
{
92
if
(t > 0)
93
appendAsciiLiteral(u32, ret);
94
else
95
// On error
96
throw
std::runtime_error{
"C_UnicodeIn::get() error "
+ std::to_string(t)};
97
}
98
return
ret;
99
}
100
101
void
addAsHex
(std::string &dst, uint32_t ch32)
102
{
103
static
constexpr
const
char
HEX[]{
"0123456789abcdef"
};
104
dst += HEX[ch32/16];
105
dst += HEX[ch32%16];
106
}
107
108
//
109
// Implement Classes
110
//
111
I_LexAttr::~I_LexAttr
() {}
112
113
C_SourcePos::C_SourcePos
(std::string_view sourcePath,
unsigned
line,
unsigned
col)
noexcept
:
114
m_Source
(sourcePath),
m_Line
(line),
m_Col
(col)
115
{
116
}
117
118
C_IntegerLex::C_IntegerLex
(std::string_view numstr,
int
_radix)
noexcept
:
119
m_numStr(numstr), m_radix(_radix)
120
{
121
}
122
123
void
C_IntegerLex::negate
()
124
{
125
m_numStr.insert(std::string::size_type(), 1,
'-'
);
126
}
127
128
void
C_IntegerLex::prependPlus
()
129
{
130
m_numStr.insert(std::string::size_type(), 1,
'+'
);
131
}
132
133
long
long
C_IntegerLex::value_()
const
134
{
135
size_t
end;
136
auto
ret = std::stoll(m_numStr, &end, m_radix);
137
if
(end < m_numStr.size())
138
throw
std::runtime_error{
"Not entirely number "
+ m_numStr};
139
140
return
ret;
141
}
142
143
}
//namespace bux
LexBase.h
UnicodeCvt.h
bux::C_IntegerLex::prependPlus
void prependPlus()
Definition
LexBase.cpp:128
bux::C_IntegerLex::negate
void negate()
Definition
LexBase.cpp:123
bux::C_IntegerLex::C_IntegerLex
C_IntegerLex(std::string_view numstr, int _radix) noexcept
Definition
LexBase.cpp:118
bux::C_UnicodeIn
Definition
UnicodeCvt.h:57
bux::C_UnicodeIn::get
int get(T_Utf32 &c)
Definition
UnicodeCvt.cpp:210
bux
THE common namespace of bux library.
Definition
AtomiX.cpp:3
bux::to_utf8
std::string_view to_utf8(T_Utf32 uc)
Definition
UnicodeCvt.cpp:133
bux::operator==
bool operator==(const C_SourcePos &a, const C_SourcePos &b) noexcept
Equivalence relation.
Definition
LexBase.cpp:63
bux::operator<
bool operator<(const C_SourcePos &a, const C_SourcePos &b) noexcept
Partial relation.
Definition
LexBase.cpp:70
bux::asciiLiteral
std::string asciiLiteral(uint32_t utf32)
Definition
LexBase.cpp:78
bux::T_Utf32
std::uint32_t T_Utf32
UTF-32 to cover the full range of codespace U+0000 ~ U+10FFFF.
Definition
UnicodeCvt.h:40
bux::addAsHex
void addAsHex(std::string &dst, uint32_t ch32)
Definition
LexBase.cpp:101
bux::C_SourcePos
Definition
LexBase.h:39
bux::C_SourcePos::m_Col
unsigned m_Col
Definition
LexBase.h:43
bux::C_SourcePos::m_Source
std::string_view m_Source
Definition
LexBase.h:41
bux::C_SourcePos::m_Line
unsigned m_Line
Definition
LexBase.h:42
bux::C_SourcePos::C_SourcePos
C_SourcePos()=default
bux::I_LexAttr::~I_LexAttr
virtual ~I_LexAttr()=0
Definition
LexBase.cpp:111
Generated by
1.18.0