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)
23 struct EventHandlerTreeState :
public ParserState
39 using state = EventHandlerTreeState;
46 Tree *C4_RESTRICT m_tree;
48 size_t m_num_directives;
49 bool m_yaml_directive;
52 #define _enable_(bits) _enable__<bits>(); _c4dbgpf("node[{}]: enable {}", m_curr->node_id, #bits)
53 #define _disable_(bits) _disable__<bits>(); _c4dbgpf("node[{}]: disable {}", m_curr->node_id, #bits)
55 #define _enable_(bits) _enable__<bits>()
56 #define _disable_(bits) _disable__<bits>()
58 #define _has_any_(bits) _has_any__<bits>()
75 if(C4_UNLIKELY(!tree))
76 _RYML_CB_ERR(m_stack.m_callbacks,
"null tree");
77 if(C4_UNLIKELY(
id >= tree->
capacity()))
78 _RYML_CB_ERR(tree->
callbacks(),
"invalid node");
79 if(C4_UNLIKELY(!tree->
is_root(
id)))
81 if(C4_UNLIKELY(!tree->
has_key(
id)))
82 _RYML_CB_ERR(tree->
callbacks(),
"destination node belongs to a map and has no key");
85 if(m_tree->is_root(
id))
88 _reset_parser_state(m_curr,
id, m_tree->root_id());
92 _stack_reset_non_root();
93 _reset_parser_state(m_parent,
id, m_tree->parent(
id));
94 _reset_parser_state(m_curr,
id,
id);
97 m_yaml_directive =
false;
109 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree !=
nullptr);
110 this->_stack_start_parse(filename, relocate_arena, relocate_arena_data);
115 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree !=
nullptr);
116 if(m_num_directives && !m_tree->is_stream(m_tree->root_id()))
117 _RYML_CB_ERR_(m_stack.m_callbacks,
"directives cannot be used without a document", {});
118 this->_stack_finish_parse();
160 _c4dbgp(
"begin_doc");
161 if(_stack_should_push_on_begin_doc())
164 _set_root_as_stream();
173 if(_stack_should_pop_on_end_doc())
175 _remove_speculative();
184 _c4dbgp(
"begin_doc_expl");
185 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->root_id() == m_curr->node_id);
186 if(!m_tree->is_stream(m_tree->root_id()))
188 _c4dbgp(
"ensure stream");
189 _set_root_as_stream();
190 id_type first = m_tree->first_child(m_tree->root_id());
191 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->is_stream(m_tree->root_id()));
192 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->num_children(m_tree->root_id()) == 1u);
193 if(m_tree->has_children(first) || m_tree->is_val(first))
202 _remove_speculative();
203 m_curr->node_id = m_tree->last_child(m_tree->root_id());
204 m_curr->tr_data = m_tree->_p(m_curr->node_id);
217 _c4dbgp(
"end_doc_expl");
218 _remove_speculative();
219 if(_stack_should_pop_on_end_doc())
224 m_yaml_directive =
false;
236 _RYML_CB_ERR_(m_stack.m_callbacks,
"ryml trees cannot handle containers as keys", m_curr->pos);
240 _RYML_CB_ERR_(m_stack.m_callbacks,
"ryml trees cannot handle containers as keys", m_curr->pos);
245 _c4dbgpf(
"node[{}]: begin_map_val_flow", m_curr->node_id);
253 _c4dbgpf(
"node[{}]: begin_map_val_block", m_curr->node_id);
263 _c4dbgpf(
"node[{}]: end_map_val", m_curr->node_id);
275 _RYML_CB_ERR_(m_stack.m_callbacks,
"ryml trees cannot handle containers as keys", m_curr->pos);
279 _RYML_CB_ERR_(m_stack.m_callbacks,
"ryml trees cannot handle containers as keys", m_curr->pos);
284 _c4dbgpf(
"node[{}]: begin_seq_val_flow", m_curr->node_id);
292 _c4dbgpf(
"node[{}]: begin_seq_val_block", m_curr->node_id);
302 _c4dbgpf(
"node[{}]: end_seq_val", m_curr->node_id);
314 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
315 _RYML_CB_ASSERT(m_stack.m_callbacks, m_parent);
316 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->has_children(m_parent->node_id));
317 NodeData const* prev = m_tree->m_buf;
318 _set_state_(m_curr, m_tree->_append_child__unprotected(m_parent->node_id));
319 if(prev != m_tree->m_buf)
320 _refresh_after_relocation();
321 _c4dbgpf(
"node[{}]: added sibling={} prev={}", m_parent->node_id, m_curr->node_id, m_tree->prev_sibling(m_curr->node_id));
331 if(C4_UNLIKELY(m_tree->is_container(m_curr->node_id)))
332 _RYML_CB_ERR_(m_stack.m_callbacks,
"ryml trees cannot handle containers as keys", m_curr->pos);
333 _RYML_CB_ASSERT(m_stack.m_callbacks, m_parent);
334 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->is_seq(m_parent->node_id));
335 _RYML_CB_ASSERT(m_stack.m_callbacks, !m_tree->is_container(m_curr->node_id));
336 _RYML_CB_ASSERT(m_stack.m_callbacks, !m_tree->has_key(m_curr->node_id));
337 const NodeData tmp = _val2key_(*m_curr->tr_data);
339 m_curr->tr_data->m_val = {};
340 begin_map_val_flow();
341 m_curr->tr_data->m_type = tmp.
m_type;
342 m_curr->tr_data->m_key = tmp.
m_key;
353 _RYML_CB_ERR_(m_stack.m_callbacks,
"ryml trees cannot handle containers as keys", m_curr->pos);
366 _c4dbgpf(
"node[{}]: set key scalar plain as empty", m_curr->node_id);
367 m_curr->tr_data->m_key.scalar = {};
372 _c4dbgpf(
"node[{}]: set val scalar plain as empty", m_curr->node_id);
373 m_curr->tr_data->m_val.scalar = {};
379 _c4dbgpf(
"node[{}]: set key scalar plain: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
380 m_curr->tr_data->m_key.scalar = scalar;
385 _c4dbgpf(
"node[{}]: set val scalar plain: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
386 m_curr->tr_data->m_val.scalar = scalar;
393 _c4dbgpf(
"node[{}]: set key scalar dquot: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
394 m_curr->tr_data->m_key.scalar = scalar;
399 _c4dbgpf(
"node[{}]: set val scalar dquot: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
400 m_curr->tr_data->m_val.scalar = scalar;
407 _c4dbgpf(
"node[{}]: set key scalar squot: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
408 m_curr->tr_data->m_key.scalar = scalar;
413 _c4dbgpf(
"node[{}]: set val scalar squot: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
414 m_curr->tr_data->m_val.scalar = scalar;
421 _c4dbgpf(
"node[{}]: set key scalar literal: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
422 m_curr->tr_data->m_key.scalar = scalar;
427 _c4dbgpf(
"node[{}]: set val scalar literal: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
428 m_curr->tr_data->m_val.scalar = scalar;
435 _c4dbgpf(
"node[{}]: set key scalar folded: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
436 m_curr->tr_data->m_key.scalar = scalar;
441 _c4dbgpf(
"node[{}]: set val scalar folded: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
442 m_curr->tr_data->m_val.scalar = scalar;
465 _c4dbgpf(
"node[{}]: set key anchor: [{}]~~~{}~~~", m_curr->node_id, anchor.len, anchor);
466 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
468 _RYML_CB_ASSERT(m_stack.m_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_ASSERT(m_stack.m_callbacks, !anchor.begins_with(
'&'));
479 m_curr->tr_data->m_val.anchor = anchor;
484 _c4dbgpf(
"node[{}]: set key ref: [{}]~~~{}~~~", m_curr->node_id, ref.len, ref);
485 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
487 _RYML_CB_ERR_(m_tree->callbacks(),
"key cannot have both anchor and ref", m_curr->pos);
488 _RYML_CB_ASSERT(m_tree->callbacks(), ref.begins_with(
'*'));
490 m_curr->tr_data->m_key.anchor = ref.sub(1);
491 m_curr->tr_data->m_key.scalar = ref;
495 _c4dbgpf(
"node[{}]: set val ref: [{}]~~~{}~~~", m_curr->node_id, ref.len, ref);
496 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
498 _RYML_CB_ERR_(m_tree->callbacks(),
"val cannot have both anchor and ref", m_curr->pos);
499 _RYML_CB_ASSERT(m_tree->callbacks(), ref.begins_with(
'*'));
501 m_curr->tr_data->m_val.anchor = ref.sub(1);
502 m_curr->tr_data->m_val.scalar = ref;
514 _c4dbgpf(
"node[{}]: set key tag: [{}]~~~{}~~~", m_curr->node_id, tag.len, tag);
516 m_curr->tr_data->m_key.tag = tag;
520 _c4dbgpf(
"node[{}]: set val tag: [{}]~~~{}~~~", m_curr->node_id, tag.len, tag);
522 m_curr->tr_data->m_val.tag = tag;
534 _c4dbgpf(
"% directive! {}", directive);
535 _RYML_CB_ASSERT(m_tree->callbacks(), directive.begins_with(
'%'));
536 if(directive.begins_with(
"%TAG"))
538 if(C4_UNLIKELY(!m_tree->add_tag_directive(directive)))
539 _RYML_CB_ERR_(m_stack.m_callbacks,
"failed to add directive", m_curr->pos);
541 else if(directive.begins_with(
"%YAML"))
543 _c4dbgpf(
"%YAML directive! ignoring...: {}", directive);
544 if(C4_UNLIKELY(m_yaml_directive))
545 _RYML_CB_ERR_(m_stack.m_callbacks,
"multiple yaml directives", m_curr->pos);
546 m_yaml_directive =
true;
550 _c4dbgpf(
"unknown directive! ignoring... {}", directive);
564 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
565 csubstr prev = m_tree->arena();
566 substr out = m_tree->alloc_arena(len);
567 substr curr = m_tree->arena();
568 if(curr.str != prev.str)
569 _stack_relocate_to_new_arena(prev, curr);
575 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
576 csubstr prev = m_tree->arena();
577 if(!prev.is_super(*relocated))
578 return alloc_arena(len);
579 substr out = alloc_arena(len);
580 substr curr = m_tree->arena();
581 if(curr.str != prev.str)
582 *relocated = _stack_relocate_to_new_arena(*relocated, prev, curr);
591 void _reset_parser_state(state* st,
id_type parse_root,
id_type node)
593 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
594 _set_state_(st, node);
598 _c4dbgpf(
"resetting state: initial flags={}", detail::_parser_flags_to_str(flagbuf, st->flags));
602 _c4dbgpf(
"node[{}] is notype", node);
603 if(m_tree->is_root(parse_root))
605 _c4dbgpf(
"node[{}] is root", node);
610 _c4dbgpf(
"node[{}] is not root. setting USTY", node);
616 _c4dbgpf(
"node[{}] is map", node);
621 _c4dbgpf(
"node[{}] is map", node);
626 _c4dbgpf(
"node[{}] has key. setting USTY", node);
631 _RYML_CB_ERR(m_tree->callbacks(),
"cannot append to node");
635 _c4dbgpf(
"node[{}] is doc", node);
639 _c4dbgpf(
"resetting state: final flags={}", detail::_parser_flags_to_str(flagbuf, st->flags));
648 NodeData
const* prev = m_tree->m_buf;
649 m_curr->node_id = m_tree->_append_child__unprotected(m_parent->node_id);
650 m_curr->tr_data = m_tree->_p(m_curr->node_id);
651 if(prev != m_tree->m_buf)
652 _refresh_after_relocation();
653 _c4dbgpf(
"pushed! level={}. top is now node={} (parent={})", m_curr->level, m_curr->node_id, m_parent ? m_parent->node_id :
NONE);
658 _remove_speculative_with_parent();
664 template<type_bits bits> C4_HOT C4_ALWAYS_INLINE
void _enable__() noexcept
666 m_curr->tr_data->m_type.type =
static_cast<NodeType_e>(m_curr->tr_data->m_type.type | bits);
668 template<type_bits bits> C4_HOT C4_ALWAYS_INLINE
void _disable__() noexcept
670 m_curr->tr_data->m_type.type =
static_cast<NodeType_e>(m_curr->tr_data->m_type.type & (~bits));
672 template<type_bits bits> C4_HOT C4_ALWAYS_INLINE
bool _has_any__() const noexcept
674 return (m_curr->tr_data->m_type.type & bits) != 0;
679 C4_ALWAYS_INLINE
void _set_state_(state *C4_RESTRICT s,
id_type id)
const noexcept
682 s->tr_data = m_tree->_p(
id);
684 void _refresh_after_relocation()
686 _c4dbgp(
"tree: refreshing stack data after tree data relocation");
687 for(
auto &st : m_stack)
688 st.tr_data = m_tree->_p(st.node_id);
691 void _set_root_as_stream()
693 _c4dbgp(
"set root as stream");
694 _RYML_CB_ASSERT(m_tree->callbacks(), m_tree->root_id() == 0u);
695 _RYML_CB_ASSERT(m_tree->callbacks(), m_curr->node_id == 0u);
696 const bool hack = !m_tree->has_children(m_curr->node_id) && !m_tree->is_val(m_curr->node_id);
698 m_tree->_p(m_tree->root_id())->m_type.add(
VAL);
699 m_tree->set_root_as_stream();
700 _RYML_CB_ASSERT(m_tree->callbacks(), m_tree->is_stream(m_tree->root_id()));
701 _RYML_CB_ASSERT(m_tree->callbacks(), m_tree->has_children(m_tree->root_id()));
702 _RYML_CB_ASSERT(m_tree->callbacks(), m_tree->is_doc(m_tree->first_child(m_tree->root_id())));
704 m_tree->_p(m_tree->first_child(m_tree->root_id()))->m_type.rem(
VAL);
705 _set_state_(m_curr, m_tree->root_id());
708 static NodeData _val2key_(NodeData
const& C4_RESTRICT d) noexcept
714 static_assert((
_VALMASK >> 1u) ==
_KEYMASK,
"required for this function to work");
718 r.m_type.type = (r.m_type.type |
KEY);
722 void _remove_speculative()
724 _c4dbgp(
"remove speculative node");
725 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
726 _RYML_CB_ASSERT(m_tree->callbacks(), !m_tree->empty());
727 const id_type last_added = m_tree->size() - 1;
728 if(m_tree->has_parent(last_added))
729 if(m_tree->_p(last_added)->m_type ==
NOTYPE)
730 m_tree->remove(last_added);
733 void _remove_speculative_with_parent()
735 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
736 _RYML_CB_ASSERT(m_tree->callbacks(), !m_tree->empty());
737 const id_type last_added = m_tree->size() - 1;
738 _RYML_CB_ASSERT(m_tree->callbacks(), m_tree->has_parent(last_added));
739 if(m_tree->_p(last_added)->m_type ==
NOTYPE)
741 _c4dbgpf(
"remove speculative node with parent. parent={} node={} parent(node)={}", m_parent->node_id, last_added, m_tree->parent(last_added));
742 m_tree->remove(last_added);
746 C4_ALWAYS_INLINE
void _save_loc()
748 _RYML_CB_ASSERT(m_stack.m_callbacks, m_tree);
749 _RYML_CB_ASSERT(m_tree->callbacks(), m_tree->_p(m_curr->node_id)->m_val.scalar.len == 0);
750 m_tree->_p(m_curr->node_id)->m_val.scalar.str = m_curr->line_contents.rem.str;
766 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 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 ...)
EventHandlerTreeState state
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