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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef INCLUDED_ORCUS_YAML_PARSER_HPP
#define INCLUDED_ORCUS_YAML_PARSER_HPP
#include "orcus/yaml_parser_base.hpp"
#include "orcus/parser_global.hpp"
namespace orcus {
/**
* Blank handler class for yaml_parser. One can sub-class this and overwrite
* callback functions one needs to handle.
*/
class yaml_handler
{
public:
/**
* Called when the parser starts parsing a content.
*/
void begin_parse() {}
/**
* Called when the parser finishes parsing an entire content.
*/
void end_parse() {}
/**
* Called when a new document is encountered.
*/
void begin_document() {}
/**
* Called when the parser has finished parsing a document.
*/
void end_document() {}
/**
* Called when a sequence begins.
*/
void begin_sequence() {}
/**
* Called when a sequence ends.
*/
void end_sequence() {}
/**
* Called when a map begins.
*/
void begin_map() {}
/**
* Called when the parser starts parsing a map key.
*/
void begin_map_key() {}
/**
* Called when the parser finishes parsing a map key.
*/
void end_map_key() {}
/**
* Called when the parser finishes parsing an entire map.
*/
void end_map() {}
/**
* Called when a string value is encountered.
*
* @param value string value.
*/
void string(std::string_view value)
{
(void)value;
}
/**
* Called when a numeric value is encountered.
*
* @param val numeric value.
*/
void number(double val)
{
(void)val;
}
/**
* Called when a boolean 'true' keyword is encountered.
*/
void boolean_true() {}
/**
* Called when a boolean 'false' keyword is encountered.
*/
void boolean_false() {}
/**
* Called when a 'null' keyword is encountered.
*/
void null() {}
};
/**
* Parser for YAML documents.
*
* @tparam HandlerT Hanlder type with member functions for event callbacks.
* Refer to yaml_handler.
*
* @warning This parser is still highly experimental. Use with caution.
*/
template<typename HandlerT>
class yaml_parser : public yaml::parser_base
{
public:
typedef HandlerT handler_type;
yaml_parser(std::string_view content, handler_type& hdl);
void parse();
private:
size_t end_scope();
void check_or_begin_document();
void check_or_begin_map();
void check_or_begin_sequence();
void parse_value(const char* p, size_t len);
void push_value(const char* p, size_t len);
void parse_line(const char* p, size_t len);
void parse_map_key(const char* p, size_t len);
void handler_begin_parse();
void handler_end_parse();
void handler_begin_document();
void handler_end_document();
void handler_begin_sequence();
void handler_end_sequence();
void handler_begin_map();
void handler_end_map();
void handler_begin_map_key();
void handler_end_map_key();
void handler_string(const char* p, size_t n);
void handler_number(double val);
void handler_boolean_true();
void handler_boolean_false();
void handler_null();
private:
handler_type& m_handler;
};
template<typename _Handler>
void yaml_parser<_Handler>::handler_begin_parse()
{
push_parse_token(yaml::detail::parse_token_t::begin_parse);
m_handler.begin_parse();
}
template<typename _Handler>
void yaml_parser<_Handler>::handler_end_parse()
{
push_parse_token(yaml::detail::parse_token_t::end_parse);
m_handler.end_parse();
}
template<typename _Handler>
void yaml_parser<_Handler>::handler_begin_document()
{
push_parse_token(yaml::detail::parse_token_t::begin_document);
m_handler.begin_document();
}
template<typename _Handler>
void yaml_parser<_Handler>::handler_end_document()
{
push_parse_token(yaml::detail::parse_token_t::end_document);
m_handler.end_document();
}
template<typename _Handler>
void yaml_parser<_Handler>::handler_begin_sequence()
{
push_parse_token(yaml::detail::parse_token_t::begin_sequence);
m_handler.begin_sequence();
}
template<typename _Handler>
void yaml_parser<_Handler>::handler_end_sequence()
{
push_parse_token(yaml::detail::parse_token_t::end_sequence);
m_handler.end_sequence();
}
template<typename _Handler>
void yaml_parser<_Handler>::handler_begin_map()
{
push_parse_token(yaml::detail::parse_token_t::begin_map);
m_handler.begin_map();
}
template<typename _Handler>
void yaml_parser<_Handler>::handler_end_map()
{
push_parse_token(yaml::detail::parse_token_t::end_map);
m_handler.end_map();
}
template<typename _Handler>
void yaml_parser<_Handler>::handler_begin_map_key()
{
push_parse_token(yaml::detail::parse_token_t::begin_map_key);
m_handler.begin_map_key();
}
template<typename _Handler>
void yaml_parser<_Handler>::handler_end_map_key()
{
push_parse_token(yaml::detail::parse_token_t::end_map_key);
m_handler.end_map_key();
}
template<typename _Handler>
void yaml_parser<_Handler>::handler_string(const char* p, size_t n)
{
push_parse_token(yaml::detail::parse_token_t::string);
m_handler.string({p, n});
}
template<typename _Handler>
void yaml_parser<_Handler>::handler_number(double val)
{
push_parse_token(yaml::detail::parse_token_t::number);
m_handler.number(val);
}
template<typename _Handler>
void yaml_parser<_Handler>::handler_boolean_true()
{
push_parse_token(yaml::detail::parse_token_t::boolean_true);
m_handler.boolean_true();
}
template<typename _Handler>
void yaml_parser<_Handler>::handler_boolean_false()
{
push_parse_token(yaml::detail::parse_token_t::boolean_false);
m_handler.boolean_false();
}
template<typename _Handler>
void yaml_parser<_Handler>::handler_null()
{
push_parse_token(yaml::detail::parse_token_t::null);
m_handler.null();
}
template<typename _Handler>
yaml_parser<_Handler>::yaml_parser(std::string_view content, handler_type& hdl) :
yaml::parser_base(content), m_handler(hdl) {}
template<typename _Handler>
void yaml_parser<_Handler>::parse()
{
handler_begin_parse();
while (has_char())
{
reset_on_new_line();
size_t indent = parse_indent();
if (indent == parse_indent_end_of_stream)
break;
if (indent == parse_indent_blank_line)
continue;
size_t cur_scope = get_scope();
if (cur_scope <= indent)
{
if (in_literal_block())
{
handle_line_in_literal(indent);
continue;
}
if (has_line_buffer())
{
// This line is part of multi-line string. Push the line to the
// buffer as-is.
handle_line_in_multi_line_string();
continue;
}
}
if (cur_scope == scope_empty)
{
if (indent > 0)
throw parse_error(
"first node of the document should not be indented.", offset());
push_scope(indent);
}
else if (indent > cur_scope)
{
push_scope(indent);
}
else if (indent < cur_scope)
{
// Current indent is less than the current scope level.
do
{
cur_scope = end_scope();
if (cur_scope < indent)
throw parse_error("parse: invalid indent level.", offset());
}
while (indent < cur_scope);
}
// Parse the rest of the line.
std::string_view line = parse_to_end_of_line();
line = trim(line);
assert(!line.empty());
parse_line(line.data(), line.size());
}
// End all remaining scopes.
size_t cur_scope = get_scope();
while (cur_scope != scope_empty)
cur_scope = end_scope();
if (get_doc_hash())
handler_end_document();
handler_end_parse();
}
template<typename _Handler>
size_t yaml_parser<_Handler>::end_scope()
{
switch (get_scope_type())
{
case yaml::detail::scope_t::map:
{
if (get_last_parse_token() == yaml::detail::parse_token_t::end_map_key)
handler_null();
handler_end_map();
break;
}
case yaml::detail::scope_t::sequence:
{
if (get_last_parse_token() == yaml::detail::parse_token_t::begin_sequence_element)
handler_null();
handler_end_sequence();
break;
}
case yaml::detail::scope_t::multi_line_string:
{
std::string_view merged = merge_line_buffer();
handler_string(merged.data(), merged.size());
break;
}
default:
{
if (has_line_buffer())
{
assert(get_line_buffer_count() == 1);
std::string_view line = pop_line_front();
parse_value(line.data(), line.size());
}
}
}
return pop_scope();
}
template<typename _Handler>
void yaml_parser<_Handler>::check_or_begin_document()
{
if (!get_doc_hash())
{
set_doc_hash(mp_char);
handler_begin_document();
}
}
template<typename _Handler>
void yaml_parser<_Handler>::check_or_begin_map()
{
switch (get_scope_type())
{
case yaml::detail::scope_t::unset:
{
check_or_begin_document();
set_scope_type(yaml::detail::scope_t::map);
handler_begin_map();
break;
}
case yaml::detail::scope_t::map:
{
if (get_last_parse_token() == yaml::detail::parse_token_t::end_map_key)
handler_null();
break;
}
default:
;
}
}
template<typename _Handler>
void yaml_parser<_Handler>::check_or_begin_sequence()
{
switch (get_scope_type())
{
case yaml::detail::scope_t::unset:
{
check_or_begin_document();
set_scope_type(yaml::detail::scope_t::sequence);
handler_begin_sequence();
break;
}
case yaml::detail::scope_t::sequence:
{
if (get_last_parse_token() == yaml::detail::parse_token_t::begin_sequence_element)
handler_null();
break;
}
default:
;
}
push_parse_token(yaml::detail::parse_token_t::begin_sequence_element);
}
template<typename _Handler>
void yaml_parser<_Handler>::parse_value(const char* p, size_t len)
{
check_or_begin_document();
const char* p0 = p;
const char* p_end = p + len;
double val;
p = parse_numeric(p, p_end, val);
if (p == p_end)
{
handler_number(val);
return;
}
yaml::detail::keyword_t kw = parse_keyword(p0, len);
if (kw != yaml::detail::keyword_t::unknown)
{
switch (kw)
{
case yaml::detail::keyword_t::null:
handler_null();
break;
case yaml::detail::keyword_t::boolean_true:
handler_boolean_true();
break;
case yaml::detail::keyword_t::boolean_false:
handler_boolean_false();
break;
default:
;
}
return;
}
// Failed to parse it as a number or a keyword. It must be a string.
handler_string(p0, len);
}
template<typename _Handler>
void yaml_parser<_Handler>::push_value(const char* p, size_t len)
{
check_or_begin_document();
if (has_line_buffer() && get_scope_type() == yaml::detail::scope_t::unset)
set_scope_type(yaml::detail::scope_t::multi_line_string);
push_line_back(p, len);
}
template<typename _Handler>
void yaml_parser<_Handler>::parse_line(const char* p, size_t len)
{
const char* p_end = p + len;
const char* p0 = p; // Save the original head position.
if (*p == '-')
{
++p;
if (p == p_end)
{
// List item start.
check_or_begin_sequence();
return;
}
switch (*p)
{
case '-':
{
// start of a document
++p;
if (p == p_end)
throw parse_error("parse_line: line ended with '--'.", offset_last_char_of_line());
if (*p != '-')
parse_error::throw_with(
"parse_line: '-' expected but '", *p, "' found.",
offset_last_char_of_line() - std::ptrdiff_t(p_end-p));
++p; // Skip the '-'.
set_doc_hash(p);
handler_begin_document();
clear_scopes();
if (p != p_end)
{
skip_blanks(p, p_end-p);
// Whatever comes after '---' is equivalent of first node.
assert(p != p_end);
push_scope(0);
parse_line(p, p_end-p);
}
return;
}
case ' ':
{
check_or_begin_sequence();
// list item start with inline first item content.
++p;
if (p == p_end)
throw parse_error(
"parse_line: list item expected, but the line ended prematurely.",
offset_last_char_of_line() - std::ptrdiff_t(p_end-p));
skip_blanks(p, p_end-p);
size_t scope_width = get_scope() + (p-p0);
push_scope(scope_width);
parse_line(p, p_end-p);
return;
}
default:
// It is none of the above.
p = p0;
}
}
if (get_scope_type() == yaml::detail::scope_t::sequence)
parse_error::throw_with(
"'-' was expected for a sequence element, but '", *p, "' was found.",
offset_last_char_of_line()-len+1);
// If the line doesn't start with a "- ", it must be a dictionary key.
parse_map_key(p, len);
}
template<typename _Handler>
void yaml_parser<_Handler>::parse_map_key(const char* p, size_t len)
{
const char* p_end = p + len;
const char* p0 = p; // Save the original head position.
switch (*p)
{
case '"':
{
std::string_view quoted_str = parse_double_quoted_string_value(p, len);
if (p == p_end)
{
handler_string(quoted_str.data(), quoted_str.size());
return;
}
skip_blanks(p, p_end-p);
if (*p != ':')
throw parse_error(
"parse_map_key: ':' is expected after the quoted string key.",
offset() - std::ptrdiff_t(p_end-p+1));
check_or_begin_map();
handler_begin_map_key();
handler_string(quoted_str.data(), quoted_str.size());
handler_end_map_key();
++p; // skip the ':'.
if (p == p_end)
return;
// Skip all white spaces.
skip_blanks(p, p_end-p);
}
break;
case '\'':
{
std::string_view quoted_str = parse_single_quoted_string_value(p, len);
if (p == p_end)
{
handler_string(quoted_str.data(), quoted_str.size());
return;
}
skip_blanks(p, p_end-p);
if (*p != ':')
throw parse_error(
"parse_map_key: ':' is expected after the quoted string key.",
offset() - std::ptrdiff_t(p_end-p+1));
check_or_begin_map();
handler_begin_map_key();
handler_string(quoted_str.data(), quoted_str.size());
handler_end_map_key();
++p; // skip the ':'.
if (p == p_end)
return;
skip_blanks(p, p_end-p);
}
break;
default:
{
key_value kv = parse_key_value(p, p_end-p);
if (kv.key.empty())
{
// No map key found.
if (*p == '|')
{
start_literal_block();
return;
}
push_value(p, len);
return;
}
check_or_begin_map();
handler_begin_map_key();
parse_value(kv.key.data(), kv.key.size());
handler_end_map_key();
if (kv.value.empty())
return;
p = kv.value.data();
}
}
if (*p == '|')
{
start_literal_block();
return;
}
// inline map item.
if (*p == '-')
throw parse_error(
"parse_map_key: sequence entry is not allowed as an inline map item.",
offset() - std::ptrdiff_t(p_end-p+1));
size_t scope_width = get_scope() + (p-p0);
push_scope(scope_width);
parse_line(p, p_end-p);
}
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|