1 #ifndef _C4_YML_EXTRA_EVENT_HANDLER_INTS_HPP_
2 #define _C4_YML_EXTRA_EVENT_HANDLER_INTS_HPP_
12 #ifndef RYML_SINGLE_HEADER
13 #ifndef _C4_YML_NODE_TYPE_HPP_
16 #ifndef _C4_YML_EVENT_HANDLER_STACK_HPP_
19 #ifndef _C4_YML_TAG_HPP_
22 #ifndef _C4_YML_DETAIL_DBGPRINT_HPP_
23 #include <c4/yml/detail/dbgprint.hpp>
151 C4_SUPPRESS_WARNING_GCC_CLANG_PUSH
152 C4_SUPPRESS_WARNING_GCC_CLANG(
"-Wold-style-cast")
153 C4_SUPPRESS_WARNING_GCC("-Wuseless-cast")
396 using state = EventHandlerIntsState;
411 bool m_has_yaml_directive;
415 #define _enable_(bits) _enable__<bits>()
416 #define _disable_(bits) _disable__<bits>()
417 #define _has_any_(bits) _has_any__<bits>()
428 reset(csubstr{}, substr{},
nullptr, 0);
439 m_curr->evt_type = {};
445 m_evt_size = dst_size;
449 m_has_yaml_directive =
false;
473 return m_evt_pos <= m_evt_size && m_arena_pos <= m_arena.len;
490 this->_stack_start_parse(filename, relocate_arena, relocate_arena_data);
495 if((_num_tag_directives() || m_has_yaml_directive) && !m_has_docs)
496 _RYML_CB_ERR_(m_stack.m_callbacks,
"directives cannot be used without a document", {});
497 this->_stack_finish_parse();
502 while(m_stack.size() > 1)
533 _c4dbgpf(
"{}/{}: begin_doc", m_evt_pos, m_evt_size);
535 if(_stack_should_push_on_begin_doc())
546 _c4dbgpf(
"{}/{}: end_doc", m_evt_pos, m_evt_size);
548 if(_stack_should_pop_on_end_doc())
558 _c4dbgpf(
"{}/{}: begin_doc_expl", m_evt_pos, m_evt_size);
568 _c4dbgpf(
"{}/{}: end_doc_expl", m_evt_pos, m_evt_size);
570 if(_stack_should_pop_on_end_doc())
575 m_has_yaml_directive =
false;
587 _c4dbgpf(
"{}/{}: bmap key flow", m_evt_pos, m_evt_size);
589 _mark_parent_with_children_();
595 _c4dbgpf(
"{}/{}: bmap key block", m_evt_pos, m_evt_size);
597 _mark_parent_with_children_();
604 _c4dbgpf(
"{}/{}: bmap flow", m_evt_pos, m_evt_size);
606 _mark_parent_with_children_();
612 _c4dbgpf(
"{}/{}: bmap block", m_evt_pos, m_evt_size);
614 _mark_parent_with_children_();
634 _c4dbgpf(
"{}/{}: bseq key flow", m_evt_pos, m_evt_size);
636 _mark_parent_with_children_();
642 _c4dbgpf(
"{}/{}: bseq key block", m_evt_pos, m_evt_size);
644 _mark_parent_with_children_();
651 _c4dbgpf(
"{}/{}: bseq flow", m_evt_pos, m_evt_size);
653 _mark_parent_with_children_();
659 _c4dbgpf(
"{}/{}: bseq block", m_evt_pos, m_evt_size);
661 _mark_parent_with_children_();
681 _RYML_CB_ASSERT(m_stack.m_callbacks, m_parent);
682 m_curr->evt_type = {};
692 _c4dbgpf(
"{}/{}: prev={} actually_val_is_first_key_of_new_map_flow", m_evt_pos, m_evt_size, m_evt_prev);
693 _RYML_CB_ASSERT(m_stack.m_callbacks, m_evt_pos > 2);
694 _RYML_CB_ASSERT(m_stack.m_callbacks, m_evt_prev > 0);
703 if(m_evt_prev + 1 < m_evt_size)
707 _c4dbgpf(
"{}/{}: WSTR", m_evt_pos, m_evt_size);
708 _RYML_CB_ASSERT(m_stack.m_callbacks, m_evt_prev > 0);
709 int32_t pos = _extend_left_to_include_tag_and_or_anchor(m_evt_prev);
710 if(m_evt_pos + 1 < m_evt_size)
712 for(int32_t i = pos; i <= m_evt_prev; i = _next(i))
717 int32_t num_move = m_evt_pos + 1 - pos;
718 _RYML_CB_ASSERT(m_stack.m_callbacks, num_move > 0);
719 memmove(m_evt + pos + 1, m_evt + pos, (
size_t)num_move *
sizeof(
ievt::DataType));
726 m_evt[pos + 1] &= ~ievt
::PSTR;
731 _c4dbgpf(
"{}/{}: container key", m_evt_pos, m_evt_size);
734 _c4dbgpf(
"{}/{}: find matching open for {}", m_evt_pos, m_evt_size, m_evt_prev);
741 _RYML_CB_ASSERT(m_stack.m_callbacks, (m_evt[m_evt_prev] &
ievt::ESEQ));
744 _c4dbgpf(
"{}/{}: matching open for {}={}", m_evt_pos, m_evt_size, m_evt_prev, pos);
745 _RYML_CB_CHECK(m_stack.m_callbacks, pos >= 0);
746 _RYML_CB_CHECK(m_stack.m_callbacks, pos < m_evt_prev);
747 _RYML_CB_ASSERT(m_stack.m_callbacks, (m_evt[pos] &
ievt::ESEQ) == (m_evt[m_evt_prev] &
ievt::BSEQ));
748 _RYML_CB_ASSERT(m_stack.m_callbacks, (m_evt[pos] &
ievt::EMAP) == (m_evt[m_evt_prev] &
ievt::BMAP));
750 int32_t posp1 = pos + 1;
751 if(m_evt_pos + 1 < m_evt_size)
753 int32_t num_move = m_evt_pos + 1 - pos;
754 _RYML_CB_ASSERT(m_stack.m_callbacks, num_move > 0);
755 memmove(m_evt + posp1, m_evt + pos, (
size_t)num_move *
sizeof(
ievt::DataType));
757 _RYML_CB_ASSERT(m_stack.m_callbacks, posp1 < m_evt_pos);
762 m_evt[posp1] &= ~ievt
::VAL_;
767 m_evt[posp1] &= ~ievt
::PSTR;
771 m_curr->evt_id = m_evt_pos - 2;
786 if(m_evt_prev < m_evt_size)
789 int32_t pos = _find_last_bdoc(m_evt_pos);
792 _RYML_CB_ASSERT(m_stack.m_callbacks, pos < m_evt_size);
793 _RYML_CB_ASSERT(m_stack.m_callbacks, pos < m_evt_pos);
794 _RYML_CB_ASSERT(m_stack.m_callbacks, (m_evt[pos] &
ievt::BDOC));
795 if(m_evt_pos < m_evt_size)
798 int32_t num_move = m_evt_pos - pos;
799 int32_t posp1 = pos + 1;
801 _RYML_CB_ASSERT(m_stack.m_callbacks, num_move > 0);
802 _RYML_CB_ASSERT(m_stack.m_callbacks, 0 == (m_evt[posp1] &
ievt::PSTR));
803 memmove(m_evt + posp1, m_evt + pos, (
size_t)num_move *
sizeof(
ievt::DataType));
805 m_evt[posp1] &= ~ievt
::VAL_;
821 int32_t _find_last_bdoc(int32_t pos)
const
823 _RYML_CB_ASSERT(m_stack.m_callbacks, m_evt_prev < m_evt_size);
835 _c4dbgpf(
"find_matching: start at {}", pos);
836 _RYML_CB_ASSERT(m_stack.m_callbacks, (m_evt[pos] & close));
837 _RYML_CB_ASSERT(m_stack.m_callbacks, !(m_evt[pos] & open));
843 _c4dbgpf(
"find_matching: pos={} count={} e={}", pos, count, m_evt[pos]);
846 _c4dbgpf(
".............: pos={} close! count={} e={}", pos, count, m_evt[pos]);
851 _c4dbgpf(
".............: pos={} open! count={} e={}", pos, count, m_evt[pos]);
859 _c4dbgpf(
"find_matching: not found!", 0);
862 int32_t _extend_left_to_include_tag_and_or_anchor(int32_t pos)
const
864 int32_t prev = _prev(pos);
867 _c4dbgpf(
"{}/{}: {} is anchor/tag. extend to {}", m_evt_pos, m_evt_size, prev, prev);
873 C4_ALWAYS_INLINE int32_t _next(int32_t pos)
const noexcept
875 _RYML_CB_ASSERT(m_stack.m_callbacks, pos < m_evt_size);
876 return pos + ((m_evt[pos] &
ievt::WSTR) ? 3 : 1);
878 C4_ALWAYS_INLINE int32_t _prev(int32_t pos)
const noexcept
880 _RYML_CB_ASSERT(m_stack.m_callbacks, pos < m_evt_size);
881 return pos - ((m_evt[pos] &
ievt::PSTR) ? 3 : 1);
893 _c4dbgpf(
"{}/{}: set_key_scalar_plain_empty", m_evt_pos, m_evt_size);
894 _send_key_scalar_(_get_latest_empty_scalar(),
ievt::PLAI);
899 _c4dbgpf(
"{}/{}: set_val_scalar_plain_empty", m_evt_pos, m_evt_size);
900 _send_val_scalar_(_get_latest_empty_scalar(),
ievt::PLAI);
908 return m_str.first(0);
914 _c4dbgpf(
"{}/{}: set_key_scalar_plain: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.str-m_str.str, scalar.len, scalar);
920 _c4dbgpf(
"{}/{}: set_val_scalar_plain: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.str-m_str.str, scalar.len, scalar);
928 _c4dbgpf(
"{}/{}: set_key_scalar_dquo: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.str-m_str.str, scalar.len, scalar);
934 _c4dbgpf(
"{}/{}: set_val_scalar_dquo: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.str-m_str.str, scalar.len, scalar);
942 _c4dbgpf(
"{}/{}: set_key_scalar_squo: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.str-m_str.str, scalar.len, scalar);
948 _c4dbgpf(
"{}/{}: set_val_scalar_squo: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.str-m_str.str, scalar.len, scalar);
956 _c4dbgpf(
"{}/{}: set_key_scalar_literal: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.str-m_str.str, scalar.len, scalar);
962 _c4dbgpf(
"{}/{}: set_val_scalar_literal: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.str-m_str.str, scalar.len, scalar);
970 _c4dbgpf(
"{}/{}: set_key_scalar_folded: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.str-m_str.str, scalar.len, scalar);
976 _c4dbgpf(
"{}/{}: set_val_scalar_folded: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.str-m_str.str, scalar.len, scalar);
984 _c4dbgpf(
"{}/{}: mark_key_scalar_unfiltered", m_evt_pos, m_evt_size);
985 if(m_evt_pos < m_evt_size)
990 _c4dbgpf(
"{}/{}: mark_val_scalar_unfiltered", m_evt_pos, m_evt_size);
991 if(m_evt_pos < m_evt_size)
1000 #define _add_scalar_(i, scalar) \
1001 _c4dbgpf("{}/{}: scalar!", i, m_evt_size); \
1002 _RYML_CB_ASSERT(m_stack.m_callbacks, scalar.is_sub(m_str) || scalar.is_sub(m_arena) || (scalar.str == nullptr)); \
1003 _RYML_CB_ASSERT(m_stack.m_callbacks, m_evt[i] & ievt::WSTR); \
1004 _RYML_CB_ASSERT(m_stack.m_callbacks, i + 3 < m_evt_size); \
1005 if(C4_LIKELY(scalar.is_sub(m_str))) \
1007 m_evt[i + 1] = (ievt::DataType)(scalar.str - m_str.str); \
1011 m_evt[i] |= ievt::AREN; \
1012 m_evt[i + 1] = (ievt::DataType)(scalar.str - m_arena.str); \
1013 _c4dbgpf("{}/{}: arena! ->{}", i, m_evt_size, m_evt[i+1]); \
1015 m_evt[i + 2] = (ievt::DataType)scalar.len; \
1016 m_evt[i + 3] = ievt::PSTR
1024 _c4dbgpf(
"{}/{}: set_key_anchor", m_evt_pos, m_evt_size);
1027 if(m_evt_pos + 3 < m_evt_size)
1030 _add_scalar_(m_evt_pos, anchor);
1032 m_evt_prev = m_evt_pos;
1037 _c4dbgpf(
"{}/{}: set_val_anchor", m_evt_pos, m_evt_size);
1040 if(m_evt_pos + 3 < m_evt_size)
1043 _add_scalar_(m_evt_pos, anchor);
1045 m_evt_prev = m_evt_pos;
1051 _RYML_CB_ASSERT(m_stack.m_callbacks, ref.begins_with(
'*'));
1053 _RYML_CB_ERR_(m_stack.m_callbacks,
"key cannot have both anchor and ref", m_curr->pos);
1059 _RYML_CB_ASSERT(m_stack.m_callbacks, ref.begins_with(
'*'));
1061 _RYML_CB_ERR_(m_stack.m_callbacks,
"val cannot have both anchor and ref", m_curr->pos);
1075 _c4dbgpf(
"{}/{}: set key tag ~~~{}~~~", m_evt_pos, m_evt_size, tag);
1081 _c4dbgpf(
"{}/{}: set val tag [{}]~~~{}~~~", m_evt_pos, m_evt_size, tag.len, tag);
1087 csubstr ttag = _transform_directive(tag);
1088 _c4dbgpf(
"{}/{}: transformed_tag [{}]~~~{}~~~", m_evt_pos, m_evt_size, ttag.len, ttag);
1089 if(m_evt_pos + 3 < m_evt_size)
1092 _add_scalar_(m_evt_pos, ttag);
1094 m_evt_prev = m_evt_pos;
1107 _c4dbgpf(
"{}/{}: add directive ~~~{}~~~", m_evt_pos, m_evt_size, directive);
1108 _RYML_CB_ASSERT(m_stack.m_callbacks, directive.begins_with(
'%'));
1109 if(directive.begins_with(
"%TAG"))
1111 const id_type pos = _num_tag_directives();
1113 _RYML_CB_ERR_(m_stack.m_callbacks,
"too many directives", m_curr->pos);
1116 _RYML_CB_ERR_(m_stack.m_callbacks,
"failed to add directive", m_curr->pos);
1121 else if(directive.begins_with(
"%YAML"))
1123 _c4dbgpf(
"%YAML directive! ignoring...: {}", directive);
1124 if(C4_UNLIKELY(m_has_yaml_directive))
1125 _RYML_CB_ERR_(m_stack.m_callbacks,
"multiple yaml directives", m_curr->pos);
1126 m_has_yaml_directive =
true;
1127 csubstr rest = directive.sub(5).triml(
' ');
1132 _c4dbgpf(
"unknown directive! ignoring... {}", directive);
1145 return C4_LIKELY(m_arena_pos <= m_arena.len) ? m_arena.sub(m_arena_pos) : m_arena.last(0);
1151 substr s = arena_rem();
1152 if(C4_LIKELY(len <= s.len))
1164 return alloc_arena(len);
1176 m_curr->evt_type = {};
1185 template<c4::yml::type_bits bits> C4_ALWAYS_INLINE
void _enable__() noexcept
1187 m_curr->evt_type |= bits;
1189 template<c4::yml::type_bits bits> C4_ALWAYS_INLINE
void _disable__() noexcept
1191 m_curr->evt_type &= ~bits;
1193 template<c4::yml::type_bits bits> C4_ALWAYS_INLINE
bool _has_any__() const noexcept
1201 m_parent->has_children =
true;
1206 _c4dbgpf(
"{}/{}: flag only", m_evt_pos, m_evt_size);
1207 if(m_evt_pos < m_evt_size)
1208 m_evt[m_evt_pos] |= flags;
1209 m_curr->evt_id = m_evt_pos;
1210 m_evt_prev = m_evt_pos;
1212 if(m_evt_pos < m_evt_size)
1213 m_evt[m_evt_pos] = {};
1218 _c4dbgpf(
"{}/{}: key scalar", m_evt_pos, m_evt_size);
1219 if(m_evt_pos + 3 < m_evt_size)
1222 _add_scalar_(m_evt_pos, scalar);
1224 m_curr->evt_id = m_evt_pos;
1225 m_evt_prev = m_evt_pos;
1231 _c4dbgpf(
"{}/{}: val scalar", m_evt_pos, m_evt_size);
1232 if(m_evt_pos + 3 < m_evt_size)
1235 _add_scalar_(m_evt_pos, scalar);
1237 m_curr->evt_id = m_evt_pos;
1238 m_evt_prev = m_evt_pos;
1244 _c4dbgpf(
"{}/{}: send str", m_evt_pos, m_evt_size);
1245 if(m_evt_pos + 3 < m_evt_size)
1247 m_evt[m_evt_pos] |= flags;
1248 _add_scalar_(m_evt_pos, scalar);
1250 m_curr->evt_id = m_evt_pos;
1251 m_evt_prev = m_evt_pos;
1265 if(m_tag_directives[i].handle.empty())
1281 substr rem = arena_rem();
1282 size_t len = td.
transform(tag, rem, m_stack.m_callbacks,
false);
1286 return rem.first(len <= rem.len ? len : 0);
1289 if(tag.begins_with(
'!'))
1293 _RYML_CB_ERR_(m_stack.m_callbacks,
"tag not found", m_curr->pos);
1313 C4_SUPPRESS_WARNING_GCC_CLANG_POP
Callbacks const & get_callbacks()
get the global callbacks
int32_t estimate_events_ints_size(csubstr src)
Read YAML source and, without undergoing a full parse, estimate the size of the integer buffer requir...
uint32_t type_bits
the integral type necessary to cover all the bits for NodeType_e
@ VALANCH
the val has an &anchor
@ 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 >
@ KEYTAG
the key has a tag
@ FLOW_SL
mark container with single-line flow style (seqs as '[val1,val2], maps as '{key: val,...
@ 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 '
@ 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_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)
bool is_custom_tag(csubstr tag)
#define RYML_MAX_TAG_DIRECTIVES
the maximum number of tag directives in a Tree
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
a c-style callbacks class.
Use this class a base of implementations of event handler to simplify the stack logic.
bool create_from_str(csubstr directive_)
leaves next_node_id unfilled
id_type next_node_id
The next node to which this tag directive applies.
size_t transform(csubstr tag, substr output, Callbacks const &callbacks, bool with_brackets=true) const
A parser event handler that creates a compact representation of the YAML tree in a buffer of integers...
id_type _num_tag_directives() const
bool _has_any__() const noexcept
void begin_doc_expl()
explicit doc start, with —
void end_doc_expl()
explicit doc end, with ...
void set_val_anchor(csubstr anchor)
void _send_key_scalar_(csubstr scalar, ievt::DataType flags)
void set_key_scalar_literal(csubstr scalar)
void set_val_scalar_plain_empty()
void begin_map_val_block()
substr alloc_arena(size_t len, substr *relocated)
this may fail, in which case an empty string is returned
bool fits_buffers() const
Predicate to test if the event buffer successfully accomodated all the parse events.
void begin_doc()
implicit doc start (without —)
void _send_flag_only_(ievt::DataType flags)
void begin_map_key_block()
void set_key_tag(csubstr tag)
void _enable__() noexcept
substr alloc_arena(size_t len)
this may fail, in which case a an empty string is returned
void mark_val_scalar_unfiltered()
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.
void begin_seq_val_block()
void _set_tag(csubstr tag, ievt::DataType which)
void set_key_scalar_squoted(csubstr scalar)
void add_directive(csubstr directive)
void set_key_scalar_folded(csubstr scalar)
void set_key_anchor(csubstr anchor)
EventHandlerInts(c4::yml::Callbacks const &cb)
void set_key_scalar_plain_empty()
void set_val_ref(csubstr ref)
void begin_map_val_flow()
void set_key_scalar_plain(csubstr scalar)
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_literal(csubstr scalar)
void _clear_tag_directives_()
int required_size_events() const
get the size needed for the event buffer from the previous parse
void set_val_scalar_folded(csubstr scalar)
void set_val_scalar_dquoted(csubstr scalar)
void _pop()
end the current scope
void _send_str_(csubstr scalar, ievt::DataType flags)
void set_key_scalar_dquoted(csubstr scalar)
void begin_seq_key_flow()
csubstr _get_latest_empty_scalar() const
EventHandlerIntsState state
void _send_val_scalar_(csubstr scalar, ievt::DataType flags)
size_t required_size_arena() const
get the size needed for the arena from the previous parse
csubstr _transform_directive(csubstr tag)
void mark_key_scalar_unfiltered()
void set_key_ref(csubstr ref)
void start_parse(const char *filename, c4::yml::detail::pfn_relocate_arena relocate_arena, void *relocate_arena_data)
void begin_seq_key_block()
void set_val_scalar_plain(csubstr scalar)
void end_doc()
implicit doc end (without ...)
ievt::DataType value_type
void _push()
push a new parent, add a child to the new parent, and set the child as the current node
void begin_seq_val_flow()
void set_val_scalar_squoted(csubstr scalar)
void begin_map_key_flow()
void _mark_parent_with_children_()
void reset(csubstr str, substr arena, ievt::DataType *dst, int32_t dst_size)
void _disable__() noexcept
void set_val_tag(csubstr tag)