1 #ifndef _C4_YML_EVENT_HANDLER_TREE_HPP_
2 #define _C4_YML_EVENT_HANDLER_TREE_HPP_
4 #ifndef _C4_YML_TREE_HPP_
8 #ifndef _C4_YML_EVENT_HANDLER_STACK_HPP_
12 C4_SUPPRESS_WARNING_MSVC_WITH_PUSH(4702)
45 Tree *C4_RESTRICT m_tree;
47 size_t m_num_directives;
48 bool m_yaml_directive;
51 #define _enable_(bits) _enable__<bits>(); _c4dbgpf("node[{}]: enable {}", m_curr->node_id, #bits)
52 #define _disable_(bits) _disable__<bits>(); _c4dbgpf("node[{}]: disable {}", m_curr->node_id, #bits)
54 #define _enable_(bits) _enable__<bits>()
55 #define _disable_(bits) _disable__<bits>()
57 #define _has_any_(bits) _has_any__<bits>()
74 if(C4_UNLIKELY(!tree))
75 _RYML_CB_ERR(m_stack.m_callbacks,
"null tree");
76 if(C4_UNLIKELY(
id >= tree->
capacity()))
77 _RYML_CB_ERR(tree->
callbacks(),
"invalid node");
78 if(C4_UNLIKELY(!tree->
is_root(
id)))
80 if(C4_UNLIKELY(!tree->
has_key(
id)))
81 _RYML_CB_ERR(tree->
callbacks(),
"destination node belongs to a map and has no key");
84 if(m_tree->is_root(
id))
87 _reset_parser_state(m_curr,
id, m_tree->root_id());
91 _stack_reset_non_root();
92 _reset_parser_state(m_parent,
id, m_tree->parent(
id));
93 _reset_parser_state(m_curr,
id,
id);
96 m_yaml_directive =
false;
108 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree !=
nullptr);
109 this->_stack_start_parse(filename, relocate_arena, relocate_arena_data);
114 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree !=
nullptr);
115 if(m_num_directives && !m_tree->is_stream(m_tree->root_id()))
116 _RYML_CB_ERR_(m_stack.m_callbacks,
"directives cannot be used without a document", {});
117 this->_stack_finish_parse();
159 _c4dbgp(
"begin_doc");
160 if(_stack_should_push_on_begin_doc())
163 _set_root_as_stream();
172 if(_stack_should_pop_on_end_doc())
174 _remove_speculative();
183 _c4dbgp(
"begin_doc_expl");
184 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->root_id() == m_curr->node_id);
185 if(!m_tree->is_stream(m_tree->root_id()))
187 _c4dbgp(
"ensure stream");
188 _set_root_as_stream();
189 id_type first = m_tree->first_child(m_tree->root_id());
190 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->is_stream(m_tree->root_id()));
191 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->num_children(m_tree->root_id()) == 1u);
192 if(m_tree->has_children(first) || m_tree->is_val(first))
201 _remove_speculative();
202 m_curr->node_id = m_tree->last_child(m_tree->root_id());
203 m_curr->tr_data = m_tree->_p(m_curr->node_id);
216 _c4dbgp(
"end_doc_expl");
217 _remove_speculative();
218 if(_stack_should_pop_on_end_doc())
223 m_yaml_directive =
false;
235 _RYML_CB_ERR_(m_stack.m_callbacks,
"ryml trees cannot handle containers as keys", m_curr->pos);
239 _RYML_CB_ERR_(m_stack.m_callbacks,
"ryml trees cannot handle containers as keys", m_curr->pos);
244 _c4dbgpf(
"node[{}]: begin_map_val_flow", m_curr->node_id);
252 _c4dbgpf(
"node[{}]: begin_map_val_block", m_curr->node_id);
262 _c4dbgpf(
"node[{}]: end_map_val", m_curr->node_id);
274 _RYML_CB_ERR_(m_stack.m_callbacks,
"ryml trees cannot handle containers as keys", m_curr->pos);
278 _RYML_CB_ERR_(m_stack.m_callbacks,
"ryml trees cannot handle containers as keys", m_curr->pos);
283 _c4dbgpf(
"node[{}]: begin_seq_val_flow", m_curr->node_id);
291 _c4dbgpf(
"node[{}]: begin_seq_val_block", m_curr->node_id);
301 _c4dbgpf(
"node[{}]: end_seq_val", m_curr->node_id);
313 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
314 _RYML_CB_ASSERT(m_stack.m_callbacks, m_parent);
315 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->has_children(m_parent->node_id));
316 NodeData const* prev = m_tree->m_buf;
317 _set_state_(m_curr, m_tree->_append_child__unprotected(m_parent->node_id));
318 if(prev != m_tree->m_buf)
319 _refresh_after_relocation();
320 _c4dbgpf(
"node[{}]: added sibling={} prev={}", m_parent->node_id, m_curr->node_id, m_tree->prev_sibling(m_curr->node_id));
330 if(C4_UNLIKELY(m_tree->is_container(m_curr->node_id)))
331 _RYML_CB_ERR_(m_stack.m_callbacks,
"ryml trees cannot handle containers as keys", m_curr->pos);
332 _RYML_CB_ASSERT(m_stack.m_callbacks, m_parent);
333 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->is_seq(m_parent->node_id));
334 _RYML_CB_ASSERT(m_stack.m_callbacks, !m_tree->is_container(m_curr->node_id));
335 _RYML_CB_ASSERT(m_stack.m_callbacks, !m_tree->has_key(m_curr->node_id));
336 const NodeData tmp = _val2key_(*m_curr->tr_data);
338 m_curr->tr_data->m_val = {};
339 begin_map_val_flow();
340 m_curr->tr_data->m_type = tmp.
m_type;
341 m_curr->tr_data->m_key = tmp.
m_key;
352 _RYML_CB_ERR_(m_stack.m_callbacks,
"ryml trees cannot handle containers as keys", m_curr->pos);
365 _c4dbgpf(
"node[{}]: set key scalar plain as empty", m_curr->node_id);
366 m_curr->tr_data->m_key.scalar = {};
371 _c4dbgpf(
"node[{}]: set val scalar plain as empty", m_curr->node_id);
372 m_curr->tr_data->m_val.scalar = {};
378 _c4dbgpf(
"node[{}]: set key scalar plain: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
379 m_curr->tr_data->m_key.scalar = scalar;
384 _c4dbgpf(
"node[{}]: set val scalar plain: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
385 m_curr->tr_data->m_val.scalar = scalar;
392 _c4dbgpf(
"node[{}]: set key scalar dquot: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
393 m_curr->tr_data->m_key.scalar = scalar;
398 _c4dbgpf(
"node[{}]: set val scalar dquot: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
399 m_curr->tr_data->m_val.scalar = scalar;
406 _c4dbgpf(
"node[{}]: set key scalar squot: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
407 m_curr->tr_data->m_key.scalar = scalar;
412 _c4dbgpf(
"node[{}]: set val scalar squot: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
413 m_curr->tr_data->m_val.scalar = scalar;
420 _c4dbgpf(
"node[{}]: set key scalar literal: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
421 m_curr->tr_data->m_key.scalar = scalar;
426 _c4dbgpf(
"node[{}]: set val scalar literal: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
427 m_curr->tr_data->m_val.scalar = scalar;
434 _c4dbgpf(
"node[{}]: set key scalar folded: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
435 m_curr->tr_data->m_key.scalar = scalar;
440 _c4dbgpf(
"node[{}]: set val scalar folded: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
441 m_curr->tr_data->m_val.scalar = scalar;
464 _c4dbgpf(
"node[{}]: set key anchor: [{}]~~~{}~~~", m_curr->node_id, anchor.len, anchor);
465 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
467 _RYML_CB_ERR_(m_tree->callbacks(),
"key cannot have both anchor and ref", m_curr->pos);
468 _RYML_CB_ASSERT(m_tree->callbacks(), !anchor.begins_with(
'&'));
470 m_curr->tr_data->m_key.anchor = anchor;
474 _c4dbgpf(
"node[{}]: set val anchor: [{}]~~~{}~~~", m_curr->node_id, anchor.len, anchor);
475 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
477 _RYML_CB_ERR_(m_tree->callbacks(),
"val cannot have both anchor and ref", m_curr->pos);
478 _RYML_CB_ASSERT(m_tree->callbacks(), !anchor.begins_with(
'&'));
480 m_curr->tr_data->m_val.anchor = anchor;
485 _c4dbgpf(
"node[{}]: set key ref: [{}]~~~{}~~~", m_curr->node_id, ref.len, ref);
486 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
488 _RYML_CB_ERR_(m_tree->callbacks(),
"key cannot have both anchor and ref", m_curr->pos);
489 _RYML_CB_ASSERT(m_tree->callbacks(), ref.begins_with(
'*'));
491 m_curr->tr_data->m_key.anchor = ref.sub(1);
492 m_curr->tr_data->m_key.scalar = ref;
496 _c4dbgpf(
"node[{}]: set val ref: [{}]~~~{}~~~", m_curr->node_id, ref.len, ref);
497 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
499 _RYML_CB_ERR_(m_tree->callbacks(),
"val cannot have both anchor and ref", m_curr->pos);
500 _RYML_CB_ASSERT(m_tree->callbacks(), ref.begins_with(
'*'));
502 m_curr->tr_data->m_val.anchor = ref.sub(1);
503 m_curr->tr_data->m_val.scalar = ref;
515 _c4dbgpf(
"node[{}]: set key tag: [{}]~~~{}~~~", m_curr->node_id, tag.len, tag);
517 m_curr->tr_data->m_key.tag = tag;
521 _c4dbgpf(
"node[{}]: set val tag: [{}]~~~{}~~~", m_curr->node_id, tag.len, tag);
523 m_curr->tr_data->m_val.tag = tag;
535 _c4dbgpf(
"% directive! {}", directive);
536 _RYML_CB_ASSERT(m_tree->callbacks(), directive.begins_with(
'%'));
537 if(directive.begins_with(
"%TAG"))
539 if(C4_UNLIKELY(!m_tree->add_tag_directive(directive)))
540 _RYML_CB_ERR_(m_stack.m_callbacks,
"failed to add directive", m_curr->pos);
542 else if(directive.begins_with(
"%YAML"))
544 _c4dbgpf(
"%YAML directive! ignoring...: {}", directive);
545 if(C4_UNLIKELY(m_yaml_directive))
546 _RYML_CB_ERR_(m_stack.m_callbacks,
"multiple yaml directives", m_curr->pos);
547 m_yaml_directive =
true;
551 _c4dbgpf(
"unknown directive! ignoring... {}", directive);
565 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
566 csubstr prev = m_tree->arena();
567 substr out = m_tree->alloc_arena(len);
568 substr curr = m_tree->arena();
569 if(curr.str != prev.str)
570 _stack_relocate_to_new_arena(prev, curr);
576 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
577 csubstr prev = m_tree->arena();
578 if(!prev.is_super(*relocated))
579 return alloc_arena(len);
580 substr out = alloc_arena(len);
581 substr curr = m_tree->arena();
582 if(curr.str != prev.str)
583 *relocated = _stack_relocate_to_new_arena(*relocated, prev, curr);
592 void _reset_parser_state(state* st,
id_type parse_root,
id_type node)
594 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
595 _set_state_(st, node);
599 _c4dbgpf(
"resetting state: initial flags={}", detail::_parser_flags_to_str(flagbuf, st->flags));
603 _c4dbgpf(
"node[{}] is notype", node);
604 if(m_tree->is_root(parse_root))
606 _c4dbgpf(
"node[{}] is root", node);
611 _c4dbgpf(
"node[{}] is not root. setting USTY", node);
617 _c4dbgpf(
"node[{}] is map", node);
622 _c4dbgpf(
"node[{}] is map", node);
627 _c4dbgpf(
"node[{}] has key. setting USTY", node);
632 _RYML_CB_ERR(m_tree->callbacks(),
"cannot append to node");
636 _c4dbgpf(
"node[{}] is doc", node);
640 _c4dbgpf(
"resetting state: final flags={}", detail::_parser_flags_to_str(flagbuf, st->flags));
649 NodeData
const* prev = m_tree->m_buf;
650 m_curr->node_id = m_tree->_append_child__unprotected(m_parent->node_id);
651 m_curr->tr_data = m_tree->_p(m_curr->node_id);
652 if(prev != m_tree->m_buf)
653 _refresh_after_relocation();
654 _c4dbgpf(
"pushed! level={}. top is now node={} (parent={})", m_curr->level, m_curr->node_id, m_parent ? m_parent->node_id :
NONE);
659 _remove_speculative_with_parent();
665 template<type_bits bits> C4_HOT C4_ALWAYS_INLINE
void _enable__() noexcept
667 m_curr->tr_data->m_type.type =
static_cast<NodeType_e>(m_curr->tr_data->m_type.type | bits);
669 template<type_bits bits> C4_HOT C4_ALWAYS_INLINE
void _disable__() noexcept
671 m_curr->tr_data->m_type.type =
static_cast<NodeType_e>(m_curr->tr_data->m_type.type & (~bits));
673 template<type_bits bits> C4_HOT C4_ALWAYS_INLINE
bool _has_any__() const noexcept
675 return (m_curr->tr_data->m_type.type & bits) != 0;
680 C4_ALWAYS_INLINE
void _set_state_(state *C4_RESTRICT s,
id_type id)
const noexcept
683 s->tr_data = m_tree->_p(
id);
685 void _refresh_after_relocation()
687 _c4dbgp(
"tree: refreshing stack data after tree data relocation");
688 for(
auto &st : m_stack)
689 st.tr_data = m_tree->_p(st.node_id);
692 void _set_root_as_stream()
694 _c4dbgp(
"set root as stream");
695 _RYML_CB_ASSERT(m_tree->callbacks(), m_tree->root_id() == 0u);
696 _RYML_CB_ASSERT(m_tree->callbacks(), m_curr->node_id == 0u);
697 const bool hack = !m_tree->has_children(m_curr->node_id) && !m_tree->is_val(m_curr->node_id);
699 m_tree->_p(m_tree->root_id())->m_type.add(
VAL);
700 m_tree->set_root_as_stream();
701 _RYML_CB_ASSERT(m_tree->callbacks(), m_tree->is_stream(m_tree->root_id()));
702 _RYML_CB_ASSERT(m_tree->callbacks(), m_tree->has_children(m_tree->root_id()));
703 _RYML_CB_ASSERT(m_tree->callbacks(), m_tree->is_doc(m_tree->first_child(m_tree->root_id())));
705 m_tree->_p(m_tree->first_child(m_tree->root_id()))->m_type.rem(
VAL);
706 _set_state_(m_curr, m_tree->root_id());
709 static NodeData _val2key_(NodeData
const& C4_RESTRICT d) noexcept
715 static_assert((
_VALMASK >> 1u) ==
_KEYMASK,
"required for this function to work");
719 r.m_type.type = (r.m_type.type |
KEY);
723 void _remove_speculative()
725 _c4dbgp(
"remove speculative node");
726 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
727 _RYML_CB_ASSERT(m_tree->callbacks(), !m_tree->empty());
728 const id_type last_added = m_tree->size() - 1;
729 if(m_tree->has_parent(last_added))
730 if(m_tree->_p(last_added)->m_type ==
NOTYPE)
731 m_tree->remove(last_added);
734 void _remove_speculative_with_parent()
736 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
737 _RYML_CB_ASSERT(m_tree->callbacks(), !m_tree->empty());
738 const id_type last_added = m_tree->size() - 1;
739 _RYML_CB_ASSERT(m_tree->callbacks(), m_tree->has_parent(last_added));
740 if(m_tree->_p(last_added)->m_type ==
NOTYPE)
742 _c4dbgpf(
"remove speculative node with parent. parent={} node={} parent(node)={}", m_parent->node_id, last_added, m_tree->parent(last_added));
743 m_tree->remove(last_added);
747 C4_ALWAYS_INLINE
void _save_loc()
749 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
750 _RYML_CB_ASSERT(m_tree->callbacks(), m_tree->_p(m_curr->node_id)->m_val.scalar.len == 0);
751 m_tree->_p(m_curr->node_id)->m_val.scalar.str = m_curr->line_contents.rem.str;
767 C4_SUPPRESS_WARNING_MSVC_POP
bool is_map(id_type node) const
bool is_root(id_type node) const
bool has_key(id_type node) const
id_type parent(id_type node) const
Callbacks const & callbacks() const
NodeType_e
a bit mask for marking node types and styles
@ VALANCH
the val has an &anchor
@ NOTYPE
no node type or style is set
@ KEY_DQUO
mark key scalar as double quoted "
@ VALREF
a *reference: the val references an &anchor
@ VALNIL
the val is null (eg {a : } results in a null val)
@ MAP
a map: a parent of KEYVAL/KEYSEQ/KEYMAP nodes
@ VAL_FOLDED
mark val scalar as multiline, block folded >
@ VAL_STYLE
mask of all the scalar styles for val (not container styles!)
@ KEYTAG
the key has a tag
@ FLOW_SL
mark container with single-line flow style (seqs as '[val1,val2], maps as '{key: val,...
@ VAL_UNFILT
the val scalar was left unfiltered; the parser was set not to filter.
@ VAL
a scalar: has a scalar (ie string) value, possibly empty. must be a leaf node, and cannot be MAP or S...
@ VALTAG
the val has a tag
@ SEQ
a seq: a parent of VAL/SEQ/MAP nodes
@ VAL_SQUO
mark val scalar as single quoted '
@ KEY_STYLE
mask of all the scalar styles for key (not container styles!)
@ VAL_PLAIN
mark val scalar as plain scalar (unquoted, even when multiline)
@ KEYREF
a *reference: the key references an &anchor
@ BLOCK
mark container with block style (seqs as '- val ', maps as 'key: val')
@ KEYANCH
the key has an &anchor
@ VAL_DQUO
mark val scalar as double quoted "
@ KEY_UNFILT
the key scalar was left unfiltered; the parser was set not to filter.
@ KEY_SQUO
mark key scalar as single quoted '
@ VAL_LITERAL
mark val scalar as multiline, block literal |
@ KEY_LITERAL
mark key scalar as multiline, block literal |
@ KEY_PLAIN
mark key scalar as plain scalar (unquoted, even when multiline)
@ KEY_FOLDED
mark key scalar as multiline, block folded >
@ KEYNIL
the key is null (eg { : b} results in a null key)
void(*)(void *, csubstr prev_arena, substr next_arena) pfn_relocate_arena
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...
@ RTOP
reading at top level
@ RUNK
reading unknown state (when starting): must determine whether scalar, map or seq
@ USTY
reading in unknown style mode - must determine FLOW or BLCK reading an implicit map nested in an expl...
a c-style callbacks class.
Use this class a base of implementations of event handler to simplify the stack logic.
The stack state needed specifically by EventHandlerTree.
The event handler to create a ryml Tree.
void set_key_scalar_plain(csubstr scalar) noexcept
void set_val_tag(csubstr tag) noexcept
void mark_val_scalar_unfiltered() noexcept
void end_doc_expl()
explicit doc end, with ...
void begin_map_key_flow()
void end_doc()
implicit doc end (without ...)
void begin_doc()
implicit doc start (without —)
void set_val_scalar_plain(csubstr scalar) noexcept
void set_key_scalar_plain_empty() noexcept
EventHandlerTree(Callbacks const &cb)
void mark_key_scalar_unfiltered() noexcept
void actually_val_is_first_key_of_new_map_block()
like its flow counterpart, but this function can only be called after the end of a flow-val at root o...
void set_val_scalar_folded(csubstr scalar) noexcept
void begin_doc_expl()
explicit doc start, with —
void add_directive(csubstr directive)
void begin_map_val_flow()
void end_stream() const noexcept
void set_val_scalar_dquoted(csubstr scalar) noexcept
void start_parse(const char *filename, detail::pfn_relocate_arena relocate_arena, void *relocate_arena_data)
void begin_seq_val_flow()
void set_val_scalar_plain_empty() noexcept
void begin_map_key_block()
substr alloc_arena(size_t len, substr *relocated)
void begin_seq_key_block()
void begin_map_val_block()
void begin_seq_val_block()
void set_val_scalar_squoted(csubstr scalar) noexcept
void set_val_ref(csubstr ref)
void set_key_ref(csubstr ref)
void reset(Tree *tree, id_type id)
void set_val_anchor(csubstr anchor)
void set_key_scalar_literal(csubstr scalar) noexcept
void actually_val_is_first_key_of_new_map_flow()
set the previous val as the first key of a new map, with flow style.
EventHandlerTree(Tree *tree, id_type id)
void begin_seq_key_flow()
void set_key_tag(csubstr tag) noexcept
void set_key_scalar_folded(csubstr scalar) noexcept
void begin_stream() const noexcept
void set_key_anchor(csubstr anchor)
substr alloc_arena(size_t len)
void set_key_scalar_dquoted(csubstr scalar) noexcept
void set_val_scalar_literal(csubstr scalar) noexcept
void set_key_scalar_squoted(csubstr scalar) noexcept
contains the data for each YAML node.
wraps a NodeType_e element with some syntactic sugar and predicates
bool has_key() const noexcept
bool is_doc() const noexcept
bool is_seq() const noexcept
bool is_map() const noexcept