|
rapidyaml
0.12.1
parse and emit YAML, and do it fast
|
A parser event handler that creates a compact representation of the YAML tree in a contiguous buffer of integers. More...
#include <event_handler_ints.hpp>
Public Types | |
types | |
| enum | { requires_strings_on_buffers = true } |
| using | value_type = ievt::DataType |
| using | state = EventHandlerIntsState |
Public Member Functions | |
construction and resetting | |
| EventHandlerInts (c4::yml::Callbacks const &cb) | |
| EventHandlerInts () | |
| void | reset (substr str, substr arena, ievt::DataType *dst, int32_t dst_size) |
| int | required_size_events () const |
| get the size needed for the event buffer from the previous parse More... | |
| size_t | required_size_arena () const |
| get the size needed for the arena from the previous parse More... | |
| bool | fits_buffers () const |
| Predicate to test if the event and arena buffers successfully accomodated all the parse events. More... | |
| void | reserve_arena (int) |
| TagDirectives & | tag_directives () |
| TagCache & | tag_cache () |
parse events | |
| void | start_parse (const char *filename, substr src) |
| void | finish_parse () |
| void | cancel_parse () |
YAML stream events | |
| void | begin_stream () |
| void | end_stream () |
YAML document events | |
| void | begin_doc () |
| implicit doc start (without —) More... | |
| void | end_doc () |
| implicit doc end (without ...) More... | |
| void | begin_doc_expl () |
| explicit doc start, with — More... | |
| void | end_doc_expl () |
| explicit doc end, with ... More... | |
YAML map functions | |
| void | begin_map_key_flow () |
| void | begin_map_key_block () |
| void | begin_map_val_flow () |
| void | begin_map_val_block () |
| void | end_map_block () |
| void | end_map_flow (bool) |
YAML seq events | |
| void | begin_seq_key_flow () |
| void | begin_seq_key_block () |
| void | begin_seq_val_flow () |
| void | begin_seq_val_block () |
| void | end_seq_block () |
| void | end_seq_flow (bool) |
YAML structure events | |
| void | add_sibling () |
| 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. More... | |
| 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 or doc level. More... | |
YAML scalar events | |
| void | set_key_scalar_plain_empty () |
| void | set_val_scalar_plain_empty () |
| void | set_key_scalar_plain (csubstr scalar) |
| void | set_val_scalar_plain (csubstr scalar) |
| void | set_key_scalar_dquoted (csubstr scalar) |
| void | set_val_scalar_dquoted (csubstr scalar) |
| void | set_key_scalar_squoted (csubstr scalar) |
| void | set_val_scalar_squoted (csubstr scalar) |
| void | set_key_scalar_literal (csubstr scalar) |
| void | set_val_scalar_literal (csubstr scalar) |
| void | set_key_scalar_folded (csubstr scalar) |
| void | set_val_scalar_folded (csubstr scalar) |
| void | mark_key_scalar_unfiltered () |
| void | mark_val_scalar_unfiltered () |
YAML anchor/reference events | |
| void | set_key_anchor (csubstr anchor) |
| void | set_val_anchor (csubstr anchor) |
| void | set_key_ref (csubstr ref) |
| void | set_val_ref (csubstr ref) |
YAML tag events | |
| void | set_key_tag (csubstr tag) |
| void | set_val_tag (csubstr tag) |
YAML directive events | |
| void | add_directive_yaml (csubstr yaml_version) |
| void | add_directive_tag (csubstr handle, csubstr prefix) |
arena events | |
| substr | arena () |
| substr | arena_rem () |
| substr | alloc_arena (size_t len) |
| this may fail, in which case an empty string is returned More... | |
implementation helpers | |
| void | _push () |
| push a new parent, add a child to the new parent, and set the child as the current node More... | |
| void | _pop () |
| end the current scope More... | |
| template<c4::yml::type_bits bits> | |
| void | _enable__ () noexcept |
| template<c4::yml::type_bits bits> | |
| void | _disable__ () noexcept |
| template<c4::yml::type_bits bits> | |
| bool | _has_any__ () const noexcept |
| int32_t | _next (int32_t pos) const noexcept |
| int32_t | _prev (int32_t pos) const noexcept |
| bool | _is_sub_ (csubstr str) const noexcept |
| void | _send_flag_only_ (ievt::DataType flags) |
| void | _send_str_ (csubstr scalar, ievt::DataType flags) |
| void | _mark_parent_with_children_ () |
| csubstr | _get_latest_empty_scalar () const |
| int32_t | _find_last_bdoc (int32_t pos) const |
| int32_t | _find_matching_open (ievt::DataType open, ievt::DataType close, int32_t pos) const |
| int32_t | _extend_left_to_include_tag_and_or_anchor (int32_t pos) const |
Public Attributes | |
| detail::stack< state > | m_stack |
| state * | m_curr |
| current stack level: top of the stack. cached here for easier access. More... | |
| state * | m_parent |
| parent of the current stack level. More... | |
| substr | m_src |
Protected Member Functions | |
| void | _stack_start_parse (const char *filename, substr ymlsrc) |
| void | _stack_finish_parse () |
| void | _stack_reset_root () |
| void | _stack_reset_non_root () |
| void | _stack_push () |
| void | _stack_pop () |
| bool | _stack_should_push_on_begin_doc () const |
| bool | _stack_should_pop_on_end_doc () const |
A parser event handler that creates a compact representation of the YAML tree in a contiguous buffer of integers.
The integers are ievt::EventFlags containing masks (to represent events), interleaved with offset+length (to represent strings in the source buffer).
This is meant for use by other programming languages, and supports container keys (unlike the ryml tree). It parses faster than the ryml tree parser, because the resulting data structure is much simpler.
The resulting integer buffer is a linear array of integers containing events (as a mask of ievt::EventFlags), which in some cases (see ievt::WSTR) are followed by an encoded string (encoded as an offset and length to the parsed source buffer).
For example, parsing [a, bb, ccc] results in the following event buffer (grouped to highlight the event sequence structure):
Here is a sketch clarifying the meaning of this event sequence:
Note that the buffer contains both events and strings encoded as integer pairs. That is, events that have an associated string are immediately followed by two integers providing the offset and length of that string in the source buffer. (In the example above, this happens in the events for the strings a, bb, and ccc at positions 3, 6 and 9, respectively).
The flag ievt::PSTR and the mask ievt::WSTR are provided to enable easier iteration over the array: you can use them to test for presence of a string when iterating over the array.
The flag ievt::PSTR announces that an event is preceded by a string. That is, the previous event has a string, so that when this flag is found while iterating right-to-left, a jump of -3 should be used to get at the bitmask of the previous event. (In the example above, this flag is present for the events for bb and ccc, but not a because it is not preceded by a string).
Likewise, to signify that the current event is followed by a string, there is the mask ievt::WSTR, which is a mask of all the flags of events that have a string: ievt::SCLR, ievt::ALIA, ievt::ANCH and ievt::TAG_. While iterating left-to-right in the array, presence of any of the bits in the mask ievt::WSTR means that a jump of +3 should be employed to get at the bitmask of the next event.
Here's another example with the result of parsing a: bb
Typical code to iterate left-to-right over the array will look like this:
This handler must be initialized with the input source buffer, the output arena, and the output event buffer. This handler will not take ownership nor attempt to resize the output buffer. If the size required for the output buffer or arena are larger than their actual size, parsing goes all way to the end, determining the required buffer sizes without writing anything past the end of the respective buffer. After parsing is finished, the user must ensure that the buffer size was enough to accomodate all the data that needs to be written into it, or react accordingly (eg, throw an error, or resize the buffer then retry the parse).
A couple of functions will be helpful to do this. After parsing, EventHandlerInts::fits_buffers() must be used to verify that the output buffers were enough to accomodate the results. Then, EventHandlerInts::required_size_events() and EventHandlerInts::required_size_arena() can be used to retrieve to necessary information. To get an estimation of the number of events before parsing, see estimate_events_ints_size().
Typical code to parse YAML with this handler will look like this:
The result of estimate_events_ints_size() (click to see more info) must be an overprediction: it overpredicts for every single case among the many hundreds covered in the unit tests. This is deliberate, and aims at ensuring that a retry parse is not needed. But conceivably, it may underpredict in some instances not found in the out tests. What to do then?
First, open an issue to allow the estimation to be improved! Second, there are two ways to handle this situation in code:
1) throw an error (as sketched above)
2) grow the buffer to the required size (see EventHandlerInts::required_size_events()), and then parse again
If your code must be able to handle any case including where the prediction undershoots before the estimate function is fixed (after you open the issue), that is, if you are considering a parse retry, there is something important that needs attention. The YAML source buffer is mutated in-place during the parse, and cannot be used to parse again. So if you want to retry, you need to keep a pristine copy of the source, and use it for the retry:
When bringing this to other programming languages, the semantics will be very similar to this.
Definition at line 436 of file event_handler_ints.hpp.
Definition at line 442 of file event_handler_ints.hpp.
| using c4::yml::extra::EventHandlerInts::state = EventHandlerIntsState |
Definition at line 443 of file event_handler_ints.hpp.
| anonymous enum |
| Enumerator | |
|---|---|
| requires_strings_on_buffers | |
Definition at line 444 of file event_handler_ints.hpp.
|
inline |
Definition at line 472 of file event_handler_ints.hpp.
|
inline |
Definition at line 477 of file event_handler_ints.hpp.
|
inline |
Definition at line 482 of file event_handler_ints.hpp.
References c4::yml::TagCache::clear(), c4::yml::TagDirectives::clear(), c4::yml::RTOP, and c4::yml::RUNK.
|
inline |
get the size needed for the event buffer from the previous parse
Definition at line 502 of file event_handler_ints.hpp.
|
inline |
get the size needed for the arena from the previous parse
Definition at line 509 of file event_handler_ints.hpp.
|
inline |
Predicate to test if the event and arena buffers successfully accomodated all the parse events.
Definition at line 518 of file event_handler_ints.hpp.
|
inline |
Definition at line 523 of file event_handler_ints.hpp.
|
inline |
Definition at line 528 of file event_handler_ints.hpp.
|
inline |
Definition at line 529 of file event_handler_ints.hpp.
|
inline |
Definition at line 538 of file event_handler_ints.hpp.
|
inline |
Definition at line 545 of file event_handler_ints.hpp.
|
inline |
Definition at line 550 of file event_handler_ints.hpp.
|
inline |
Definition at line 563 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::BSTR.
|
inline |
Definition at line 568 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::ESTR.
|
inline |
implicit doc start (without —)
Definition at line 581 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::BDOC, and c4::yml::DOC.
|
inline |
implicit doc end (without ...)
Definition at line 593 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::EDOC.
|
inline |
explicit doc start, with —
Definition at line 606 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::BDOC, c4::yml::DOC, and c4::yml::extra::ievt::EXPL.
|
inline |
explicit doc end, with ...
Definition at line 615 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::EDOC, and c4::yml::extra::ievt::EXPL.
|
inline |
Definition at line 634 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::BMAP, c4::yml::extra::ievt::FLOW, c4::yml::FLOW_SL, c4::yml::KEY, c4::yml::extra::ievt::KEY_, and c4::yml::MAP.
|
inline |
Definition at line 642 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::BLCK, c4::yml::BLOCK, c4::yml::extra::ievt::BMAP, c4::yml::KEY, c4::yml::extra::ievt::KEY_, and c4::yml::MAP.
|
inline |
Definition at line 651 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::BMAP, c4::yml::extra::ievt::FLOW, c4::yml::FLOW_SL, c4::yml::MAP, and c4::yml::extra::ievt::VAL_.
|
inline |
Definition at line 659 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::BLCK, c4::yml::BLOCK, c4::yml::extra::ievt::BMAP, c4::yml::MAP, and c4::yml::extra::ievt::VAL_.
|
inline |
Definition at line 668 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::EMAP.
|
inline |
Definition at line 674 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::EMAP.
|
inline |
Definition at line 687 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::BSEQ, c4::yml::extra::ievt::FLOW, c4::yml::FLOW_SL, c4::yml::KEY, c4::yml::extra::ievt::KEY_, and c4::yml::SEQ.
|
inline |
Definition at line 695 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::BLCK, c4::yml::BLOCK, c4::yml::extra::ievt::BSEQ, c4::yml::KEY, c4::yml::extra::ievt::KEY_, and c4::yml::SEQ.
|
inline |
Definition at line 704 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::BSEQ, c4::yml::extra::ievt::FLOW, c4::yml::FLOW_SL, c4::yml::SEQ, and c4::yml::extra::ievt::VAL_.
|
inline |
Definition at line 712 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::BLCK, c4::yml::BLOCK, c4::yml::extra::ievt::BSEQ, c4::yml::SEQ, and c4::yml::extra::ievt::VAL_.
|
inline |
Definition at line 721 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::ESEQ.
|
inline |
Definition at line 727 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::ESEQ.
|
inline |
Definition at line 740 of file event_handler_ints.hpp.
|
inline |
Definition at line 754 of file event_handler_ints.hpp.
References c4::yml::KEY, c4::yml::extra::ievt::KEY_, c4::yml::KEY_PLAIN, c4::yml::KEYNIL, c4::yml::extra::ievt::PLAI, and c4::yml::extra::ievt::SCLR.
|
inline |
Definition at line 760 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::PLAI, c4::yml::extra::ievt::SCLR, c4::yml::VAL, c4::yml::extra::ievt::VAL_, c4::yml::VAL_PLAIN, and c4::yml::VALNIL.
|
inline |
Definition at line 768 of file event_handler_ints.hpp.
References c4::yml::KEY, c4::yml::extra::ievt::KEY_, c4::yml::KEY_PLAIN, c4::yml::extra::ievt::PLAI, and c4::yml::extra::ievt::SCLR.
|
inline |
Definition at line 774 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::PLAI, c4::yml::extra::ievt::SCLR, c4::yml::VAL, c4::yml::extra::ievt::VAL_, and c4::yml::VAL_PLAIN.
|
inline |
Definition at line 782 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::DQUO, c4::yml::KEY, c4::yml::extra::ievt::KEY_, c4::yml::KEY_DQUO, and c4::yml::extra::ievt::SCLR.
|
inline |
Definition at line 788 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::DQUO, c4::yml::extra::ievt::SCLR, c4::yml::VAL, c4::yml::extra::ievt::VAL_, and c4::yml::VAL_DQUO.
|
inline |
Definition at line 796 of file event_handler_ints.hpp.
References c4::yml::KEY, c4::yml::extra::ievt::KEY_, c4::yml::KEY_SQUO, c4::yml::extra::ievt::SCLR, and c4::yml::extra::ievt::SQUO.
|
inline |
Definition at line 802 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::SCLR, c4::yml::extra::ievt::SQUO, c4::yml::VAL, c4::yml::extra::ievt::VAL_, and c4::yml::VAL_SQUO.
|
inline |
Definition at line 810 of file event_handler_ints.hpp.
References c4::yml::KEY, c4::yml::extra::ievt::KEY_, c4::yml::KEY_LITERAL, c4::yml::extra::ievt::LITL, and c4::yml::extra::ievt::SCLR.
|
inline |
Definition at line 816 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::LITL, c4::yml::extra::ievt::SCLR, c4::yml::VAL, c4::yml::extra::ievt::VAL_, and c4::yml::VAL_LITERAL.
|
inline |
Definition at line 824 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::FOLD, c4::yml::KEY, c4::yml::extra::ievt::KEY_, c4::yml::KEY_FOLDED, and c4::yml::extra::ievt::SCLR.
|
inline |
Definition at line 830 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::FOLD, c4::yml::extra::ievt::SCLR, c4::yml::VAL, c4::yml::extra::ievt::VAL_, and c4::yml::VAL_FOLDED.
|
inline |
Definition at line 838 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::UNFILT.
|
inline |
|
inline |
Definition at line 880 of file event_handler_ints.hpp.
References _has_any_, c4::yml::extra::ievt::ANCH, c4::yml::extra::ievt::KEY_, c4::yml::KEYANCH, and c4::yml::KEYREF.
|
inline |
Definition at line 893 of file event_handler_ints.hpp.
References _has_any_, c4::yml::extra::ievt::ANCH, c4::yml::extra::ievt::VAL_, c4::yml::VALANCH, and c4::yml::VALREF.
|
inline |
Definition at line 907 of file event_handler_ints.hpp.
References _has_any_, c4::yml::extra::ievt::ALIA, c4::yml::KEY, c4::yml::extra::ievt::KEY_, c4::yml::KEYANCH, and c4::yml::KEYREF.
|
inline |
Definition at line 916 of file event_handler_ints.hpp.
References _has_any_, c4::yml::extra::ievt::ALIA, c4::yml::VAL, c4::yml::extra::ievt::VAL_, c4::yml::VALANCH, and c4::yml::VALREF.
|
inline |
Definition at line 933 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::KEY_, c4::yml::KEYTAG, and c4::yml::extra::ievt::TAG_.
|
inline |
Definition at line 940 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::TAG_, c4::yml::extra::ievt::VAL_, and c4::yml::VALTAG.
|
inline |
Definition at line 955 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::YAML.
|
inline |
Definition at line 961 of file event_handler_ints.hpp.
References c4::yml::TagDirectives::add(), c4::yml::extra::ievt::TAGH, and c4::yml::extra::ievt::TAGP.
|
inline |
set the previous val as the first key of a new map, with flow style.
See the documentation for Event Handlers, which has important notes about this event.
Definition at line 982 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::BMAP, c4::yml::extra::ievt::BSEQ, c4::yml::extra::ievt::EMAP, c4::yml::extra::ievt::ESEQ, c4::yml::extra::ievt::FLOW, c4::yml::FLOW_SL, c4::yml::extra::ievt::KEY_, c4::yml::MAP, c4::yml::extra::ievt::PSTR, c4::yml::extra::ievt::VAL_, and c4::yml::extra::ievt::WSTR.
|
inline |
like its flow counterpart, but this function can only be called after the end of a flow-val at root or doc level.
See the documentation for Event Handlers, which has important notes about this event.
Definition at line 1076 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::BDOC, c4::yml::extra::ievt::BLCK, c4::yml::extra::ievt::BMAP, c4::yml::extra::ievt::BSEQ, c4::yml::extra::ievt::KEY_, c4::yml::extra::ievt::PSTR, and c4::yml::extra::ievt::VAL_.
|
inline |
Definition at line 1116 of file event_handler_ints.hpp.
|
inline |
Definition at line 1120 of file event_handler_ints.hpp.
|
inline |
this may fail, in which case an empty string is returned
Definition at line 1125 of file event_handler_ints.hpp.
|
inline |
push a new parent, add a child to the new parent, and set the child as the current node
Definition at line 1145 of file event_handler_ints.hpp.
|
inline |
end the current scope
Definition at line 1152 of file event_handler_ints.hpp.
|
inlinenoexcept |
Definition at line 1157 of file event_handler_ints.hpp.
|
inlinenoexcept |
Definition at line 1161 of file event_handler_ints.hpp.
|
inlinenoexcept |
Definition at line 1165 of file event_handler_ints.hpp.
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
Definition at line 1182 of file event_handler_ints.hpp.
|
inline |
Definition at line 1187 of file event_handler_ints.hpp.
|
inline |
Definition at line 1199 of file event_handler_ints.hpp.
|
inline |
Definition at line 1212 of file event_handler_ints.hpp.
|
inline |
Definition at line 1218 of file event_handler_ints.hpp.
|
inline |
Definition at line 1226 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::BDOC, and c4::yml::extra::ievt::PSTR.
|
inline |
Definition at line 1239 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::END_.
|
inline |
Definition at line 1270 of file event_handler_ints.hpp.
References c4::yml::extra::ievt::ANCH, and c4::yml::extra::ievt::TAG_.
|
inlineprotectedinherited |
|
inlineprotectedinherited |
Definition at line 65 of file event_handler_stack.hpp.
|
inlineprotectedinherited |
|
inlineprotectedinherited |
|
inlineprotectedinherited |
|
inlineprotectedinherited |
Definition at line 96 of file event_handler_stack.hpp.
|
inlineprotectedinherited |
|
inlineprotectedinherited |
Definition at line 127 of file event_handler_stack.hpp.
|
inherited |
Definition at line 46 of file event_handler_stack.hpp.
|
inherited |
current stack level: top of the stack. cached here for easier access.
Definition at line 47 of file event_handler_stack.hpp.
|
inherited |
parent of the current stack level.
Definition at line 48 of file event_handler_stack.hpp.
|
inherited |
Definition at line 49 of file event_handler_stack.hpp.