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
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 {
56 char buf[10];
57 const auto t = std::to_chars(buf, buf+sizeof(buf), utf32, 16);
58 if (t.ec == std::errc())
59 // On success
60 {
61 const auto n = t.ptr - buf;
62 dst += n <= 4? "\\u": "\\U";
63 if (const auto nonzeros = n % 4)
64 dst.append(size_t(4-nonzeros), '0');
65
66 dst.append(buf, t.ptr);
67 }
68 else
69 {
70 const auto ecode = make_error_code(t.ec);
71 RUNTIME_ERROR("std::to_chars() error: {}:{}", ecode.category().name(), ecode.value());
72 }
73 }
74}
75
76}
77
78namespace bux {
79
80//
81// Functions
82//
83bool operator==(const C_SourcePos &a, const C_SourcePos &b) noexcept
84{
85 return a.m_Col == b.m_Col &&
86 a.m_Line == b.m_Line &&
87 a.m_Source == b.m_Source;
88}
89
90bool operator<(const C_SourcePos &a, const C_SourcePos &b) noexcept
91{
92 if (a.m_Source == b.m_Source)
93 return a.m_Line < b.m_Line || a.m_Line == b.m_Line && a.m_Col < b.m_Col;
94
95 return false;
96}
97
98std::string asciiLiteral(uint32_t utf32)
99{
100 std::string ret;
101 appendAsciiLiteral(utf32, ret);
102 return ret;
103}
104
105std::string asciiLiteral(std::string_view utf8)
106{
107 C_UnicodeIn uin{utf8};
108 std::string ret;
109 T_Utf32 u32;
110 while (const auto t = uin.get(u32))
111 {
112 if (t > 0)
113 appendAsciiLiteral(u32, ret);
114 else
115 // On error
116 RUNTIME_ERROR("C_UnicodeIn::get() error {}", t);
117 }
118 return ret;
119}
120
121void addAsHex(std::string &dst, uint32_t ch32)
122{
123 static constexpr const char HEX[]{"0123456789abcdef"};
124 dst += HEX[ch32/16];
125 dst += HEX[ch32%16];
126}
127
128//
129// Implement Classes
130//
132
133C_SourcePos::C_SourcePos(std::string_view sourcePath, unsigned line, unsigned col) noexcept:
134 m_Source(sourcePath), m_Line(line), m_Col(col)
135{
136}
137
138C_IntegerLex::C_IntegerLex(std::string_view numstr, int _radix) noexcept:
139 m_numStr(numstr), m_radix(_radix)
140{
141}
142
144{
145 m_numStr.insert(std::string::size_type(), 1, '-');
146}
147
149{
150 m_numStr.insert(std::string::size_type(), 1, '+');
151}
152
153long long C_IntegerLex::value_() const
154{
155 size_t end;
156 auto ret = std::stoll(m_numStr, &end, m_radix);
157 if (end < m_numStr.size())
158 RUNTIME_ERROR("Not entirely number {}", m_numStr);
159
160 return ret;
161}
162
163} //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:138
THE common namespace of bux library.
Definition AtomiX.cpp:3
bool operator==(const C_SourcePos &a, const C_SourcePos &b) noexcept
Equivalence relation.
Definition LexBase.cpp:83
bool operator<(const C_SourcePos &a, const C_SourcePos &b) noexcept
Partial relation.
Definition LexBase.cpp:90
std::string asciiLiteral(uint32_t utf32)
Definition LexBase.cpp:98
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:121
C_SourcePos()=default
virtual ~I_LexAttr()=0
Definition LexBase.cpp:131