-
Notifications
You must be signed in to change notification settings - Fork 0
/
tjson.cpp
420 lines (351 loc) · 8.81 KB
/
tjson.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include "tjson.h"
#include <algorithm>
#include <cstdio>
#include <locale>
#include <ostream>
#include <regex>
#include <sstream>
namespace tjson {
/*static*/ const Val Val::INVALID;
// -
std::string
escape(const std::string& in)
{
std::string out;
out.reserve(in.size() + 2); // Only reserve the required quotes.
out += '"';
for (const auto c : in) {
if (c == '"' || c == '\\') {
out += '\\';
}
out += c;
}
out += '"';
return out;
}
bool
unescape(const std::string& in, std::string* const out)
{
if (in.size() < 2)
return false;
if (in[0] != '"' || in[in.size()-1] != '"')
return false;
std::string wip;
wip.reserve(in.size() - 2);
bool in_escape = false;
auto itr = in.c_str() + 1;
const auto end = in.c_str() + in.size() - 1;
for (; itr != end; ++itr) {
const auto& c = *itr;
if (!in_escape && c == '\\') {
in_escape = true;
} else {
in_escape = false;
wip += c;
}
}
if (in_escape)
return false;
*out = std::move(wip);
return true;
}
// -
struct Token final
{
enum class Type : uint8_t {
MALFORMED = '?',
WHITESPACE = '_',
STRING = '"',
WORD = 'a',
SYMBOL = '$',
};
const char* begin;
const char* end;
uint64_t line_num;
uint64_t line_pos;
Type type;
bool operator==(const char* const r) const {
const auto len = strlen(r);
if (len != end - begin)
return false;
return std::equal(begin, end, r);
}
std::string str() const { return std::string(begin, end); }
};
const std::regex RE_WHITESPACE(R"([ \t\n\r]+)");
const std::regex RE_STRING(R"_("(:?[^"\\]*(:?\\.)*)*")_");
const std::regex RE_WORD(R"([A-Za-z0-9_+\-.]+)");
const std::regex RE_SYMBOL(R"([{:,}\[\]])");
static bool
next_token_from_regex(const std::regex& regex, Token* const out)
{
std::cmatch res;
if (!std::regex_search(out->begin, out->end, res, regex,
std::regex_constants::match_continuous))
{
return false;
}
out->end = out->begin + res.length();
return true;
}
class TokenGen final
{
Token meta_token_;
public:
TokenGen(const char* const begin, const char* const end)
: meta_token_{begin, end, 1, 1, Token::Type::MALFORMED}
{ }
Token Next() {
auto ret = meta_token_;
if (next_token_from_regex(RE_WHITESPACE, &ret)) {
ret.type = Token::Type::WHITESPACE;
} else if (next_token_from_regex(RE_STRING, &ret)) {
ret.type = Token::Type::STRING;
} else if (next_token_from_regex(RE_WORD, &ret)) {
ret.type = Token::Type::WORD;
} else if (next_token_from_regex(RE_SYMBOL, &ret)) {
ret.type = Token::Type::SYMBOL;
}
meta_token_.begin = ret.end;
for (auto itr = ret.begin; itr != ret.end; ++itr) {
if (*itr == '\n') {
meta_token_.line_num += 1;
meta_token_.line_pos = 0;
}
meta_token_.line_pos += 1;
}
return ret;
}
Token NextNonWS() {
while (true) {
const auto ret = Next();
if (ret.type != Token::Type::WHITESPACE) {
//#define SPEW_TOKENS
#ifdef SPEW_TOKENS
fprintf(stderr, "%c @ L%llu:%llu: %s\n\n", int(ret.type), ret.line_num,
ret.line_pos, ret.str().c_str());
#endif
return ret;
}
}
}
};
// -
std::unique_ptr<Val>
read(const char* const begin, const char* const end,
std::string* const out_err)
{
TokenGen tok_gen(begin, end);
auto ret = read(&tok_gen, out_err);
return std::move(ret);
}
std::unique_ptr<Val>
read(TokenGen* const tok_gen,
std::string* const out_err)
{
const auto fn_err = [&](const Token& tok, const char* const expected) {
auto str = tok.str();
if (str.length() > 20) {
str.resize(20);
}
std::ostringstream err;
err << "Error: L" << tok.line_num << ":" << tok.line_pos << ": Expected "
<< expected << ", got: \"" << str << "\".";
*out_err = err.str();
};
const auto fn_is_expected = [&](const Token& tok,
const char* const expected_str = nullptr)
{
std::string expl_expected_str;
const char* expl_expected;
if (expected_str) {
if (tok == expected_str)
return true;
expl_expected_str = std::string("\"") + expected_str + "\"";
expl_expected = expl_expected_str.c_str();
} else {
if (tok.type != Token::Type::MALFORMED)
return true;
expl_expected = "!MALFORMED";
}
fn_err(tok, expl_expected);
return false;
};
auto ret = std::unique_ptr<Val>(new Val);
auto& cur = *ret;
const auto tok = tok_gen->NextNonWS();
if (tok == "{") {
cur.set_dict();
auto peek_gen = *tok_gen;
const auto peek = peek_gen.NextNonWS();
if (peek == "}") {
*tok_gen = peek_gen;
} else {
while (true) {
const auto k = tok_gen->NextNonWS();
if (k.type != Token::Type::STRING) {
fn_err(k, "STRING");
return nullptr;
}
const auto colon = tok_gen->NextNonWS();
if (!fn_is_expected(colon, ":"))
return nullptr;
auto v = read(tok_gen, out_err);
if (!v)
return nullptr;
std::string key_name;
(void)unescape(k.str(), &key_name);
cur[key_name] = std::move(v); // Overwrite.
const auto comma = tok_gen->NextNonWS();
if (comma == "}")
break;
if (!fn_is_expected(comma, ","))
return nullptr;
continue;
}
}
return ret;
}
if (tok == "[") {
cur.set_list();
auto peek_gen = *tok_gen;
const auto peek = peek_gen.NextNonWS();
if (peek == "]") {
*tok_gen = peek_gen;
} else {
size_t i = 0;
while (true) {
auto v = read(tok_gen, out_err);
if (!v)
return nullptr;
cur[i] = std::move(v);
i += 1;
const auto comma = tok_gen->NextNonWS();
if (comma == "]")
break;
if (!fn_is_expected(comma, ","))
return nullptr;
continue;
}
}
return ret;
}
if (!fn_is_expected(tok))
return nullptr;
cur.val() = tok.str();
return ret;
}
void
write(const Val& root, std::ostream* const out, const std::string& indent)
{
if (root.is_dict()) {
const auto& d = root.dict();
*out << "{";
if (!d.size()) {
*out << "}";
return;
}
const auto indent_plus = indent + " ";
bool needsComma = false;
for (const auto& kv : d) {
if (needsComma) {
*out << ",";
}
*out << "\n" << indent_plus << escape(kv.first) << ": ";
kv.second->write(out, indent_plus);
needsComma = true;
}
*out << "\n" << indent << "}";
return;
}
if (root.is_list()) {
const auto& l = root.list();
*out << "[";
if (!l.size()) {
*out << "]";
return;
}
const auto indent_plus = indent + " ";
bool needsComma = false;
for (const auto& v : l) {
if (needsComma) {
*out << ",";
}
*out << "\n" << indent_plus;
v->write(out, indent_plus);
needsComma = true;
}
*out << "\n" << indent << "]";
return;
}
*out << root.val();
}
// -
bool
Val::as_number(double* const out) const
{
auto stream = std::istringstream(val_);
// Set the C locale, otherwise 1.5 parses as 1.0 in decimal-comma locales!
const auto locale = std::locale::classic();
stream.imbue(locale);
double ret;
stream >> ret;
if (stream.fail())
return false;
*out = ret;
return true;
}
void
Val::val(const double x)
{
auto stream = std::ostringstream();
const auto locale = std::locale::classic();
stream.imbue(locale);
stream << x;
val() = stream.str();
}
// -
void
Val::reset()
{
dict_.clear();
is_dict_ = false;
list_.clear();
is_list_ = false;
val_ = "";
}
// -
const Val&
Val::operator[](const std::string& x) const
{
const auto itr = dict_.find(x);
if (itr == dict_.end())
return INVALID;
return *(itr->second.get());
}
std::unique_ptr<Val>&
Val::operator[](const std::string& x)
{
set_dict();
auto& val = dict_[x];
val.reset(new Val);
return val;
}
// -
const Val&
Val::operator[](const size_t i) const
{
if (i >= list_.size())
return INVALID;
return *(list_[i].get());
}
std::unique_ptr<Val>&
Val::operator[](const size_t i)
{
set_list();
while (i >= list_.size()) {
list_.push_back(std::unique_ptr<Val>(new Val));
}
return list_[i];
}
} // namespace tjson