Skip to content

Commit

Permalink
wip [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed Apr 1, 2024
1 parent 71dd67f commit 3d7b883
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 44 deletions.
39 changes: 20 additions & 19 deletions src/c4/yml/detail/parser_dbg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,19 @@
#else
# define _c4err(fmt, ...) \
do { RYML_DEBUG_BREAK(); this->_err("ERROR:\n" "{}:{}: " fmt, __FILE__, __LINE__, ## __VA_ARGS__); } while(0)
# define _c4dbgt(fmt, ...) do { if(_dbg_enabled()) { this->_dbg ("{}:{}: " fmt , __FILE__, __LINE__, ## __VA_ARGS__); } } while(0)
# define _c4dbgpf(fmt, ...) do { if(_dbg_enabled()) { _dbg_printf("{}:{}: " fmt "\n", __FILE__, __LINE__, ## __VA_ARGS__); } } while(0)
# define _c4dbgpf_(fmt, ...) do { if(_dbg_enabled()) { _dbg_printf("{}:{}: " fmt , __FILE__, __LINE__, ## __VA_ARGS__); } } while(0)
# define _c4dbgp(msg) do { if(_dbg_enabled()) { _dbg_printf("{}:{}: " msg "\n", __FILE__, __LINE__ ); } } while(0)
# define _c4dbgp_(msg) do { if(_dbg_enabled()) { _dbg_printf("{}:{}: " msg , __FILE__, __LINE__ ); } } while(0)
# define _c4dbgq(msg) do { if(_dbg_enabled()) { _dbg_printf(msg "\n"); } } while(0)
# define _c4presc(...) __c4presc(__VA_ARGS__)
# define _c4dbgt(fmt, ...) do { if(_dbg_enabled()) { \
this->_dbg ("{}:{}: " fmt , __FILE__, __LINE__, ## __VA_ARGS__); } } while(0)
# define _c4dbgpf(fmt, ...) _dbg_printf("{}:{}: " fmt "\n", __FILE__, __LINE__, ## __VA_ARGS__)
# define _c4dbgpf_(fmt, ...) _dbg_printf("{}:{}: " fmt , __FILE__, __LINE__, ## __VA_ARGS__)
# define _c4dbgp(msg) _dbg_printf("{}:{}: " msg "\n", __FILE__, __LINE__ )
# define _c4dbgp_(msg) _dbg_printf("{}:{}: " msg , __FILE__, __LINE__ )
# define _c4dbgq(msg) _dbg_printf(msg "\n")
# define _c4presc(...) do { if(_dbg_enabled()) __c4presc(__VA_ARGS__); } while(0)
# define _c4prscalar(msg, scalar, keep_newlines) \
do { \
_c4dbgpf_("{}: [{}]~~~", msg, scalar.len); \
if(_dbg_enabled()) { \
__c4presc((scalar).str, (scalar).len, (keep_newlines)); \
__c4presc((scalar).str, (scalar).len, (keep_newlines)); \
} \
_c4dbgq("~~~"); \
} while(0)
Expand All @@ -67,25 +68,25 @@
#include <c4/dump.hpp>
namespace c4 {
inline bool& _dbg_enabled() { static bool enabled = true; return enabled; }
inline void _set_dbg_enabled(bool yes) { _dbg_enabled() = yes; }
inline void _dbg_set_enabled(bool yes) { _dbg_enabled() = yes; }
inline void _dbg_dumper(csubstr s)
{
if(s.str)
fwrite(s.str, 1, s.len, stdout);
}
inline substr _dbg_buf() noexcept
{
static char writebuf[512];
return writebuf;
}
template<class ...Args>
void _dbg_printf(c4::csubstr fmt, Args&& ...args)
C4_NO_INLINE void _dbg_printf(c4::csubstr fmt, Args const& ...args)
{
static char writebuf[256];
auto results = c4::format_dump_resume<&_dbg_dumper>(writebuf, fmt, std::forward<Args>(args)...);
// resume writing if the results failed to fit the buffer
if(C4_UNLIKELY(results.bufsize > sizeof(writebuf))) // bufsize will be that of the largest element serialized. Eg int(1), will require 1 byte.
if(_dbg_enabled())
{
results = format_dump_resume<&_dbg_dumper>(results, writebuf, fmt, std::forward<Args>(args)...);
if(C4_UNLIKELY(results.bufsize > sizeof(writebuf)))
{
results = format_dump_resume<&_dbg_dumper>(results, writebuf, fmt, std::forward<Args>(args)...);
}
substr buf = _dbg_buf();
const size_t needed_size = c4::format_dump(&_dbg_dumper, buf, fmt, args...);
C4_CHECK(needed_size <= buf.len);
}
}
inline void __c4presc(const char *s, size_t len, bool keep_newlines=false)
Expand Down
4 changes: 2 additions & 2 deletions src/c4/yml/detail/print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ inline id_type print_node(Tree const& p, id_type node, int level, id_type count,
char typebuf[128];
csubstr typestr = p.type(node).type_str(typebuf);
RYML_CHECK(typestr.str);
printf(" %.*s:", (int)typestr.len, typestr.str);
printf(" %.*s", (int)typestr.len, typestr.str);
if(p.has_key(node))
{
if(p.has_key_anchor(node))
Expand Down Expand Up @@ -116,7 +116,7 @@ inline id_type print_node(Tree const& p, id_type node, int level, id_type count,
}
else
{
printf(" %zu children:\n", (size_t)p.num_children(node));
printf(" (%zu children)\n", (size_t)p.num_children(node));
if(print_children)
{
for(id_type i = p.first_child(node); i != NONE; i = p.next_sibling(i))
Expand Down
6 changes: 6 additions & 0 deletions src/c4/yml/event_handler_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ struct EventHandlerTree
* child as the current node */
void _push()
{
printf("====\n");
for(auto const& s : m_stack) printf("pushing! state[%zu]: ind=%zu node=%zu\n", s.level, s.indref, s.tr_id);
m_stack.push_top();
m_parent = &m_stack.top(1); // don't use m_curr. watch out for relocations inside the prev push
m_curr = &m_stack.top();
Expand All @@ -176,11 +178,14 @@ struct EventHandlerTree
_tr_refresh_after_relocation();
_c4dbgpf("pushed! level={}. top is now node={} (parent={})", m_curr->level, m_curr->tr_id, m_parent ? m_parent->tr_id : NONE);
}
printf("----\n");
for(auto const& s : m_stack) printf("pushed! state[%zu]: ind=%zu node=%zu\n", s.level, s.indref, s.tr_id);
}

/** end the current scope */
void _pop()
{
for(auto const& s : m_stack) printf("popping... state[%zu]: ind=%zu node=%zu\n", s.level, s.indref, s.tr_id);
_RYML_CB_ASSERT(m_stack.m_callbacks, m_parent);
_RYML_CB_ASSERT(m_stack.m_callbacks, m_stack.size() > 1);
{
Expand All @@ -198,6 +203,7 @@ struct EventHandlerTree
_c4dbgpf("popped! top is now node={} @ ROOT", m_curr->tr_id);
}
#endif
for(auto const& s : m_stack) printf("popped! state[%zu]: ind=%zu node=%zu\n", s.level, s.indref, s.tr_id);
}

template<type_bits bits> C4_ALWAYS_INLINE void _enable__() noexcept
Expand Down
40 changes: 30 additions & 10 deletions src/c4/yml/parse_engine.def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,13 @@ template<class EventHandler>
template<class ...Args>
void ParseEngine<EventHandler>::_dbg(csubstr fmt, Args const& C4_RESTRICT ...args) const
{
auto dumpfn = [](csubstr s){ if(s.str) fwrite(s.str, 1, s.len, stdout); };
detail::_parse_dump(dumpfn, fmt, args...);
dumpfn("\n");
_fmt_msg(dumpfn);
if(_dbg_enabled())
{
auto dumpfn = [](csubstr s){ if(s.str) fwrite(s.str, 1, s.len, stdout); };
detail::_parse_dump(dumpfn, fmt, args...);
dumpfn("\n");
_fmt_msg(dumpfn);
}
}
#endif

Expand Down Expand Up @@ -1372,6 +1375,7 @@ template<class EventHandler>
void ParseEngine<EventHandler>::_set_indentation(size_t indentation)
{
m_state->indref = indentation;
printf("state[%zu]: fdx saving indentation: %zu\n", m_state->level, m_state->indref);
_c4dbgpf("state[{}]: saving indentation: {}", m_state->level, m_state->indref);
}

Expand All @@ -1380,6 +1384,7 @@ void ParseEngine<EventHandler>::_save_indentation()
{
_RYML_CB_ASSERT(m_evt_handler->m_stack.m_callbacks, m_state->line_contents.rem.begin() >= m_state->line_contents.full.begin());
m_state->indref = m_state->line_contents.current_col();
//printf("state[%zu]: crl saving indentation: %zu\n", m_state->level, m_state->indref);
_c4dbgpf("state[{}]: saving indentation: {}", m_state->level, m_state->indref);
}

Expand Down Expand Up @@ -1622,9 +1627,12 @@ void ParseEngine<EventHandler>::_handle_indentation_pop_from_block_seq()
_RYML_CB_ASSERT(stack.m_callbacks, m_state >= stack.begin() && m_state < stack.end());
const size_t ind = m_state->line_contents.indentation;
#ifdef RYML_DBG
char flagbuf_[128];
for(state_type const& s : stack)
_dbg_printf("state[{}]: ind={} node={} flags={}\n", s.level, s.indref, s.tr_id, detail::_parser_flags_to_str(flagbuf_, s.flags));
if(_dbg_enabled())
{
char flagbuf_[128];
for(state_type const& s : stack)
_dbg_printf("state[{}]: ind={} node={} flags={}\n", s.level, s.indref, s.tr_id, detail::_parser_flags_to_str(flagbuf_, s.flags));
}
#endif
for(state_type const* s = m_state-1; s >= stack.begin(); --s)
{
Expand Down Expand Up @@ -1655,31 +1663,43 @@ void ParseEngine<EventHandler>::_handle_indentation_pop_from_block_map()
state_type const* popto = nullptr;
#ifdef RYML_DBG
char flagbuf_[128];
for(state_type const& s : stack)
_dbg_printf("state[{}]: ind={} node={} flags={}\n", s.level, s.indref, s.tr_id, detail::_parser_flags_to_str(flagbuf_, s.flags));
if(_dbg_enabled())
{
for(state_type const& s : stack)
_dbg_printf("state[{}]: ind={} node={} flags={}\n", s.level, s.indref, s.tr_id, detail::_parser_flags_to_str(flagbuf_, s.flags));
}
#endif
for(state_type const& s : stack) printf("state[%zu]: ind=%zu node=%zu\n", s.level, s.indref, s.tr_id);
for(state_type const* s = m_state-1; s > stack.begin(); --s) // never go to the stack bottom. that's the root
{
printf("aqui 1 indentation %zu. current: ind=%zu,level=%zu,node=%zu\n", (size_t)ind, (size_t)s->indref, (size_t)s->level, (size_t)s->tr_id);
_c4dbgpf("searching for state with indentation {}. current: ind={},level={},node={},flags={}", ind, s->indref, s->level, s->tr_id, detail::_parser_flags_to_str(flagbuf_, s->flags));
if(s->indref < ind)
{
printf("aqui 2 s->indref=%zu\n", (size_t)s->indref);
break;
}
else if(s->indref == ind)
{
printf("aqui 3 s->indref=%zu\n", (size_t)s->indref);
_c4dbgpf("same indentation!!! level={} node={}", s->level, s->tr_id);
if(popto && has_any(RTOP, s) && has_none(RMAP|RSEQ, s))
{
printf("aqui 3.1 s->indref=%zu\n", (size_t)s->indref);
break;
}
popto = s;
if(has_all(RSEQ|BLCK, s))
{
printf("aqui 3.2 s->indref=%zu\n", (size_t)s->indref);
csubstr rem = m_state->line_contents.rem;
const size_t first = rem.first_not_of(' ');
_RYML_CB_ASSERT(stack.m_callbacks, first == ind || first == npos);
rem = rem.right_of(first, true);
_c4dbgpf("indentless? rem='{}' first={}", rem, first);
if(rem == "-" || rem.begins_with("- "))
if(rem.begins_with('-') && _is_blck_token(rem))
{
printf("aqui 3.3 s->indref=%zu\n", (size_t)s->indref);
_c4dbgp("parent was indentless seq");
break;
}
Expand Down
23 changes: 10 additions & 13 deletions test/test_lib/test_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ namespace yml {
void YmlTestCase::_test_parse_using_ryml(CaseDataLineEndings *cd)
{
#ifdef RYML_DBG
printf("---------------\n%.*s\n---------------\n", (int)c->src.len, c->src.str);
if(_dbg_enabled())
printf("---------------\n%.*s\n---------------\n", (int)c->src.len, c->src.str);
#endif

if(c->flags & EXPECT_PARSE_ERROR)
Expand All @@ -25,12 +26,9 @@ void YmlTestCase::_test_parse_using_ryml(CaseDataLineEndings *cd)
parse_in_place(c->fileline, cd->src, &cd->parsed_tree);
if(flags & RESOLVE_REFS)
cd->parsed_tree.resolve();
#ifdef RYML_DBG
// if this point was reached, then it means that the expected
// error failed to occur. So print debugging info.
printf("failed to catch expected error while parsing.\n");
print_tree("PARSED TREE", cd->parsed_tree);
#endif
_c4dbg_tree("UNEXPECTED PARSED TREE", cd->parsed_tree);
}, c->expected_location);
return;
}
Expand All @@ -39,8 +37,11 @@ void YmlTestCase::_test_parse_using_ryml(CaseDataLineEndings *cd)
parse_in_place(c->fileline, cd->src, &cd->parsed_tree);

#ifdef RYML_DBG
print_test_tree("REF TREE", c->root);
print_tree("PARSED TREE", cd->parsed_tree);
if(_dbg_enabled())
{
print_test_tree("REF TREE", c->root);
_c4dbg_tree("PARSED TREE", cd->parsed_tree);
}
#endif

{
Expand All @@ -55,9 +56,7 @@ void YmlTestCase::_test_parse_using_ryml(CaseDataLineEndings *cd)
if(c->flags & RESOLVE_REFS)
{
cd->parsed_tree.resolve();
#ifdef RYML_DBG
print_tree("resolved tree!!!\n", cd->parsed_tree);
#endif
_c4dbg_tree("resolved tree!!!", cd->parsed_tree);
{
SCOPED_TRACE("checking tree invariants of resolved parsed tree");
test_invariants(cd->parsed_tree);
Expand All @@ -78,9 +77,7 @@ void YmlTestCase::_test_parse_using_ryml(CaseDataLineEndings *cd)
if(c->flags & RESOLVE_REFS)
{
cd->parsed_tree.reorder();
#ifdef RYML_DBG
print_tree("reordered tree!!!\n", cd->parsed_tree);
#endif
_c4dbg_tree("reordered tree!!!", cd->parsed_tree);
{
SCOPED_TRACE("checking tree invariants of reordered parsed tree after resolving");
test_invariants(cd->parsed_tree);
Expand Down

0 comments on commit 3d7b883

Please sign in to comment.