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>
137using DataType RYML_DEPRECATED(
"use evt_bits") =
evt_bits;
138using EventFlags RYML_DEPRECATED(
"use EventBits") =
EventBits;
184C4_SUPPRESS_WARNING_GCC_CLANG_PUSH
185C4_SUPPRESS_WARNING_GCC_CLANG(
"-Wold-style-cast")
186C4_SUPPRESS_WARNING_GCC(
"-Wuseless-cast")
197struct EventHandlerIntsState :
public c4::yml::ParserState
465 using state = EventHandlerIntsState;
484 #define ryml_enable_(bits) enable_<bits>()
485 #define ryml_disable_(bits) disable_<bits>()
486 #define ryml_has_any_(bits) has_any_<bits>()
514 m_evt_size = dst_size;
518 m_tag_directives.
clear();
542 return m_evt_pos <= m_evt_size && m_arena_pos <= m_arena.
len;
605 _c4dbgpf(
"{}/{}: begin_doc", m_evt_pos, m_evt_size);
617 _c4dbgpf(
"{}/{}: end_doc", m_evt_pos, m_evt_size);
630 _c4dbgpf(
"{}/{}: begin_doc_expl", m_evt_pos, m_evt_size);
639 _c4dbgpf(
"{}/{}: end_doc_expl", m_evt_pos, m_evt_size);
658 _c4dbgpf(
"{}/{}: bmap key flow", m_evt_pos, m_evt_size);
666 _c4dbgpf(
"{}/{}: bmap key block", m_evt_pos, m_evt_size);
675 _c4dbgpf(
"{}/{}: bmap flow", m_evt_pos, m_evt_size);
683 _c4dbgpf(
"{}/{}: bmap block", m_evt_pos, m_evt_size);
711 _c4dbgpf(
"{}/{}: bseq key flow", m_evt_pos, m_evt_size);
719 _c4dbgpf(
"{}/{}: bseq key block", m_evt_pos, m_evt_size);
728 _c4dbgpf(
"{}/{}: bseq flow", m_evt_pos, m_evt_size);
736 _c4dbgpf(
"{}/{}: bseq block", m_evt_pos, m_evt_size);
778 _c4dbgpf(
"{}/{}: set_key_scalar_plain_empty", m_evt_pos, m_evt_size);
784 _c4dbgpf(
"{}/{}: set_val_scalar_plain_empty", m_evt_pos, m_evt_size);
792 _c4dbgpf(
"{}/{}: set_key_scalar_plain: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.
str-
m_src.str, scalar.
len, scalar);
798 _c4dbgpf(
"{}/{}: set_val_scalar_plain: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.
str-
m_src.str, scalar.
len, scalar);
806 _c4dbgpf(
"{}/{}: set_key_scalar_dquo: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.
str?
size_t(scalar.
str-
m_src.str):
m_src.len, scalar.
len, scalar.
str?scalar:
csubstr{});
812 _c4dbgpf(
"{}/{}: set_val_scalar_dquo: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.
str?
size_t(scalar.
str-
m_src.str):
m_src.len, scalar.
len, scalar.
str?scalar:
csubstr{});
820 _c4dbgpf(
"{}/{}: set_key_scalar_squo: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.
str-
m_src.str, scalar.
len, scalar);
826 _c4dbgpf(
"{}/{}: set_val_scalar_squo: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.
str-
m_src.str, scalar.
len, scalar);
834 _c4dbgpf(
"{}/{}: set_key_scalar_literal: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.
str?
size_t(scalar.
str-
m_src.str):
m_src.len, scalar.
len, scalar.
str?scalar:
csubstr{});
840 _c4dbgpf(
"{}/{}: set_val_scalar_literal: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.
str?
size_t(scalar.
str-
m_src.str):
m_src.len, scalar.
len, scalar.
str?scalar:
csubstr{});
848 _c4dbgpf(
"{}/{}: set_key_scalar_folded: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.
str?
size_t(scalar.
str-
m_src.str):
m_src.len, scalar.
len, scalar.
str?scalar:
csubstr{});
854 _c4dbgpf(
"{}/{}: set_val_scalar_folded: @{} [{}]~~~{}~~~", m_evt_pos, m_evt_size, scalar.
str?
size_t(scalar.
str-
m_src.str):
m_src.len, scalar.
len, scalar.
str?scalar:
csubstr{});
862 _c4dbgpf(
"{}/{}: mark_key_scalar_unfiltered", m_evt_pos, m_evt_size);
863 if(m_evt_pos < m_evt_size)
868 _c4dbgpf(
"{}/{}: mark_val_scalar_unfiltered", m_evt_pos, m_evt_size);
869 if(m_evt_pos < m_evt_size)
878 #define _add_scalar_(i, scalar) \
879 _c4dbgpf("{}/{}: scalar!", i, m_evt_size); \
880 RYML_ASSERT_BASIC_CB_(m_stack.m_callbacks, _is_sub_(scalar)); \
881 RYML_ASSERT_BASIC_CB_(m_stack.m_callbacks, m_evt[i] & ievt::WSTR); \
882 RYML_ASSERT_BASIC_CB_(m_stack.m_callbacks, ((i) + 3) < m_evt_size); \
883 if C4_LIKELY((scalar).is_sub(m_src)) \
885 m_evt[(i) + 1] = (ievt::evt_bits)((scalar).str - m_src.str); \
889 m_evt[i] |= ievt::AREN; \
890 m_evt[(i) + 1] = (ievt::evt_bits)((scalar).str - m_arena.str); \
891 _c4dbgpf("{}/{}: arena! ->{}", i, m_evt_size, m_evt[(i)+1]); \
893 m_evt[(i) + 2] = (ievt::evt_bits)(scalar).len; \
894 m_evt[(i) + 3] = ievt::PSTR
904 _c4dbgpf(
"{}/{}: set_key_anchor: {}", m_evt_pos, m_evt_size, anchor);
907 if(m_evt_pos + 3 < m_evt_size)
910 _add_scalar_(m_evt_pos, anchor);
912 m_evt_prev = m_evt_pos;
917 _c4dbgpf(
"{}/{}: set_val_anchor: {}", m_evt_pos, m_evt_size, anchor);
920 if(m_evt_pos + 3 < m_evt_size)
923 _add_scalar_(m_evt_pos, anchor);
925 m_evt_prev = m_evt_pos;
931 _c4dbgpf(
"{}/{}: set_key_ref: {}", m_evt_pos, m_evt_size, ref);
939 _c4dbgpf(
"{}/{}: set_val_ref: {}", m_evt_pos, m_evt_size, ref);
955 _c4dbgpf(
"{}/{}: set key tag [{}]~~~{}~~~", m_evt_pos, m_evt_size, tag.
len, tag.
str ? tag :
csubstr(
"(arena full)"));
962 _c4dbgpf(
"{}/{}: set val tag [{}]~~~{}~~~", m_evt_pos, m_evt_size, tag.
len, tag.
str ? tag :
csubstr(
"(arena full)"));
977 _c4dbgpf(
"{}/{}: %YAML directive! version={}", m_evt_pos, m_evt_size, yaml_version);
983 _c4dbgpf(
"{}/{}: %TAG directive! handle={} prefix={} doc_id={}", m_evt_pos, m_evt_size, handle, prefix, m_curr_doc);
984 if C4_UNLIKELY(!m_tag_directives.
add(handle, prefix, m_curr_doc))
985 RYML_ERR_PARSE_CB_(
m_stack.m_callbacks,
m_curr->pos,
"too many %TAG directives");
1004 _c4dbgpf(
"{}/{}: prev={} actually_val_is_first_key_of_new_map_flow", m_evt_pos, m_evt_size, m_evt_prev);
1005 RYML_ASSERT_BASIC_CB_(
m_stack.m_callbacks, m_evt_pos > 2);
1006 RYML_ASSERT_BASIC_CB_(
m_stack.m_callbacks, m_evt_prev > 0);
1015 if(m_evt_pos < m_evt_size)
1019 _c4dbgpf(
"{}/{}: WSTR", m_evt_pos, m_evt_size);
1020 RYML_ASSERT_BASIC_CB_(
m_stack.m_callbacks, m_evt_prev > 0);
1022 if(m_evt_pos + 1 < m_evt_size)
1027 m_evt[i] &=
~ievt::VAL_;
1029 evt_size num_move = m_evt_pos + 1 - pos;
1030 RYML_ASSERT_BASIC_CB_(
m_stack.m_callbacks, num_move > 0);
1031 memmove(m_evt + pos + 1, m_evt + pos, (
size_t)num_move *
sizeof(
ievt::evt_bits));
1038 m_evt[pos + 1] &=
~ievt::PSTR;
1043 _c4dbgpf(
"{}/{}: container key", m_evt_pos, m_evt_size);
1046 _c4dbgpf(
"{}/{}: find matching open for {}", m_evt_pos, m_evt_size, m_evt_prev);
1056 _c4dbgpf(
"{}/{}: matching open for {}={}", m_evt_pos, m_evt_size, m_evt_prev, pos);
1057 RYML_CHECK_BASIC_CB_(
m_stack.m_callbacks, pos >= 0);
1058 RYML_CHECK_BASIC_CB_(
m_stack.m_callbacks, pos < m_evt_prev);
1063 if(m_evt_pos + 1 < m_evt_size)
1065 evt_size num_move = m_evt_pos + 1 - pos;
1066 RYML_ASSERT_BASIC_CB_(
m_stack.m_callbacks, num_move > 0);
1067 memmove(m_evt + posp1, m_evt + pos, (
size_t)num_move *
sizeof(
ievt::evt_bits));
1069 RYML_ASSERT_BASIC_CB_(
m_stack.m_callbacks, posp1 < m_evt_pos);
1074 m_evt[posp1] &=
~ievt::VAL_;
1079 m_evt[posp1] &=
~ievt::PSTR;
1083 m_curr->evt_id = m_evt_pos - 2;
1098 _c4dbgpf(
"{}/{}: prev={} actually_val_is_first_key_of_new_map_block", m_evt_pos, m_evt_size, m_evt_prev);
1099 if(m_evt_pos < m_evt_size)
1105 RYML_ASSERT_BASIC_CB_(
m_stack.m_callbacks, pos < m_evt_size);
1106 RYML_ASSERT_BASIC_CB_(
m_stack.m_callbacks, pos < m_evt_pos);
1108 if(m_evt_pos < m_evt_size)
1111 evt_size num_move = m_evt_pos - pos;
1114 RYML_ASSERT_BASIC_CB_(
m_stack.m_callbacks, num_move > 0);
1116 memmove(m_evt + posp1, m_evt + pos, (
size_t)num_move *
sizeof(
ievt::evt_bits));
1118 m_evt[posp1] &=
~ievt::VAL_;
1138 return m_arena.
first(m_arena_pos < m_arena.
len ? m_arena_pos : m_arena.
len);
1142 return C4_EXPECT(m_arena_pos <= m_arena.
len, 1) ? m_arena.
sub(m_arena_pos) : m_arena.
last(0);
1148 if C4_LIKELY(len <= s.
len)
1177 template<c4::yml::type_bits bits> C4_ALWAYS_INLINE
void enable_() noexcept
1179 m_curr->evt_type |= bits;
1181 template<c4::yml::type_bits bits> C4_ALWAYS_INLINE
void disable_() noexcept
1183 m_curr->evt_type &= ~bits;
1185 template<c4::yml::type_bits bits> C4_ALWAYS_INLINE
bool has_any_() const noexcept
1192 RYML_ASSERT_BASIC_CB_(
m_stack.m_callbacks, pos < m_evt_size);
1193 return pos + ((m_evt[pos] &
ievt::WSTR) ? 3 : 1);
1198 RYML_ASSERT_BASIC_CB_(
m_stack.m_callbacks, pos < m_evt_size);
1199 return pos - ((m_evt[pos] &
ievt::PSTR) ? 3 : 1);
1204 return (!str.str || str.is_sub(
m_src) || str.is_sub(m_arena));
1209 _c4dbgpf(
"{}/{}: flag only", m_evt_pos, m_evt_size);
1210 if(m_evt_pos < m_evt_size)
1211 m_evt[m_evt_pos] |= flags;
1212 m_curr->evt_id = m_evt_pos;
1213 m_evt_prev = m_evt_pos;
1215 if(m_evt_pos < m_evt_size)
1216 m_evt[m_evt_pos] = {};
1221 _c4dbgpf(
"{}/{}: send str", m_evt_pos, m_evt_size);
1222 if(m_evt_pos + 3 < m_evt_size)
1224 m_evt[m_evt_pos] |= flags;
1225 _add_scalar_(m_evt_pos, scalar);
1227 m_curr->evt_id = m_evt_pos;
1228 m_evt_prev = m_evt_pos;
1243 return m_src.first(0);
1248 RYML_ASSERT_BASIC_CB_(
m_stack.m_callbacks, pos < m_evt_size);
1261 _c4dbgpf(
"find_matching: start at {}", pos);
1262 RYML_ASSERT_BASIC_CB_(
m_stack.m_callbacks, pos < m_evt_size);
1263 RYML_ASSERT_BASIC_CB_(
m_stack.m_callbacks, (m_evt[pos] & close) == close);
1264 RYML_ASSERT_BASIC_CB_(
m_stack.m_callbacks, (m_evt[pos] & open) == (close & ~
ievt::END_));
1270 _c4dbgpf(
"find_matching: pos={} count={} e={}", pos, count, m_evt[pos]);
1271 if((e & close) == close)
1273 _c4dbgpf(
".............: pos={} close! count={} e={}", pos, count, m_evt[pos]);
1276 else if((e & open) == open)
1278 _c4dbgpf(
".............: pos={} open! count={} e={}", pos, count, m_evt[pos]);
1286 _c4dbgpf(
"find_matching: not found!", 0);
1292 RYML_ASSERT_BASIC_CB_(
m_stack.m_callbacks, pos < m_evt_size);
1296 _c4dbgpf(
"{}/{}: {} is anchor/tag. extend to {}", m_evt_pos, m_evt_size, prev, prev);
1320C4_SUPPRESS_WARNING_GCC_CLANG_POP
#define ryml_has_any_(bits)
Callbacks const & get_callbacks()
get the global callbacks
int32_t evt_size
data type for integer events size.
evt_size 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
@ KEY
the scalar to the left of : in a map's member
@ FLOW_ML1
mark container with multi-line flow style, 1 element per line
@ VAL_FOLDED
mark val scalar as multiline, block folded >
@ KEYTAG
the key has a tag
@ FLOW_SL
mark container with single-line flow style
@ 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
@ 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)
basic_substring< char > substr
a mutable string view
basic_substring< const char > csubstr
an immutable string view
@ RTOP
reading at top level
@ RUNK
reading unknown state (when starting): must determine whether scalar, map or seq
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...
bool begins_with(const C c) const noexcept
true if the first character of the string is c
size_t len
the length of the substring
basic_substring last(size_t num) const noexcept
return the last num elements: [len-num,len[
basic_substring first(size_t num) const noexcept
return the first num elements: [0,num[
basic_substring sub(size_t first) const noexcept
return [first,len[
C * str
a restricted pointer to the first character of the substring
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()
bool _stack_should_push_on_begin_doc() const
void _stack_start_parse(const char *filename, substr ymlsrc)
detail::stack< state > m_stack
bool _stack_should_pop_on_end_doc() const
Accelerator structure to reduce memory requirements by enabling reuse of resolved tags.
TagDirective const * add(csubstr handle, csubstr prefix, id_type doc_id) noexcept
evt_size _extend_left_to_include_tag_and_or_anchor(evt_size pos) const
void begin_doc_expl()
explicit doc start, with —
void end_doc_expl()
explicit doc end, with ...
evt_size required_size_events() const
get the size needed for the event buffer from the previous parse
void set_val_anchor(csubstr anchor)
void add_directive_tag(csubstr handle, csubstr prefix)
void set_key_scalar_literal(csubstr scalar)
void set_val_scalar_plain_empty()
void begin_map_val_block()
void start_parse(const char *filename, substr src)
evt_size _find_matching_open(ievt::evt_bits open, ievt::evt_bits close, evt_size pos) const
bool fits_buffers() const
Predicate to test if the event and arena buffers successfully accomodated all the parse events.
void begin_doc()
implicit doc start (without —)
void begin_map_key_block()
void set_key_tag(csubstr tag)
substr alloc_arena(size_t len)
this may fail, in which case an empty string is returned
void mark_val_scalar_unfiltered()
void reset(substr str, substr arena, ievt::evt_bits *dst, evt_size dst_size)
EventHandlerInts(c4::yml::Callbacks const &cb) noexcept
evt_size _prev(evt_size pos) const 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.
void begin_seq_val_block()
void set_key_scalar_squoted(csubstr scalar)
evt_size _find_last_bdoc(evt_size pos) const
void set_key_scalar_folded(csubstr scalar)
void set_key_anchor(csubstr anchor)
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 _send_str_(csubstr scalar, ievt::evt_bits flags)
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 set_val_scalar_folded(csubstr scalar)
void set_val_scalar_dquoted(csubstr scalar)
void _pop()
end the current scope
void set_key_scalar_dquoted(csubstr scalar)
void begin_seq_key_flow()
void _send_flag_only_(ievt::evt_bits flags)
csubstr _get_latest_empty_scalar() const
ievt::evt_bits value_type
EventHandlerIntsState state
EventHandlerInts() noexcept
size_t required_size_arena() const
get the size needed for the arena from the previous parse
bool has_any_() const noexcept
bool _is_sub_(csubstr str) const noexcept
void mark_key_scalar_unfiltered()
void set_key_ref(csubstr ref)
TagDirectives & tag_directives()
void begin_seq_key_block()
void set_val_scalar_plain(csubstr scalar)
void end_doc()
implicit doc end (without ...)
void add_directive_yaml(csubstr yaml_version)
void _push()
push a new parent, add a child to the new parent, and set the child as the current node
void end_seq_flow(bool, type_bits=FLOW_ML1)
void begin_seq_val_flow()
void set_val_scalar_squoted(csubstr scalar)
evt_size _next(evt_size pos) const noexcept
@ requires_strings_on_buffers
void end_map_flow(bool, type_bits=FLOW_ML1)
void begin_map_key_flow()
void _mark_parent_with_children_()
void set_val_tag(csubstr tag)