xo-interpreter2 stack: parse literal lists (w/ implicit types)
This commit is contained in:
parent
906bb2a913
commit
f2a9aa3f52
19 changed files with 1052 additions and 99 deletions
155
include/xo/reader2/DExpectQListSsm.hpp
Normal file
155
include/xo/reader2/DExpectQListSsm.hpp
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
/** @file DExpectQListSsm.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 ... )
|
||||
* ^ ^ ^
|
||||
* | qlist_1a qlist_2(done)
|
||||
* qlist_0
|
||||
*
|
||||
* qlist_0 --on_leftparen_token()--> qlist_1a [push ExpectQLiteralSsm]
|
||||
* qlist_1a --on_quoted_literal()--> qlist_1a [append literal]
|
||||
* qlist_1a --on_rightparen_token()--> qlist_2(done) [report quoted list]
|
||||
**/
|
||||
class QListXst {
|
||||
public:
|
||||
enum class code {
|
||||
invalid = -1,
|
||||
|
||||
qlist_0,
|
||||
qlist_1a,
|
||||
qlist_2,
|
||||
|
||||
N
|
||||
};
|
||||
|
||||
explicit QListXst(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, QListXst x) {
|
||||
os << QListXst::_descr(x.code_);
|
||||
return os;
|
||||
}
|
||||
|
||||
/** @class DExpectQListSsm
|
||||
* @brief parser state-machine for a formal parameter list
|
||||
**/
|
||||
class DExpectQListSsm : public DSyntaxStateMachine<DExpectQListSsm> {
|
||||
public:
|
||||
using Super = DSyntaxStateMachine<DExpectQListSsm>;
|
||||
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-expectqlistssm-ctors constructors **/
|
||||
///@{
|
||||
|
||||
DExpectQListSsm();
|
||||
|
||||
/** create instance, using memory from @parser_mm **/
|
||||
static obj<ASyntaxStateMachine,DExpectQListSsm> make(DArena & parser_mm);
|
||||
static DExpectQListSsm * _make(DArena & parser_mm);
|
||||
|
||||
/** start nested syntax for a quoted literal **/
|
||||
static void start(ParserStateMachine * p_psm);
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-expectformalarglistssm-methods general methods **/
|
||||
///@{
|
||||
|
||||
static const char * ssm_classname() { return "DExpectQListSsm"; }
|
||||
|
||||
/** update state on incoming token @p tk, with overall parser state in @p p_psm **/
|
||||
void on_leftparen_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
void on_rightparen_token(const Token & tk,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-expectqlistssm-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 list
|
||||
**/
|
||||
void on_quoted_literal(obj<AGCObject> lit,
|
||||
ParserStateMachine * p_psm);
|
||||
|
||||
///@}
|
||||
/** @defgroup scm-expectqlistssm-printable-facet printable facet methods **/
|
||||
///@{
|
||||
|
||||
bool pretty(const ppindentinfo & ppii) const;
|
||||
|
||||
///@}
|
||||
|
||||
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 **/
|
||||
///@{
|
||||
|
||||
/** identifies qlist parsing state **/
|
||||
QListXst state_;
|
||||
|
||||
/** first node in literal list **/
|
||||
DList * start_ = nullptr;
|
||||
/** last node in literal list **/
|
||||
DList * end_ = nullptr;
|
||||
|
||||
///@}
|
||||
};
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end DExpectQListSsm.hpp */
|
||||
|
|
@ -11,37 +11,7 @@
|
|||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
#ifdef NOT_USING
|
||||
/**
|
||||
* Already in quoted-literal context
|
||||
*
|
||||
* #q{ }
|
||||
* ^
|
||||
* qliteral_0
|
||||
*
|
||||
* qliteral_0 --on_leftparen_token()--> push QuotedListSsm
|
||||
**/
|
||||
enum class formalarglstatetype {
|
||||
invalid = -1,
|
||||
|
||||
argl_0,
|
||||
argl_1a,
|
||||
argl_1b,
|
||||
|
||||
n_formalarglstatetype,
|
||||
};
|
||||
|
||||
extern const char *
|
||||
formalarglstatetype_descr(formalarglstatetype x);
|
||||
|
||||
inline std::ostream &
|
||||
operator<< (std::ostream & os, formalarglstatetype x) {
|
||||
os << formalarglstatetype_descr(x);
|
||||
return os;
|
||||
}
|
||||
#endif
|
||||
|
||||
/** @class expect_formal_arglist
|
||||
/** @class DExpectQLiteralSsm
|
||||
* @brief parser state-machine for a formal parameter list
|
||||
**/
|
||||
class DExpectQLiteralSsm : public DSyntaxStateMachine<DExpectQLiteralSsm> {
|
||||
|
|
@ -54,14 +24,17 @@ namespace xo {
|
|||
using size_type = std::uint32_t;
|
||||
|
||||
public:
|
||||
DExpectQLiteralSsm();
|
||||
explicit DExpectQLiteralSsm(bool cxl_on_rightparen);
|
||||
|
||||
/** create instance, using memory from @parser_mm **/
|
||||
static obj<ASyntaxStateMachine,DExpectQLiteralSsm> make(DArena & parser_mm);
|
||||
static DExpectQLiteralSsm * _make(DArena & parser_mm);
|
||||
static obj<ASyntaxStateMachine,DExpectQLiteralSsm> make(DArena & parser_mm,
|
||||
bool cxl_on_rightparen);
|
||||
static DExpectQLiteralSsm * _make(DArena & parser_mm,
|
||||
bool cxl_on_rightparen);
|
||||
|
||||
/** start nested syntax for a quoted literal **/
|
||||
static void start(ParserStateMachine * p_psm);
|
||||
static void start(ParserStateMachine * p_psm,
|
||||
bool cxl_on_rightparen = false);
|
||||
|
||||
/** @defgroup scm-expectformalarglistssm-methods general methods **/
|
||||
///@{
|
||||
|
|
@ -126,16 +99,8 @@ namespace xo {
|
|||
///@}
|
||||
|
||||
private:
|
||||
#ifdef NOT_USING
|
||||
/** parsing state-machine state **/
|
||||
formalarglstatetype fastate_ = formalarglstatetype::argl_0;
|
||||
/** populate with (parameter-name, parameter-type) list
|
||||
* as they're encountered.
|
||||
*
|
||||
* Not using flexible array here since we don't know size at construction time
|
||||
**/
|
||||
DArray * argl_ = nullptr;
|
||||
#endif
|
||||
/** if true rightparen pops + delegates to parent ssm **/
|
||||
bool cxl_on_rightparen_ = false;
|
||||
};
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
|
|
|||
|
|
@ -32,6 +32,18 @@ namespace xo {
|
|||
using TypeDescr = xo::reflect::TypeDescr;
|
||||
using AGCObject = xo::mm::AGCObject;
|
||||
|
||||
/** Explicit error path **/
|
||||
void illegal_token(const Token & tk,
|
||||
ParserStateMachine * p_psm)
|
||||
{
|
||||
// starting with c++23 can use "this auto&& self" instead
|
||||
Derived & self = reinterpret_cast<Derived&>(*this);
|
||||
|
||||
p_psm->illegal_input_on_token(Derived::ssm_classname(),
|
||||
tk,
|
||||
self.get_expect_str());
|
||||
}
|
||||
|
||||
/** Explicit error path **/
|
||||
void illegal_quoted_literal(obj<AGCObject> lit,
|
||||
ParserStateMachine * p_psm)
|
||||
|
|
@ -49,12 +61,7 @@ namespace xo {
|
|||
void on_token(const Token & tk,
|
||||
ParserStateMachine * p_psm)
|
||||
{
|
||||
// starting with c++23 can use "this auto&& self" instead
|
||||
Derived & self = reinterpret_cast<Derived&>(*this);
|
||||
|
||||
p_psm->illegal_input_on_token(Derived::ssm_classname(),
|
||||
tk,
|
||||
self.get_expect_str());
|
||||
this->illegal_token(tk, p_psm);
|
||||
}
|
||||
|
||||
void on_parsed_symbol(std::string_view sym,
|
||||
|
|
|
|||
12
include/xo/reader2/ExpectQListSsm.hpp
Normal file
12
include/xo/reader2/ExpectQListSsm.hpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
/** @file ExpectQListSsm.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Mar 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "DExpectQListSsm.hpp"
|
||||
#include "ssm/ISyntaxStateMachine_DExpectQListSsm.hpp"
|
||||
#include "ssm/IPrintable_DExpectQListSsm.hpp"
|
||||
|
||||
/* end ExpectQListSsm.hpp */
|
||||
62
include/xo/reader2/ssm/IPrintable_DExpectQListSsm.hpp
Normal file
62
include/xo/reader2/ssm/IPrintable_DExpectQListSsm.hpp
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
/** @file IPrintable_DExpectQListSsm.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/IPrintable_DExpectQListSsm.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_repr.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/IPrintable_DExpectQListSsm.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Printable.hpp"
|
||||
#include <xo/printable2/Printable.hpp>
|
||||
#include <xo/printable2/detail/IPrintable_Xfer.hpp>
|
||||
#include "DExpectQListSsm.hpp"
|
||||
|
||||
namespace xo { namespace scm { class IPrintable_DExpectQListSsm; } }
|
||||
|
||||
namespace xo {
|
||||
namespace facet {
|
||||
template <>
|
||||
struct FacetImplementation<xo::print::APrintable,
|
||||
xo::scm::DExpectQListSsm>
|
||||
{
|
||||
using ImplType = xo::print::IPrintable_Xfer
|
||||
<xo::scm::DExpectQListSsm,
|
||||
xo::scm::IPrintable_DExpectQListSsm>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @class IPrintable_DExpectQListSsm
|
||||
**/
|
||||
class IPrintable_DExpectQListSsm {
|
||||
public:
|
||||
/** @defgroup scm-printable-dexpectqlistssm-type-traits **/
|
||||
///@{
|
||||
using ppindentinfo = xo::print::APrintable::ppindentinfo;
|
||||
using Copaque = xo::print::APrintable::Copaque;
|
||||
using Opaque = xo::print::APrintable::Opaque;
|
||||
///@}
|
||||
/** @defgroup scm-printable-dexpectqlistssm-methods **/
|
||||
///@{
|
||||
// const methods
|
||||
/** Pretty-printing support for this object.
|
||||
See [xo-indentlog/xo/indentlog/pretty.hpp] **/
|
||||
static bool pretty(const DExpectQListSsm & self, const ppindentinfo & ppii);
|
||||
|
||||
// non-const methods
|
||||
///@}
|
||||
};
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end */
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
/** @file ISyntaxStateMachine_DExpectQListSsm.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/ISyntaxStateMachine_DExpectQListSsm.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_repr.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/ISyntaxStateMachine_DExpectQListSsm.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "SyntaxStateMachine.hpp"
|
||||
#include "SyntaxStateMachine.hpp"
|
||||
#include "ssm/ISyntaxStateMachine_Xfer.hpp"
|
||||
#include "DExpectQListSsm.hpp"
|
||||
|
||||
namespace xo { namespace scm { class ISyntaxStateMachine_DExpectQListSsm; } }
|
||||
|
||||
namespace xo {
|
||||
namespace facet {
|
||||
template <>
|
||||
struct FacetImplementation<xo::scm::ASyntaxStateMachine,
|
||||
xo::scm::DExpectQListSsm>
|
||||
{
|
||||
using ImplType = xo::scm::ISyntaxStateMachine_Xfer
|
||||
<xo::scm::DExpectQListSsm,
|
||||
xo::scm::ISyntaxStateMachine_DExpectQListSsm>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @class ISyntaxStateMachine_DExpectQListSsm
|
||||
**/
|
||||
class ISyntaxStateMachine_DExpectQListSsm {
|
||||
public:
|
||||
/** @defgroup scm-syntaxstatemachine-dexpectqlistssm-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-dexpectqlistssm-methods **/
|
||||
///@{
|
||||
// const methods
|
||||
/** identify a type of syntax state machine **/
|
||||
static syntaxstatetype ssm_type(const DExpectQListSsm & self) noexcept;
|
||||
/** text describing expected/allowed input to this ssm in current state **/
|
||||
static std::string_view get_expect_str(const DExpectQListSsm & self) noexcept;
|
||||
|
||||
// non-const methods
|
||||
/** operate state machine for incoming token @p tk **/
|
||||
static void on_token(DExpectQListSsm & self, const Token & tk, ParserStateMachine * p_psm);
|
||||
/** update stat machine for incoming parsed symbol @p sym **/
|
||||
static void on_parsed_symbol(DExpectQListSsm & self, std::string_view sym, ParserStateMachine * p_psm);
|
||||
/** operate state machine for incoming type description @p td **/
|
||||
static void on_parsed_typedescr(DExpectQListSsm & self, TypeDescr td, ParserStateMachine * p_psm);
|
||||
/** operate state machine for formal emitted by nested ssm **/
|
||||
static void on_parsed_formal(DExpectQListSsm & 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(DExpectQListSsm & 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(DExpectQListSsm & self, DArray * arglist, ParserStateMachine * p_psm);
|
||||
/** update state machine for nested parsed expression @p expr **/
|
||||
static void on_parsed_expression(DExpectQListSsm & 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(DExpectQListSsm & self, obj<AExpression> expr, const Token & tk, ParserStateMachine * p_psm);
|
||||
/** update state machine for nested quoted literal @p lit **/
|
||||
static void on_quoted_literal(DExpectQListSsm & self, obj<AGCObject> lit, ParserStateMachine * p_psm);
|
||||
///@}
|
||||
};
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end */
|
||||
|
|
@ -63,6 +63,9 @@ namespace xo {
|
|||
/** expecting a quoted literal. See @ref DExpectQLiteralSsm **/
|
||||
expect_qliteral,
|
||||
|
||||
/** expecint a quoted list. See @ref DExpectQListSsm **/
|
||||
expect_qlist,
|
||||
|
||||
/** comes lasts, counts number of valid enums **/
|
||||
N
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue