xo-reader2 xo-expression2: pprint for DDefineExpr + DVariable

This commit is contained in:
Roland Conybeare 2026-01-20 15:06:58 -05:00
commit eca64875fd
7 changed files with 144 additions and 5 deletions

View file

@ -8,6 +8,7 @@
#include "SyntaxStateMachine.hpp"
#include <xo/arena/DArena.hpp>
#include <xo/facet/obj.hpp>
#include <xo/indentlog/print/pretty.hpp>
namespace xo {
namespace scm {
@ -21,6 +22,7 @@ namespace xo {
class ParserStack {
public:
using DArena = xo::mm::DArena;
using ppindentinfo = xo::print::ppindentinfo;
public:
ParserStack(DArena::Checkpoint ckp,
@ -42,6 +44,11 @@ namespace xo {
obj<ASyntaxStateMachine> top() const noexcept { return ssm_; }
ParserStack * parent() const noexcept { return parent_; }
/** regular printing **/
void print(std::ostream & os) const;
/** pretty-printer support **/
bool pretty(const ppindentinfo & ppii) const;
private:
/** stack pointer: top of stack just before this instance created **/
DArena::Checkpoint ckp_;
@ -51,7 +58,32 @@ namespace xo {
ParserStack * parent_ = nullptr;
};
inline std::ostream & operator<< (std::ostream & os, const ParserStack * x) {
if (x) {
x->print(os);
} else {
os << "nullptr";
}
return os;
}
} /*namespace scm*/
namespace print {
/** pretty printer in <xo/indentlog/print/pretty.hpp> relies on this specialization
* to handle ParserResult instances
**/
template <>
struct ppdetail<xo::scm::ParserStack*> {
static inline bool print_pretty(const ppindentinfo & ppii, const xo::scm::ParserStack * p) {
if (p)
return p->pretty(ppii);
else
return ppii.pps()->print_upto("nullptr");
}
};
}
} /*namespace xo*/
/* end ParserStack.hpp */

View file

@ -19,7 +19,8 @@ namespace xo {
//
class ASyntaxStateMachine;
// note: load-bearing to forward-declare ParserStack,
// note: it's load-bearing here to forward-declare ParserStack,
// see ParserStack.hpp for impl
// because ASyntaxStateMachine.hpp includes ParserStateMachine.hpp;
// before obj<SyntaxStateMachine> is defined.
class ParserStack;

View file

@ -153,9 +153,10 @@ namespace xo {
**/
class SchematikaParser {
public:
using token_type = Token;
using ArenaConfig = xo::mm::ArenaConfig;
using AAllocator = xo::mm::AAllocator;
using token_type = Token;
using ppindentinfo = xo::print::ppindentinfo;
public:
/** create parser in initial state;
@ -216,6 +217,8 @@ namespace xo {
/** print human-readable representation on stream @p os **/
void print(std::ostream & os) const;
/** pretty-printer support **/
bool pretty(const ppindentinfo & ppii) const;
private:
/** state machine **/
@ -227,12 +230,31 @@ namespace xo {
inline std::ostream &
operator<< (std::ostream & os,
const SchematikaParser & x) {
x.print(os);
const SchematikaParser * x) {
if (x) {
x->print(os);
} else {
os << "nullptr";
}
return os;
}
} /*namespace scm*/
namespace print {
/** pretty printer in <xo/indentlog/print/pretty.hpp> relies on this specialization
* to handle ParserResult instances
**/
template <>
struct ppdetail<xo::scm::SchematikaParser*> {
static inline bool print_pretty(const ppindentinfo & ppii, const xo::scm::SchematikaParser* p) {
if (p)
return p->pretty(ppii);
else
return ppii.pps()->print_upto("nullptr");
}
};
}
} /*namespace xo*/
/* end SchematikaParser.hpp */

View file

@ -6,6 +6,9 @@
#include "DDefineSsm.hpp"
#include "DExpectSymbolSsm.hpp"
#include "ssm/ISyntaxStateMachine_DDefineSsm.hpp"
#include "ssm/IPrintable_DDefineSsm.hpp"
#include <xo/expression2/detail/IPrintable_DDefineExpr.hpp>
#include <xo/printable2/Printable.hpp>
#ifdef NOT_YET
#include "parserstatemachine.hpp"
@ -16,6 +19,7 @@
#endif
namespace xo {
using xo::print::APrintable;
using xo::facet::with_facet;
using xo::facet::typeseq;
@ -497,10 +501,13 @@ namespace xo {
bool
DDefineSsm::pretty(const ppindentinfo & ppii) const
{
auto expr = with_facet<APrintable>::mkobj(def_expr_.data());
return ppii.pps()->pretty_struct
(ppii,
"DDefineSsm",
refrtag("defstate", defstate_));
refrtag("defstate", defstate_),
refrtag("def_expr", expr));
}
} /*namespace scm*/
} /*namespace xo*/

View file

@ -5,8 +5,12 @@
#include "ParserStack.hpp"
#include "SyntaxStateMachine.hpp"
#include <xo/printable2/Printable.hpp>
#include <xo/facet/FacetRegistry.hpp>
namespace xo {
using xo::print::APrintable;
using xo::facet::FacetRegistry;
using xo::facet::typeseq;
namespace scm {
@ -41,6 +45,48 @@ namespace xo {
return stack->parent();
}
void
ParserStack::print(std::ostream & os) const
{
os << "<ParserStack>";
os << xtag("ssm", "*placeholder*");
os << xtag("parent", "*placeholder*");
os << ">";
}
bool
ParserStack::pretty(const ppindentinfo & ppii) const
{
auto * pps = ppii.pps();
/* always use multiple lines */
if (ppii.upto())
return false;
pps->write("<ParserStack");
const ParserStack * frame = this;
std::size_t i_frame = 0;
while (frame) {
char buf[80];
snprintf(buf, sizeof(buf), "[%lu]", i_frame);
auto ssm = FacetRegistry::instance().variant<APrintable,
ASyntaxStateMachine> (frame->top());
pps->newline_pretty_tag(ppii.ci1(), buf, ssm);
++i_frame;
frame = frame->parent_;
}
pps->write(">");
return false;
}
} /*namespace scm*/
} /*namespace xo*/

View file

@ -86,6 +86,28 @@ namespace xo {
<< xtag("has_stack", (psm_.stack() != nullptr))
<< ">" << std::endl;
}
bool
SchematikaParser::pretty(const ppindentinfo & ppii) const {
auto * pps = ppii.pps();
if (ppii.upto())
return false;
// TODO: consider printing:
// psm.stringtable_
// psm.parser_alloc_
// psm.parser_alloc_ckp_
// psm.expr_alloc_
// psm.result_
// psm.debug_flag_
//
return pps->pretty_struct
(ppii,
"SchematikaParser",
refrtag("stack", psm_.stack()));
}
} /*namespace scm*/
} /*namespace xo*/

View file

@ -4,6 +4,7 @@
**/
#include <xo/reader2/SchematikaParser.hpp>
#include <xo/reader2/init_reader2.hpp>
#include <xo/alloc2/arena/IAllocator_DArena.hpp>
#include <catch2/catch.hpp>
@ -17,6 +18,8 @@ namespace xo {
using xo::mm::DArena;
using xo::facet::with_facet;
static InitEvidence s_init = (InitSubsys<S_reader2_tag>::require());
namespace ut {
TEST_CASE("SchematikaParser-ctor", "[reader2][SchematikaParser]")
{
@ -92,6 +95,10 @@ namespace xo {
// but is still "at toplevel" in the sense of ready for input
REQUIRE(parser.has_incomplete_expr() == true);
REQUIRE(result.is_incomplete());
log && log("after def token:");
log && log(xtag("parser", &parser));
log && log(xtag("result", result));
}
{
@ -100,6 +107,8 @@ namespace xo {
REQUIRE(parser.has_incomplete_expr() == true);
REQUIRE(result.is_incomplete());
log && log("after lhs symbol token:");
log && log(xtag("parser", &parser));
log && log(xtag("result", result));
}