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.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
6namespace {
7
8using namespace bux;
9
10//
11// Functions
12//
13void 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
58namespace bux {
59
60//
61// Functions
62//
63bool 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
70bool 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
78std::string asciiLiteral(uint32_t utf32)
79{
80 std::string ret;
81 appendAsciiLiteral(utf32, ret);
82 return ret;
83}
84
85std::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
101void 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//
112
113C_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
118C_IntegerLex::C_IntegerLex(std::string_view numstr, int _radix) noexcept:
119 m_numStr(numstr), m_radix(_radix)
120{
121}
122
124{
125 m_numStr.insert(std::string::size_type(), 1, '-');
126}
127
129{
130 m_numStr.insert(std::string::size_type(), 1, '+');
131}
132
133long 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
C_IntegerLex(std::string_view numstr, int _radix) noexcept
Definition LexBase.cpp:118
THE common namespace of bux library.
Definition AtomiX.cpp:3
std::string_view to_utf8(T_Utf32 uc)
bool operator==(const C_SourcePos &a, const C_SourcePos &b) noexcept
Equivalence relation.
Definition LexBase.cpp:63
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
std::uint32_t T_Utf32
UTF-32 to cover the full range of codespace U+0000 ~ U+10FFFF.
Definition UnicodeCvt.h:39
void addAsHex(std::string &dst, uint32_t ch32)
Definition LexBase.cpp:101
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
virtual ~I_LexAttr()=0
Definition LexBase.cpp:111