From 339bc5d3f417f09dfc8eb3a99fc4bdf84cdcab55 Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Mon, 1 Apr 2024 23:57:32 +0100 Subject: [PATCH] wip [ci skip] --- src/c4/yml/detail/parser_dbg.hpp | 39 +++++++++++++++--------------- src/c4/yml/detail/print.hpp | 4 ++-- src/c4/yml/event_handler_tree.hpp | 6 +++++ src/c4/yml/parse_engine.def.hpp | 40 +++++++++++++++++++++++-------- test/test_lib/test_group.cpp | 23 ++++++++---------- 5 files changed, 68 insertions(+), 44 deletions(-) diff --git a/src/c4/yml/detail/parser_dbg.hpp b/src/c4/yml/detail/parser_dbg.hpp index 1ca87d068..311bbb608 100644 --- a/src/c4/yml/detail/parser_dbg.hpp +++ b/src/c4/yml/detail/parser_dbg.hpp @@ -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) @@ -67,25 +68,25 @@ #include 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 -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)...); - // 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)...); - if(C4_UNLIKELY(results.bufsize > sizeof(writebuf))) - { - results = format_dump_resume<&_dbg_dumper>(results, writebuf, fmt, std::forward(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) diff --git a/src/c4/yml/detail/print.hpp b/src/c4/yml/detail/print.hpp index 82d5c9c43..dd10964ae 100644 --- a/src/c4/yml/detail/print.hpp +++ b/src/c4/yml/detail/print.hpp @@ -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)) @@ -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)) diff --git a/src/c4/yml/event_handler_tree.hpp b/src/c4/yml/event_handler_tree.hpp index 654f45281..b04099029 100644 --- a/src/c4/yml/event_handler_tree.hpp +++ b/src/c4/yml/event_handler_tree.hpp @@ -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(); @@ -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); { @@ -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 C4_ALWAYS_INLINE void _enable__() noexcept diff --git a/src/c4/yml/parse_engine.def.hpp b/src/c4/yml/parse_engine.def.hpp index 5c605b6b7..bb29f9fc3 100644 --- a/src/c4/yml/parse_engine.def.hpp +++ b/src/c4/yml/parse_engine.def.hpp @@ -434,10 +434,13 @@ template template void ParseEngine::_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 @@ -1372,6 +1375,7 @@ template void ParseEngine::_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); } @@ -1380,6 +1384,7 @@ void ParseEngine::_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); } @@ -1622,9 +1627,12 @@ void ParseEngine::_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) { @@ -1655,31 +1663,43 @@ void ParseEngine::_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; } diff --git a/test/test_lib/test_group.cpp b/test/test_lib/test_group.cpp index 2f45fb194..dddbed0a0 100644 --- a/test/test_lib/test_group.cpp +++ b/test/test_lib/test_group.cpp @@ -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) @@ -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; } @@ -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 { @@ -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); @@ -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);