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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
|
#include "test_suite_events.hpp"
#include "test_suite_common.hpp"
#ifndef RYML_SINGLE_HEADER
#include <c4/yml/detail/stack.hpp>
#endif
namespace c4 {
namespace yml {
namespace /*anon*/ {
struct ScalarType
{
typedef enum {
PLAIN = 0,
SQUOTED,
DQUOTED,
LITERAL,
FOLDED
} ScalarType_e;
ScalarType_e val = PLAIN;
bool operator== (ScalarType_e v) const { return val == v; }
bool operator!= (ScalarType_e v) const { return val != v; }
ScalarType& operator= (ScalarType_e v) { val = v; return *this; }
csubstr to_str() const
{
switch(val)
{
case ScalarType::PLAIN: return csubstr("PLAIN");
case ScalarType::SQUOTED: return csubstr("SQUOTED");
case ScalarType::DQUOTED: return csubstr("DQUOTED");
case ScalarType::LITERAL: return csubstr("LITERAL");
case ScalarType::FOLDED: return csubstr("FOLDED");
}
C4_ERROR("");
return csubstr("");
}
bool is_quoted() const { return val == ScalarType::SQUOTED || val == ScalarType::DQUOTED; }
};
struct OptionalScalar
{
csubstr val = {};
bool was_set = false;
inline operator csubstr() const { return get(); }
inline operator bool() const { return was_set; }
void operator= (csubstr v) { val = v; was_set = true; }
csubstr get() const { RYML_ASSERT(was_set); return val; }
};
#if RYML_NFO
size_t to_chars(c4::substr buf, OptionalScalar const& s)
{
if(!s)
return 0u;
if(s.val.len <= buf.len)
memcpy(buf.str, s.val.str, s.val.len);
return s.val.len;
}
#endif
csubstr filtered_scalar(csubstr str, ScalarType scalar_type, Tree *tree)
{
(void)scalar_type;
csubstr tokens[] = {R"(\n)", R"(\t)", R"(\\)"};
if(!str.first_of_any_iter(std::begin(tokens), std::end(tokens)))
return str;
substr buf = tree->alloc_arena(str.len); // we are going to always replace with less characters
size_t strpos = 0;
size_t bufpos = 0;
auto append_str = [&](size_t pos){
csubstr rng = str.range(strpos, pos);
memcpy(buf.str + bufpos, rng.str, rng.len);
bufpos += rng.len;
strpos = pos;
};
size_t i;
auto append_chars = [&](csubstr s, size_t skipstr){
memcpy(buf.str + bufpos, s.str, s.len);
bufpos += s.len;
i += skipstr - 1; // incremented at the loop
strpos += skipstr;
};
for(i = 0; i < str.len; ++i)
{
char curr = str[i];
char next1 = i+1 < str.len ? str[i+1] : '\0';
if(curr == '\\')
{
if(next1 == '\\')
{
char next2 = i+2 < str.len ? str[i+2] : '\0';
if(next2 == 'n')
{
append_str(i);
append_chars(R"(\n)", 3u); // '\\n' -> '\n'
}
else if(next2 == 't')
{
append_str(i);
append_chars(R"(\t)", 3u); // '\\t' -> '\t'
}
else
{
append_str(i);
append_chars(R"(\)", 2u); // '\\' -> '\'
}
}
else if(next1 == 'n')
{
append_str(i);
append_chars("\n", 2u);
}
else if(next1 == 't')
{
append_str(i);
append_chars("\t", 2u);
}
}
}
append_str(str.len);
buf = buf.first(bufpos);
_nfo_logf("filtering: result=~~~{}~~~", buf);
return buf;
}
struct Scalar
{
OptionalScalar scalar = {};
OptionalScalar anchor = {};
OptionalScalar ref = {};
OptionalScalar tag = {};
ScalarType type = {};
inline operator bool() const { if(anchor || tag) { RYML_ASSERT(scalar); } return scalar.was_set; }
void add_key_props(Tree *tree, size_t node) const
{
if(ref)
{
_nfo_logf("node[{}]: set key ref: '{}'", node, ref);
tree->set_key_ref(node, ref);
}
if(anchor)
{
_nfo_logf("node[{}]: set key anchor: '{}'", node, anchor);
tree->set_key_anchor(node, anchor);
}
if(tag)
{
csubstr ntag = normalize_tag(tag);
_nfo_logf("node[{}]: set key tag: '{}' -> '{}'", node, tag, ntag);
tree->set_key_tag(node, ntag);
}
if(type.is_quoted())
{
_nfo_logf("node[{}]: set key as quoted", node);
tree->_add_flags(node, KEYQUO);
}
}
void add_val_props(Tree *tree, size_t node) const
{
if(ref)
{
_nfo_logf("node[{}]: set val ref: '{}'", node, ref);
tree->set_val_ref(node, ref);
}
if(anchor)
{
_nfo_logf("node[{}]: set val anchor: '{}'", node, anchor);
tree->set_val_anchor(node, anchor);
}
if(tag)
{
csubstr ntag = normalize_tag(tag);
_nfo_logf("node[{}]: set val tag: '{}' -> '{}'", node, tag, ntag);
tree->set_val_tag(node, ntag);
}
if(type.is_quoted())
{
_nfo_logf("node[{}]: set val as quoted", node);
tree->_add_flags(node, VALQUO);
}
}
csubstr filtered_scalar(Tree *tree) const
{
return ::c4::yml::filtered_scalar(scalar, type, tree);
}
};
csubstr parse_anchor_and_tag(csubstr tokens, OptionalScalar *anchor, OptionalScalar *tag)
{
*anchor = OptionalScalar{};
*tag = OptionalScalar{};
if(tokens.begins_with('&'))
{
size_t pos = tokens.first_of(' ');
if(pos == (size_t)csubstr::npos)
{
*anchor = tokens.sub(1);
tokens = {};
}
else
{
*anchor = tokens.first(pos).sub(1);
tokens = tokens.right_of(pos);
}
_nfo_logf("anchor: {}", *anchor);
}
if(tokens.begins_with('<'))
{
size_t pos = tokens.find('>');
RYML_ASSERT(pos != (size_t)csubstr::npos);
*tag = tokens.first(pos + 1);
tokens = tokens.right_of(pos).triml(' ');
_nfo_logf("tag: {}", *tag);
}
return tokens;
}
} // namespace /*anon*/
void EventsParser::parse(csubstr src, Tree *C4_RESTRICT tree_)
{
struct ParseLevel { size_t tree_node; };
detail::stack<ParseLevel> m_stack = {};
Tree &C4_RESTRICT tree = *tree_;
size_t linenum = 0;
Scalar key = {};
_nfo_logf("parsing events! src:\n{}", src);
for(csubstr line : src.split('\n'))
{
line = line.trimr('\r');
line = line.triml(' ');
_nfo_printf("\n\n-----------------------\n");
_nfo_llog("");
_nfo_logf("line[{}]: top={} type={}", linenum, m_stack.empty() ? tree.root_id() : m_stack.top().tree_node, NodeType::type_str(tree.type(m_stack.empty() ? tree.root_id() : m_stack.top().tree_node)));
if(line.begins_with("=VAL "))
{
line = line.stripl("=VAL ");
ASSERT_GE(m_stack.size(), 0u);
Scalar curr = {};
line = parse_anchor_and_tag(line, &curr.anchor, &curr.tag);
if(line.begins_with('"'))
{
_nfo_llog("double-quoted scalar!");
curr.scalar = line.sub(1);
curr.type = ScalarType::DQUOTED;
}
else if(line.begins_with('\''))
{
_nfo_llog("single-quoted scalar!");
curr.scalar = line.sub(1);
curr.type = ScalarType::SQUOTED;
}
else if(line.begins_with('|'))
{
_nfo_llog("block literal scalar!");
curr.scalar = line.sub(1);
curr.type = ScalarType::LITERAL;
}
else if(line.begins_with('>'))
{
_nfo_llog("block folded scalar!");
curr.scalar = line.sub(1);
curr.type = ScalarType::FOLDED;
}
else
{
_nfo_llog("plain scalar");
ASSERT_TRUE(line.begins_with(':'));
curr.scalar = line.sub(1);
}
_nfo_logf("parsed scalar: '{}'", curr.scalar);
if(m_stack.empty())
{
_nfo_log("stack was empty, pushing root as DOC...");
//tree._p(tree.root_id())->m_type.add(DOC);
m_stack.push({tree.root_id()});
}
ParseLevel &top = m_stack.top();
if(tree.is_seq(top.tree_node))
{
_nfo_logf("is seq! seq_id={}", top.tree_node);
ASSERT_FALSE(key);
ASSERT_TRUE(curr);
_nfo_logf("seq[{}]: adding child", top.tree_node);
size_t node = tree.append_child(top.tree_node);
NodeType_e as_doc = tree.is_stream(top.tree_node) ? DOC : NOTYPE;
_nfo_logf("seq[{}]: child={} val='{}' as_doc=", top.tree_node, node, curr.scalar, NodeType::type_str(as_doc));
tree.to_val(node, curr.filtered_scalar(&tree), as_doc);
curr.add_val_props(&tree, node);
}
else if(tree.is_map(top.tree_node))
{
_nfo_logf("is map! map_id={}", top.tree_node);
if(!key)
{
_nfo_logf("store key='{}' anchor='{}' tag='{}' type={}", curr.scalar, curr.anchor, curr.tag, curr.type.to_str());
key = curr;
}
else
{
_nfo_logf("map[{}]: adding child", top.tree_node);
size_t node = tree.append_child(top.tree_node);
NodeType_e as_doc = tree.is_stream(top.tree_node) ? DOC : NOTYPE;
_nfo_logf("map[{}]: child={} key='{}' val='{}' as_doc={}", top.tree_node, node, key.scalar, curr.scalar, NodeType::type_str(as_doc));
tree.to_keyval(node, key.filtered_scalar(&tree), curr.filtered_scalar(&tree), as_doc);
key.add_key_props(&tree, node);
curr.add_val_props(&tree, node);
_nfo_logf("clear key='{}'", key.scalar);
key = {};
}
}
else
{
_nfo_logf("setting tree_node={} to DOCVAL...", top.tree_node);
tree.to_val(top.tree_node, curr.filtered_scalar(&tree), DOC);
curr.add_val_props(&tree, top.tree_node);
}
}
else if(line.begins_with("=ALI "))
{
csubstr alias = line.stripl("=ALI ");
_nfo_logf("REF token: {}", alias);
ParseLevel top = m_stack.top();
if(tree.is_seq(top.tree_node))
{
_nfo_logf("node[{}] is seq: set {} as val ref", top.tree_node, alias);
ASSERT_FALSE(key);
size_t node = tree.append_child(top.tree_node);
tree.to_val(node, alias);
tree.set_val_ref(node, alias);
}
else if(tree.is_map(top.tree_node))
{
if(key)
{
_nfo_logf("node[{}] is map and key '{}' is pending: set {} as val ref", top.tree_node, key.scalar, alias);
size_t node = tree.append_child(top.tree_node);
tree.to_keyval(node, key.filtered_scalar(&tree), alias);
key.add_key_props(&tree, node);
tree.set_val_ref(node, alias);
_nfo_logf("clear key='{}'", key);
key = {};
}
else
{
_nfo_logf("node[{}] is map and no key is pending: save {} as key ref", top.tree_node, alias);
key.scalar = alias;
key.ref = alias;
}
}
else
{
C4_ERROR("ALI event requires map or seq");
}
}
else if(line.begins_with("+SEQ"))
{
_nfo_log("pushing SEQ");
OptionalScalar anchor = {};
OptionalScalar tag = {};
csubstr more_tokens = line.stripl("+SEQ").triml(' ');
if(more_tokens.begins_with('['))
{
ASSERT_TRUE(more_tokens.begins_with("[]"));
more_tokens = more_tokens.offs(2, 0).triml(' ');
}
parse_anchor_and_tag(more_tokens, &anchor, &tag);
size_t node = tree.root_id();
if(m_stack.empty())
{
_nfo_log("stack was empty, set root to SEQ");
tree._add_flags(node, SEQ);
m_stack.push({node});
ASSERT_FALSE(key); // for the key to exist, the parent must exist and be a map
}
else
{
size_t parent = m_stack.top().tree_node;
_nfo_logf("stack was not empty. parent={}", parent);
ASSERT_NE(parent, (size_t)NONE);
NodeType more_flags = NOTYPE;
if(tree.is_doc(parent) && !(tree.is_seq(parent) || tree.is_map(parent)))
{
_nfo_logf("set node to parent={}, add DOC", parent);
node = parent;
more_flags.add(DOC);
}
else
{
_nfo_logf("add child to parent={}", parent);
node = tree.append_child(parent);
m_stack.push({node});
_nfo_logf("add child to parent={}: child={}", parent, node);
}
if(key)
{
_nfo_logf("has key, set to keyseq: parent={} child={} key='{}'", parent, node, key);
ASSERT_EQ(tree.is_map(parent) || node == parent, true);
tree.to_seq(node, key.filtered_scalar(&tree), more_flags);
key.add_key_props(&tree, node);
_nfo_logf("clear key='{}'", key.scalar);
key = {};
}
else
{
if(tree.is_map(parent))
{
_nfo_logf("has null key, set to keyseq: parent={} child={}", parent, node);
ASSERT_EQ(tree.is_map(parent) || node == parent, true);
tree.to_seq(node, csubstr{}, more_flags);
}
else
{
_nfo_logf("no key, set to seq: parent={} child={}", parent, node);
tree.to_seq(node, more_flags);
}
}
}
if(tag)
tree.set_val_tag(node, normalize_tag(tag));
if(anchor)
tree.set_val_anchor(node, anchor);
}
else if(line.begins_with("+MAP"))
{
_nfo_log("pushing MAP");
OptionalScalar anchor = {};
OptionalScalar tag = {};
csubstr more_tokens = line.stripl("+MAP").triml(' ');
if(more_tokens.begins_with('{'))
{
ASSERT_TRUE(more_tokens.begins_with("{}"));
more_tokens = more_tokens.offs(2, 0).triml(' ');
}
parse_anchor_and_tag(more_tokens, &anchor, &tag);
size_t node = tree.root_id();
if(m_stack.empty())
{
_nfo_log("stack was empty, set root to MAP");
tree._add_flags(node, MAP);
m_stack.push({node});
ASSERT_FALSE(key); // for the key to exist, the parent must exist and be a map
}
else
{
size_t parent = m_stack.top().tree_node;
_nfo_logf("stack was not empty. parent={}", parent);
ASSERT_NE(parent, (size_t)NONE);
NodeType more_flags = NOTYPE;
if(tree.is_doc(parent) && !(tree.is_seq(parent) || tree.is_map(parent)))
{
_nfo_logf("set node to parent={}, add DOC", parent);
node = parent;
more_flags.add(DOC);
}
else
{
_nfo_logf("add child to parent={}", parent);
node = tree.append_child(parent);
m_stack.push({node});
_nfo_logf("add child to parent={}: child={}", parent, node);
}
if(key)
{
_nfo_logf("has key, set to keymap: parent={} child={} key='{}'", parent, node, key);
ASSERT_EQ(tree.is_map(parent) || node == parent, true);
tree.to_map(node, key.filtered_scalar(&tree), more_flags);
key.add_key_props(&tree, node);
_nfo_logf("clear key='{}'", key.scalar);
key = {};
}
else
{
if(tree.is_map(parent))
{
_nfo_logf("has null key, set to keymap: parent={} child={}", parent, node);
ASSERT_EQ(tree.is_map(parent) || node == parent, true);
tree.to_map(node, csubstr{}, more_flags);
}
else
{
_nfo_logf("no key, set to map: parent={} child={}", parent, node);
tree.to_map(node, more_flags);
}
}
}
if(tag)
tree.set_val_tag(node, normalize_tag(tag));
if(anchor)
tree.set_val_anchor(node, anchor);
}
else if(line.begins_with("-SEQ"))
{
_nfo_logf("popping SEQ, empty={}", m_stack.empty());
size_t node;
if(m_stack.empty())
node = tree.root_id();
else
node = m_stack.pop().tree_node;
ASSERT_TRUE(tree.is_seq(node)) << "node=" << node;
}
else if(line.begins_with("-MAP"))
{
_nfo_logf("popping MAP, empty={}", m_stack.empty());
size_t node;
if(m_stack.empty())
node = tree.root_id();
else
node = m_stack.pop().tree_node;
ASSERT_TRUE(tree.is_map(node)) << "node=" << node;
}
else if(line.begins_with("+DOC"))
{
csubstr rem = line.stripl("+DOC").triml(' ');
_nfo_logf("pushing DOC: {}", rem);
size_t node = tree.root_id();
auto is_sep = rem.first_of_any("---\n", "--- ", "---\r") || rem.ends_with("---");
ASSERT_EQ(key, false); // for the key to exist, the parent must exist and be a map
if(m_stack.empty())
{
_nfo_log("stack was empty");
ASSERT_EQ(node, tree.root_id());
if(tree.is_stream(node))
{
_nfo_log("there is already a stream, append a DOC");
node = tree.append_child(node);
tree.to_doc(node);
m_stack.push({node});
}
else if(is_sep)
{
_nfo_logf("separator was specified: {}", rem);
if((!tree.is_container(node)) && (!tree.is_doc(node)))
{
tree._add_flags(node, STREAM);
node = tree.append_child(node);
_nfo_logf("create STREAM at {} and add DOC child={}", tree.root_id(), node);
tree.to_doc(node);
m_stack.push({node});
}
else
{
_nfo_log("rearrange root as STREAM");
tree.set_root_as_stream();
node = tree.append_child(tree.root_id());
_nfo_logf("added doc as STREAM child: {}", node);
tree.to_doc(node);
m_stack.push({node});
}
}
else
{
if(tree.is_doc(node))
{
_nfo_log("rearrange root as STREAM");
tree.set_root_as_stream();
m_stack.push({node});
}
}
}
else
{
size_t parent = m_stack.top().tree_node;
_nfo_logf("add DOC to parent={}", parent);
ASSERT_NE(parent, (size_t)NONE);
node = tree.append_child(parent);
_nfo_logf("child DOC={}", node);
tree.to_doc(node);
m_stack.push({node});
}
}
else if(line.begins_with("-DOC"))
{
_nfo_log("popping DOC");
if(!m_stack.empty())
m_stack.pop();
}
else if(line.begins_with("+STR"))
{
ASSERT_EQ(m_stack.size(), 0u);
}
else if(line.begins_with("-STR"))
{
ASSERT_LE(m_stack.size(), 1u);
if(!m_stack.empty())
m_stack.pop();
}
else if(line.empty())
{
// nothing to do
}
else
{
C4_ERROR("unknown event: '%.*s'", (int)line.len, line.str);
}
linenum++;
}
}
} // namespace yml
} // namespace c4
|