xo-reader2: parse list types + utest

This commit is contained in:
Roland Conybeare 2026-03-11 14:13:48 -05:00
commit 46637ec16a
19 changed files with 787 additions and 34 deletions

View file

@ -16,13 +16,13 @@ namespace xo {
* Already in quoted-literal context
*
* [ quote-expr ... ]
* ^ ^ ^
* | qarray_1a qarray_2(done)
* qarray_0
* ^ ^ ^ ^
* | qarray_1a | qarray_2(done)
* qarray_0 qarray_1a
*
* qarray_0 --on_leftbrace_token()--> qarray_1a [push ExpectQLiteralSsm]
* qarray_0 --on_leftbracket_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]
* qarray_1a --on_rightbracket_token()--> qarray_2(done) [report quoted array]
**/
class QArrayXst {
public:

View file

@ -14,12 +14,14 @@ namespace xo {
/** @class DExpectTypeSsm
* @brief syntax state-machine for accepting a Schemtika typename-expression
*
* Jan 2026
* Placeholder implementation at present.
* Only types are a handful of baked-in values
*
* @pre
*
* any universal polymorphic type
* unit empty type (like c/c++ void)
* f64 64-bit float
* i32 32-bit signed integer
* list<T> parameterized list (polymorphic if T is any)
* array<T> parameterized array (polymorphic if T is any)
* function<T (U,..)> function (U x ...) -> T
* @endpre
**/
class DExpectTypeSsm : public DSyntaxStateMachine<DExpectTypeSsm> {
@ -30,17 +32,26 @@ namespace xo {
using ppindentinfo = xo::print::ppindentinfo;
public:
/** @defgroup scm-expecttype-ctors constructors **/
///@{
explicit DExpectTypeSsm(bool corrected);
/** create instance **/
static DExpectTypeSsm * _make(DArena & parser_mm, bool corrected);
/** create fop referring to new DExpectTypeSsm **/
static obj<ASyntaxStateMachine,DExpectTypeSsm> make(DArena & parser_mm, bool corrected);
///@}
/** @defgroup scm-expecttype-general-methods general methods **/
///@{
static void start(bool corrected, ParserStateMachine * p_psm);
static const char * ssm_classname() { return "DExpectTypeSsm"; }
///@}
/** @defgroup scm-expecttype-ssm-facet syntaxstatemachine facet methods **/
///@{

View file

@ -0,0 +1,12 @@
/** @file ExpectListTypeSsm.hpp
*
* @author Roland Conybeare, Mar 2026
**/
#pragma once
#include "expect_listtype/DExpectListTypeSsm.hpp"
#include "expect_listtype/ISyntaxStateMachine_DExpectListTypeSsm.hpp"
#include "expect_listtype/IPrintable_DExpectListTypeSsm.hpp"
/* end ExpectListTypeSsm.hpp */

View file

@ -0,0 +1,149 @@
/** @file DExpectListTypeSsm.hpp
*
* @author Roland Conybeare, Mar 2026
**/
#pragma once
#include "DSyntaxStateMachine.hpp"
namespace xo {
namespace scm {
/**
* list < type-expr >
* ^ ^ ^ ^ ^
* | | | | (done)
* | | | type_3 --on_rightangle_token() --> (done) [pop + report listtype]
* | | type_2 --on_parsed_type()--> type_3
* | type_1 --on_leftangle_token() --> type_2 [push ExpectTypeSsm]
* type_0 --on_symbol_token()--> type_1
*
**/
class ListTypeXst {
public:
enum class code {
invalid = -1,
type_0,
type_1,
type_2,
type_3,
N,
};
explicit ListTypeXst(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, ListTypeXst x) {
os << ListTypeXst::_descr(x.code_);
return os;
}
/** @class DExpectListTypeSsm
* @brief parser state-machine for a list type
*
* Examples:
* @pre
* list<i32>
* list<list<bool>>
* @endpre
**/
class DExpectListTypeSsm : public DSyntaxStateMachine<DExpectListTypeSsm> {
public:
using Super = DSyntaxStateMachine<DExpectListTypeSsm>;
using TypeDescr = xo::reflect::TypeDescr;
using DArena = xo::mm::DArena;
using ppindentinfo = xo::print::ppindentinfo;
public:
/** default ctor **/
explicit DExpectListTypeSsm();
/** create instance. Get memory from @p parser_mm **/
static DExpectListTypeSsm * _make(DArena & parser_mm);
/** create instance as fop. Get memory from @p parser_mm **/
static obj<ASyntaxStateMachine, DExpectListTypeSsm> make(DArena & parser_mm);
/** start nested state machine to parse a list type **/
static void start(ParserStateMachine * p_psm);
/** @defgroup scm-expectlisttypessm-expression-methods general methods **/
///@{
static const char * ssm_classname() { return "DExpectListTypeSsm"; }
syntaxstatetype ssm_type() const noexcept;
std::string_view get_expect_str() const noexcept;
/** update ssm for symbol token @p tk, with overall parsing state in @p p_psm.
* in state type_0: advance to type_1.
* otherwise error
**/
void on_symbol_token(const Token & tk,
ParserStateMachine * p_psm);
/** update ssm for leftangle (<) token @p tk, with overall parser state in @p p_psm.
* in state type_1: advance to type_2, pushing nested ExpectTypeSsm.
* otherwise error.
**/
void on_leftangle_token(const Token & tk,
ParserStateMachine * p_psm);
/** update ssm for rightangle (>) token @p tk, with overall parser state in @p p_psm.
* in state type_3: aseemble list type, report completed syntax to parent ssm
**/
void on_rightangle_token(const Token & tk,
ParserStateMachine * p_psm);
///@}
/** @defgroup scm-expectlisttypessm-expression-facet expression facet methods **/
///@{
/** update ssm for token @p tk, with overall state in @p p_psm **/
void on_token(const Token & tk,
ParserStateMachine * p_psm);
/** update ssm for type @p type produced by nested ssm, with overall parser
* state in @p p_psm.
* in state type_2: @p type is element type for target list type.
* otherwise error.
**/
void on_parsed_type(obj<AType> type, ParserStateMachine * p_psm);
///@}
/** @defgroup scm-expectlisttypessm-printable-facet printable facet methods **/
///@{
/** pretty-printing support **/
bool pretty(const ppindentinfo & ppii) const;
///@}
private:
/** @defgroup scm-expectlisttypessm-instance-vars instance variables **/
///@{
/** local parsing state **/
ListTypeXst state_{ListTypeXst::code::type_0};
/** element type for target list type **/
obj<AType> elt_type_;
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end DExpectListTypeSsm.hpp */

View file

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

View file

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

View file

@ -85,14 +85,14 @@ namespace xo {
obj<AAllocator> expr_mm,
ParserStateMachine * p_psm);
static const char * ssm_classname() { return "DIfElseSsm"; }
DSyntaxStateMachine<DIfElseSsm> * super() { return this; }
///@}
/** @defgroup scm-ifelsessm-expression-methods general methods **/
///@{
static const char * ssm_classname() { return "DIfElseSsm"; }
DSyntaxStateMachine<DIfElseSsm> * super() { return this; }
/** operate state machine on if-token input @p tk,
* with overall parser state in @p p_psm
**/
@ -178,6 +178,9 @@ namespace xo {
#endif
private:
/** @defgroup scm-expectlisttypessm-instance-vars instance variables **/
///@{
ifexprstatetype ifstate_ = ifexprstatetype::invalid;
/** scaffold ifelse-expression here.
* This will eventually be the output of this ssm
@ -186,6 +189,8 @@ namespace xo {
**/
obj<AExpression,DIfElseExpr> if_expr_;
///@}
};
} /*namespace scm*/
} /*namespace xo*/

View file

@ -60,6 +60,9 @@ namespace xo {
/** expecting a type. See @ref DExpectTypeSsm **/
expect_type,
/** expecting a list type. See @ref DExpectListTypeSsm **/
expect_listtype,
/** expecting a rhs expression. See @ref DExpectExprSsm **/
expect_rhs_expression,