xo-interpreter2 stack: + literal array parsing

This commit is contained in:
Roland Conybeare 2026-03-03 12:12:09 +11:00
commit 680416d077
18 changed files with 812 additions and 51 deletions

View file

@ -0,0 +1,138 @@
/** @file DExpectQArraySsm.hpp
*
* @author Roland Conybeare, Mar 2026
**/
#pragma once
#include "DSyntaxStateMachine.hpp"
#include <xo/object2/ListOps.hpp>
//#include <xo/arena/DArena.hpp>
#include <xo/facet/obj.hpp>
namespace xo {
namespace scm {
/**
* Already in quoted-literal context
*
* [ quote-expr ... ]
* ^ ^ ^
* | qarray_1a qarray_2(done)
* qarray_0
*
* qarray_0 --on_leftbrace_token()--> qarray_1a [push ExpectQLiteralSsm]
* qarray_1a --on_quoted_literal()--> qarray_1a [append literal]
* qarray_1a --on_rightbrace_token()--> qarray_2(done) [report quoted array]
**/
class QArrayXst {
public:
enum class code {
invalid = -1,
qarray_0,
qarray_1a,
qarray_2,
N
};
explicit QArrayXst(code x) : code_{x} {}
/** @return string representation for enum @p x **/
static const char * _descr(code x);
code code() const noexcept { return code_; }
enum code code_;
};
inline std::ostream &
operator<< (std::ostream & os, QArrayXst x) {
os << QArrayXst::_descr(x.code_);
return os;
}
/** @class DExpectQArraySsm
* @brief parser state-machine for a literal array
**/
class DExpectQArraySsm : public DSyntaxStateMachine<DExpectQArraySsm> {
public:
using Super = DSyntaxStateMachine<DExpectQArraySsm>;
using AAllocator = xo::mm::AAllocator;
using DArena = xo::mm::DArena;
using TypeDescr = xo::reflect::TypeDescr;
using ppindentinfo = xo::print::ppindentinfo;
using size_type = std::uint32_t;
public:
/** @defgroup scm-expectqarrayssm-ctors constructors **/
///@{
DExpectQArraySsm();
/** create instance, using memory from @parser_mm **/
static obj<ASyntaxStateMachine,DExpectQArraySsm> make(DArena & parser_mm);
static DExpectQArraySsm * _make(DArena & parser_mm);
/** start nested syntax for a quoted literal **/
static void start(ParserStateMachine * p_psm);
///@}
/** @defgroup scm-expectformalargarrayssm-methods general methods **/
///@{
static const char * ssm_classname() { return "DExpectQArraySsm"; }
/** update state on incoming token @p tk, with overall parser state in @p p_psm **/
void on_leftbracket_token(const Token & tk,
ParserStateMachine * p_psm);
void on_rightbracket_token(const Token & tk,
ParserStateMachine * p_psm);
///@}
/** @defgroup scm-expectqarrayssm-ssm-facet syntaxstatemachine facet methods **/
///@{
/** identifies the ssm implemented here **/
syntaxstatetype ssm_type() const noexcept;
/** mnemonic for expected input (for this ssm) in current state **/
std::string_view get_expect_str() const;
/** update state on incoming token @p tk,
* with overall parser state in @p p_psm
**/
void on_token(const Token & tk,
ParserStateMachine * p_psm);
/** update state for nested qliteral @p lit, with overall parser state in @p p_psm.
* Appends @p lit to target array
**/
void on_quoted_literal(obj<AGCObject> lit,
ParserStateMachine * p_psm);
///@}
/** @defgroup scm-expectqarrayssm-printable-facet printable facet methods **/
///@{
bool pretty(const ppindentinfo & ppii) const;
///@}
private:
/** @defgroup scm-expectqarrayssm-member-vars **/
///@{
/** identifies qarray parsing state **/
QArrayXst state_;
/** target array **/
DArray * array_ = nullptr;
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end DExpectQArraySsm.hpp */

View file

@ -53,7 +53,7 @@ namespace xo {
}
/** @class DExpectQListSsm
* @brief parser state-machine for a formal parameter list
* @brief parser state-machine for a literal list
**/
class DExpectQListSsm : public DSyntaxStateMachine<DExpectQListSsm> {
public:
@ -120,21 +120,6 @@ namespace xo {
///@}
private:
/** @defgroup scm-expectqlistssm-impl-methods **/
///@{
/** update local state to include parsed formal (param_name, param_type).
* If stack memory needed, get from @p parser_alloc.
* Lifetime until formal arglist completely parsed
**/
void _accept_formal(obj<AAllocator> expr_alloc,
DArena & parser_alloc,
const DUniqueString * param_name,
TypeDescr param_type);
///@}
private:
/** @defgroup scm-expectqlistssm-member-vars **/
///@{

View file

@ -24,17 +24,21 @@ namespace xo {
using size_type = std::uint32_t;
public:
explicit DExpectQLiteralSsm(bool cxl_on_rightparen);
explicit DExpectQLiteralSsm(bool cxl_on_rightparen,
bool cxl_on_rightbracket);
/** create instance, using memory from @parser_mm **/
static obj<ASyntaxStateMachine,DExpectQLiteralSsm> make(DArena & parser_mm,
bool cxl_on_rightparen);
bool cxl_on_rightparen,
bool cxl_on_rightbracket);
static DExpectQLiteralSsm * _make(DArena & parser_mm,
bool cxl_on_rightparen);
bool cxl_on_rightparen,
bool cxl_on_rightbracket);
/** start nested syntax for a quoted literal **/
static void start(ParserStateMachine * p_psm,
bool cxl_on_rightparen = false);
bool cxl_on_rightparen = false,
bool cxl_on_rightbracket = false);
/** @defgroup scm-expectformalarglistssm-methods general methods **/
///@{
@ -47,18 +51,38 @@ namespace xo {
void on_f64_token(const Token & tk,
ParserStateMachine * p_psm);
/** update state on incoming token @p tk, with overall parser state in @p p_psm **/
/** update state on incoming token @p tk,
* with overall parser state in @p p_psm.
*
* Forward in-place to ExpectQListSsm.
**/
void on_leftparen_token(const Token & tk,
ParserStateMachine * p_psm);
/** update state on incoming token @p tk, with overall parser state in @p p_psm **/
void on_comma_token(const Token & tk,
ParserStateMachine * p_psm);
/** update state on incoming rightparen token @p tk, with overall parser state in @p p_psm **/
/** update state on incoming rightparen token @p tk,
* with overall parser state in @p p_psm
*
* Error unless @ref cxl_on_rightparen_
**/
void on_rightparen_token(const Token & tk,
ParserStateMachine * p_psm);
/** update state on incoming leftbracket token @p tk,
* with overall parser state in @p p_psm.
*
* Forward in-place to ExpectQArraySsm
**/
void on_leftbracket_token(const Token &tk,
ParserStateMachine * p_psm);
/** update state on incoming rightbracket token @p tk,
* with overall parser state in @p p_psm.
*
* Error unless @ref cxl_on_rightbracket_
**/
void on_rightbracket_token(const Token & tk,
ParserStateMachine * p_psm);
///@}
/** @defgroup scm-expectformalarglistssm-ssm-facet syntaxstatemachine facet methods **/
///@{
@ -101,6 +125,8 @@ namespace xo {
private:
/** if true rightparen pops + delegates to parent ssm **/
bool cxl_on_rightparen_ = false;
/** if true rightbracket pops + delegates to parent ssm **/
bool cxl_on_rightbracket_ = false;
};
} /*namespace scm*/
} /*namespace xo*/

View file

@ -0,0 +1,12 @@
/** @file ExpectQArraySsm.hpp
*
* @author Roland Conybeare, Mar 2026
**/
#pragma once
#include "DExpectQArraySsm.hpp"
#include "ssm/ISyntaxStateMachine_DExpectQArraySsm.hpp"
#include "ssm/IPrintable_DExpectQArraySsm.hpp"
/* end ExpectQArraySsm.hpp */

View file

@ -0,0 +1,62 @@
/** @file IPrintable_DExpectQArraySsm.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IPrintable_DExpectQArraySsm.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_repr.hpp.j2]
* 3. idl for facet methods
* [idl/IPrintable_DExpectQArraySsm.json5]
**/
#pragma once
#include "Printable.hpp"
#include <xo/printable2/Printable.hpp>
#include <xo/printable2/detail/IPrintable_Xfer.hpp>
#include "DExpectQArraySsm.hpp"
namespace xo { namespace scm { class IPrintable_DExpectQArraySsm; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::print::APrintable,
xo::scm::DExpectQArraySsm>
{
using ImplType = xo::print::IPrintable_Xfer
<xo::scm::DExpectQArraySsm,
xo::scm::IPrintable_DExpectQArraySsm>;
};
}
}
namespace xo {
namespace scm {
/** @class IPrintable_DExpectQArraySsm
**/
class IPrintable_DExpectQArraySsm {
public:
/** @defgroup scm-printable-dexpectqarrayssm-type-traits **/
///@{
using ppindentinfo = xo::print::APrintable::ppindentinfo;
using Copaque = xo::print::APrintable::Copaque;
using Opaque = xo::print::APrintable::Opaque;
///@}
/** @defgroup scm-printable-dexpectqarrayssm-methods **/
///@{
// const methods
/** Pretty-printing support for this object.
See [xo-indentlog/xo/indentlog/pretty.hpp] **/
static bool pretty(const DExpectQArraySsm & self, const ppindentinfo & ppii);
// non-const methods
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

@ -0,0 +1,82 @@
/** @file ISyntaxStateMachine_DExpectQArraySsm.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/ISyntaxStateMachine_DExpectQArraySsm.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_repr.hpp.j2]
* 3. idl for facet methods
* [idl/ISyntaxStateMachine_DExpectQArraySsm.json5]
**/
#pragma once
#include "SyntaxStateMachine.hpp"
#include "SyntaxStateMachine.hpp"
#include "ssm/ISyntaxStateMachine_Xfer.hpp"
#include "DExpectQArraySsm.hpp"
namespace xo { namespace scm { class ISyntaxStateMachine_DExpectQArraySsm; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::scm::ASyntaxStateMachine,
xo::scm::DExpectQArraySsm>
{
using ImplType = xo::scm::ISyntaxStateMachine_Xfer
<xo::scm::DExpectQArraySsm,
xo::scm::ISyntaxStateMachine_DExpectQArraySsm>;
};
}
}
namespace xo {
namespace scm {
/** @class ISyntaxStateMachine_DExpectQArraySsm
**/
class ISyntaxStateMachine_DExpectQArraySsm {
public:
/** @defgroup scm-syntaxstatemachine-dexpectqarrayssm-type-traits **/
///@{
using TypeDescr = xo::scm::ASyntaxStateMachine::TypeDescr;
using AGCObject = xo::scm::ASyntaxStateMachine::AGCObject;
using Copaque = xo::scm::ASyntaxStateMachine::Copaque;
using Opaque = xo::scm::ASyntaxStateMachine::Opaque;
///@}
/** @defgroup scm-syntaxstatemachine-dexpectqarrayssm-methods **/
///@{
// const methods
/** identify a type of syntax state machine **/
static syntaxstatetype ssm_type(const DExpectQArraySsm & self) noexcept;
/** text describing expected/allowed input to this ssm in current state **/
static std::string_view get_expect_str(const DExpectQArraySsm & self) noexcept;
// non-const methods
/** operate state machine for incoming token @p tk **/
static void on_token(DExpectQArraySsm & self, const Token & tk, ParserStateMachine * p_psm);
/** update stat machine for incoming parsed symbol @p sym **/
static void on_parsed_symbol(DExpectQArraySsm & self, std::string_view sym, ParserStateMachine * p_psm);
/** operate state machine for incoming type description @p td **/
static void on_parsed_typedescr(DExpectQArraySsm & self, TypeDescr td, ParserStateMachine * p_psm);
/** operate state machine for formal emitted by nested ssm **/
static void on_parsed_formal(DExpectQArraySsm & self, const DUniqueString * param_name, TypeDescr param_type, ParserStateMachine * p_psm);
/** operate state machine for formal emitted by nested ssm **/
static void on_parsed_formal_with_token(DExpectQArraySsm & self, const DUniqueString * param_name, TypeDescr param_type, const Token & tk, ParserStateMachine * p_psm);
/** consume formal arglist emitted by nested ssm **/
static void on_parsed_formal_arglist(DExpectQArraySsm & self, DArray * arglist, ParserStateMachine * p_psm);
/** update state machine for nested parsed expression @p expr **/
static void on_parsed_expression(DExpectQArraySsm & self, obj<AExpression> expr, ParserStateMachine * p_psm);
/** update state machine @p p_psm for incoming parsed expression @p expr followed by token @p tk **/
static void on_parsed_expression_with_token(DExpectQArraySsm & self, obj<AExpression> expr, const Token & tk, ParserStateMachine * p_psm);
/** update state machine for nested quoted literal @p lit **/
static void on_quoted_literal(DExpectQArraySsm & self, obj<AGCObject> lit, ParserStateMachine * p_psm);
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

@ -63,10 +63,13 @@ namespace xo {
/** expecting a quoted literal. See @ref DExpectQLiteralSsm **/
expect_qliteral,
/** expecint a quoted list. See @ref DExpectQListSsm **/
/** expecting a quoted list. See @ref DExpectQListSsm **/
expect_qlist,
/** comes lasts, counts number of valid enums **/
/** expecting a quoted array. See @ref DExpectQArraySsm **/
expect_qarray,
/** comes last, counts number of valid enums **/
N
};