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_GCC_PUSH
13 C4_SUPPRESS_WARNING_MSVC_PUSH
14 C4_SUPPRESS_WARNING_MSVC(4702)
15 #if defined(__GNUC__) && __GNUC__ >= 6
16 C4_SUPPRESS_WARNING_GCC(
"-Wnull-dereference")
29 struct EventHandlerTreeState :
public ParserState
45 using state = EventHandlerTreeState;
53 Tree *C4_RESTRICT m_tree;
58 #define _enable_(bits) _enable__<bits>(); _c4dbgpf("node[{}]: enable {}", m_curr->node_id, #bits)
59 #define _disable_(bits) _disable__<bits>(); _c4dbgpf("node[{}]: disable {}", m_curr->node_id, #bits)
61 #define _enable_(bits) _enable__<bits>()
62 #define _disable_(bits) _disable__<bits>()
64 #define _has_any_(bits) _has_any__<bits>()
81 if(C4_UNLIKELY(!tree))
82 _RYML_ERR_BASIC_(
m_stack.m_callbacks,
"null tree");
83 if(C4_UNLIKELY(
id >= tree->
capacity()))
84 _RYML_ERR_BASIC_(tree->
callbacks(),
"invalid node");
85 if(C4_UNLIKELY(!tree->
is_root(
id)))
87 if(C4_UNLIKELY(!tree->
has_key(
id)))
88 _RYML_ERR_BASIC_(tree->
callbacks(),
"destination node belongs to a map and has no key");
90 if(m_tree->is_root(
id))
93 _reset_parser_state(
m_curr,
id, m_tree->root_id());
98 _reset_parser_state(
m_parent,
id, m_tree->parent(
id));
99 _reset_parser_state(
m_curr,
id,
id);
101 m_curr_doc = m_tree->ancestor_doc(
id);
119 _RYML_ASSERT_BASIC_(
m_stack.m_callbacks, m_tree !=
nullptr);
125 _RYML_ASSERT_BASIC_(
m_stack.m_callbacks, m_tree !=
nullptr);
168 _c4dbgp(
"begin_doc");
172 _set_root_as_stream();
176 m_curr_doc =
m_curr->node_id;
182 m_curr_doc = m_tree->size();
185 _remove_speculative();
194 _c4dbgp(
"begin_doc_expl");
195 _RYML_ASSERT_VISIT_(
m_stack.m_callbacks, m_tree->root_id() ==
m_curr->node_id, m_tree,
m_curr->node_id);
196 if(m_tree->is_stream(m_tree->root_id()))
203 _c4dbgp(
"ensure stream");
204 _set_root_as_stream();
205 const id_type root = m_tree->root_id();
206 const id_type first = m_tree->first_child(root);
207 _RYML_ASSERT_VISIT_(
m_stack.m_callbacks, m_tree->is_stream(root), m_tree, root);
208 _RYML_ASSERT_VISIT_(
m_stack.m_callbacks, m_tree->num_children(root) == 1u, m_tree, root);
209 if(m_tree->is_container(first) || m_tree->is_val(first))
213 #ifdef RYML_WITH_COMMENTS
214 m_tree->_p(root)->m_first_comment =
NONE;
215 m_tree->_p(root)->m_last_comment =
NONE;
222 _remove_speculative();
223 m_curr->node_id = m_tree->last_child(root);
228 m_curr_doc =
m_curr->node_id;
233 _c4dbgp(
"end_doc_expl");
234 m_curr_doc = m_tree->size();
235 _remove_speculative();
252 _RYML_ERR_PARSE_(
m_stack.m_callbacks,
m_curr->pos,
"ryml trees cannot handle containers as keys");
256 _RYML_ERR_PARSE_(
m_stack.m_callbacks,
m_curr->pos,
"ryml trees cannot handle containers as keys");
261 _c4dbgpf(
"node[{}]: begin_map_val_flow",
m_curr->node_id);
269 _c4dbgpf(
"node[{}]: begin_map_val_block",
m_curr->node_id);
284 _c4dbgpf(
"node[{}]: end_map. multiline={} startline={} endline={}",
m_parent->node_id, multiline,
m_parent->pos.line,
m_curr->pos.line);
302 _RYML_ERR_PARSE_(
m_stack.m_callbacks,
m_curr->pos,
"ryml trees cannot handle containers as keys");
306 _RYML_ERR_PARSE_(
m_stack.m_callbacks,
m_curr->pos,
"ryml trees cannot handle containers as keys");
311 _c4dbgpf(
"node[{}]: begin_seq_val_flow",
m_curr->node_id);
319 _c4dbgpf(
"node[{}]: begin_seq_val_block",
m_curr->node_id);
334 _c4dbgpf(
"node[{}]: end_seq. multiline={} startline={} endline={}",
m_parent->node_id, multiline,
m_parent->pos.line,
m_curr->pos.line);
352 _RYML_ASSERT_BASIC_(
m_stack.m_callbacks, m_tree);
355 NodeData const*
const prev = m_tree->m_buf;
356 _set_state_(
m_curr, m_tree->_append_child__unprotected(
m_parent->node_id));
357 if(prev != m_tree->m_buf)
358 _refresh_after_relocation();
359 _c4dbgpf(
"node[{}]: added sibling={} prev={}",
m_parent->node_id,
m_curr->node_id, m_tree->prev_sibling(
m_curr->node_id));
369 if(C4_UNLIKELY(m_tree->is_container(
m_curr->node_id)))
370 _RYML_ERR_PARSE_(
m_stack.m_callbacks,
m_curr->pos,
"ryml trees cannot handle containers as keys");
373 _RYML_ASSERT_VISIT_(
m_stack.m_callbacks, !m_tree->is_container(
m_curr->node_id), m_tree,
m_curr->node_id);
374 _RYML_ASSERT_VISIT_(
m_stack.m_callbacks, !m_tree->has_key(
m_curr->node_id), m_tree,
m_curr->node_id);
377 m_curr->tr_data->m_val = {};
391 _RYML_ERR_PARSE_(
m_stack.m_callbacks,
m_curr->pos,
"ryml trees cannot handle containers as keys");
404 _c4dbgpf(
"node[{}]: set key scalar plain as empty",
m_curr->node_id);
405 m_curr->tr_data->m_key.scalar = {};
410 _c4dbgpf(
"node[{}]: set val scalar plain as empty",
m_curr->node_id);
411 m_curr->tr_data->m_val.scalar = {};
417 _c4dbgpf(
"node[{}]: set key scalar plain: [{}]~~~{}~~~",
m_curr->node_id, scalar.len, scalar);
418 m_curr->tr_data->m_key.scalar = scalar;
423 _c4dbgpf(
"node[{}]: set val scalar plain: [{}]~~~{}~~~",
m_curr->node_id, scalar.len, scalar);
424 m_curr->tr_data->m_val.scalar = scalar;
431 _c4dbgpf(
"node[{}]: set key scalar dquot: [{}]~~~{}~~~",
m_curr->node_id, scalar.len, scalar);
432 m_curr->tr_data->m_key.scalar = scalar;
437 _c4dbgpf(
"node[{}]: set val scalar dquot: [{}]~~~{}~~~",
m_curr->node_id, scalar.len, scalar);
438 m_curr->tr_data->m_val.scalar = scalar;
445 _c4dbgpf(
"node[{}]: set key scalar squot: [{}]~~~{}~~~",
m_curr->node_id, scalar.len, scalar);
446 m_curr->tr_data->m_key.scalar = scalar;
451 _c4dbgpf(
"node[{}]: set val scalar squot: [{}]~~~{}~~~",
m_curr->node_id, scalar.len, scalar);
452 m_curr->tr_data->m_val.scalar = scalar;
459 _c4dbgpf(
"node[{}]: set key scalar literal: [{}]~~~{}~~~",
m_curr->node_id, scalar.len, scalar);
460 m_curr->tr_data->m_key.scalar = scalar;
465 _c4dbgpf(
"node[{}]: set val scalar literal: [{}]~~~{}~~~",
m_curr->node_id, scalar.len, scalar);
466 m_curr->tr_data->m_val.scalar = scalar;
473 _c4dbgpf(
"node[{}]: set key scalar folded: [{}]~~~{}~~~",
m_curr->node_id, scalar.len, scalar);
474 m_curr->tr_data->m_key.scalar = scalar;
479 _c4dbgpf(
"node[{}]: set val scalar folded: [{}]~~~{}~~~",
m_curr->node_id, scalar.len, scalar);
480 m_curr->tr_data->m_val.scalar = scalar;
503 _c4dbgpf(
"node[{}]: set key anchor: [{}]~~~{}~~~",
m_curr->node_id, anchor.len, anchor);
504 _RYML_ASSERT_BASIC_(
m_stack.m_callbacks, m_tree);
506 _RYML_ASSERT_PARSE_(m_tree->callbacks(), !anchor.begins_with(
'&'),
m_curr->pos);
508 m_curr->tr_data->m_key.anchor = anchor;
512 _c4dbgpf(
"node[{}]: set val anchor: [{}]~~~{}~~~",
m_curr->node_id, anchor.len, anchor);
513 _RYML_ASSERT_BASIC_(
m_stack.m_callbacks, m_tree);
515 _RYML_ASSERT_PARSE_(m_tree->callbacks(), !anchor.begins_with(
'&'),
m_curr->pos);
517 m_curr->tr_data->m_val.anchor = anchor;
522 _c4dbgpf(
"node[{}]: set key ref: [{}]~~~{}~~~",
m_curr->node_id, ref.len, ref);
523 _RYML_ASSERT_BASIC_(
m_stack.m_callbacks, m_tree);
525 _RYML_ERR_PARSE_(m_tree->callbacks(),
m_curr->pos,
"key cannot have both anchor and ref");
526 _RYML_ASSERT_PARSE_(m_tree->callbacks(), ref.begins_with(
'*'),
m_curr->pos);
528 m_curr->tr_data->m_key.anchor = ref.sub(1);
529 m_curr->tr_data->m_key.scalar = ref;
533 _c4dbgpf(
"node[{}]: set val ref: [{}]~~~{}~~~",
m_curr->node_id, ref.len, ref);
534 _RYML_ASSERT_BASIC_(
m_stack.m_callbacks, m_tree);
536 _RYML_ERR_PARSE_(m_tree->callbacks(),
m_curr->pos,
"val cannot have both anchor and ref");
537 _RYML_ASSERT_PARSE_(m_tree->callbacks(), ref.begins_with(
'*'),
m_curr->pos);
539 m_curr->tr_data->m_val.anchor = ref.sub(1);
540 m_curr->tr_data->m_val.scalar = ref;
552 _c4dbgpf(
"node[{}]: set key tag: [{}]~~~{}~~~",
m_curr->node_id, tag.len, tag);
554 m_curr->tr_data->m_key.tag = tag;
558 _c4dbgpf(
"node[{}]: set val tag: [{}]~~~{}~~~",
m_curr->node_id, tag.len, tag);
560 m_curr->tr_data->m_val.tag = tag;
572 _c4dbgpf(
"%YAML directive! version={}", yaml_version);
578 _c4dbgpf(
"%TAG directive! handle={} prefix={} id={}", handle, prefix, m_curr_doc);
579 if(C4_UNLIKELY(!m_tree->m_tag_directives.add(handle, prefix, m_curr_doc)))
580 _RYML_ERR_PARSE_(
m_stack.m_callbacks,
m_curr->pos,
"too many %TAG directives");
592 _RYML_ASSERT_BASIC_(
m_stack.m_callbacks, m_tree);
593 return m_tree->m_arena.first(m_tree->m_arena_pos);
597 _RYML_ASSERT_BASIC_(
m_stack.m_callbacks, m_tree);
598 return m_tree->m_arena.sub(m_tree->m_arena_pos);
602 return m_tree->alloc_arena(len);
612 _RYML_ASSERT_BASIC_(
m_stack.m_callbacks, m_tree);
613 _set_state_(st, node);
617 _c4dbgpf(
"resetting state: initial flags={}", detail::_parser_flags_to_str(flagbuf, st->flags));
621 _c4dbgpf(
"node[{}] is notype", node);
622 if(m_tree->is_root(parse_root))
624 _c4dbgpf(
"node[{}] is root", node);
629 _c4dbgpf(
"node[{}] is not root. setting USTY", node);
635 _c4dbgpf(
"node[{}] is map", node);
640 _c4dbgpf(
"node[{}] is map", node);
645 _c4dbgpf(
"node[{}] has key. setting USTY", node);
650 _RYML_ERR_VISIT_(m_tree->callbacks(), m_tree, node,
"cannot append to node");
654 _c4dbgpf(
"node[{}] is doc", node);
658 _c4dbgpf(
"resetting state: final flags={}", detail::_parser_flags_to_str(flagbuf, st->flags));
667 NodeData
const* prev = m_tree->m_buf;
668 m_curr->node_id = m_tree->_append_child__unprotected(
m_parent->node_id);
670 if(prev != m_tree->m_buf)
671 _refresh_after_relocation();
677 _remove_speculative_with_parent();
683 template<type_bits bits> C4_HOT C4_ALWAYS_INLINE
void _enable__() noexcept
687 template<type_bits bits> C4_HOT C4_ALWAYS_INLINE
void _disable__() noexcept
691 template<type_bits bits> C4_HOT C4_ALWAYS_INLINE
bool _has_any__() const noexcept
693 return (
m_curr->tr_data->m_type.type & bits) != 0;
698 C4_ALWAYS_INLINE
void _set_state_(
state *C4_RESTRICT s,
id_type id)
const noexcept
701 s->tr_data = m_tree->_p(
id);
703 void _refresh_after_relocation()
705 _c4dbgp(
"tree: refreshing stack data after tree data relocation");
707 st.tr_data = m_tree->_p(st.node_id);
710 void _set_root_as_stream()
712 _c4dbgp(
"set root as stream");
713 _RYML_ASSERT_VISIT_(m_tree->callbacks(), m_tree->root_id() == 0u, m_tree, m_tree->root_id());
714 _RYML_ASSERT_VISIT_(m_tree->callbacks(),
m_curr->node_id == 0u, m_tree,
m_curr->node_id);
715 m_tree->set_root_as_stream();
716 _RYML_ASSERT_VISIT_(m_tree->callbacks(), m_tree->is_stream(m_tree->root_id()), m_tree, m_tree->root_id());
717 _RYML_ASSERT_VISIT_(m_tree->callbacks(), m_tree->has_children(m_tree->root_id()), m_tree, m_tree->root_id());
718 _RYML_ASSERT_VISIT_(m_tree->callbacks(), m_tree->is_doc(m_tree->first_child(m_tree->root_id())), m_tree, m_tree->root_id());
719 _set_state_(
m_curr, m_tree->root_id());
722 static NodeData _val2key_(NodeData
const& C4_RESTRICT d) noexcept
728 static_assert((
_VALMASK >> 1u) ==
_KEYMASK,
"required for this function to work");
732 r.m_type.type = (r.m_type.type |
KEY);
733 if(d.m_type.type &
VALNIL)
734 r.m_type.type = (r.m_type.type |
KEYNIL);
738 void _remove_speculative()
740 _RYML_ASSERT_BASIC_(
m_stack.m_callbacks, m_tree);
741 _RYML_ASSERT_BASIC_(m_tree->callbacks(), !m_tree->empty());
742 const id_type last_added = m_tree->size() - 1;
743 const NodeData *C4_RESTRICT d = m_tree->_p(last_added);
744 if(d->m_parent !=
NONE && d->m_type ==
NOTYPE)
746 _c4dbgpf(
"remove speculative: currparent={} node={} parent(node)={}",
m_parent->node_id, last_added, d->m_parent);
747 m_tree->remove(last_added);
752 void _remove_speculative_with_parent()
754 _RYML_ASSERT_BASIC_(
m_stack.m_callbacks, m_tree);
755 _RYML_ASSERT_BASIC_(m_tree->callbacks(), !m_tree->empty());
756 const id_type last_added = m_tree->size() - 1;
757 _RYML_ASSERT_VISIT_(m_tree->callbacks(), m_tree->has_parent(last_added), m_tree, last_added);
758 if(m_tree->_p(last_added)->m_type ==
NOTYPE)
760 _c4dbgpf(
"remove speculative node with parent. parent={} node={} parent(node)={}",
m_parent->node_id, last_added, m_tree->parent(last_added));
761 m_tree->remove(last_added);
766 C4_ALWAYS_INLINE
void _save_loc()
768 _RYML_ASSERT_BASIC_(
m_stack.m_callbacks, m_tree);
769 _RYML_ASSERT_BASIC_(m_tree->callbacks(), m_tree->_p(
m_curr->node_id)->m_val.scalar.len == 0);
770 m_tree->_p(
m_curr->node_id)->m_val.scalar.str =
m_curr->line_contents.rem.str;
786 C4_SUPPRESS_WARNING_MSVC_POP
787 C4_SUPPRESS_WARNING_GCC_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.
void _stack_finish_parse()
void _stack_reset_non_root()
state * m_curr
current stack level: top of the stack. cached here for easier access.
bool _stack_should_push_on_begin_doc() const
void _stack_start_parse(const char *filename, substr ymlsrc)
detail::stack< state > m_stack
state * m_parent
parent of the current stack level.
bool _stack_should_pop_on_end_doc() const
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()
@ requires_strings_on_buffers
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.