Skip to content

Commit

Permalink
event-based parser policy pt7: clean event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed May 5, 2024
1 parent b955880 commit 7ee863c
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 179 deletions.
8 changes: 4 additions & 4 deletions changelog/current.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The parser was completely refactored ([#PR414](https://github.com/biojppm/rapidy
- Filtering can now be disabled while parsing, to ensure a fully-readonly parse (but this feature is still experimental and somewhat untested, given the scope of the rewrite work).
- The parser now offers methods to filter scalars in place or out of place.
- Style flags were added to `NodeType_e`:
```
```cpp
FLOW_SL ///< mark container with single-line flow style (seqs as '[val1,val2], maps as '{key: val,key2: val2}')
FLOW_ML ///< mark container with multi-line flow style (seqs as '[\n val1,\n val2\n], maps as '{\n key: val,\n key2: val2\n}')
BLOCK ///< mark container with block style (seqs as '- val\n', maps as 'key: val')
Expand All @@ -46,7 +46,7 @@ The parser was completely refactored ([#PR414](https://github.com/biojppm/rapidy
VAL_PLAIN ///< mark val scalar as plain scalar (unquoted, even when multiline)
```
- Style predicates were added to `NodeType`, `Tree`, `ConstNodeRef` and `NodeRef`:
```
```cpp
bool is_container_styled() const;
bool is_block() const
bool is_flow_sl() const;
Expand All @@ -67,13 +67,13 @@ The parser was completely refactored ([#PR414](https://github.com/biojppm/rapidy
bool is_val_plain() const;
```
- Style modifiers were also added:
```
```cpp
void set_container_style(NodeType_e style);
void set_key_style(NodeType_e style);
void set_val_style(NodeType_e style);
```
- Emit helper predicates were added, and are used when an emitted node was built programatically without style flags:
```
```cpp
/** choose a YAML emitting style based on the scalar's contents */
NodeType_e scalar_style_choose(csubstr scalar) noexcept;
/** query whether a scalar can be encoded using single quotes.
Expand Down
55 changes: 25 additions & 30 deletions src/c4/yml/event_handler_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ namespace yml {
* @{ */


/** The stack state needed specifically by @ref EventHandlerTree */
struct EventHandlerTreeState : public ParserState
{
NodeData *tr_data;
};


/** The event handler to create a ryml @ref Tree. See the
* documentation for @ref doc_event_handlers, which has important
* notes about the event model used by rapidyaml. */
Expand All @@ -32,17 +34,13 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
/** @name types
* @{ */

// our internal state must inherit from parser state
using state = EventHandlerTreeState;

/** @} */

public:

/** @cond dev */
static constexpr const bool is_events = false; // remove
static constexpr const bool is_wtree = true;

Tree *C4_RESTRICT m_tree;
id_type m_id;

Expand All @@ -54,7 +52,6 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
#define _disable_(bits) _disable__<bits>()
#endif
#define _has_any_(bits) _has_any__<bits>()

/** @endcond */

public:
Expand Down Expand Up @@ -151,7 +148,7 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
if(_stack_should_push_on_begin_doc())
{
_c4dbgp("push!");
_tr_set_root_as_stream();
_set_root_as_stream();
_push();
_enable_(DOC);
}
Expand All @@ -162,7 +159,7 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
_c4dbgp("end_doc");
if(_stack_should_pop_on_end_doc())
{
_tr_remove_speculative();
_remove_speculative();
_c4dbgp("pop!");
_pop();
}
Expand All @@ -176,7 +173,7 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
if(!m_tree->is_stream(m_tree->root_id())) //if(_should_push_on_begin_doc())
{
_c4dbgp("ensure stream");
_tr_set_root_as_stream();
_set_root_as_stream();
id_type first = m_tree->first_child(m_tree->root_id());
_RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->is_stream(m_tree->root_id()));
_RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->num_children(m_tree->root_id()) == 1u);
Expand All @@ -189,7 +186,7 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
{
_c4dbgp("tweak");
_push();
_tr_remove_speculative();
_remove_speculative();
m_curr->node_id = m_tree->last_child(m_tree->root_id());
m_curr->tr_data = m_tree->_p(m_curr->node_id);
}
Expand All @@ -205,9 +202,7 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
void end_doc_expl()
{
_c4dbgp("end_doc_expl");
{
_tr_remove_speculative();
}
_remove_speculative();
if(_stack_should_pop_on_end_doc())
{
_c4dbgp("pop!");
Expand Down Expand Up @@ -235,14 +230,14 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
{
_c4dbgpf("node[{}]: begin_map_val_flow", m_curr->node_id);
_enable_(MAP|FLOW_SL);
_tr_save_loc();
_save_loc();
_push();
}
void begin_map_val_block()
{
_c4dbgpf("node[{}]: begin_map_val_block", m_curr->node_id);
_enable_(MAP|BLOCK);
_tr_save_loc();
_save_loc();
_push();
}

Expand Down Expand Up @@ -272,14 +267,14 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
{
_c4dbgpf("node[{}]: begin_seq_val_flow", m_curr->node_id);
_enable_(SEQ|FLOW_SL);
_tr_save_loc();
_save_loc();
_push();
}
void begin_seq_val_block()
{
_c4dbgpf("node[{}]: begin_seq_val_block", m_curr->node_id);
_enable_(SEQ|BLOCK);
_tr_save_loc();
_save_loc();
_push();
}

Expand All @@ -301,9 +296,9 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
_RYML_CB_ASSERT(m_stack.m_callbacks, m_parent);
_RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->has_children(m_parent->node_id));
NodeData const* prev = m_tree->m_buf; // watchout against relocation of the tree nodes
_tr_set_state_(m_curr, m_tree->_append_child__unprotected(m_parent->node_id));
_set_state_(m_curr, m_tree->_append_child__unprotected(m_parent->node_id));
if(prev != m_tree->m_buf)
_tr_refresh_after_relocation();
_refresh_after_relocation();
_c4dbgpf("node[{}]: added sibling={} prev={}", m_parent->node_id, m_curr->node_id, m_tree->prev_sibling(m_curr->node_id));
}

Expand All @@ -320,7 +315,7 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
_RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->is_seq(m_parent->node_id));
_RYML_CB_ASSERT(m_stack.m_callbacks, !m_tree->is_container(m_curr->node_id));
_RYML_CB_ASSERT(m_stack.m_callbacks, !m_tree->has_key(m_curr->node_id));
const NodeData tmp = _tr_val2key_(*m_curr->tr_data);
const NodeData tmp = _val2key_(*m_curr->tr_data);
_disable_(_VALMASK|VAL_STYLE);
m_curr->tr_data->m_val = {};
begin_map_val_flow();
Expand Down Expand Up @@ -530,7 +525,7 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
/** @cond dev */
void _reset_parser_state(state* st, id_type parse_root, id_type node)
{
_tr_set_state_(st, node);
_set_state_(st, node);
const NodeType type = m_tree->type(node);
#ifdef RYML_DBG
char flagbuf[80];
Expand Down Expand Up @@ -586,13 +581,13 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
m_curr->node_id = m_tree->_append_child__unprotected(m_parent->node_id);
m_curr->tr_data = m_tree->_p(m_curr->node_id);
if(prev != m_tree->m_buf)
_tr_refresh_after_relocation();
_refresh_after_relocation();
_c4dbgpf("pushed! level={}. top is now node={} (parent={})", m_curr->level, m_curr->node_id, m_parent ? m_parent->node_id : NONE);
}
/** end the current scope */
void _pop()
{
_tr_remove_speculative_with_parent();
_remove_speculative_with_parent();
_stack_pop();
}

Expand All @@ -613,19 +608,19 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle

public:

C4_ALWAYS_INLINE void _tr_set_state_(state *C4_RESTRICT s, id_type id) noexcept
C4_ALWAYS_INLINE void _set_state_(state *C4_RESTRICT s, id_type id) noexcept
{
s->node_id = id;
s->tr_data = m_tree->_p(id);
}
void _tr_refresh_after_relocation()
void _refresh_after_relocation()
{
_c4dbgp("tree: refreshing stack data after tree data relocation");
for(auto &st : m_stack)
st.tr_data = m_tree->_p(st.node_id);
}

void _tr_set_root_as_stream()
void _set_root_as_stream()
{
_c4dbgp("set root as stream");
_RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->root_id() == 0u);
Expand All @@ -639,10 +634,10 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
_RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->is_doc(m_tree->first_child(m_tree->root_id())));
if(hack)
m_tree->_p(m_tree->first_child(m_tree->root_id()))->m_type.rem(VAL);
_tr_set_state_(m_curr, m_tree->root_id());
_set_state_(m_curr, m_tree->root_id());
}

static NodeData _tr_val2key_(NodeData const& C4_RESTRICT d) noexcept
static NodeData _val2key_(NodeData const& C4_RESTRICT d) noexcept
{
NodeData r = d;
r.m_key = d.m_val;
Expand All @@ -656,7 +651,7 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
return r;
}

void _tr_remove_speculative()
void _remove_speculative()
{
_c4dbgp("remove speculative node");
_RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->size() > 0);
Expand All @@ -666,7 +661,7 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
m_tree->remove(last_added);
}

void _tr_remove_speculative_with_parent()
void _remove_speculative_with_parent()
{
_RYML_CB_ASSERT(m_stack.m_callbacks, m_tree->size() > 0);
const id_type last_added = m_tree->size() - 1;
Expand All @@ -678,7 +673,7 @@ struct EventHandlerTree : public EventHandlerStack<EventHandlerTree, EventHandle
}
}

C4_ALWAYS_INLINE void _tr_save_loc()
C4_ALWAYS_INLINE void _save_loc()
{
m_tree->_p(m_curr->node_id)->m_val.scalar.str = m_curr->line_contents.rem.str;
}
Expand Down
Loading

0 comments on commit 7ee863c

Please sign in to comment.