1 #ifndef _C4_YML_PARSE_ENGINE_HPP_
2 #define _C4_YML_PARSE_ENGINE_HPP_
4 #ifndef _C4_YML_PARSER_STATE_HPP_
10 # pragma warning(push)
11 # pragma warning(disable: 4251)
206 struct FilterResultExtending;
263 template<
class EventHandler>
309 m_evt_handler->m_stack.reserve(capacity);
316 _resize_locations(num_source_lines);
319 RYML_DEPRECATED(
"filter arena no longer needed")
333 Callbacks const&
callbacks()
const { _RYML_ASSERT_BASIC(m_evt_handler);
return m_evt_handler->m_stack.m_callbacks; }
348 RYML_DEPRECATED(
"filter arena no longer needed")
371 template<
class U=EventHandler> RYML_DEPRECATED(
"removed, deliberately undefined. use the function in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_place(csubstr
filename, substr yaml,
Tree *t,
size_t node_id);
372 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the function in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_place( substr yaml,
Tree *t,
size_t node_id);
373 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the function in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_place(csubstr
filename, substr yaml,
Tree *t );
374 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the function in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_place( substr yaml,
Tree *t );
375 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the function in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_place(csubstr
filename, substr yaml,
NodeRef node );
376 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the function in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_place( substr yaml,
NodeRef node );
377 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the function in parse.hpp.") typename std::enable_if<U::is_wtree,
Tree>::type
parse_in_place(csubstr
filename, substr yaml );
378 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the function in parse.hpp.") typename std::enable_if<U::is_wtree,
Tree>::type
parse_in_place( substr yaml );
379 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the function in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_arena(csubstr
filename, csubstr yaml,
Tree *t,
size_t node_id);
380 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the function in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_arena( csubstr yaml,
Tree *t,
size_t node_id);
381 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the function in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_arena(csubstr
filename, csubstr yaml,
Tree *t );
382 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the function in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_arena( csubstr yaml,
Tree *t );
383 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the function in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_arena(csubstr
filename, csubstr yaml,
NodeRef node );
384 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the function in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_arena( csubstr yaml,
NodeRef node );
385 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the function in parse.hpp.") typename std::enable_if<U::is_wtree,
Tree>::type
parse_in_arena(csubstr
filename, csubstr yaml );
386 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the function in parse.hpp.") typename std::enable_if<U::is_wtree,
Tree>::type
parse_in_arena( csubstr yaml );
387 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the csubstr
version in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_arena(csubstr
filename, substr yaml,
Tree *t,
size_t node_id);
388 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the csubstr
version in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_arena( substr yaml,
Tree *t,
size_t node_id);
389 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the csubstr
version in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_arena(csubstr
filename, substr yaml,
Tree *t );
390 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the csubstr
version in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_arena( substr yaml,
Tree *t );
391 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the csubstr
version in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_arena(csubstr
filename, substr yaml,
NodeRef node );
392 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the csubstr
version in parse.hpp.") typename std::enable_if<U::is_wtree,
void>::type
parse_in_arena( substr yaml,
NodeRef node );
393 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the csubstr
version in parse.hpp.") typename std::enable_if<U::is_wtree,
Tree>::type
parse_in_arena(csubstr
filename, substr yaml );
394 template<class U=EventHandler> RYML_DEPRECATED("removed, deliberately undefined. use the csubstr
version in parse.hpp.") typename std::enable_if<U::is_wtree,
Tree>::type
parse_in_arena( substr yaml );
417 RYML_DEPRECATED("moved to
Tree::location(
Parser const&). deliberately undefined here.")
418 auto location(
Tree const&,
id_type node) const -> typename std::enable_if<U::is_wtree,
Location>::type;
421 RYML_DEPRECATED("moved to
ConstNodeRef::location(
Parser const&), deliberately undefined here.")
422 auto location(
ConstNodeRef const&) const -> typename std::enable_if<U::is_wtree,
Location>::type;
474 bool _is_doc_begin(csubstr s);
475 bool _is_doc_end(csubstr s);
477 bool _scan_scalar_plain_blck(ScannedScalar *C4_RESTRICT sc,
size_t indentation);
478 bool _scan_scalar_plain_seq_flow(ScannedScalar *C4_RESTRICT sc);
479 bool _scan_scalar_plain_seq_blck(ScannedScalar *C4_RESTRICT sc);
480 bool _scan_scalar_plain_map_flow(ScannedScalar *C4_RESTRICT sc);
481 bool _scan_scalar_plain_map_blck(ScannedScalar *C4_RESTRICT sc);
482 bool _scan_scalar_map_json(ScannedScalar *C4_RESTRICT sc);
483 bool _scan_scalar_seq_json(ScannedScalar *C4_RESTRICT sc);
484 bool _scan_scalar_plain_unk(ScannedScalar *C4_RESTRICT sc);
485 bool _is_valid_start_scalar_plain_flow(csubstr s);
487 ScannedScalar _scan_scalar_squot();
488 ScannedScalar _scan_scalar_dquot();
490 void _scan_block(ScannedBlock *C4_RESTRICT sb,
size_t indref);
492 csubstr _scan_anchor();
493 csubstr _scan_ref_seq();
494 csubstr _scan_ref_map();
500 csubstr _filter_scalar_plain(substr s,
size_t indentation);
501 csubstr _filter_scalar_squot(substr s);
502 csubstr _filter_scalar_dquot(substr s);
503 csubstr _filter_scalar_literal(substr s,
size_t indentation,
BlockChomp_e chomp);
504 csubstr _filter_scalar_folded(substr s,
size_t indentation,
BlockChomp_e chomp);
505 csubstr _move_scalar_left_and_add_newline(substr s);
507 csubstr _maybe_filter_key_scalar_plain(ScannedScalar
const& sc,
size_t indendation);
508 csubstr _maybe_filter_val_scalar_plain(ScannedScalar
const& sc,
size_t indendation);
509 csubstr _maybe_filter_key_scalar_squot(ScannedScalar
const& sc);
510 csubstr _maybe_filter_val_scalar_squot(ScannedScalar
const& sc);
511 csubstr _maybe_filter_key_scalar_dquot(ScannedScalar
const& sc);
512 csubstr _maybe_filter_val_scalar_dquot(ScannedScalar
const& sc);
513 csubstr _maybe_filter_key_scalar_literal(ScannedBlock
const& sb);
514 csubstr _maybe_filter_val_scalar_literal(ScannedBlock
const& sb);
515 csubstr _maybe_filter_key_scalar_folded(ScannedBlock
const& sb);
516 csubstr _maybe_filter_val_scalar_folded(ScannedBlock
const& sb);
521 void _handle_map_block();
522 void _handle_seq_block();
523 void _handle_map_flow();
524 void _handle_seq_flow();
525 void _handle_seq_imap();
526 void _handle_map_json();
527 void _handle_seq_json();
530 void _handle_unk_json();
533 void _handle_flow_skip_whitespace();
535 void _end_map_flow();
536 void _end_seq_flow();
537 void _end_map_blck();
538 void _end_seq_blck();
543 void _begin2_doc_expl();
545 void _end2_doc_expl();
547 void _maybe_begin_doc();
548 void _maybe_end_doc();
550 void _start_doc_suddenly();
551 void _end_doc_suddenly();
552 void _end_doc_suddenly__pop();
555 void _set_indentation(
size_t indentation);
556 void _save_indentation();
557 void _handle_indentation_pop_from_block_seq();
558 void _handle_indentation_pop_from_block_map();
559 void _handle_indentation_pop(ParserState
const* dst);
561 void _maybe_skip_comment();
562 void _skip_comment();
563 void _maybe_skip_whitespace_tokens();
564 void _maybe_skipchars(
char c);
565 #ifdef RYML_NO_COVERAGE__TO_BE_DELETED
566 void _maybe_skipchars_up_to(
char c,
size_t max_to_skip);
569 void _skipchars(
const char (&chars)[N]);
570 bool _maybe_scan_following_colon() noexcept;
571 bool _maybe_scan_following_comma() noexcept;
576 template<class FilterProcessor> auto _filter_plain(FilterProcessor &C4_RESTRICT proc,
size_t indentation) -> decltype(proc.result());
577 template<class FilterProcessor> auto _filter_squoted(FilterProcessor &C4_RESTRICT proc) -> decltype(proc.result());
578 template<class FilterProcessor> auto _filter_dquoted(FilterProcessor &C4_RESTRICT proc) -> decltype(proc.result());
579 template<class FilterProcessor> auto _filter_block_literal(FilterProcessor &C4_RESTRICT proc,
size_t indentation,
BlockChomp_e chomp) -> decltype(proc.result());
580 template<class FilterProcessor> auto _filter_block_folded(FilterProcessor &C4_RESTRICT proc,
size_t indentation,
BlockChomp_e chomp) -> decltype(proc.result());
586 template<class FilterProcessor>
void _filter_nl_plain(FilterProcessor &C4_RESTRICT proc,
size_t indentation);
587 template<class FilterProcessor>
void _filter_nl_squoted(FilterProcessor &C4_RESTRICT proc);
588 template<class FilterProcessor>
void _filter_nl_dquoted(FilterProcessor &C4_RESTRICT proc);
590 template<class FilterProcessor>
bool _filter_ws_handle_to_first_non_space(FilterProcessor &C4_RESTRICT proc);
591 template<class FilterProcessor>
void _filter_ws_copy_trailing(FilterProcessor &C4_RESTRICT proc);
592 template<class FilterProcessor>
void _filter_ws_skip_trailing(FilterProcessor &C4_RESTRICT proc);
594 template<class FilterProcessor>
void _filter_dquoted_backslash(FilterProcessor &C4_RESTRICT proc);
596 template<class FilterProcessor>
void _filter_chomp(FilterProcessor &C4_RESTRICT proc,
BlockChomp_e chomp,
size_t indentation);
597 template<class FilterProcessor>
size_t _handle_all_whitespace(FilterProcessor &C4_RESTRICT proc,
BlockChomp_e chomp);
598 template<class FilterProcessor>
size_t _extend_to_chomp(FilterProcessor &C4_RESTRICT proc,
size_t contents_len);
599 template<class FilterProcessor>
void _filter_block_indentation(FilterProcessor &C4_RESTRICT proc,
size_t indentation);
600 template<class FilterProcessor>
void _filter_block_folded_newlines(FilterProcessor &C4_RESTRICT proc,
size_t indentation,
size_t len);
601 template<class FilterProcessor>
size_t _filter_block_folded_newlines_compress(FilterProcessor &C4_RESTRICT proc,
size_t num_newl,
size_t wpos_at_first_newl);
602 template<class FilterProcessor>
void _filter_block_folded_newlines_leading(FilterProcessor &C4_RESTRICT proc,
size_t indentation,
size_t len);
603 template<class FilterProcessor>
void _filter_block_folded_indented_block(FilterProcessor &C4_RESTRICT proc,
size_t indentation,
size_t len,
size_t curr_indentation) noexcept;
609 void _line_progressed(
size_t ahead);
611 void _line_ended_undo();
613 bool _finished_file() const;
614 bool _finished_line() const;
617 substr _peek_next_line(
size_t pos=
npos) const;
619 bool _at_line_begin()
const
621 return m_evt_handler->m_curr->line_contents.rem.begin() == m_evt_handler->m_curr->line_contents.full.begin();
624 void _relocate_arena(csubstr prev_arena, substr next_arena);
625 static void _s_relocate_arena(
void*, csubstr prev_arena, substr next_arena);
629 C4_ALWAYS_INLINE
bool has_all(
ParserFlag_t f)
const noexcept {
return (m_evt_handler->m_curr->flags & f) == f; }
630 C4_ALWAYS_INLINE
bool has_any(
ParserFlag_t f)
const noexcept {
return (m_evt_handler->m_curr->flags & f) != 0; }
631 C4_ALWAYS_INLINE
bool has_none(
ParserFlag_t f)
const noexcept {
return (m_evt_handler->m_curr->flags & f) == 0; }
632 static C4_ALWAYS_INLINE
bool has_all(
ParserFlag_t f, ParserState
const* C4_RESTRICT s) noexcept {
return (s->flags & f) == f; }
633 static C4_ALWAYS_INLINE
bool has_any(
ParserFlag_t f, ParserState
const* C4_RESTRICT s) noexcept {
return (s->flags & f) != 0; }
634 static C4_ALWAYS_INLINE
bool has_none(
ParserFlag_t f, ParserState
const* C4_RESTRICT s) noexcept {
return (s->flags & f) == 0; }
637 C4_ALWAYS_INLINE
static void add_flags(
ParserFlag_t on, ParserState *C4_RESTRICT s) noexcept { s->flags |= on; }
638 C4_ALWAYS_INLINE
static void addrem_flags(
ParserFlag_t on,
ParserFlag_t off, ParserState *C4_RESTRICT s) noexcept { s->flags &= ~off; s->flags |= on; }
639 C4_ALWAYS_INLINE
static void rem_flags(
ParserFlag_t off, ParserState *C4_RESTRICT s) noexcept { s->flags &= ~off; }
640 C4_ALWAYS_INLINE
void add_flags(
ParserFlag_t on) noexcept { m_evt_handler->m_curr->flags |= on; }
641 C4_ALWAYS_INLINE
void addrem_flags(
ParserFlag_t on,
ParserFlag_t off) noexcept { m_evt_handler->m_curr->flags &= ~off; m_evt_handler->m_curr->flags |= on; }
642 C4_ALWAYS_INLINE
void rem_flags(
ParserFlag_t off) noexcept { m_evt_handler->m_curr->flags &= ~off; }
644 static void add_flags(
ParserFlag_t on, ParserState *C4_RESTRICT s);
646 static void rem_flags(
ParserFlag_t off, ParserState *C4_RESTRICT s);
647 C4_ALWAYS_INLINE
void add_flags(
ParserFlag_t on) noexcept { add_flags(on, m_evt_handler->m_curr); }
648 C4_ALWAYS_INLINE
void addrem_flags(
ParserFlag_t on,
ParserFlag_t off) noexcept { addrem_flags(on, off, m_evt_handler->m_curr); }
649 C4_ALWAYS_INLINE
void rem_flags(
ParserFlag_t off) noexcept { rem_flags(off, m_evt_handler->m_curr); }
654 void _prepare_locations();
655 void _resize_locations(
size_t sz);
656 bool _locations_dirty()
const;
665 template<
class ...Args> C4_NO_INLINE
void _dbg(csubstr fmt, Args
const& ...args)
const;
666 template<
class DumpFn> C4_NO_INLINE
void _fmt_msg(DumpFn &&dumpfn)
const;
668 template<
class ...Args> C4_NORETURN C4_NO_INLINE
void _err(Location
const& cpploc,
const char *fmt, Args
const& ...args)
const;
669 template<
class ...Args> C4_NORETURN C4_NO_INLINE
void _err(Location
const& cpploc, Location
const& ymlloc,
const char *fmt, Args
const& ...args)
const;
683 Entry annotations[2];
687 void _handle_colon();
688 void _add_annotation(Annotation *C4_RESTRICT dst, csubstr str,
size_t indentation,
size_t line);
689 void _clear_annotations(Annotation *C4_RESTRICT dst);
690 bool _has_pending_annotations()
const {
return m_pending_tags.num_entries || m_pending_anchors.num_entries; }
691 #ifdef RYML_NO_COVERAGE__TO_BE_DELETED
692 bool _handle_indentation_from_annotations();
694 bool _annotations_require_key_container()
const;
695 void _handle_annotations_before_blck_key_scalar();
696 void _handle_annotations_before_blck_val_scalar();
697 void _handle_annotations_before_start_mapblck(
size_t current_line);
698 void _handle_annotations_before_start_mapblck_as_key();
699 void _handle_annotations_and_indentation_after_start_mapblck(
size_t key_indentation,
size_t key_line);
700 size_t _select_indentation_from_annotations(
size_t val_indentation,
size_t val_line);
701 void _handle_directive(csubstr rem);
705 void _check_tag(csubstr tag);
717 EventHandler *C4_RESTRICT m_evt_handler;
722 Annotation m_pending_anchors;
723 Annotation m_pending_tags;
725 bool m_was_inside_qmrk;
726 bool m_doc_empty =
true;
727 size_t m_prev_colon =
npos;
731 size_t m_bom_len = 0;
732 size_t m_bom_line = 0;
737 size_t *m_newline_offsets;
738 size_t m_newline_offsets_size;
739 size_t m_newline_offsets_capacity;
740 csubstr m_newline_offsets_buf;
752 #if defined(_MSC_VER)
753 # pragma warning(pop)
Holds a pointer to an existing tree, and a node id.
A reference to a node in an existing yaml tree, offering a more convenient API than the index-based A...
This is the main driver of parsing logic: it scans the YAML or JSON source for tokens,...
void reserve_stack(id_type capacity)
Reserve a certain capacity for the parsing stack.
FilterResult filter_scalar_plain(csubstr scalar, substr dst, size_t indentation)
filter a plain scalar
csubstr location_contents(Location const &loc) const
Get the string starting at a particular location, to the end of the parsed source buffer.
FilterResult filter_scalar_squoted(csubstr scalar, substr dst)
filter a single-quoted scalar
ParseEngine(EventHandler *evt_handler, ParserOptions opts={})
FilterResult filter_scalar_dquoted(csubstr scalar, substr dst)
filter a double-quoted scalar
void reserve_filter_arena(size_t)
void parse_json_in_place_ev(csubstr filename, substr src)
parse JSON in place, emitting events to the current handler
Location val_location(const char *val) const
Given a pointer to a buffer position, get the location.
FilterResult filter_scalar_plain_in_place(substr scalar, size_t cap, size_t indentation)
filter a plain scalar in place
FilterResult filter_scalar_squoted_in_place(substr scalar, size_t cap)
filter a single-quoted scalar in place
FilterResultExtending filter_scalar_dquoted_in_place(substr scalar, size_t cap)
filter a double-quoted scalar in place
Encoding_e encoding() const
Get the encoding of the latest YAML buffer parsed by this object.
size_t locations_capacity() const
void parse_in_place_ev(csubstr filename, substr src)
parse YAML in place, emitting events to the current handler
csubstr source() const
Get the latest YAML buffer parsed by this object.
FilterResult filter_scalar_block_literal_in_place(substr scalar, size_t cap, size_t indentation, BlockChomp_e chomp)
filter a block-literal scalar in place
ParserOptions const & options() const
Get the options used to build this parser object.
size_t filter_arena_capacity() const
FilterResult filter_scalar_block_literal(csubstr scalar, substr dst, size_t indentation, BlockChomp_e chomp)
filter a block-literal scalar
id_type stack_capacity() const
Callbacks const & callbacks() const
Get the current callbacks in the parser.
EventHandler handler_type
FilterResult filter_scalar_block_folded_in_place(substr scalar, size_t cap, size_t indentation, BlockChomp_e chomp)
filter a block-folded scalar in place
csubstr filename() const
Get the name of the latest file parsed by this object.
void reserve_locations(size_t num_source_lines)
Reserve a certain capacity for the array used to track node locations in the source buffer.
FilterResult filter_scalar_block_folded(csubstr scalar, substr dst, size_t indentation, BlockChomp_e chomp)
filter a block-folded scalar
void parse_in_arena(Parser *parser, csubstr filename, csubstr yaml, Tree *t, id_type node_id)
(1) parse YAML into an existing tree node. The filename will be used in any error messages arising du...
void parse_in_place(Parser *parser, csubstr filename, substr yaml, Tree *t, id_type node_id)
(1) parse YAML into an existing tree node.
id_type estimate_tree_capacity(csubstr src)
Quickly inspect the source to estimate the number of nodes the resulting tree is likely to have.
enum c4::yml::BlockChomp_ BlockChomp_e
@ CHOMP_CLIP
single newline at end (default)
@ CHOMP_KEEP
all newlines from end (+)
@ CHOMP_STRIP
no newline at end (-)
RYML_ID_TYPE id_type
The type of a node id in the YAML tree; to override the default type, define the macro RYML_ID_TYPE t...
@ npos
a null string position
int ParserFlag_t
data type for ParserState_e
@ NOBOM
No Byte Order Mark was found.
enum c4::yml::Encoding_ Encoding_e
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
A c-style callbacks class to customize behavior on errors or allocation.
Abstracts the fact that a scalar filter result may not fit in the intended memory.
Abstracts the fact that a scalar filter result may not fit in the intended memory.
holds a source or yaml file position, for example when an error is detected; See also location_format...
Options to give to the parser to control its behavior.