Skip to content

Commit

Permalink
[refac] split event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed Mar 27, 2024
1 parent cbcdefa commit 12b70a9
Show file tree
Hide file tree
Showing 15 changed files with 1,094 additions and 710 deletions.
716 changes: 71 additions & 645 deletions src/c4/yml/event_handler_tree.hpp

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/c4/yml/parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
namespace c4 {
namespace yml {

template<bool is_events, class EventStrSink> struct EventHandler;
using EventHandlerTree = EventHandler<false, int>;
struct EventHandlerTree;
template<class EventHandler> class ParseEngine;
class NodeRef;
class Tree;
Expand Down
8 changes: 4 additions & 4 deletions src/c4/yml/parse_engine.def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#define _add_flags2(f) this->add_flags2(f)()
#define _addrem_flags2(on, off) this->addrem_flags(on, off)
#define _rem_flags2(off) this->rem_flags2(off)
#define m_state (_st_tree()) // FIXME REMOVE
#define m_state (m_evt_handler->m_curr) // FIXME REMOVE
#define _c4dbgnextline() \
do { \
_c4dbgq("\n-----------"); \
Expand Down Expand Up @@ -299,7 +299,7 @@ template<class EventHandler>
template<class DumpFn>
void ParseEngine<EventHandler>::_fmt_msg(DumpFn &&dumpfn) const
{
auto const *const C4_RESTRICT st = _st_tree();
auto const *const C4_RESTRICT st = m_evt_handler->m_curr;
auto const& lc = st->line_contents;
csubstr contents = lc.stripped;
if(contents.len)
Expand Down Expand Up @@ -7650,7 +7650,7 @@ void ParseEngine<EventHandler>::parse_json_in_place_ev(csubstr filename, substr
while( ! _finished_line())
{
_c4dbgnextline();
_RYML_CB_ASSERT(m_evt_handler->m_stack.m_callbacks, ! _st_tree()->line_contents.rem.empty());
_RYML_CB_ASSERT(m_evt_handler->m_stack.m_callbacks, ! m_evt_handler->m_curr->line_contents.rem.empty());
if(has_any(RSEQ))
{
_handle2_seq_json();
Expand Down Expand Up @@ -7695,7 +7695,7 @@ void ParseEngine<EventHandler>::parse_in_place_ev(csubstr filename, substr src)
while( ! _finished_line())
{
_c4dbgnextline();
_RYML_CB_ASSERT(m_evt_handler->m_stack.m_callbacks, ! _st_tree()->line_contents.rem.empty());
_RYML_CB_ASSERT(m_evt_handler->m_stack.m_callbacks, ! m_evt_handler->m_curr->line_contents.rem.empty());
if(has_any(FLOW))
{
if(has_none(RSEQIMAP))
Expand Down
34 changes: 16 additions & 18 deletions src/c4/yml/parse_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,6 @@ RYML_EXPORT class RYML_EXPORT ParseEngine
public://FIXME scaffold for sink refactor

Check notice

Code scanning / CodeQL

FIXME comment Note

FIXME comment: scaffold for sink refactor
using State = typename EventHandler::state;
EventHandler *C4_RESTRICT m_evt_handler;
C4_ALWAYS_INLINE auto const* _st_tree() const noexcept { return m_evt_handler->m_curr; }
C4_ALWAYS_INLINE auto* _st_tree() noexcept { return m_evt_handler->m_curr; }
bool m_was_inside_qmrk;
private://FIXME

Check notice

Code scanning / CodeQL

FIXME comment Note

FIXME comment

Expand All @@ -559,17 +557,17 @@ RYML_EXPORT class RYML_EXPORT ParseEngine

inline bool _at_line_begin() const
{
return _st_tree()->line_contents.rem.begin() == _st_tree()->line_contents.full.begin();
return m_evt_handler->m_curr->line_contents.rem.begin() == m_evt_handler->m_curr->line_contents.full.begin();
}
inline bool _at_line_end() const
{
csubstr r = _st_tree()->line_contents.rem;
csubstr r = m_evt_handler->m_curr->line_contents.rem;
return r.empty() || r.begins_with(' ', r.len);
}

C4_ALWAYS_INLINE bool has_all(ParserFlag_t f) const noexcept { return (_st_tree()->flags & f) == f; }
C4_ALWAYS_INLINE bool has_any(ParserFlag_t f) const noexcept { return (_st_tree()->flags & f) != 0; }
C4_ALWAYS_INLINE bool has_none(ParserFlag_t f) const noexcept { return (_st_tree()->flags & f) == 0; }
C4_ALWAYS_INLINE bool has_all(ParserFlag_t f) const noexcept { return (m_evt_handler->m_curr->flags & f) == f; }
C4_ALWAYS_INLINE bool has_any(ParserFlag_t f) const noexcept { return (m_evt_handler->m_curr->flags & f) != 0; }
C4_ALWAYS_INLINE bool has_none(ParserFlag_t f) const noexcept { return (m_evt_handler->m_curr->flags & f) == 0; }
static C4_ALWAYS_INLINE bool has_all(ParserFlag_t f, ParserState const* C4_RESTRICT s) noexcept { return (s->flags & f) == f; }
static C4_ALWAYS_INLINE bool has_any(ParserFlag_t f, ParserState const* C4_RESTRICT s) noexcept { return (s->flags & f) != 0; }
static C4_ALWAYS_INLINE bool has_none(ParserFlag_t f, ParserState const* C4_RESTRICT s) noexcept { return (s->flags & f) == 0; }
Expand All @@ -579,24 +577,24 @@ RYML_EXPORT class RYML_EXPORT ParseEngine
static void add_flags(ParserFlag_t on, ParserState *C4_RESTRICT s);
static void addrem_flags(ParserFlag_t on, ParserFlag_t off, ParserState *C4_RESTRICT s);
static void rem_flags(ParserFlag_t off, ParserState *C4_RESTRICT s);
C4_ALWAYS_INLINE void set_flags(ParserFlag_t f) noexcept { set_flags(f, _st_tree()); }
C4_ALWAYS_INLINE void add_flags(ParserFlag_t on) noexcept { add_flags(on, _st_tree()); }
C4_ALWAYS_INLINE void addrem_flags(ParserFlag_t on, ParserFlag_t off) noexcept { addrem_flags(on, off, _st_tree()); }
C4_ALWAYS_INLINE void rem_flags(ParserFlag_t off) noexcept { rem_flags(off, _st_tree()); }
C4_ALWAYS_INLINE void set_flags(ParserFlag_t f) noexcept { set_flags(f, m_evt_handler->m_curr); }
C4_ALWAYS_INLINE void add_flags(ParserFlag_t on) noexcept { add_flags(on, m_evt_handler->m_curr); }
C4_ALWAYS_INLINE void addrem_flags(ParserFlag_t on, ParserFlag_t off) noexcept { addrem_flags(on, off, m_evt_handler->m_curr); }
C4_ALWAYS_INLINE void rem_flags(ParserFlag_t off) noexcept { rem_flags(off, m_evt_handler->m_curr); }
#else
C4_ALWAYS_INLINE static void set_flags(ParserFlag_t f, ParserState *C4_RESTRICT s) noexcept { s->flags = f; }
C4_ALWAYS_INLINE static void add_flags(ParserFlag_t on, ParserState *C4_RESTRICT s) noexcept { s->flags |= on; }
C4_ALWAYS_INLINE static void addrem_flags(ParserFlag_t on, ParserFlag_t off, ParserState *C4_RESTRICT s) noexcept { s->flags &= ~off; s->flags |= on; }
C4_ALWAYS_INLINE static void rem_flags(ParserFlag_t off, ParserState *C4_RESTRICT s) noexcept { s->flags &= ~off; }
C4_ALWAYS_INLINE void set_flags(ParserFlag_t f) noexcept { _st_tree()->flags = f; }
C4_ALWAYS_INLINE void add_flags(ParserFlag_t on) noexcept { _st_tree()->flags |= on; }
C4_ALWAYS_INLINE void addrem_flags(ParserFlag_t on, ParserFlag_t off) noexcept { _st_tree()->flags &= ~off; _st_tree()->flags |= on; }
C4_ALWAYS_INLINE void rem_flags(ParserFlag_t off) noexcept { _st_tree()->flags &= ~off; }
C4_ALWAYS_INLINE void set_flags(ParserFlag_t f) noexcept { m_evt_handler->m_curr->flags = f; }
C4_ALWAYS_INLINE void add_flags(ParserFlag_t on) noexcept { m_evt_handler->m_curr->flags |= on; }
C4_ALWAYS_INLINE void addrem_flags(ParserFlag_t on, ParserFlag_t off) noexcept { m_evt_handler->m_curr->flags &= ~off; m_evt_handler->m_curr->flags |= on; }
C4_ALWAYS_INLINE void rem_flags(ParserFlag_t off) noexcept { m_evt_handler->m_curr->flags &= ~off; }
#endif

csubstr _scan2_anchor()
{
csubstr s = _st_tree()->line_contents.rem;
csubstr s = m_evt_handler->m_curr->line_contents.rem;
_RYML_CB_ASSERT(m_evt_handler->m_stack.m_callbacks, s.begins_with('&'));
csubstr anchor = s.range(1, s.first_of(' '));
_line_progressed(1u + anchor.len);
Expand All @@ -605,15 +603,15 @@ RYML_EXPORT class RYML_EXPORT ParseEngine
}
csubstr _scan2_ref_seq()
{
csubstr s = _st_tree()->line_contents.rem;
csubstr s = m_evt_handler->m_curr->line_contents.rem;
_RYML_CB_ASSERT(m_evt_handler->m_stack.m_callbacks, s.begins_with('*'));
csubstr ref = s.first(s.first_of(",] :"));
_line_progressed(ref.len);
return ref;
}
csubstr _scan2_ref_map()
{
csubstr s = _st_tree()->line_contents.rem;
csubstr s = m_evt_handler->m_curr->line_contents.rem;
_RYML_CB_ASSERT(m_evt_handler->m_stack.m_callbacks, s.begins_with('*'));
csubstr ref = s.first(s.first_of(",} "));
_line_progressed(ref.len);
Expand Down
5 changes: 5 additions & 0 deletions src/c4/yml/tag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace yml {

class Tree;

#ifndef RYML_MAX_TAG_DIRECTIVES
/** the maximum number of tag directives in a Tree */
#define RYML_MAX_TAG_DIRECTIVES 4
#endif

/** the integral type necessary to cover all the bits marking node tags */
using tag_bits = uint16_t;

Expand Down
10 changes: 0 additions & 10 deletions src/c4/yml/tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,6 @@ bool from_chars_float(csubstr buf, T *C4_RESTRICT val)
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

#ifndef RYML_MAX_TAG_DIRECTIVES
/** the maximum number of tag directives in a Tree */
#define RYML_MAX_TAG_DIRECTIVES 4
#endif


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
Expand Down
17 changes: 12 additions & 5 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ endif()
function(ryml_add_test test_name)
set(t ryml-test-${test_name})
c4_add_executable(${t}
SOURCES test_${test_name}.cpp
LIBS ${ARGN} ryml-_testlib gtest_main
SOURCES test_${test_name}.cpp ${ARGN}
LIBS ryml-_testlib gtest_main
FOLDER test)
c4_add_test(${t})
endfunction()
Expand All @@ -44,7 +44,8 @@ c4_add_library(ryml-_testgroup LIBRARY_TYPE OBJECT
LIBS ryml ryml-_testlib c4fs
FOLDER test)
function(ryml_add_test_case_group name)
ryml_add_test(${name} ryml-_testgroup)
ryml_add_test(${name})
target_link_directories(ryml-test-${name} PUBLIC ryml-_testgroup)
endfunction()


Expand All @@ -54,7 +55,9 @@ endfunction()
ryml_add_test(callbacks)
ryml_add_test(stack)
ryml_add_test(filter)
ryml_add_test(parse_engine)
ryml_add_test(parse_engine
test_suite/test_suite_event_handler.cpp
test_suite/test_suite_event_handler.hpp)
ryml_add_test(parser)
ryml_add_test(tree)
ryml_add_test(noderef)
Expand All @@ -66,7 +69,9 @@ ryml_add_test(json)
ryml_add_test(preprocess)
ryml_add_test(merge)
ryml_add_test(location)
ryml_add_test(yaml_events)
ryml_add_test(yaml_events
test_suite/test_suite_event_handler.cpp
test_suite/test_suite_event_handler.hpp)
ryml_add_test_case_group(empty_file)
ryml_add_test_case_group(empty_map)
ryml_add_test_case_group(empty_seq)
Expand Down Expand Up @@ -176,6 +181,8 @@ if(RYML_TEST_SUITE)
test_suite.cpp
test_suite/test_suite_common.hpp
test_suite/test_suite_events_emitter.cpp
test_suite/test_suite_event_handler.cpp
test_suite/test_suite_event_handler.hpp
test_suite/test_suite_events.cpp
test_suite/test_suite_events.hpp
test_suite/test_suite_parts.cpp
Expand Down
17 changes: 4 additions & 13 deletions test/test_lib/test_engine.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
#include "./test_engine.hpp"


// FIXME instantiate the templates
#include "c4/yml/parse_engine.def.hpp"
namespace c4 {
namespace yml {
using EventHandlerStdStr = EventHandlerStd<EventSink>;
template class ParseEngine<EventHandlerStdStr>;
} // namespace yml
} // namespace c4


namespace c4 {
namespace yml {
Expand Down Expand Up @@ -51,9 +42,9 @@ void test_expected_error_events_from_yaml(std::string const& parsed_yaml, Locati
{
ExpectError::do_check([&]{
EventSink sink;
EventHandlerStdStr handler(&sink);
EventHandlerYamlStd handler(&sink);
handler.reset();
ParseEngine<EventHandlerStdStr> parser(&handler);
ParseEngine<EventHandlerYamlStd> parser(&handler);
std::string copy = parsed_yaml;
parser.parse_in_place_ev("(testyaml)", to_substr(copy));
}, expected_error_location);
Expand All @@ -76,9 +67,9 @@ void test_expected_error_tree_from_yaml(std::string const& parsed_yaml, Location
void test_new_parser_events_from_yaml(ReferenceYaml const& yaml, std::string const& expected_events)
{
EventSink sink;
EventHandlerStdStr handler(&sink);
EventHandlerYamlStd handler(&sink);
handler.reset();
ParseEngine<EventHandlerStdStr> parser(&handler);
ParseEngine<EventHandlerYamlStd> parser(&handler);
std::string copy = yaml.parsed;
parser.parse_in_place_ev("(testyaml)", to_substr(copy));
_c4dbgpf("~~~\n{}~~~\n", sink.result);
Expand Down
4 changes: 2 additions & 2 deletions test/test_lib/test_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
#endif
#include <gtest/gtest.h>
#include "./test_lib/test_case.hpp"
#include "./test_suite/test_suite_event_handler.hpp"


namespace c4 {
namespace yml {
using EventHandlerStdStr = EventHandlerStd<EventSink>;


struct ReferenceYaml
Expand All @@ -37,7 +37,7 @@ struct ReferenceYaml
template<template<class> class EventProducerFn>
C4_NO_INLINE void test_new_parser_str_from_events(std::string const& expected_events)
{
using Handler = EventHandlerStdStr;
using Handler = EventHandlerYamlStd;
EventSink sink;
Handler handler(&sink);
handler.reset();
Expand Down
7 changes: 3 additions & 4 deletions test/test_suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "test_suite/test_suite_common.hpp"
#include "test_suite/test_suite_parts.hpp"
#include "test_suite/test_suite_events.hpp"
#include "test_suite/test_suite_event_handler.hpp"
#include <c4/fs/fs.hpp>
#include <c4/log/log.hpp>
#include <gtest/gtest.h>
Expand Down Expand Up @@ -132,8 +133,6 @@ struct TestSuiteCaseEvents
/** a processing level */
struct TestSequenceLevel
{
using EventHandlerStrSink = EventHandlerStd<EventSink>;
using EventParserStrSink = ParseEngine<EventHandlerStrSink>;
size_t level;
TestSequenceLevel *prev;
csubstr filename;
Expand All @@ -145,8 +144,8 @@ struct TestSequenceLevel
std::string emitted_from_tree_parsed_from_src;

EventSink evt_str_sink;
EventHandlerStrSink evt_handler_str_sink;
EventParserStrSink parser_str_sink;
EventHandlerYamlStd evt_handler_str_sink;
ParseEngine<EventHandlerYamlStd> parser_str_sink;

bool immutable = false;
bool reuse = false;
Expand Down
13 changes: 13 additions & 0 deletions test/test_suite/test_suite_event_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef RYML_SINGLE_HEADER
#include <c4/yml/node.hpp>
#include <c4/yml/std/string.hpp>
#endif
#include "./test_suite_event_handler.hpp"

// FIXME instantiate the templates
#include "c4/yml/parse_engine.def.hpp"
namespace c4 {
namespace yml {
template class ParseEngine<EventHandlerYamlStd>;
} // namespace yml
} // namespace c4
Loading

0 comments on commit 12b70a9

Please sign in to comment.