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;
40 enum { requires_strings_on_buffers =
false };
47 Tree *C4_RESTRICT m_tree;
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_ERR_BASIC_(m_stack.m_callbacks,
"null tree");
77 if(C4_UNLIKELY(
id >= tree->
capacity()))
78 _RYML_ERR_BASIC_(tree->
callbacks(),
"invalid node");
79 if(C4_UNLIKELY(!tree->
is_root(
id)))
81 if(C4_UNLIKELY(!tree->
has_key(
id)))
82 _RYML_ERR_BASIC_(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);
95 m_curr_doc = m_tree->ancestor_doc(
id);
113 _RYML_ASSERT_BASIC_(m_stack.m_callbacks, m_tree !=
nullptr);
114 this->_stack_start_parse(filename, ymlsrc);
119 _RYML_ASSERT_BASIC_(m_stack.m_callbacks, m_tree !=
nullptr);
120 this->_stack_finish_parse();
162 _c4dbgp(
"begin_doc");
163 if(_stack_should_push_on_begin_doc())
166 _set_root_as_stream();
170 m_curr_doc = m_curr->node_id;
176 m_curr_doc = m_tree->size();
177 if(_stack_should_pop_on_end_doc())
179 _remove_speculative();
188 _c4dbgp(
"begin_doc_expl");
189 _RYML_ASSERT_VISIT_(m_stack.m_callbacks, m_tree->root_id() == m_curr->node_id, m_tree, m_curr->node_id);
190 if(m_tree->is_stream(m_tree->root_id()))
197 _c4dbgp(
"ensure stream");
198 _set_root_as_stream();
199 const id_type root = m_tree->root_id();
200 const id_type first = m_tree->first_child(root);
201 _RYML_ASSERT_VISIT_(m_stack.m_callbacks, m_tree->is_stream(root), m_tree, root);
202 _RYML_ASSERT_VISIT_(m_stack.m_callbacks, m_tree->num_children(root) == 1u, m_tree, root);
203 if(m_tree->is_container(first) || m_tree->is_val(first))
207 #ifdef RYML_WITH_COMMENTS
208 m_tree->_p(root)->m_first_comment =
NONE;
209 m_tree->_p(root)->m_last_comment =
NONE;
216 _remove_speculative();
217 m_curr->node_id = m_tree->last_child(root);
218 m_curr->tr_data = m_tree->_p(m_curr->node_id);
222 m_curr_doc = m_curr->node_id;
227 _c4dbgp(
"end_doc_expl");
228 m_curr_doc = m_tree->size();
229 _remove_speculative();
230 if(_stack_should_pop_on_end_doc())
246 _RYML_ERR_PARSE_(m_stack.m_callbacks, m_curr->pos,
"ryml trees cannot handle containers as keys");
250 _RYML_ERR_PARSE_(m_stack.m_callbacks, m_curr->pos,
"ryml trees cannot handle containers as keys");
255 _c4dbgpf(
"node[{}]: begin_map_val_flow", m_curr->node_id);
256 _RYML_CHECK_BASIC_(m_stack.m_callbacks, !
_has_any_(
VAL));
263 _c4dbgpf(
"node[{}]: begin_map_val_block", m_curr->node_id);
264 _RYML_CHECK_BASIC_(m_stack.m_callbacks, !
_has_any_(
VAL));
272 _c4dbgpf(
"node[{}]: end_map_block", m_parent->node_id, m_parent->pos.line, m_curr->pos.line);
278 _c4dbgpf(
"node[{}]: end_map. multiline={} startline={} endline={}", m_parent->node_id, multiline, m_parent->pos.line, m_curr->pos.line);
296 _RYML_ERR_PARSE_(m_stack.m_callbacks, m_curr->pos,
"ryml trees cannot handle containers as keys");
300 _RYML_ERR_PARSE_(m_stack.m_callbacks, m_curr->pos,
"ryml trees cannot handle containers as keys");
305 _c4dbgpf(
"node[{}]: begin_seq_val_flow", m_curr->node_id);
306 _RYML_CHECK_BASIC_(m_stack.m_callbacks, !
_has_any_(
VAL));
313 _c4dbgpf(
"node[{}]: begin_seq_val_block", m_curr->node_id);
314 _RYML_CHECK_BASIC_(m_stack.m_callbacks, !
_has_any_(
VAL));
322 _c4dbgpf(
"node[{}]: end_seq_block", m_parent->node_id, m_parent->pos.line, m_curr->pos.line);
328 _c4dbgpf(
"node[{}]: end_seq. multiline={} startline={} endline={}", m_parent->node_id, multiline, m_parent->pos.line, m_curr->pos.line);
346 _RYML_ASSERT_BASIC_(m_stack.m_callbacks, m_tree);
347 _RYML_ASSERT_BASIC_(m_stack.m_callbacks, m_parent);
348 _RYML_ASSERT_VISIT_(m_stack.m_callbacks, m_tree->has_children(m_parent->node_id), m_tree, m_parent->node_id);
349 NodeData const*
const prev = m_tree->m_buf;
350 _set_state_(m_curr, m_tree->_append_child__unprotected(m_parent->node_id));
351 if(prev != m_tree->m_buf)
352 _refresh_after_relocation();
353 _c4dbgpf(
"node[{}]: added sibling={} prev={}", m_parent->node_id, m_curr->node_id, m_tree->prev_sibling(m_curr->node_id));
363 if(C4_UNLIKELY(m_tree->is_container(m_curr->node_id)))
364 _RYML_ERR_PARSE_(m_stack.m_callbacks, m_curr->pos,
"ryml trees cannot handle containers as keys");
365 _RYML_ASSERT_BASIC_(m_stack.m_callbacks, m_parent);
366 _RYML_ASSERT_VISIT_(m_stack.m_callbacks, m_tree->is_seq(m_parent->node_id), m_tree, m_parent->node_id);
367 _RYML_ASSERT_VISIT_(m_stack.m_callbacks, !m_tree->is_container(m_curr->node_id), m_tree, m_curr->node_id);
368 _RYML_ASSERT_VISIT_(m_stack.m_callbacks, !m_tree->has_key(m_curr->node_id), m_tree, m_curr->node_id);
369 const NodeData tmp = _val2key_(*m_curr->tr_data);
371 m_curr->tr_data->m_val = {};
372 begin_map_val_flow();
373 m_curr->tr_data->m_type = tmp.
m_type;
374 m_curr->tr_data->m_key = tmp.
m_key;
385 _RYML_ERR_PARSE_(m_stack.m_callbacks, m_curr->pos,
"ryml trees cannot handle containers as keys");
398 _c4dbgpf(
"node[{}]: set key scalar plain as empty", m_curr->node_id);
399 m_curr->tr_data->m_key.scalar = {};
404 _c4dbgpf(
"node[{}]: set val scalar plain as empty", m_curr->node_id);
405 m_curr->tr_data->m_val.scalar = {};
411 _c4dbgpf(
"node[{}]: set key scalar plain: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
412 m_curr->tr_data->m_key.scalar = scalar;
417 _c4dbgpf(
"node[{}]: set val scalar plain: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
418 m_curr->tr_data->m_val.scalar = scalar;
425 _c4dbgpf(
"node[{}]: set key scalar dquot: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
426 m_curr->tr_data->m_key.scalar = scalar;
431 _c4dbgpf(
"node[{}]: set val scalar dquot: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
432 m_curr->tr_data->m_val.scalar = scalar;
439 _c4dbgpf(
"node[{}]: set key scalar squot: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
440 m_curr->tr_data->m_key.scalar = scalar;
445 _c4dbgpf(
"node[{}]: set val scalar squot: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
446 m_curr->tr_data->m_val.scalar = scalar;
453 _c4dbgpf(
"node[{}]: set key scalar literal: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
454 m_curr->tr_data->m_key.scalar = scalar;
459 _c4dbgpf(
"node[{}]: set val scalar literal: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
460 m_curr->tr_data->m_val.scalar = scalar;
467 _c4dbgpf(
"node[{}]: set key scalar folded: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
468 m_curr->tr_data->m_key.scalar = scalar;
473 _c4dbgpf(
"node[{}]: set val scalar folded: [{}]~~~{}~~~", m_curr->node_id, scalar.len, scalar);
474 m_curr->tr_data->m_val.scalar = scalar;
497 _c4dbgpf(
"node[{}]: set key anchor: [{}]~~~{}~~~", m_curr->node_id, anchor.len, anchor);
498 _RYML_ASSERT_BASIC_(m_stack.m_callbacks, m_tree);
500 _RYML_ASSERT_PARSE_(m_tree->callbacks(), !anchor.begins_with(
'&'), m_curr->pos);
502 m_curr->tr_data->m_key.anchor = anchor;
506 _c4dbgpf(
"node[{}]: set val anchor: [{}]~~~{}~~~", m_curr->node_id, anchor.len, anchor);
507 _RYML_ASSERT_BASIC_(m_stack.m_callbacks, m_tree);
509 _RYML_ASSERT_PARSE_(m_tree->callbacks(), !anchor.begins_with(
'&'), m_curr->pos);
511 m_curr->tr_data->m_val.anchor = anchor;
516 _c4dbgpf(
"node[{}]: set key ref: [{}]~~~{}~~~", m_curr->node_id, ref.len, ref);
517 _RYML_ASSERT_BASIC_(m_stack.m_callbacks, m_tree);
519 _RYML_ERR_PARSE_(m_tree->callbacks(), m_curr->pos,
"key cannot have both anchor and ref");
520 _RYML_ASSERT_PARSE_(m_tree->callbacks(), ref.begins_with(
'*'), m_curr->pos);
522 m_curr->tr_data->m_key.anchor = ref.sub(1);
523 m_curr->tr_data->m_key.scalar = ref;
527 _c4dbgpf(
"node[{}]: set val ref: [{}]~~~{}~~~", m_curr->node_id, ref.len, ref);
528 _RYML_ASSERT_BASIC_(m_stack.m_callbacks, m_tree);
530 _RYML_ERR_PARSE_(m_tree->callbacks(), m_curr->pos,
"val cannot have both anchor and ref");
531 _RYML_ASSERT_PARSE_(m_tree->callbacks(), ref.begins_with(
'*'), m_curr->pos);
533 m_curr->tr_data->m_val.anchor = ref.sub(1);
534 m_curr->tr_data->m_val.scalar = ref;
546 _c4dbgpf(
"node[{}]: set key tag: [{}]~~~{}~~~", m_curr->node_id, tag.len, tag);
548 m_curr->tr_data->m_key.tag = tag;
552 _c4dbgpf(
"node[{}]: set val tag: [{}]~~~{}~~~", m_curr->node_id, tag.len, tag);
554 m_curr->tr_data->m_val.tag = tag;
566 _c4dbgpf(
"%YAML directive! version={}", yaml_version);
572 _c4dbgpf(
"%TAG directive! handle={} prefix={} id={}", handle, prefix, m_curr_doc);
573 if(C4_UNLIKELY(!m_tree->m_tag_directives.add(handle, prefix, m_curr_doc)))
574 _RYML_ERR_PARSE_(m_stack.m_callbacks, m_curr->pos,
"too many %TAG directives");
586 _RYML_ASSERT_BASIC_(m_stack.m_callbacks, m_tree);
587 return m_tree->m_arena.first(m_tree->m_arena_pos);
591 _RYML_ASSERT_BASIC_(m_stack.m_callbacks, m_tree);
592 return m_tree->m_arena.sub(m_tree->m_arena_pos);
596 return m_tree->alloc_arena(len);
604 void _reset_parser_state(state* st,
id_type parse_root,
id_type node)
606 _RYML_ASSERT_BASIC_(m_stack.m_callbacks, m_tree);
607 _set_state_(st, node);
611 _c4dbgpf(
"resetting state: initial flags={}", detail::_parser_flags_to_str(flagbuf, st->flags));
615 _c4dbgpf(
"node[{}] is notype", node);
616 if(m_tree->is_root(parse_root))
618 _c4dbgpf(
"node[{}] is root", node);
623 _c4dbgpf(
"node[{}] is not root. setting USTY", node);
629 _c4dbgpf(
"node[{}] is map", node);
634 _c4dbgpf(
"node[{}] is map", node);
639 _c4dbgpf(
"node[{}] has key. setting USTY", node);
644 _RYML_ERR_VISIT_(m_tree->callbacks(), m_tree, node,
"cannot append to node");
648 _c4dbgpf(
"node[{}] is doc", node);
652 _c4dbgpf(
"resetting state: final flags={}", detail::_parser_flags_to_str(flagbuf, st->flags));
661 NodeData
const* prev = m_tree->m_buf;
662 m_curr->node_id = m_tree->_append_child__unprotected(m_parent->node_id);
663 m_curr->tr_data = m_tree->_p(m_curr->node_id);
664 if(prev != m_tree->m_buf)
665 _refresh_after_relocation();
666 _c4dbgpf(
"pushed! level={}. top is now node={} (parent={})", m_curr->level, m_curr->node_id, m_parent ? m_parent->node_id :
NONE);
671 _remove_speculative_with_parent();
677 template<type_bits bits> C4_HOT C4_ALWAYS_INLINE
void _enable__() noexcept
679 m_curr->tr_data->m_type.type =
static_cast<NodeType_e>(m_curr->tr_data->m_type.type | bits);
681 template<type_bits bits> C4_HOT C4_ALWAYS_INLINE
void _disable__() noexcept
683 m_curr->tr_data->m_type.type =
static_cast<NodeType_e>(m_curr->tr_data->m_type.type & (~bits));
685 template<type_bits bits> C4_HOT C4_ALWAYS_INLINE
bool _has_any__() const noexcept
687 return (m_curr->tr_data->m_type.type & bits) != 0;
692 C4_ALWAYS_INLINE
void _set_state_(state *C4_RESTRICT s,
id_type id)
const noexcept
695 s->tr_data = m_tree->_p(
id);
697 void _refresh_after_relocation()
699 _c4dbgp(
"tree: refreshing stack data after tree data relocation");
700 for(
auto &st : m_stack)
701 st.tr_data = m_tree->_p(st.node_id);
704 void _set_root_as_stream()
706 _c4dbgp(
"set root as stream");
707 _RYML_ASSERT_VISIT_(m_tree->callbacks(), m_tree->root_id() == 0u, m_tree, m_tree->root_id());
708 _RYML_ASSERT_VISIT_(m_tree->callbacks(), m_curr->node_id == 0u, m_tree, m_curr->node_id);
709 m_tree->set_root_as_stream();
710 _RYML_ASSERT_VISIT_(m_tree->callbacks(), m_tree->is_stream(m_tree->root_id()), m_tree, m_tree->root_id());
711 _RYML_ASSERT_VISIT_(m_tree->callbacks(), m_tree->has_children(m_tree->root_id()), m_tree, m_tree->root_id());
712 _RYML_ASSERT_VISIT_(m_tree->callbacks(), m_tree->is_doc(m_tree->first_child(m_tree->root_id())), m_tree, m_tree->root_id());
713 _set_state_(m_curr, m_tree->root_id());
716 static NodeData _val2key_(NodeData
const& C4_RESTRICT d) noexcept
722 static_assert((
_VALMASK >> 1u) ==
_KEYMASK,
"required for this function to work");
726 r.m_type.type = (r.m_type.type |
KEY);
727 if(d.m_type.type &
VALNIL)
728 r.m_type.type = (r.m_type.type |
KEYNIL);
732 void _remove_speculative()
734 _RYML_ASSERT_BASIC_(m_stack.m_callbacks, m_tree);
735 _RYML_ASSERT_BASIC_(m_tree->callbacks(), !m_tree->empty());
736 const id_type last_added = m_tree->size() - 1;
737 const NodeData *C4_RESTRICT d = m_tree->_p(last_added);
738 if(d->m_parent !=
NONE && d->m_type ==
NOTYPE)
740 _c4dbgpf(
"remove speculative: currparent={} node={} parent(node)={}", m_parent->node_id, last_added, d->m_parent);
741 m_tree->remove(last_added);
746 void _remove_speculative_with_parent()
748 _RYML_ASSERT_BASIC_(m_stack.m_callbacks, m_tree);
749 _RYML_ASSERT_BASIC_(m_tree->callbacks(), !m_tree->empty());
750 const id_type last_added = m_tree->size() - 1;
751 _RYML_ASSERT_VISIT_(m_tree->callbacks(), m_tree->has_parent(last_added), m_tree, last_added);
752 if(m_tree->_p(last_added)->m_type ==
NOTYPE)
754 _c4dbgpf(
"remove speculative node with parent. parent={} node={} parent(node)={}", m_parent->node_id, last_added, m_tree->parent(last_added));
755 m_tree->remove(last_added);
760 C4_ALWAYS_INLINE
void _save_loc()
762 _RYML_ASSERT_BASIC_(m_stack.m_callbacks, m_tree);
763 _RYML_ASSERT_BASIC_(m_tree->callbacks(), m_tree->_p(m_curr->node_id)->m_val.scalar.len == 0);
764 m_tree->_p(m_curr->node_id)->m_val.scalar.str = m_curr->line_contents.rem.str;
780 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,...
@ FLOW_ML
mark container with multi-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)
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...
(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.
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 mark_val_scalar_unfiltered() noexcept
void end_doc_expl()
explicit doc end, with ...
void end_seq_flow(bool multiline)
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 set_val_tag(csubstr tag)
Callbacks const & callbacks() const
void begin_doc_expl()
explicit doc start, with —
void set_key_tag(csubstr tag)
void begin_map_val_flow()
void end_stream() const noexcept
void set_val_scalar_dquoted(csubstr scalar) noexcept
void begin_seq_val_flow()
void set_val_scalar_plain_empty() noexcept
void begin_map_key_block()
void start_parse(const char *filename, substr ymlsrc)
void begin_seq_key_block()
void end_map_flow(bool multiline)
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)
TagDirectives & tag_directives()
void set_key_scalar_literal(csubstr scalar) noexcept
void actually_val_is_first_key_of_new_map_flow()
reset 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_scalar_folded(csubstr scalar) noexcept
void begin_stream() const noexcept
void add_directive_tag(csubstr handle, csubstr prefix)
void add_directive_yaml(csubstr yaml_version)
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
Accelerator structure to reduce memory requirements by enabling reuse of resolved tags.