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